您的位置:首页 > 其它

GDB调试错误的常见方法

2015-01-14 13:57 441 查看
1、加载core文件

$ gdb IMUserFollowServer.symbol core.2564

(gdb) info thread
15 process 2564  0x0000003baa4c9c5c in epoll_wait () from /lib64/tls/libc.so.6
14 process 2566  0x0000003baa4901d5 in __nanosleep_nocancel () from /lib64/tls/libc.so.6
13 process 2567  0x0000003baa4901d5 in __nanosleep_nocancel () from /lib64/tls/libc.so.6
12 process 2568  0x0000003baa4901d5 in __nanosleep_nocancel () from /lib64/tls/libc.so.6
11 process 2569  0x0000003baa4901d5 in __nanosleep_nocancel () from /lib64/tls/libc.so.6
10 process 2570  0x0000003baa4901d5 in __nanosleep_nocancel () from /lib64/tls/libc.so.6
9 process 2571  0x0000003baa4901d5 in __nanosleep_nocancel () from /lib64/tls/libc.so.6
8 process 2572  0x0000003baa4901d5 in __nanosleep_nocancel () from /lib64/tls/libc.so.6
7 process 2573  0x0000003baa4901d5 in __nanosleep_nocancel () from /lib64/tls/libc.so.6
6 process 2574  0x0000003baa4901d5 in __nanosleep_nocancel () from /lib64/tls/libc.so.6
5 process 2575  0x0000003baa4901d5 in __nanosleep_nocancel () from /lib64/tls/libc.so.6
4 process 2576  0x0000003baa4901d5 in __nanosleep_nocancel () from /lib64/tls/libc.so.6
3 process 2577  0x0000003baa4901d5 in __nanosleep_nocancel () from /lib64/tls/libc.so.6
2 process 2578  0x0000003baa4901d5 in __nanosleep_nocancel () from /lib64/tls/libc.so.6
* 1 process 2579  UserFollowTask::cmdMsgParse_Forward_Client (this=0x2a97e73010, requesterid=57438039, cmd=0x2a9a2f67aa, cmdLen=Variable "cmdLen" is not available.
2、判断是0号线程出问题了,其他线程都是正常的。切到0号线程,查看堆栈

(gdb) thread 0
Thread ID 0 not known.
(gdb) bt
#0  UserFollowTask::cmdMsgParse_Forward_Client (this=0x2a97e73010, requesterid=57438039, cmd=0x2a9a2f67aa, cmdLen=Variable "cmdLen" is not available.
)
at UserFollowTask.cpp:1042
#1  0x000000000046e7df in UserFollowTask::cmdMsgParse_Forward (this=0x8a67b8, cmd=Variable "cmd" is not available.
) at UserFollowTask.cpp:156
#2  0x000000000046e84a in UserFollowTask::cmdMsgParse (this=0x8a67b8, cmd=0x48bfccac, cmdLen=1075) at UserFollowTask.cpp:131
#3  0x000000000046ff54 in UserFollowTaskManager::execEvery (this=Variable "this" is not available.
) at ../base/MessageQueue.h:24
#4  0x000000000046418d in UserFollowTimeTick::run (this=0xe48be10) at ../base/zSingleton.h:31
#5  0x000000000049f249 in zThread::threadFunc (arg=Variable "arg" is not available.
) at zThread.cpp:48
#6  0x0000003baab06137 in start_thread () from /lib64/tls/libpthread.so.0
#7  0x0000003baa4c9883 in clone () from /lib64/tls/libc.so.6
3、解析cmd=0x2a9a2f67aa的时候出的问题,查看这个cmd的结构体。

struct stRequestFollowedSingerInfo
{
BYTE byCmd;
BYTE byParam;
DWORD dwTimestamp;
BYTE byClientFlag;
WORD size;
DWORD list[0];
}
4、无符号的情况下,直接用二进制的形式查看,由于是可变长度结构体。一段一段的根据相应的类型显示。(b:1 byte h:2 bytes w:4 bytes g:8 bytes)
(gdb) p/x *((BYTE*) 0x1fd74190)@37
$1 = {0x3, 0x3b, 0xc4, 0x5a, 0x5f, 0x3, 0xfe, 0x51, 0x9c, 0x3, 0xb, 0x0, 0x0, 0x0, 0x32, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x42,
0xa0, 0x3a, 0x0, 0x0, 0x0, 0x0, 0x0, 0xbc, 0x8f, 0x0, 0x0, 0x0, 0x0, 0x0}


(gdb) x/10ub 0x2a9a2f67aa
0x2a9a2f67aa:   3       159     136     235     127     237     90      139
0x2a9a2f67b2:   247     18
(gdb) x/10xw 0x2a9a2f67aa+2
0x2a9a2f67ac:   0xed7feb88      0x12f78b5a      0xe0913ee3      0x8354e3dd
0x2a9a2f67bc:   0xa503c9ff      0x0003644f      0x36340000      0x5d373335
0x2a9a2f67cc:   0x4d03177d      0xdc035f2e
(gdb) x/10ub 0x2a9a2f67aa+6
0x2a9a2f67b0:   90      139     247     18      227     62      145     224
0x2a9a2f67b8:   221     227
(gdb) x/10uh 0x2a9a2f67aa+7
0x2a9a2f67b1:   63371   58130   37182   56800   21731   65411   969     20389
0x2a9a2f67c1:   868     0
(gdb) x/10xw 0x2a9a2f67aa+9
0x2a9a2f67b3:   0x913ee312      0x54e3dde0      0x03c9ff83      0x03644fa5
0x2a9a2f67c3:   0x34000000      0x37333536      0x03177d5d      0x035f2e4d
0x2a9a2f67d3:   0x0364b5dc      0x0364b5dc
5、根据上面的显示得到结构体结果如下:

struct stRequestFollowedSingerInfo
{
BYTE byCmd = 3;
BYTE byParam = 159;
DWORD dwTimestamp = <span style="font-family: Arial, Helvetica, sans-serif;">0xed7feb88</span>;
BYTE byClientFlag= 90;
WORD size = 63371;
DWORD list[0] = 0x2a9a2f67b3;
}

6、明显可以看出时间戳错误,size也错误,结构体出问题了。就是请求的发送包出问题了。

7、有符号的情况下就比较简单了,直接显示结构体:

(gdb) p *(const Cmd::stRequestFollowedSingerInfo*)0x2a9a2f67aa
$1 = {<Cmd::stMainCharInfoUserCmd> = {<Cmd::stNullUserCmd> = {<Cmd::t_NullCmd> = {{<No data fields>}},
dwTimestamp = 3984583560}, <No data fields>}, byClientFlag = 90 'Z', size = 63371, list = 0x2a9a2f67b3}

8、补充

1) 查找相关结构体的方法:

info types 正则表达式

(gdb) info types stRequestFollowedSingerInfo
All types matching regular expression "stRequestFollowedSingerInfo":

File UserFollowTask.cpp:
Cmd::stRequestFollowedSingerInfo;
typedef Cmd::stRequestFollowedSingerInfo;


2) 显示结构体的方法

(gdb) ptype const Cmd::stRequestFollowedSingerInfo
type = const class Cmd::stRequestFollowedSingerInfo : public Cmd::stMainCharInfoUserCmd {
public:
BYTE byClientFlag;
WORD size;
DWORD list[0];

void stRequestFollowedSingerInfo(void);
DWORD getSize();
}
(gdb) ptype Cmd::stMainCharInfoUserCmd
type = class Cmd::stMainCharInfoUserCmd : public Cmd::stNullUserCmd {
public:
void stMainCharInfoUserCmd(void);
}
(gdb) ptype Cmd::stNullUserCmd
type = class Cmd::stNullUserCmd : public Cmd::t_NullCmd {
public:
DWORD dwTimestamp;

void stNullUserCmd(void);
}
(gdb) ptype Cmd::t_NullCmd
type = class Cmd::t_NullCmd {
public:
union {
<no data fields>
};

void t_NullCmd(BYTE, BYTE);
}
3) 显示结构体成员偏移的方法

(gdb) p &((const Cmd::stRequestFollowedSingerInfo*)0)->byClientFlag
$1 = (BYTE *) 0x6 <Address 0x6 out of bounds>
(gdb) p &((const Cmd::stRequestFollowedSingerInfo*)0)->size
$2 = (WORD *) 0x7
(gdb) p &((const Cmd::stRequestFollowedSingerInfo*)0)->list
$3 = (DWORD (*)[0]) 0x9
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: