lecture10 - CIS 450 Computer Architecture and Organization...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CIS 450 Computer Architecture and Organization Lecture 10: Debuggers Mitch Neilsen [email protected] 219D Nichols Hall Topics Debuggers gdb Gnu DeBugger 2 GDB (GNU DeBugger) Basic functionality: Can run programs in an observable environment Uses ptrace-interface to insert breakpoint, single step, inspect & change registers and variables Does not require compilation with "-g", but works much better if it has the symbol tables available Maintains source line numbers and can inspect source files Ability to attach to a running process Ability to watch memory locations Conditional breakpoints Some graphical user interfaces exist (DDD, KDbg, ...) 3 DDD Graphical front-end to GDB with extended data visualization support: http://www.gnu.org/software/ddd 4 Debugging What can debuggers do? Run programs Make the program stops on specified places or on specified conditions Give information about current variables' values, the memory and the stack Let you examine the program execution step by step - stepping Let you examine the change of program variables' values - tracing To be able to debug your program, you should compile it with the -g option (generates the symbol table) ! $ gcc g o myProg myProg.c 5 GDB - Freeware GNU Debugger Starting GDB: $ gdb Loading the executable code and symbol table (if available): > file myProg Start GDB and load the symbol table in one step: $ gdb myProg Exit GDB: > quit Executing shell commands: make is a special case: > shell command args > make args 6 GDB Commands Command names may be truncated if the abbreviation is unambiguous: s (for step), r (for run) UNIX style TAB completion for the command names. Alternative way: > complete chars. Ex.: > complete h results in: handle hbreak help Getting help: help (or h) lists all classes of commands. h command - displays a short description of the command 7 GDB Running Programs Running a program: > run (or r) -- creates an inferior process that runs your program. if there are no execution errors the program will finish and results will be displayed in case of error, the GDB will show: - the line the program has stopped on and - a short description of what it believes has caused the error There is a certain information that affects the execution of a program: program's arguments program's environment program's working directory standard input and output 8 GDB Program's arguments Specifying command-line arguments for your program: As arguments to run: With set args command: Notes: > run without arguments uses the same arguments used by the previous run. > set args without arguments removes all arguments. > show args command shows the arguments your program has been started with. > run arg1 arg2 ... > set args arg1 arg2 ... 9 GDB Program's environment Changing the PATH environment variable: > path dir add the directory dir at the beginning of the PATH variable. You may specify several directory names separated by `:' or white space. > show paths displays the search paths for executables. Changing the working directory: > cd dir to change the working directory Redirecting output: > run > outfile - direct the output to the file outfile. 10 GDB -- Debugging an already-running process Debugging an already-running process From inside GDB: > attach process-id // To obtain the process ID use the UNIX command ps (process status) > shell ps to execute a shell command (in this case ps) from within gdb. From outside GDB: $ gdb myProg process-id The first thing GDB does after arranging to debug the specified process is to stop it. > detach detaches the currently attached process from the GDB control. A detached process continues its own execution. 11 GDB Breakpoints and watchpoints Breakpoints and watchpoints allow you to specify the places or the conditions where you want your program to stop. > break arg stops when the execution reaches the specified line // arg = function name, line number, +/- offset / > watch expr stops whenever the value of the expression changes > clear [arg] Without arguments deletes any breakpoint at the next instruction to be executed in the current stack frame > delete [bnum] Without arguments deletes all breakpoints. 12 GDB Examining variables Global variables can be examined from every point in the source file. Local variables can be examined only in their scope or using: file::variable or function::variable Print variable type: Current variable value: Automatic display: > ptype var > print var > display var - adds var to the automatic display list. > undisplay dnum ___________________________________________________ Specifying the output format (x, o, d, u, t, a, f, and c) : > print /t var - prints the value of var in binary format 13 GDB Value history The value history keeps the values printed by the print command. Previously printed values can be accessed by typing $ followed by their history number. $ - refers to the most recent value and $$n - refers to the n-th value from the end. > show values [n|+] Without argument the last 10 values. n 10 values centered around n + 10 values after the last printed 14 GDB Stepping through the program > step [count] program execution continue to next source line going into function calls. > next [count] program execution continue to the next source line omitting function calls. > continue resume program execution > until continue until the next source line in the current stack frame is reached. /useful to exit from loops/ 15 GDB Altering execution Returning from a function > finish - forced return > return [ret_value] pops the current stack frame Continuing at different address > jump line_num | *address Altering the value of a variable > set i=256 Proceeding to a specified point: > until [line_num | *address | function_name] 16 GDB The stack frame Stack frames are identified by their addresses, which are kept in the frame pointer register. Selecting a frame: > frame n|addr > up n 0 1 2 > down n Information about the current frame > frame brief description > info args shows function arguments > info locals shows local variables 17 GDB Convenience variables Convenience variables are used to store values that you may want to refer later. Any string preceded by $ is regarded as a convenience variable. Ex.: $table = *table_ptr There are several automatically created convenience variables: $pc program counter $sp stack pointer $fp frame pointer $ps processor status $_ - contains the last examined address $__ - the value in the last examined address $_exitcode - the exit code of the debugged program 18 DDD - The Data Display Debugger DDD is a GUI debugger that can work with several inferior debuggers including GDB. GDB commands can be typed in the debugger console window. DDD sets some GDB settings automatically and will not work correctly if you change them. These are: set height 0 set width 0 set verbose off set prompt (gdb) ! 19 DDD View argument field command tool source window debugger console 20 DDD GUI Advantages Frequently used commands are on the toolbars, have assigned shortcut keys or can be done just with a mouse click. Easy browsing through the source ! Examining current variables values directly by placing the mouse pointer over them. Possibility to graphically display the program data. Help menu. 21 DDD Command menu 22 GDB Examining memory The x command (for "examine"): > x/nfu addr specify the number of units (n), the display format (f) and the unit size (u) of the memory you want to examine, starting from the address addr. Unit size can be b, h (half), w and g (giant). > x addr start printing from the address addr, others default > x all default Registers Registers names are different for each machine. Use > info registers to see the names used on your machine. GDB has four "standard" registers names that are available on most machines: program counter, stack pointer, frame pointer and processor status. 23 GDB Additional process information > info proc summarize available information about the current process. > info proc mappings address range accessible in the program. > info proc times starting time, user CPU time and system CPU time for your program and its children. > help info > info signals information about the system signals and how GDB handles them. 24 The Binary Bomb Lab Your task is to use GDB to diffuse a binary bomb! Good luck!!! 25 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online