2.5 GCC調試選項
GCC本身對包含了眾多的調試選項,既可以為用戶程序生成調試信息,也可以將GCC運行過程中的關鍵信息保存在文件或輸出在終端上,常用的調試選項如表2-2所示。如果需要了解GCC在處理的各個階段里中間表示的具體內容,或者需要了解GCC中某個處理過程對于中間表示的處理細節時,就可以使用表2-2中給出的各種GCC調試選項,輸出GCC運行過程中所生成的中間表示的調試信息和處理過程細節,并結合GCC的代碼,從而了解GCC的具體工作細節。
表2-2 GCC主要的調試選項

例2-1 GCC調試選項的使用
假設有如下的源代碼:
[GCC@localhost test]$ cat test.c int main(){ int i=0, sum=0; sum = sum + i; return sum; }
為了了解GCC對該文件編譯過程中的主要處理過程,可以使用如下命令輸出GCC處理過程的主要調試信息和工作流程。
[GCC@localhost test]$ ~/paag-gcc/host-i686-pc-linux-gnu/gcc/cc1 -fdump-tree-all -fdump- rtl-all test.c [GCC@localhost test]$ ls test.c* test.c test.c.123t.optimized test.c.168r.asmcons test.c.001t.tu test.c.125t.blocks test.c.171r.subregs_of_mode_init test.c.003t.original test.c.126t.final_cleanup test.c.172r.ira test.c.004t.gimple test.c.128r.expand test.c.173r.subregs_of_mode_finish test.c.006t.vcg test.c.129r.sibling test.c.176r.split2 test.c.007t.useless test.c.131r.initvals test.c.178r.pro_and_epilogue test.c.010t.lower test.c.132r.unshare test.c.192r.stack test.c.011t.ehopt test.c.133r.vregs test.c.193r.alignments test.c.012t.eh test.c.134r.into_cfglayout test.c.196r.mach test.c.013t.cfg test.c.135r.jump test.c.197r.barriers test.c.014t.cplxlower0 test.c.154r.reginfo test.c.200r.eh_ranges test.c.015t.veclower test.c.157r.outof_cfglayout test.c.201r.shorten test.c.021t.cleanup_cfg test.c.163r.split1 test.c.202r.dfinish test.c.023t.ssa test.c.165r.dfinit test.c.203t.statistics test.c.038t.release_ssa test.c.166r.mode_sw
可以看出,此時輸出的各種調試文件名稱格式為:test.c.nnn[r/t].name,其中nnn為一個編號,t表示該處理過程是基于tree的GIMPLE處理過程,r表示該處理過程是基于RTL的處理過程。如果讀者關注函數控制流圖(CFG, Control F low Graph)的信息,那么可以打開test.c.013t.cfg文件,查看其中的具體內容。內容如下:
[GCC@localhost test]$ cat test.c.013t.cfg ;; Function main (main) Merging blocks 2 and 3 main () { int sum; int i; int D.1234; <bb 2>: i = 0; sum = 0; sum = sum + i; D.1234 = sum; return D.1234; }
其中就包含了例子中給出函數的控制流圖,如果想了解更詳細的CFG信息,也可以使用如下的編譯形式:
[GCC@localhost test]$ ~/paag-gcc/host-i686-pc-linux-gnu/gcc/cc1 -fdump-tree-cfg- all test.c [GCC@localhost test]$ cat test.c.013t.cfg ;; Function main (main) Scope blocks: { Scope block #0 intD.0 iD.1232; (unused) intD.0 sumD.1233; (unused) } Pass statistics: ---------------- Merging blocks 2 and 3 main () { intD.0 sumD.1233; intD.0 iD.1232; intD.0 D.1234; # BLOCK 2 # PRED: ENTRY (fallthru) iD.1232 = 0; sumD.1233 = 0; sumD.1233 = sumD.1233 + iD.1232; D.1234 = sumD.1233; return D.1234; # SUCC: EXIT }
可以看出,GCC編譯時會生成更加詳細的CFG信息。
讀者也可以根據自己的需要,合理地使用表2-2中的調試選項,輸出GCC編譯過程中感興趣的調試信息,從而分析GCC的工作細節。