2009-09-03 17:59:41 -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 (
|
|
|
|
"fmt";
|
|
|
|
"os";
|
|
|
|
"ptrace";
|
|
|
|
)
|
|
|
|
|
2009-09-03 18:41:25 -06:00
|
|
|
// A Goroutine represents a goroutine in a remote process.
|
|
|
|
type Goroutine struct {
|
2009-09-03 17:59:41 -06:00
|
|
|
g remoteStruct;
|
|
|
|
frame *Frame;
|
|
|
|
dead bool;
|
|
|
|
}
|
|
|
|
|
2009-09-03 18:41:25 -06:00
|
|
|
func (t *Goroutine) String() string {
|
2009-09-03 17:59:41 -06:00
|
|
|
if t.dead {
|
|
|
|
return "<dead thread>";
|
|
|
|
}
|
2009-09-03 18:41:25 -06:00
|
|
|
// TODO(austin) Give threads friendly ID's, possibly including
|
|
|
|
// the name of the entry function.
|
2009-09-03 17:59:41 -06:00
|
|
|
return fmt.Sprintf("thread %#x", t.g.addr().base);
|
|
|
|
}
|
|
|
|
|
|
|
|
// isG0 returns true if this thread if the internal idle thread
|
2009-09-03 18:41:25 -06:00
|
|
|
func (t *Goroutine) isG0() bool {
|
2009-09-03 17:59:41 -06:00
|
|
|
return t.g.addr().base == t.g.r.p.sys.g0.addr().base;
|
|
|
|
}
|
|
|
|
|
2009-09-03 18:41:25 -06:00
|
|
|
func (t *Goroutine) resetFrame() {
|
2009-09-03 17:59:41 -06:00
|
|
|
// TODO(austin) NewFrame can abort
|
|
|
|
// TODO(austin) Reuse any live part of the current frame stack
|
|
|
|
// so existing references to Frame's keep working.
|
|
|
|
t.frame = NewFrame(t.g);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Out selects the caller frame of the current frame.
|
2009-09-03 18:41:25 -06:00
|
|
|
func (t *Goroutine) Out() os.Error {
|
2009-09-03 17:59:41 -06:00
|
|
|
// TODO(austin) Outer can abort
|
|
|
|
f := t.frame.Outer();
|
|
|
|
if f != nil {
|
|
|
|
t.frame = f;
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In selects the frame called by the current frame.
|
2009-09-03 18:41:25 -06:00
|
|
|
func (t *Goroutine) In() os.Error {
|
2009-09-03 17:59:41 -06:00
|
|
|
f := t.frame.Inner();
|
|
|
|
if f != nil {
|
|
|
|
t.frame = f;
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
func readylockedBP(ev Event) (EventAction, os.Error) {
|
|
|
|
b := ev.(*Breakpoint);
|
|
|
|
p := b.Process();
|
|
|
|
|
|
|
|
// The new g is the only argument to this function, so the
|
|
|
|
// stack will have the return address, then the G*.
|
|
|
|
regs, err := b.osThread.Regs();
|
|
|
|
if err != nil {
|
|
|
|
return EAStop, err;
|
|
|
|
}
|
|
|
|
sp := regs.SP();
|
|
|
|
addr := sp + ptrace.Word(p.PtrSize());
|
|
|
|
arg := remotePtr{remote{addr, p}, p.runtime.G};
|
2009-09-03 18:41:25 -06:00
|
|
|
gp := arg.Get();
|
|
|
|
if gp == nil {
|
|
|
|
return EAStop, UnknownGoroutine{b.osThread, 0};
|
2009-09-03 17:59:41 -06:00
|
|
|
}
|
2009-09-03 18:41:25 -06:00
|
|
|
gs := gp.(remoteStruct);
|
|
|
|
g := &Goroutine{gs, nil, false};
|
|
|
|
p.goroutines[gs.addr().base] = g;
|
2009-09-03 17:59:41 -06:00
|
|
|
|
2009-09-03 18:41:25 -06:00
|
|
|
// Enqueue goroutine creation event
|
|
|
|
parent := b.Goroutine();
|
2009-09-03 17:59:41 -06:00
|
|
|
if parent.isG0() {
|
|
|
|
parent = nil;
|
|
|
|
}
|
2009-09-03 18:41:25 -06:00
|
|
|
p.postEvent(&GoroutineCreate{commonEvent{p, g}, parent});
|
2009-09-03 17:59:41 -06:00
|
|
|
|
|
|
|
// If we don't have any thread selected, select this one
|
2009-09-03 18:41:25 -06:00
|
|
|
if p.curGoroutine == nil {
|
|
|
|
p.curGoroutine = g;
|
2009-09-03 17:59:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return EADefault, nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
func goexitBP(ev Event) (EventAction, os.Error) {
|
|
|
|
b := ev.(*Breakpoint);
|
|
|
|
p := b.Process();
|
|
|
|
|
2009-09-03 18:41:25 -06:00
|
|
|
g := b.Goroutine();
|
|
|
|
g.dead = true;
|
2009-09-03 17:59:41 -06:00
|
|
|
|
2009-09-03 18:41:25 -06:00
|
|
|
addr := g.g.addr().base;
|
|
|
|
p.goroutines[addr] = nil, false;
|
2009-09-03 17:59:41 -06:00
|
|
|
|
|
|
|
// Enqueue thread exit event
|
2009-09-03 18:41:25 -06:00
|
|
|
p.postEvent(&GoroutineExit{commonEvent{p, g}});
|
2009-09-03 17:59:41 -06:00
|
|
|
|
2009-09-03 18:41:25 -06:00
|
|
|
// If we just exited our selected goroutine, selected another
|
|
|
|
if p.curGoroutine == g {
|
|
|
|
p.selectSomeGoroutine();
|
2009-09-03 17:59:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return EADefault, nil;
|
|
|
|
}
|