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