Download - CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
![Page 1: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/1.jpg)
CSc 352Debugging Tools
Saumya DebrayDept. of Computer Science
The University of Arizona, [email protected]
![Page 2: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/2.jpg)
Uninitialized pointers
2
str was never initialized to point to anything
![Page 3: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/3.jpg)
Memory error diagnosis: valgrind
3
invoking the tool: valgrind progName arg1 arg2
…
indicates there was a problem; helps narrow down where the problem arose
![Page 4: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/4.jpg)
Dangling pointersWe looked at this code earlier:
4
runtime stack
main
my_read
read_string
acba
buf
string
str
dangling pointer!
![Page 5: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/5.jpg)
Dangling pointers
Minor variation on this code:
5
![Page 6: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/6.jpg)
Dangling pointers
6
the code seems to work!!!(on hedgehog.cs.arizona.edu)
![Page 7: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/7.jpg)
Dangling pointers
7
doesn’t work(on lectura.cs.arizona.edu)
![Page 8: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/8.jpg)
What’s going on?
8
runtime stack
main
my_read
read_stringacba
a
padding[ ]
buf[ ]
strlen
the array padding[ ] “protects” buf[ ] from getting overwritten — so the code seems to work (on some machines)
![Page 9: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/9.jpg)
More diagnosis
9
![Page 10: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/10.jpg)
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
![Page 11: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/11.jpg)
Another example
11
![Page 12: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/12.jpg)
Example 2
12
out of bounds memory access
![Page 13: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/13.jpg)
Example 2
13
where the invalid memory access occurred, (incl. stack trace)
where this memory was allocated(incl. stack trace)
![Page 14: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/14.jpg)
Example 3
14
off-by-one problem fixed
![Page 15: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/15.jpg)
Example 3
15
![Page 16: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/16.jpg)
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
![Page 17: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/17.jpg)
Interactive debugging: gdb
17
expected behavior
buggy behavior
![Page 18: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/18.jpg)
gdb: example usage
18
invocation
set a breakpoint in this case: at entry to main()
start executionspecify command-line
arguments here
execution reaches breakpoint and returns
control to user
move to next statement
examine the program
![Page 19: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/19.jpg)
gdb: Looking at the program
19
“list the program source around the line number specified”
![Page 20: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/20.jpg)
gdb
20
set a breakpoint here
![Page 21: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/21.jpg)
gdb
21
execution reaches breakpoint and returns
control to user
single-step through the execution
![Page 22: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/22.jpg)
gdb
22
examine program state
continue to next breakpoint
![Page 23: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/23.jpg)
gdb
23
![Page 24: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/24.jpg)
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
![Page 25: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/25.jpg)
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
![Page 26: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/26.jpg)
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
![Page 27: CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson](https://reader036.vdocuments.net/reader036/viewer/2022062600/5a4d1b517f8b9ab0599a7d32/html5/thumbnails/27.jpg)
ddd: a GUI front end for GDB
27
program source
gdb interaction
common operations