Monday, March 1, 2010

Some useful commands when using GDB

some useful gdb commands and usage I didn't know about:

1. run gdb with -tui parameter gives gdb a native gui to show your source code. you may browse the source code using page up/down, arrow keys, using list to indicate where in the code you wish to show

2. "file" command specify which executable you wish to debug with when in gdb console
3. "break" has shortcut 'b', break points can be enabled and disabled using "enable" and "disable" command. To remove a breakpoint, use "clear" with the line number or use "delete" with the break point ID
4. "info" command, with shortcut 'i', can be used to list out all breakpoints, watched variables,
5. "run" command has shortcut 'r'
6. there are several ways to tell gdb show variable values. one is "display" command, which causes the gdb to report you the variable value so long as it is in scope. you can remove a variable on display using "undisplay" command. "print" and "printf" command can also be used to display a variable's value, print takes a variable name as parameter, printf takes a string with paramters, just like c printf function. "watch" command display the value of a variable only when it has been modified. "rwatch" detects if a variable is being read, "awatch" detects both when variable is being read or written to
7. there are many commands for stepping, "next", "stepi", "step", and also "finish" which brings to the end of a function, "advance" command advance execution to some line or function, just like a temporary breakpoint
8. to trace the stack, use the "backtrace" command to show the call stack
9. you can modify the value of a variable during execution using "set variable" command, or "set ( = )", which is set with parenthesis.
10. you can also attach gdb to a currently running program using the "attach " command
11. when a program exited with segmentation fault, the program will produce a core dump. you may access that core dump using gdb with this command: "gdb -c core "
12. when in gui mode, you may access window information using "info win". you can switch focus of window in console using "fs " or "fs next" to focus on next window in the window info list. you may also set the height of a window using "winheight" command
13. there are several layouts for windows, here is some example:
"layout src"
"layout asm"
"layout split"
"layout reg"
"tui reg general"
"tui reg float"
"tui reg system"
"tui reg next"
14. you may switch your disassembly flavor using the "set disassembly-flavor" to either intel or att

(taken from Beej's GDB guide)