DOC PREVIEW
Princeton COS 217 - Debugging

This preview shows page 1-2-16-17-18-34-35 out of 35 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

DebuggingGoals of this LectureTesting vs. DebuggingDebugging HeuristicsUnderstand Error MessagesUnderstand Error Messages (cont.)Slide 7Think Before WritingLook for Familiar BugsDivide and ConquerDivide and Conquer (cont.)Add More Internal TestsDisplay OutputDisplay Output (cont.)Use a DebuggerUsing GDBUsing GDB (cont.)Slide 18Slide 19Slide 20Slide 21Focus on Recent ChangesMaintaining Previous VersionsRCSUsing RCSUsing RCS (cont.)Slide 27SummaryAppendix: Debugging Mem MgmtThe Rest of This WeekSlide 31Slide 32Slide 33Slide 34Slide 351DebuggingThe material for this lecture is drawn, in part, fromThe Practice of Programming (Kernighan & Pike) Chapter 5Professor Jennifer Rexfordhttp://www.cs.princeton.edu/~jrex2Goals of this Lecture•Help you learn about:•Strategies for debugging your code•The GDB debugger•The RCS version control system•Why?•Debugging large programs can be difficult•A power programmer knows a wide variety of debugging strategies•A power programmer knows about tools that facilitate debugging•Debuggers•Version control systems3Testing vs. Debugging•Testing•What should I do to try to break my program?•Debugging•What should I do to try to fix my program?4Debugging HeuristicsDebugging Heuristic When Applicable(1) Understand error messages Build-time(2) Think before writingRun-time(3) Look for familiar bugs(4) Divide and conquer(5) Add more internal tests(6) Display output(7) Use a debugger(8) Focus on recent changes5Understand Error MessagesDebugging at build-time is easier than debugging at run-time, if and only if you…(1) Understand the error messages!!!•Some are from the preprocessor#include <stdioo.h>int main(void)/* Print "hello, world" to stdout and return 0. { printf("hello, world\n"); return 0;}$ gcc217 hello.c -o hellohello.c:1:20: stdioo.h: No such file or directoryhello.c:3:1: unterminated commenthello.c:2: error: syntax error at end of inputMisspelled #include fileMissing */6Understand Error Messages (cont.)(1) Understand the error messages (cont.)•Some are from the compiler#include <stdio.h>int main(void)/* Print "hello, world" to stdout and return 0. */ { printf("hello, world\n") retun 0;}$ gcc217 hello.c -o hellohello.c: In function `main':hello.c:7: error: `retun' undeclared (first use in this function)hello.c:7: error: (Each undeclared identifier is reported only oncehello.c:7: error: for each function it appears in.)hello.c:7: error: syntax error before numeric constantMisspelled keyword7Understand Error Messages (cont.)(1) Understand error messages (cont.)•Some are from the linker#include <stdio.h>int main(void)/* Print "hello, world" to stdout and return 0. */ { prinf("hello, world\n") return 0;}$ gcc217 hello.c -o hellohello.c: In function `main':hello.c:6: warning: implicit declaration of function `prinf'/tmp/cc43ebjk.o(.text+0x25): In function `main':: undefined reference to `prinf'collect2: ld returned 1 exit statusMisspelled function nameCompiler warning (not error): prinf() is called before declaredLinker error: Cannot find definition of prinf()8Think Before WritingInappropriate changes could make matters worse, so…(2) Think before writing•Draw pictures of the data structures•Update pictures as the algorithms change data structures•Take a break•Sleep on it!•Start early so you can!!!•Explain the code to:•Yourself•Someone else•A teddy bear•A giant wookie9Look for Familiar Bugs(3) Look for familiar bugs•Some of our favorites:int i;…scanf("%d", i);char c;…c = getchar();switch (i) { case 0: … /* missing break */ case 1: … break; …}if (i = 5) …if (5 < i < 10) …if (i & j) …while (c = getchar() != EOF) …Note: enabling warnings will catch some (but not all) of these10Divide and Conquer(4) Divide and conquer•Eliminate input•Incrementally find smallest/simplest input that illustrates the bug•Example: Program fails on large input file filex•Make copy of filex named filexcopy•Delete 2nd half of filexcopy•Run program on filexcopy•Program works => 1st half of filex does not illustrate bug, so discard 1st half and keep 2nd half•Program fails => 1st half of filex illustrates bug, so keep 1st half and discard 2nd half•Recurse until no lines of filex can be discarded•Alternative: Start with small subset of filex, and incrementally add lines until bug appears11Divide and Conquer (cont.)(4) Divide and conquer (cont.)•Eliminate code•Incrementally find smallest code subset that illustrates the bug•Example: Test client for your module fails•In test client, comment out calls of some function•Or in your module, create stub definition of some function•Run test client•Bug disappears => it’s in commented-out code•Bug remains => it’s in remaining code, so repeat for another function12Add More Internal Tests(5) Add more internal tests•(See “Testing” lecture)•Internal tests help finding bugs•Internal test also can help eliminate bugs•Checking invariants and conservation properties can eliminate some functions from the bug hunt13Display Output(6) Display output•Print values of important variables at critical spots•Poor:•Maybe better:•Better:printf("%d", keyvariable);stdout is buffered; program may crash before output appearsprintf("%d", keyvariable);fflush(stdout);printf("%d\n", keyvariable);Call fflush() to flush stdout buffer explicitlyPrinting '\n' flushes the stdout buffer, but not if stdout is redirected to a file14Display Output (cont.)(6) Display output (cont.)•Maybe even better:•Maybe better still:fprintf(stderr, "%d", keyvariable);FILE *fp = fopen("logfile", "w");…fprintf(fp, "%d", keyvariable);fflush(fp);Write debugging output to stderr; debugging output can be separated from normal output via redirectionWrite to a log fileBonus: stderr is unbuffered15Use a Debugger(7) Use a debugger•Bugs often are the result of a flawed mental model; debugger can help correct mental model•Sometimes (but not always) debugger is more convenient than inserting printing statements•Debugger can load “core dumps” and let you step through state of program when it died•Can “attach” to running programs to examine execution•The GDB Debugger•Part of the GNU development environment•Integrated with XEMACS editor16Using GDB•An example programFile testintmath.c:#include <stdio.h> int gcd(int i, int j) { int temp; while (j != 0) { temp = i % j; i = j; j =


View Full Document

Princeton COS 217 - Debugging

Documents in this Course
Summary

Summary

4 pages

Lecture

Lecture

4 pages

Generics

Generics

14 pages

Generics

Generics

16 pages

Lecture

Lecture

20 pages

Types

Types

7 pages

Lecture

Lecture

21 pages

Assembler

Assembler

16 pages

Lecture

Lecture

20 pages

Lecture

Lecture

39 pages

Testing

Testing

44 pages

Pipeline

Pipeline

19 pages

Lecture

Lecture

6 pages

Signals

Signals

67 pages

Building

Building

17 pages

Lecture

Lecture

7 pages

Modules

Modules

12 pages

Generics

Generics

16 pages

Testing

Testing

22 pages

Signals

Signals

34 pages

Lecture

Lecture

19 pages

Load more
Download Debugging
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Debugging and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Debugging 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?