summaryrefslogtreecommitdiff
path: root/usr/austin/ogle/thread.go
blob: 888d01eff6cd3e0b01de54aa2b4d2fa481815177 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// 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";
)

// A Thread represents a Go thread.
type Thread struct {
	g remoteStruct;
	frame *Frame;
	dead bool;
}

func (t *Thread) String() string {
	if t.dead {
		return "<dead thread>";
	}
	// TODO(austin) Give threads friendly ID's
	return fmt.Sprintf("thread %#x", t.g.addr().base);
}

// isG0 returns true if this thread if the internal idle thread
func (t *Thread) isG0() bool {
	return t.g.addr().base == t.g.r.p.sys.g0.addr().base;
}

func (t *Thread) resetFrame() {
	// 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.
func (t *Thread) Out() os.Error {
	// 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.
func (t *Thread) 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 + ptrace.Word(p.PtrSize());
	arg := remotePtr{remote{addr, p}, p.runtime.G};
	g := arg.Get();
	if g == nil {
		return EAStop, UnknownThread{b.osThread, 0};
	}
	gs := g.(remoteStruct);
	t := &Thread{gs, nil, false};
	p.threads[gs.addr().base] = t;

	// Enqueue thread creation event
	parent := b.Thread();
	if parent.isG0() {
		parent = nil;
	}
	p.postEvent(&ThreadCreate{commonEvent{p, t}, parent});

	// If we don't have any thread selected, select this one
	if p.curThread == nil {
		p.curThread = t;
	}

	return EADefault, nil;
}

func goexitBP(ev Event) (EventAction, os.Error) {
	b := ev.(*Breakpoint);
	p := b.Process();

	t := b.Thread();
	t.dead = true;

	addr := t.g.addr().base;
	p.threads[addr] = nil, false;

	// Enqueue thread exit event
	p.postEvent(&ThreadExit{commonEvent{p, t}});

	// If we just exited our selected thread, selected another
	if p.curThread == t {
		p.selectSomeThread();
	}

	return EADefault, nil;
}