Linux Advanced Programming--04.gdb Debug Program (Getting Started overview)

Source: Internet
Author: User
Tags aliases

GDB Overview

GDB is a powerful UNIX program debugging tool released by the GNU Open source organization. Perhaps, you prefer that graphical interface, such as VC, BCB and other IDE debugging, but if you are in the UNIX platform to do software, you will find GDB this debugging tool than VC, BCB graphical debugger more powerful features. The so-called "inch, the ruler is short" is this truth.

In general, GDB is the main help you complete the following four aspects of the function:

    1. Start your program and you can run the program as you wish, in accordance with your custom requirements.
    2. Allows the program to be debugged to stop at the breakpoint you have specified for the reset. (Breakpoint can be a conditional expression)
    3. When the program is stopped, you can check what happens in your program at this time.
    4. Dynamically change the execution environment of your program.

From the above, GDB and the General debugging tool is not the same, basically is to complete these functions, but in the details, you will find GDB this debugging tool is powerful, you may be more accustomed to the graphical debugging tools, but sometimes, the command line debugging tools have a graphical tool can not be done. Let's see.

A debugging example
//test.c#include <stdio.h>int func(int n) {    int sum=0,i;    for(i=0; i<n; i++){            sum+=i;    }    return sum;}int main() {    int i;    long result = 0;    for(i=1; i<=100; i++){        result += i;    }    printf("result[1-100] = %d \n", result );    printf("result[1-250] = %d \n", func(250) );}

Compile Build:

gcc -g test.c -o test./testresult[1-100] = 5050result[1-250] = 31125
Debugging with GDB:
GDB Test <----------start Gdbgnu gdb (gdb) SUSE (7.1-8.9.1) Copyright (C) Free Software Foundation, Inc. . License gplv3+: GNU GPL version 3 or later 

Well, with the above perceptual knowledge, or let us have a systematic understanding of gdb it.

Using GDB

In general, GDB is mainly debugging the C + + program. In order to debug a C + + program, first at compile time, we have to add debug information to the executable file. This can be done using the-g parameter of the compiler (cc/gcc/g++). Such as:

> cc -g hello.c -o hello> g++ -g hello.cpp -o hello

Without-G, you will not see the program's function name, variable name, instead of the memory address of the runtime. After you have added the debug information with-G and successfully compiled the target code, let's look at how to debug it with GDB.

There are several ways to start gdb:

    1. GDB : Program is your execution file, usually under the directory of course.

    2. GDB Core: Debug a running program and core file with GDB at the same time, the core is the file generated after the core dump is executed illegally by the program.

    3. GDB : If your program is a service program, you can specify the process ID at which the service program runs. GDB will automatically attach up and debug him. The program should be searched in the PATH environment variable.

GDB start, you can add some gdb start switch, detailed switch can be used to gdb -help view. I'll just cite some of the more commonly used parameters below:

    • -symbols /s : reads the symbol table from the specified file.
    • -se file: Reads the symbol table information from the specified file and uses it in the executable file.
    • -core /C : Core dump cores file when debugging.
    • -directory /D : Adds a search path to a source file. The default search path is the path defined by path in the environment variable.
GDB's command

Once GDB is started, you can start debugging with GDB's commands, as soon as you are brought into GDB's debug environment, and GDB commands can be viewed using the Help command, as shown below:

Gdbgnu gdb (GDB) SUSE (7.1-8.9.1) Copyright (C) free software Foundation, inc.license gplv3+: GNU GPL version 3 or lat Er 

GDB has a lot of commands, and gdb divides it into many different categories. The help command is just an example of the type of GDB command, and if you want to see the commands in the category, you can use the Breakpoints command, such as: helps, to see all the commands that set breakpoints. You can also directly help to see the commands.

GDB, the input command, you can not play the full command, only the first few characters to play the command can be, of course, the first few characters of the command should be marked a unique command, under Linux, you can hit two times tab to fill the command, if there are duplicates, then GDB will take its example.

Example one: When entering the function func, set a breakpoint. You can either knock the break func, or directly be B func.

(gdb) b funcBreakpoint 1 at 0x400613: file test.c, line 3.

Example two: Typing B by pressing two times tab, you will see all the B commands:

(gdb) bbacktrace bookmark break bt

Example three: Just remember the prefix of the function, so you can:

(gdb) b f <按两次TAB键>frame_dummy funcGDB把所有make开头的函数全部例出来给你查看。

Example four: When you debug a C + + program, you can have the same function name. Such as:

(gdb) b bubble( <按两次TAB键>bubble(double,double) bubble(int,int)你可以查看到C++中的所有的重载函数及参数。(注:M-?和“按两次TAB键”是一个意思)

To exit GDB, simply send a quit or a command called Q.

A shell program that runs UNIX in GDB

In the GDB environment, you can execute the UNIX shell command, using GDB's shell command to complete:

(gdb) shell  lstest.c  test  test.o

There is also a gdb command, make, which can be executed in gdb to build your own program again.

(gdb) make
Run the program in GDB

When GDB is started in gdb, GDB searches the path path and the current directory for the source files. To confirm that GDB is reading the source file, you can use the L or List command to see if GDB can list the source code.

In GDB, run the program using the R or Run command. program runs, you may need to set up the following four things.

    1. Program run parameters.

      • Set args: You can specify run-time parameters. (Example: Set args 10 20 30 40 50)
      • Show args: command to view the set of running parameters.
    2. Operating environment.

      • Path [dir]: You can set the running path of the program.
      • Show paths: View the running path of the program.
      • Set environment VarName [=value]: Sets environment variables. such as: Set env User=hchen
      • Show environment [varname]: View environment variables.
    3. Working directory.

      • cd [dir]: equivalent to the Shell's CD command.
      • PWD: Displays the current directory.
    4. The input and output of the program.

      • Info Terminal: Shows the mode of terminal used by your program.
      • Use redirection control program output. such as: Run > outfile
      • A TTY command can refer to an end device that writes input and output. such as: Tty/dev/ttyb


From for notes (Wiz)

Linux Advanced Programming--04.gdb Debug Program (Getting Started overview)

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.