]>
Commit | Line | Data |
---|---|---|
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 | /* | |
22 | * Copyright 2009 Sun Microsystems, Inc. All rights reserved. | |
23 | * Use is subject to license terms. | |
24 | */ | |
25 | ||
26 | #include <sys/param.h> | |
27 | #include <sys/systm.h> | |
28 | #include <sys/errno.h> | |
29 | #include <sys/stat.h> | |
30 | #include <sys/ioctl.h> | |
31 | #include <sys/conf.h> | |
32 | #include <sys/fcntl.h> | |
33 | #include <miscfs/devfs/devfs.h> | |
34 | ||
35 | #if defined(__arm__) || defined(__arm64__) | |
36 | #include <arm/caches_internal.h> | |
37 | #endif /* defined(__arm__) || defined(__arm64__) */ | |
38 | ||
39 | #include <sys/dtrace.h> | |
40 | #include <sys/dtrace_impl.h> | |
41 | ||
42 | #include <sys/dtrace_glue.h> | |
43 | ||
44 | #include <sys/sdt_impl.h> | |
45 | extern int dtrace_kernel_symbol_mode; | |
46 | ||
47 | #include <ptrauth.h> | |
48 | ||
49 | /* #include <machine/trap.h */ | |
50 | struct savearea_t; /* Used anonymously */ | |
51 | ||
52 | #if defined(__arm__) | |
53 | typedef kern_return_t (*perfCallback)(int, struct savearea_t *, __unused int, __unused int); | |
54 | extern perfCallback tempDTraceTrapHook; | |
55 | extern kern_return_t fbt_perfCallback(int, struct savearea_t *, __unused int, __unused int); | |
56 | #define SDT_PATCHVAL 0xdefc | |
57 | #define SDT_AFRAMES 7 | |
58 | #elif defined(__arm64__) | |
59 | typedef kern_return_t (*perfCallback)(int, struct savearea_t *, __unused int, __unused int); | |
60 | extern perfCallback tempDTraceTrapHook; | |
61 | extern kern_return_t fbt_perfCallback(int, struct savearea_t *, __unused int, __unused int); | |
62 | #define SDT_PATCHVAL 0xe7eeee7e | |
63 | #define SDT_AFRAMES 7 | |
64 | #elif defined(__x86_64__) | |
65 | typedef kern_return_t (*perfCallback)(int, struct savearea_t *, uintptr_t *, int); | |
66 | extern perfCallback tempDTraceTrapHook; | |
67 | extern kern_return_t fbt_perfCallback(int, struct savearea_t *, uintptr_t *, int); | |
68 | #define SDT_PATCHVAL 0xf0 | |
69 | #define SDT_AFRAMES 6 | |
70 | #else | |
71 | #error Unknown architecture | |
72 | #endif | |
73 | ||
74 | #define SDT_PROBETAB_SIZE 0x1000 /* 4k entries -- 16K total */ | |
75 | ||
76 | #define DTRACE_PROBE_PREFIX "_dtrace_probe$" | |
77 | ||
78 | static int sdt_verbose = 0; | |
79 | sdt_probe_t **sdt_probetab; | |
80 | int sdt_probetab_size; | |
81 | int sdt_probetab_mask; | |
82 | ||
83 | /*ARGSUSED*/ | |
84 | static void | |
85 | __sdt_provide_module(void *arg, struct modctl *ctl) | |
86 | { | |
87 | #pragma unused(arg) | |
88 | struct module *mp = (struct module *)ctl->mod_address; | |
89 | char *modname = ctl->mod_modname; | |
90 | sdt_probedesc_t *sdpd; | |
91 | sdt_probe_t *sdp, *old; | |
92 | sdt_provider_t *prov; | |
93 | ||
94 | /* | |
95 | * One for all, and all for one: if we haven't yet registered all of | |
96 | * our providers, we'll refuse to provide anything. | |
97 | */ | |
98 | for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) { | |
99 | if (prov->sdtp_id == DTRACE_PROVNONE) { | |
100 | return; | |
101 | } | |
102 | } | |
103 | ||
104 | if (!mp || mp->sdt_nprobes != 0 || (sdpd = mp->sdt_probes) == NULL) { | |
105 | return; | |
106 | } | |
107 | ||
108 | for (sdpd = mp->sdt_probes; sdpd != NULL; sdpd = sdpd->sdpd_next) { | |
109 | const char *func; | |
110 | dtrace_id_t id; | |
111 | ||
112 | /* Validate probe's provider name. Do not provide probes for unknown providers. */ | |
113 | for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) { | |
114 | if (strcmp(prov->sdtp_prefix, sdpd->sdpd_prov) == 0) { | |
115 | break; | |
116 | } | |
117 | } | |
118 | ||
119 | if (prov->sdtp_name == NULL) { | |
120 | printf("Ignoring probes from unsupported provider %s\n", sdpd->sdpd_prov); | |
121 | continue; | |
122 | } | |
123 | ||
124 | sdp = kmem_zalloc(sizeof(sdt_probe_t), KM_SLEEP); | |
125 | sdp->sdp_loadcnt = ctl->mod_loadcnt; | |
126 | sdp->sdp_ctl = ctl; | |
127 | sdp->sdp_name = kmem_alloc(strlen(sdpd->sdpd_name) + 1, KM_SLEEP); | |
128 | strncpy(sdp->sdp_name, sdpd->sdpd_name, strlen(sdpd->sdpd_name) + 1); | |
129 | sdp->sdp_namelen = strlen(sdpd->sdpd_name) + 1; | |
130 | sdp->sdp_provider = prov; | |
131 | ||
132 | func = (sdpd->sdpd_func != NULL) ? sdpd->sdpd_func : "<unknown>"; | |
133 | ||
134 | /* | |
135 | * We have our provider. Now create the probe. | |
136 | */ | |
137 | if ((id = dtrace_probe_lookup(prov->sdtp_id, modname, | |
138 | func, sdp->sdp_name)) != DTRACE_IDNONE) { | |
139 | old = dtrace_probe_arg(prov->sdtp_id, id); | |
140 | ASSERT(old != NULL); | |
141 | ||
142 | sdp->sdp_next = old->sdp_next; | |
143 | sdp->sdp_id = id; | |
144 | old->sdp_next = sdp; | |
145 | } else { | |
146 | sdp->sdp_id = dtrace_probe_create(prov->sdtp_id, | |
147 | modname, func, sdp->sdp_name, SDT_AFRAMES, sdp); | |
148 | ||
149 | mp->sdt_nprobes++; | |
150 | } | |
151 | ||
152 | #if 0 | |
153 | printf("__sdt_provide_module: sdpd=0x%p sdp=0x%p name=%s, id=%d\n", sdpd, sdp, | |
154 | sdp->sdp_name, sdp->sdp_id); | |
155 | #endif | |
156 | ||
157 | sdp->sdp_hashnext = | |
158 | sdt_probetab[SDT_ADDR2NDX(sdpd->sdpd_offset)]; | |
159 | sdt_probetab[SDT_ADDR2NDX(sdpd->sdpd_offset)] = sdp; | |
160 | ||
161 | sdp->sdp_patchval = SDT_PATCHVAL; | |
162 | sdp->sdp_patchpoint = (sdt_instr_t *)sdpd->sdpd_offset; | |
163 | sdp->sdp_savedval = *sdp->sdp_patchpoint; | |
164 | } | |
165 | } | |
166 | ||
167 | /*ARGSUSED*/ | |
168 | static void | |
169 | sdt_destroy(void *arg, dtrace_id_t id, void *parg) | |
170 | { | |
171 | #pragma unused(arg,id) | |
172 | sdt_probe_t *sdp = parg, *old, *last, *hash; | |
173 | int ndx; | |
174 | ||
175 | #if !defined(__APPLE__) | |
176 | /* | |
177 | * APPLE NOTE: sdt probes for kexts not yet implemented | |
178 | */ | |
179 | struct modctl *ctl = sdp->sdp_ctl; | |
180 | ||
181 | if (ctl != NULL && ctl->mod_loadcnt == sdp->sdp_loadcnt) { | |
182 | if ((ctl->mod_loadcnt == sdp->sdp_loadcnt && | |
183 | ctl->mod_loaded)) { | |
184 | ((struct module *)(ctl->mod_mp))->sdt_nprobes--; | |
185 | } | |
186 | } | |
187 | #endif /* __APPLE__ */ | |
188 | ||
189 | while (sdp != NULL) { | |
190 | old = sdp; | |
191 | ||
192 | /* | |
193 | * Now we need to remove this probe from the sdt_probetab. | |
194 | */ | |
195 | ndx = SDT_ADDR2NDX(sdp->sdp_patchpoint); | |
196 | last = NULL; | |
197 | hash = sdt_probetab[ndx]; | |
198 | ||
199 | while (hash != sdp) { | |
200 | ASSERT(hash != NULL); | |
201 | last = hash; | |
202 | hash = hash->sdp_hashnext; | |
203 | } | |
204 | ||
205 | if (last != NULL) { | |
206 | last->sdp_hashnext = sdp->sdp_hashnext; | |
207 | } else { | |
208 | sdt_probetab[ndx] = sdp->sdp_hashnext; | |
209 | } | |
210 | ||
211 | kmem_free(sdp->sdp_name, sdp->sdp_namelen); | |
212 | sdp = sdp->sdp_next; | |
213 | kmem_free(old, sizeof(sdt_probe_t)); | |
214 | } | |
215 | } | |
216 | ||
217 | /*ARGSUSED*/ | |
218 | static int | |
219 | sdt_enable(void *arg, dtrace_id_t id, void *parg) | |
220 | { | |
221 | #pragma unused(arg,id) | |
222 | sdt_probe_t *sdp = parg; | |
223 | struct modctl *ctl = sdp->sdp_ctl; | |
224 | ||
225 | ctl->mod_nenabled++; | |
226 | ||
227 | /* | |
228 | * If this module has disappeared since we discovered its probes, | |
229 | * refuse to enable it. | |
230 | */ | |
231 | if (!ctl->mod_loaded) { | |
232 | if (sdt_verbose) { | |
233 | cmn_err(CE_NOTE, "sdt is failing for probe %s " | |
234 | "(module %s unloaded)", | |
235 | sdp->sdp_name, ctl->mod_modname); | |
236 | } | |
237 | goto err; | |
238 | } | |
239 | ||
240 | /* | |
241 | * Now check that our modctl has the expected load count. If it | |
242 | * doesn't, this module must have been unloaded and reloaded -- and | |
243 | * we're not going to touch it. | |
244 | */ | |
245 | if (ctl->mod_loadcnt != sdp->sdp_loadcnt) { | |
246 | if (sdt_verbose) { | |
247 | cmn_err(CE_NOTE, "sdt is failing for probe %s " | |
248 | "(module %s reloaded)", | |
249 | sdp->sdp_name, ctl->mod_modname); | |
250 | } | |
251 | goto err; | |
252 | } | |
253 | ||
254 | dtrace_casptr(&tempDTraceTrapHook, NULL, ptrauth_nop_cast(void *, &fbt_perfCallback)); | |
255 | if (tempDTraceTrapHook != (perfCallback)fbt_perfCallback) { | |
256 | if (sdt_verbose) { | |
257 | cmn_err(CE_NOTE, "sdt_enable is failing for probe %s " | |
258 | "in module %s: tempDTraceTrapHook already occupied.", | |
259 | sdp->sdp_name, ctl->mod_modname); | |
260 | } | |
261 | return 0; | |
262 | } | |
263 | ||
264 | while (sdp != NULL) { | |
265 | (void)ml_nofault_copy((vm_offset_t)&sdp->sdp_patchval, (vm_offset_t)sdp->sdp_patchpoint, | |
266 | (vm_size_t)sizeof(sdp->sdp_patchval)); | |
267 | ||
268 | /* | |
269 | * Make the patched instruction visible via a data + instruction | |
270 | * cache fush on platforms that need it | |
271 | */ | |
272 | flush_dcache((vm_offset_t)sdp->sdp_patchpoint, (vm_size_t)sizeof(sdp->sdp_patchval), 0); | |
273 | invalidate_icache((vm_offset_t)sdp->sdp_patchpoint, (vm_size_t)sizeof(sdp->sdp_patchval), 0); | |
274 | ||
275 | sdp = sdp->sdp_next; | |
276 | } | |
277 | ||
278 | err: | |
279 | return 0; | |
280 | } | |
281 | ||
282 | /*ARGSUSED*/ | |
283 | static void | |
284 | sdt_disable(void *arg, dtrace_id_t id, void *parg) | |
285 | { | |
286 | #pragma unused(arg,id) | |
287 | sdt_probe_t *sdp = parg; | |
288 | struct modctl *ctl = sdp->sdp_ctl; | |
289 | ||
290 | ctl->mod_nenabled--; | |
291 | ||
292 | if (!ctl->mod_loaded || ctl->mod_loadcnt != sdp->sdp_loadcnt) { | |
293 | goto err; | |
294 | } | |
295 | ||
296 | while (sdp != NULL) { | |
297 | (void)ml_nofault_copy((vm_offset_t)&sdp->sdp_savedval, (vm_offset_t)sdp->sdp_patchpoint, | |
298 | (vm_size_t)sizeof(sdp->sdp_savedval)); | |
299 | /* | |
300 | * Make the patched instruction visible via a data + instruction | |
301 | * cache flush on platforms that need it | |
302 | */ | |
303 | flush_dcache((vm_offset_t)sdp->sdp_patchpoint, (vm_size_t)sizeof(sdp->sdp_savedval), 0); | |
304 | invalidate_icache((vm_offset_t)sdp->sdp_patchpoint, (vm_size_t)sizeof(sdp->sdp_savedval), 0); | |
305 | sdp = sdp->sdp_next; | |
306 | } | |
307 | ||
308 | err: | |
309 | ; | |
310 | } | |
311 | ||
312 | static dtrace_pops_t sdt_pops = { | |
313 | .dtps_provide = NULL, | |
314 | .dtps_provide_module = sdt_provide_module, | |
315 | .dtps_enable = sdt_enable, | |
316 | .dtps_disable = sdt_disable, | |
317 | .dtps_suspend = NULL, | |
318 | .dtps_resume = NULL, | |
319 | .dtps_getargdesc = sdt_getargdesc, | |
320 | .dtps_getargval = sdt_getarg, | |
321 | .dtps_usermode = NULL, | |
322 | .dtps_destroy = sdt_destroy, | |
323 | }; | |
324 | ||
325 | /*ARGSUSED*/ | |
326 | static int | |
327 | sdt_attach(dev_info_t *devi) | |
328 | { | |
329 | sdt_provider_t *prov; | |
330 | ||
331 | if (ddi_create_minor_node(devi, "sdt", S_IFCHR, | |
332 | 0, DDI_PSEUDO, 0) == DDI_FAILURE) { | |
333 | cmn_err(CE_NOTE, "/dev/sdt couldn't create minor node"); | |
334 | ddi_remove_minor_node(devi, NULL); | |
335 | return DDI_FAILURE; | |
336 | } | |
337 | ||
338 | if (sdt_probetab_size == 0) { | |
339 | sdt_probetab_size = SDT_PROBETAB_SIZE; | |
340 | } | |
341 | ||
342 | sdt_probetab_mask = sdt_probetab_size - 1; | |
343 | sdt_probetab = | |
344 | kmem_zalloc(sdt_probetab_size * sizeof(sdt_probe_t *), KM_SLEEP); | |
345 | dtrace_invop_add(sdt_invop); | |
346 | ||
347 | for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) { | |
348 | if (dtrace_register(prov->sdtp_name, prov->sdtp_attr, | |
349 | DTRACE_PRIV_KERNEL, NULL, | |
350 | &sdt_pops, prov, &prov->sdtp_id) != 0) { | |
351 | cmn_err(CE_WARN, "failed to register sdt provider %s", | |
352 | prov->sdtp_name); | |
353 | } | |
354 | } | |
355 | ||
356 | return DDI_SUCCESS; | |
357 | } | |
358 | ||
359 | /* | |
360 | * APPLE NOTE: sdt_detach not implemented | |
361 | */ | |
362 | #if !defined(__APPLE__) | |
363 | /*ARGSUSED*/ | |
364 | static int | |
365 | sdt_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) | |
366 | { | |
367 | sdt_provider_t *prov; | |
368 | ||
369 | switch (cmd) { | |
370 | case DDI_DETACH: | |
371 | break; | |
372 | ||
373 | case DDI_SUSPEND: | |
374 | return DDI_SUCCESS; | |
375 | ||
376 | default: | |
377 | return DDI_FAILURE; | |
378 | } | |
379 | ||
380 | for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) { | |
381 | if (prov->sdtp_id != DTRACE_PROVNONE) { | |
382 | if (dtrace_unregister(prov->sdtp_id) != 0) { | |
383 | return DDI_FAILURE; | |
384 | } | |
385 | ||
386 | prov->sdtp_id = DTRACE_PROVNONE; | |
387 | } | |
388 | } | |
389 | ||
390 | dtrace_invop_remove(sdt_invop); | |
391 | kmem_free(sdt_probetab, sdt_probetab_size * sizeof(sdt_probe_t *)); | |
392 | ||
393 | return DDI_SUCCESS; | |
394 | } | |
395 | #endif /* __APPLE__ */ | |
396 | ||
397 | d_open_t _sdt_open; | |
398 | ||
399 | int | |
400 | _sdt_open(dev_t dev, int flags, int devtype, struct proc *p) | |
401 | { | |
402 | #pragma unused(dev,flags,devtype,p) | |
403 | return 0; | |
404 | } | |
405 | ||
406 | #define SDT_MAJOR -24 /* let the kernel pick the device number */ | |
407 | ||
408 | static const struct cdevsw sdt_cdevsw = | |
409 | { | |
410 | .d_open = _sdt_open, | |
411 | .d_close = eno_opcl, | |
412 | .d_read = eno_rdwrt, | |
413 | .d_write = eno_rdwrt, | |
414 | .d_ioctl = eno_ioctl, | |
415 | .d_stop = (stop_fcn_t *)nulldev, | |
416 | .d_reset = (reset_fcn_t *)nulldev, | |
417 | .d_select = eno_select, | |
418 | .d_mmap = eno_mmap, | |
419 | .d_strategy = eno_strat, | |
420 | .d_reserved_1 = eno_getc, | |
421 | .d_reserved_2 = eno_putc, | |
422 | }; | |
423 | ||
424 | static struct modctl g_sdt_kernctl; | |
425 | static struct module g_sdt_mach_module; | |
426 | ||
427 | #include <mach-o/nlist.h> | |
428 | #include <libkern/kernel_mach_header.h> | |
429 | ||
430 | /* | |
431 | * Represents single record in __DATA,__sdt section. | |
432 | */ | |
433 | typedef struct dtrace_sdt_def { | |
434 | uintptr_t dsd_addr; /* probe site location */ | |
435 | const char *dsd_prov; /* provider's name */ | |
436 | const char *dsd_name; /* probe's name */ | |
437 | } __attribute__((__packed__)) dtrace_sdt_def_t; | |
438 | ||
439 | /* | |
440 | * Creates a copy of name and unescapes '-' characters. | |
441 | */ | |
442 | static char * | |
443 | sdt_strdup_name(const char *name) | |
444 | { | |
445 | size_t len = strlen(name) + 1; | |
446 | size_t i, j; | |
447 | char *nname = kmem_alloc(len, KM_SLEEP); | |
448 | ||
449 | for (i = 0, j = 0; name[j] != '\0'; i++) { | |
450 | if (name[j] == '_' && name[j + 1] == '_') { | |
451 | nname[i] = '-'; | |
452 | j += 2; | |
453 | } else { | |
454 | nname[i] = name[j++]; | |
455 | } | |
456 | } | |
457 | ||
458 | nname[i] = '\0'; | |
459 | return nname; | |
460 | } | |
461 | ||
462 | void | |
463 | sdt_early_init( void ) | |
464 | { | |
465 | if (dtrace_sdt_probes_restricted()) { | |
466 | return; | |
467 | } | |
468 | if (MH_MAGIC_KERNEL != _mh_execute_header.magic) { | |
469 | g_sdt_kernctl.mod_address = (vm_address_t)NULL; | |
470 | g_sdt_kernctl.mod_size = 0; | |
471 | } else { | |
472 | kernel_mach_header_t *mh; | |
473 | struct load_command *cmd; | |
474 | kernel_segment_command_t *orig_ts = NULL, *orig_le = NULL; | |
475 | kernel_section_t *orig_dt = NULL; | |
476 | struct symtab_command *orig_st = NULL; | |
477 | kernel_nlist_t *sym = NULL; | |
478 | char *strings; | |
479 | unsigned int i; | |
480 | unsigned int len; | |
481 | ||
482 | g_sdt_mach_module.sdt_nprobes = 0; | |
483 | g_sdt_mach_module.sdt_probes = NULL; | |
484 | ||
485 | g_sdt_kernctl.mod_address = (vm_address_t)&g_sdt_mach_module; | |
486 | g_sdt_kernctl.mod_size = 0; | |
487 | strncpy((char *)&(g_sdt_kernctl.mod_modname), "mach_kernel", KMOD_MAX_NAME); | |
488 | ||
489 | g_sdt_kernctl.mod_next = NULL; | |
490 | g_sdt_kernctl.mod_stale = NULL; | |
491 | g_sdt_kernctl.mod_id = 0; | |
492 | g_sdt_kernctl.mod_loadcnt = 1; | |
493 | g_sdt_kernctl.mod_loaded = 1; | |
494 | g_sdt_kernctl.mod_flags = 0; | |
495 | g_sdt_kernctl.mod_nenabled = 0; | |
496 | ||
497 | mh = &_mh_execute_header; | |
498 | cmd = (struct load_command*) &mh[1]; | |
499 | for (i = 0; i < mh->ncmds; i++) { | |
500 | if (cmd->cmd == LC_SEGMENT_KERNEL) { | |
501 | kernel_segment_command_t *orig_sg = (kernel_segment_command_t *) cmd; | |
502 | ||
503 | if (LIT_STRNEQL(orig_sg->segname, SEG_TEXT)) { | |
504 | orig_ts = orig_sg; | |
505 | } else if (LIT_STRNEQL(orig_sg->segname, SEG_LINKEDIT)) { | |
506 | orig_le = orig_sg; | |
507 | } else if (LIT_STRNEQL(orig_sg->segname, "")) { | |
508 | orig_ts = orig_sg; /* kexts have a single unnamed segment */ | |
509 | } | |
510 | } else if (cmd->cmd == LC_SYMTAB) { | |
511 | orig_st = (struct symtab_command *) cmd; | |
512 | } | |
513 | ||
514 | cmd = (struct load_command *) ((uintptr_t) cmd + cmd->cmdsize); | |
515 | } | |
516 | ||
517 | /* Locate DTrace SDT section in the object. */ | |
518 | if ((orig_dt = getsectbyname("__DATA", "__sdt")) == NULL) { | |
519 | printf("DTrace section not found.\n"); | |
520 | return; | |
521 | } | |
522 | ||
523 | if ((orig_ts == NULL) || (orig_st == NULL) || (orig_le == NULL)) { | |
524 | return; | |
525 | } | |
526 | ||
527 | sym = (kernel_nlist_t *)(orig_le->vmaddr + orig_st->symoff - orig_le->fileoff); | |
528 | strings = (char *)(orig_le->vmaddr + orig_st->stroff - orig_le->fileoff); | |
529 | ||
530 | /* | |
531 | * Iterate over SDT section and establish all SDT probes. | |
532 | */ | |
533 | dtrace_sdt_def_t *sdtdef = (dtrace_sdt_def_t *)(orig_dt->addr); | |
534 | for (size_t k = 0; k < orig_dt->size / sizeof(dtrace_sdt_def_t); k++, sdtdef++) { | |
535 | const char *funcname; | |
536 | unsigned long best; | |
537 | ||
538 | sdt_probedesc_t *sdpd = kmem_alloc(sizeof(sdt_probedesc_t), KM_SLEEP); | |
539 | ||
540 | /* Unescape probe name and keep a note of the size of original memory allocation. */ | |
541 | sdpd->sdpd_name = sdt_strdup_name(sdtdef->dsd_name); | |
542 | sdpd->sdpd_namelen = strlen(sdtdef->dsd_name) + 1; | |
543 | ||
544 | /* Used only for provider structure lookup so there is no need to make dynamic copy. */ | |
545 | sdpd->sdpd_prov = sdtdef->dsd_prov; | |
546 | ||
547 | /* | |
548 | * Find the symbol immediately preceding the sdt probe site just discovered, | |
549 | * that symbol names the function containing the sdt probe. | |
550 | */ | |
551 | funcname = "<unknown>"; | |
552 | for (i = 0; i < orig_st->nsyms; i++) { | |
553 | uint8_t jn_type = sym[i].n_type & N_TYPE; | |
554 | char *jname = strings + sym[i].n_un.n_strx; | |
555 | ||
556 | if ((N_SECT != jn_type && N_ABS != jn_type)) { | |
557 | continue; | |
558 | } | |
559 | ||
560 | if (0 == sym[i].n_un.n_strx) { /* iff a null, "", name. */ | |
561 | continue; | |
562 | } | |
563 | ||
564 | if (*jname == '_') { | |
565 | jname += 1; | |
566 | } | |
567 | ||
568 | if (sdtdef->dsd_addr <= (unsigned long)sym[i].n_value) { | |
569 | continue; | |
570 | } | |
571 | ||
572 | if ((unsigned long)sym[i].n_value > best) { | |
573 | best = (unsigned long)sym[i].n_value; | |
574 | funcname = jname; | |
575 | } | |
576 | } | |
577 | ||
578 | len = strlen(funcname) + 1; | |
579 | sdpd->sdpd_func = kmem_alloc(len, KM_SLEEP); | |
580 | strncpy(sdpd->sdpd_func, funcname, len); | |
581 | ||
582 | sdpd->sdpd_offset = sdtdef->dsd_addr; | |
583 | #if defined(__arm__) | |
584 | /* PR8353094 - mask off thumb-bit */ | |
585 | sdpd->sdpd_offset &= ~0x1U; | |
586 | #elif defined(__arm64__) | |
587 | sdpd->sdpd_offset &= ~0x1LU; | |
588 | #endif /* __arm__ */ | |
589 | ||
590 | sdpd->sdpd_next = g_sdt_mach_module.sdt_probes; | |
591 | g_sdt_mach_module.sdt_probes = sdpd; | |
592 | } | |
593 | } | |
594 | } | |
595 | ||
596 | void | |
597 | sdt_init( void ) | |
598 | { | |
599 | int majdevno = cdevsw_add(SDT_MAJOR, &sdt_cdevsw); | |
600 | ||
601 | if (majdevno < 0) { | |
602 | printf("sdt_init: failed to allocate a major number!\n"); | |
603 | return; | |
604 | } | |
605 | ||
606 | if (dtrace_sdt_probes_restricted()) { | |
607 | return; | |
608 | } | |
609 | ||
610 | sdt_attach((dev_info_t*)(uintptr_t)majdevno); | |
611 | } | |
612 | ||
613 | #undef SDT_MAJOR | |
614 | ||
615 | /*ARGSUSED*/ | |
616 | void | |
617 | sdt_provide_module(void *arg, struct modctl *ctl) | |
618 | { | |
619 | #pragma unused(arg) | |
620 | ASSERT(ctl != NULL); | |
621 | ASSERT(dtrace_kernel_symbol_mode != DTRACE_KERNEL_SYMBOLS_NEVER); | |
622 | LCK_MTX_ASSERT(&mod_lock, LCK_MTX_ASSERT_OWNED); | |
623 | ||
624 | if (MOD_SDT_DONE(ctl)) { | |
625 | return; | |
626 | } | |
627 | ||
628 | if (MOD_IS_MACH_KERNEL(ctl)) { | |
629 | __sdt_provide_module(arg, &g_sdt_kernctl); | |
630 | ||
631 | sdt_probedesc_t *sdpd = g_sdt_mach_module.sdt_probes; | |
632 | while (sdpd) { | |
633 | sdt_probedesc_t *this_sdpd = sdpd; | |
634 | kmem_free((void *)sdpd->sdpd_name, sdpd->sdpd_namelen); | |
635 | kmem_free((void *)sdpd->sdpd_func, strlen(sdpd->sdpd_func) + 1); | |
636 | sdpd = sdpd->sdpd_next; | |
637 | kmem_free((void *)this_sdpd, sizeof(sdt_probedesc_t)); | |
638 | } | |
639 | g_sdt_mach_module.sdt_probes = NULL; | |
640 | } else { | |
641 | /* | |
642 | * APPLE NOTE: sdt probes for kexts not yet implemented | |
643 | */ | |
644 | } | |
645 | ||
646 | /* Need to mark this module as completed */ | |
647 | ctl->mod_flags |= MODCTL_SDT_PROBES_PROVIDED; | |
648 | } |