Linux Modules Character drivers IO & Memory Linux Kernel Process Management Process Address space Linux Scheduler Memory Management Interrupts Signals System Calls Kernel Synchronization Linux Inter Process Communications Serial Ports Parallel Ports Introduction to Hardware Linux Timers DMA in Linux Linux Threads Linux Thread Synchronization Linux Multi Threading Debugging in Linux GDB GNU Debugger KDB Kernel Debugger KGDB Kernel GNU Debugger Example Ethernet Driver |
GNU Debugger gdb is capable of either debugging programs as they run, or examining the cause for a program crash with a core dump. 1. A sample gdb session 2. Getting In and Out of gdb 3. Stopping the program 4. Running the program 5. Examining Data 6. Machine Level debugging 7. Changing the program GDB can do following four main kinds of things: ü Start your program specifying anything that might affect its behavior ü Make your program stop on specified conditions ü Examine what has happened when your program has stopped ü Change things in your program, so you can experiment with the correcting bugs Invoking GDB The most usual way of starting GDB is $ gdb $ gdb program $ gdb program core $ gdb program pid Other command line options- silent :-Suppress initial screen. -help (-h) :-Displays available options. -symbols(-s) :-Read symbol table from ‘file’. -exec(-e) :-Use ‘file’ as executable. -se file :-Read symbol table from ‘file’ and use it as executable. -core(-c) file :-Use ‘file’ as a core dump to examine. -c number :-Connect to process id number. -command (-x) file :-Execute GDB command from ‘file’. -batch :-Run in batch mode. -cd directory :-Run GDB using directory as its working directory. Shell Commands shell :-command -string. make :-make args. Running program under GDB ü Compile your program with -g option ü GDB supports debugging with optimization enabled (-O) or disabled. ü Optimization disabled is less confusing. ü Use ‘run’ command from inside GDB to start your code running. Setting your Programs Arguments set args argument list :-Sets the arguments. show args :-Shows the arguments list. Program’s Input/Output You can redirect your programs input/output using shell redirection with the ‘run’ command .run > outfile Debugging an already running process attach pid (Use file command to specify the program running in the process and load its symbol table, before using attach). detach :-Detaches the process. Break points, Watch points & Exceptions: ü A break point makes your program stop whenever a certain point in the program is reached. ü A watch point is a special BP that stops your program when the value of an expression changes. Setting Break points break function break +offset break -offset break -linenum break filename:linenum break filename:function break *address break ----if cond tbreak args ü info breakpoints : displays all break points. Setting Watch points : You can use a watchpoint to stop execution whenever the value
of an expression changes, without having to predict a particular place where
this may happen. watch expr rwatch expr awatch expr ü info watchpoints : displays all watch points. Setting
catchpoints You can use catch points to cause the debugger to stop for certain kinds of program events, such as C++ exceptions or the loading of a shared library. Use the catch command to set a catch point. catch event Stop when event occurs. event can be any of the following: throw: The throwing of a C++ exception. catch: The catching of a C++ exception. Deleting Break points: clear Deleting any break points at the next instruction to be executed. clear function clear filename:function clear linenum clear filename linenum delete [bnums…] Disabling/ Enabling Breakpoints : disable [breakpoints] [bnums..] enable [breakpoints] [bnums..] enable [breakpoints] once bnums.. enable [breakpoints] delete bnums.. Continuing/Stepping : continue /c /fg :-Ignore count -Resume program execution ignoring break points. Step :-Continue running until control reaches a different source line. step count :-Continue executing ‘count’ source lines. next [count] :-Next is similar to step but function calls are executed without stopping. finish :-Continue running until just after function returns. step i/si :-Execute one machine instruction. next i[count] :-Execute one/count machine instructions, but if it is a function call, proceed until the function call, proceed until the function returns. Examining
the Stack When your program has stopped, the first thing you need to know is where it stopped and how it got there. Each time your program performs a function call, information about the call is generated. That information includes the location of the call in your program, the arguments of the call, and the local variables of the function being called. The information is saved in a block of data called a stack frame. The stack frames are allocated in a region of memory called the call stack. Stack
frames The call stack is divided up into contiguous pieces called stack frames, or frames for short; each frame is the data associated with one call to one function. The frame contains the arguments given to the function, the function’s local variables, and the address at which the function is executing. Backtraces: A backtrace is a summary of how your program got where it is. It shows one line per frame, for many frames, starting with the currently executing frame (frame 0), followed by its caller(frame 1), and on up the stack. backtrace /bt :-Print backtrace of entire stack (C-c to stop) bt n :-Print only the innermost n frames. bt -n :-print only the outermost n frames. Selecting
a frame Most commands for examining the stack and other data in your program work on whichever stack frame is selected at the moment. Here are the commands for selecting a stack frame; all of them finish by printing a brief description of the stack frame just selected. frame n f n Select frame number n. frame addr f addr Select the frame at address addr. up n down n Information about a frame There are several other commands to print information about the selected stack frame. info frame info f This command prints a verbose description of the selected stack frame, including: ü the address of the frame ü the address of the next frame down (called by this frame) ü the address of the next frame up (caller of this frame) ü the language in which the source code is written. ü the address of the frame’s arguments ü the address of the frame’s local variables ü the program counter saved in it (the address in the caller frame) ü which registers were saved in the frame Examining Source Files: ü When your program stops, GDB spontaneously prints the line where it stopped. When you select a stack frame, GDB prints the line where execution in that frame has stopped. ü You can print other portions of source files by explicit command. list linenum :-Print lines centered around ‘linenum’. list function :-Print lines centered around the beginning of ‘function’. List :-Print more lines. List :-Print lines just before the lines last printed. set listsize count show listsize Examining data: print exp :-Print the value of exp. print /f exp :-Print the value of exp with format ‘f’. print :-Display the last value again. Normally GDB can display variables visible in the current scope. However we can get the value of other variables by specifying explicitly. p file :: variable name print format supports are: x :-Regard the bits of the values as an integer, and print the integer in hex. d :-Print as integer in signed decimal. u :-Print as integer in unsigned decimal. o :-Print as integer in octal. t :-Print as integer in binary. a :-Print as an address. c :-Regard as an integer and print as a char constant. f :-Regard as floating point number and print floating point syntax. Examining Memory: You can use command ‘x’ to examine memory. x/nfu addr n is the repeat count in units ‘u’. f is the format. u is the unit size. b-Bytes, h-Halfwords (2 Bytes), w-Words (4 Bytes), g-Gaint words(8 bytes) addr-starting display address. Registers Info registers Info registers regname P/x $pc X/i $pc Examining
the Symbol Table To inquire about the symbols (names of variables, functions and types) defined in your program. info address symbol info symbol addr whatis expr info source info functions info variables Altering Execution You can store new values into variables or memory locations, give your program a signal, restart it at a different address, or even return prematurely from a function. Assignment to variables To alter the value of a variable, evaluate an assignment expression. For example, print x = 4 stores the value 4 into the variable x Use the set command, instead of the print command. (gdb) whatis count (gdb) set count = 47 Continuing at a different address Ordinarily, when you continue your program, you do so at the place where it stopped, with the continue command. You can instead continue at an address of your own. jump linespec jump *address set $pc = 0x485
Returning
from a function return return expression You can cancel execution of a function call with the return command. If you give an expression argument, its value is used as the function’s return value. Signals A signal is an asynchronous event. gdb has ability to detect any occurrence of signal in the program. You can specify gdb what to do with each signal. info signals info handle handle signal keyword signal
SIGALRM.. keyword
nostop, stop print,
noprint pass
noignore no
pass, ignore Giving
your program a signal signal signal Resume execution where your program stopped, but immediately give it the signal signal. signal can be the name or the number of signal. For example, on many systems signal 2 and signal SIGINT are both ways of sending an interrupt signal. Debugging programs with multiple processes By default, when a program forks, gdb will continue to debug the parent process and the child process will run unimpeded show follow-fork-mode set follow-fork-mode {parent | child } Debugging programs with multiple threads Thread threadno Info threads Thread apply [threadno] [all] args Thread specific break points Stopping and starting multi-threaded program When your program has multiple threads, you can choose to set breakpoints on all threads or on a particular thread. break linespec thread
threadno break linespec thread
threadno if ... (gdb) break frik.c:13 thread 28 if bartab > lim Generating core files A core file contains the contents of a program's memory space at the time the program ended. To produce a core file, you must enable core file generation. To enable core file generation, run the following command and then start the server from the same command line: ulimit -c unlimited ulimit -H -c unlimited The ulimit for core files might be set to zero. Be sure to run these commands so that the core file size is not limited. Core dump analysis The GNU debugger is called gdb and is a command line mode source -level debugger. It allows you to see what is going on inside a program as it executes (normal debugging) or after it crashed (core dump analysis). (There is also a graphical version of gdb called xxgdb which runs under the X11 environment.) In order to use gdb, you need to compile your program with the –g compiler flag. To start a normal gdb debugging session, you simply do gdb program or gdb program core If you are doing core dump (post-crash) analysis. You can see the stack by using where command. |