Added comments to gdb_test.go

This commit is contained in:
Özgür Kesim 2020-01-16 10:43:30 +01:00
parent 554ae92194
commit 3d9ecdca27

View File

@ -10,6 +10,9 @@ import (
"testing"
)
// buildSimple calls make in the testdata-directory in order to build the
// test-binary `simple` that is linked against python3.7. If this fails, make
// necessary adjustments to the Makefile and/or your environment and try again.
func buildSimple() error {
cmd := exec.Command("make")
cmd.Dir = "testdata"
@ -19,6 +22,9 @@ func buildSimple() error {
return cmd.Run()
}
// TestSimpeGDB runs the program testdata/simple multiple times and uses gdb to
// extract the address of the symbol _PyRuntime for each pid. It compares
// those with the results from Scanner.Run().
func TestSimpleGDB(t *testing.T) {
ourResults := map[int]uint64{}
gdbResults := map[int]uint64{}
@ -43,10 +49,10 @@ func TestSimpleGDB(t *testing.T) {
defer func() {
simple.Process.Kill()
go simple.Wait()
simple.Wait()
}()
t.Logf("asking Gdb about PID %d", simple.Process.Pid)
t.Logf("Asking Gdb about PID %d", simple.Process.Pid)
offset, err := extractOffsetWithGdb(simple.Process.Pid, t)
if err != nil {
@ -70,8 +76,13 @@ func TestSimpleGDB(t *testing.T) {
}
}
// searchRX should match the output of gdb when it prints the address of the
// symbol
var searchRX = regexp.MustCompile(`Symbol "_PyRuntime" is static storage at address 0x([0-9a-fA-F]+)`)
// extractOffsetWithGdb calls gdb in batch-mode on a pid and looks up the
// address of the symbol _PyRuntime by calling `info address _PyRuntime`. If
// found, it extracts the address from the gdb-output.
func extractOffsetWithGdb(pid int, t *testing.T) (offset uint64, err error) {
cmd := exec.Command("gdb",
"--batch",
@ -102,5 +113,5 @@ func extractOffsetWithGdb(pid int, t *testing.T) (offset uint64, err error) {
return 0, err
}
return 0, fmt.Errorf("symbol not found with gdb")
return 0, fmt.Errorf("Symbol not found with gdb")
}