2009-09-01 14:01:37 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package ogle
|
|
|
|
|
|
|
|
import (
|
2009-09-25 10:39:08 -06:00
|
|
|
"debug/gosym";
|
2009-09-24 10:07:47 -06:00
|
|
|
"debug/proc";
|
2009-09-01 14:01:37 -06:00
|
|
|
"fmt";
|
2009-09-04 12:58:00 -06:00
|
|
|
"os";
|
2009-09-01 14:01:37 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// A Frame represents a single frame on a remote call stack.
|
|
|
|
type Frame struct {
|
|
|
|
// pc is the PC of the next instruction that will execute in
|
|
|
|
// this frame. For lower frames, this is the instruction
|
|
|
|
// following the CALL instruction.
|
2009-09-24 10:07:47 -06:00
|
|
|
pc, sp, fp proc.Word;
|
2009-09-01 14:01:37 -06:00
|
|
|
// The runtime.Stktop of the active stack segment
|
|
|
|
stk remoteStruct;
|
|
|
|
// The function this stack frame is in
|
2009-09-25 10:39:08 -06:00
|
|
|
fn *gosym.Func;
|
2009-09-01 14:01:37 -06:00
|
|
|
// The path and line of the CALL or current instruction. Note
|
|
|
|
// that this differs slightly from the meaning of Frame.pc.
|
|
|
|
path string;
|
|
|
|
line int;
|
|
|
|
// The inner and outer frames of this frame. outer is filled
|
|
|
|
// in lazily.
|
|
|
|
inner, outer *Frame;
|
|
|
|
}
|
|
|
|
|
2009-09-04 12:58:00 -06:00
|
|
|
// newFrame returns the top-most Frame of the given g's thread.
|
|
|
|
func newFrame(g remoteStruct) (*Frame, os.Error) {
|
|
|
|
var f *Frame;
|
|
|
|
err := try(func(a aborter) { f = aNewFrame(a, g) });
|
|
|
|
return f, err;
|
|
|
|
}
|
|
|
|
|
|
|
|
func aNewFrame(a aborter, g remoteStruct) *Frame {
|
2009-09-01 14:01:37 -06:00
|
|
|
p := g.r.p;
|
2009-09-24 10:07:47 -06:00
|
|
|
var pc, sp proc.Word;
|
2009-09-01 14:01:37 -06:00
|
|
|
|
|
|
|
// Is this G alive?
|
2009-09-04 12:58:00 -06:00
|
|
|
switch g.field(p.f.G.Status).(remoteInt).aGet(a) {
|
2009-09-01 14:01:37 -06:00
|
|
|
case p.runtime.Gidle, p.runtime.Gmoribund, p.runtime.Gdead:
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the OS thread for this G
|
|
|
|
|
|
|
|
// TODO(austin) Ideally, we could look at the G's state and
|
|
|
|
// figure out if it's on an OS thread or not. However, this
|
|
|
|
// is difficult because the state isn't updated atomically
|
|
|
|
// with scheduling changes.
|
2009-09-03 17:59:41 -06:00
|
|
|
for _, t := range p.proc.Threads() {
|
2009-09-01 14:01:37 -06:00
|
|
|
regs, err := t.Regs();
|
|
|
|
if err != nil {
|
|
|
|
// TODO(austin) What to do?
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
thisg := p.G(regs);
|
|
|
|
if thisg == g.addr().base {
|
|
|
|
// Found this G's OS thread
|
|
|
|
pc = regs.PC();
|
|
|
|
sp = regs.SP();
|
|
|
|
|
|
|
|
// If this thread crashed, try to recover it
|
|
|
|
if pc == 0 {
|
2009-09-04 12:58:00 -06:00
|
|
|
pc = p.peekUintptr(a, pc);
|
2009-09-01 14:01:37 -06:00
|
|
|
sp += 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if pc == 0 && sp == 0 {
|
|
|
|
// G is not mapped to an OS thread. Use the
|
|
|
|
// scheduler's stored PC and SP.
|
2009-09-04 12:58:00 -06:00
|
|
|
sched := g.field(p.f.G.Sched).(remoteStruct);
|
2009-09-24 10:07:47 -06:00
|
|
|
pc = proc.Word(sched.field(p.f.Gobuf.Pc).(remoteUint).aGet(a));
|
|
|
|
sp = proc.Word(sched.field(p.f.Gobuf.Sp).(remoteUint).aGet(a));
|
2009-09-01 14:01:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get Stktop
|
2009-09-04 12:58:00 -06:00
|
|
|
stk := g.field(p.f.G.Stackbase).(remotePtr).aGet(a).(remoteStruct);
|
2009-09-01 14:01:37 -06:00
|
|
|
|
2009-09-04 12:58:00 -06:00
|
|
|
return prepareFrame(a, pc, sp, stk, nil);
|
2009-09-01 14:01:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// prepareFrame creates a Frame from the PC and SP within that frame,
|
|
|
|
// as well as the active stack segment. This function takes care of
|
2009-09-04 12:58:00 -06:00
|
|
|
// traversing stack breaks and unwinding closures.
|
2009-09-24 10:07:47 -06:00
|
|
|
func prepareFrame(a aborter, pc, sp proc.Word, stk remoteStruct, inner *Frame) *Frame {
|
2009-09-01 14:01:37 -06:00
|
|
|
// Based on src/pkg/runtime/amd64/traceback.c:traceback
|
|
|
|
p := stk.r.p;
|
|
|
|
top := inner == nil;
|
|
|
|
|
|
|
|
// Get function
|
|
|
|
var path string;
|
|
|
|
var line int;
|
2009-09-25 10:39:08 -06:00
|
|
|
var fn *gosym.Func;
|
2009-09-01 14:01:37 -06:00
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
// Traverse segmented stack breaks
|
2009-09-24 10:07:47 -06:00
|
|
|
if p.sys.lessstack != nil && pc == proc.Word(p.sys.lessstack.Value) {
|
2009-09-01 14:01:37 -06:00
|
|
|
// Get stk->gobuf.pc
|
2009-09-24 10:07:47 -06:00
|
|
|
pc = proc.Word(stk.field(p.f.Stktop.Gobuf).(remoteStruct).field(p.f.Gobuf.Pc).(remoteUint).aGet(a));
|
2009-09-01 14:01:37 -06:00
|
|
|
// Get stk->gobuf.sp
|
2009-09-24 10:07:47 -06:00
|
|
|
sp = proc.Word(stk.field(p.f.Stktop.Gobuf).(remoteStruct).field(p.f.Gobuf.Sp).(remoteUint).aGet(a));
|
2009-09-01 14:01:37 -06:00
|
|
|
// Get stk->stackbase
|
2009-09-04 12:58:00 -06:00
|
|
|
stk = stk.field(p.f.Stktop.Stackbase).(remotePtr).aGet(a).(remoteStruct);
|
2009-09-01 14:01:37 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the PC of the call instruction
|
|
|
|
callpc := pc;
|
2009-09-24 10:07:47 -06:00
|
|
|
if !top && (p.sys.goexit == nil || pc != proc.Word(p.sys.goexit.Value)) {
|
2009-09-01 14:01:37 -06:00
|
|
|
callpc--;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look up function
|
2009-09-25 10:39:08 -06:00
|
|
|
path, line, fn = p.syms.PCToLine(uint64(callpc));
|
2009-09-01 14:01:37 -06:00
|
|
|
if fn != nil {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Closure?
|
|
|
|
var buf = make([]byte, p.ClosureSize());
|
|
|
|
if _, err := p.Peek(pc, buf); err != nil {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
spdelta, ok := p.ParseClosure(buf);
|
|
|
|
if ok {
|
2009-09-24 10:07:47 -06:00
|
|
|
sp += proc.Word(spdelta);
|
|
|
|
pc = p.peekUintptr(a, sp - proc.Word(p.PtrSize()));
|
2009-09-01 14:01:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if fn == nil {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute frame pointer
|
2009-09-24 10:07:47 -06:00
|
|
|
var fp proc.Word;
|
2009-09-01 14:01:37 -06:00
|
|
|
if fn.FrameSize < p.PtrSize() {
|
2009-09-24 10:07:47 -06:00
|
|
|
fp = sp + proc.Word(p.PtrSize());
|
2009-09-01 14:01:37 -06:00
|
|
|
} else {
|
2009-09-24 10:07:47 -06:00
|
|
|
fp = sp + proc.Word(fn.FrameSize);
|
2009-09-01 14:01:37 -06:00
|
|
|
}
|
|
|
|
// TODO(austin) To really figure out if we're in the prologue,
|
|
|
|
// we need to disassemble the function and look for the call
|
|
|
|
// to morestack. For now, just special case the entry point.
|
|
|
|
//
|
|
|
|
// TODO(austin) What if we're in the call to morestack in the
|
|
|
|
// prologue? Then top == false.
|
2009-09-25 10:39:08 -06:00
|
|
|
if top && pc == proc.Word(fn.Entry) {
|
2009-09-01 14:01:37 -06:00
|
|
|
// We're in the function prologue, before SP
|
|
|
|
// has been adjusted for the frame.
|
2009-09-24 10:07:47 -06:00
|
|
|
fp -= proc.Word(fn.FrameSize - p.PtrSize());
|
2009-09-01 14:01:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Frame{pc, sp, fp, stk, fn, path, line, inner, nil};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Outer returns the Frame that called this Frame, or nil if this is
|
2009-09-04 12:58:00 -06:00
|
|
|
// the outermost frame.
|
|
|
|
func (f *Frame) Outer() (*Frame, os.Error) {
|
|
|
|
var fr *Frame;
|
|
|
|
err := try(func(a aborter) { fr = f.aOuter(a) });
|
|
|
|
return fr, err;
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Frame) aOuter(a aborter) *Frame {
|
2009-09-01 14:01:37 -06:00
|
|
|
// Is there a cached outer frame
|
|
|
|
if f.outer != nil {
|
|
|
|
return f.outer;
|
|
|
|
}
|
|
|
|
|
|
|
|
p := f.stk.r.p;
|
|
|
|
|
|
|
|
sp := f.fp;
|
|
|
|
if f.fn == p.sys.newproc && f.fn == p.sys.deferproc {
|
|
|
|
// TODO(rsc) The compiler inserts two push/pop's
|
|
|
|
// around calls to go and defer. Russ says this
|
|
|
|
// should get fixed in the compiler, but we account
|
|
|
|
// for it for now.
|
2009-09-24 10:07:47 -06:00
|
|
|
sp += proc.Word(2 * p.PtrSize());
|
2009-09-01 14:01:37 -06:00
|
|
|
}
|
|
|
|
|
2009-09-24 10:07:47 -06:00
|
|
|
pc := p.peekUintptr(a, f.fp - proc.Word(p.PtrSize()));
|
2009-09-01 14:01:37 -06:00
|
|
|
if pc < 0x1000 {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2009-09-03 17:59:41 -06:00
|
|
|
// TODO(austin) Register this frame for shoot-down.
|
|
|
|
|
2009-09-04 12:58:00 -06:00
|
|
|
f.outer = prepareFrame(a, pc, sp, f.stk, f);
|
2009-09-01 14:01:37 -06:00
|
|
|
return f.outer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inner returns the Frame called by this Frame, or nil if this is the
|
|
|
|
// innermost frame.
|
|
|
|
func (f *Frame) Inner() *Frame {
|
|
|
|
return f.inner;
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Frame) String() string {
|
|
|
|
res := f.fn.Name;
|
2009-09-24 10:07:47 -06:00
|
|
|
if f.pc > proc.Word(f.fn.Value) {
|
2009-09-25 10:39:08 -06:00
|
|
|
res += fmt.Sprintf("+%#x", f.pc - proc.Word(f.fn.Entry));
|
2009-09-01 14:01:37 -06:00
|
|
|
}
|
|
|
|
return res + fmt.Sprintf(" %s:%d", f.path, f.line);
|
|
|
|
}
|