diff --git a/_fixtures/testvariables.go b/_fixtures/testvariables.go index c310e1675f174ac9a12bea8e0d71695d77482378..c9d741ef50128351d81091c981121ad276bd8902 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 098e414b4b50584a414426aa375aebf7542efd9b..f4a1d14b8bc5e821038464057926a7e3ebf47075 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 e165bafeaa46f2b227c7b060248d1434cb2f0918..226912f44d755326177f9bb8bf8eee292d070477 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")