Use GDB debugging procedures (1)

zhaozj2021-02-08  253

GDB debugging program

GDB overview ----

GDB is a powerful UNIX published by GNU open source organization. Perhaps, you prefer the graphical interface method, like the debugging of IDE, etc., but if you are software under the UNIX platform, you will find that the GDB debugging tool has a graphic debugger than VC, BCB. powerful functions. The so-called "inch is a long, the ruler is short" is this truth.

In general, GDB mainly helps you complete the following four features:

1. Start your program, you can run the program as you want to follow your custom requirements. 2. Allow the debugged program to stop at the breakpoint you specified. (Breakpoint can be conditional expression) 3, when the program is stopped, you can check what happened in your program. 4. Dynamic changes your program's execution environment.

From above, GDB and general debugging tools have no two, basically complete these features, but in detail, you will find the power of the debugging tool of GDB, everyone may be used to the graphical debugging tool, but there is At the time, the debug tool of the command line has the function that cannot be completed. Let us look in one by one.

A debug example ------

Source: tst.c

1 #include 2 3 int fullc (int N) 4 {5 int sum = 0, i; 6 for (i = 0; i

Compilation Generation Document: (under Linux) HCHEN / TEST> CC -G TST.C -O TST

Use GDB debugging:

HCHEN / TEST> GDB TST <---------- Starting GDBGNU GDB 5.1.1Copyright 2002 Free Software Foundation, Inc.gdb is Free Software, Covered by The gnu general public liclecense, and you arewelcome to change IT and YOU / or distribute copies of it under certain conditions.Type "show copying" to see the conditions.There is absolutely no warranty for GDB. Type "show warranty" for details.This GDB was configured as "i386-suse-linux" .. (GDB) L <------------------ l Commands is equivalent to List, and the original code is started from the first line. 1 #include 23 int fullc (int N) 4 {5 int sum = 0, i; 6 for (i = 0; i

Breakpoint 1, Main () at tst.c: 17 <---------- Announced at the breakpoint. 17 Long Result = 0; (GDB) N <-------------------- Single statement execution, next command is short. 18 for (i = 1; i <= 100; i ) (gdb) N20 results = I; (GDB) N18 for (i = 1; i <= 100; i ) (GDB) N20 results = i; GDB) c <--------------------- Continue to run the program, and the contractue command is short. Continuing.Result [1-100] = 5050 <---------- Program Output. Breakpoint 2, Func (n = 250) at tst.c: 55 int sum = 0, i; (gdb) N6 for (i = 1; i <= n; i ) (GDB) PI <------ --------------- Print the value of the variable I, the print command is short. $ 1 = 134513808 (GDB) N8 SUM = I; (GDB) N6 for (i = 1; i <= n; i ) (GDB) P Sum $ 2 = 1 (GDB) N8 SUM = I; (GDB) PI $ 3 = 2 (GDB) N6 for (i = 1; i <= n; i ) (GDB) P Sum $ 4 = 3 (GDB) BT <----------------- - View function stack. # 0 func (n = 250) At tst.c: 5 # 1 0x080484e4 in main () at tst.c: 24 # 2 0x400409ed in __libc_start_main () from /lib/libc.so.6(gdb) Finish <- ------------------ exit function. Run Till Exit from # 0 func (n = 250) at Tst.c: 50x080484e4 in main () at tst.c: 2424 Printf ("Result [1-250] =% D / N", FUNC (250)); Value Returned IS $ 6 = 31375 (GDB) C <-------------------- Continue to run. Continuing.Result [1-250] = 31375 <---------- Program Output.

Program evted with code 027. <-------- Program exits, the debugging ends. (GDB) Q <-------------------- exits GDB. HCHEN / TEST>

Ok, has the above feelings, or let us systematically meet GDB.

Use GDB ----

Generally, GDB mainly debugging is a program of C / C . To debug the C / C program, first in compile, we must add debug information to the executable. This can be done using the -g parameter using the compiler (CC / GCC / G ). Such as:> cc -g hello.c -o hello> g -g hello.cpp -o hello

If there is no -g, you will see the function name, variable name, which instead, all of which is the memory address of the runtime. When you use -g to join the debug information, then successfully compile the target code, let's take a look at how to use GDB to debug him.

There are several ways to start GDB:

1, GDB Program is also your executable, generally under the directory.

2, GDB Core uses GDB to simultaneously debug a running program and core file, and Core is the file generated after the program is illegally executed.

3, GDB If your program is a service, you can specify the process ID of this service run. GDB will automatically go up to Attach and debug him. Program should be searched in the PATH environment variable.

When GDB is started, you can add some GDB launch switches. Detailed switches can be viewed with GDB -HELP. I only exemplified some commonly used parameters:

-Symbols -s Read symbol tables from the specified file.

-SE file reads symbolic table information from the specified file and uses him in the executable.

-core -c debugging the core file of Core Dump.

-directory -d Add a search path for a source file. The default search path is the path defined in the environment variable.

Next->

(All rights reserved, please indicate the author and the source when reproduced)

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

New Post(0)