2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 #include <sys/kdebug.h>
32 #include <sys/errno.h>
33 #include <sys/param.h>
34 #include <sys/proc_internal.h>
36 #include <sys/sysctl.h>
37 #include <sys/systm.h>
38 #include <vm/vm_kern.h>
39 #include <machine/machine_routines.h>
41 vm_offset_t pc_buftomem
= 0;
42 unsigned int * pc_buffer
= 0; /* buffer that holds each pc */
43 unsigned int * pc_bufptr
= 0;
44 unsigned int * pc_buflast
= 0;
45 unsigned int npcbufs
= 8192; /* number of pc entries in buffer */
46 unsigned int pc_bufsize
= 0;
47 unsigned int pcsample_flags
= 0;
48 unsigned int pcsample_enable
= 0;
50 pid_t pc_sample_pid
= 0;
51 boolean_t pc_trace_frameworks
= FALSE
;
53 char pcsample_comm
[MAXCOMLEN
+ 1];
55 /* Set the default framework boundaries */
56 unsigned int pcsample_beg
= 0;
57 unsigned int pcsample_end
= 0;
59 static pid_t global_state_pid
= -1; /* Used to control exclusive use of pc_buffer */
61 extern int pc_trace_buf
[];
62 extern int pc_trace_cnt
;
64 void add_pcbuffer(void);
65 int branch_tracing_enabled(void);
66 int disable_branch_tracing(void);
67 int enable_branch_tracing(void);
68 int pcsamples_bootstrap(void);
69 void pcsamples_clear(void);
70 int pcsamples_control(int *name
, u_int namelen
, user_addr_t where
, size_t *sizep
);
71 int pcsamples_read(user_addr_t buffer
, size_t *number
);
72 int pcsamples_reinit(void);
75 enable_branch_tracing(void)
79 if (-1 != pc_sample_pid
) {
80 p
= pfind(pc_sample_pid
);
82 p
->p_flag
|= P_BTRACE
;
86 pc_trace_frameworks
= TRUE
;
97 disable_branch_tracing(void)
100 switch (pc_sample_pid
) {
102 pc_trace_frameworks
= FALSE
;
107 p
= pfind(pc_sample_pid
);
109 p
->p_flag
&= ~P_BTRACE
;
118 * this only works for the current proc as it
119 * is called from context_switch in the scheduler
122 branch_tracing_enabled(void)
124 struct proc
*p
= current_proc();
125 if (TRUE
== pc_trace_frameworks
) return TRUE
;
127 return (P_BTRACE
== (p
->p_flag
& P_BTRACE
));
139 if (!pcsample_enable
)
142 for (i
=0; i
< pc_trace_cnt
; i
++)
144 pc
= pc_trace_buf
[i
];
146 if ((pcsample_beg
<= pc
) && (pc
< pcsample_end
))
148 if (pc_bufptr
> pc_buffer
)
150 if ( (*(pc_bufptr
-1)) == pc
)
151 continue; /* Ignore, probably spinning */
154 /* Then the sample is in our range */
160 /* We never wrap the buffer */
161 if ((pc_bufptr
+ pc_trace_cnt
) >= pc_buflast
)
164 (void)disable_branch_tracing();
165 wakeup(&pcsample_enable
);
171 pcsamples_bootstrap(void)
173 if (!disable_branch_tracing())
176 pc_bufsize
= npcbufs
* sizeof(* pc_buffer
);
177 if (kmem_alloc(kernel_map
, &pc_buftomem
,
178 (vm_size_t
)pc_bufsize
) == KERN_SUCCESS
)
179 pc_buffer
= (unsigned int *) pc_buftomem
;
184 pc_bufptr
= pc_buffer
;
185 pc_buflast
= &pc_bufptr
[npcbufs
];
196 pcsamples_reinit(void)
202 if (pc_bufsize
&& pc_buffer
)
203 kmem_free(kernel_map
, (vm_offset_t
)pc_buffer
, pc_bufsize
);
205 ret
= pcsamples_bootstrap();
210 pcsamples_clear(void)
212 /* Clean up the sample buffer, set defaults */
213 global_state_pid
= -1;
215 if(pc_bufsize
&& pc_buffer
)
216 kmem_free(kernel_map
, (vm_offset_t
)pc_buffer
, pc_bufsize
);
223 bzero((void *)pcsample_comm
, sizeof(pcsample_comm
));
224 (void)disable_branch_tracing();
226 pc_trace_frameworks
= FALSE
;
230 pcsamples_control(int *name
, __unused u_int namelen
, user_addr_t where
, size_t *sizep
)
239 struct proc
*p
, *curproc
;
241 if (name
[0] != PCSAMPLE_GETNUMBUF
)
243 curproc
= current_proc();
245 curpid
= curproc
->p_pid
;
249 if (global_state_pid
== -1)
250 global_state_pid
= curpid
;
251 else if (global_state_pid
!= curpid
)
253 if((p
= pfind(global_state_pid
)) == NULL
)
255 /* The global pid no longer exists */
256 global_state_pid
= curpid
;
260 /* The global pid exists, deny this request */
268 case PCSAMPLE_DISABLE
: /* used to disable */
271 case PCSAMPLE_SETNUMBUF
:
272 /* The buffer size is bounded by a min and max number of samples */
273 if (value
< pc_trace_cnt
) {
277 if (value
<= MAX_PCSAMPLES
)
278 /* npcbufs = value & ~(PC_TRACE_CNT-1); */
281 npcbufs
= MAX_PCSAMPLES
;
283 case PCSAMPLE_GETNUMBUF
:
284 if (size
< sizeof(pc_bufinfo
)) {
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
)))
299 ret
=pcsamples_reinit();
301 case PCSAMPLE_REMOVE
:
304 case PCSAMPLE_READBUF
:
305 /* A nonzero value says enable and wait on the buffer */
306 /* A zero value says read up the buffer immediately */
309 /* Do not wait on the buffer */
311 (void)disable_branch_tracing();
312 ret
= pcsamples_read(where
, sizep
);
315 else if ((pc_bufsize
<= 0) || (!pc_buffer
))
317 /* enable only if buffer is initialized */
322 /* Turn on branch tracing */
323 if (!enable_branch_tracing())
329 /* Enable sampling */
332 ret
= tsleep(&pcsample_enable
, PRIBIO
| PCATCH
, "pcsample", 0);
334 (void)disable_branch_tracing();
338 /* Eventually fix this... if (ret != EINTR) */
341 /* On errors, except EINTR, we want to cleanup buffer ptrs */
342 /* pc_bufptr = pc_buffer; */
348 /* The only way to get here is if the buffer is full */
349 ret
= pcsamples_read(where
, sizep
);
353 case PCSAMPLE_SETREG
:
354 if (size
< sizeof(pc_bufinfo
))
359 if (copyin(where
, &pc_bufinfo
, sizeof(pc_bufinfo
)))
365 pcsample_beg
= pc_bufinfo
.pcsample_beg
;
366 pcsample_end
= pc_bufinfo
.pcsample_end
;
369 if (!(sizeof(pcsample_comm
) > size
))
374 bzero((void *)pcsample_comm
, sizeof(pcsample_comm
));
375 if (copyin(where
, pcsample_comm
, size
))
381 /* Check for command name or pid */
382 if (pcsample_comm
[0] != '\0')
389 if (size
!= (2 * sizeof(pid_t
)))
396 pidcheck
= (pid_t
*)pcsample_comm
;
397 pc_sample_pid
= pidcheck
[1];
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.
417 pcsamples_read(user_addr_t buffer
, size_t *number
)
422 count
= (*number
)/sizeof(* pc_buffer
);
424 if (count
&& pc_bufsize
&& pc_buffer
)
426 copycount
= pc_bufptr
- pc_buffer
;
434 if (copycount
> count
)
437 /* We actually have data to send up */
438 if(copyout(pc_buffer
, buffer
, copycount
* sizeof(* pc_buffer
)))
444 pc_bufptr
= pc_buffer
;