gdb 使用小记

0 Preview

gdb 是 linux/uinx 下面的代码调试工具, 本文用一个简单的例子来说明一些使用。

1 Code

首先写一个简单的c程序,这个程序提供了函数的参数访问,命名为 getconf.c。

#include "stdio.h"
#include "unistd.h"  
#include "stdlib.h"
#include "stdio.h"  
#include "getopt.h"  
  
int main(int argc, char **argv)  
{  
   int opt;  
   int digit_optind = 0;  
   int option_index = 0;  
   static struct option long_options[] = {  
       {"reqarg", required_argument, NULL, 'r'},  
       {"noarg",  no_argument,       NULL, 'n'},  
       {"optarg", optional_argument, NULL, 'o'},  
       {0, 0, 0, 0}  
   };  
  
   while ( (opt = getopt_long(argc, argv, "a:b:c:d", long_options, &option_index)) != -1)  
   {  
        printf("opt = %c\n", opt);  
        printf("optarg = %s\n", optarg);  
        printf("optind = %d\n", optind);  
        printf("argv[optind - 1] = %s\n",  argv[optind - 1]);  
        printf("option_index = %d\n", option_index);  
   }  
  
   return 0;  
}  


gcc -o getconf getconf.c -g

注意这里的 -o 是指定编译后的输出二进制文件名称,-g 是指把源码信息编入二进制文件中,因为本次gdb调试需要,所以我们带上 -g 参数。

2 Usage

首先在当前目录下,敲入命令 gdb 进入 gdb调试行。

(gdb) (in here)

在光标到上面的界面时就可以输入操作命令了。

下面介绍一些简单的操作命令,以及含义:

(gdb) file $(execute_file_name)   file命令后面加一个可执行文件 代表现在gdb加载这个文件。

(gdb) r/c  $(argv_list)  直接/继续 执行,类似于在shell中 ./$(execute_file_name) ,遇到断点会停止。

(gdb) b/d   <行号> / <函数名称>   给源码的   某行/某函数   加/清除  断点。 

(gdb) s/n 相当于vs调试中的F10/F11 区别是s是进入一行及其子函数,n是执行这一行不进入子函数。他们必须在编译时带 -g 参数才可以使用,也就是必须把源码信息编译进可执行文件中。

(gdb) p $(param) 打印变量的值,注意代码要执行到变量定义之后,销毁之前才可以。

(gdb) i r  这里的 i 是 Info 的意思,用于显示各类信息,丰富输出。

(gdb) q 这里的q是表示退出 gdb 调试。

3 Reference

https://blog.csdn.net/liigo/article/details/582231

man gdb












猜你喜欢

转载自blog.csdn.net/mistakk/article/details/80054444
今日推荐