Download presentation
Presentation is loading. Please wait.
Published byDrusilla Dennis Modified over 8 years ago
1
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu
2
Uninitialized pointers 2 str was never initialized to point to anything
3
Memory error diagnosis: valgrind 3 invoking the tool: valgrind progName arg1 arg2 … indicates there was a problem; helps narrow down where the problem arose
4
Dangling pointers We looked at this code earlier: 4 runtime stack main my_read read_string a c b a buf string str dangling pointer!
5
Dangling pointers Minor variation on this code: 5
6
Dangling pointers 6 the code seems to work!!! (on hedgehog.cs.arizona.edu)
7
Dangling pointers 7 doesn’t work (on lectura.cs.arizona.edu)
8
What’s going on? 8 runtime stack main my_read read_string a c b a a padding[ ] buf[ ] strlen the array padding[ ] “protects” buf[ ] from getting overwritten — so the code seems to work (on some machines )
9
More diagnosis 9
10
Summary Just because a program produces the expected output doesn’t mean that it’s correct – the observed behavior may be accidental – the observed behavior may be system-dependent Use valgrind to check whether the execution was free of memory errors – provides information only about one execution other executions may contain erroneous behaviors – provides some help in identifying where the error occurred. 10
11
Another example 11
12
Example 2 12 out of bounds memory access
13
Example 2 13 where the invalid memory access occurred, (incl. stack trace) where this memory was allocated (incl. stack trace)
14
Example 3 14 off-by-one problem fixed
15
Example 3 15
16
gdb: basic functionality Interactive debugger – allows the user to run a program and interactively examine its execution. Features include: breakpoints (“run until control reaches here, then prompt user”) stack backtrace (chain of calls leading to some point in the code) examination of program variables Usage: – compile program using gcc –g … – invoke the program as gdb prog (then supply arguments inside gdb) 16
17
Interactive debugging: gdb 17 expected behavior buggy behavior
18
gdb: example usage 18 invocation set a breakpoint in this case: at entry to main() start execution specify command-line arguments here execution reaches breakpoint and returns control to user move to next statement examine the program
19
gdb: Looking at the program 19 “list the program source around the line number specified”
20
gdb 20 set a breakpoint here
21
gdb 21 execution reaches breakpoint and returns control to user single-step through the execution
22
gdb 22 examine program state continue to next breakpoint
23
gdb 23
24
gdb: moving around the runtime stack 24 where did the Seg Fault occur? move up the stack (i.e., to the caller) to examine variable values
25
gdb: other features Gdb provides many other debugging features, e.g.: – conditional breakpoints “break execution at some point in the code and return control to the user if some condition holds” – watchpoints “break execution and return control to user if a variable is read or written’ – change the value of a variable in the program state See tutorials in the DOCS area of class website 25
26
gdb: reading commands from a file 26 input to the program to be debugged script of commands to gdb invoking gdb to read commands from script file
27
ddd: a GUI front end for GDB 27 program source gdb interaction common operations
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.