2 * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <sys/kdebug.h>
30 #include <sys/errno.h>
31 #include <sys/param.h>
32 #include <sys/proc_internal.h>
34 #include <sys/sysctl.h>
35 #include <sys/systm.h>
36 #include <vm/vm_kern.h>
37 #include <machine/machine_routines.h>
39 vm_offset_t pc_buftomem
= 0;
40 unsigned int * pc_buffer
= 0; /* buffer that holds each pc */
41 unsigned int * pc_bufptr
= 0;
42 unsigned int * pc_buflast
= 0;
43 unsigned int npcbufs
= 8192; /* number of pc entries in buffer */
44 unsigned int pc_bufsize
= 0;
45 unsigned int pcsample_flags
= 0;
46 unsigned int pcsample_enable
= 0;
48 pid_t pc_sample_pid
= 0;
49 boolean_t pc_trace_frameworks
= FALSE
;
51 char pcsample_comm
[MAXCOMLEN
+ 1];
53 /* Set the default framework boundaries */
54 unsigned int pcsample_beg
= 0;
55 unsigned int pcsample_end
= 0;
57 static pid_t global_state_pid
= -1; /* Used to control exclusive use of pc_buffer */
59 extern unsigned int pc_trace_buf
[];
60 extern int pc_trace_cnt
;
62 void add_pcbuffer(void);
63 int branch_tracing_enabled(void);
64 int disable_branch_tracing(void);
65 int enable_branch_tracing(void);
66 int pcsamples_bootstrap(void);
67 void pcsamples_clear(void);
68 int pcsamples_control(int *name
, u_int namelen
, user_addr_t where
, size_t *sizep
);
69 int pcsamples_read(user_addr_t buffer
, size_t *number
);
70 int pcsamples_reinit(void);
73 enable_branch_tracing(void)
76 if (-1 != pc_sample_pid
) {
77 p
= proc_find(pc_sample_pid
);
84 pc_trace_frameworks
= TRUE
;
92 disable_branch_tracing(void)
95 switch (pc_sample_pid
) {
97 pc_trace_frameworks
= FALSE
;
102 p
= proc_find(pc_sample_pid
);
114 * this only works for the current proc as it
115 * is called from context_switch in the scheduler
118 branch_tracing_enabled(void)
120 struct proc
*p
= current_proc();
121 if (TRUE
== pc_trace_frameworks
) return TRUE
;
123 return (p
->p_btrace
);
135 if (!pcsample_enable
)
138 for (i
=0; i
< pc_trace_cnt
; i
++)
140 pc
= pc_trace_buf
[i
];
142 if ((pcsample_beg
<= pc
) && (pc
< pcsample_end
))
144 if (pc_bufptr
> pc_buffer
)
146 if ( (*(pc_bufptr
-1)) == pc
)
147 continue; /* Ignore, probably spinning */
150 /* Then the sample is in our range */
156 /* We never wrap the buffer */
157 if ((pc_bufptr
+ pc_trace_cnt
) >= pc_buflast
)
160 (void)disable_branch_tracing();
161 wakeup(&pcsample_enable
);
167 pcsamples_bootstrap(void)
169 if (!disable_branch_tracing())
172 pc_bufsize
= npcbufs
* sizeof(* pc_buffer
);
173 if (kmem_alloc(kernel_map
, &pc_buftomem
,
174 (vm_size_t
)pc_bufsize
) == KERN_SUCCESS
)
175 pc_buffer
= (unsigned int *) pc_buftomem
;
180 pc_bufptr
= pc_buffer
;
181 pc_buflast
= &pc_bufptr
[npcbufs
];
192 pcsamples_reinit(void)
198 if (pc_bufsize
&& pc_buffer
)
199 kmem_free(kernel_map
, (vm_offset_t
)pc_buffer
, pc_bufsize
);
201 ret
= pcsamples_bootstrap();
206 pcsamples_clear(void)
208 /* Clean up the sample buffer, set defaults */
209 global_state_pid
= -1;
211 if(pc_bufsize
&& pc_buffer
)
212 kmem_free(kernel_map
, (vm_offset_t
)pc_buffer
, pc_bufsize
);
219 bzero((void *)pcsample_comm
, sizeof(pcsample_comm
));
220 (void)disable_branch_tracing();
222 pc_trace_frameworks
= FALSE
;
226 pcsamples_control(int *name
, __unused u_int namelen
, user_addr_t where
, size_t *sizep
)
235 struct proc
*p
, *curproc
;
237 if (name
[0] != PCSAMPLE_GETNUMBUF
)
239 curproc
= current_proc();
241 curpid
= curproc
->p_pid
;
245 if (global_state_pid
== -1)
246 global_state_pid
= curpid
;
247 else if (global_state_pid
!= curpid
)
249 if((p
= proc_find(global_state_pid
)) == NULL
)
251 /* The global pid no longer exists */
252 global_state_pid
= curpid
;
257 /* The global pid exists, deny this request */
265 case PCSAMPLE_DISABLE
: /* used to disable */
268 case PCSAMPLE_SETNUMBUF
:
269 /* The buffer size is bounded by a min and max number of samples */
270 if (value
< pc_trace_cnt
) {
274 if (value
<= MAX_PCSAMPLES
)
275 /* npcbufs = value & ~(PC_TRACE_CNT-1); */
278 npcbufs
= MAX_PCSAMPLES
;
280 case PCSAMPLE_GETNUMBUF
:
281 if (size
< sizeof(pc_bufinfo
)) {
285 pc_bufinfo
.npcbufs
= npcbufs
;
286 pc_bufinfo
.bufsize
= pc_bufsize
;
287 pc_bufinfo
.enable
= pcsample_enable
;
288 pc_bufinfo
.pcsample_beg
= pcsample_beg
;
289 pc_bufinfo
.pcsample_end
= pcsample_end
;
290 if(copyout (&pc_bufinfo
, where
, sizeof(pc_bufinfo
)))
296 ret
=pcsamples_reinit();
298 case PCSAMPLE_REMOVE
:
301 case PCSAMPLE_READBUF
:
302 /* A nonzero value says enable and wait on the buffer */
303 /* A zero value says read up the buffer immediately */
306 /* Do not wait on the buffer */
308 (void)disable_branch_tracing();
309 ret
= pcsamples_read(where
, sizep
);
312 else if ((pc_bufsize
<= 0) || (!pc_buffer
))
314 /* enable only if buffer is initialized */
319 /* Turn on branch tracing */
320 if (!enable_branch_tracing())
326 /* Enable sampling */
329 ret
= tsleep(&pcsample_enable
, PRIBIO
| PCATCH
, "pcsample", 0);
331 (void)disable_branch_tracing();
335 /* Eventually fix this... if (ret != EINTR) */
338 /* On errors, except EINTR, we want to cleanup buffer ptrs */
339 /* pc_bufptr = pc_buffer; */
345 /* The only way to get here is if the buffer is full */
346 ret
= pcsamples_read(where
, sizep
);
350 case PCSAMPLE_SETREG
:
351 if (size
< sizeof(pc_bufinfo
))
356 if (copyin(where
, &pc_bufinfo
, sizeof(pc_bufinfo
)))
362 pcsample_beg
= pc_bufinfo
.pcsample_beg
;
363 pcsample_end
= pc_bufinfo
.pcsample_end
;
366 if (!(sizeof(pcsample_comm
) > size
))
371 bzero((void *)pcsample_comm
, sizeof(pcsample_comm
));
372 if (copyin(where
, pcsample_comm
, size
))
378 /* Check for command name or pid */
379 if (pcsample_comm
[0] != '\0')
386 if (size
!= (2 * sizeof(pid_t
)))
393 pidcheck
= (pid_t
*)pcsample_comm
;
394 pc_sample_pid
= pidcheck
[1];
407 This buffer must be read up in one call.
408 If the buffer isn't big enough to hold
409 all the samples, it will copy up enough
410 to fill the buffer and throw the rest away.
411 This buffer never wraps.
414 pcsamples_read(user_addr_t buffer
, size_t *number
)
419 count
= (*number
)/sizeof(* pc_buffer
);
421 if (count
&& pc_bufsize
&& pc_buffer
)
423 copycount
= pc_bufptr
- pc_buffer
;
431 if (copycount
> count
)
434 /* We actually have data to send up */
435 if(copyout(pc_buffer
, buffer
, copycount
* sizeof(* pc_buffer
)))
441 pc_bufptr
= pc_buffer
;