summaryrefslogtreecommitdiff
path: root/libgo/go/exp/ogle/event.go
blob: d7092ded336db99ff9f58dec39addb53f3414f6a (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// 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"
	"fmt"
	"os"
)

/*
 * Hooks and events
 */

// An EventHandler is a function that takes an event and returns a
// response to that event and possibly an error.  If an event handler
// returns an error, the process stops and no other handlers for that
// event are executed.
type EventHandler func(e Event) (EventAction, os.Error)

// An EventAction is an event handler's response to an event.  If all
// of an event's handlers execute without returning errors, their
// results are combined as follows: If any handler returned
// EAContinue, then the process resumes (without returning from
// WaitStop); otherwise, if any handler returned EAStop, the process
// remains stopped; otherwise, if all handlers returned EADefault, the
// process resumes.  A handler may return EARemoveSelf bit-wise or'd
// with any other action to indicate that the handler should be
// removed from the hook.
type EventAction int

const (
	EARemoveSelf EventAction = 0x100
	EADefault    EventAction = iota
	EAStop
	EAContinue
)

// A EventHook allows event handlers to be added and removed.
type EventHook interface {
	AddHandler(EventHandler)
	RemoveHandler(EventHandler)
	NumHandler() int
	handle(e Event) (EventAction, os.Error)
	String() string
}

// EventHook is almost, but not quite, suitable for user-defined
// events.  If we want user-defined events, make EventHook a struct,
// special-case adding and removing handlers in breakpoint hooks, and
// provide a public interface for posting events to hooks.

type Event interface {
	Process() *Process
	Goroutine() *Goroutine
	String() string
}

type commonHook struct {
	// Head of handler chain
	head *handler
	// Number of non-internal handlers
	len int
}

type handler struct {
	eh EventHandler
	// True if this handler must be run before user-defined
	// handlers in order to ensure correctness.
	internal bool
	// True if this handler has been removed from the chain.
	removed bool
	next    *handler
}

func (h *commonHook) AddHandler(eh EventHandler) {
	h.addHandler(eh, false)
}

func (h *commonHook) addHandler(eh EventHandler, internal bool) {
	// Ensure uniqueness of handlers
	h.RemoveHandler(eh)

	if !internal {
		h.len++
	}
	// Add internal handlers to the beginning
	if internal || h.head == nil {
		h.head = &handler{eh, internal, false, h.head}
		return
	}
	// Add handler after internal handlers
	// TODO(austin) This should probably go on the end instead
	prev := h.head
	for prev.next != nil && prev.internal {
		prev = prev.next
	}
	prev.next = &handler{eh, internal, false, prev.next}
}

func (h *commonHook) RemoveHandler(eh EventHandler) {
	plink := &h.head
	for l := *plink; l != nil; plink, l = &l.next, l.next {
		if l.eh == eh {
			if !l.internal {
				h.len--
			}
			l.removed = true
			*plink = l.next
			break
		}
	}
}

func (h *commonHook) NumHandler() int { return h.len }

func (h *commonHook) handle(e Event) (EventAction, os.Error) {
	action := EADefault
	plink := &h.head
	for l := *plink; l != nil; plink, l = &l.next, l.next {
		if l.removed {
			continue
		}
		a, err := l.eh(e)
		if a&EARemoveSelf == EARemoveSelf {
			if !l.internal {
				h.len--
			}
			l.removed = true
			*plink = l.next
			a &^= EARemoveSelf
		}
		if err != nil {
			return EAStop, err
		}
		if a > action {
			action = a
		}
	}
	return action, nil
}

type commonEvent struct {
	// The process of this event
	p *Process
	// The goroutine of this event.
	t *Goroutine
}

func (e *commonEvent) Process() *Process { return e.p }

func (e *commonEvent) Goroutine() *Goroutine { return e.t }

/*
 * Standard event handlers
 */

// EventPrint is a standard event handler that prints events as they
// occur.  It will not cause the process to stop.
func EventPrint(ev Event) (EventAction, os.Error) {
	// TODO(austin) Include process name here?
	fmt.Fprintf(os.Stderr, "*** %v\n", ev.String())
	return EADefault, nil
}

// EventStop is a standard event handler that causes the process to stop.
func EventStop(ev Event) (EventAction, os.Error) {
	return EAStop, nil
}

/*
 * Breakpoints
 */

type breakpointHook struct {
	commonHook
	p  *Process
	pc proc.Word
}

// A Breakpoint event occurs when a process reaches a particular
// program counter.  When this event is handled, the current goroutine
// will be the goroutine that reached the program counter.
type Breakpoint struct {
	commonEvent
	osThread proc.Thread
	pc       proc.Word
}

func (h *breakpointHook) AddHandler(eh EventHandler) {
	h.addHandler(eh, false)
}

func (h *breakpointHook) addHandler(eh EventHandler, internal bool) {
	// We register breakpoint events lazily to avoid holding
	// references to breakpoints without handlers.  Be sure to use
	// the "canonical" breakpoint if there is one.
	if cur, ok := h.p.breakpointHooks[h.pc]; ok {
		h = cur
	}
	oldhead := h.head
	h.commonHook.addHandler(eh, internal)
	if oldhead == nil && h.head != nil {
		h.p.proc.AddBreakpoint(h.pc)
		h.p.breakpointHooks[h.pc] = h
	}
}

func (h *breakpointHook) RemoveHandler(eh EventHandler) {
	oldhead := h.head
	h.commonHook.RemoveHandler(eh)
	if oldhead != nil && h.head == nil {
		h.p.proc.RemoveBreakpoint(h.pc)
		h.p.breakpointHooks[h.pc] = nil, false
	}
}

func (h *breakpointHook) String() string {
	// TODO(austin) Include process name?
	// TODO(austin) Use line:pc or at least sym+%#x
	return fmt.Sprintf("breakpoint at %#x", h.pc)
}

func (b *Breakpoint) PC() proc.Word { return b.pc }

func (b *Breakpoint) String() string {
	// TODO(austin) Include process name and goroutine
	// TODO(austin) Use line:pc or at least sym+%#x
	return fmt.Sprintf("breakpoint at %#x", b.pc)
}

/*
 * Goroutine create/exit
 */

type goroutineCreateHook struct {
	commonHook
}

func (h *goroutineCreateHook) String() string { return "goroutine create" }

// A GoroutineCreate event occurs when a process creates a new
// goroutine.  When this event is handled, the current goroutine will
// be the newly created goroutine.
type GoroutineCreate struct {
	commonEvent
	parent *Goroutine
}

// Parent returns the goroutine that created this goroutine.  May be
// nil if this event is the creation of the first goroutine.
func (e *GoroutineCreate) Parent() *Goroutine { return e.parent }

func (e *GoroutineCreate) String() string {
	// TODO(austin) Include process name
	if e.parent == nil {
		return fmt.Sprintf("%v created", e.t)
	}
	return fmt.Sprintf("%v created by %v", e.t, e.parent)
}

type goroutineExitHook struct {
	commonHook
}

func (h *goroutineExitHook) String() string { return "goroutine exit" }

// A GoroutineExit event occurs when a Go goroutine exits.
type GoroutineExit struct {
	commonEvent
}

func (e *GoroutineExit) String() string {
	// TODO(austin) Include process name
	//return fmt.Sprintf("%v exited", e.t);
	// For debugging purposes
	return fmt.Sprintf("goroutine %#x exited", e.t.g.addr().base)
}