简体   繁体   中英

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.

I have tried GCC options like -S -Wa,-ahlms (and even -Wa,--gstabs 'cause I read it somewhere).

Oh! BTW, I am on a Mac so I don't have objdump .

(Sorry this is short, I have to get off the train!)

Output of 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. Then add source after the .loc 1 8 0 lines. I'm not sure how to do it with a single shell command, but a short script should be able to do it:

#!/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. just intall the "binutils" package and then use "gobjdump"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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