08 Debugging1
08 Debugging1
Test
• Testing techniques (done)
Debug
• Debugging techniques & tools <-- we are here
Maintain
• Performance improvement techniques & tools
4
Goals of this Lecture
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 systems
5
Testing vs. Debugging
Testing
• What should I do to try to break my program?
Debugging
• What should I do to try to fix my program?
6
Agenda
(1) Understand error messages
(2) Think before writing
(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 changes
7
Understand Error Messages
Debugging at build-time is easier than debugging at run-
time, if and only if you…
Understand the error messages!
#include <stdioo.h>
/* Print "hello, world" to stdout and What are the
return 0.
errors? (No
int main(void)
{ printf("hello, world\n"); fair looking at
return 0; the next slide!)
}
8
Understand Error Messages
#include <stdioo.h>
/* Print "hello, world" to stdout and
return 0. Which tool
int main(void) (preprocessor,
{ printf("hello, world\n"); compiler, or
return 0; linker) reports
}
the error(s)?
9
Understand Error Messages
#include <stdio.h>
/* Print "hello, world" to stdout and
return 0. */ What are the
int main(void) errors? (No
{ printf("hello, world\n") fair looking at
return 0;
} the next slide!)
10
Understand Error Messages
#include <stdio.h>
/* Print "hello, world" to stdout and
Which tool
return 0. */
int main(void) (preprocessor,
{ printf("hello, world\n") compiler, or
return 0; linker) reports
}
the error?
11
Understand Error Messages
#include <stdio.h>
/* Print "hello, world" to stdout and
return 0. */ What are the
int main(void) errors? (No
{ prinf("hello, world\n"); fair looking at
return 0;
} the next slide!)
12
Understand Error Messages
#include <stdio.h>
/* Print "hello, world" to stdout and Which tool
return 0. */ (preprocessor,
int main(void)
{ prinf("hello, world\n")
compiler, or
return 0; linker) reports
} the error?
13
Understand Error Messages
#include <stdio.h>
#include <stdlib.h>
enum StateType What are the
{ STATE_REGULAR, errors? (No
STATE_INWORD fair looking at
}
int main(void) the next slide!)
{ printf("just hanging around\n");
return EXIT_SUCCESS;
}
14
Understand Error Messages
#include <stdio.h>
#include <stdlib.h>
enum StateType
{ STATE_REGULAR, What does
STATE_INWORD this error
} message even
int main(void)
{ printf("just hanging around\n");
mean?
return EXIT_SUCCESS;
}
15
Understand Error Messages
Caveats concerning error messages
• Line # in error message may be approximate
• Error message may seem nonsensical
• Compiler may not report the real error
16
Agenda
(1) Understand error messages
(2) Think before writing
(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 changes
17
Think Before Writing
Inappropriate changes could make matters worse, so…
18
Agenda
(1) Understand error messages
(2) Think before writing
(3) Look for common bugs
(4) Divide and conquer
(5) Add more internal tests
(6) Display output
(7) Use a debugger
(8) Focus on recent changes
19
Look for Common Bugs
Some of our favorites:
int i;
…
switch (i) scanf("%d", i);
{ case 0:
… char c;
break; …
case 1: c = getchar();
…
case 2:
… while (c = getchar() != EOF)
} …
if (i = 5)
…
What are
the
if (5 < i < 10) if (i & j)
… …
errors?
20
Look for Common Bugs
Some of our favorites:
21
Look for Common Bugs
Some of our favorites:
{ int i;
…
i = 5; What value is
if (something) written if this
{ int i;
statement is
…
i = 6; present? Absent?
…
}
…
printf("%d\n", i);
…
}
22
Agenda
(1) Understand error messages
(2) Think before writing
(3) Look for common bugs
(4) Divide and conquer
(5) Add more internal tests
(6) Display output
(7) Use a debugger
(8) Focus on recent changes
23
Divide and Conquer
Divide and conquer: To debug a program…
25
Agenda
(1) Understand error messages
(2) Think before writing
(3) Look for common bugs
(4) Divide and conquer
(5) Add more internal tests
(6) Display output
(7) Use a debugger
(8) Focus on recent changes
26
Add More Internal Tests
27
Agenda
(1) Understand error messages
(2) Think before writing
(3) Look for common bugs
(4) Divide and conquer
(5) Add more internal tests
(6) Display output
(7) Use a debugger
(8) Focus on recent changes
28
Display Output
Write values of important variables at critical spots
30
Agenda
(1) Understand error messages
(2) Think before writing
(3) Look for common bugs
(4) Divide and conquer
(5) Add more internal tests
(6) Display output
(7) Use a debugger
(8) Focus on recent changes
31
Use a Debugger
Use a debugger
32
The GDB Debugger
GNU Debugger
• Part of the GNU development environment
• Integrated with Emacs editor
• Allows user to:
• Run program
• Set breakpoints
• Step through code one line at a time
• Examine values of variables during run
• Etc.
34
Focus on Recent Changes
Focus on recent changes
Difficult: Easier:
35
Focus on Recent Changes
Focus on recent change (cont.)
Difficult: Easier:
36
Maintaining Old Versions
To maintain old versions…
Approach 1: Manually copy project directory
…
$ mkdir myproject
$ cd myproject
$ cd ..
$ cp –r myproject myprojectDateTime
$ cd myproject
37
Maintaining Old Versions
39
Summary
40
Appendix 1: Using GDB
An example program Euclid’s algorithm;
File testintmath.c: Don’t be concerned
with details
#include <stdio.h>
…
int gcd(int i, int j) int main(void)
{ int temp; { int iGcd;
while (j != 0) int iLcm;
{ temp = i % j; iGcd = gcd(8, 12);
i = j; iLcm = lcm(8, 12);
j = temp; printf("%d %d\n", iGcd, iLcm);
return 0;
}
return i; }
}
The program is correct
int lcm(int i, int j)
{ return (i / gcd(i, j)) * j;
} But let’s pretend it has a
… runtime error in gcd()…
41
Appendix 1: Using GDB
42
Appendix 1: Using GDB
Typical steps for using GDB:
46
Appendix 2: Using RCS
Typical steps for using RCS:
(a) Create project directory, as usual
mkdir helloproj
cd helloproj
(b) Create RCS directory in project directory
mkdir RCS
• RCS will store its repository in that directory
(c) Create source code files in project directory
emacs hello.c …
(d) Check in
ci hello.c
• Adds file to RCS repository
• Deletes local copy (don t panic!)
• Can provide description of file (1st time)
• Can provide log message, typically describing changes
47
Appendix 2: Using RCS
Typical steps for using RCS (cont.):
(e) Check out most recent version for reading
co hello.c
• Copies file from repository to project directory
• File in project directory has read-only permissions
(f) Check out most recent version for reading/writing
co –l hello.c
• Copies file from repository to project directory
• File in project directory has read/write permissions
(g) List versions in repository
rlog hello.c
• Shows versions of file, by number (1.1, 1.2, etc.), with
descriptions
(h) Check out a specified version
co –l –rversionnumber hello.c
48
Appendix 2: Using RCS
49