Debugging Code Generation in Go

E7526ec3e801f8ba99f6746498a154a6?s=47 JBD
February 02, 2020

Debugging Code Generation in Go

E7526ec3e801f8ba99f6746498a154a6?s=128

JBD

February 02, 2020
Tweet

Transcript

  1. Debugging Code Generation in Go Jaana B. Dogan, Google jbd@google.com

  2. None
  3. None
  4. @rakyll Who needs this talk? • Developers who want to

    debug the compilation. • Reverse engineers, security practitioners, ... • Current or prospective Go contributors. • Anyone who wants to study compilers.
  5. Lexing and parsing Transformations on AST SSA optimizations

  6. main program package main import "fmt" func main() { sum

    := 1 + 1 fmt.Printf("sum: %v\n", sum) }
  7. main program $ go build -x WORK=/var/folders/zq/wgh6df4s6fz2wjwdp5dvzlww005d 21/T/go-build399551378 mkdir -p

    $WORK/b001/ cat >$WORK/b001/importcfg.link << 'EOF' # internal packagefile github.com/rakyll/codegen=/Users/jbd/Library/Cach es/go-build/00/00ed60989b390a64bad3cb01d4ec61aae6 d0ea61e3e38fc35052d1ab5b0efe66-d packagefile ...
  8. main program $ go build -n # run dry WORK=/var/folders/zq/wgh6df4s6fz2wjwdp5dvzlww005d

    21/T/go-build399551378 mkdir -p $WORK/b001/ cat >$WORK/b001/importcfg.link << 'EOF' # internal packagefile github.com/rakyll/codegen=/Users/jbd/Library/Cach es/go-build/00/00ed60989b390a64bad3cb01d4ec61aae6 d0ea61e3e38fc35052d1ab5b0efe66-d packagefile ...
  9. main program $ go build -a # build without the

    cache
  10. main program $ go build -work # don’t delete work

    directory WORK=/var/folders/zq/wgh6df4s6fz2wjwdp5dvzlww005d 21/T/go-build444532126
  11. main program $ go list -f {{.GoFiles}} # investigate files

    [main.go] $ GOOS=windows go list -f {{.GoFiles}} [main.go main_windows.go] See https://golang.org/pkg/cmd/go/internal/list/ for more.
  12. main program $ go build -gcflags="-S" # print Go assembly

    # github.com/rakyll/codegen os.(*File).close STEXT dupok nosplit size=26 args=0x18 locals=0x0 0x0000 00000 (<autogenerated>:1) TEXT os.(*File).close(SB), DUPOK|NOSPLIT|ABIInternal, $0-24 0x0000 00000 (<autogenerated>:1) FUNCDATA$0, gclocals·e6397a44f8e1b6e77d0f200b4fba5269(SB) 0x0000 00000 (<autogenerated>:1) FUNCDATA...
  13. main program $ go tool objdump -s main.main <binary> TEXT

    main.main(SB) /Users/jbd/go/src/github.com/rakyll/codegen/main. go main.go:5 0x109adc0 65488b0c2530000000 MOVQ GS:0x30, CX main.go:5 0x109adc9 483b6110 CMPQ 0x10(CX), SP main.go:5 0x109adcd 0f8690000000 JBE 0x109ae63 main.go:5 0x109add3 4883ec68 SUBQ ...
  14. main program $ go tool nm <binary> | grep runtime.walltime

    1044b20 T runtime.walltime 1054c50 T runtime.walltime_trampoline
  15. main program $ go build -gcflags="-N" # disable optimizations $

    go build -gcflags="-l" # disable inlining $ go build -gcflags="-N -l" # disable both
  16. main program $ GOSSAFUNC=main go build && open ssa.html

  17. main program $ go build -gcflags="-m" # escape analysis $

    go build -gcflags="-m=2" # more info
  18. main program $ go build -gcflags="-live" # debug liveness $

    go build -gcflags="-live=2" # more info
  19. main program $ go build -gcflags="-bench=bench.out" $ go build -gcflags="-race"

    # race detector $ go build -gcflags="-memprofile=profile.out" $ go build -gcflags="-traceprofile=trace.out" # ...
  20. Thanks. Jaana B. Dogan, Google jbd@google.com