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