From 3d9ecdca27799276711f278946723fda77dec031 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zg=C3=BCr=20Kesim?= Date: Thu, 16 Jan 2020 10:43:30 +0100 Subject: [PATCH] Added comments to gdb_test.go --- GetRuntimeAddresses/symbolyze/gdb_test.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/GetRuntimeAddresses/symbolyze/gdb_test.go b/GetRuntimeAddresses/symbolyze/gdb_test.go index 36eac45..0f2a1cf 100644 --- a/GetRuntimeAddresses/symbolyze/gdb_test.go +++ b/GetRuntimeAddresses/symbolyze/gdb_test.go @@ -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") }