1
0
mirror of https://github.com/golang/go synced 2024-11-14 19:00:27 -07:00
go/usr/austin/ogle/goroutine.go
Austin Clements ffe83e582e Switch ogle over to the in-tree debug/proc package. Fix
debug/proc to install to the right place.  Delete the old
ptrace package.  The diff looks huge, but it's mostly
s/ptrace/proc/.

R=rsc
APPROVED=rsc
DELTA=1940  (10 added, 1835 deleted, 95 changed)
OCL=34966
CL=34968
2009-09-24 09:07:47 -07:00

120 lines
2.6 KiB
Go

// 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 (
"debug/proc";
"eval";
"fmt";
"os";
)
// A Goroutine represents a goroutine in a remote process.
type Goroutine struct {
g remoteStruct;
frame *Frame;
dead bool;
}
func (t *Goroutine) String() string {
if t.dead {
return "<dead thread>";
}
// TODO(austin) Give threads friendly ID's, possibly including
// the name of the entry function.
return fmt.Sprintf("thread %#x", t.g.addr().base);
}
// isG0 returns true if this thread if the internal idle thread
func (t *Goroutine) isG0() bool {
return t.g.addr().base == t.g.r.p.sys.g0.addr().base;
}
func (t *Goroutine) resetFrame() (err os.Error) {
// TODO(austin) Reuse any live part of the current frame stack
// so existing references to Frame's keep working.
t.frame, err = newFrame(t.g);
return;
}
// Out selects the caller frame of the current frame.
func (t *Goroutine) Out() os.Error {
f, err := t.frame.Outer();
if f != nil {
t.frame = f;
}
return err;
}
// In selects the frame called by the current frame.
func (t *Goroutine) In() os.Error {
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 + proc.Word(p.PtrSize());
arg := remotePtr{remote{addr, p}, p.runtime.G};
var gp eval.Value;
err = try(func(a aborter) { gp = arg.aGet(a) });
if err != nil {
return EAStop, err;
}
if gp == nil {
return EAStop, UnknownGoroutine{b.osThread, 0};
}
gs := gp.(remoteStruct);
g := &Goroutine{gs, nil, false};
p.goroutines[gs.addr().base] = g;
// Enqueue goroutine creation event
parent := b.Goroutine();
if parent.isG0() {
parent = nil;
}
p.postEvent(&GoroutineCreate{commonEvent{p, g}, parent});
// If we don't have any thread selected, select this one
if p.curGoroutine == nil {
p.curGoroutine = g;
}
return EADefault, nil;
}
func goexitBP(ev Event) (EventAction, os.Error) {
b := ev.(*Breakpoint);
p := b.Process();
g := b.Goroutine();
g.dead = true;
addr := g.g.addr().base;
p.goroutines[addr] = nil, false;
// Enqueue thread exit event
p.postEvent(&GoroutineExit{commonEvent{p, g}});
// If we just exited our selected goroutine, selected another
if p.curGoroutine == g {
p.selectSomeGoroutine();
}
return EADefault, nil;
}