github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/src/debug/gosym/pclntab_test.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gosym
     6  
     7  import (
     8  	"debug/elf"
     9  	"internal/testenv"
    10  	"io/ioutil"
    11  	"os"
    12  	"os/exec"
    13  	"path/filepath"
    14  	"runtime"
    15  	"strings"
    16  	"testing"
    17  )
    18  
    19  var (
    20  	pclineTempDir    string
    21  	pclinetestBinary string
    22  )
    23  
    24  func dotest(t *testing.T) {
    25  	testenv.MustHaveGoBuild(t)
    26  	// For now, only works on amd64 platforms.
    27  	if runtime.GOARCH != "amd64" {
    28  		t.Skipf("skipping on non-AMD64 system %s", runtime.GOARCH)
    29  	}
    30  	var err error
    31  	pclineTempDir, err = ioutil.TempDir("", "pclinetest")
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  	// This command builds pclinetest from pclinetest.asm;
    36  	// the resulting binary looks like it was built from pclinetest.s,
    37  	// but we have renamed it to keep it away from the go tool.
    38  	pclinetestBinary = filepath.Join(pclineTempDir, "pclinetest")
    39  	cmd := exec.Command("go", "tool", "asm", "-o", pclinetestBinary+".o", "pclinetest.asm")
    40  	cmd.Stdout = os.Stdout
    41  	cmd.Stderr = os.Stderr
    42  	if err := cmd.Run(); err != nil {
    43  		t.Fatal(err)
    44  	}
    45  	cmd = exec.Command("go", "tool", "link", "-H", "linux",
    46  		"-o", pclinetestBinary, pclinetestBinary+".o")
    47  	cmd.Stdout = os.Stdout
    48  	cmd.Stderr = os.Stderr
    49  	if err := cmd.Run(); err != nil {
    50  		t.Fatal(err)
    51  	}
    52  }
    53  
    54  func endtest() {
    55  	if pclineTempDir != "" {
    56  		os.RemoveAll(pclineTempDir)
    57  		pclineTempDir = ""
    58  		pclinetestBinary = ""
    59  	}
    60  }
    61  
    62  // skipIfNotELF skips the test if we are not running on an ELF system.
    63  // These tests open and examine the test binary, and use elf.Open to do so.
    64  func skipIfNotELF(t *testing.T) {
    65  	switch runtime.GOOS {
    66  	case "dragonfly", "freebsd", "linux", "netbsd", "openbsd", "solaris":
    67  		// OK.
    68  	default:
    69  		t.Skipf("skipping on non-ELF system %s", runtime.GOOS)
    70  	}
    71  }
    72  
    73  func getTable(t *testing.T) *Table {
    74  	f, tab := crack(os.Args[0], t)
    75  	f.Close()
    76  	return tab
    77  }
    78  
    79  func crack(file string, t *testing.T) (*elf.File, *Table) {
    80  	// Open self
    81  	f, err := elf.Open(file)
    82  	if err != nil {
    83  		t.Fatal(err)
    84  	}
    85  	return parse(file, f, t)
    86  }
    87  
    88  func parse(file string, f *elf.File, t *testing.T) (*elf.File, *Table) {
    89  	s := f.Section(".gosymtab")
    90  	if s == nil {
    91  		t.Skip("no .gosymtab section")
    92  	}
    93  	symdat, err := s.Data()
    94  	if err != nil {
    95  		f.Close()
    96  		t.Fatalf("reading %s gosymtab: %v", file, err)
    97  	}
    98  	pclndat, err := f.Section(".gopclntab").Data()
    99  	if err != nil {
   100  		f.Close()
   101  		t.Fatalf("reading %s gopclntab: %v", file, err)
   102  	}
   103  
   104  	pcln := NewLineTable(pclndat, f.Section(".text").Addr)
   105  	tab, err := NewTable(symdat, pcln)
   106  	if err != nil {
   107  		f.Close()
   108  		t.Fatalf("parsing %s gosymtab: %v", file, err)
   109  	}
   110  
   111  	return f, tab
   112  }
   113  
   114  func TestLineFromAline(t *testing.T) {
   115  	skipIfNotELF(t)
   116  
   117  	tab := getTable(t)
   118  	if tab.go12line != nil {
   119  		// aline's don't exist in the Go 1.2 table.
   120  		t.Skip("not relevant to Go 1.2 symbol table")
   121  	}
   122  
   123  	// Find the sym package
   124  	pkg := tab.LookupFunc("debug/gosym.TestLineFromAline").Obj
   125  	if pkg == nil {
   126  		t.Fatalf("nil pkg")
   127  	}
   128  
   129  	// Walk every absolute line and ensure that we hit every
   130  	// source line monotonically
   131  	lastline := make(map[string]int)
   132  	final := -1
   133  	for i := 0; i < 10000; i++ {
   134  		path, line := pkg.lineFromAline(i)
   135  		// Check for end of object
   136  		if path == "" {
   137  			if final == -1 {
   138  				final = i - 1
   139  			}
   140  			continue
   141  		} else if final != -1 {
   142  			t.Fatalf("reached end of package at absolute line %d, but absolute line %d mapped to %s:%d", final, i, path, line)
   143  		}
   144  		// It's okay to see files multiple times (e.g., sys.a)
   145  		if line == 1 {
   146  			lastline[path] = 1
   147  			continue
   148  		}
   149  		// Check that the is the next line in path
   150  		ll, ok := lastline[path]
   151  		if !ok {
   152  			t.Errorf("file %s starts on line %d", path, line)
   153  		} else if line != ll+1 {
   154  			t.Fatalf("expected next line of file %s to be %d, got %d", path, ll+1, line)
   155  		}
   156  		lastline[path] = line
   157  	}
   158  	if final == -1 {
   159  		t.Errorf("never reached end of object")
   160  	}
   161  }
   162  
   163  func TestLineAline(t *testing.T) {
   164  	skipIfNotELF(t)
   165  
   166  	tab := getTable(t)
   167  	if tab.go12line != nil {
   168  		// aline's don't exist in the Go 1.2 table.
   169  		t.Skip("not relevant to Go 1.2 symbol table")
   170  	}
   171  
   172  	for _, o := range tab.Files {
   173  		// A source file can appear multiple times in a
   174  		// object.  alineFromLine will always return alines in
   175  		// the first file, so track which lines we've seen.
   176  		found := make(map[string]int)
   177  		for i := 0; i < 1000; i++ {
   178  			path, line := o.lineFromAline(i)
   179  			if path == "" {
   180  				break
   181  			}
   182  
   183  			// cgo files are full of 'Z' symbols, which we don't handle
   184  			if len(path) > 4 && path[len(path)-4:] == ".cgo" {
   185  				continue
   186  			}
   187  
   188  			if minline, ok := found[path]; path != "" && ok {
   189  				if minline >= line {
   190  					// We've already covered this file
   191  					continue
   192  				}
   193  			}
   194  			found[path] = line
   195  
   196  			a, err := o.alineFromLine(path, line)
   197  			if err != nil {
   198  				t.Errorf("absolute line %d in object %s maps to %s:%d, but mapping that back gives error %s", i, o.Paths[0].Name, path, line, err)
   199  			} else if a != i {
   200  				t.Errorf("absolute line %d in object %s maps to %s:%d, which maps back to absolute line %d\n", i, o.Paths[0].Name, path, line, a)
   201  			}
   202  		}
   203  	}
   204  }
   205  
   206  func TestPCLine(t *testing.T) {
   207  	dotest(t)
   208  	defer endtest()
   209  
   210  	f, tab := crack(pclinetestBinary, t)
   211  	defer f.Close()
   212  	text := f.Section(".text")
   213  	textdat, err := text.Data()
   214  	if err != nil {
   215  		t.Fatalf("reading .text: %v", err)
   216  	}
   217  
   218  	// Test PCToLine
   219  	sym := tab.LookupFunc("linefrompc")
   220  	wantLine := 0
   221  	for pc := sym.Entry; pc < sym.End; pc++ {
   222  		off := pc - text.Addr // TODO(rsc): should not need off; bug in 8g
   223  		if textdat[off] == 255 {
   224  			break
   225  		}
   226  		wantLine += int(textdat[off])
   227  		t.Logf("off is %d %#x (max %d)", off, textdat[off], sym.End-pc)
   228  		file, line, fn := tab.PCToLine(pc)
   229  		if fn == nil {
   230  			t.Errorf("failed to get line of PC %#x", pc)
   231  		} else if !strings.HasSuffix(file, "pclinetest.asm") || line != wantLine || fn != sym {
   232  			t.Errorf("PCToLine(%#x) = %s:%d (%s), want %s:%d (%s)", pc, file, line, fn.Name, "pclinetest.asm", wantLine, sym.Name)
   233  		}
   234  	}
   235  
   236  	// Test LineToPC
   237  	sym = tab.LookupFunc("pcfromline")
   238  	lookupline := -1
   239  	wantLine = 0
   240  	off := uint64(0) // TODO(rsc): should not need off; bug in 8g
   241  	for pc := sym.Value; pc < sym.End; pc += 2 + uint64(textdat[off]) {
   242  		file, line, fn := tab.PCToLine(pc)
   243  		off = pc - text.Addr
   244  		if textdat[off] == 255 {
   245  			break
   246  		}
   247  		wantLine += int(textdat[off])
   248  		if line != wantLine {
   249  			t.Errorf("expected line %d at PC %#x in pcfromline, got %d", wantLine, pc, line)
   250  			off = pc + 1 - text.Addr
   251  			continue
   252  		}
   253  		if lookupline == -1 {
   254  			lookupline = line
   255  		}
   256  		for ; lookupline <= line; lookupline++ {
   257  			pc2, fn2, err := tab.LineToPC(file, lookupline)
   258  			if lookupline != line {
   259  				// Should be nothing on this line
   260  				if err == nil {
   261  					t.Errorf("expected no PC at line %d, got %#x (%s)", lookupline, pc2, fn2.Name)
   262  				}
   263  			} else if err != nil {
   264  				t.Errorf("failed to get PC of line %d: %s", lookupline, err)
   265  			} else if pc != pc2 {
   266  				t.Errorf("expected PC %#x (%s) at line %d, got PC %#x (%s)", pc, fn.Name, line, pc2, fn2.Name)
   267  			}
   268  		}
   269  		off = pc + 1 - text.Addr
   270  	}
   271  }