2014-06-29 16:52:21 +00:00
|
|
|
package line
|
|
|
|
|
|
|
|
import (
|
2014-10-04 02:44:43 +00:00
|
|
|
"debug/elf"
|
|
|
|
"debug/gosym"
|
|
|
|
"fmt"
|
2014-09-12 20:19:36 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2014-06-29 16:52:21 +00:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-10-04 02:44:43 +00:00
|
|
|
func parseGoSymTab(exe *elf.File, t *testing.T) *gosym.Table {
|
|
|
|
var (
|
|
|
|
symdat []byte
|
|
|
|
pclndat []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if sec := exe.Section(".gosymtab"); sec != nil {
|
|
|
|
symdat, err = sec.Data()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("could not get .gosymtab section", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sec := exe.Section(".gopclntab"); sec != nil {
|
|
|
|
pclndat, err = sec.Data()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("could not get .gopclntab section", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
2014-06-29 16:52:21 +00:00
|
|
|
|
2014-10-04 02:44:43 +00:00
|
|
|
pcln := gosym.NewLineTable(pclndat, exe.Section(".text").Addr)
|
|
|
|
tab, err := gosym.NewTable(symdat, pcln)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("could not get initialize line table", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tab
|
2014-06-29 16:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNextLocAfterPC(t *testing.T) {
|
2014-10-04 02:44:43 +00:00
|
|
|
testfile, _ := filepath.Abs("../../_fixtures/testnextprog")
|
|
|
|
|
2014-09-12 20:19:36 +00:00
|
|
|
p, err := filepath.Abs("../../_fixtures/testnextprog")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = exec.Command("go", "build", "-gcflags=-N -l", "-o", p, p+".go").Run()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not compile test file", p, err)
|
|
|
|
}
|
|
|
|
defer os.Remove(p)
|
|
|
|
|
2014-06-29 16:52:21 +00:00
|
|
|
var (
|
2014-09-17 03:37:48 +00:00
|
|
|
data = grabDebugLineSection(p, t)
|
|
|
|
dbl = Parse(data)
|
2014-06-29 16:52:21 +00:00
|
|
|
)
|
|
|
|
|
2014-10-04 02:44:43 +00:00
|
|
|
f, err := os.Open(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
e, err := elf.NewFile(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
symtab := parseGoSymTab(e, t)
|
|
|
|
pc, _, err := symtab.LineToPC(testfile+".go", 23)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
loc := dbl.NextLocation(pc, 23)
|
2014-06-29 16:52:21 +00:00
|
|
|
|
2014-07-11 19:52:55 +00:00
|
|
|
if loc.File != testfile+".go" {
|
|
|
|
t.Fatal("File not returned correctly", loc.File)
|
2014-06-29 16:52:21 +00:00
|
|
|
}
|
|
|
|
|
2014-10-04 02:44:43 +00:00
|
|
|
if loc.Line != 25 {
|
2014-07-11 19:52:55 +00:00
|
|
|
t.Fatal("Line not returned correctly", loc.Line)
|
2014-06-29 16:52:21 +00:00
|
|
|
}
|
|
|
|
}
|