]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_pcsamples.c
xnu-792.6.56.tar.gz
[apple/xnu.git] / bsd / kern / kern_pcsamples.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <sys/kdebug.h>
25 #include <sys/errno.h>
26 #include <sys/param.h>
27 #include <sys/proc_internal.h>
28 #include <sys/vm.h>
29 #include <sys/sysctl.h>
30 #include <sys/systm.h>
31 #include <vm/vm_kern.h>
32 #include <machine/machine_routines.h>
33
34 vm_offset_t pc_buftomem = 0;
35 unsigned int * pc_buffer = 0; /* buffer that holds each pc */
36 unsigned int * pc_bufptr = 0;
37 unsigned int * pc_buflast = 0;
38 unsigned int npcbufs = 8192; /* number of pc entries in buffer */
39 unsigned int pc_bufsize = 0;
40 unsigned int pcsample_flags = 0;
41 unsigned int pcsample_enable = 0;
42
43 pid_t pc_sample_pid = 0;
44 boolean_t pc_trace_frameworks = FALSE;
45
46 char pcsample_comm[MAXCOMLEN + 1];
47
48 /* Set the default framework boundaries */
49 unsigned int pcsample_beg = 0;
50 unsigned int pcsample_end = 0;
51
52 static pid_t global_state_pid = -1; /* Used to control exclusive use of pc_buffer */
53
54 extern int pc_trace_buf[];
55 extern int pc_trace_cnt;
56
57 void add_pcbuffer(void);
58 int branch_tracing_enabled(void);
59 int disable_branch_tracing(void);
60 int enable_branch_tracing(void);
61 int pcsamples_bootstrap(void);
62 void pcsamples_clear(void);
63 int pcsamples_control(int *name, u_int namelen, user_addr_t where, size_t *sizep);
64 int pcsamples_read(user_addr_t buffer, size_t *number);
65 int pcsamples_reinit(void);
66
67 int
68 enable_branch_tracing(void)
69 {
70 #ifndef i386
71 struct proc *p;
72 if (-1 != pc_sample_pid) {
73 p = pfind(pc_sample_pid);
74 if (p) {
75 p->p_flag |= P_BTRACE;
76 }
77 }
78 else {
79 pc_trace_frameworks = TRUE;
80 }
81
82 return 1;
83
84 #else
85 return 0;
86 #endif
87 }
88
89 int
90 disable_branch_tracing(void)
91 {
92 struct proc *p;
93 switch (pc_sample_pid) {
94 case -1:
95 pc_trace_frameworks = FALSE;
96 break;
97 case 0:
98 break;
99 default:
100 p = pfind(pc_sample_pid);
101 if (p) {
102 p->p_flag &= ~P_BTRACE;
103 }
104 break;
105 }
106 clr_be_bit();
107 return 1;
108 }
109
110 /*
111 * this only works for the current proc as it
112 * is called from context_switch in the scheduler
113 */
114 int
115 branch_tracing_enabled(void)
116 {
117 struct proc *p = current_proc();
118 if (TRUE == pc_trace_frameworks) return TRUE;
119 if (p) {
120 return (P_BTRACE == (p->p_flag & P_BTRACE));
121 }
122 return 0;
123 }
124
125
126 void
127 add_pcbuffer(void)
128 {
129 int i;
130 unsigned int pc;
131
132 if (!pcsample_enable)
133 return;
134
135 for (i=0; i < pc_trace_cnt; i++)
136 {
137 pc = pc_trace_buf[i];
138
139 if ((pcsample_beg <= pc) && (pc < pcsample_end))
140 {
141 if (pc_bufptr > pc_buffer)
142 {
143 if ( (*(pc_bufptr-1)) == pc )
144 continue; /* Ignore, probably spinning */
145 }
146
147 /* Then the sample is in our range */
148 *pc_bufptr = pc;
149 pc_bufptr++;
150 }
151 }
152
153 /* We never wrap the buffer */
154 if ((pc_bufptr + pc_trace_cnt) >= pc_buflast)
155 {
156 pcsample_enable = 0;
157 (void)disable_branch_tracing();
158 wakeup(&pcsample_enable);
159 }
160 return;
161 }
162
163 int
164 pcsamples_bootstrap(void)
165 {
166 if (!disable_branch_tracing())
167 return(ENOTSUP);
168
169 pc_bufsize = npcbufs * sizeof(* pc_buffer);
170 if (kmem_alloc(kernel_map, &pc_buftomem,
171 (vm_size_t)pc_bufsize) == KERN_SUCCESS)
172 pc_buffer = (unsigned int *) pc_buftomem;
173 else
174 pc_buffer = NULL;
175
176 if (pc_buffer) {
177 pc_bufptr = pc_buffer;
178 pc_buflast = &pc_bufptr[npcbufs];
179 pcsample_enable = 0;
180 return(0);
181 } else {
182 pc_bufsize=0;
183 return(EINVAL);
184 }
185
186 }
187
188 int
189 pcsamples_reinit(void)
190 {
191 int ret=0;
192
193 pcsample_enable = 0;
194
195 if (pc_bufsize && pc_buffer)
196 kmem_free(kernel_map, (vm_offset_t)pc_buffer, pc_bufsize);
197
198 ret= pcsamples_bootstrap();
199 return(ret);
200 }
201
202 void
203 pcsamples_clear(void)
204 {
205 /* Clean up the sample buffer, set defaults */
206 global_state_pid = -1;
207 pcsample_enable = 0;
208 if(pc_bufsize && pc_buffer)
209 kmem_free(kernel_map, (vm_offset_t)pc_buffer, pc_bufsize);
210 pc_buffer = NULL;
211 pc_bufptr = NULL;
212 pc_buflast = NULL;
213 pc_bufsize = 0;
214 pcsample_beg= 0;
215 pcsample_end= 0;
216 bzero((void *)pcsample_comm, sizeof(pcsample_comm));
217 (void)disable_branch_tracing();
218 pc_sample_pid = 0;
219 pc_trace_frameworks = FALSE;
220 }
221
222 int
223 pcsamples_control(int *name, __unused u_int namelen, user_addr_t where, size_t *sizep)
224 {
225 int ret=0;
226 size_t size=*sizep;
227 int value = name[1];
228 pcinfo_t pc_bufinfo;
229 pid_t *pidcheck;
230
231 pid_t curpid;
232 struct proc *p, *curproc;
233
234 if (name[0] != PCSAMPLE_GETNUMBUF)
235 {
236 curproc = current_proc();
237 if (curproc)
238 curpid = curproc->p_pid;
239 else
240 return (ESRCH);
241
242 if (global_state_pid == -1)
243 global_state_pid = curpid;
244 else if (global_state_pid != curpid)
245 {
246 if((p = pfind(global_state_pid)) == NULL)
247 {
248 /* The global pid no longer exists */
249 global_state_pid = curpid;
250 }
251 else
252 {
253 /* The global pid exists, deny this request */
254 return(EBUSY);
255 }
256 }
257 }
258
259
260 switch(name[0]) {
261 case PCSAMPLE_DISABLE: /* used to disable */
262 pcsample_enable=0;
263 break;
264 case PCSAMPLE_SETNUMBUF:
265 /* The buffer size is bounded by a min and max number of samples */
266 if (value < pc_trace_cnt) {
267 ret=EINVAL;
268 break;
269 }
270 if (value <= MAX_PCSAMPLES)
271 /* npcbufs = value & ~(PC_TRACE_CNT-1); */
272 npcbufs = value;
273 else
274 npcbufs = MAX_PCSAMPLES;
275 break;
276 case PCSAMPLE_GETNUMBUF:
277 if (size < sizeof(pc_bufinfo)) {
278 ret=EINVAL;
279 break;
280 }
281 pc_bufinfo.npcbufs = npcbufs;
282 pc_bufinfo.bufsize = pc_bufsize;
283 pc_bufinfo.enable = pcsample_enable;
284 pc_bufinfo.pcsample_beg = pcsample_beg;
285 pc_bufinfo.pcsample_end = pcsample_end;
286 if(copyout (&pc_bufinfo, where, sizeof(pc_bufinfo)))
287 {
288 ret=EINVAL;
289 }
290 break;
291 case PCSAMPLE_SETUP:
292 ret=pcsamples_reinit();
293 break;
294 case PCSAMPLE_REMOVE:
295 pcsamples_clear();
296 break;
297 case PCSAMPLE_READBUF:
298 /* A nonzero value says enable and wait on the buffer */
299 /* A zero value says read up the buffer immediately */
300 if (value == 0)
301 {
302 /* Do not wait on the buffer */
303 pcsample_enable = 0;
304 (void)disable_branch_tracing();
305 ret = pcsamples_read(where, sizep);
306 break;
307 }
308 else if ((pc_bufsize <= 0) || (!pc_buffer))
309 {
310 /* enable only if buffer is initialized */
311 ret=EINVAL;
312 break;
313 }
314
315 /* Turn on branch tracing */
316 if (!enable_branch_tracing())
317 {
318 ret = ENOTSUP;
319 break;
320 }
321
322 /* Enable sampling */
323 pcsample_enable = 1;
324
325 ret = tsleep(&pcsample_enable, PRIBIO | PCATCH, "pcsample", 0);
326 pcsample_enable = 0;
327 (void)disable_branch_tracing();
328
329 if (ret)
330 {
331 /* Eventually fix this... if (ret != EINTR) */
332 if (ret)
333 {
334 /* On errors, except EINTR, we want to cleanup buffer ptrs */
335 /* pc_bufptr = pc_buffer; */
336 *sizep = 0;
337 }
338 }
339 else
340 {
341 /* The only way to get here is if the buffer is full */
342 ret = pcsamples_read(where, sizep);
343 }
344
345 break;
346 case PCSAMPLE_SETREG:
347 if (size < sizeof(pc_bufinfo))
348 {
349 ret = EINVAL;
350 break;
351 }
352 if (copyin(where, &pc_bufinfo, sizeof(pc_bufinfo)))
353 {
354 ret = EINVAL;
355 break;
356 }
357
358 pcsample_beg = pc_bufinfo.pcsample_beg;
359 pcsample_end = pc_bufinfo.pcsample_end;
360 break;
361 case PCSAMPLE_COMM:
362 if (!(sizeof(pcsample_comm) > size))
363 {
364 ret = EINVAL;
365 break;
366 }
367 bzero((void *)pcsample_comm, sizeof(pcsample_comm));
368 if (copyin(where, pcsample_comm, size))
369 {
370 ret = EINVAL;
371 break;
372 }
373
374 /* Check for command name or pid */
375 if (pcsample_comm[0] != '\0')
376 {
377 ret= ENOTSUP;
378 break;
379 }
380 else
381 {
382 if (size != (2 * sizeof(pid_t)))
383 {
384 ret = EINVAL;
385 break;
386 }
387 else
388 {
389 pidcheck = (pid_t *)pcsample_comm;
390 pc_sample_pid = pidcheck[1];
391 }
392 }
393 break;
394 default:
395 ret= ENOTSUP;
396 break;
397 }
398 return(ret);
399 }
400
401
402 /*
403 This buffer must be read up in one call.
404 If the buffer isn't big enough to hold
405 all the samples, it will copy up enough
406 to fill the buffer and throw the rest away.
407 This buffer never wraps.
408 */
409 int
410 pcsamples_read(user_addr_t buffer, size_t *number)
411 {
412 size_t count=0;
413 size_t copycount;
414
415 count = (*number)/sizeof(* pc_buffer);
416
417 if (count && pc_bufsize && pc_buffer)
418 {
419 copycount = pc_bufptr - pc_buffer;
420
421 if (copycount <= 0)
422 {
423 *number = 0;
424 return(0);
425 }
426
427 if (copycount > count)
428 copycount = count;
429
430 /* We actually have data to send up */
431 if(copyout(pc_buffer, buffer, copycount * sizeof(* pc_buffer)))
432 {
433 *number = 0;
434 return(EINVAL);
435 }
436 *number = copycount;
437 pc_bufptr = pc_buffer;
438 return(0);
439 }
440 else
441 {
442 *number = 0;
443 return(0);
444 }
445 }
446