2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <sys/kdebug.h>
25 #include <sys/errno.h>
26 #include <sys/param.h>
27 #include <sys/proc_internal.h>
29 #include <sys/sysctl.h>
30 #include <sys/systm.h>
31 #include <vm/vm_kern.h>
32 #include <machine/machine_routines.h>
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;
43 pid_t pc_sample_pid
= 0;
44 boolean_t pc_trace_frameworks
= FALSE
;
46 char pcsample_comm
[MAXCOMLEN
+ 1];
48 /* Set the default framework boundaries */
49 unsigned int pcsample_beg
= 0;
50 unsigned int pcsample_end
= 0;
52 static pid_t global_state_pid
= -1; /* Used to control exclusive use of pc_buffer */
54 extern int pc_trace_buf
[];
55 extern int pc_trace_cnt
;
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);
68 enable_branch_tracing(void)
72 if (-1 != pc_sample_pid
) {
73 p
= pfind(pc_sample_pid
);
75 p
->p_flag
|= P_BTRACE
;
79 pc_trace_frameworks
= TRUE
;
90 disable_branch_tracing(void)
93 switch (pc_sample_pid
) {
95 pc_trace_frameworks
= FALSE
;
100 p
= pfind(pc_sample_pid
);
102 p
->p_flag
&= ~P_BTRACE
;
111 * this only works for the current proc as it
112 * is called from context_switch in the scheduler
115 branch_tracing_enabled(void)
117 struct proc
*p
= current_proc();
118 if (TRUE
== pc_trace_frameworks
) return TRUE
;
120 return (P_BTRACE
== (p
->p_flag
& P_BTRACE
));
132 if (!pcsample_enable
)
135 for (i
=0; i
< pc_trace_cnt
; i
++)
137 pc
= pc_trace_buf
[i
];
139 if ((pcsample_beg
<= pc
) && (pc
< pcsample_end
))
141 if (pc_bufptr
> pc_buffer
)
143 if ( (*(pc_bufptr
-1)) == pc
)
144 continue; /* Ignore, probably spinning */
147 /* Then the sample is in our range */
153 /* We never wrap the buffer */
154 if ((pc_bufptr
+ pc_trace_cnt
) >= pc_buflast
)
157 (void)disable_branch_tracing();
158 wakeup(&pcsample_enable
);
164 pcsamples_bootstrap(void)
166 if (!disable_branch_tracing())
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
;
177 pc_bufptr
= pc_buffer
;
178 pc_buflast
= &pc_bufptr
[npcbufs
];
189 pcsamples_reinit(void)
195 if (pc_bufsize
&& pc_buffer
)
196 kmem_free(kernel_map
, (vm_offset_t
)pc_buffer
, pc_bufsize
);
198 ret
= pcsamples_bootstrap();
203 pcsamples_clear(void)
205 /* Clean up the sample buffer, set defaults */
206 global_state_pid
= -1;
208 if(pc_bufsize
&& pc_buffer
)
209 kmem_free(kernel_map
, (vm_offset_t
)pc_buffer
, pc_bufsize
);
216 bzero((void *)pcsample_comm
, sizeof(pcsample_comm
));
217 (void)disable_branch_tracing();
219 pc_trace_frameworks
= FALSE
;
223 pcsamples_control(int *name
, __unused u_int namelen
, user_addr_t where
, size_t *sizep
)
232 struct proc
*p
, *curproc
;
234 if (name
[0] != PCSAMPLE_GETNUMBUF
)
236 curproc
= current_proc();
238 curpid
= curproc
->p_pid
;
242 if (global_state_pid
== -1)
243 global_state_pid
= curpid
;
244 else if (global_state_pid
!= curpid
)
246 if((p
= pfind(global_state_pid
)) == NULL
)
248 /* The global pid no longer exists */
249 global_state_pid
= curpid
;
253 /* The global pid exists, deny this request */
261 case PCSAMPLE_DISABLE
: /* used to disable */
264 case PCSAMPLE_SETNUMBUF
:
265 /* The buffer size is bounded by a min and max number of samples */
266 if (value
< pc_trace_cnt
) {
270 if (value
<= MAX_PCSAMPLES
)
271 /* npcbufs = value & ~(PC_TRACE_CNT-1); */
274 npcbufs
= MAX_PCSAMPLES
;
276 case PCSAMPLE_GETNUMBUF
:
277 if (size
< sizeof(pc_bufinfo
)) {
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
)))
292 ret
=pcsamples_reinit();
294 case PCSAMPLE_REMOVE
:
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 */
302 /* Do not wait on the buffer */
304 (void)disable_branch_tracing();
305 ret
= pcsamples_read(where
, sizep
);
308 else if ((pc_bufsize
<= 0) || (!pc_buffer
))
310 /* enable only if buffer is initialized */
315 /* Turn on branch tracing */
316 if (!enable_branch_tracing())
322 /* Enable sampling */
325 ret
= tsleep(&pcsample_enable
, PRIBIO
| PCATCH
, "pcsample", 0);
327 (void)disable_branch_tracing();
331 /* Eventually fix this... if (ret != EINTR) */
334 /* On errors, except EINTR, we want to cleanup buffer ptrs */
335 /* pc_bufptr = pc_buffer; */
341 /* The only way to get here is if the buffer is full */
342 ret
= pcsamples_read(where
, sizep
);
346 case PCSAMPLE_SETREG
:
347 if (size
< sizeof(pc_bufinfo
))
352 if (copyin(where
, &pc_bufinfo
, sizeof(pc_bufinfo
)))
358 pcsample_beg
= pc_bufinfo
.pcsample_beg
;
359 pcsample_end
= pc_bufinfo
.pcsample_end
;
362 if (!(sizeof(pcsample_comm
) > size
))
367 bzero((void *)pcsample_comm
, sizeof(pcsample_comm
));
368 if (copyin(where
, pcsample_comm
, size
))
374 /* Check for command name or pid */
375 if (pcsample_comm
[0] != '\0')
382 if (size
!= (2 * sizeof(pid_t
)))
389 pidcheck
= (pid_t
*)pcsample_comm
;
390 pc_sample_pid
= pidcheck
[1];
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.
410 pcsamples_read(user_addr_t buffer
, size_t *number
)
415 count
= (*number
)/sizeof(* pc_buffer
);
417 if (count
&& pc_bufsize
&& pc_buffer
)
419 copycount
= pc_bufptr
- pc_buffer
;
427 if (copycount
> count
)
430 /* We actually have data to send up */
431 if(copyout(pc_buffer
, buffer
, copycount
* sizeof(* pc_buffer
)))
437 pc_bufptr
= pc_buffer
;