一背景 C程序发布的时候,经常去掉g编译选项的,那么这就遇到一个问题,当程序运行coredump后,想去调试查看core的具体信息,会发现很多符号都被优化掉了,看到的栈信息要么是问号,要么变量无法打印值;去掉符号表,却可以让程序体积更小,而且不容易泄漏程序的信息,更安全些。 这就产生了矛盾,我们在运行的时候不需要符号表,调试的时候需要符合表,那我们能否把符号表在发布程序的时候删除,调试的时候加载符号表信息那,这样就满足了需要。二测试程序2。1测试源码 为了直观起见,先写个简单的c代码用于演示,代码如下:1includestdio。h23intfunc()4{5inta5;6intba58;7intpiNULL;8pi0;9printf(Infuncais:dbis:d,a,b);10}1112intmain()13{14intafunc();15printf(d,a);16}2。2编译 编译下:miaoubuntulab:ctestgccotesttestdebug。c gdb调试看看:rootubuntulab:homemiaoctestgdb。testqReadingsymbolsfrom。test。。。(Nodebuggingsymbolsfoundin。test)(gdb)quit 可以看到显示没有调试符号表信息,我们重新用g编译选项试试:rootubuntulab:homemiaoctestgccotestdebugg。testdebug。crootubuntulab:homemiaoctestlstesttestdebugtestdebug。crootubuntulab:homemiaoctestgdb。testdebugqReadingsymbolsfrom。testdebug。。。(gdb) 其实也不是完全没有符号,也还是有不少的,只是没有调试信息,可以用命令查看:rootubuntulab:homemiaoctestreadelfh。testgrepstringtableindexSectionheaderstringtableindex:30rootubuntulab:homemiaoctestrootubuntulab:homemiaoctestreadelfh。testdebuggrepstringtableindexSectionheaderstringtableindex:36 两个符号表的大小是有差距的差距6个,这个表示符号表的index的个数。查下段表更清晰:rootubuntulab:homemiaoctestreadelfS。testdebuggrep。debug〔28〕。debugarangesPROGBITS000000000000000000003035〔29〕。debuginfoPROGBITS000000000000000000003065〔30〕。debugabbrevPROGBITS000000000000000000003159〔31〕。debuglinePROGBITS0000000000000000000031fd〔32〕。debugstrPROGBITS00000000000000000000326e〔33〕。debuglinestrPROGBITS000000000000000000003353rootubuntulab:homemiaoctestrootubuntulab:homemiaoctestrootubuntulab:homemiaoctestrootubuntulab:homemiaoctestreadelfS。testgrep。debugrootubuntulab:homemiaoctest2。3删除符号表 编译的时候可以采用g编译,然后发布的时候去掉符号表,可以使用命令:strip。如下最简单的处理下:rootubuntulab:homemiaoctestllal。testrwxrwxrx1miaomiao15984Apr3010:41。testrwxrxrx1rootroot17304Apr3010:48。testdebugrwrwr1miaomiao171Apr3010:41。testdebug。crootubuntulab:homemiaocteststrip。testdebugrootubuntulab:homemiaoctestllaltotal44drwxrwxrx2miaomiao4096Apr3011:19。drwxrx16miaomiao4096Apr3010:41。。rwxrwxrx1miaomiao15984Apr3010:41testrwxrxrx1rootroot14464Apr3011:19testdebugrwrwr1miaomiao171Apr3010:41testdebug。crootubuntulab:homemiaoctestnmstestdebugnm:testdebug:nosymbolsrootubuntulab:homemiaoctestnmstest000000000000038crabitag0000000000004010Bbssstart0000000000004010bcompleted。0wcxafinalizeGLIBC2。2。5。。。 可以看到strip处理过的testdebug,比不用g选项的编译出来的test文件更小,通过nm命令验证下,确实任何符号都被删除了,而不用g编译的文件还可以看到符号信息的。三符号表引入测试3。1core测试 默认情况下不会产生core文件,加大core文件尺寸:rootubuntulab:homemiaoctestulimitcunlimitedrootubuntulab:homemiaoctesttailf 重新编译运行:rootubuntulab:homemiaoctestgccgotestdebug。testdebug。crootubuntulab:homemiaocteststriptestdebugotestdebugstrip 看下core的信息:rootubuntulab:homemiaoctesttailfvarlogapport。logERROR:apport(pid125154)SatApr3011:52:202022:executable:homemiaoctesttestdebugstrip(commandline。testdebugstrip)ERROR:apport(pid125154)SatApr3011:52:202022:executabledoesnotbelongtoapackage,ignoringERROR:apport(pid125154)SatApr3011:52:202022:writingcoredumptocore。homemiaoctesttestdebugstrip。0。3fc6ea1694404d7281f87221d0b6684d。125153。3233349(limit:1) 调试下看看:rootubuntulab:homemiaoctestgdb。testdebugstripvarlibapportcoredumpcore。homemiaoctesttestdebugstrip。0。3fc6ea1694404d7281f87221d0b6684d。125153。3233349qReadingsymbolsfrom。testdebugstrip。。。(Nodebuggingsymbolsfoundin。testdebugstrip)〔NewLWP125153〕〔Threaddebuggingusinglibthreaddbenabled〕Usinghostlibthreaddblibrarylibx8664linuxgnulibthreaddb。so。1。Corewasgeneratedby。testdebugstrip。ProgramterminatedwithsignalSIGSEGV,Segmentationfault。00x000055d8d93bc178in??()(gdb)where00x000055d8d93bc178in??()10x000055d8d93bc1b3in??()20x00007f173a2ebfd0inlibcstartcallmain(mainmainentry0x55d8d93bc19d,argcargcentry1,argvargventry0x7ffe16913908)at。。sysdepsnptllibcstartcallmain。h:5830x00007f173a2ec07dinlibcstartmainimpl(main0x55d8d93bc19d,argc1,argv0x7ffe16913908,initoptimizedout,finioptimizedout,rtldfinioptimizedout,stackend0x7ffe169138f8)at。。csulibcstart。c:40940x000055d8d93bc085in??()(gdb)bt00x000055d8d93bc178in??()10x000055d8d93bc1b3in??()20x00007f173a2ebfd0inlibcstartcallmain(mainmainentry0x55d8d93bc19d,argcargcentry1,argvargventry0x7ffe16913908)at。。sysdepsnptllibcstartcallmain。h:5830x00007f173a2ec07dinlibcstartmainimpl(main0x55d8d93bc19d,argc1,argv0x7ffe16913908,initoptimizedout,finioptimizedout,rtldfinioptimizedout,stackend0x7ffe169138f8)at。。csulibcstart。c:40940x000055d8d93bc085in??() 因为没有符号信息,很可惜看不到具体的符号信息,也不知道在哪里core了。(gdb)symbolfilehomemiaoctesttestdebugLoadnewsymboltablefromhomemiaoctesttestdebug?(yorn)yReadingsymbolsfromhomemiaoctesttestdebug。。。(gdb)where00x000055d8d93bc178infunc()at。testdebug。c:810x000055d8d93bc1b3inmain()at。testdebug。c:14(gdb) 看重点,加载符号文件,这个是直接加载没有strip前的文件,是包含符号表的。我们清晰的可以看到core的位置是在第8行。 顺便说下,gdb调试多线程,打印多线程堆栈信息命令:threadapplyallbtfull3。2提取符号表rootubuntulab:homemiaoctesteustriptestdebugftestdebug。symrootubuntulab:homemiaoctestlltotal100drwxrwxrx2miaomiao4096Apr3012:23。drwxrx16miaomiao4096Apr3012:09。。rwxrxrx1rootroot15984Apr3011:23testrwxrxrx1rootroot14464Apr3011:24testdebugrwxrxrx1rootroot14568Apr3012:23testdebugrwrwr1miaomiao205Apr3011:38testdebug。crwxrxrx1rootroot14464Apr3011:52testdebugstriprwxrxrx1rootroot5864Apr3012:23testdebug。symrootubuntulab:homemiaoctestfiletest。symtest。sym:cannotopentest。sym(Nosuchfileordirectory)rootubuntulab:homemiaoctestfile。testdebug。sym。testdebug。sym:ELF64bitLSBsharedobject,x8664,version1(SYSV),dynamicallylinked,interpreter04,BuildID〔sha1〕5881c326d8f784139407ed0c7576149cb19270f4,forGNULinux3。2。0,withdebuginfo,notstripped 我们通过命令:eustriptestdebugftestdebug。sym提取testdebug中的符号表,保存为文件testdebug。sym。 我们gdb调试的时候导入这个符号试试:rootubuntulab:homemiaoctestgdb。testdebugstripvarlibapportcoredumpcore。homemiaoctesttestdebugstrip。0。3fc6ea1694404d7281f87221d0b6684d。125153。3233349qReadingsymbolsfrom。testdebugstrip。。。(Nodebuggingsymbolsfoundin。testdebugstrip)〔NewLWP125153〕〔Threaddebuggingusinglibthreaddbenabled〕Usinghostlibthreaddblibrarylibx8664linuxgnulibthreaddb。so。1。Corewasgeneratedby。testdebugstrip。ProgramterminatedwithsignalSIGSEGV,Segmentationfault。00x000055d8d93bc178in??()(gdb)symbolfilehomemiaoctesttestdebug。symLoadnewsymboltablefromhomemiaoctesttestdebug。sym?(yorn)yReadingsymbolsfromhomemiaoctesttestdebug。sym。。。(gdb)where00x000055d8d93bc178infunc()attestdebug。c:810x000055d8d93bc1b3inmain()attestdebug。c:14(gdb)四参考网站〔https:blog。csdn。netbluebubblearticledetails10407217〕(https:blog。csdn。netbluebubblearticledetails10407217)