mirror of
				https://github.com/go-delve/delve.git
				synced 2025-11-04 06:32:16 +08:00 
			
		
		
		
	Rebuild binaries for every test
This commit is contained in:
		@ -1,6 +1,7 @@
 | 
				
			|||||||
package helper
 | 
					package helper
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
	"os/exec"
 | 
						"os/exec"
 | 
				
			||||||
	"runtime"
 | 
						"runtime"
 | 
				
			||||||
	"syscall"
 | 
						"syscall"
 | 
				
			||||||
@ -22,6 +23,11 @@ func GetRegisters(p *proctl.DebuggedProcess, t *testing.T) *syscall.PtraceRegs {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func WithTestProcess(name string, t *testing.T, fn testfunc) {
 | 
					func WithTestProcess(name string, t *testing.T, fn testfunc) {
 | 
				
			||||||
	runtime.LockOSThread()
 | 
						runtime.LockOSThread()
 | 
				
			||||||
 | 
						err := CompileTestProg(name)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatalf("Could not compile %s due to %s", name, err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	cmd, err := startTestProcess(name)
 | 
						cmd, err := startTestProcess(name)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		t.Fatal("Starting test process:", err)
 | 
							t.Fatal("Starting test process:", err)
 | 
				
			||||||
@ -32,11 +38,18 @@ func WithTestProcess(name string, t *testing.T, fn testfunc) {
 | 
				
			|||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		t.Fatal("NewDebugProcess():", err)
 | 
							t.Fatal("NewDebugProcess():", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	defer cmd.Process.Kill()
 | 
						defer func() {
 | 
				
			||||||
 | 
							cmd.Process.Kill()
 | 
				
			||||||
 | 
							os.Remove(name)
 | 
				
			||||||
 | 
						}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	fn(p)
 | 
						fn(p)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func CompileTestProg(source string) error {
 | 
				
			||||||
 | 
						return exec.Command("go", "build", "-gcflags=-N -l", "-o", source, source+".go").Run()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func startTestProcess(name string) (*exec.Cmd, error) {
 | 
					func startTestProcess(name string) (*exec.Cmd, error) {
 | 
				
			||||||
	cmd := exec.Command(name)
 | 
						cmd := exec.Command(name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -13,14 +13,15 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestFindReturnAddress(t *testing.T) {
 | 
					func TestFindReturnAddress(t *testing.T) {
 | 
				
			||||||
 | 
						var testfile, _ = filepath.Abs("../../_fixtures/testnextprog")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						helper.WithTestProcess(testfile, t, func(p *proctl.DebuggedProcess) {
 | 
				
			||||||
		var (
 | 
							var (
 | 
				
			||||||
		testfile, _ = filepath.Abs("../../_fixtures/testnextprog")
 | 
					 | 
				
			||||||
			dbframe = dwarfhelper.GrabDebugFrameSection(testfile, t)
 | 
								dbframe = dwarfhelper.GrabDebugFrameSection(testfile, t)
 | 
				
			||||||
			fdes    = frame.Parse(dbframe)
 | 
								fdes    = frame.Parse(dbframe)
 | 
				
			||||||
			gsd     = dwarfhelper.GosymData(testfile, t)
 | 
								gsd     = dwarfhelper.GosymData(testfile, t)
 | 
				
			||||||
		)
 | 
							)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	helper.WithTestProcess(testfile, t, func(p *proctl.DebuggedProcess) {
 | 
					 | 
				
			||||||
		testsourcefile := testfile + ".go"
 | 
							testsourcefile := testfile + ".go"
 | 
				
			||||||
		start, _, err := gsd.LineToPC(testsourcefile, 22)
 | 
							start, _, err := gsd.LineToPC(testsourcefile, 22)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
 | 
				
			|||||||
@ -3,6 +3,7 @@ package line
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"debug/elf"
 | 
						"debug/elf"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
 | 
						"os/exec"
 | 
				
			||||||
	"path/filepath"
 | 
						"path/filepath"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
@ -10,12 +11,7 @@ import (
 | 
				
			|||||||
	"github.com/davecheney/profile"
 | 
						"github.com/davecheney/profile"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func grabDebugLineSection(fp string, t *testing.T) []byte {
 | 
					func grabDebugLineSection(p string, t *testing.T) []byte {
 | 
				
			||||||
	p, err := filepath.Abs(fp)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		t.Fatal(err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	f, err := os.Open(p)
 | 
						f, err := os.Open(p)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		t.Fatal(err)
 | 
							t.Fatal(err)
 | 
				
			||||||
@ -36,24 +32,24 @@ func grabDebugLineSection(fp string, t *testing.T) []byte {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func TestDebugLinePrologueParser(t *testing.T) {
 | 
					func TestDebugLinePrologueParser(t *testing.T) {
 | 
				
			||||||
	// Test against known good values, from readelf --debug-dump=rawline _fixtures/testnextprog
 | 
						// Test against known good values, from readelf --debug-dump=rawline _fixtures/testnextprog
 | 
				
			||||||
	var (
 | 
						p, err := filepath.Abs("../../_fixtures/testnextprog")
 | 
				
			||||||
		data     = grabDebugLineSection("../../_fixtures/testnextprog", t)
 | 
						if err != nil {
 | 
				
			||||||
		dbl      = Parse(data)
 | 
							t.Fatal(err)
 | 
				
			||||||
		prologue = dbl.Prologue
 | 
					 | 
				
			||||||
	)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if prologue.Length != uint32(60685) {
 | 
					 | 
				
			||||||
		t.Fatal("Length was not parsed correctly", prologue.Length)
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						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)
 | 
				
			||||||
 | 
						data := grabDebugLineSection(p, t)
 | 
				
			||||||
 | 
						dbl := Parse(data)
 | 
				
			||||||
 | 
						prologue := dbl.Prologue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if prologue.Version != uint16(2) {
 | 
						if prologue.Version != uint16(2) {
 | 
				
			||||||
		t.Fatal("Version not parsed correctly", prologue.Version)
 | 
							t.Fatal("Version not parsed correctly", prologue.Version)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if prologue.PrologueLength != uint32(5363) {
 | 
					 | 
				
			||||||
		t.Fatal("Prologue Length not parsed correctly", prologue.PrologueLength)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if prologue.MinInstrLength != uint8(1) {
 | 
						if prologue.MinInstrLength != uint8(1) {
 | 
				
			||||||
		t.Fatal("Minimun Instruction Length not parsed correctly", prologue.MinInstrLength)
 | 
							t.Fatal("Minimun Instruction Length not parsed correctly", prologue.MinInstrLength)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@ -85,10 +81,6 @@ func TestDebugLinePrologueParser(t *testing.T) {
 | 
				
			|||||||
		t.Fatal("Include dirs not parsed correctly")
 | 
							t.Fatal("Include dirs not parsed correctly")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if len(dbl.FileNames) != 126 {
 | 
					 | 
				
			||||||
		t.Fatal("Filenames not parsed correctly", len(dbl.FileNames))
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if !strings.Contains(dbl.FileNames[0].Name, "/dbg/_fixtures/testnextprog.go") {
 | 
						if !strings.Contains(dbl.FileNames[0].Name, "/dbg/_fixtures/testnextprog.go") {
 | 
				
			||||||
		t.Fatal("First entry not parsed correctly")
 | 
							t.Fatal("First entry not parsed correctly")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@ -96,7 +88,12 @@ func TestDebugLinePrologueParser(t *testing.T) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func BenchmarkLineParser(b *testing.B) {
 | 
					func BenchmarkLineParser(b *testing.B) {
 | 
				
			||||||
	defer profile.Start(profile.MemProfile).Stop()
 | 
						defer profile.Start(profile.MemProfile).Stop()
 | 
				
			||||||
	data := grabDebugLineSection("../../_fixtures/testnextprog", nil)
 | 
						p, err := filepath.Abs("../../_fixtures/testnextprog")
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							b.Fatal(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						data := grabDebugLineSection(p, nil)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	b.ResetTimer()
 | 
						b.ResetTimer()
 | 
				
			||||||
	for i := 0; i < b.N; i++ {
 | 
						for i := 0; i < b.N; i++ {
 | 
				
			||||||
 | 
				
			|||||||
@ -1,6 +1,8 @@
 | 
				
			|||||||
package line
 | 
					package line
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"os/exec"
 | 
				
			||||||
	"path/filepath"
 | 
						"path/filepath"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -14,8 +16,19 @@ func init() {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestNextLocAfterPC(t *testing.T) {
 | 
					func TestNextLocAfterPC(t *testing.T) {
 | 
				
			||||||
 | 
						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)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var (
 | 
						var (
 | 
				
			||||||
		data     = grabDebugLineSection("../../_fixtures/testnextprog", t)
 | 
							data     = grabDebugLineSection(p, t)
 | 
				
			||||||
		dbl      = Parse(data)
 | 
							dbl      = Parse(data)
 | 
				
			||||||
		gosym    = dwarfhelper.GosymData(testfile, t)
 | 
							gosym    = dwarfhelper.GosymData(testfile, t)
 | 
				
			||||||
		pc, _, _ = gosym.LineToPC(testfile+".go", 20)
 | 
							pc, _, _ = gosym.LineToPC(testfile+".go", 20)
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user