An analysis of GDB's-G option Get ready
GDB is the most commonly used debug tool for learning C + + under Linux. To use this powerful tool, you must add the-G option at compile time for the resulting target file to be debugged with GDB.
For example, first create a test.c file with the following content:
1 #include <stdio.h>2 3 int Main (void 4 int a[2 ]; 5 a[0 ] = ; 6 a[1 ] = ; 7 return 0 8 }
Compile by using the Compile statement:
gcc -G test.c-o test.out
Analysis of the-G option
The function of the-G option is to include the source information in the build-generated target file. For example, the machine instruction in the target file corresponds to the number of lines in the source code. Note that this does not add the entire source code to the target file, but simply adds the corresponding line number information , so when debugging, you must ensure that GDB can find the source file.
Here is a small test to verify:
(1) First for the above file debugging:
1 gdb test.out 2 (GDB) Start
As follows:
As you can see, the first breakpoint is line 5th, and the source code is:
A[0] = 1;
This time, if you add 1 lines to the first and third rows in the source file, then run GDB (note, do not run GCC compilation)
1 gdb test.out 2 (GDB) Start
At this point, found that the first breakpoint gdb is still the 5th row, but the source is the original line.
This validates our assertion that "gdb simply adds the corresponding line number information, not the embedded source code."
Extended
The-G option has three levels. That
-g1
Does not contain local variables and debug information related to line numbers, so it can only be used for backtracking tracing and stack storage and function invocation, and so on.
-g2
The default level at which debug information is generated including extended symbol tables, line numbers, local or external variable information
-g3
Contains all debugging information in Level 2, as well as macros defined in the source code