Wednesday, November 18, 2009

About GDB

GDB is a wonderful tool if you are using linux as your platform for software development.
I will list done some of the frequently used approaches here.

watch point:
awatch *0x8092581000
rwatch *0x8092581000
it will start breaking whenever there is read/write at: 0xa87c0e255f7f0080

find /g 0x8080000000,0x8092581000, 0x8080459578
-- this is to search for value: 0x8080459578 between address 0x8080000000 && 0x8092581000
find  0x8080000000,0x8092581000, "tbl_sm_main"
-- this is to search a string. 
 
Printf usage
printf "Delete object: %d\n", i

Commands:
1. info frame
 -- will list down the registry level info about the particular frame, it will be useful when you are debugging cores.
2. info args
  -- will list the arguments passed to the function in particular frame.
3. disassemble
  -- will give the assembly code for the function.
4. disassemble 0xf1704f0 0xf17050f
  -- will give the assembly code between two addresses
5.  info registers
  -- will give the details of registers and stored values
6. info reg OR x $
  -- will output the contents for regiser, ex1: info reg r32 ex2: x $r32
7. info source
8. info sources
9. set pagination on/off
10. x/32w 0x100f3000
  -- To print 32 words from address 0x100f3000
11. x/s
  -- To print contents of the address as string
12. x/nc
  -- To print content of the address as character, it will print "n" characters starting from the address.
13. x/t
  -- To print in binary
14. x/nx
  -- To print in hexa
15. core
  -- To attached to the core file
16. generate-core-file
  -- To generate a core at the particular instance on a running process.
17. set follow-fork-mode parent/child/ask
  -- To set whether to follow parent or child or ask whom to follow.
18.  show follow-fork-mode
  -- To display the status of fork mode
19. set history expansion on
  -- To enable capturing history, you can "!" to rerun the olde commands as similar to Bash command.
20. set height
21. set width 
   -- To set width of the screen. if the lines in gdb are not wrapping after giving above command then run "shell reset" to make it work properly
22. set confirm off/on
  -- Disables/Enables confirmation requests
23. show confirm
   -- Displays the status of confirmation requests
24. show convenience
   -- Print a list of convenience variables used so far, and their values. Abbreviated show conv.
      Convenience variables are prefixed with `$'. Any name preceded by `$' can be used  for a 
      convenience variable, unless it is one of the predefined machine-specific register names.
25. output/fmt expression
   -- Print the value of expression and nothing but that value: no newlines, no `$nn = '. 
      The value is not entered in the value history either. You can use the same formats as for print.
26. set print pretty on/off
   -- Cause GDB to print structures in an indented format with one member per line.
27. show print pretty
   -- Show which format GDB is using to print structures.  
28. info proc map
   -- to list process memory mapping
29.  maintenance info sections
   -- to list core memory mapping
30. info files
   -- list the memory mapping of all the files 
31. info line <function name>
   -- display 
32. add-symbol-file <library.so> .txt 
  -- we can get.txt address of library from info files.
  -- this will help adding loading symbol files manually
34. info wallclock 
-- this will give timing in the recording.
35.   set backtrace limit unlimited 
-- to set backtrace to unlimited


To allow the system to dump the core:
ulimit -c unlimited
 
History related commands:
These commands display the state of the GDB history parameters.
show history
show history filename
show history save
show history size
show history expansion
show commands
Display the last ten commands in the command history.
show commands n
Print ten commands centered on command number n.
show commands +
Print ten commands just after the commands last printed.
 
Logging related commands:
show logging                    # displays weather logging is on / off
set logging on                  # enable logging
set logging off                 # disable logging
set logging file log-file.txt   # set name for log file, default is gdb.txt
set logging overwrite   # To append logs of debug sessions or
                                 create new on every debug session using 
 
 
 Good post on checkpoints:
http://sourceware.org/gdb/onlinedocs/gdb/Checkpoint_002fRestart.html#Checkpoint_002fRestart
 
Tracing function calls of process through gdb:
http://stackoverflow.com/questions/311840/tool-to-trace-local-function-calls-in-linux#312058
Breaking the process whenever a function from a specific file is getting executed:
http://stackoverflow.com/questions/475283/using-gdb-stop-the-program-when-it-is-using-any-function-from-file-x

default configuration file for gdb is: .gdbinit

To print the size of the structure in GDB:
p sizeof(struct ) ---- make sure symbol for the structure is present.

Breakpoints:

break FUNCTION
Set a breakpoint at entry to function FUNCTION.
break LINENUM
Set a breakpoint at line LINENUM in the current source file.
break FILENAME:LINENUM
Set a breakpoint at line LINENUM in source file FILENAME.
break FILENAME:FUNCTION
Set a breakpoint at entry to function FUNCTION found in file FILENAME.
break ... if COND
Set a breakpoint with condition COND; evaluate the expression COND each time the breakpoint is reached, and stop only if the value is nonzero--that is, if COND evaluates as true. ... stands for one of the possible arguments described above (or no argument) specifying where to break.
To generate core dumps during program running:
#include
void assert(int expression);

If expression evaluates to 0 (false), then the expression, sourcecode filename, and line number are sent to the standard error, and then calls the abort function.
To enable core dumps for your current shell, use ulimit to set a maximal core dump size in megabytes.
For this example, we'll "limit" core dumps to 1 gigabyte:
$ulimit -c 1024

To use macros in the gdb use following option during compiling:

gcc -gdwarf-2 -g3 sample.c -o sample

#define NDEBUG === > then the macro assert does nothing.

References:
http://sourceware.org/gdb/onlinedocs/gdb/Forks.html
http://arioch.unomaha.edu/~jclark/gdb_plus.html
http://discussknowhow.blogspot.com/2008/07/analyzing-core-dumps-and-assert.html
http://developer.apple.com/mac/library/documentation/DeveloperTools/gdb/gdb/gdb_10.html
http://www.haifux.org/lectures/222/GDB_haifux_David_Khosid.pdf -- Advanced debbuging with gdb
GNU Debugger - Advanced GDB Actions - GoldhiveWiki
http://sunsite.ualberta.ca/Documentation/Gnu/gdb-4.18/html_chapter/gdb_15.html
http://web.mit.edu/gnu/doc/html/gdb_10.html 
https://eli.thegreenplace.net/2011/09/06/stack-frame-layout-on-x86-64

No comments:

Post a Comment