From 76076791b9640b76e39bd8429fbd889e22d90d8a Mon Sep 17 00:00:00 2001 From: Derek Parker Date: Fri, 16 Jan 2015 15:30:22 -0600 Subject: [PATCH] Fix stack frame calculation bug There were certain instances where the calculation of the stack frame was incorrect, causing for garbage to be returned by a print command. --- dwarf/frame/entries.go | 1 - dwarf/frame/table.go | 8 +------- proctl/variables.go | 9 ++++----- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/dwarf/frame/entries.go b/dwarf/frame/entries.go index 39f760a7..b5e74529 100644 --- a/dwarf/frame/entries.go +++ b/dwarf/frame/entries.go @@ -50,7 +50,6 @@ func (fde *FrameDescriptionEntry) EstablishFrame(pc uint64) *FrameContext { func (fde *FrameDescriptionEntry) ReturnAddressOffset(pc uint64) int64 { frame := fde.EstablishFrame(pc) - return frame.cfa.offset + frame.regs[fde.CIE.ReturnAddressRegister].offset } diff --git a/dwarf/frame/table.go b/dwarf/frame/table.go index 24cc04fc..84c5f407 100644 --- a/dwarf/frame/table.go +++ b/dwarf/frame/table.go @@ -157,15 +157,9 @@ func (frame *FrameContext) ExecuteUntilPC(instructions []byte) { // We only need to execute the instructions until // ctx.loc > ctx.addess (which is the address we // are currently at in the traced process). - for frame.address != frame.loc && frame.buf.Len() > 0 { + for frame.address > frame.loc && frame.buf.Len() > 0 { executeDwarfInstruction(frame) } - // make sure we get the update cfa offset - cfa := frame.CFAOffset() - for ncfa := cfa; ncfa == cfa && frame.buf.Len() > 0; { - executeDwarfInstruction(frame) - ncfa = frame.CFAOffset() - } } func executeDwarfInstruction(frame *FrameContext) { diff --git a/proctl/variables.go b/proctl/variables.go index 91ba35c9..20a1ea48 100644 --- a/proctl/variables.go +++ b/proctl/variables.go @@ -511,9 +511,8 @@ func (thread *ThreadContext) executeStackProgram(instructions []byte) (int64, er } fctx := fde.EstablishFrame(regs.PC()) - cfaOffset := fctx.CFAOffset() + int64(regs.SP()) - - address, err := op.ExecuteStackProgram(cfaOffset, instructions) + cfa := fctx.CFAOffset() + int64(regs.SP()) + address, err := op.ExecuteStackProgram(cfa, instructions) if err != nil { return 0, err } @@ -625,13 +624,13 @@ func (thread *ThreadContext) extractValue(instructions []byte, addr int64, typ i } func (thread *ThreadContext) readString(addr uintptr, size int64) (string, error) { + // deref the pointer to the string val, err := thread.readMemory(addr, uintptr(size)) if err != nil { return "", err } - - // deref the pointer to the string addr = uintptr(binary.LittleEndian.Uint64(val)) + val, err = thread.readMemory(addr, 16) if err != nil { return "", err