Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/objdump: print Go code alongside assembly #18245

Closed
mkevac opened this issue Dec 8, 2016 · 6 comments
Closed

cmd/objdump: print Go code alongside assembly #18245

mkevac opened this issue Dec 8, 2016 · 6 comments
Labels
FrozenDueToAge help wanted Proposal-Accepted Suggested Issues that may be good for new contributors looking for work to do.
Milestone

Comments

@mkevac
Copy link
Contributor

mkevac commented Dec 8, 2016

I would like a way to get disassembly with corresponding Go code. Similar to what objdump -S does, but with Go assembly.

$ objdump -S testif.test
[...]
	if m == hash[h] {
  40c41c:	89 da                	mov    %ebx,%edx
  40c41e:	48 81 fa f1 03 00 00 	cmp    $0x3f1,%rdx
  40c425:	0f 83 55 01 00 00    	jae    40c580 <runtime.additab+0x3f0>
  40c42b:	48 8d 05 ae 80 1e 00 	lea    0x1e80ae(%rip),%rax        # 5f44e0 <runtime.hash>
  40c432:	48 8b 1c d0          	mov    (%rax,%rdx,8),%rbx
  40c436:	48 8d 04 d0          	lea    (%rax,%rdx,8),%rax
  40c43a:	48 89 84 24 b8 00 00 	mov    %rax,0xb8(%rsp)
  40c441:	00 
  40c442:	48 39 d9             	cmp    %rbx,%rcx
  40c445:	74 50                	je     40c497 <runtime.additab+0x307>
		println("duplicate itab for", typ.string(), "and", inter.typ.string())
		throw("duplicate itabs")
	}
	m.link = hash[h]
  40c447:	8b 15 13 14 20 00    	mov    0x201413(%rip),%edx        # 60d860 <runtime.writeBarrier>
  40c44d:	48 8d 71 10          	lea    0x10(%rcx),%rsi
  40c451:	85 d2                	test   %edx,%edx
  40c453:	75 22                	jne    40c477 <runtime.additab+0x2e7>
  40c455:	48 89 59 10          	mov    %rbx,0x10(%rcx)
	atomicstorep(unsafe.Pointer(&hash[h]), unsafe.Pointer(m))
  40c459:	48 89 04 24          	mov    %rax,(%rsp)
  40c45d:	48 89 4c 24 08       	mov    %rcx,0x8(%rsp)
  40c462:	e8 29 5b ff ff       	callq  401f90 <runtime.atomicstorep>
[...]

I propose that we add -S to go tool objdump.

The only way I know to get similar thing is to use weblist command in go tool pprof. But you need profile data for that.

$ go version
go version devel +d4b46aa Thu Dec 8 01:36:44 2016 +0000 linux/amd64
@bradfitz bradfitz added this to the Proposal milestone Dec 8, 2016
@bradfitz bradfitz changed the title Print Go assembly with corresponding Go code in between proposal: cmd/objdump: print Go code alongside assembly Dec 8, 2016
@rsc rsc modified the milestones: Unplanned, Proposal Dec 12, 2016
@rsc
Copy link
Contributor

rsc commented Dec 12, 2016

This seems like a reasonable addition. If you (or anyone else) would like to send a CL, please do.

@bradfitz bradfitz added help wanted Suggested Issues that may be good for new contributors looking for work to do. labels Dec 12, 2016
@ALTree
Copy link
Member

ALTree commented Dec 15, 2016

I've poked around in cmd/internal/objfile (which objdump uses to disassemble) and it looks like it shouldn't be too hard to add some support for -S in objdump. May look into this for 1.9.

@rsc rsc changed the title proposal: cmd/objdump: print Go code alongside assembly cmd/objdump: print Go code alongside assembly Jan 5, 2017
@gopherbot
Copy link

CL https://golang.org/cl/37953 mentions this issue.

@rugginoso
Copy link
Contributor

@ALTree I've added you for the code review since you looked at this issue.

@dr2chase
Copy link
Contributor

dr2chase commented Mar 9, 2017

You might want to see how the output looks in combination with https://go-review.googlesource.com/c/36207/ . This is designed to reduce the amount of churn in line numbers from optimization (mostly from value movement in/out of registers) and might lead to somewhat more sensible output. Longer-term we ought to make use of Dwarf's support for "not a line number" but for now the unimportant instructions inherit line numbers from their predecessor.

@rugginoso
Copy link
Contributor

@dr2chase for the binary I tested (the one generated from src/cmd/objdump/testdata/fmthello.go), the output is the same as without CL 36207 (pasting here both outputs in the case I miss something):

Without 36207:

TEXT main.main(SB) /home/rugginoso/Documents/src/go/src/cmd/objdump/testdata/fmthello.go
func main() {
			0x47c2a0	64488b0c25f8ffffff	FS MOVQ FS:0xfffffff8, CX
			0x47c2a9	483b6110		CMPQ 0x10(CX), SP
			0x47c2ad	7631			JBE 0x47c2e0
			0x47c2af	4883ec18		SUBQ $0x18, SP
			0x47c2b3	48896c2410		MOVQ BP, 0x10(SP)
			0x47c2b8	488d6c2410		LEAQ 0x10(SP), BP
	Println("hello, world")
			0x47c2bd	488d0513b20200		LEAQ 0x2b213(IP), AX
			0x47c2c4	48890424		MOVQ AX, 0(SP)
			0x47c2c8	48c74424080c000000	MOVQ $0xc, 0x8(SP)
			0x47c2d1	e81a000000		CALL main.Println(SB)
}
			0x47c2d6	488b6c2410	MOVQ 0x10(SP), BP
			0x47c2db	4883c418	ADDQ $0x18, SP
			0x47c2df	c3		RET
func main() {
			0x47c2e0	e8cbf3fcff	CALL runtime.morestack_noctxt(SB)
			0x47c2e5	ebb9		JMP main.main(SB)

With 36207:

TEXT main.main(SB) /home/rugginoso/Documenti/src/go/src/cmd/objdump/testdata/fmthello.go
func main() {
			0x47c850	64488b0c25f8ffffff	FS MOVQ FS:0xfffffff8, CX	
			0x47c859	483b6110		CMPQ 0x10(CX), SP		
			0x47c85d	7631			JBE 0x47c890			
			0x47c85f	4883ec18		SUBQ $0x18, SP			
			0x47c863	48896c2410		MOVQ BP, 0x10(SP)		
			0x47c868	488d6c2410		LEAQ 0x10(SP), BP		
	Println("hello, world")
			0x47c86d	488d0563ac0200		LEAQ 0x2ac63(IP), AX	
			0x47c874	48890424		MOVQ AX, 0(SP)		
			0x47c878	48c74424080c000000	MOVQ $0xc, 0x8(SP)	
			0x47c881	e81a000000		CALL main.Println(SB)	
}
			0x47c886	488b6c2410	MOVQ 0x10(SP), BP	
			0x47c88b	4883c418	ADDQ $0x18, SP		
			0x47c88f	c3		RET			
func main() {
			0x47c890	e82bf1fcff	CALL runtime.morestack_noctxt(SB)	
			0x47c895	ebb9		JMP main.main(SB)

This example is probably too simplistic.

lparth pushed a commit to lparth/go that referenced this issue Apr 13, 2017
Added -S flag to print go source file line above corresponding disassembly:

$ go tool objdump -S -s main.main fmthello
TEXT main.main(SB) /home/rugginoso/Documents/src/go/src/cmd/objdump/testdata/fmthello.go
func main() {
  0x47d450		64488b0c25f8ffffff	FS MOVQ FS:0xfffffff8, CX
  0x47d459		483b6110		CMPQ 0x10(CX), SP
  0x47d45d		7631			JBE 0x47d490
  0x47d45f		4883ec18		SUBQ $0x18, SP
  0x47d463		48896c2410		MOVQ BP, 0x10(SP)
  0x47d468		488d6c2410		LEAQ 0x10(SP), BP
	Println("hello, world")
  0x47d46d		488d0563b00200		LEAQ 0x2b063(IP), AX
  0x47d474		48890424		MOVQ AX, 0(SP)
  0x47d478		48c74424080c000000	MOVQ $0xc, 0x8(SP)
  0x47d481		e81a000000		CALL main.Println(SB)
}
  0x47d486		488b6c2410		MOVQ 0x10(SP), BP
  0x47d48b		4883c418		ADDQ $0x18, SP
  0x47d48f		c3			RET
func main() {
  0x47d490		e8ebf1fcff		CALL runtime.morestack_noctxt(SB)
  0x47d495		ebb9			JMP main.main(SB)

Execution time:

$ time go tool objdump testdata/fmthello > /dev/null
real	0m0.430s
user	0m0.440s
sys	0m0.000s

$ time go tool objdump -S testdata/fmthello > /dev/null
real	0m0.471s
user	0m0.476s
sys	0m0.012s

Fixes golang#18245

Change-Id: I9b2f8338f9ee443c1352efd270d3ba85e3dd9b78
Reviewed-on: https://go-review.googlesource.com/37953
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
@golang golang locked and limited conversation to collaborators Apr 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge help wanted Proposal-Accepted Suggested Issues that may be good for new contributors looking for work to do.
Projects
None yet
Development

No branches or pull requests

7 participants