调试C++程序崩溃
- core文件生效
echo "* soft core unlimited" | sudo dd of=/etc/security/limits.d/core.conf #或 ulimit -c unlimited sudo sysctl -w kernel.core_pattern=/tmp/core-%e.%p.%h.%t #重启 上面这行无法永久修改 重启后总是被重写 #可编辑apport文件将enabled=1改为enabled=0 sudo vim /etc/default/apport #然后重启apport服务即生效 sudo systemctl restart apport
- 编辑崩溃程序(a.cpp)
#include <iostream> void test01() { int *p = nullptr; *p = 23; } int main() { test01(); return 0; }
- 编译奔溃程序(注意要加-g)
> g++ -o bin -g a.cpp
- 执行崩溃程序生成core文件
> ./bin > ls /tmp/ | grep core
core-bin.4079.ubuntu.1643104689
- 通过core文件查看崩溃信息
> gdb program_wgame core.14031
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-94.el7 Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-redhat-linux-gnu". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>... Reading symbols from /home/killer/program/bin/program_wgame...done. [New LWP 14031] [New LWP 14033] [New LWP 14040] [New LWP 14052] [New LWP 14037] [New LWP 14063] [New LWP 14039] [New LWP 14300] [New LWP 14038] [New LWP 14036] [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib64/libthread_db.so.1". warning: the debug information found in "/usr/lib/debug//lib64/libselinux.so.1.debug" does not match "/lib64/libselinux.so.1" (CRC mismatch). warning: the debug information found in "/usr/lib/debug/usr/lib64/libselinux.so.1.debug" does not match "/lib64/libselinux.so.1" (CRC mismatch). Missing separate debuginfo for /lib64/libselinux.so.1 Try: yum --enablerepo='*debug*' install /usr/lib/debug/.build-id/90/3a0bd0bfb4fee8c284f41beb9773ded94cbc52.debug Core was generated by `./program_wgame server.cfg log/'. Program terminated with signal 11, Segmentation fault. #0 wang::wbattle_mgr::gm_enter_test_scene (this=0x6b47260, scene_id=213) at /home/killer/Server/game/wbattle_mgr.cpp:552 552 monster_ids.Add(scene_info->monster_id[i]); (gdb) bt #0 wang::wbattle_mgr::gm_enter_test_scene (this=0x6b47260, scene_id=213) at /home/killer/Server/game/wbattle_mgr.cpp:552 #1 0x00000000008e5e44 in wang::wgm_cmd::_handle_enter_battle (this=<optimized out>, param=...) at /home/killer/Server/game/wgm_cmd.cpp:1766 #2 0x00000000008e1a5c in wang::wgm_cmd::parse_commands (this=0x14c0820 <wang::g_gm_cmd>, cmd=..., session_ptr=session_ptr@entry=0x36827c0) at /home/killer/Server/game/wgm_cmd.cpp:223 #3 0x0000000000919207 in wang::wclient_session::handler_gm_cmd (this=0x36827c0, msg_ptr=0x7fb530400a0c, msg_size=18) at /home/killer/Server/game/whandler_login.cpp:174 #4 0x00000000009644cd in wang::wlan_server::on_msg_receive (this=<optimized out>, session_id=<optimized out>, msg=...) at /home/killer/Server/game/wlan_server.cpp:434 #5 0x0000000000f88a11 in wang::wnet_mgr::process_msg() () #6 0x00000000009653e3 in wang::wlan_server::update (this=0x14c2200 <wang::g_lan_server>, uDeltaTime=uDeltaTime@entry=101) at /home/killer/Server/game/wlan_server.cpp:241 #7 0x00000000008d6aee in wang::wgame_server::Loop (this=0x14bff60 <wang::g_game_server>) at /home/killer/Server/game/wgame_server.cpp:167 #8 0x000000000083da00 in main (argc=3, argv=0x7fff417e72f8) at /home/killer/Server/game/main.cpp:34 (gdb)
- gdb断点调试
PS C:\WorkSpace\exercise\VsCode> gcc -g .\test.c .\func.c -o .\test.exe PS C:\WorkSpace\exercise\VsCode> gdb < ----------启动GDB GNU gdb (GDB) 7.6.1 Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "mingw32". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. (gdb) file test.exe < ------------------ 指定GDB检测这个可执行文件 Reading symbols from C:\WorkSpace\exercise\VsCode\test.exe...done. (gdb) set args Shaw < ----------设置主函数输入参数args (gdb) info break < ----------打印程序断点信息 No breakpoints or watchpoints. (gdb) break test.c:37 < ----------设置断点 Breakpoint 1 at 0x4014ba: file .\test.c, line 37. (gdb) info break Num Type Disp Enb Address What 1 breakpoint keep y 0x004014ba in main at .\test.c:37 (gdb) start < ------------启动代码 Temporary breakpoint 2 at 0x401442: file .\test.c, line 25. Starting program: C:\WorkSpace\exercise\VsCode/test.exe Shaw [New Thread 10792.0x300c] [New Thread 10792.0x3798] Temporary breakpoint 2, main (argc=2, argv=0x651560) at .\test.c:25 25 (gdb) continue < ------------- 继续执行代码 Continuing. main() : begin... argv[0] = C:\WorkSpace\exercise\VsCode/test.exe argv[1] = Shaw Breakpoint 1, main (argc=2, argv=0x651560) at .\test.c:37 37 } (gdb) next < ------------- 运行下一步 test_1() : 004013E0 38 (gdb) next 35 fa[i%3](); (gdb) next Breakpoint 1, main (argc=2, argv=0x651560) at .\test.c:37 37 } (gdb) next test_2() : 004013FC 38 (gdb) print i < -------------- 打印变量值 $1 = 1 (gdb) set var i=100 < ----------------- 设置变量值 (gdb) countinue Program received signal SIGSEGV, Segmentation fault. 0x00401539 in func () at .\func.c:7
本站文章采用知识共享署名 4.0 国际许可协议进行许可。