GDB User Guide

xiaoxiao2021-03-06  121

GDB (GNU Debugger) is a GNU debugger, generally used by GNU Compiler Collection. To use GDB for debugging, you want to specify -g or -ggdb compilation option when compiling. Such as:

gcc -g main.c

GCC -GGDB main.c

In this way, GCC generates debug messages when generating executables. -g is used to generate a general debug message, and -GGDB is used to generate GDB-specific debug messages. When using -GGDB, the size of the executable will be greatly increased.

The basic directive of GDB will be explained below:

f (i le): Specify an executable file for debugging, GDB will read some file debug messages, such as f a.exe

l (iest): Column Program Export File

R (UN): After loading the executable to be debugged, you can run the executable with the run command.

B (Reak): Setting break points, such as B 25, set a breakpoint on the 25th line of the source program, which is interrupted when the program executes to 25th; also can also use B FuncName, FuncName is the name of the function, when the program calls some functions, the interrupt is generated.

c (ontinue): c Command can continue to execute, until the next interrupt point or program ends

P (RINT): Enter the value of a variable, if the program defines an int AA, P AA outputs the current value of AA

N (EXT): The program is executed when the program executes the breakpoint, and can perform single-step execution with the n command.

S (TEP): The program is interrupted when the program executes, and can perform a single step with the S command to perform a function.

Q (UIT): Exit GDB

Let us now give a simple example to illustrate the use of GDB, suppose we have the following programs:

/ ************************************************** ***********************************

GDB_SAMPLE.C

*********************************************************** ********************************** /

#include

Void Println (Const Char * PMSG)

{

Printf ("% S / N", PMSG);

}

Int main (int Argc, char * argv [])

{

Println ("Hello GDB");

Return 0;

}

Press the following command compiler GCC -G GDB_SAMPLE.C -O A.EXE to generate a.exe's executable. To use a GDB debugger, execute:

GDB a.exe

In this way, we entered the GDB debugging environment. Enter List in the command line of GDB, GDB prints out of the above source, and enter the list again, turn the page. Next, we enter B 13, indicating the 13th line of Println ("Hello GDB") in the source program. After setting breakpoints, we start executing the program, enter RUN in the command line, and a.exe begins to execute. Since we set a breakpoint on page 13, the program will be interrupted at Println ("Hello GDB"). At this time, we can enter S, GDB will run in the println function single step, then enter N, the program will Execute this statement: Printf ("% s / n", pmsg), at this time, we will enter the Print PMSG again, and GDB outputs the value of PMSG: "Hello GDB". Finally, we enter C, the program continues, and then exits normally. I hope this article will help with comrades who have just started using GDB.

MA811 at 2004-10-22

转载请注明原文地址:https://www.9cbs.com/read-102377.html

New Post(0)