The GNU Debugger, usually called GDB, is the standard debugger for the GNU software system. It is a portable debugger that runs on many Unix-like systems and works for many programming languages, including Ada, C, C ++, FreeBASIC, and Fortran. GDB was first published by Richard Stallman in 1986 as part of his GNU system after the GNU Emacs was “sufficiently stable.” GDB is free software, published under the GPL. It was modeled after the Dbx debugger, which came with BSD distributions.
GDB can be used in two ways to debug the program:
1. Run it using the gdb command
2. Using the core file generated by a serious error.
The second is useful if the bug was not corrected before launching the program. In this case, if the user encounters a serious error, he can send the programmer the core file with which he can debug the program and correct the bug. The simplest form of debugging with GDB is where we want to determine the program line at which the error occurred.
Basic GDB commands:
1. b [reactionpoint] – receives as a argument a function name (ex: main), a line number and possibly a file (eg: break source.c: 50), a function (b source.c: my_function ) or an address (e.g. breakpoint * 0x80483d3).
2. [ext] – will continue to run the program until the next line in the source code is reached. If the line to execute contains a function call, the function will be executed completely.
3. s [toe] – if you want to inspect the functions.
4. fin [ish] – if you want to exit the current function.
5. list – this command will list the source file of the debug program. The command receives as argument a line number (possibly filename), a function or an address from which to list. The second argument is optional and specifies how many lines will be displayed. If the command has no parameter, it will list where the last display stopped.
6. continuity – is used when it is desired to continue the program execution.
7. print – with it you can display the values of the variables from the current function or the global variables. print can get as complex argument and expressions (pointers deferens, variable references, arithmetic expressions, almost any valid C expression). In addition, print can display data structures such as struct and union, or evaluate functions and return their result.
On most operating systems that it has been ported to, gdb can not detect when a process performs a fork operation. When the program is started, debugging takes place exclusively in the initial process, the child processes not being attached to the debugger. In this case, the only solution is to introduce delays in the execution of the newly created process (eg by sleep system call), which gives the programmer enough time to manually attach the gdb to the process, assuming he has learned PID in advance. To attach the debugger to an existing process, use the attach command as follows:
(gdb) attach PID
This method is quite inconvenient and can even cause abnormal operation of the debugged application if the process synchronization needs are strict (eg timeout operations).
Fortunately, on a limited number of systems, including Linux, gdb allows convenient debugging of programs that create multiple fork and vfork processes.