gdb 调试跟踪运行

 
有些时候程序会出现预料之内的错误,但是这些错误信息,对于其他接手代码的人员而言,依然是无法直接定位错误的。这时候,可以使用以下方法快速跟踪定位代码的每一步,来定位错误。
 
 
 
常用命令
    set  args  用于添加程序运行参数
    list          显示代码内容
    break      设置运行断点,可以直接添加行数
    step        单步执行,多用于进入函数内部继续调试
    next        单步执行
 
    continue  程序暂停时,继续运行程序。调试时,若执行,可以当做一个断点到另一个断点
 
    print   可用于打印程序中的变量值
 
 
示例:
[root@jiangmin bin]# gdb -q DaemonServer  运行时不显示gdb提示信息
Reading symbols from /usr/local/jiangmin/bin/DaemonServer...done.
(gdb) set args -c ../conf/scan_server.cfg -i
(gdb)
(gdb) list                #这里列出了min程序,默认只有10行,通过回车显示了额外内容,也可以设置参数,一次显示多行
1255                strcpy(cfg->LicenseErr, "Error:License Check Failed.");
1256                break;
1257        }
1258        return -7;
1259    }
1260    
1261    
1262    int main(int argc, char *argv[])
1263    {
1264        struct scanSrvConfig_t *cfg = &g_cfg;
(gdb)
1265        struct scanSrvEng_t    eng;
1266        struct scanSrvStatus_t status;
1267    
1268        ss_argv_parse(argc, argv);
1269    
1270        if ( g_configfile == NULL )
1271        {
1272            ss_usage(argc, argv);
1273            return 0;
1274        }
(gdb)
1275        printf("%s\n", g_configfile);
1276        memset(&status, 0x00, SizeSt(scanSrvStatus_t));
1277        //if ( ss_load_config("../conf/scan_server.cfg", cfg) < 0 )
1278        if ( ss_load_config(g_configfile, cfg) < 0 )
1279        {
1280            return 0;
1281        }
1282    
1283        checkLicense(cfg, cfg->License);
1284        ss_init_server(cfg, &eng);
(gdb)
1285    
1286        ss_create_server(cfg, &eng, &status);
1287    
1288        ss_fini_server(&eng);
1289    }
1290    
1291    
1292    
1293    
1294    
(gdb) break 1284                    标识运行到server.cpp的1284行会停下来
Breakpoint 1 at 0x412eeb: file server.cpp, line 1284.
(gdb) run                                开始运行
Starting program: /usr/local/jiangmin/bin/DaemonServer -c ../conf/scan_server.cfg -i
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
../conf/scan_server.cfg
[2020-03-21 15:25:45][SYS ]    Load Config : success.
 
Breakpoint 1, main (argc=4, argv=0x7fffffffe108) at server.cpp:1284
1284        ss_init_server(cfg, &eng);
Missing separate debuginfos, use: debuginfo-install glibc-2.17-222.el7.x86_64 libgcc-4.8.5-39.el7.x86_64
(gdb) step             #步入函数内部,此时跳到了另一个文件中
ss_init_server (cfg=0x618d60 <g_cfg>, eng=0x7fffffffe010) at server.cpp:394
394        if ( ss_init_eng(cfg, eng) != 0 )
(gdb) list
389        return 0;
390    }
391    
392    int  ss_init_server(struct scanSrvConfig_t *cfg, struct scanSrvEng_t *eng)
393    {
394        if ( ss_init_eng(cfg, eng) != 0 )
395        {
396            return -1;
397        }
398    
(gdb)
399        g_dataQueue = (CDataQueue *)malloc(sizeof(CDataQueue) * 65535 );
400        if ( g_dataQueue == NULL )
401        {
402            ss_dp_err("cannot init g_dataQueue\n");
403            return -2;
404        }
405    
406        return 0;
407    }
 
(gdb) break 394
Breakpoint 2 at 0x410782: file server.cpp, line 394.
(gdb) continue
Continuing.
[2020-03-21 15:37:10][SYS ]    Init Engine ...
 
[2020-03-21 15:37:15][ERR ]    WBInitEng failed. -1  
[2020-03-21 15:37:15][SYS ]    -----------Server Start Working...---------
 
 
^C
Program received signal SIGINT, Interrupt.
0x00007ffff71d5163 in __epoll_wait_nocancel () from /lib64/libc.so.6
(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /usr/local/jiangmin/bin/DaemonServer -c ../conf/scan_server.cfg -i
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
../conf/scan_server.cfg
[2020-03-21 15:38:20][SYS ]    Load Config : success.
 
Breakpoint 1, main (argc=4, argv=0x7fffffffe108) at server.cpp:1284
1284        ss_init_server(cfg, &eng);
(gdb) step
 
Breakpoint 2, ss_init_server (cfg=0x618d60 <g_cfg>, eng=0x7fffffffe010) at server.cpp:394
394        if ( ss_init_eng(cfg, eng) != 0 )
(gdb) list
389        return 0;
390    }
391    
392    int  ss_init_server(struct scanSrvConfig_t *cfg, struct scanSrvEng_t *eng)
393    {
394        if ( ss_init_eng(cfg, eng) != 0 )
395        {
396            return -1;
397        }
398    
(gdb) step
ss_init_eng (cfg=0x618d60 <g_cfg>, eng=0x7fffffffe010) at server.cpp:345
345        ss_dp_p("SYS ", "Init Engine ...\n");
(gdb) list
340    }
341    
342    
343    int  ss_init_eng(struct scanSrvConfig_t *cfg, struct scanSrvEng_t *eng)
344    {
345        ss_dp_p("SYS ", "Init Engine ...\n");
346    
347        if ( cfg->LoadLibMode == 0 )
348        {   //重新生成map文件
349            if ( WBInitEng(cfg->LibPath) != 0 )
(gdb)
350            {
351                ss_dp_err("WBInitEng failed. -1  \n");
352                return -1;
353            }
354        }
355        else if ( cfg->LoadLibMode == 1 )
356        {   //直接使用map文件
357            if ( WBInitEngMap(cfg->LibPath) != 0 )
358            {
359                ss_dp_err("WBInitEng failed. -2  \n");
(gdb)
360                return -2;
361            }
362        }
363        else
364        {
365            ss_dp_err("Config:LoadLibMode=%d\n", cfg->LoadLibMode);
366            return -3;
367    
368        }
369    
(gdb) break 349
Breakpoint 3 at 0x41063b: file server.cpp, line 349.
(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) n
Program not restarted.
(gdb) start
The program being debugged has been started already.
Start it from the beginning? (y or n) n
Program not restarted.
(gdb) c
Continuing.
[2020-03-21 15:40:57][SYS ]    Init Engine ...
 
Breakpoint 3, ss_init_eng (cfg=0x618d60 <g_cfg>, eng=0x7fffffffe010) at server.cpp:349
349            if ( WBInitEng(cfg->LibPath) != 0 )
(gdb) c
call              cd                clear             collect           compare-sections  condition         core-file         
catch             checkpoint        clone-inferior    commands          complete          continue          
(gdb) step
WBInitEng (libDir=0x618e80 <g_cfg+288> "../lib") at ../../src/WBEng/WBEng.cpp:109
109        struct whiteListHandle *w_handle = NULL;
(gdb) list
104        return 0;
105    }
106    
107    int WBInitEng (const char* libDir)
108    {
109        struct whiteListHandle *w_handle = NULL;
110        struct blackListHandle *b_handle = NULL;
111        char   libPath[LibPathMax];
112    
113    #ifdef _WIN32
(gdb) next
110        struct blackListHandle *b_handle = NULL;
(gdb) next
116        snprintf(libPath, LibPathMax, "%s/WhiteLib", libDir);
(gdb) print libPath
$1 = '\000' <repeats 16 times>, "\t\305u^\000\000\000\000 \035J\367\377\177\000\000\220\337\377\377\377\177\000\000\000\000\000\000\000\000\000\000\220\337\377\377\377\177\000\000I\374@\000\000\000\000\000\001?A\000\000\000\000\000W9A\000\000\000\000\000\243\266A4_MϾ\020\000\000\000\060\000\000\000\240\337\377\377\377\177\000\000\340\336\377\377\377\177\000\000B\034\331/\337\a\256\213\211\033\357\304\000\000\000\000\020\340\377\377\377\177\000\000\000\000\000\000\000\000\000\000\060\330\377\377\377\177\000\000\236\246\025\204 +h\027P\337\377\377\377\177\000\000\000\000\000\000\000\000\000\000p\337\377\377\377\177\000\000\000\032@\000\000\000\000\000\000\341\377\377\377\177", '\000' <repeats 18 times>...
(gdb) next
118        w_handle = whiteListInit(libPath, modeDefault);
(gdb) step
whiteListInit (blkLibDir=0x7fffffffde70 "../lib/WhiteLib", mode=modeDefault) at ../../src/WBEng/hashmap.cpp:148
148        struct whiteListHandle *handle = NULL;
(gdb) next
150        switch ( mode )
(gdb) next
153                handle = whiteListInitDefault(blkLibDir);
(gdb) print blkLibDir
$2 = 0x7fffffffde70 "../lib/WhiteLib"
(gdb)
 
 
 
 
 
 
 
posted @ 2020-12-19 21:28  dos_hello_world  阅读(710)  评论(0)    收藏  举报