2009-08-28 19:04:35 -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 (
|
|
|
|
"eval";
|
2009-09-03 17:59:41 -06:00
|
|
|
"fmt";
|
|
|
|
"log";
|
|
|
|
"os";
|
2009-08-28 19:04:35 -06:00
|
|
|
"ptrace";
|
|
|
|
"reflect";
|
|
|
|
"sym";
|
|
|
|
)
|
|
|
|
|
|
|
|
// A FormatError indicates a failure to process information in or
|
|
|
|
// about a remote process, such as unexpected or missing information
|
|
|
|
// in the object file or runtime structures.
|
|
|
|
type FormatError string
|
|
|
|
|
|
|
|
func (e FormatError) String() string {
|
|
|
|
return string(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
// An UnknownArchitecture occurs when trying to load an object file
|
|
|
|
// that indicates an architecture not supported by the debugger.
|
|
|
|
type UnknownArchitecture sym.ElfMachine
|
|
|
|
|
|
|
|
func (e UnknownArchitecture) String() string {
|
|
|
|
return "unknown architecture: " + sym.ElfMachine(e).String();
|
|
|
|
}
|
|
|
|
|
2009-09-01 14:01:37 -06:00
|
|
|
// A ProcessNotStopped error occurs when attempting to read or write
|
|
|
|
// memory or registers of a process that is not stopped.
|
|
|
|
type ProcessNotStopped struct {}
|
|
|
|
|
|
|
|
func (e ProcessNotStopped) String() string {
|
|
|
|
return "process not stopped";
|
|
|
|
}
|
|
|
|
|
2009-09-03 18:41:25 -06:00
|
|
|
// An UnknownGoroutine error is an internal error representing an
|
2009-09-03 17:59:41 -06:00
|
|
|
// unrecognized G structure pointer.
|
2009-09-03 18:41:25 -06:00
|
|
|
type UnknownGoroutine struct {
|
2009-09-03 17:59:41 -06:00
|
|
|
OSThread ptrace.Thread;
|
2009-09-03 18:41:25 -06:00
|
|
|
Goroutine ptrace.Word;
|
2009-09-03 17:59:41 -06:00
|
|
|
}
|
|
|
|
|
2009-09-03 18:41:25 -06:00
|
|
|
func (e UnknownGoroutine) String() string {
|
|
|
|
return fmt.Sprintf("internal error: unknown goroutine (G %#x)", e.Goroutine);
|
2009-09-03 17:59:41 -06:00
|
|
|
}
|
|
|
|
|
2009-09-03 18:41:25 -06:00
|
|
|
// A NoCurrentGoroutine error occurs when no goroutine is currently
|
|
|
|
// selected in a process (or when there are no goroutines in a
|
|
|
|
// process).
|
|
|
|
type NoCurrentGoroutine struct {}
|
2009-09-03 17:59:41 -06:00
|
|
|
|
2009-09-03 18:41:25 -06:00
|
|
|
func (e NoCurrentGoroutine) String() string {
|
|
|
|
return "no current goroutine";
|
2009-09-03 17:59:41 -06:00
|
|
|
}
|
|
|
|
|
2009-08-28 19:04:35 -06:00
|
|
|
// A Process represents a remote attached process.
|
|
|
|
type Process struct {
|
|
|
|
Arch;
|
2009-09-03 17:59:41 -06:00
|
|
|
proc ptrace.Process;
|
2009-08-28 19:04:35 -06:00
|
|
|
|
|
|
|
// The symbol table of this process
|
|
|
|
syms *sym.GoSymTable;
|
|
|
|
|
2009-09-01 14:01:37 -06:00
|
|
|
// A possibly-stopped OS thread, or nil
|
|
|
|
threadCache ptrace.Thread;
|
2009-08-28 19:04:35 -06:00
|
|
|
|
|
|
|
// Types parsed from the remote process
|
|
|
|
types map[ptrace.Word] *remoteType;
|
|
|
|
|
|
|
|
// Types and values from the remote runtime package
|
|
|
|
runtime runtimeValues;
|
|
|
|
|
|
|
|
// Runtime field indexes
|
|
|
|
f runtimeIndexes;
|
2009-09-01 14:01:37 -06:00
|
|
|
|
|
|
|
// Globals from the sys package (or from no package)
|
|
|
|
sys struct {
|
2009-09-03 17:59:41 -06:00
|
|
|
lessstack, goexit, newproc, deferproc, newprocreadylocked *sym.TextSym;
|
|
|
|
allg remotePtr;
|
|
|
|
g0 remoteStruct;
|
2009-09-01 14:01:37 -06:00
|
|
|
};
|
2009-09-03 17:59:41 -06:00
|
|
|
|
|
|
|
// Event queue
|
|
|
|
posted []Event;
|
|
|
|
pending []Event;
|
|
|
|
event Event;
|
|
|
|
|
|
|
|
// Event hooks
|
|
|
|
breakpointHooks map[ptrace.Word] *breakpointHook;
|
2009-09-03 18:41:25 -06:00
|
|
|
goroutineCreateHook *goroutineCreateHook;
|
|
|
|
goroutineExitHook *goroutineExitHook;
|
2009-09-03 17:59:41 -06:00
|
|
|
|
2009-09-03 18:41:25 -06:00
|
|
|
// Current goroutine, or nil if there are no goroutines
|
|
|
|
curGoroutine *Goroutine;
|
2009-09-03 17:59:41 -06:00
|
|
|
|
2009-09-03 18:41:25 -06:00
|
|
|
// Goroutines by the address of their G structure
|
|
|
|
goroutines map[ptrace.Word] *Goroutine;
|
2009-08-28 19:04:35 -06:00
|
|
|
}
|
|
|
|
|
2009-09-03 17:59:41 -06:00
|
|
|
/*
|
|
|
|
* Process creation
|
|
|
|
*/
|
|
|
|
|
2009-08-28 19:04:35 -06:00
|
|
|
// NewProcess constructs a new remote process around a ptrace'd
|
|
|
|
// process, an architecture, and a symbol table.
|
2009-09-03 17:59:41 -06:00
|
|
|
func NewProcess(proc ptrace.Process, arch Arch, syms *sym.GoSymTable) (*Process, os.Error) {
|
2009-08-28 19:04:35 -06:00
|
|
|
p := &Process{
|
|
|
|
Arch: arch,
|
2009-09-03 17:59:41 -06:00
|
|
|
proc: proc,
|
2009-08-28 19:04:35 -06:00
|
|
|
syms: syms,
|
|
|
|
types: make(map[ptrace.Word] *remoteType),
|
2009-09-03 17:59:41 -06:00
|
|
|
breakpointHooks: make(map[ptrace.Word] *breakpointHook),
|
2009-09-03 18:41:25 -06:00
|
|
|
goroutineCreateHook: new(goroutineCreateHook),
|
|
|
|
goroutineExitHook: new(goroutineExitHook),
|
|
|
|
goroutines: make(map[ptrace.Word] *Goroutine),
|
2009-08-28 19:04:35 -06:00
|
|
|
};
|
|
|
|
|
2009-09-03 17:59:41 -06:00
|
|
|
// Fill in remote runtime
|
2009-08-28 19:04:35 -06:00
|
|
|
p.bootstrap();
|
2009-09-03 17:59:41 -06:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case p.sys.allg.addr().base == 0:
|
|
|
|
return nil, FormatError("failed to find runtime symbol 'allg'");
|
|
|
|
case p.sys.g0.addr().base == 0:
|
|
|
|
return nil, FormatError("failed to find runtime symbol 'g0'");
|
|
|
|
case p.sys.newprocreadylocked == nil:
|
|
|
|
return nil, FormatError("failed to find runtime symbol 'newprocreadylocked'");
|
|
|
|
case p.sys.goexit == nil:
|
|
|
|
return nil, FormatError("failed to find runtime symbol 'sys.goexit'");
|
|
|
|
}
|
|
|
|
|
2009-09-03 18:41:25 -06:00
|
|
|
// Get current goroutines
|
|
|
|
p.goroutines[p.sys.g0.addr().base] = &Goroutine{p.sys.g0, nil, false};
|
2009-09-04 12:58:00 -06:00
|
|
|
err := try(func(a aborter) {
|
|
|
|
g := p.sys.allg.aGet(a);
|
|
|
|
for g != nil {
|
|
|
|
gs := g.(remoteStruct);
|
|
|
|
fmt.Printf("*** Found goroutine at %#x\n", gs.addr().base);
|
|
|
|
p.goroutines[gs.addr().base] = &Goroutine{gs, nil, false};
|
|
|
|
g = gs.field(p.f.G.Alllink).(remotePtr).aGet(a);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if err != nil {
|
|
|
|
return nil, err;
|
2009-09-03 17:59:41 -06:00
|
|
|
}
|
|
|
|
|
2009-09-03 18:41:25 -06:00
|
|
|
// Create internal breakpoints to catch new and exited goroutines
|
2009-09-03 17:59:41 -06:00
|
|
|
p.OnBreakpoint(ptrace.Word(p.sys.newprocreadylocked.Entry())).(*breakpointHook).addHandler(readylockedBP, true);
|
|
|
|
p.OnBreakpoint(ptrace.Word(p.sys.goexit.Entry())).(*breakpointHook).addHandler(goexitBP, true);
|
|
|
|
|
2009-09-18 10:11:19 -06:00
|
|
|
// Select current frames
|
|
|
|
for _, g := range p.goroutines {
|
|
|
|
g.resetFrame();
|
|
|
|
}
|
|
|
|
|
|
|
|
p.selectSomeGoroutine();
|
|
|
|
|
2009-09-03 17:59:41 -06:00
|
|
|
return p, nil;
|
2009-08-28 19:04:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewProcessElf constructs a new remote process around a ptrace'd
|
|
|
|
// process and the process' ELF object.
|
|
|
|
func NewProcessElf(proc ptrace.Process, elf *sym.Elf) (*Process, os.Error) {
|
|
|
|
syms, err := sym.ElfGoSyms(elf);
|
|
|
|
if err != nil {
|
|
|
|
return nil, err;
|
|
|
|
}
|
|
|
|
if syms == nil {
|
|
|
|
return nil, FormatError("Failed to find symbol table");
|
|
|
|
}
|
|
|
|
var arch Arch;
|
|
|
|
switch elf.Machine {
|
|
|
|
case sym.ElfX86_64:
|
|
|
|
arch = Amd64;
|
|
|
|
default:
|
|
|
|
return nil, UnknownArchitecture(elf.Machine);
|
|
|
|
}
|
2009-09-03 17:59:41 -06:00
|
|
|
return NewProcess(proc, arch, syms);
|
2009-08-28 19:04:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// bootstrap constructs the runtime structure of a remote process.
|
|
|
|
func (p *Process) bootstrap() {
|
|
|
|
// Manually construct runtime types
|
|
|
|
p.runtime.String = newManualType(eval.TypeOfNative(rt1String{}), p.Arch);
|
|
|
|
p.runtime.Slice = newManualType(eval.TypeOfNative(rt1Slice{}), p.Arch);
|
|
|
|
p.runtime.Eface = newManualType(eval.TypeOfNative(rt1Eface{}), p.Arch);
|
|
|
|
|
|
|
|
p.runtime.Type = newManualType(eval.TypeOfNative(rt1Type{}), p.Arch);
|
|
|
|
p.runtime.CommonType = newManualType(eval.TypeOfNative(rt1CommonType{}), p.Arch);
|
|
|
|
p.runtime.UncommonType = newManualType(eval.TypeOfNative(rt1UncommonType{}), p.Arch);
|
|
|
|
p.runtime.StructField = newManualType(eval.TypeOfNative(rt1StructField{}), p.Arch);
|
|
|
|
p.runtime.StructType = newManualType(eval.TypeOfNative(rt1StructType{}), p.Arch);
|
|
|
|
p.runtime.PtrType = newManualType(eval.TypeOfNative(rt1PtrType{}), p.Arch);
|
|
|
|
p.runtime.ArrayType = newManualType(eval.TypeOfNative(rt1ArrayType{}), p.Arch);
|
|
|
|
p.runtime.SliceType = newManualType(eval.TypeOfNative(rt1SliceType{}), p.Arch);
|
|
|
|
|
|
|
|
p.runtime.Stktop = newManualType(eval.TypeOfNative(rt1Stktop{}), p.Arch);
|
|
|
|
p.runtime.Gobuf = newManualType(eval.TypeOfNative(rt1Gobuf{}), p.Arch);
|
|
|
|
p.runtime.G = newManualType(eval.TypeOfNative(rt1G{}), p.Arch);
|
|
|
|
|
|
|
|
// Get addresses of type·*runtime.XType for discrimination.
|
|
|
|
rtv := reflect.Indirect(reflect.NewValue(&p.runtime)).(*reflect.StructValue);
|
|
|
|
rtvt := rtv.Type().(*reflect.StructType);
|
|
|
|
for i := 0; i < rtv.NumField(); i++ {
|
|
|
|
n := rtvt.Field(i).Name;
|
|
|
|
if n[0] != 'P' || n[1] < 'A' || n[1] > 'Z' {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
sym := p.syms.SymFromName("type·*runtime." + n[1:len(n)]);
|
|
|
|
if sym == nil {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
rtv.Field(i).(*reflect.Uint64Value).Set(sym.Common().Value);
|
|
|
|
}
|
|
|
|
|
2009-09-01 14:01:37 -06:00
|
|
|
// Get runtime field indexes
|
2009-08-28 19:04:35 -06:00
|
|
|
fillRuntimeIndexes(&p.runtime, &p.f);
|
2009-09-01 14:01:37 -06:00
|
|
|
|
|
|
|
// Fill G status
|
|
|
|
p.runtime.runtimeGStatus = rt1GStatus;
|
|
|
|
|
|
|
|
// Get globals
|
|
|
|
globalFn := func(name string) *sym.TextSym {
|
|
|
|
if sym, ok := p.syms.SymFromName(name).(*sym.TextSym); ok {
|
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
};
|
|
|
|
p.sys.lessstack = globalFn("sys·lessstack");
|
|
|
|
p.sys.goexit = globalFn("goexit");
|
|
|
|
p.sys.newproc = globalFn("sys·newproc");
|
|
|
|
p.sys.deferproc = globalFn("sys·deferproc");
|
2009-09-03 17:59:41 -06:00
|
|
|
p.sys.newprocreadylocked = globalFn("newprocreadylocked");
|
|
|
|
if allg := p.syms.SymFromName("allg"); allg != nil {
|
|
|
|
p.sys.allg = remotePtr{remote{ptrace.Word(allg.Common().Value), p}, p.runtime.G};
|
|
|
|
}
|
|
|
|
if g0 := p.syms.SymFromName("g0"); g0 != nil {
|
|
|
|
p.sys.g0 = p.runtime.G.mk(remote{ptrace.Word(g0.Common().Value), p}).(remoteStruct);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-03 18:41:25 -06:00
|
|
|
func (p *Process) selectSomeGoroutine() {
|
|
|
|
// Once we have friendly goroutine ID's, there might be a more
|
2009-09-03 17:59:41 -06:00
|
|
|
// reasonable behavior for this.
|
2009-09-03 18:41:25 -06:00
|
|
|
p.curGoroutine = nil;
|
2009-09-18 10:11:19 -06:00
|
|
|
for _, g := range p.goroutines {
|
|
|
|
if !g.isG0() && g.frame != nil {
|
|
|
|
p.curGoroutine = g;
|
2009-09-03 17:59:41 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2009-09-01 14:01:37 -06:00
|
|
|
}
|
|
|
|
|
2009-09-03 17:59:41 -06:00
|
|
|
/*
|
|
|
|
* Process memory
|
|
|
|
*/
|
|
|
|
|
|
|
|
func (p *Process) someStoppedOSThread() ptrace.Thread {
|
2009-09-01 14:01:37 -06:00
|
|
|
if p.threadCache != nil {
|
|
|
|
if _, err := p.threadCache.Stopped(); err == nil {
|
|
|
|
return p.threadCache;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-03 17:59:41 -06:00
|
|
|
for _, t := range p.proc.Threads() {
|
2009-09-01 14:01:37 -06:00
|
|
|
if _, err := t.Stopped(); err == nil {
|
|
|
|
p.threadCache = t;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) Peek(addr ptrace.Word, out []byte) (int, os.Error) {
|
2009-09-03 17:59:41 -06:00
|
|
|
thr := p.someStoppedOSThread();
|
2009-09-01 14:01:37 -06:00
|
|
|
if thr == nil {
|
|
|
|
return 0, ProcessNotStopped{};
|
|
|
|
}
|
|
|
|
return thr.Peek(addr, out);
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) Poke(addr ptrace.Word, b []byte) (int, os.Error) {
|
2009-09-03 17:59:41 -06:00
|
|
|
thr := p.someStoppedOSThread();
|
2009-09-01 14:01:37 -06:00
|
|
|
if thr == nil {
|
|
|
|
return 0, ProcessNotStopped{};
|
|
|
|
}
|
|
|
|
return thr.Poke(addr, b);
|
|
|
|
}
|
|
|
|
|
2009-09-04 12:58:00 -06:00
|
|
|
func (p *Process) peekUintptr(a aborter, addr ptrace.Word) ptrace.Word {
|
|
|
|
return ptrace.Word(mkUintptr(remote{addr, p}).(remoteUint).aGet(a));
|
2009-08-28 19:04:35 -06:00
|
|
|
}
|
2009-09-03 17:59:41 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Events
|
|
|
|
*/
|
|
|
|
|
|
|
|
// OnBreakpoint returns the hook that is run when the program reaches
|
|
|
|
// the given program counter.
|
|
|
|
func (p *Process) OnBreakpoint(pc ptrace.Word) EventHook {
|
|
|
|
if bp, ok := p.breakpointHooks[pc]; ok {
|
|
|
|
return bp;
|
|
|
|
}
|
|
|
|
// The breakpoint will register itself when a handler is added
|
|
|
|
return &breakpointHook{commonHook{nil, 0}, p, pc};
|
|
|
|
}
|
|
|
|
|
2009-09-03 18:41:25 -06:00
|
|
|
// OnGoroutineCreate returns the hook that is run when a goroutine is created.
|
|
|
|
func (p *Process) OnGoroutineCreate() EventHook {
|
|
|
|
return p.goroutineCreateHook;
|
2009-09-03 17:59:41 -06:00
|
|
|
}
|
|
|
|
|
2009-09-03 18:41:25 -06:00
|
|
|
// OnGoroutineExit returns the hook that is run when a goroutine exits.
|
|
|
|
func (p *Process) OnGoroutineExit() EventHook {
|
|
|
|
return p.goroutineExitHook;
|
2009-09-03 17:59:41 -06:00
|
|
|
}
|
|
|
|
|
2009-09-03 18:41:25 -06:00
|
|
|
// osThreadToGoroutine looks up the goroutine running on an OS thread.
|
|
|
|
func (p *Process) osThreadToGoroutine(t ptrace.Thread) (*Goroutine, os.Error) {
|
2009-09-03 17:59:41 -06:00
|
|
|
regs, err := t.Regs();
|
|
|
|
if err != nil {
|
|
|
|
return nil, err;
|
|
|
|
}
|
|
|
|
g := p.G(regs);
|
2009-09-03 18:41:25 -06:00
|
|
|
gt, ok := p.goroutines[g];
|
2009-09-03 17:59:41 -06:00
|
|
|
if !ok {
|
2009-09-03 18:41:25 -06:00
|
|
|
return nil, UnknownGoroutine{t, g};
|
2009-09-03 17:59:41 -06:00
|
|
|
}
|
|
|
|
return gt, nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
// causesToEvents translates the stop causes of the underlying process
|
|
|
|
// into an event queue.
|
|
|
|
func (p *Process) causesToEvents() ([]Event, os.Error) {
|
|
|
|
// Count causes we're interested in
|
|
|
|
nev := 0;
|
|
|
|
for _, t := range p.proc.Threads() {
|
|
|
|
if c, err := t.Stopped(); err == nil {
|
|
|
|
switch c := c.(type) {
|
|
|
|
case ptrace.Breakpoint:
|
|
|
|
nev++;
|
|
|
|
case ptrace.Signal:
|
|
|
|
// TODO(austin)
|
|
|
|
//nev++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Translate causes to events
|
|
|
|
events := make([]Event, nev);
|
|
|
|
i := 0;
|
|
|
|
for _, t := range p.proc.Threads() {
|
|
|
|
if c, err := t.Stopped(); err == nil {
|
|
|
|
switch c := c.(type) {
|
|
|
|
case ptrace.Breakpoint:
|
2009-09-03 18:41:25 -06:00
|
|
|
gt, err := p.osThreadToGoroutine(t);
|
2009-09-03 17:59:41 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err;
|
|
|
|
}
|
|
|
|
events[i] = &Breakpoint{commonEvent{p, gt}, t, ptrace.Word(c)};
|
|
|
|
i++;
|
|
|
|
case ptrace.Signal:
|
|
|
|
// TODO(austin)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return events, nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
// postEvent appends an event to the posted queue. These events will
|
|
|
|
// be processed before any currently pending events.
|
|
|
|
func (p *Process) postEvent(ev Event) {
|
|
|
|
n := len(p.posted);
|
|
|
|
m := n*2;
|
|
|
|
if m == 0 {
|
|
|
|
m = 4;
|
|
|
|
}
|
|
|
|
posted := make([]Event, n+1, m);
|
|
|
|
for i, p := range p.posted {
|
|
|
|
posted[i] = p;
|
|
|
|
}
|
|
|
|
posted[n] = ev;
|
|
|
|
p.posted = posted;
|
|
|
|
}
|
|
|
|
|
|
|
|
// processEvents processes events in the event queue until no events
|
|
|
|
// remain, a handler returns EAStop, or a handler returns an error.
|
|
|
|
// It returns either EAStop or EAContinue and possibly an error.
|
|
|
|
func (p *Process) processEvents() (EventAction, os.Error) {
|
|
|
|
var ev Event;
|
|
|
|
for len(p.posted) > 0 {
|
|
|
|
ev, p.posted = p.posted[0], p.posted[1:len(p.posted)];
|
|
|
|
action, err := p.processEvent(ev);
|
|
|
|
if action == EAStop {
|
|
|
|
return action, err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for len(p.pending) > 0 {
|
|
|
|
ev, p.pending = p.pending[0], p.pending[1:len(p.pending)];
|
|
|
|
action, err := p.processEvent(ev);
|
|
|
|
if action == EAStop {
|
|
|
|
return action, err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return EAContinue, nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
// processEvent processes a single event, without manipulating the
|
|
|
|
// event queues. It returns either EAStop or EAContinue and possibly
|
|
|
|
// an error.
|
|
|
|
func (p *Process) processEvent(ev Event) (EventAction, os.Error) {
|
|
|
|
p.event = ev;
|
|
|
|
|
|
|
|
var action EventAction;
|
|
|
|
var err os.Error;
|
|
|
|
switch ev := p.event.(type) {
|
|
|
|
case *Breakpoint:
|
|
|
|
hook, ok := p.breakpointHooks[ev.pc];
|
|
|
|
if !ok {
|
|
|
|
break;
|
|
|
|
}
|
2009-09-03 18:41:25 -06:00
|
|
|
p.curGoroutine = ev.Goroutine();
|
2009-09-03 17:59:41 -06:00
|
|
|
action, err = hook.handle(ev);
|
|
|
|
|
2009-09-03 18:41:25 -06:00
|
|
|
case *GoroutineCreate:
|
|
|
|
p.curGoroutine = ev.Goroutine();
|
|
|
|
action, err = p.goroutineCreateHook.handle(ev);
|
2009-09-03 17:59:41 -06:00
|
|
|
|
2009-09-03 18:41:25 -06:00
|
|
|
case *GoroutineExit:
|
|
|
|
action, err = p.goroutineExitHook.handle(ev);
|
2009-09-03 17:59:41 -06:00
|
|
|
|
|
|
|
default:
|
|
|
|
log.Crashf("Unknown event type %T in queue", p.event);
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return EAStop, err;
|
|
|
|
} else if action == EAStop {
|
|
|
|
return EAStop, nil;
|
|
|
|
}
|
|
|
|
return EAContinue, nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Event returns the last event that caused the process to stop. This
|
|
|
|
// may return nil if the process has never been stopped by an event.
|
|
|
|
//
|
|
|
|
// TODO(austin) Return nil if the user calls p.Stop()?
|
|
|
|
func (p *Process) Event() Event {
|
|
|
|
return p.event;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process control
|
|
|
|
*/
|
|
|
|
|
|
|
|
// TODO(austin) Cont, WaitStop, and Stop. Need to figure out how
|
|
|
|
// event handling works with these. Originally I did it only in
|
|
|
|
// WaitStop, but if you Cont and there are pending events, then you
|
|
|
|
// have to not actually continue and wait until a WaitStop to process
|
|
|
|
// them, even if the event handlers will tell you to continue. We
|
|
|
|
// could handle them in both Cont and WaitStop to avoid this problem,
|
|
|
|
// but it's still weird if an event happens after the Cont and before
|
|
|
|
// the WaitStop that the handlers say to continue from. Or we could
|
|
|
|
// handle them on a separate thread. Then obviously you get weird
|
|
|
|
// asynchrony things, like prints while the user it typing a command,
|
|
|
|
// but that's not necessarily a bad thing.
|
|
|
|
|
|
|
|
// ContWait resumes process execution and waits for an event to occur
|
|
|
|
// that stops the process.
|
|
|
|
func (p *Process) ContWait() os.Error {
|
|
|
|
for {
|
|
|
|
a, err := p.processEvents();
|
|
|
|
if err != nil {
|
|
|
|
return err;
|
|
|
|
} else if a == EAStop {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
err = p.proc.Continue();
|
|
|
|
if err != nil {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
err = p.proc.WaitStop();
|
|
|
|
if err != nil {
|
|
|
|
return err;
|
|
|
|
}
|
2009-09-18 10:11:19 -06:00
|
|
|
for _, g := range p.goroutines {
|
|
|
|
g.resetFrame();
|
2009-09-03 17:59:41 -06:00
|
|
|
}
|
|
|
|
p.pending, err = p.causesToEvents();
|
|
|
|
if err != nil {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Out selects the caller frame of the current frame.
|
|
|
|
func (p *Process) Out() os.Error {
|
2009-09-03 18:41:25 -06:00
|
|
|
if p.curGoroutine == nil {
|
|
|
|
return NoCurrentGoroutine{};
|
2009-09-03 17:59:41 -06:00
|
|
|
}
|
2009-09-03 18:41:25 -06:00
|
|
|
return p.curGoroutine.Out();
|
2009-09-03 17:59:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// In selects the frame called by the current frame.
|
|
|
|
func (p *Process) In() os.Error {
|
2009-09-03 18:41:25 -06:00
|
|
|
if p.curGoroutine == nil {
|
|
|
|
return NoCurrentGoroutine{};
|
2009-09-03 17:59:41 -06:00
|
|
|
}
|
2009-09-03 18:41:25 -06:00
|
|
|
return p.curGoroutine.In();
|
2009-09-03 17:59:41 -06:00
|
|
|
}
|