简体   繁体   English

如何使用GCC将源代码行与汇编输出内联?

[英]How can I get the source lines inline with the assembly output using GCC?

I'd like to get the C source lines inline with the assembly output to see what code is being generated. 我想让C源代码行与汇编输出内联,以查看正在生成的代码。

I have tried GCC options like -S -Wa,-ahlms (and even -Wa,--gstabs 'cause I read it somewhere). 我尝试过像-S -Wa,-ahlms这样的GCC选项(甚至-Wa,--gstabs因为我在某处读过它)。

Oh! 哦! BTW, I am on a Mac so I don't have objdump . 顺便说一句,我在Mac上,所以我没有objdump

(Sorry this is short, I have to get off the train!) (对不起,这很简短,我得下车!)

Output of gcc pc-clisp.c -S -g -fverbose-asm -fnested-functions 输出gcc pc-clisp.c -S -g -fverbose-asm -fnested-functions

.globl _getBool
_getBool:
LFB32:
LM21:
    pushl   %ebp    #
LCFI30:
    movl    %esp, %ebp      #,
LCFI31:
    subl    $8, %esp        #,
LCFI32:
LM22:
    movzbl  8(%ebp), %eax   # atom.pred, D.4112
    movzbl  %al, %eax       # D.4112, D.4113
    andl    $-16, %eax      #, D.4114
    sarl    $4, %eax        #, D.4115
    testl   %eax, %eax      # D.4115
    setne   %al     #, tmp64
    movzbl  %al, %eax       # tmp64, D.4111
    leave
    ret
LFE32:

Maybe debug + a post-process step? 也许调试+后处理步骤?

gcc <source.c> -S -g -fverbose-asm

Find .file 1 "1.c" to tell you the file number to file name mapping. 找到.file 1 "1.c"告诉你文件名到文件名的映射。 Then add source after the .loc 1 8 0 lines. 然后在.loc 1 8 0行之后添加源。 I'm not sure how to do it with a single shell command, but a short script should be able to do it: 我不确定如何使用单个shell命令来执行此操作,但是短脚本应该能够执行此操作:

#!/usr/bin/env python

import re
import sys

filename = sys.argv[1]

f = open(filename)
lines = f.readlines()
f.close()

FILE_RE=re.compile(r"\s+\.file (\d+) \"(.*)\"")
LOC_RE =re.compile(r"\s+\.loc (\d+) (\d+)")

output = []
files = {}
for line in lines:
    output.append(line)
    mo = FILE_RE.match(line)
    if mo is not None:
       files[mo.group(1)] = open(mo.group(2)).readlines()
       print mo.group(1),"=",mo.group(2)
       continue
    mo = LOC_RE.match(line)
    if mo is not None:
       print mo.group(1),"=",mo.group(2)
       source = files[mo.group(1)][int(mo.group(2))-1]
       output.append("\t#"+source)

f = open(filename+".2","w")
f.writelines(output)
f.close()

这不完全是你所要求的,但你可能会发现-S -fverbose-asm很有帮助。

you can get objdump via macports. 你可以通过macports获得objdump。 just intall the "binutils" package and then use "gobjdump" 只需安装“binutils”包,然后使用“gobjdump”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM