设为首页 加入收藏

TOP

GDB调试指南-启动调试(一)
2019-01-11 22:10:35 】 浏览:228
Tags:GDB 调试 指南 启动

前言

GDB(GNU Debugger)是UNIX及UNIX-like下的强大调试工具,可以调试ada, c, c++, asm, minimal, d, fortran, objective-c, go, java,pascal等语言。本文以C程序为例,介绍GDB启动调试的多种方式。

哪类程序可被调试

对于C程序来说,需要在编译时加上-g参数,保留调试信息,否则不能使用GDB进行调试。
但如果不是自己编译的程序,并不知道是否带有-g参数,如何判断一个文件是否带有调试信息呢?

gdb 文件

例如:

$ gdb helloworld
Reading symbols from helloWorld...(no debugging symbols found)...done.

如果没有调试信息,会提示no debugging symbols found。
如果是下面的提示:

Reading symbols from helloWorld...done.

则可以进行调试。

readelf查看段信息

例如:

$ readelf -S helloWorld|grep debug
  [28] .debug_aranges    PROGBITS         0000000000000000  0000106d
  [29] .debug_info       PROGBITS         0000000000000000  0000109d
  [30] .debug_abbrev     PROGBITS         0000000000000000  0000115b
  [31] .debug_line       PROGBITS         0000000000000000  000011b9
  [32] .debug_str        PROGBITS         0000000000000000  000011fc

helloWorld为文件名,如果没有任何debug信息,则不能被调试。

file查看strip状况

下面的情况也是不可调试的:

file helloWorld
helloWorld: (省略前面内容) stripped

如果最后是stripped,则说明该文件的符号表信息和调试信息已被去除,不能使用gdb调试。但是not stripped的情况并不能说明能够被调试。

调试方式运行程序

程序还未启动时,可有多种方式启动调试。

调试启动无参程序

例如:

$ gdb helloWorld
(gdb)

输入run命令,即可运行程序

调试启动带参程序

假设有以下程序,启动时需要带参数:

#include<stdio.h>
int main(int argc,char *argv[])
{
    if(1 >= argc)
    {
        printf("usage:hello name\n");
        return 0;
    }
    printf("Hello World %s!\n",argv[1]);
    return 0 ;
}

编译:

gcc -g -o hello hello.c

这种情况如何启动调试呢?需要设置参数:

$ gdb hello
(gdb)run 编程珠玑
Starting program: /home/shouwang/workspaces/c/hello 编程珠玑
Hello World 编程珠玑!
[Inferior 1 (process 20084) exited normally]
(gdb)

只需要run的时候带上参数即可。
或者使用set args,然后在用run启动:

gdb hello
(gdb) set args 编程珠玑
(gdb) run
Starting program: /home/hyb/workspaces/c/hello 编程珠玑
Hello World 编程珠玑!
[Inferior 1 (process 20201) exited normally]
(gdb) 

调试core文件

当程序core dump时,可能会产生core文件,它能够很大程序帮助我们定位问题。但前提是系统没有限制core文件的产生。可以使用命令limit -c查看:

$ ulimit -c
0

如果结果是0,那么恭喜你,即便程序core dump了也不会有core文件留下。我们需要让core文件能够产生:

ulimit -c unlimied  #表示不限制core文件大小
ulimit -c 10       
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇单片机实现简易版shell的方法和原.. 下一篇分支程序设计练习(初学者)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目