Debug with GDB in Linux

To debug a process with GDB, you need to compile the program with debugging information first so that GDB can correctly interpret the program’s symbol table. When debugging with GDB, you can follow these steps:

  1. Add debugging information when compiling the program. Use the -g option of the compiler to generate an executable file with debugging information. For example, compile a C program using the following command:
  2. Compile the program.c file with debug information and name the executable program.
  3. Run GDB in the command line and specify the executable file you want to debug.
  4. debugging program
  5. Set a breakpoint in the GDB command line. By setting a breakpoint on a specific line of source code, you can specify where the program should stop for debugging. For example, to set a breakpoint on line 10, use the following command:
  6. Take a 10-minute break.
  7. Start the program. Use the run command to launch the program and start debugging. For example:
  8. sprint
  9. When the program reaches a breakpoint, GDB will pause and wait for your command. You can use the ‘next’ command to execute step by step, the ‘step’ command to execute line by line, or the ‘continue’ command to proceed with the program until the next breakpoint.
  10. Obtain the value of a variable during runtime. Using the print command allows you to view the current value of the variable when the program pauses. For example:
  11. output the variable
  12. Track function calls during program execution. Use the backtrace command to view the current function call stack, and the up and down commands to navigate up and down the function call stack.
  13. During the debugging process, you can also use other GDB commands to view and modify the program’s status, such as the watch command to monitor the changes in variable values, the set command to modify variable values, and the info command to view the current program state.

After finishing debugging, you can exit GDB by using the quit command.

These are just the basic steps for debugging with GDB. GDB also offers more advanced debugging features, such as conditional breakpoints, memory inspection, etc. You can refer to the GDB documentation or use the help command to obtain more information.

bannerAds