当前位置:   article > 正文

GDB(二):启动GDB_gdb executable

gdb executable

        GDB 是GNU 开源组织发布的一个强大的UNIX 下的程序调试工具。一般来说,GDB主要帮忙你完成下面四个方面的功能:

<1>启动你的程序,可以按照你的自定义的要求随心所欲的运行程序。

<2>可让被调试的程序在你所指定的调置的断点处停住。(断点可以是条件表达式)

<3>当程序被停住时,可以检查此时你的程序中所发生的事。

<4>动态的改变你程序的执行环境。


1 GDB命令

root@ubuntu:/# gdb --help

This is the GNU debugger.  Usage:

gdb [options] [executable-file [core-file or process-id]]

gdb [options] --args executable-file [inferior-arguments ...]

其中,options包含如下:

参数

说明

--args

Arguments after executable-file are passed to inferior

--cd=DIR

Change current directory to DIR.

--command=FILE, -x

Execute GDB commands from FILE.

--eval-command=COMMAND, -ex

Execute a single GDB command.


2 启动GDB的方法

    启动GDB有以下几种方法:

<1>gdb executable-file          即通过gdb来调试可执行文件

<2>gdb executable-file core  其中,core是程序在执行过程中遇到段错误等异常时而生成的保存当时堆栈信息的文件。通过gdb + 可执行文件 + core,可用来调试查看当时发生段错误时的堆栈信息。

<3>gdb process  pid              即通过gdb来调试运行的进程

2.1 GDB 可执行文件

启动方式:gdb executable-file

<1>设置命令行参数的方法1:

root@ubuntu:/home/ # gdb executable-file

GNU gdb (GDB) 7.1-ubuntu

………

Reading symbols from /home/lincoln/lchtool/form...done.

(gdb) set args argument1 argument2                                                         /*设置参数*/

(gdb) r

<2>设置命令行参数的方法2:

root@ubuntu:/home/ # gdb --args executable-file argument1 argument2   /*直接在启动命令设置参数*/

(gdb) r

<3>设置命令行参数的方法2:

root@ubuntu:/home/ # gdb executable-file 

(gdb) r argument1 argument2          /*直接在启动命令设置参数*/

<4>显示命令行参数:

root@ubuntu:/home/ # gdb --args executable-file argument1 argument2

(gdb)show args         /*显示命令行参数*/


2.2 GDB coredump

启动方式:gdb executable-file core

root@ubuntu:/ # gdb gdbtest core-gdbtest-1669-1477049624

GNU gdb (GDB) 7.1-ubuntu

Copyright (C) 2010 Free Software Foundation, Inc.

Core was generated by `./gdbtest'.

Program terminated with signal 11, Segmentation fault.

#0  0x0000000000400548 in main () at gdbtest.c:9

9            *a = b;

(gdb) bt

#0  0x0000000000400548 in main () at gdbtest.c:9

(gdb)



2.3 GDB 运行的进程

启动方式1:gdb executable-file pid

启动方式2:gdb attach pid

启动方式3:通过gdb executable-file  关联源代码,然后在执行 attach pid 命令挂接进程,使gdb取得进程的控制权,最后通过detach来取消挂接的进程。

root@ubuntu:/# gdb gdbtest

GNU gdb (GDB) 7.1-ubuntu

……

Reading symbols from /gdbtest...done.

(gdb) attach 1686

Attaching to program: / gdbtest, process 1686

Reading symbols from /lib/libc.so.6...(no debugging symbols found)...done.

Loaded symbols for /lib/libc.so.6

Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done.

Loaded symbols for /lib64/ld-linux-x86-64.so.2

main (argc=1, argv=0x7fff9b7bc238) at gdbtest.c:25

25       }

(gdb)




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/742355
推荐阅读
相关标签
  

闽ICP备14008679号