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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
/*
* Mach Operating System
* Copyright (c) 1993,1992 Carnegie Mellon University
* All Rights Reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
*
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
*
* Carnegie Mellon requests users of this software to return to
*
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
*
* any improvements or extensions that they make and grant Carnegie Mellon
* the rights to redistribute these changes.
*/
#include <mach_pcsample.h>
#include <mach/mach_types.h> /* vm_address_t */
#include <mach/std_types.h> /* pointer_t */
#include <mach/pc_sample.h>
#include <kern/host.h>
#include <kern/thread.h>
#include <kern/pc_sample.h>
#if MACH_PCSAMPLE
#define MAX_PC_SAMPLES 512
typedef sampled_pc_t sampled_pcs[MAX_PC_SAMPLES];
int pc_sampling_enabled = 0;
decl_simple_lock_data(, pc_sampling_lock) /* lock for enabling */
void take_pc_sample(
register thread_t t,
register sample_control_t *cp,
sampled_pc_flavor_t flavor)
{
vm_offset_t pc;
struct sampled_pc *sample;
pc = interrupted_pc(t);
cp->seqno++;
sample = &((sampled_pc_t *)cp->buffer)[cp->seqno % MAX_PC_SAMPLES];
sample->id = (natural_t)t;
sample->pc = pc;
sample->sampletype = flavor;
}
kern_return_t
thread_enable_pc_sampling(
thread_t thread,
int *tickp,
sampled_pc_flavor_t flavors)
{
vm_offset_t buf;
extern int tick;
if (thread == THREAD_NULL) {
return KERN_INVALID_ARGUMENT;
}
if (thread->pc_sample.buffer == 0) {
buf = (vm_offset_t) kalloc(sizeof (sampled_pcs));
if (buf == 0) {
printf("thread_enable_pc_sampling: kalloc failed\n");
return KERN_INVALID_ARGUMENT;
}
thread->pc_sample.buffer = buf;
thread->pc_sample.seqno = 0;
}
*tickp = tick;
thread->pc_sample.sampletypes = flavors;
return KERN_SUCCESS;
}
kern_return_t
task_enable_pc_sampling(
task_t task,
int *tickp,
sampled_pc_flavor_t flavors)
{
vm_offset_t buf;
extern int tick;
if (task == TASK_NULL) {
return KERN_INVALID_ARGUMENT;
}
if (task->pc_sample.buffer == 0) {
buf = (vm_offset_t) kalloc(sizeof (sampled_pcs));
if (buf == 0) {
printf("task_enable_pc_sampling: kalloc failed\n");
return KERN_INVALID_ARGUMENT;
}
task->pc_sample.buffer = buf;
task->pc_sample.seqno = 0;
}
*tickp = tick;
task->pc_sample.sampletypes = flavors;
return KERN_SUCCESS;
}
kern_return_t
thread_disable_pc_sampling(
thread_t thread,
int *samplecntp)
{
vm_offset_t buf;
if (thread == THREAD_NULL) {
return KERN_INVALID_ARGUMENT;
}
if ((buf = thread->pc_sample.buffer) != 0)
kfree(buf, sizeof (sampled_pcs));
thread->pc_sample.buffer = (vm_offset_t) 0;
thread->pc_sample.seqno = 0;
thread->pc_sample.sampletypes = 0; /* shut off sampling */
return KERN_SUCCESS;
}
kern_return_t
task_disable_pc_sampling(
task_t task,
int *samplecntp)
{
vm_offset_t buf;
if (task == TASK_NULL) {
return KERN_INVALID_ARGUMENT;
}
if ((buf = task->pc_sample.buffer) != 0)
kfree(buf, sizeof (sampled_pcs));
task->pc_sample.buffer = (vm_offset_t) 0;
task->pc_sample.seqno = 0;
task->pc_sample.sampletypes = 0; /* shut off sampling */
return KERN_SUCCESS;
}
static kern_return_t
get_sampled_pcs(
sample_control_t *cp,
sampled_pc_seqno_t *seqnop,
sampled_pc_array_t sampled_pcs_out,
int *sampled_pcs_cntp)
{
int nsamples;
sampled_pc_seqno_t seqidx1, seqidx2;
nsamples = cp->seqno - *seqnop;
seqidx1 = *seqnop % MAX_PC_SAMPLES; /* index of *seqnop */
seqidx2 = cp->seqno % MAX_PC_SAMPLES; /* index of cp->seqno */
if (nsamples > MAX_PC_SAMPLES) {
nsamples = MAX_PC_SAMPLES;
seqidx1 = (seqidx2 + 1) % MAX_PC_SAMPLES;
}
if (nsamples > 0) {
/*
* Carefully copy sampled_pcs into sampled_pcs_msgbuf IN ORDER.
*/
if (seqidx1 < seqidx2) {
/*
* Simple case: no wraparound.
* Copy from seqidx1 to seqidx2.
*/
bcopy((sampled_pc_array_t)cp->buffer + seqidx1 + 1,
sampled_pcs_out,
nsamples * sizeof(sampled_pc_t));
} else {
/* seqidx1 > seqidx2 -- Handle wraparound. */
bcopy((sampled_pc_array_t)cp->buffer + seqidx1 + 1,
sampled_pcs_out,
(MAX_PC_SAMPLES - seqidx1 - 1) * sizeof(sampled_pc_t));
bcopy((sampled_pc_array_t)cp->buffer,
sampled_pcs_out + (MAX_PC_SAMPLES - seqidx1 - 1),
(seqidx2 + 1) * sizeof(sampled_pc_t));
}
} else {
/* could either be zero because of overflow, or because
* we are being lied to. In either case, return nothing.
* If overflow, only once in a blue moon. If being lied to,
* then we have no obligation to return anything useful anyway.
*/
;
}
*sampled_pcs_cntp = nsamples;
*seqnop = cp->seqno;
return KERN_SUCCESS;
}
kern_return_t
thread_get_sampled_pcs(
thread_t thread,
sampled_pc_seqno_t *seqnop,
sampled_pc_array_t sampled_pcs_out,
int *sampled_pcs_cntp)
{
if (thread == THREAD_NULL)
return KERN_INVALID_ARGUMENT;
if (thread->pc_sample.buffer == 0)
return KERN_FAILURE;
return get_sampled_pcs(&thread->pc_sample, seqnop, sampled_pcs_out,
sampled_pcs_cntp);
}
kern_return_t
task_get_sampled_pcs(
task_t task,
sampled_pc_seqno_t *seqnop,
sampled_pc_array_t sampled_pcs_out,
int *sampled_pcs_cntp)
{
if (task == TASK_NULL)
return KERN_INVALID_ARGUMENT;
if (task->pc_sample.buffer == 0)
return KERN_FAILURE;
return get_sampled_pcs(&task->pc_sample, seqnop, sampled_pcs_out,
sampled_pcs_cntp);
}
#else /* MACH_PCSAMPLE */
kern_return_t
thread_enable_pc_sampling(
thread_t thread,
int *tickp,
sampled_pc_flavor_t flavors)
{
return KERN_FAILURE; /* not implemented */
}
kern_return_t
task_enable_pc_sampling(
task_t task,
int *tickp,
sampled_pc_flavor_t flavors)
{
return KERN_FAILURE; /* not implemented */
}
kern_return_t
thread_disable_pc_sampling(
thread_t thread,
int *samplecntp)
{
return KERN_FAILURE; /* not implemented */
}
kern_return_t
task_disable_pc_sampling(
task_t task,
int *samplecntp)
{
return KERN_FAILURE; /* not implemented */
}
kern_return_t
thread_get_sampled_pcs(
thread_t thread,
sampled_pc_seqno_t *seqnop,
sampled_pc_array_t sampled_pcs_out,
int *sampled_pcs_cntp)
{
return KERN_FAILURE; /* not implemented */
}
kern_return_t
task_get_sampled_pcs(
task_t task,
sampled_pc_seqno_t *seqnop,
sampled_pc_array_t sampled_pcs_out,
int *sampled_pcs_cntp)
{
return KERN_FAILURE; /* not implemented */
}
#endif /* MACH_PCSAMPLE */
|