相關資源‎ > ‎

gdb

Debugger
  • Help programmer to locate source of problem.
  • Run a program, see where it crashes, or stop it in the middle of running to examine program state.  
  • Allow programmer to:
    • control the program execution
    • examine what has happened 
  • Two ways to run:
    • gdb binary (to run binary inside of gdb)
    • gdb binary core-file  (to debug crashed program)

Controlling Execution
  • run <cmd-line args>
  • break <func>
  • breakpoint
  • delete
  • step 
  • next
  • continue
  • finish
  • until
  • kill
  • control-c 
Getting info Command
  • Getting Info
  • backtrace
  • print <expr>
  • display
  • info locals
  • list
  • up/down
  • watch
  • frame
GDB Tricks & Tips
  • Remember:  always compile with –g , and no optimizations.  
    • g++ –g hello.cc
  • If you are not getting core dump files, type:   
  •         ‘unlimit coredumpsize’ 
  • You can use GDB in emacs! (see slides at end)
Using GDB
  • To prepare for debugging
    • Compile C program with ‘-g’ flag.
    • e.g., gcc -g prog.c -o prog
  • To start debugger
    • Pass program name as the first argument to gdb.
    • e.g., gdb prog
    • gdb shows a ‘(gdb)’ prompt and waits for commands.
  • To exit debugger
    • (gdb) quit
Useful options when invoking GDB
  • -d dir :  look in dir for sources
  • -x file: read and execute  file before accepting any commands
  • -nx: don’t execute commands from the initialization file
  • -batch: run in batch mode

GDB Commands

  • run [arg1 ...] (r)
    • Start program with optional argument list.
  • Alternative:
    • (gdb) set args [arg1 ...]
    • (gdb) run
(gdb) set args –b –x
(gdb) show args
(gdb) run

  • Execution can be stopped with Ctrl-C at any time.
  • kill (k)
    • Kill execution of program.
    • Program can be started again with run command.
  • Print program counter
(gdb) x/4i $pc

To be continued
Reference
  • Book
    • Programming with GNU Software
    • Debugging with GDB
    • Art Of Debugging With GDB, DDD, And Eclipse, The (Paperback)
  • Web
    • http://www.dirac.org/linux/gdb/ (a GDB guide)
  • Web
    • Using GNU's GDB Debugger. http://eric.bachard.free.fr/UTBM_LO22/P07/C/Documentation/C/tutorial_gdb/Peter%27s_gdb_Tutorial.html   By Peter Jay Salzman
  • Study Area:.用Open Source工具開發軟體: 新軟體開發觀念 
  • http://www.study-area.org/cyril/opentools/opentools /book1.html
  • Reference Card : http://users.ece.utexas.edu/~adnan/gdb-refcard.pdf (GDB reference card)

Comments