Commit b211d060 authored by Alex Brainman's avatar Alex Brainman

cmd/addr2line: works with windows pe executables now

Update #7406
Fixes #7899

LGTM=bradfitz
R=golang-codereviews, rsc, bradfitz
CC=golang-codereviews
https://golang.org/cl/96960043
parent bb5a827a
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"bytes"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
)
func loadSyms(t *testing.T) map[string]string {
cmd := exec.Command("go", "tool", "nm", os.Args[0])
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("go tool nm %v: %v\n%s", os.Args[0], err, string(out))
}
syms := make(map[string]string)
scanner := bufio.NewScanner(bytes.NewReader(out))
for scanner.Scan() {
f := strings.Fields(scanner.Text())
if len(f) < 3 {
continue
}
syms[f[2]] = f[0]
}
if err := scanner.Err(); err != nil {
t.Fatalf("error reading symbols: %v", err)
}
return syms
}
func runAddr2Line(t *testing.T, exepath, addr string) (funcname, path, lineno string) {
cmd := exec.Command(exepath, os.Args[0])
cmd.Stdin = strings.NewReader(addr)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("go tool addr2line %v: %v\n%s", os.Args[0], err, string(out))
}
f := strings.Split(string(out), "\n")
if len(f) < 3 && f[2] == "" {
t.Fatal("addr2line output must have 2 lines")
}
funcname = f[0]
pathAndLineNo := f[1]
f = strings.Split(pathAndLineNo, ":")
if runtime.GOOS == "windows" {
switch len(f) {
case 2:
return funcname, f[0], f[1]
case 3:
return funcname, f[0] + ":" + f[1], f[2]
default:
t.Fatalf("no line number found in %q", pathAndLineNo)
}
}
if len(f) != 2 {
t.Fatalf("no line number found in %q", pathAndLineNo)
}
return funcname, f[0], f[1]
}
func TestAddr2Line(t *testing.T) {
syms := loadSyms(t)
tmpDir, err := ioutil.TempDir("", "TestAddr2Line")
if err != nil {
t.Fatal("TempDir failed: ", err)
}
defer os.RemoveAll(tmpDir)
exepath := filepath.Join(tmpDir, "testaddr2line.exe")
out, err := exec.Command("go", "build", "-o", exepath, "cmd/addr2line").CombinedOutput()
if err != nil {
t.Fatalf("go build -o %v cmd/addr2line: %v\n%s", exepath, err, string(out))
}
const symName = "cmd/addr2line.TestAddr2Line"
funcName, srcPath, srcLineNo := runAddr2Line(t, exepath, syms[symName])
if symName != funcName {
t.Fatalf("expected function name %v; got %v", symName, funcName)
}
fi1, err := os.Stat("addr2line_test.go")
if err != nil {
t.Fatalf("Stat failed: %v", err)
}
fi2, err := os.Stat(srcPath)
if err != nil {
t.Fatalf("Stat failed: %v", err)
}
if !os.SameFile(fi1, fi2) {
t.Fatalf("addr2line_test.go and %s are not same file", srcPath)
}
if srcLineNo != "70" {
t.Fatalf("line number = %v; want 70", srcLineNo)
}
}
......@@ -141,18 +141,50 @@ func loadTables(f *os.File) (textStart uint64, symtab, pclntab []byte, err error
if sect := obj.Section(".text"); sect != nil {
textStart = uint64(sect.VirtualAddress)
}
if sect := obj.Section(".gosymtab"); sect != nil {
if symtab, err = sect.Data(); err != nil {
return 0, nil, nil, err
}
if pclntab, err = loadPETable(obj, "pclntab", "epclntab"); err != nil {
return 0, nil, nil, err
}
if sect := obj.Section(".gopclntab"); sect != nil {
if pclntab, err = sect.Data(); err != nil {
return 0, nil, nil, err
}
if symtab, err = loadPETable(obj, "symtab", "esymtab"); err != nil {
return 0, nil, nil, err
}
return textStart, symtab, pclntab, nil
}
return 0, nil, nil, fmt.Errorf("unrecognized binary format")
}
func findPESymbol(f *pe.File, name string) (*pe.Symbol, error) {
for _, s := range f.Symbols {
if s.Name != name {
continue
}
if s.SectionNumber <= 0 {
return nil, fmt.Errorf("symbol %s: invalid section number %d", name, s.SectionNumber)
}
if len(f.Sections) < int(s.SectionNumber) {
return nil, fmt.Errorf("symbol %s: section number %d is larger than max %d", name, s.SectionNumber, len(f.Sections))
}
return s, nil
}
return nil, fmt.Errorf("no %s symbol found", name)
}
func loadPETable(f *pe.File, sname, ename string) ([]byte, error) {
ssym, err := findPESymbol(f, sname)
if err != nil {
return nil, err
}
esym, err := findPESymbol(f, ename)
if err != nil {
return nil, err
}
if ssym.SectionNumber != esym.SectionNumber {
return nil, fmt.Errorf("%s and %s symbols must be in the same section", sname, ename)
}
sect := f.Sections[ssym.SectionNumber-1]
data, err := sect.Data()
if err != nil {
return nil, err
}
return data[ssym.Value:esym.Value], nil
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment