14

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:
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
philcolbourn
  • 4,042
  • 3
  • 28
  • 33
  • It looks like what I want to do can't be easily be done on a Mac. 1) there is no `objdump` and `otools` doesn't do the job either. 2) `gdb` in newer versions has `disas /m ` - the Mac has an older `gdb`. 3) I suspect that the assembler output of `GCC` is not very helpful on the Mac. 4) Why should I be forced to use `XCode`? (Don't answer that). – philcolbourn Mar 10 '10 at 10:26
  • Shortly after this question I installed linux and gave up on OS-X, but Douglas seems to be closer to a solution. – philcolbourn Oct 14 '12 at 11:35
  • If you are looking at small portions of code godbolt may be a simpler solution as I [explain in this answer](http://stackoverflow.com/a/25781924/1708801). – Shafik Yaghmour Sep 19 '14 at 12:50
  • @Shafik Yaghmour: Interesting. Thanks. – philcolbourn Oct 03 '14 at 00:21

3 Answers3

13

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()
Douglas Leeder
  • 52,368
  • 9
  • 94
  • 137
  • Thanks. This looks interesting. However The assembler file does not contain .file nor .loc statements. I'll post the assembler in the question. – philcolbourn Mar 10 '10 at 10:15
  • 1
    You can get the ".loc" lines by adding "-g" to the gcc command line. +1 to Douglas for a useful script: exactly what I was looking for. No need to reinvent the wheel. – snap Sep 01 '11 at 08:22
5

It's not exactly what you're asking for, but you may find -S -fverbose-asm helpful.

Stephen Canon
  • 103,815
  • 19
  • 183
  • 269
3

you can get objdump via macports. just intall the "binutils" package and then use "gobjdump"

didito
  • 748
  • 7
  • 17
  • Thanks. I have tried macports and fink. Both are good, but they were both painful to maintain. I gave up on macports and favoured fink. Probably easier to run linux in a VM. – philcolbourn Oct 14 '12 at 11:38
  • homebrew: https://superuser.com/questions/206547/how-can-i-install-objdump-on-mac-os-x – rogerdpack Dec 02 '22 at 05:28