1 / 44

Linux Programming Tutoring

Linux Programming Tutoring. Introduction to fundamental GDB- More GDB function -. Get files. Slides : http://140.112.28.132/gdb_leb Packages : $ wget http:// 140.112.28.132/gdb.zip $unzip gdb.zip. Outline. Why debugger Introduction Running program Stop and continue

mariah
Télécharger la présentation

Linux Programming Tutoring

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linux Programming Tutoring Introduction to fundamental GDB- More GDB function -

  2. Get files Slides : http://140.112.28.132/gdb_leb Packages : $wgethttp://140.112.28.132/gdb.zip $unzip gdb.zip

  3. Outline • Why debugger • Introduction • Running program • Stop and continue • Examining source and data

  4. 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(...); ... }

  5. 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 ... }

  6. 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

  7. Outline • Why debugger • Introduction • Running program • Stop and continue • Examining source and data

  8. GDB : The GNU Project Debugger • Official website: • http://www.gnu.org/software/gdb/ • We focus on how to use today • Documentation/User Manual

  9. Invoking and quitting • gdb program • Debug program • gdb program pid • Debug running program with pid • quit[q] or <Ctrl> + d • Quit gdb

  10. Something useful • shell command-strings • invoke shell to execute command-strings • $SHELL • <tab> • auto completion • blank line input (<ret>) • repeat last command

  11. Outline • Why debugger • Introduction • Running program • Stop and continue • Examining source and data

  12. Running program • gcc –g foo.c –o foo • compiling for debugging • gdbfoo • Debug program • run[r] • run the program under gdb

  13. Arugments • Bash$ foo arg1 arg2 • set args arg1 arg2 • to set arguments • set args • clear arguments • show args • display current arguments

  14. Enviroment • show environment • Show thecurrent environment • path directory • add directory to $PATH • set environmentvarname [=value] • assign value to varname set environment SHELL=/bin/bash

  15. Working directory • Program inherits working directory from the gdb • cd directory • change working directory • pwd • display current workign directory

  16. Outline • Why debugger • Introduction • Running program • Stopping and Continuing • Examining source and data

  17. Stopping program • We use breakpoints to stop the program • info program • display status , why we stop

  18. Stopping program • info breakpoints • print a table of all breakpoints

  19. 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...]

  20. Continuing and Stepping • continue[c] • Resume program execution • step[s] [count] • Continue, step in function • next[n] [count] • Continue, execute function without stop

  21. Schematic diagram of continue • Main • { • ins1 • foo(); • ins2 • ins3 • } foo { ins4 ins5 }

  22. Schematic diagram of step • Main • { • ins1 • foo(); • ins2 • ins3 • } foo { ins4 ins5 }

  23. Schematic diagram of next • Main • { • ins1 • foo(); • ins2 • ins3 • } foo { ins4 ins5 }

  24. Outline • Why debugger • Introduction • Running program • Stopping and Continuing • Examining source and data

  25. Examining source • list[l] location • print source line centered around specific location • list first, last • print lines between first and last  list 10,30

  26. Examining source • list • print next lines • list - • print lines before the line last printed list - Last print list

  27. Examining data • print[p] expression • print the value of expression • print function::variable • print static variable in function

  28. Automic display • info display • print a list of all set up expression • display expr • print expr automatically • undisplay dnum

  29. Automic display • delete display dnum • disable display dnum • enable display dnum

  30. Outline • Breakpoint • Formating output • Examing memory • Print dynamic allocate array • Altering execution

  31. 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

  32. Condition break(2/2) • conditionbnum expression • add condition to breakpoint bnum • conditionbnum • clear condition • break foo • condition 1 argc == 1

  33. One stop breakpoint • tbreak location • Set up as the same as break • But automatically remove after stop • tbreak foo

  34. Outline • Breakpoint • Formating output • Examing memory • Print dynamic allocate array • Altering execution

  35. Formating output(1/2) • We can format output like printf • command/format expression

  36. 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

  37. Outline • Breakpoint • Formating output • Examing memory • Print dynamic allocate array • Altering execution

  38. 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

  39. 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}

  40. Outline • Breakpoint • Formating output • Examing memory • Print dynamic allocate array • Altering execution

  41. Altering execution • We can alter execution by • change value of variable • jump to some location • return makeup value

  42. Altering execution • set var assignment_exp • print assignment_exp • gdb will  evaluate the expression • p foo=4

  43. jump and return • jump location • jump to assigned location, and continue execution

  44. jump and return • return expression • return from function call with expression value

More Related