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
|
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "portfns.h"
#include "ureg.h"
#include "../port/error.h"
//
// These bits used to be in port/devdbg but were removed in
// order to allow for using hardware debug features on certain
// architectures
//
extern void breakset(Bkpt *b);
extern void breakrestore(Bkpt *b);
extern Bkpt* breakclear(int id);
extern void breaknotify(Bkpt *b, Proc *p);
extern int breakmatch(BkptCond *cond, Ureg *ur, Proc *p);
void skipfree(Bkpt *b);
Bkpt*newskip(ulong addr, Bkpt *skipb, Proc *skipp);
Bkpt *skipalloc;
extern Bkpt *breakpoints;
typedef struct SkipArg SkipArg;
struct SkipArg
{
Bkpt *b;
Proc *p;
};
void
skiphandler(Bkpt *b)
{
SkipArg *a = b->aux;
Bkpt *l;
if(breakclear(b->id) == nil)
panic("skiphandler: breakclear() failed");
breakrestore(a->b);
l = a->b->link;
while(l != nil) {
breakrestore(l);
l = l->link;
}
skipfree(b);
a->p->dbgstop = 0; // Whoo!
if(a->p->state == Stopped)
ready(a->p);
}
Bkpt*
newskip(ulong addr, Bkpt *skipb, Proc *skipp)
{
Bkpt *b;
SkipArg *a;
b = skipalloc;
if(b == nil)
panic("newskip(): no free skips\n");
skipalloc = b->next;
b->addr = addr;
b->conditions->val = addr;
b->link = nil;
a = b->aux;
a->b = skipb;
a->p = skipp;
return b;
}
void
skipfree(Bkpt *b)
{
b->next = skipalloc;
skipalloc = b;
}
//
// Called from the exception handler when a breakpoint instruction has been
// hit. This cannot not be called unless at least one breakpoint with this
// address is in the list of breakpoints. (All breakpoint notifications must
// previously have been set via setbreak())
//
// foreach breakpoint in list
// if breakpoint matches conditions
// notify the break handler
// if no breakpoints matched the conditions
// pick a random breakpoint set to this address
//
// set a breakpoint at the next instruction to be executed,
// and pass the current breakpoint to the "skiphandler"
//
// clear the current breakpoint
//
// Tell the scheduler to stop scheduling, so the caller is
// guaranteed to execute the instruction, followed by the
// added breakpoint.
//
//
int
breakhit(Ureg *ur, Proc *p)
{
Bkpt *b;
int nmatched;
Bkpt *skip;
nmatched = 0;
for(b = breakpoints; b != nil; b = b->next) {
if(breakmatch(b->conditions, ur, p)) {
breaknotify(b, p);
++nmatched;
}
}
if(nmatched)
return BrkSched;
skip = nil;
for(b = breakpoints; b != nil; b = b->next) {
if(b->addr == ur->pc) {
if(breakclear(b->id) == nil)
panic("breakhit: breakclear() failed");
if(skip == nil)
skip = newskip(machnextaddr(ur), b, p);
else {
b->link = skip->link;
skip->link = b;
}
}
}
if(skip == nil)
return BrkSched;
breakset(skip);
return BrkNoSched;
}
void
portbreakinit(void)
{
Bkpt *b;
int i;
skipalloc = mallocz(conf.nproc*(sizeof(Bkpt)+sizeof(BkptCond)+sizeof(SkipArg)), 1);
if(skipalloc == nil)
error(Enomem);
b = skipalloc;
for(i=0; i < conf.nproc-1; i++) {
b->id = -(i+1);
b->conditions = (BkptCond*)((uchar*)b + sizeof(Bkpt));
b->conditions->op = 'b';
b->handler = skiphandler;
b->aux = (SkipArg*)((uchar*)b+sizeof(Bkpt)+sizeof(BkptCond));
b->next = (Bkpt*)((uchar*)b+sizeof(Bkpt)+sizeof(BkptCond)+sizeof(SkipArg));
b = b->next;
}
b->next = nil;
}
|