程序core了,看到的还是问号
一 背景
C程序发布的时候,经常去掉-g编译选项的,那么这就遇到一个问题,当程序运行core dump后,想去调试查看core的具体信息,会发现很多符号都被优化掉了,看到的栈信息要么是问号,要么变量无法打印值; 去掉符号表,却可以让程序体积更小,而且不容易泄漏程序的信息,更安全些。
这就产生了矛盾,我们在运行的时候不需要符号表,调试的时候需要符合表,那我们能否把符号表在发布程序的时候删除,调试的时候加载符号表信息那,这样就满足了需要。 二 测试程序2.1 测试源码
为了直观起见,先写个简单的c代码用于演示,代码如下: 1 #include 2 3 int func() 4 { 5 int a = 5; 6 int b = a*5+8; 7 int * pi = NULL; 8 *pi = 0; 9 printf("In func a is:%d b is:%d",a,b); 10 } 11 12 int main() 13 { 14 int a = func(); 15 printf("%d ", a); 16 } 2.2 编译
编译下: miao@ubuntu-lab:~/c-test$ gcc -o test testdebug.c
gdb调试看看: root@ubuntu-lab:/home/miao/c-test# gdb ./test -q Reading symbols from ./test... (No debugging symbols found in ./test) (gdb) quit
可以看到显示没有调试符号表信息,我们重新用-g编译选项试试: root@ubuntu-lab:/home/miao/c-test# gcc -o test-debug -g ./testdebug.c root@ubuntu-lab:/home/miao/c-test# ls test test-debug testdebug.c root@ubuntu-lab:/home/miao/c-test# gdb ./test-debug -q Reading symbols from ./test-debug... (gdb)
其实也不是完全没有符号,也还是有不少的,只是没有调试信息,可以用命令查看: root@ubuntu-lab:/home/miao/c-test# readelf -h ./test|grep "string table index" Section header string table index: 30 root@ubuntu-lab:/home/miao/c-test# root@ubuntu-lab:/home/miao/c-test# readelf -h ./test-debug|grep "string table index" Section header string table index: 36
两个符号表的大小是有差距的差距6个,这个表示符号表的index的个数。 查下段表更清晰: root@ubuntu-lab:/home/miao/c-test# readelf -S ./test-debug|grep ".debug" [28] .debug_aranges PROGBITS 0000000000000000 00003035 [29] .debug_info PROGBITS 0000000000000000 00003065 [30] .debug_abbrev PROGBITS 0000000000000000 00003159 [31] .debug_line PROGBITS 0000000000000000 000031fd [32] .debug_str PROGBITS 0000000000000000 0000326e [33] .debug_line_str PROGBITS 0000000000000000 00003353 root@ubuntu-lab:/home/miao/c-test# root@ubuntu-lab:/home/miao/c-test# root@ubuntu-lab:/home/miao/c-test# root@ubuntu-lab:/home/miao/c-test# readelf -S ./test|grep ".debug" root@ubuntu-lab:/home/miao/c-test# 2.3 删除符号表
编译的时候可以采用-g编译,然后发布的时候去掉符号表,可以使用命令:strip。 如下最简单的处理下: root@ubuntu-lab:/home/miao/c-test# ll -al ./test* -rwxrwxr-x 1 miao miao 15984 Apr 30 10:41 ./test* -rwxr-xr-x 1 root root 17304 Apr 30 10:48 ./test-debug* -rw-rw-r-- 1 miao miao 171 Apr 30 10:41 ./testdebug.c root@ubuntu-lab:/home/miao/c-test# strip ./test-debug root@ubuntu-lab:/home/miao/c-test# ll -al total 44 drwxrwxr-x 2 miao miao 4096 Apr 30 11:19 ./ drwxr-x--- 16 miao miao 4096 Apr 30 10:41 ../ -rwxrwxr-x 1 miao miao 15984 Apr 30 10:41 test* -rwxr-xr-x 1 root root 14464 Apr 30 11:19 test-debug* -rw-rw-r-- 1 miao miao 171 Apr 30 10:41 testdebug.c root@ubuntu-lab:/home/miao/c-test# nm -s test-debug nm: test-debug: no symbols root@ubuntu-lab:/home/miao/c-test# nm -s test 000000000000038c r __abi_tag 0000000000004010 B __bss_start 0000000000004010 b completed.0 w __cxa_finalize@GLIBC_2.2.5 ...
可以看到strip处理过的testdebug,比不用-g 选项的编译出来的test文件更小,通过nm命令验证下,确实任何符号都被删除了,而不用-g编译的文件还可以看到符号信息的。 三 符号表引入测试3.1 core测试
默认情况下不会产生core文件,加大core文件尺寸: root@ubuntu-lab:/home/miao/c-test# ulimit -c unlimited root@ubuntu-lab:/home/miao/c-test# tail -f
重新编译运行: root@ubuntu-lab:/home/miao/c-test# gcc -g -o testdebug ./testdebug.c root@ubuntu-lab:/home/miao/c-test# strip testdebug -o testdebug-strip
看下core的信息: root@ubuntu-lab:/home/miao/c-test# tail -f /var/log/apport.log ERROR: apport (pid 125154) Sat Apr 30 11:52:20 2022: executable: /home/miao/c-test/testdebug-strip (command line "./testdebug-strip") ERROR: apport (pid 125154) Sat Apr 30 11:52:20 2022: executable does not belong to a package, ignoring ERROR: apport (pid 125154) Sat Apr 30 11:52:20 2022: writing core dump to core._home_miao_c-test_testdebug-strip.0.3fc6ea16-9440-4d72-81f8-7221d0b6684d.125153.3233349 (limit: -1)
调试下看看: root@ubuntu-lab:/home/miao/c-test# gdb ./testdebug-strip /var/lib/apport/coredump/core._home_miao_c-test_testdebug-strip.0.3fc6ea16-9440-4d72-81f8-7221d0b6684d.125153.3233349 -q Reading symbols from ./testdebug-strip... (No debugging symbols found in ./testdebug-strip) [New LWP 125153] [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". Core was generated by `./testdebug-strip". Program terminated with signal SIGSEGV, Segmentation fault. #0 0x000055d8d93bc178 in ?? () (gdb) where #0 0x000055d8d93bc178 in ?? () #1 0x000055d8d93bc1b3 in ?? () #2 0x00007f173a2ebfd0 in __libc_start_call_main (main=main@entry=0x55d8d93bc19d, argc=argc@entry=1, argv=argv@entry=0x7ffe16913908) at ../sysdeps/nptl/libc_start_call_main.h:58 #3 0x00007f173a2ec07d in __libc_start_main_impl (main=0x55d8d93bc19d, argc=1, argv=0x7ffe16913908, init=, fini=, rtld_fini=, stack_end=0x7ffe169138f8) at ../csu/libc-start.c:409 #4 0x000055d8d93bc085 in ?? () (gdb) bt #0 0x000055d8d93bc178 in ?? () #1 0x000055d8d93bc1b3 in ?? () #2 0x00007f173a2ebfd0 in __libc_start_call_main (main=main@entry=0x55d8d93bc19d, argc=argc@entry=1, argv=argv@entry=0x7ffe16913908) at ../sysdeps/nptl/libc_start_call_main.h:58 #3 0x00007f173a2ec07d in __libc_start_main_impl (main=0x55d8d93bc19d, argc=1, argv=0x7ffe16913908, init=, fini=, rtld_fini=, stack_end=0x7ffe169138f8) at ../csu/libc-start.c:409 #4 0x000055d8d93bc085 in ?? ()
因为没有符号信息,很可惜看不到具体的符号信息,也不知道在哪里core了。 (gdb) symbol-file /home/miao/c-test/testdebug Load new symbol table from "/home/miao/c-test/testdebug"? (y or n) y Reading symbols from /home/miao/c-test/testdebug... (gdb) where #0 0x000055d8d93bc178 in func () at ./testdebug.c:8 #1 0x000055d8d93bc1b3 in main () at ./testdebug.c:14 (gdb)
看重点,加载符号文件,这个是直接加载没有strip前的文件,是包含符号表的。我们清晰的可以看到core的位置是在第8行。
顺便说下,gdb调试多线程,打印多线程堆栈信息命令:thread apply all bt full 3.2 提取符号表root@ubuntu-lab:/home/miao/c-test# eu-strip testdebug -f testdebug.sym root@ubuntu-lab:/home/miao/c-test# ll total 100 drwxrwxr-x 2 miao miao 4096 Apr 30 12:23 ./ drwxr-x--- 16 miao miao 4096 Apr 30 12:09 ../ -rwxr-xr-x 1 root root 15984 Apr 30 11:23 test* -rwxr-xr-x 1 root root 14464 Apr 30 11:24 test-debug* -rwxr-xr-x 1 root root 14568 Apr 30 12:23 testdebug* -rw-rw-r-- 1 miao miao 205 Apr 30 11:38 testdebug.c -rwxr-xr-x 1 root root 14464 Apr 30 11:52 testdebug-strip* -rwxr-xr-x 1 root root 5864 Apr 30 12:23 testdebug.sym* root@ubuntu-lab:/home/miao/c-test# file test.sym test.sym: cannot open `test.sym" (No such file or directory) root@ubuntu-lab:/home/miao/c-test# file ./testdebug.sym ./testdebug.sym: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter 04, BuildID[sha1]=5881c326d8f784139407ed0c7576149cb19270f4, for GNU/Linux 3.2.0, with debug_info, not stripped
我们通过命令: eu-strip testdebug -f testdebug.sym 提取testdebug中的符号表,保存为文件testdebug.sym。
我们gdb调试的时候导入这个符号试试: root@ubuntu-lab:/home/miao/c-test# gdb ./testdebug-strip /var/lib/apport/coredump/core._home_miao_c-test_testdebug-strip.0.3fc6ea16-9440-4d72-81f8-7221d0b6684d.125153.3233349 -q Reading symbols from ./testdebug-strip... (No debugging symbols found in ./testdebug-strip) [New LWP 125153] [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". Core was generated by `./testdebug-strip". Program terminated with signal SIGSEGV, Segmentation fault. #0 0x000055d8d93bc178 in ?? () (gdb) symbol-file /home/miao/c-test/testdebug.sym Load new symbol table from "/home/miao/c-test/testdebug.sym"? (y or n) y Reading symbols from /home/miao/c-test/testdebug.sym... (gdb) where #0 0x000055d8d93bc178 in func () at testdebug.c:8 #1 0x000055d8d93bc1b3 in main () at testdebug.c:14 (gdb) 四 参考网站[https://blog.csdn.net/bluebubble/article/details/10407217](https://blog.csdn.net/bluebubble/article/details/10407217)
腾讯陆续启用中文顶级域名QQ。中国和腾讯。中国新网域名资讯随着政策导向的日渐鲜明和移动互联网的不断发展,近年来,中文域名的技术和应用环境逐步提高。据中国互联网络信息中心(CNNIC)资讯腾讯公司于今年陆续开通解析了纯中文顶级域
PR插件项目媒体修剪存档整理脚本PlumePackPR项目媒体修剪存档整理脚本PlumePack插件支持ProRes。BRAW。R3D图像序列以及压缩的H。264和H。265(HEVC),根据您的编辑删除未使用的帧来修剪文件,并带
宝马3系强压雅阁赢得5月中级车销量冠军,中级车在走下坡?5月份,根据乘联会的最新数据显示,轿车销量为79。4万辆。同比增长3。3,环比增长3。4。相比于SUV,轿车市场无疑还是正向稳步提升。即便在新能源汽车销量快速攀升的当下,仍然有把蛋
中国不需要道歉,该道歉的是无良媒体的种族主义!阿丘你今天道歉了吗首先,疫情首先出现在中国,但不一定起源于中国,想想猪流感是怎么来的吧,墨西哥道歉了吗?第二,只要是疾病,就会与全人类有关,这不是哪一个国家的问题,09年美国的H1
媒体标记即将推出市场MediaMarkt即将在欧洲各地推出其新的市场功能。该平台已在德国奥地利和西班牙的Beta阶段推出。名单上的下一个是荷兰。之后,市场将进一步扩展到比荷卢。德国主要的消费电子产品零
新网X青云战略合作,共谋未来7月29日,由青云科技主办的预见数字自由CIC2021云计算峰会在北京成功举办,峰会旨在全面展示交流分享云计算前沿技术最新应用和实践成果。新网受邀参加峰会,并在会上与青云科技正式达
总裁班教务老师,未来的前途出路提升在哪里?资本策划师(CCP),人生腾飞的最佳选择朱老师,我是您去授课的北大金融总裁班的项目负责人小李老师,听说你们开了一个关于资本策划师(CCP)的认证班,我们几个也想报名去读参加认证,具
河南暴雨后浸泡的汽车怎么办?保险的估损又是多少最后何去何从?2021年的7月20日八时,河南中北部出现有史以来的特大暴雨,最严重的郑州城区的下雨量高达500657毫米,国家防疫总局启动防汛级应急响应,针对暴雨引发的险情,应急部第一时间派出多
三角梅的扦插技巧!如何用白糖促根?三角梅的扦插繁殖中,无论是老枝还是嫩枝,都可以利用生根剂处理达到枝条快速生根的效果。家庭养花繁殖中,在没有专用生根剂的情况下,可以利用什么来进行促根呢?这种促根剂就是我们家庭中常用
一切成功都是酝酿了太久太久富爸爸,穷爸爸原创读书笔记有时创业也不能一味追求稳,太稳代表着一定程度的保守。有些时候特别害怕失败,不管是有没有过失败,发生失败是因为自己太害怕失败。有人勇于面对失败,因为失败会增加下一次成功的机会,有人遇
生活日记水墨气息浓郁的机械键盘,谁说数码产品没有文艺范?引言说到机械键盘,我用过三款不同品牌的机械键盘,轴体有青轴和茶轴。其他的红轴,黑轴等各类轴体,我没有长时间使用过,但是就青轴和茶轴而言,如果你没有使用过机械键盘,我推荐你人生第一把