From f1ceba090952b22ec253c9119f3f58fd03485754 Mon Sep 17 00:00:00 2001 From: Derek Parker Date: Sun, 26 Oct 2014 12:44:26 -0500 Subject: [PATCH] Support 32 bit floats --- _fixtures/testvariables.go | 3 ++- proctl/variables_linux_amd64.go | 21 +++++++++++++++------ proctl/variables_test.go | 3 ++- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/_fixtures/testvariables.go b/_fixtures/testvariables.go index c310e167..c9d741ef 100644 --- a/_fixtures/testvariables.go +++ b/_fixtures/testvariables.go @@ -18,9 +18,10 @@ func foobar(baz string) { a7 = &FooBar{Baz: 5, Bur: "strum"} neg = -1 i8 = int8(1) + f32 = float32(1.2) ) - fmt.Println(a1, a2, a3, a4, a5, a6, a7, baz, neg, i8) + fmt.Println(a1, a2, a3, a4, a5, a6, a7, baz, neg, i8, f32) } func main() { diff --git a/proctl/variables_linux_amd64.go b/proctl/variables_linux_amd64.go index 098e414b..f4a1d14b 100644 --- a/proctl/variables_linux_amd64.go +++ b/proctl/variables_linux_amd64.go @@ -141,7 +141,7 @@ func (thread *ThreadContext) extractValue(instructions []byte, off int64, typ in case *dwarf.IntType: return thread.readInt(offaddr, t.ByteSize) case *dwarf.FloatType: - return thread.readFloat64(offaddr) + return thread.readFloat(offaddr, t.ByteSize) } return "", fmt.Errorf("could not find value for type %s", typ) @@ -242,16 +242,25 @@ func (thread *ThreadContext) readInt(addr uintptr, size int64) (string, error) { return strconv.Itoa(n), nil } -func (thread *ThreadContext) readFloat64(addr uintptr) (string, error) { - var n float64 - val, err := thread.readMemory(addr, 8) +func (thread *ThreadContext) readFloat(addr uintptr, size int64) (string, error) { + val, err := thread.readMemory(addr, uintptr(size)) if err != nil { return "", err } buf := bytes.NewBuffer(val) - binary.Read(buf, binary.LittleEndian, &n) - return strconv.FormatFloat(n, 'f', -1, 64), nil + switch size { + case 4: + n := float32(0) + binary.Read(buf, binary.LittleEndian, &n) + return strconv.FormatFloat(float64(n), 'f', -1, int(size)*8), nil + case 8: + n := float64(0) + binary.Read(buf, binary.LittleEndian, &n) + return strconv.FormatFloat(n, 'f', -1, int(size)*8), nil + } + + return "", fmt.Errorf("could not read float") } func (thread *ThreadContext) readMemory(addr uintptr, size uintptr) ([]byte, error) { diff --git a/proctl/variables_test.go b/proctl/variables_test.go index e165bafe..226912f4 100644 --- a/proctl/variables_test.go +++ b/proctl/variables_test.go @@ -31,10 +31,11 @@ func TestVariableEvaluation(t *testing.T) { {"baz", "bazburzum", "struct string"}, {"neg", "-1", "int"}, {"i8", "1", "int8"}, + {"f32", "1.2", "float32"}, } helper.WithTestProcess(executablePath, t, func(p *proctl.DebuggedProcess) { - pc, _, _ := p.GoSymTable.LineToPC(fp, 23) + pc, _, _ := p.GoSymTable.LineToPC(fp, 24) _, err := p.Break(uintptr(pc)) assertNoError(err, t, "Break() returned an error") -- GitLab