summaryrefslogtreecommitdiff
path: root/src/cmd/addr2line
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/addr2line')
-rw-r--r--src/cmd/addr2line/addr2line_test.go115
-rw-r--r--src/cmd/addr2line/main.c90
-rw-r--r--src/cmd/addr2line/main.go253
3 files changed, 368 insertions, 90 deletions
diff --git a/src/cmd/addr2line/addr2line_test.go b/src/cmd/addr2line/addr2line_test.go
new file mode 100644
index 000000000..b278d08ce
--- /dev/null
+++ b/src/cmd/addr2line/addr2line_test.go
@@ -0,0 +1,115 @@
+// 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]
+}
+
+const symName = "cmd/addr2line.TestAddr2Line"
+
+func testAddr2Line(t *testing.T, exepath, addr string) {
+ funcName, srcPath, srcLineNo := runAddr2Line(t, exepath, addr)
+ 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 != "94" {
+ t.Fatalf("line number = %v; want 94", srcLineNo)
+ }
+}
+
+// This is line 93. The test depends on that.
+func TestAddr2Line(t *testing.T) {
+ if runtime.GOOS == "nacl" {
+ t.Skip("skipping on nacl")
+ }
+
+ 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))
+ }
+
+ testAddr2Line(t, exepath, syms[symName])
+ testAddr2Line(t, exepath, "0x"+syms[symName])
+}
diff --git a/src/cmd/addr2line/main.c b/src/cmd/addr2line/main.c
deleted file mode 100644
index 54c4d90b5..000000000
--- a/src/cmd/addr2line/main.c
+++ /dev/null
@@ -1,90 +0,0 @@
-// Copyright 2012 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.
-
-/*
- * addr2line simulation - only enough to make pprof work on Macs
- */
-
-#include <u.h>
-#include <libc.h>
-#include <bio.h>
-#include <mach.h>
-
-void
-printusage(int fd)
-{
- fprint(fd, "usage: addr2line binary\n");
- fprint(fd, "reads addresses from standard input and writes two lines for each:\n");
- fprint(fd, "\tfunction name\n");
- fprint(fd, "\tfile:line\n");
-}
-
-void
-usage(void)
-{
- printusage(2);
- exits("usage");
-}
-
-void
-main(int argc, char **argv)
-{
- int fd;
- char *p, *q;
- uvlong pc;
- Symbol s;
- Fhdr fhdr;
- Biobuf bin, bout;
- char file[1024];
-
- if(argc > 1 && strcmp(argv[1], "--help") == 0) {
- printusage(1);
- exits(0);
- }
-
- ARGBEGIN{
- default:
- usage();
- }ARGEND
-
- if(argc != 1)
- usage();
-
- fd = open(argv[0], OREAD);
- if(fd < 0)
- sysfatal("open %s: %r", argv[0]);
- if(crackhdr(fd, &fhdr) <= 0)
- sysfatal("crackhdr: %r");
- machbytype(fhdr.type);
- if(syminit(fd, &fhdr) <= 0)
- sysfatal("syminit: %r");
-
- Binit(&bin, 0, OREAD);
- Binit(&bout, 1, OWRITE);
- for(;;) {
- p = Brdline(&bin, '\n');
- if(p == nil)
- break;
- p[Blinelen(&bin)-1] = '\0';
- q = strchr(p, ':');
- if(q != nil) {
- // reverse: translate file:line to pc
- *q++ = '\0';
- pc = file2pc(p, atoi(q));
- if(pc == ~(uvlong)0)
- Bprint(&bout, "!%r\n");
- else
- Bprint(&bout, "0x%llux\n", pc);
- continue;
- }
- pc = strtoull(p, 0, 16);
- if(!findsym(pc, CTEXT, &s))
- s.name = "??";
- if(!fileline(file, sizeof file, pc))
- strcpy(file, "??:0");
- Bprint(&bout, "%s\n%s\n", s.name, file);
- }
- Bflush(&bout);
- exits(0);
-}
diff --git a/src/cmd/addr2line/main.go b/src/cmd/addr2line/main.go
new file mode 100644
index 000000000..b94ba12ef
--- /dev/null
+++ b/src/cmd/addr2line/main.go
@@ -0,0 +1,253 @@
+// Copyright 2012 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.
+
+// Addr2line is a minimal simulation of the GNU addr2line tool,
+// just enough to support pprof.
+//
+// Usage:
+// go tool addr2line binary
+//
+// Addr2line reads hexadecimal addresses, one per line and with optional 0x prefix,
+// from standard input. For each input address, addr2line prints two output lines,
+// first the name of the function containing the address and second the file:line
+// of the source code corresponding to that address.
+//
+// This tool is intended for use only by pprof; its interface may change or
+// it may be deleted entirely in future releases.
+package main
+
+import (
+ "bufio"
+ "debug/elf"
+ "debug/gosym"
+ "debug/macho"
+ "debug/pe"
+ "debug/plan9obj"
+ "flag"
+ "fmt"
+ "log"
+ "os"
+ "strconv"
+ "strings"
+)
+
+func printUsage(w *os.File) {
+ fmt.Fprintf(w, "usage: addr2line binary\n")
+ fmt.Fprintf(w, "reads addresses from standard input and writes two lines for each:\n")
+ fmt.Fprintf(w, "\tfunction name\n")
+ fmt.Fprintf(w, "\tfile:line\n")
+}
+
+func usage() {
+ printUsage(os.Stderr)
+ os.Exit(2)
+}
+
+func main() {
+ log.SetFlags(0)
+ log.SetPrefix("addr2line: ")
+
+ // pprof expects this behavior when checking for addr2line
+ if len(os.Args) > 1 && os.Args[1] == "--help" {
+ printUsage(os.Stdout)
+ os.Exit(0)
+ }
+
+ flag.Usage = usage
+ flag.Parse()
+ if flag.NArg() != 1 {
+ usage()
+ }
+
+ f, err := os.Open(flag.Arg(0))
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ textStart, symtab, pclntab, err := loadTables(f)
+ if err != nil {
+ log.Fatalf("reading %s: %v", flag.Arg(0), err)
+ }
+
+ pcln := gosym.NewLineTable(pclntab, textStart)
+ tab, err := gosym.NewTable(symtab, pcln)
+ if err != nil {
+ log.Fatalf("reading %s: %v", flag.Arg(0), err)
+ }
+
+ stdin := bufio.NewScanner(os.Stdin)
+ stdout := bufio.NewWriter(os.Stdout)
+
+ for stdin.Scan() {
+ p := stdin.Text()
+ if strings.Contains(p, ":") {
+ // Reverse translate file:line to pc.
+ // This was an extension in the old C version of 'go tool addr2line'
+ // and is probably not used by anyone, but recognize the syntax.
+ // We don't have an implementation.
+ fmt.Fprintf(stdout, "!reverse translation not implemented\n")
+ continue
+ }
+ pc, _ := strconv.ParseUint(strings.TrimPrefix(p, "0x"), 16, 64)
+ file, line, fn := tab.PCToLine(pc)
+ name := "?"
+ if fn != nil {
+ name = fn.Name
+ } else {
+ file = "?"
+ line = 0
+ }
+ fmt.Fprintf(stdout, "%s\n%s:%d\n", name, file, line)
+ }
+ stdout.Flush()
+}
+
+func loadTables(f *os.File) (textStart uint64, symtab, pclntab []byte, err error) {
+ if obj, err := elf.NewFile(f); err == nil {
+ if sect := obj.Section(".text"); sect != nil {
+ textStart = sect.Addr
+ }
+ if sect := obj.Section(".gosymtab"); sect != nil {
+ if symtab, err = sect.Data(); 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
+ }
+ }
+ return textStart, symtab, pclntab, nil
+ }
+
+ if obj, err := macho.NewFile(f); err == nil {
+ if sect := obj.Section("__text"); sect != nil {
+ textStart = sect.Addr
+ }
+ if sect := obj.Section("__gosymtab"); sect != nil {
+ if symtab, err = sect.Data(); 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
+ }
+ }
+ return textStart, symtab, pclntab, nil
+ }
+
+ if obj, err := pe.NewFile(f); err == nil {
+ var imageBase uint64
+ switch oh := obj.OptionalHeader.(type) {
+ case *pe.OptionalHeader32:
+ imageBase = uint64(oh.ImageBase)
+ case *pe.OptionalHeader64:
+ imageBase = oh.ImageBase
+ default:
+ return 0, nil, nil, fmt.Errorf("pe file format not recognized")
+ }
+ if sect := obj.Section(".text"); sect != nil {
+ textStart = imageBase + uint64(sect.VirtualAddress)
+ }
+ if pclntab, err = loadPETable(obj, "pclntab", "epclntab"); 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
+ }
+
+ if obj, err := plan9obj.NewFile(f); err == nil {
+ sym, err := findPlan9Symbol(obj, "text")
+ if err != nil {
+ return 0, nil, nil, err
+ }
+ textStart = sym.Value
+ if pclntab, err = loadPlan9Table(obj, "pclntab", "epclntab"); err != nil {
+ return 0, nil, nil, err
+ }
+ if symtab, err = loadPlan9Table(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
+}
+
+func findPlan9Symbol(f *plan9obj.File, name string) (*plan9obj.Sym, error) {
+ syms, err := f.Symbols()
+ if err != nil {
+ return nil, err
+ }
+ for _, s := range syms {
+ if s.Name != name {
+ continue
+ }
+ return &s, nil
+ }
+ return nil, fmt.Errorf("no %s symbol found", name)
+}
+
+func loadPlan9Table(f *plan9obj.File, sname, ename string) ([]byte, error) {
+ ssym, err := findPlan9Symbol(f, sname)
+ if err != nil {
+ return nil, err
+ }
+ esym, err := findPlan9Symbol(f, ename)
+ if err != nil {
+ return nil, err
+ }
+ text, err := findPlan9Symbol(f, "text")
+ if err != nil {
+ return nil, err
+ }
+ sect := f.Section("text")
+ if sect == nil {
+ return nil, err
+ }
+ data, err := sect.Data()
+ if err != nil {
+ return nil, err
+ }
+ return data[ssym.Value-text.Value : esym.Value-text.Value], nil
+}