440 likes | 570 Vues
Dive into the fundamentals of GDB, the GNU Project Debugger, via this comprehensive guide. Learn why debuggers are essential, how to run programs within GDB, and effectively manage program flow with breakpoints and commands like 'continue' and 'step'. This tutorial covers examining source code, printing variable values, setting arguments, and adjusting the working environment. By mastering these techniques, you can debug code more efficiently, enhancing your programming skills in C. Get ready to elevate your coding experience with GDB!
E N D
Linux Programming Tutoring Introduction to fundamental GDB- More GDB function -
Get files Slides : http://140.112.28.132/gdb_leb Packages : $wgethttp://140.112.28.132/gdb.zip $unzip gdb.zip
Outline • Why debugger • Introduction • Running program • Stop and continue • Examining source and data
Why Debugger(1/3) • With print (printf in c) • Add lines to print values • Must comment debug lines when release Int main(int argc, char** argv) { int var1,var2, ... ; ... print(var1,var2, ...); ... //print(...); ... }
Why Debugger(2/3) • Use preprocessor • define, ifdef • Still not flexible enough #define debugmode Int main(int argc, char** argv) { int var1,var2, ... ; ... #ifdef debugmode print(...) #endif ... }
Why Debugger(3/3) • With debugger • We can concentrate on writing program easily • We can print values and stop anywhere without recompilation • We can make up some value or alter program flow
Outline • Why debugger • Introduction • Running program • Stop and continue • Examining source and data
GDB : The GNU Project Debugger • Official website: • http://www.gnu.org/software/gdb/ • We focus on how to use today • Documentation/User Manual
Invoking and quitting • gdb program • Debug program • gdb program pid • Debug running program with pid • quit[q] or <Ctrl> + d • Quit gdb
Something useful • shell command-strings • invoke shell to execute command-strings • $SHELL • <tab> • auto completion • blank line input (<ret>) • repeat last command
Outline • Why debugger • Introduction • Running program • Stop and continue • Examining source and data
Running program • gcc –g foo.c –o foo • compiling for debugging • gdbfoo • Debug program • run[r] • run the program under gdb
Arugments • Bash$ foo arg1 arg2 • set args arg1 arg2 • to set arguments • set args • clear arguments • show args • display current arguments
Enviroment • show environment • Show thecurrent environment • path directory • add directory to $PATH • set environmentvarname [=value] • assign value to varname set environment SHELL=/bin/bash
Working directory • Program inherits working directory from the gdb • cd directory • change working directory • pwd • display current workign directory
Outline • Why debugger • Introduction • Running program • Stopping and Continuing • Examining source and data
Stopping program • We use breakpoints to stop the program • info program • display status , why we stop
Stopping program • info breakpoints • print a table of all breakpoints
Breakpoints filename:linenum filename:function -/+offset • break[b] location • set breakpoint at location • clear location • clear breakpoint at location • enable/disable [breakpoints] [range...] • delete [breakpoints] [range...]
Continuing and Stepping • continue[c] • Resume program execution • step[s] [count] • Continue, step in function • next[n] [count] • Continue, execute function without stop
Schematic diagram of continue • Main • { • ins1 • foo(); • ins2 • ins3 • } foo { ins4 ins5 }
Schematic diagram of step • Main • { • ins1 • foo(); • ins2 • ins3 • } foo { ins4 ins5 }
Schematic diagram of next • Main • { • ins1 • foo(); • ins2 • ins3 • } foo { ins4 ins5 }
Outline • Why debugger • Introduction • Running program • Stopping and Continuing • Examining source and data
Examining source • list[l] location • print source line centered around specific location • list first, last • print lines between first and last list 10,30
Examining source • list • print next lines • list - • print lines before the line last printed list - Last print list
Examining data • print[p] expression • print the value of expression • print function::variable • print static variable in function
Automic display • info display • print a list of all set up expression • display expr • print expr automatically • undisplay dnum
Automic display • delete display dnum • disable display dnum • enable display dnum
Outline • Breakpoint • Formating output • Examing memory • Print dynamic allocate array • Altering execution
Condition break(1/2) • info breakpoint • break[b] location • stop everytime • break location if expression • break foo break foo if argc == 1 break foo if argc >= 3
Condition break(2/2) • conditionbnum expression • add condition to breakpoint bnum • conditionbnum • clear condition • break foo • condition 1 argc == 1
One stop breakpoint • tbreak location • Set up as the same as break • But automatically remove after stop • tbreak foo
Outline • Breakpoint • Formating output • Examing memory • Print dynamic allocate array • Altering execution
Formating output(1/2) • We can format output like printf • command/format expression
Formating output(2/2) • p/format expression • print expression value in format • display/format expression • print expression valuein fromat automatically • p/x counter • display/x counter
Outline • Breakpoint • Formating output • Examing memory • Print dynamic allocate array • Altering execution
Examing memory • x address • examine address value • x/nfu address • n : number of elements • f : format as previously describe • u : unit of element x/4xb &variable
Print dynamic allocate array int *marray = (int*)malloc( len * sizeof(int) ); • pmarray • $1 = (int *) 0x601010 • p *marray • $2 = 0 • p *marray@len • $3 = {0, 0, 0, 0, 0}
Outline • Breakpoint • Formating output • Examing memory • Print dynamic allocate array • Altering execution
Altering execution • We can alter execution by • change value of variable • jump to some location • return makeup value
Altering execution • set var assignment_exp • print assignment_exp • gdb will evaluate the expression • p foo=4
jump and return • jump location • jump to assigned location, and continue execution
jump and return • return expression • return from function call with expression value