gdb - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

gdb

Description:

gdb. Lecture 24. CAAM 420. Fall 2004. Success on jungle being slow. jungle == slow. arcadien == fast ... While testing/writing code, many of you find it ... – PowerPoint PPT presentation

Number of Views:98
Avg rating:3.0/5.0
Slides: 25
Provided by: timwar1
Category:
Tags: gdb | jungle | watch

less

Transcript and Presenter's Notes

Title: gdb


1
gdb
  • Lecture 24
  • CAAM 420
  • Fall 2004

2
Success on jungle being slow
  • jungle slow
  • arcadien fast
  • le fast
  • se fast

3
gdb
  • While testing/writing code, many of you find it
    difficult to find the cause of incorrect output,
    or premature code exits.
  • gdb is the gnu debugger
  • To get full use out of gdb you must compile your
    code with the command line argument g
  • E.g. g -g c foo.cpp
  • will include debugging information for gdb in the
    created foo.o object.

4
Example
  • We first compile with g then we launch gdb and
    are now faced with a gdb prompt.
  • We are inside a program running under nix
  • This is NOT a nix prompt

5
Basic gdb Commands
  • You can use run to start executing the program
  • You can use break to insert breakpoints where
    execution will suspend
  • You can step through each executed line of code
    with step. This will step into any function
    called.
  • You can execute each line of code in a function
    with next. This will execute a function call
    without suspending execution, unless there is a
    break or activated watch point.

6
help
  • There is a built in help function

7
run
  • The first command you should try is run
  • Notice, gdb has run the code and everthing
    completed normally (see exit statement from gdb)

8
An Example Which Does Not Finish Cleanly
Code which forces a crash by out of bounds write
to an unguarded vector
9
Finding the Cause of Segmentation Fault
Compile with -g
Run
Launch gdb
Run inside gdb
gdb stops running and tells us that there was a
segmentation fault and where it happened
10
Examining the Variables at Crash Time
  • We can now use print to find out what the state
    of the variables was at the time of crash.
  • We can see that in this case we wrote 1525 values
    beyond the actual, last, legal, allocated entry
    in the array. sometimes writing one bad value
    will segfault

11
Seg. Fault Due to Writing to Unallocated Memory
  • Here we try to use a NULL pointer to write to
    memory..

12
Writing to Unallocated Memory
In this case the error reported is the same
phrase as before so it is not particularly
specific. gdb does show correct location of
violation.
13
Using watch in gdb
Type watch var For variable var
Start watch
a changed (initiated)
a changed set to 2.0
Watch deleted asvariable goes outof scope..
14
Using watch with a Conditional Watch Variable
  • Look at this code
  • We can watch to see when n2 becomes true.

15
Using watch with Conditional Statement
  • We can also set a watch which only activates when
    a logical condition changes between true and
    false.

Here I create a watch with a logical test
Setting n2 changesthe watch value
Setting n2 from n3changes the watch value
Setting n4 from n3-- no watch change
16
Using break to Suspend Execution
  • Typing
  • (gdb) break foo
  • instructs gdb to suspend execution inside
    function foo if it ever gets called while the
    code is executing.
  • Typing
  • (gdb) break 127
  • instructs gdb to suspend execution at line 127 in
    the current code file if it ever gets to that
    line.
  • Typing
  • (gdb) break foo.c63
  • instructs gdb to suspend execution at line 63 in
    foo.c if it is ever the next line to execute.

17
Using cont to resume execution
  • If you wish to resume the execution of the
    program after a break or watch has suspended
    execution type cont

18
up and down
  • Suppose you have stepped into a function, or
    suspended execution in a function with break or
    while
  • Say you want to see what was happening in its
    calling function then type up and you will be
    able to view the variables in the calling
    functions scope.
  • You can keep going up until you reach the main
    function.
  • You can go back down the function stack one
    function at a time by typing down

19
Tip 1 to Avoid Frustrating Mistakes
  • If you have change a class or struct description,
    then recompile everything that can possibly
    depend on the class.
  • If you do not then prototype assumed when the
    dependent code was compiled will be out of date,
    and the hard coded class and/or struct variables
    may be in a different location than was assumed.

20
Tip 2 Use Error Checking
  • Write your own error checking routines.
  • However, be careful when you write this code.
  • If you get it wrong it can tell you about
    errors which do not exist.

21
Tip 3 Not Every Memory Access Error Creates A
Seg. Fault
  • Notice the first 1000 or so out of bounds
    writes did not crash the mycrash1.cpp code.
  • i.e. you can go out of bounds, which is a bad,
    bad thing and the code can still execute.
    However, an out of bounds write can create
    unintentional alteration of other variables.

22
Tip 3 cont
  • Just in case you think this cant happen
  • I set a and b, and find their addresses.
  • Next I find out their separation in memory
  • Finally, I treat b as the first entry in an array
    and use the computed offset to accidentally
    change the value of a

23
Tip 3 cont
  • In action
  • skip2 implies that b and a are in consecutive
    positions in memory (why?).

24
Class Exercise
  • Use gdb to perform the following
  • Download mygdbex1.c
  • Without modifying the code, find out what value
    of n causes the function myfn to return 12
  • Download mygdbex2.c
  • Find out where the code crashes, fix any obvious
    bugs which causes the code to crash. i.e. why
    does the code crash?
Write a Comment
User Comments (0)
About PowerShow.com