]>
Commit | Line | Data |
---|---|---|
2d21ac55 A |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
9 | * or http://www.opensolaris.org/os/licensing. | |
10 | * See the License for the specific language governing permissions | |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | /* | |
6d2010ae | 22 | * Copyright 2009 Sun Microsystems, Inc. All rights reserved. |
2d21ac55 A |
23 | * Use is subject to license terms. |
24 | */ | |
25 | ||
b0d623f7 | 26 | /* #pragma ident "@(#)sdt.c 1.9 08/07/01 SMI" */ |
2d21ac55 A |
27 | |
28 | #ifdef KERNEL | |
29 | #ifndef _KERNEL | |
30 | #define _KERNEL /* Solaris vs. Darwin */ | |
31 | #endif | |
32 | #endif | |
33 | ||
34 | #include <sys/param.h> | |
35 | #include <sys/systm.h> | |
36 | #include <sys/errno.h> | |
37 | #include <sys/stat.h> | |
38 | #include <sys/ioctl.h> | |
39 | #include <sys/conf.h> | |
40 | #include <sys/fcntl.h> | |
41 | #include <miscfs/devfs/devfs.h> | |
42 | ||
39236c6e | 43 | |
2d21ac55 A |
44 | #include <sys/dtrace.h> |
45 | #include <sys/dtrace_impl.h> | |
46 | ||
47 | #include <sys/dtrace_glue.h> | |
48 | ||
49 | #include <sys/sdt_impl.h> | |
6d2010ae | 50 | extern int dtrace_kernel_symbol_mode; |
2d21ac55 | 51 | |
fe8ab488 | 52 | /* #include <machine/trap.h */ |
2d21ac55 | 53 | struct savearea_t; /* Used anonymously */ |
2d21ac55 | 54 | |
fe8ab488 A |
55 | #if defined(__x86_64__) |
56 | typedef kern_return_t (*perfCallback)(int, struct savearea_t *, uintptr_t *, int); | |
2d21ac55 | 57 | extern perfCallback tempDTraceTrapHook; |
fe8ab488 | 58 | extern kern_return_t fbt_perfCallback(int, struct savearea_t *, uintptr_t *, int); |
2d21ac55 A |
59 | #define SDT_PATCHVAL 0xf0 |
60 | #define SDT_AFRAMES 6 | |
61 | #else | |
62 | #error Unknown architecture | |
63 | #endif | |
64 | ||
65 | #define SDT_PROBETAB_SIZE 0x1000 /* 4k entries -- 16K total */ | |
66 | ||
b0d623f7 | 67 | #define DTRACE_PROBE_PREFIX "_dtrace_probe$" |
b0d623f7 | 68 | |
2d21ac55 A |
69 | static dev_info_t *sdt_devi; |
70 | static int sdt_verbose = 0; | |
71 | sdt_probe_t **sdt_probetab; | |
72 | int sdt_probetab_size; | |
73 | int sdt_probetab_mask; | |
74 | ||
75 | /*ARGSUSED*/ | |
76 | static void | |
77 | __sdt_provide_module(void *arg, struct modctl *ctl) | |
78 | { | |
79 | #pragma unused(arg) | |
6d2010ae | 80 | struct module *mp = (struct module *)ctl->mod_address; |
2d21ac55 A |
81 | char *modname = ctl->mod_modname; |
82 | sdt_probedesc_t *sdpd; | |
83 | sdt_probe_t *sdp, *old; | |
84 | sdt_provider_t *prov; | |
85 | int len; | |
86 | ||
87 | /* | |
88 | * One for all, and all for one: if we haven't yet registered all of | |
89 | * our providers, we'll refuse to provide anything. | |
90 | */ | |
91 | for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) { | |
92 | if (prov->sdtp_id == DTRACE_PROVNONE) | |
93 | return; | |
94 | } | |
95 | ||
b0d623f7 | 96 | if (!mp || mp->sdt_nprobes != 0 || (sdpd = mp->sdt_probes) == NULL) |
2d21ac55 A |
97 | return; |
98 | ||
99 | for (sdpd = mp->sdt_probes; sdpd != NULL; sdpd = sdpd->sdpd_next) { | |
b0d623f7 A |
100 | const char *name = sdpd->sdpd_name, *func; |
101 | char *nname; | |
2d21ac55 A |
102 | int i, j; |
103 | dtrace_id_t id; | |
104 | ||
105 | for (prov = sdt_providers; prov->sdtp_prefix != NULL; prov++) { | |
b0d623f7 | 106 | const char *prefpart, *prefix = prov->sdtp_prefix; |
2d21ac55 A |
107 | |
108 | if ((prefpart = strstr(name, prefix))) { | |
109 | name = prefpart + strlen(prefix); | |
110 | break; | |
111 | } | |
112 | } | |
113 | ||
114 | nname = kmem_alloc(len = strlen(name) + 1, KM_SLEEP); | |
115 | ||
116 | for (i = 0, j = 0; name[j] != '\0'; i++) { | |
117 | if (name[j] == '_' && name[j + 1] == '_') { | |
118 | nname[i] = '-'; | |
119 | j += 2; | |
120 | } else { | |
121 | nname[i] = name[j++]; | |
122 | } | |
123 | } | |
124 | ||
125 | nname[i] = '\0'; | |
126 | ||
127 | sdp = kmem_zalloc(sizeof (sdt_probe_t), KM_SLEEP); | |
128 | sdp->sdp_loadcnt = ctl->mod_loadcnt; | |
129 | sdp->sdp_ctl = ctl; | |
130 | sdp->sdp_name = nname; | |
131 | sdp->sdp_namelen = len; | |
132 | sdp->sdp_provider = prov; | |
133 | ||
134 | func = sdpd->sdpd_func; | |
135 | ||
136 | if (func == NULL) | |
137 | func = "<unknown>"; | |
138 | ||
139 | /* | |
140 | * We have our provider. Now create the probe. | |
141 | */ | |
142 | if ((id = dtrace_probe_lookup(prov->sdtp_id, modname, | |
143 | func, nname)) != DTRACE_IDNONE) { | |
144 | old = dtrace_probe_arg(prov->sdtp_id, id); | |
145 | ASSERT(old != NULL); | |
146 | ||
147 | sdp->sdp_next = old->sdp_next; | |
148 | sdp->sdp_id = id; | |
149 | old->sdp_next = sdp; | |
150 | } else { | |
151 | sdp->sdp_id = dtrace_probe_create(prov->sdtp_id, | |
152 | modname, func, nname, SDT_AFRAMES, sdp); | |
153 | ||
154 | mp->sdt_nprobes++; | |
155 | } | |
156 | ||
39236c6e A |
157 | #if 0 |
158 | printf ("__sdt_provide_module: sdpd=0x%p sdp=0x%p name=%s, id=%d\n", sdpd, sdp, nname, sdp->sdp_id); | |
159 | #endif | |
160 | ||
2d21ac55 A |
161 | sdp->sdp_hashnext = |
162 | sdt_probetab[SDT_ADDR2NDX(sdpd->sdpd_offset)]; | |
163 | sdt_probetab[SDT_ADDR2NDX(sdpd->sdpd_offset)] = sdp; | |
164 | ||
165 | sdp->sdp_patchval = SDT_PATCHVAL; | |
166 | sdp->sdp_patchpoint = (sdt_instr_t *)sdpd->sdpd_offset; | |
167 | sdp->sdp_savedval = *sdp->sdp_patchpoint; | |
168 | } | |
169 | } | |
170 | ||
171 | /*ARGSUSED*/ | |
172 | static void | |
173 | sdt_destroy(void *arg, dtrace_id_t id, void *parg) | |
174 | { | |
175 | #pragma unused(arg,id) | |
176 | sdt_probe_t *sdp = parg, *old, *last, *hash; | |
177 | int ndx; | |
fe8ab488 | 178 | |
2d21ac55 | 179 | #if !defined(__APPLE__) |
fe8ab488 A |
180 | /* |
181 | * APPLE NOTE: sdt probes for kexts not yet implemented | |
182 | */ | |
2d21ac55 A |
183 | struct modctl *ctl = sdp->sdp_ctl; |
184 | ||
185 | if (ctl != NULL && ctl->mod_loadcnt == sdp->sdp_loadcnt) { | |
186 | if ((ctl->mod_loadcnt == sdp->sdp_loadcnt && | |
187 | ctl->mod_loaded)) { | |
188 | ((struct module *)(ctl->mod_mp))->sdt_nprobes--; | |
189 | } | |
190 | } | |
191 | #endif /* __APPLE__ */ | |
192 | ||
193 | while (sdp != NULL) { | |
194 | old = sdp; | |
195 | ||
196 | /* | |
197 | * Now we need to remove this probe from the sdt_probetab. | |
198 | */ | |
199 | ndx = SDT_ADDR2NDX(sdp->sdp_patchpoint); | |
200 | last = NULL; | |
201 | hash = sdt_probetab[ndx]; | |
202 | ||
203 | while (hash != sdp) { | |
204 | ASSERT(hash != NULL); | |
205 | last = hash; | |
206 | hash = hash->sdp_hashnext; | |
207 | } | |
208 | ||
209 | if (last != NULL) { | |
210 | last->sdp_hashnext = sdp->sdp_hashnext; | |
211 | } else { | |
212 | sdt_probetab[ndx] = sdp->sdp_hashnext; | |
213 | } | |
214 | ||
215 | kmem_free(sdp->sdp_name, sdp->sdp_namelen); | |
216 | sdp = sdp->sdp_next; | |
217 | kmem_free(old, sizeof (sdt_probe_t)); | |
218 | } | |
219 | } | |
220 | ||
221 | /*ARGSUSED*/ | |
6d2010ae | 222 | static int |
2d21ac55 A |
223 | sdt_enable(void *arg, dtrace_id_t id, void *parg) |
224 | { | |
225 | #pragma unused(arg,id) | |
226 | sdt_probe_t *sdp = parg; | |
227 | struct modctl *ctl = sdp->sdp_ctl; | |
228 | ||
2d21ac55 A |
229 | ctl->mod_nenabled++; |
230 | ||
231 | /* | |
232 | * If this module has disappeared since we discovered its probes, | |
233 | * refuse to enable it. | |
234 | */ | |
235 | if (!ctl->mod_loaded) { | |
236 | if (sdt_verbose) { | |
237 | cmn_err(CE_NOTE, "sdt is failing for probe %s " | |
238 | "(module %s unloaded)", | |
239 | sdp->sdp_name, ctl->mod_modname); | |
240 | } | |
241 | goto err; | |
242 | } | |
243 | ||
244 | /* | |
245 | * Now check that our modctl has the expected load count. If it | |
246 | * doesn't, this module must have been unloaded and reloaded -- and | |
247 | * we're not going to touch it. | |
248 | */ | |
249 | if (ctl->mod_loadcnt != sdp->sdp_loadcnt) { | |
250 | if (sdt_verbose) { | |
251 | cmn_err(CE_NOTE, "sdt is failing for probe %s " | |
252 | "(module %s reloaded)", | |
253 | sdp->sdp_name, ctl->mod_modname); | |
254 | } | |
255 | goto err; | |
256 | } | |
2d21ac55 | 257 | |
2d21ac55 A |
258 | dtrace_casptr(&tempDTraceTrapHook, NULL, fbt_perfCallback); |
259 | if (tempDTraceTrapHook != (perfCallback)fbt_perfCallback) { | |
260 | if (sdt_verbose) { | |
261 | cmn_err(CE_NOTE, "sdt_enable is failing for probe %s " | |
262 | "in module %s: tempDTraceTrapHook already occupied.", | |
263 | sdp->sdp_name, ctl->mod_modname); | |
264 | } | |
6d2010ae | 265 | return (0); |
2d21ac55 A |
266 | } |
267 | ||
268 | while (sdp != NULL) { | |
269 | (void)ml_nofault_copy( (vm_offset_t)&sdp->sdp_patchval, (vm_offset_t)sdp->sdp_patchpoint, | |
b0d623f7 | 270 | (vm_size_t)sizeof(sdp->sdp_patchval)); |
39037602 A |
271 | |
272 | /* | |
273 | * Make the patched instruction visible via a data + instruction | |
274 | * cache fush on platforms that need it | |
275 | */ | |
276 | flush_dcache((vm_offset_t)sdp->sdp_patchpoint,(vm_size_t)sizeof(sdp->sdp_patchval), 0); | |
277 | invalidate_icache((vm_offset_t)sdp->sdp_patchpoint,(vm_size_t)sizeof(sdp->sdp_patchval), 0); | |
278 | ||
2d21ac55 A |
279 | sdp = sdp->sdp_next; |
280 | } | |
6d2010ae | 281 | |
2d21ac55 | 282 | err: |
6d2010ae | 283 | return (0); |
2d21ac55 A |
284 | } |
285 | ||
286 | /*ARGSUSED*/ | |
287 | static void | |
288 | sdt_disable(void *arg, dtrace_id_t id, void *parg) | |
289 | { | |
290 | #pragma unused(arg,id) | |
291 | sdt_probe_t *sdp = parg; | |
2d21ac55 A |
292 | struct modctl *ctl = sdp->sdp_ctl; |
293 | ||
294 | ctl->mod_nenabled--; | |
295 | ||
296 | if (!ctl->mod_loaded || ctl->mod_loadcnt != sdp->sdp_loadcnt) | |
297 | goto err; | |
2d21ac55 A |
298 | |
299 | while (sdp != NULL) { | |
300 | (void)ml_nofault_copy( (vm_offset_t)&sdp->sdp_savedval, (vm_offset_t)sdp->sdp_patchpoint, | |
b0d623f7 | 301 | (vm_size_t)sizeof(sdp->sdp_savedval)); |
39037602 A |
302 | /* |
303 | * Make the patched instruction visible via a data + instruction | |
304 | * cache flush on platforms that need it | |
305 | */ | |
306 | flush_dcache((vm_offset_t)sdp->sdp_patchpoint,(vm_size_t)sizeof(sdp->sdp_savedval), 0); | |
307 | invalidate_icache((vm_offset_t)sdp->sdp_patchpoint,(vm_size_t)sizeof(sdp->sdp_savedval), 0); | |
2d21ac55 A |
308 | sdp = sdp->sdp_next; |
309 | } | |
310 | ||
311 | err: | |
312 | ; | |
313 | } | |
314 | ||
315 | static dtrace_pops_t sdt_pops = { | |
316 | NULL, | |
317 | sdt_provide_module, | |
318 | sdt_enable, | |
319 | sdt_disable, | |
320 | NULL, | |
321 | NULL, | |
322 | sdt_getargdesc, | |
b0d623f7 | 323 | sdt_getarg, |
2d21ac55 A |
324 | NULL, |
325 | sdt_destroy | |
326 | }; | |
327 | ||
328 | /*ARGSUSED*/ | |
329 | static int | |
330 | sdt_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) | |
331 | { | |
332 | #pragma unused(cmd) | |
333 | sdt_provider_t *prov; | |
334 | ||
335 | if (ddi_create_minor_node(devi, "sdt", S_IFCHR, | |
336 | 0, DDI_PSEUDO, 0) == DDI_FAILURE) { | |
337 | cmn_err(CE_NOTE, "/dev/sdt couldn't create minor node"); | |
338 | ddi_remove_minor_node(devi, NULL); | |
339 | return (DDI_FAILURE); | |
340 | } | |
341 | ||
342 | ddi_report_dev(devi); | |
343 | sdt_devi = devi; | |
344 | ||
345 | if (sdt_probetab_size == 0) | |
346 | sdt_probetab_size = SDT_PROBETAB_SIZE; | |
347 | ||
348 | sdt_probetab_mask = sdt_probetab_size - 1; | |
349 | sdt_probetab = | |
350 | kmem_zalloc(sdt_probetab_size * sizeof (sdt_probe_t *), KM_SLEEP); | |
351 | dtrace_invop_add(sdt_invop); | |
352 | ||
353 | for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) { | |
354 | if (dtrace_register(prov->sdtp_name, prov->sdtp_attr, | |
355 | DTRACE_PRIV_KERNEL, NULL, | |
356 | &sdt_pops, prov, &prov->sdtp_id) != 0) { | |
357 | cmn_err(CE_WARN, "failed to register sdt provider %s", | |
358 | prov->sdtp_name); | |
359 | } | |
360 | } | |
361 | ||
362 | return (DDI_SUCCESS); | |
363 | } | |
364 | ||
fe8ab488 A |
365 | /* |
366 | * APPLE NOTE: sdt_detach not implemented | |
367 | */ | |
2d21ac55 A |
368 | #if !defined(__APPLE__) |
369 | /*ARGSUSED*/ | |
370 | static int | |
371 | sdt_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) | |
372 | { | |
373 | sdt_provider_t *prov; | |
374 | ||
375 | switch (cmd) { | |
376 | case DDI_DETACH: | |
377 | break; | |
378 | ||
379 | case DDI_SUSPEND: | |
380 | return (DDI_SUCCESS); | |
381 | ||
382 | default: | |
383 | return (DDI_FAILURE); | |
384 | } | |
385 | ||
386 | for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) { | |
387 | if (prov->sdtp_id != DTRACE_PROVNONE) { | |
388 | if (dtrace_unregister(prov->sdtp_id) != 0) | |
389 | return (DDI_FAILURE); | |
390 | ||
391 | prov->sdtp_id = DTRACE_PROVNONE; | |
392 | } | |
393 | } | |
394 | ||
395 | dtrace_invop_remove(sdt_invop); | |
396 | kmem_free(sdt_probetab, sdt_probetab_size * sizeof (sdt_probe_t *)); | |
397 | ||
398 | return (DDI_SUCCESS); | |
399 | } | |
fe8ab488 | 400 | #endif /* __APPLE__ */ |
2d21ac55 | 401 | |
2d21ac55 A |
402 | d_open_t _sdt_open; |
403 | ||
404 | int _sdt_open(dev_t dev, int flags, int devtype, struct proc *p) | |
405 | { | |
406 | #pragma unused(dev,flags,devtype,p) | |
407 | return 0; | |
408 | } | |
409 | ||
410 | #define SDT_MAJOR -24 /* let the kernel pick the device number */ | |
411 | ||
412 | /* | |
413 | * A struct describing which functions will get invoked for certain | |
414 | * actions. | |
415 | */ | |
416 | static struct cdevsw sdt_cdevsw = | |
417 | { | |
418 | _sdt_open, /* open */ | |
419 | eno_opcl, /* close */ | |
420 | eno_rdwrt, /* read */ | |
421 | eno_rdwrt, /* write */ | |
422 | eno_ioctl, /* ioctl */ | |
423 | (stop_fcn_t *)nulldev, /* stop */ | |
424 | (reset_fcn_t *)nulldev, /* reset */ | |
425 | NULL, /* tty's */ | |
426 | eno_select, /* select */ | |
427 | eno_mmap, /* mmap */ | |
428 | eno_strat, /* strategy */ | |
429 | eno_getc, /* getc */ | |
430 | eno_putc, /* putc */ | |
431 | 0 /* type */ | |
432 | }; | |
433 | ||
434 | static int gSDTInited = 0; | |
435 | static struct modctl g_sdt_kernctl; | |
436 | static struct module g_sdt_mach_module; | |
437 | ||
2d21ac55 | 438 | #include <mach-o/nlist.h> |
b0d623f7 | 439 | #include <libkern/kernel_mach_header.h> |
2d21ac55 | 440 | |
2d21ac55 A |
441 | void sdt_init( void ) |
442 | { | |
443 | if (0 == gSDTInited) | |
444 | { | |
445 | int majdevno = cdevsw_add(SDT_MAJOR, &sdt_cdevsw); | |
446 | ||
447 | if (majdevno < 0) { | |
448 | printf("sdt_init: failed to allocate a major number!\n"); | |
449 | gSDTInited = 0; | |
450 | return; | |
451 | } | |
452 | ||
39037602 | 453 | if (dtrace_sdt_probes_restricted()) { |
3e170ce0 A |
454 | return; |
455 | } | |
456 | ||
316670eb | 457 | if (MH_MAGIC_KERNEL != _mh_execute_header.magic) { |
6d2010ae A |
458 | g_sdt_kernctl.mod_address = (vm_address_t)NULL; |
459 | g_sdt_kernctl.mod_size = 0; | |
2d21ac55 | 460 | } else { |
6d2010ae A |
461 | kernel_mach_header_t *mh; |
462 | struct load_command *cmd; | |
463 | kernel_segment_command_t *orig_ts = NULL, *orig_le = NULL; | |
464 | struct symtab_command *orig_st = NULL; | |
465 | kernel_nlist_t *sym = NULL; | |
466 | char *strings; | |
467 | unsigned int i; | |
468 | ||
469 | g_sdt_mach_module.sdt_nprobes = 0; | |
470 | g_sdt_mach_module.sdt_probes = NULL; | |
471 | ||
472 | g_sdt_kernctl.mod_address = (vm_address_t)&g_sdt_mach_module; | |
473 | g_sdt_kernctl.mod_size = 0; | |
474 | strncpy((char *)&(g_sdt_kernctl.mod_modname), "mach_kernel", KMOD_MAX_NAME); | |
475 | ||
476 | g_sdt_kernctl.mod_next = NULL; | |
477 | g_sdt_kernctl.mod_stale = NULL; | |
478 | g_sdt_kernctl.mod_id = 0; | |
479 | g_sdt_kernctl.mod_loadcnt = 1; | |
480 | g_sdt_kernctl.mod_loaded = 1; | |
481 | g_sdt_kernctl.mod_flags = 0; | |
482 | g_sdt_kernctl.mod_nenabled = 0; | |
483 | ||
484 | mh = &_mh_execute_header; | |
485 | cmd = (struct load_command*) &mh[1]; | |
486 | for (i = 0; i < mh->ncmds; i++) { | |
487 | if (cmd->cmd == LC_SEGMENT_KERNEL) { | |
488 | kernel_segment_command_t *orig_sg = (kernel_segment_command_t *) cmd; | |
489 | ||
490 | if (LIT_STRNEQL(orig_sg->segname, SEG_TEXT)) | |
491 | orig_ts = orig_sg; | |
492 | else if (LIT_STRNEQL(orig_sg->segname, SEG_LINKEDIT)) | |
493 | orig_le = orig_sg; | |
494 | else if (LIT_STRNEQL(orig_sg->segname, "")) | |
495 | orig_ts = orig_sg; /* kexts have a single unnamed segment */ | |
496 | } | |
497 | else if (cmd->cmd == LC_SYMTAB) | |
498 | orig_st = (struct symtab_command *) cmd; | |
499 | ||
500 | cmd = (struct load_command *) ((uintptr_t) cmd + cmd->cmdsize); | |
501 | } | |
502 | ||
503 | if ((orig_ts == NULL) || (orig_st == NULL) || (orig_le == NULL)) | |
504 | return; | |
505 | ||
506 | sym = (kernel_nlist_t *)(orig_le->vmaddr + orig_st->symoff - orig_le->fileoff); | |
507 | strings = (char *)(orig_le->vmaddr + orig_st->stroff - orig_le->fileoff); | |
508 | ||
509 | for (i = 0; i < orig_st->nsyms; i++) { | |
510 | uint8_t n_type = sym[i].n_type & (N_TYPE | N_EXT); | |
511 | char *name = strings + sym[i].n_un.n_strx; | |
b0d623f7 | 512 | const char *prev_name; |
2d21ac55 A |
513 | unsigned long best; |
514 | unsigned int j; | |
6d2010ae A |
515 | |
516 | /* Check that the symbol is a global and that it has a name. */ | |
517 | if (((N_SECT | N_EXT) != n_type && (N_ABS | N_EXT) != n_type)) | |
518 | continue; | |
519 | ||
520 | if (0 == sym[i].n_un.n_strx) /* iff a null, "", name. */ | |
521 | continue; | |
522 | ||
523 | /* Lop off omnipresent leading underscore. */ | |
524 | if (*name == '_') | |
525 | name += 1; | |
526 | ||
527 | if (strncmp(name, DTRACE_PROBE_PREFIX, sizeof(DTRACE_PROBE_PREFIX) - 1) == 0) { | |
2d21ac55 A |
528 | sdt_probedesc_t *sdpd = kmem_alloc(sizeof(sdt_probedesc_t), KM_SLEEP); |
529 | int len = strlen(name) + 1; | |
6d2010ae | 530 | |
2d21ac55 A |
531 | sdpd->sdpd_name = kmem_alloc(len, KM_SLEEP); |
532 | strncpy(sdpd->sdpd_name, name, len); /* NUL termination is ensured. */ | |
6d2010ae | 533 | |
2d21ac55 A |
534 | prev_name = "<unknown>"; |
535 | best = 0; | |
b0d623f7 | 536 | |
6d2010ae A |
537 | /* |
538 | * Find the symbol immediately preceding the sdt probe site just discovered, | |
539 | * that symbol names the function containing the sdt probe. | |
540 | */ | |
2d21ac55 | 541 | for (j = 0; j < orig_st->nsyms; j++) { |
b0d623f7 A |
542 | uint8_t jn_type = sym[j].n_type & (N_TYPE | N_EXT); |
543 | char *jname = strings + sym[j].n_un.n_strx; | |
6d2010ae | 544 | |
b0d623f7 | 545 | if (((N_SECT | N_EXT) != jn_type && (N_ABS | N_EXT) != jn_type)) |
2d21ac55 | 546 | continue; |
6d2010ae | 547 | |
2d21ac55 A |
548 | if (0 == sym[j].n_un.n_strx) /* iff a null, "", name. */ |
549 | continue; | |
6d2010ae | 550 | |
b0d623f7 A |
551 | if (*jname == '_') |
552 | jname += 1; | |
6d2010ae | 553 | |
2d21ac55 A |
554 | if (*(unsigned long *)sym[i].n_value <= (unsigned long)sym[j].n_value) |
555 | continue; | |
6d2010ae | 556 | |
2d21ac55 A |
557 | if ((unsigned long)sym[j].n_value > best) { |
558 | best = (unsigned long)sym[j].n_value; | |
b0d623f7 | 559 | prev_name = jname; |
2d21ac55 A |
560 | } |
561 | } | |
6d2010ae | 562 | |
2d21ac55 A |
563 | sdpd->sdpd_func = kmem_alloc((len = strlen(prev_name) + 1), KM_SLEEP); |
564 | strncpy(sdpd->sdpd_func, prev_name, len); /* NUL termination is ensured. */ | |
6d2010ae | 565 | |
2d21ac55 | 566 | sdpd->sdpd_offset = *(unsigned long *)sym[i].n_value; |
39236c6e A |
567 | |
568 | #if 0 | |
569 | printf("sdt_init: sdpd_offset=0x%lx, n_value=0x%lx, name=%s\n", | |
570 | sdpd->sdpd_offset, *(unsigned long *)sym[i].n_value, name); | |
571 | #endif | |
572 | ||
2d21ac55 A |
573 | sdpd->sdpd_next = g_sdt_mach_module.sdt_probes; |
574 | g_sdt_mach_module.sdt_probes = sdpd; | |
575 | } else { | |
576 | prev_name = name; | |
577 | } | |
578 | } | |
579 | } | |
6d2010ae | 580 | |
b0d623f7 | 581 | sdt_attach( (dev_info_t *)(uintptr_t)majdevno, DDI_ATTACH ); |
6d2010ae | 582 | |
2d21ac55 A |
583 | gSDTInited = 1; |
584 | } else | |
585 | panic("sdt_init: called twice!\n"); | |
586 | } | |
b0d623f7 | 587 | |
2d21ac55 A |
588 | #undef SDT_MAJOR |
589 | ||
590 | /*ARGSUSED*/ | |
591 | void | |
592 | sdt_provide_module(void *arg, struct modctl *ctl) | |
593 | { | |
c910b4d9 | 594 | #pragma unused(arg) |
6d2010ae A |
595 | ASSERT(ctl != NULL); |
596 | ASSERT(dtrace_kernel_symbol_mode != DTRACE_KERNEL_SYMBOLS_NEVER); | |
597 | lck_mtx_assert(&mod_lock, LCK_MTX_ASSERT_OWNED); | |
598 | ||
599 | if (MOD_SDT_DONE(ctl)) | |
600 | return; | |
601 | ||
602 | if (MOD_IS_MACH_KERNEL(ctl)) { | |
603 | __sdt_provide_module(arg, &g_sdt_kernctl); | |
604 | ||
605 | sdt_probedesc_t *sdpd = g_sdt_mach_module.sdt_probes; | |
606 | while (sdpd) { | |
607 | sdt_probedesc_t *this_sdpd = sdpd; | |
608 | kmem_free((void *)sdpd->sdpd_name, strlen(sdpd->sdpd_name) + 1); | |
609 | kmem_free((void *)sdpd->sdpd_func, strlen(sdpd->sdpd_func) + 1); | |
610 | sdpd = sdpd->sdpd_next; | |
611 | kmem_free((void *)this_sdpd, sizeof(sdt_probedesc_t)); | |
612 | } | |
613 | g_sdt_mach_module.sdt_probes = NULL; | |
614 | } else { | |
fe8ab488 A |
615 | /* |
616 | * APPLE NOTE: sdt probes for kexts not yet implemented | |
617 | */ | |
2d21ac55 | 618 | } |
6d2010ae A |
619 | |
620 | /* Need to mark this module as completed */ | |
621 | ctl->mod_flags |= MODCTL_SDT_PROBES_PROVIDED; | |
2d21ac55 | 622 | } |