190 likes | 231 Views
Learn how to use GDB effectively in your program with additional compile options, running programs, setting breakpoints, single-stepping instructions, manipulating breakpoints, watchpoints, and displaying values. Master the art of debugging with GDB!
E N D
GDB commands Hyeon-gyu Lee(hglee@archi.snu.ac.kr) School of Computer Science and Engineering Seoul National University
To Use GDB in Your Program • Additional compile option • gcc –g –o prog prog.c • Execute gdb • gdb prog
Looking the Program Source • List instruction • (gdb) list • Abbreviated to the letter ‘l’ • Shows your code of your program
Looking the Program Source • List instruction options • (gdb) list <function> • Shows the code of <function> • (gdb) list <file>:<function> • Shows the code of <function> in the <file> • (gdb) list <line number> • Shows the code from <line number> • (gdb) list <file>:<line number> • Shows the code from <line number> in the <file> • (gdb) list - • Shows the code right before your current position • Current unit lines of printing: 10 • (gdb) set listsize 20 • Set the unit lines of printing to 20
Running the program • Run the program • (gdb) run • Abbreviated to the letter ‘r’ • Run your code continuously • If confronts breakpoint, gdb stops there. • If there are no breakpoints, gdb runs until the program terminates. • Useful when your program has errors • (gdb) run arg1 arg2 … • Run your code continuously with arguments
Breakpoints • Make points to stop • (gdb) break • Abbreviated to the letter ‘b’ • Without any option, breakpoint is made at your current position
Making Breakpoints • Breakpoint options • (gdb) break <function> • Breakpoint at the start of the function • (gdb) break <file>:<function> • Breakpoint at the start of the function in the file • (gdb) break <line number> • Breakpoint at the designated line • (gdb) break <+/-number> • Breakpoint at the current-position-relatively designated line • (gdb) break <memory address> • Breakpoint at the instruction that pointed by the memory address • Used at assembly debugging • Above all options can be done with conditions • e.g. (gdb) break 10 if var==0
Manipulating Breakpoints • Breakpoint information • (gdb) info break • Shows the information about your breakpoints • Valid until the gdb terminates • Deleting breakpoint • (gdb) clear • Abbreviated to ‘cl’ • Clear breakpoints that you made • Should be used with options • (gdb) clear <line number> • Delete breakpoint at that line number
Manipulating Breakpoints • Deleting breakpoint options • (gdb) clear <line number> • Delete breakpoint at that line number • (gdb) clear <function> • Delete breakpoint at the start of the function • (gdb) clear <file>:<function> • Delete breakpoint at the start of the function in the file • (gdb) d • Delete all breakpoints • (gdb) disable/enable br • Disable/enable all breakpoints • (gdb) disable/enable br 1 3 • Disable/enable breakpoints with number 1, 3.
Single-stepping • Single stepping • (gdb) step • Abbreviated to the letter ‘s’ • When a function is called at the point, it goes into the function • (gdb) step <number> • Repeat ‘step’ with times of the number. • (gdb) next • Abbreviated to the letter ‘n’ • When a function is called at the point, it executes the whole function continuously • (gdb) next <number> • Repeat ‘next’ with times of the number.
Single-stepping • Other instructions • (gdb) continue • Abbreviated to the letter ‘c’ • Continuously execute from your current position • (gdb) advance <line number> • Continuously execute from your current position to the line number • (gdb) until • Continuously execute until current repetition finish point • When the current position is not in a repetition, it operates like ‘next’
Single-stepping • Other instructions • (gdb) finish • Continuously execute from your current position until current function finish point • (gdb) return <return value> • Return without executing the remaining part of the function • When needed, you can manually designate a return value • (gdb) set var <variable>=<value> • Change the content of the variable to the given value
Watchpoints • Stop when a variable is set to a specific value • (gdb) watch <variable> • Break when a value is written in the variable • (gdb) rwatch <variable> • Break when the value is read in the variable • (gdb) awatch <variable> • Break when a value is read/written in the variable
Displaying Values • See values in your program • (gdb) info locals • Shows values of local variables that you can access from your current position • (gdb) info variables • Shows values of global variables that you can access from your current position • (gdb) print <variable> • Shows value of the variable • When the variable is a pointer, dereferencing is possible with an asterisk. • (gdb) print <function> • Shows address of the function
Displaying Values • See values in your program • (gdb) print $<register> • Shows value of the register. • (gdb) print ‘<file>’::<variable> • Shows value of the variable in the file. • (gdb) print <function>::<variable> • Shows value of the variable in the function • (gdb) whatis <variable> • Shows type of the variable • (gdb) display <variable> • Shows value of the variable automatically with progress of the program • Enabling/disabling is possible • (gdb) undisplay <display#> • Delete display
Examining the Stack • Backtracing the stack • (gdb) backtrace • Abbreviated to the ‘bt’ • Shows stack trace of your current position
Killing the Executing Program in the GDB • Kill instruction • (gdb) kill • Kill the executing program forcefully
Quitting the GDB • Quit and exit to the Linux • (gdb) quit • Abbreviated to the letter ‘q’
References • References • http://kwanseob.blogspot.kr/2012/03/gdb.html, retrieved on May 20, 2015. • http://darkdust.net/files/GDB%20Cheat%20Sheet.pdf, retrieved on May 20, 2015. • https://kldp.org/node/71806, retrieved on May 20, 2015.