gdb: GNU Debugger - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

gdb: GNU Debugger

Description:

Starting with pid very useful for programs that misbehave non-deterministically (scenario difficult to repeat; problem exhibited sporadically; ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 14
Provided by: csunEdua9
Learn more at: http://www.csun.edu
Category:
Tags: gnu | debugger | gdb | pid

less

Transcript and Presenter's Notes

Title: gdb: GNU Debugger


1
gdb GNU Debugger
  • Lecturer Prof. Andrzej (AJ) Bieszczad
  • Email andrzej_at_csun.edu
  • Phone 818-677-4954

UNIX for Programmers and Users Third Edition,
Prentice-Hall, GRAHAM GLASS, KING ABLES Slides
partially adapted from Kumoh National University
of Technology (Korea) and NYU
2
GDB, the GNU Debugger
  • Text-based, invoked with
  • gdb ltprogramfilegt ltcorefilegtltpidgt
  • Argument descriptions
  • ltprogramfilegt executable program file
  • ltcorefilegt core dump of program (crash
    records)
  • ltpidgt process id of already running program
  • Compile ltprogramfilegt with g for debug info (cc
    -g ltprogramfilegt)
  • Use -xdb for compatibility with xdb
  • corefiles very useful in post mortem analysis
    (figuring out what went wrong why did it crash?)
  • Starting with pid very useful for programs that
    misbehave non-deterministically (scenario
    difficult to repeat problem exhibited
    sporadically field troubleshooting)

3
gdb Example
  • Get reversefirst.c from
  • http//www.csun.edu/andrzej/COMP421-F04/source/ch
    12/reversefirst.c
  • cc -g reversefirst.c -o reverse
  • gdb reverse
  • HP gdb 3.2 for PA-RISC 1.1 or 2.0 (narrow), HP-UX
    11.00.
  • ltgt
  • (gdb) help
  • ltgt
  • Type "help" followed by a class name for a list
    of commands in that class.
  • Type "help" followed by command name for full
    documentation.
  • Command name abbreviations are allowed if
    unambiguous.
  • (gdb)

4
Basic GDB Commands
  • General Commands
  • file ltfilegt selects ltfilegt as the program to
    debug
  • run ltargsgt runs selected program with
    arguments ltargsgt
  • attach ltpidgt attach gdb to a running process
    ltpidgt
  • kill kills the process being debugged
  • quit quits the gdb program
  • help lttopicgt accesses the internal help
    documentation
  • Stepping and Continuing
  • continue continue execution (after a stop)
  • step step one line, entering called functions
  • next step one line, without entering
    functions
  • finish finish the function and print the return
    value

5
GDB Breakpoints
  • Useful breakpoint commands
  • break ltwheregt sets breakpoints. ltwheregt can
    be a number of things, including a
    hex address, a function name, a
    line number, or a relative line offset
  • rwatch ltexprgt sets a watchpoint, which will
    break when ltexprgt is written to or read
  • info breakpoints prints out a listing of all
    breakpoints
  • clear ltwheregt clears a breakpoint at ltwheregt
  • delete ltnumsgt deletes breakpoints by number

6
Playing with Data in GDB
  • Commands for looking around
  • list ltwheregt prints out source code at ltwheregt
  • search ltregexpgt searches source code for ltregexpgt
  • backtrace ltngt prints a backtrace ltngt levels
    deep
  • info ltwhatgt prints out info on ltwhatgt
    (like local variables or function args)
  • print ltexprgt prints out the evaluation of
    ltexprgt
  • Commands for altering data and control path
  • set ltnamegt ltexprgt sets variables or arguments
  • return ltexprgt returns ltexprgt from current
    function
  • jump ltwheregt jumps execution to ltwheregt

7
gdb - Example
  • (gdb) list 1 ---gt can use l for list
  • 1 / REVERSE.C /
  • 2
  • 3 include ltstdio.hgt
  • 4
  • 5 / Function Prototype /
  • 6 void reverse ()
  • 7
  • 8 /
    /
  • 9
  • 10 main ()
  • (gdb) l ---gt same as list continues from
    the previous
  • 11
  • 12
  • 13 char str 100 / Buffer to hold reversed
    string /
  • 14
  • 15 reverse ("cat", str) / Reverse the string
    "cat" /
  • 16 printf ("reverse (\"cat\") s\n", str) /
    Display /
  • 17 reverse ("noon", str) / Reverse the string
    "noon" /

8
gdb - Example
  • (gdb) break main ---gt set a breakpoint in
    function main
  • Breakpoint 1 at 0x29f4 file reversefirst.c, line
    15.
  • (gdb) break 16 ---gt set a breakpoint in line 16
    (current source)
  • Breakpoint 2 at 0x2a0c file reversefirst.c, line
    16.
  • (gdb) break reverse ---gt set a breakpoint in
    function reverse
  • Breakpoint 3 at 0x2a80 file reversefirst.c, line
    33.
  • (gdb) info break ---gt display information on
    breakpoints
  • Num Type Disp Enb Address What
  • 1 breakpoint keep y 0x000029f4 in main at
    reversefirst.c15
  • 2 breakpoint keep y 0x00002a0c in main at
    reversefirst.c16
  • 3 breakpoint keep y 0x00002a80 in reverse
    at reversefirst.c33
  • (gdb) run
  • Starting program /home/usersNN/userID/reverse/rev
    erse
  • Breakpoint 1, main () at reversefirst.c15
  • 15 reverse ("cat", str) / Reverse the string
    "cat" /
  • (gdb) ontinue ---gt you can use c as well
  • Breakpoint 3, reverse (before0x40001028 "cat",
    after0x7f7f0958 "")

9
gdb - Example
  • (gdb) backtrace ---gt show the execution stack
  • 0 reverse (before0x40001028 "cat",
    after0x7f7f0958 "") at reversefirst.c33
  • 1 0x2a0c in main () at reversefirst.c15
  • (gdb) l
  • 28
  • 29 int i
  • 30 int j
  • 31 int len
  • 32
  • 33 len strlen (before)
  • 34
  • 35 for (j len - 1, i 0 j gt 0 j--, i) /
    Reverse loop /
  • 36 afteri beforej
  • 37
  • (gdb) next ---gt execute next line
  • 35 for (j len - 1, i 0 j gt 0 j--, i) /
    Reverse loop /
  • (gdb) n ---gt same as next
  • 36 afteri beforej
  • (gdb) _

10
gdb - Example
  • (gdb) print afteri ---gt display data
    (expression)
  • 1 0 '\000'
  • (gdb) p beforej ---gt same as print
  • 2 116 't'
  • (gdb) _
  • (gdb) n
  • 35 for (j len - 1, i 0 j gt 0 j--, i) /
    Reverse loop /
  • (gdb) p after ---gt print
  • 4 0x7f7f0958 "t"
  • (gdb) p before
  • 5 0x40001028 "cat"
  • (gdb) c
  • Continuing.
  • Breakpoint 2, main () at reversefirst.c16
  • printf ("reverse (\"cat\") s\n", str) /
    Display /
  • (gdb) _

11
gdb - Example
  • (gdb) n
  • reverse ("cat") tac
  • 17 reverse ("noon", str) / Reverse the string
    "noon" /
  • (gdb) s
  • Breakpoint 3, reverse (before0x40001030 "noon",
    after0x7f7f0958 "tac")
  • at reversefirst.c33
  • 33 len strlen (before)
  • (gdb) return 0
  • Make reverse return now? (y or n) y
  • 0 main () at reversefirst.c18
  • 18 printf ("reverse (\"noon\") s\n", str) /
    Display /
  • (gdb) p str
  • 4 "tac", '\000' ltrepeats 96 timesgt
  • (gdb) n
  • reverse ("noon") tac ---gt this is the output
    from the program
  • 19
  • (gdb) quit
  • _

12
gdb/C Challenge
  • The reverse program from the next slide is buggy.
  • Copy the files (reverse1.c and reverse1.h) to
    your local file system then, compile and run
    them.
  • Do you see the problem?
  • Can you fix it easily?
  • gdb will help
  • Use gdb to examine program behavior, fix the bug
    and call me to show the working version.

13
gdb/C Challenge
  • / REVERSE1.C /
  • include ltstdio.hgt
  • include "reverse1.h"
  • /
    /
  • int reverse (str)
  • char str
  • int i
  • int len
  • char c
  • len strlen (str)
  • for (i 0 i lt len/2 i) / Reverse loop /
  • c stri
  • (strI) strlen-i-1
  • (strlen-i-1) c
  • int main()
Write a Comment
User Comments (0)
About PowerShow.com