GDB tips (1) - Overview and start

GDB Overview

GDB is a debugging tool under a powerful UNIX GNU open source release. Perhaps you prefer the kind of graphical interface, like debugging VC, BCB IDE, etc., but if you are doing software in the UNIX platform, you will find the GDB debugger tool more than VC, BCB graphical debugger powerful functions. The so-called "inch a director, has a short foot" is the truth.

In general, GDB help you complete the main functions of the following four areas:

    1, start your program, you can be arbitrary in accordance with the requirements of your custom program.
    2, can let the program being debugged stopped at a breakpoint tone set you specify. (Breakpoints can be conditional expressions)
    3, when the program is stopped, then you can check what's happened in the program.
    4, dynamic change your program's execution environment.

From the above it seems, GDB debugging tool and general no different, basically accomplish these functions, but in the details, you will find this powerful GDB debugging tool, we may be more accustomed to the graphical debugging tool, but there are when debugging command line tool can not be completed but it has the function of graphical tools. Let us have it seems.

Use GDB

In general GDB debugger it is mainly C / C ++ program. To debug C C ++ program /, first at compile time, we have to put debugging information to the executable file. Compiler (cc / gcc / g ++) is -g parameter can do it. Such as:

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

If no -g, you will not see the name of the function procedures, variable names, all of which are replaced by run-time memory address. When you use the -g debugging information is added, and after successfully compiled object code, let's look at how to use gdb debugging him.

Way to start GDB are the following:

    1, gdb <Program>
       Program is your executable file, usually in the course catalog.

    2, gdb <program> core
       with gdb to debug a running program and the core file, core file is generated by the implementation of the program illegal after core dump.

    3, gdb <program> <PID >
       If your program is a service program, you can specify the process ID of the service program is running. gdb will automatically attach up, and commissioning him. Search program should get in the PATH environment variable.

When starting GDB, GDB can add some of the start switch, the switch may -help detailed view with gdb. Below I've only mentioned some of the more commonly used parameters:

  • -symbols <file>
  • -s <file>

The symbol table is read from the specified file.

  • -the file

Reads the symbol table information from the specified file, and put him in with the executable file.

  • -core <file>
  • -c <file>

The core core dump files when debugging.

  • -directory <directory>
  • -d <directory>

Join the search path of a source file. The default search path is the path defined by the PATH environment variable.

COMMISSIONING

Source: test.c

#include <stdio.h>

int func(int n)
{
        int sum=0,i;
        for(i=0; i<n; i++)
        {
                sum+=i;
        }
        return sum;
}


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) );
}

编译生成执行文件:(Linux下)
    hchen/test> cc -g tst.c -o tst

使用GDB调试:

hchen/test> gdb tst  <---------- 启动GDB
GNU gdb 5.1.1
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/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命令相当于list,从第一行开始例出原码。
1        #include <stdio.h>
2
3        int func(int n)
4        {
5                int sum=0,i;
6                for(i=0; i<n; i++)
7                {
8                        sum+=i;
9                }
10               return sum;
(gdb)       <-------------------- 直接回车表示,重复上一次命令
11       }
12
13
14       main()
15       {
16               int i;
17               long result = 0;
18               for(i=1; i<=100; i++)
19               {
20                       result += i;   
(gdb) break 16    <-------------------- 设置断点,在源程序第16行处。
Breakpoint 1 at 0x8048496: file tst.c, line 16.
(gdb) break func  <-------------------- 设置断点,在函数func()入口处。
Breakpoint 2 at 0x8048456: file tst.c, line 5.
(gdb) info break  <-------------------- 查看断点信息。
Num Type           Disp Enb Address    What
1   breakpoint     keep y   0x08048496 in main at tst.c:16
2   breakpoint     keep y   0x08048456 in func at tst.c:5
(gdb) r           <--------------------- 运行程序,run命令简写
Starting program: /home/hchen/test/tst

Breakpoint 1, main () at tst.c:17    <---------- 在断点处停住。
17               long result = 0;
(gdb) n          <--------------------- 单条语句执行,next命令简写。
18               for(i=1; i<=100; i++)
(gdb) n
20                       result += i;
(gdb) n
18               for(i=1; i<=100; i++)
(gdb) n
20                       result += i;
(gdb) c          <--------------------- 继续运行程序,continue命令简写。
Continuing.
result[1-100] = 5050       <----------程序输出。

Breakpoint 2, func (n=250) at tst.c:5
5                int sum=0,i;
(gdb) n
6                for(i=1; i<=n; i++)
(gdb) p i        <--------------------- 打印变量i的值,print命令简写。
$1 = 134513808
(gdb) n
8                        sum+=i;
(gdb) n
6                for(i=1; i<=n; i++)
(gdb) p sum
$2 = 1
(gdb) n
8                        sum+=i;
(gdb) p i
$3 = 2
(gdb) n
6                for(i=1; i<=n; i++)
(gdb) p sum
$4 = 3
(gdb) bt        <--------------------- 查看函数堆栈。
#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    <--------------------- 退出函数。
Run till exit from #0  func (n=250) at tst.c:5
0x080484e4 in main () at tst.c:24
24              printf("result[1-250] = %d /n", func(250) );
Value returned is $6 = 31375
(gdb) c     <--------------------- 继续运行。
Continuing.
result[1-250] = 31375    <----------程序输出。

Program exited with code 027. <--------程序退出,调试结束。
(gdb) q     <--------------------- 退出gdb。
hchen/test>

 

Original link: https://blog.csdn.net/haoel/article/details/2879

Published 175 original articles · won praise 262 · views 700 000 +

Guess you like

Origin blog.csdn.net/li_wen01/article/details/105221599