使用gdb调试FB2000
之前发现一个很奇怪的bug,就是有些站务下不了站,下站的时候就会又
SYSOP 03月26日19:31:28 brc_save_all
SYSOP 03月26日19:31:28 brc_save_all
修改成这样 CFLAGS = -ggdb -Wunused -I../include
2)在$bbshome/bbssrc/src下编译程序
4)用一个id登陆进去
bbs 7096 1 0 01:53 ? 00:00:00 ./bbsd 2008
bbs 7111 7096 0 01:55 ? 00:00:00 ./bbsd 2008
GNU gdb 6.1.1
Copyright 2004 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 "i486-slackware-linux"...Using host
504 register time_t uptime;
505 int readset;
506 int value;
507 struct timeval tv;
508
509 /* --------------------------------------------------- */
510 /* setup standalone */
511 /* --------------------------------------------------- */
512
513 start_daemon();
(gdb)
可以看到,当我们试着执行list的时候,可以看到程序的代码了。
Attaching to program: /bbs/bbssrc/src/bbsd, process 7111
Reading symbols from /lib/libtermcap.so.2...done.
Loaded symbols for /lib/libtermcap.so.2
Reading symbols from /lib/libdl.so.2...done.
Loaded symbols for /lib/libdl.so.2
Reading symbols from /lib/libnsl.so.1...done.
Loaded symbols for /lib/libnsl.so.1
Reading symbols from /lib/libcrypt.so.1...done.
Loaded symbols for /lib/libcrypt.so.1
Reading symbols from /lib/libc.so.6...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
Reading symbols from /lib/libnss_files.so.2...done.
Loaded symbols for /lib/libnss_files.so.2
0x40143c62 in select () from /lib/libc.so.6
(gdb) list
514
515 (void) signal(SIGCHLD, reapchild);
516 (void) signal(SIGTERM, close_daemon);
517
518
519 /* --------------------------------------------------- */
520 /* port binding */
521 /* --------------------------------------------------- */
522
523 xsin.sin_family = AF_INET;
当我们再执行list的时候可以看到程序执行到当前的代码,这时你登陆
8)设置断点break(b)
(gdb) b u_exit
Breakpoint 1 at 0x807b010: file main.c, line 224.
显示断点的位置。当程序执行到这个函数的时候就会停止下来,这时你
Continuing.
这里程序继续执行了,你登陆的id也可以活动了,当执行到8)我们设置
Continuing.
224 if (strcmp(currentuser.userid, "guest") != 0)
225 brc_saveall(); // 保存所有阅读记录
(gdb) s
brc_saveall () at boards.c:1172
1172 report("brc_save_all");
(gdb)
$1 = 2
(gdb) p brc_read
$2 = {{bid = 1, list = {1158002774 <repeats 60 times>}, num = 60, changed = 0 '\0', readpos = 0}, {bid = 5, list = {1158013150, 1,
0 <repeats 58 times>}, num = 2, changed = 0 '\0', readpos = 0}, {bid = 0, list = {0 <repeats 60 times>}, num = 0,
changed = 0 '\0', readpos = 0} <repeats 248 times>}
(gdb) set print pretty on
(gdb) p brc_read
$3 = {{
bid = 1,
list = {1158002774 <repeats 60 times>},
num = 60,
changed = 0 '\0',
readpos = 0
}, {
bid = 5,
list = {1158013150, 1, 0 <repeats 58 times>},
num = 2,
changed = 0 '\0',
readpos = 0
}, {
bid = 0,
list = {0 <repeats 60 times>},
num = 0,
changed = 0 '\0',
readpos = 0
} <repeats 248 times>}
1184 brc_putrecord(ptr, b_name, brc_read[i].num,
1185 brc_read[i].list);
#0 brc_putrecord (ptr=0xbfff2ed0 "junk", name=0x403d4430 "junk", num=2, list=0x8194244) at boards.c:1156
#1 0x08067319 in brc_saveall () at boards.c:1183
#2 0x0807b02d in u_exit () at main.c:225
#3 0x0805c92a in Q_Goodbye () at bbs.c:3451
#4 0x0805cda6 in Goodbye () at bbs.c:3557
#5 0x0806ca4a in domenu (menu_name=0x80bcb32 "TOPMENU") at comm_lists.c:704
#6 0x0807d221 in start_client () at main.c:1281
#7 0x0805f180 in main (argc=2, argv=0xbffff7d4) at bbsd.c:651
如:$bbshome/bbs/home/S/SYSOP/.boardbrc
分析:
后记 :(都是一些使用gdb的技巧)
参考: http://www.ibm.com/developerworks/cn/linux/l-cn-gdbmp/index.html
2)打印结构体的内容,p *structname
3)bt查看函数堆栈后,用frame可以跳到相应层调试 如:
4)set print pretty on 可以美化输出格式
----------- 2,3,4 示例-------------------------------------------------------------------------
(gdb) bt
#0 0x00007f2764232221 in ?? () from /lib/x86_64-linux-gnu/libc.so.6
#1 0x00007f27641f3e82 in vfprintf () from /lib/x86_64-linux-gnu/libc.so.6
#2 0x00007f276421a814 in vsprintf () from /lib/x86_64-linux-gnu/libc.so.6
#3 0x00007f27641fca07 in sprintf () from /lib/x86_64-linux-gnu/libc.so.6
#4 0x000000000043434a in do_userlist () at list.c:303
#5 0x00000000004344d7 in show_userlist () at list.c:363
#6 0x0000000000436205 in choose (update=1, defaultn=0, title_show=0x43380e <print_title>, key_deal=0x4344ed <show_userlist+272>,
list_show=0x4343d6 <do_userlist+1085>, read=0x435af4 <setlistrange+11>) at list.c:958
#7 0x0000000000435e15 in t_friends () at list.c:873
#8 0x000000000042abd2 in domenu (menu_name=0xe2a1a9 "M_TALK") at comm_lists.c:738
#9 0x000000000042abd2 in domenu (menu_name=0x4867bb "TOPMENU") at comm_lists.c:738
#10 0x000000000043ebda in start_client () at main.c:1331
#11 0x000000000041af58 in main (argc=2, argv=0x7fff12def758) at bbsd.c:667
(gdb) frame 4
#4 0x000000000043434a in do_userlist () at list.c:303
303 snprintf(user_info_str, STRLEN * 2,
(gdb) p *uentp
$1 = {active = 1, uid = 3, pid = 26460, invisible = 0, sockactive = 0, sockaddr = 0, destuid = 0, mode = 35, pager = 15,
in_chat = 0, fnum = 0, ext_idle = 1, chatid = "\000\000\000\000\000\000\000\000\000",
from = "localhost", '\000' <repeats 50 times>, idle_time = 1353765641, userid = "suit", '\000' <repeats 15 times>,
realname = "xxxxx", '\000' <repeats 14 times>, username = "suit", '\000' <repeats 35 times>, friend = {0 <repeats 200 times>},
reject = {0 <repeats 32 times>}, board = '\000' <repeats 79 times>, define = 4294901759}
(gdb) set print pretty on
(gdb) p *uentp
$2 = {
active = 1,
uid = 3,
pid = 26460,
invisible = 0,
sockactive = 0,
sockaddr = 0,
destuid = 0,
mode = 35,
pager = 15,
in_chat = 0,
fnum = 0,
ext_idle = 1,
chatid = "\000\000\000\000\000\000\000\000\000",
from = "localhost", '\000' <repeats 50 times>,
idle_time = 1353765641,
userid = "suit", '\000' <repeats 15 times>,
realname = "xxxxx", '\000' <repeats 14 times>,
username = "suit", '\000' <repeats 35 times>,
friend = {0 <repeats 200 times>},
reject = {0 <repeats 32 times>},
board = '\000' <repeats 79 times>,
define = 4294901759
}
(gdb)
-----------------------------------------------------------------------------------------------
5)设置断点
gdb设置断点:单个文件
b line-number(行数);
b function-name(函数);
b line-or-function if condition(条件成功,断点);
多个文件
b filename:line-number (文件名:行数)
b filename:function-name (文件名:函数)