A brief analysis of C + + and GDB's-G option

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.