key.proto == UDP_PROTO) { 15 if (size >-6ren">
gpt4 book ai didi

c++ - "gdb"调试器奇怪地跳过断点

转载 作者:太空宇宙 更新时间:2023-11-04 02:32:00 24 4
gpt4 key购买 nike

这里有一小段我的代码:

 //l2tp_inspector.cc
14 else if (f_info->key.proto == UDP_PROTO) {
15 if (size >= 4) {
16 uint32_t l2tp_part;
17 l2tp_part = *((uint32_t*)(data));
18
19 if ((l2tp_part & 0xFFFF0000) == 0xC802 &&
20 (l2tp_part & 0x0000FFFF) == size) {
21 f_info->protocol_id = Bina::Protocols::UDP_L2TP;
22 f_info->application_id = Bina::Applications::UNKNOWN;
23 return PROTOCOL_ACCEPTED_FINALIZED;
24 }
25 }
26 return REJECTED;
27 }

在编译代码并使用 gdb 调试器并在第 17、18 和 19 行设置断点后,我们得到以下输出:

test@ubuntu:~/prj$ gdb prj-loader 

GNU gdb (Ubuntu 7.11.90.20161005-0ubuntu1) 7.11.90.20161005-git
This GDB was configured as "x86_64-linux-gnu".

(gdb) b l2tp_inspector.cc:17
No source file named l2tp_inspector.cc.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (l2tp_inspector.cc:17) pending.

(gdb) b l2tp_inspector.cc:18
No source file named l2tp_inspector.cc.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 2 (l2tp_inspector.cc:18) pending.

(gdb) b l2tp_inspector.cc:19
No source file named l2tp_inspector.cc.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 3 (l2tp_inspector.cc:19) pending.

(gdb) r
Starting program: ~/prj/prj-loader
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[2017-02-02 08:44:52.267622] [0x00007ffff7fc9780] [info] New id registered TCP with id 0
[2017-02-02 08:44:52.267899] [0x00007ffff7fc9780] [info] New id registered TCP_PAYLOAD with id 1
[2017-02-02 08:44:52.267991] [0x00007ffff7fc9780] [info] New id registered UDP with id 2
[2017-02-02 08:44:52.268025] [0x00007ffff7fc9780] [info] New id registered UDP_PAYLOAD with id 3
[2017-02-02 08:44:52.268169] [0x00007ffff7fc9780] [info] Already registered id TCP with id 0
[2017-02-02 08:44:52.268201] [0x00007ffff7fc9780] [info] Already registered id TCP_PAYLOAD with id 1
[2017-02-02 08:44:52.268285] [0x00007ffff7fc9780] [info] Already registered id UDP with id 2
[2017-02-02 08:44:52.268380] [0x00007ffff7fc9780] [info] Already registered id UDP_PAYLOAD with id 3
[2017-02-02 08:44:52.268519] [0x00007ffff7fc9780] [info] New id registered IPV4 with id 4
[2017-02-02 08:44:52.268553] [0x00007ffff7fc9780] [info] New id registered IPV4FRAG with id 5
[2017-02-02 08:44:52.268632] [0x00007ffff7fc9780] [info] Already registered id TCP with id 0
[2017-02-02 08:44:52.268715] [0x00007ffff7fc9780] [info] Already registered id TCP_PAYLOAD with id 1
[2017-02-02 08:44:52.268795] [0x00007ffff7fc9780] [info] Already registered id UDP with id 2
[2017-02-02 08:44:52.268877] [0x00007ffff7fc9780] [info] Already registered id UDP_PAYLOAD with id 3
[2017-02-02 08:44:52.268972] [0x00007ffff7fc9780] [info] Already registered id TCP with id 0
[2017-02-02 08:44:52.269066] [0x00007ffff7fc9780] [info] Already registered id TCP_PAYLOAD with id 1
[2017-02-02 08:44:52.269149] [0x00007ffff7fc9780] [info] Already registered id UDP with id 2
[2017-02-02 08:44:52.269229] [0x00007ffff7fc9780] [info] Already registered id UDP_PAYLOAD with id 3
[2017-02-02 08:44:52.269370] [0x00007ffff7fc9780] [info] Already registered id TCP with id 0
[2017-02-02 08:44:52.269401] [0x00007ffff7fc9780] [info] Already registered id TCP_PAYLOAD with id 1
[2017-02-02 08:44:52.269481] [0x00007ffff7fc9780] [info] Already registered id UDP with id 2
[2017-02-02 08:44:52.269561] [0x00007ffff7fc9780] [info] Already registered id UDP_PAYLOAD with id 3
[2017-02-02 08:44:52.269643] [0x00007ffff7fc9780] [info] Already registered id IPV4 with id 4
[2017-02-02 08:44:52.269723] [0x00007ffff7fc9780] [info] Already registered id IPV4FRAG with id 5
[2017-02-02 08:44:52.269807] [0x00007ffff7fc9780] [info] New id registered ETHERNET with id 6

Breakpoint 1, inspect (f_info=0x7fffffffce50, data=0x555555be439a "\310\002", size=106, s_module=@0x7fffffffcde0: 0x0)
at ../src/modules/prj-lib/src/proto-inspectors/l2tp_inspector.cc:17
17 l2tp_part = *((uint32_t*)(data));

(gdb) s
Breakpoint 2, inspect (f_info=0x7fffffffce50, data=0x555555be439a "\310\002", size=106, s_module=@0x7fffffffcde0: 0x0)
at ../src/modules/prj-lib/src/proto-inspectors/l2tp_inspector.cc:26
26 return REJECTED;

正如您在上面看到的,当我在第 19 行打断时,gdb 不会在那行停止并跳到第 26 行,为什么?

当我在第 19 行用 0xC8020000 替换 0xC802 时,它工作正常 gdb 不再错过第 19 行:

 //l2tp_inspector.cc
14 else if (f_info->key.proto == UDP_PROTO) {
15 if (size >= 4) {
16 uint32_t l2tp_part;
17 l2tp_part = *((uint32_t*)(data));
18
19 if ((l2tp_part & 0xFFFF0000) == 0xC8020000 &&
20 (l2tp_part & 0x0000FFFF) == size) {
21 f_info->protocol_id = Bina::Protocols::UDP_L2TP;
22 f_info->application_id = Bina::Applications::UNKNOWN;
23 return PROTOCOL_ACCEPTED_FINALIZED;
24 }
25 }
26 return REJECTED;
27 }

国内生产总值产出:

test@ubuntu:~/prj$ gdb prj-loader 
GNU gdb (Ubuntu 7.11.90.20161005-0ubuntu1) 7.11.90.20161005-git
This GDB was configured as "x86_64-linux-gnu".

(gdb) b l2tp_inspector.cc:17
No source file named l2tp_inspector.cc.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (l2tp_inspector.cc:17) pending.

(gdb) b l2tp_inspector.cc:18
No source file named l2tp_inspector.cc.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 2 (l2tp_inspector.cc:18) pending.

(gdb) b l2tp_inspector.cc:19
No source file named l2tp_inspector.cc.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 3 (l2tp_inspector.cc:19) pending.

(gdb) r
Starting program: ~/prj/prj-loader
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[2017-02-02 08:53:07.201600] [0x00007ffff7fc9780] [info] New id registered TCP with id 0
[2017-02-02 08:53:07.201677] [0x00007ffff7fc9780] [info] New id registered TCP_PAYLOAD with id 1
[2017-02-02 08:53:07.201711] [0x00007ffff7fc9780] [info] New id registered UDP with id 2
[2017-02-02 08:53:07.201728] [0x00007ffff7fc9780] [info] New id registered UDP_PAYLOAD with id 3
[2017-02-02 08:53:07.201757] [0x00007ffff7fc9780] [info] Already registered id TCP with id 0
[2017-02-02 08:53:07.201773] [0x00007ffff7fc9780] [info] Already registered id TCP_PAYLOAD with id 1
[2017-02-02 08:53:07.201817] [0x00007ffff7fc9780] [info] Already registered id UDP with id 2
[2017-02-02 08:53:07.201832] [0x00007ffff7fc9780] [info] Already registered id UDP_PAYLOAD with id 3
[2017-02-02 08:53:07.201853] [0x00007ffff7fc9780] [info] New id registered IPV4 with id 4
[2017-02-02 08:53:07.201871] [0x00007ffff7fc9780] [info] New id registered IPV4FRAG with id 5
[2017-02-02 08:53:07.201888] [0x00007ffff7fc9780] [info] Already registered id TCP with id 0
[2017-02-02 08:53:07.201903] [0x00007ffff7fc9780] [info] Already registered id TCP_PAYLOAD with id 1
[2017-02-02 08:53:07.201933] [0x00007ffff7fc9780] [info] Already registered id UDP with id 2
[2017-02-02 08:53:07.201951] [0x00007ffff7fc9780] [info] Already registered id UDP_PAYLOAD with id 3
[2017-02-02 08:53:07.201968] [0x00007ffff7fc9780] [info] Already registered id TCP with id 0
[2017-02-02 08:53:07.201983] [0x00007ffff7fc9780] [info] Already registered id TCP_PAYLOAD with id 1
[2017-02-02 08:53:07.201998] [0x00007ffff7fc9780] [info] Already registered id UDP with id 2
[2017-02-02 08:53:07.202023] [0x00007ffff7fc9780] [info] Already registered id UDP_PAYLOAD with id 3
[2017-02-02 08:53:07.202048] [0x00007ffff7fc9780] [info] Already registered id TCP with id 0
[2017-02-02 08:53:07.202067] [0x00007ffff7fc9780] [info] Already registered id TCP_PAYLOAD with id 1
[2017-02-02 08:53:07.202083] [0x00007ffff7fc9780] [info] Already registered id UDP with id 2
[2017-02-02 08:53:07.202099] [0x00007ffff7fc9780] [info] Already registered id UDP_PAYLOAD with id 3
[2017-02-02 08:53:07.202114] [0x00007ffff7fc9780] [info] Already registered id IPV4 with id 4
[2017-02-02 08:53:07.202133] [0x00007ffff7fc9780] [info] Already registered id IPV4FRAG with id 5
[2017-02-02 08:53:07.202150] [0x00007ffff7fc9780] [info] New id registered ETHERNET with id 6

Breakpoint 1, inspect (f_info=0x7fffffffce50, data=0x555555be439a "\310\002", size=106, s_module=@0x7fffffffcde0: 0x0)
at ../src/modules/prj-lib/src/proto-inspectors/l2tp_inspector.cc:17
17 l2tp_part = *((uint32_t*)(data));

(gdb) s
Breakpoint 2, inspect (f_info=0x7fffffffce50, data=0x555555be439a "\310\002", size=106, s_module=@0x7fffffffcde0: 0x0)
at ../src/modules/prj-lib/src/proto-inspectors/l2tp_inspector.cc:19
19 if ((l2tp_part & 0xFFFF0000) == 0xC8020000 &&

(gdb) s
26 return REJECTED;

为什么 dbg 在第一个代码段中跳过了第 19 行,但在第二个代码段中停在了第 19 行?几乎所有东西都是一样的,唯一的区别就是值(value)。

最佳答案

我想我明白了。

简答:

编译器优化完全删除了 if 条件及其 block !

长答案:

(l2tp_part & 0xFFFF0000) == 0xC802if 条件中的结果总是 false!因为我们正在比较一个 4 字节数字,其中 2 个最低有效字节等于 0x0000,与一个 2 字节非零数字。换句话说,我们将 0xXXXX0000 与 0x0000C802 进行比较,显然它们在任何情况下都不相等。所以整个条件总是假的,因为 if block 是多余的,编译器将其删除。所以调试器不能在那一行停止。

关于c++ - "gdb"调试器奇怪地跳过断点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41995063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com