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 * Portions Copyright 2006-2008 John Birrell jb@freebsd.org 22 * 23 * $FreeBSD$ 24 * 25 */ 26 27 #include "opt_kdtrace.h" 28 29 #include <sys/cdefs.h> 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 33 #include <sys/conf.h> 34 #include <sys/eventhandler.h> 35 #include <sys/kernel.h> 36 #include <sys/limits.h> 37 #include <sys/linker.h> 38 #include <sys/linker_set.h> 39 #include <sys/lock.h> 40 #include <sys/malloc.h> 41 #include <sys/module.h> 42 #include <sys/mutex.h> 43 #include <sys/queue.h> 44 #include <sys/sdt.h> 45 46 #include <sys/dtrace.h> 47 #include <sys/dtrace_bsd.h> 48 49 /* DTrace methods. */ 50 static void sdt_getargdesc(void *, dtrace_id_t, void *, dtrace_argdesc_t *); 51 static void sdt_provide_probes(void *, dtrace_probedesc_t *); 52 static void sdt_destroy(void *, dtrace_id_t, void *); 53 static void sdt_enable(void *, dtrace_id_t, void *); 54 static void sdt_disable(void *, dtrace_id_t, void *); 55 56 static d_open_t sdt_open; 57 static void sdt_load(void *); 58 static int sdt_unload(void *); 59 static void sdt_create_provider(struct sdt_provider *); 60 static void sdt_create_probe(struct sdt_probe *); 61 static void sdt_kld_load(void *, struct linker_file *); 62 static void sdt_kld_unload(void *, struct linker_file *, int *); 63 64 static MALLOC_DEFINE(M_SDT, "SDT", "DTrace SDT providers"); 65 66 static struct cdevsw sdt_cdevsw = { 67 .d_version = D_VERSION, 68 .d_open = sdt_open, 69 .d_name = "sdt", 70 }; 71 72 static dtrace_pattr_t sdt_attr = { 73 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, 74 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 75 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA }, 76 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, 77 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA }, 78 }; 79 80 static dtrace_pops_t sdt_pops = { 81 sdt_provide_probes, 82 NULL, 83 sdt_enable, 84 sdt_disable, 85 NULL, 86 NULL, 87 sdt_getargdesc, 88 NULL, 89 NULL, 90 sdt_destroy, 91 }; 92 93 static struct cdev *sdt_cdev; 94 95 static TAILQ_HEAD(, sdt_provider) sdt_prov_list; 96 97 eventhandler_tag sdt_kld_load_tag; 98 eventhandler_tag sdt_kld_unload_tag; 99 100 static void 101 sdt_create_provider(struct sdt_provider *prov) 102 { 103 struct sdt_provider *curr, *newprov; 104 105 TAILQ_FOREACH(curr, &sdt_prov_list, prov_entry) 106 if (strcmp(prov->name, curr->name) == 0) { 107 /* The provider has already been defined. */ 108 curr->sdt_refs++; 109 return; 110 } 111 112 /* 113 * Make a copy of prov so that we don't lose fields if its module is 114 * unloaded but the provider isn't destroyed. This could happen with 115 * a provider that spans multiple modules. 116 */ 117 newprov = malloc(sizeof(*newprov), M_SDT, M_WAITOK | M_ZERO); 118 newprov->name = strdup(prov->name, M_SDT); 119 prov->sdt_refs = newprov->sdt_refs = 1; 120 TAILQ_INIT(&newprov->probe_list); 121 122 TAILQ_INSERT_TAIL(&sdt_prov_list, newprov, prov_entry); 123 124 (void)dtrace_register(newprov->name, &sdt_attr, DTRACE_PRIV_USER, NULL, 125 &sdt_pops, NULL, (dtrace_provider_id_t *)&newprov->id); 126 prov->id = newprov->id; 127 } 128 129 static void 130 sdt_create_probe(struct sdt_probe *probe) 131 { 132 struct sdt_provider *prov; 133 char mod[DTRACE_MODNAMELEN]; 134 char func[DTRACE_FUNCNAMELEN]; 135 char name[DTRACE_NAMELEN]; 136 size_t len; 137 138 TAILQ_FOREACH(prov, &sdt_prov_list, prov_entry) 139 if (strcmp(prov->name, probe->prov->name) == 0) 140 break; 141 142 KASSERT(prov != NULL, ("probe defined without a provider")); 143 144 /* If no module name was specified, use the module filename. */ 145 if (*probe->mod == 0) { 146 len = strlcpy(mod, probe->sdtp_lf->filename, sizeof(mod)); 147 if (len > 3 && strcmp(mod + len - 3, ".ko") == 0) 148 mod[len - 3] = '\0'; 149 } else 150 strlcpy(mod, probe->mod, sizeof(mod)); 151 152 /* 153 * Unfortunately this is necessary because the Solaris DTrace 154 * code mixes consts and non-consts with casts to override 155 * the incompatibilies. On FreeBSD, we use strict warnings 156 * in the C compiler, so we have to respect const vs non-const. 157 */ 158 strlcpy(func, probe->func, sizeof(func)); 159 strlcpy(name, probe->name, sizeof(name)); 160 161 if (dtrace_probe_lookup(prov->id, mod, func, name) != DTRACE_IDNONE) 162 return; 163 164 TAILQ_INSERT_TAIL(&prov->probe_list, probe, probe_entry); 165 166 (void)dtrace_probe_create(prov->id, mod, func, name, 1, probe); 167 } 168 169 /* Probes are created through the SDT module load/unload hook. */ 170 static void 171 sdt_provide_probes(void *arg, dtrace_probedesc_t *desc) 172 { 173 } 174 175 static void 176 sdt_enable(void *arg __unused, dtrace_id_t id, void *parg) 177 { 178 struct sdt_probe *probe = parg; 179 180 probe->id = id; 181 probe->sdtp_lf->nenabled++; 182 } 183 184 static void 185 sdt_disable(void *arg __unused, dtrace_id_t id, void *parg) 186 { 187 struct sdt_probe *probe = parg; 188 189 KASSERT(probe->sdtp_lf->nenabled > 0, ("no probes enabled")); 190 191 probe->id = 0; 192 probe->sdtp_lf->nenabled--; 193 } 194 195 static void 196 sdt_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc) 197 { 198 struct sdt_argtype *argtype; 199 struct sdt_probe *probe = parg; 200 201 if (desc->dtargd_ndx < probe->n_args) { 202 TAILQ_FOREACH(argtype, &probe->argtype_list, argtype_entry) { 203 if (desc->dtargd_ndx == argtype->ndx) { 204 /* XXX */ 205 desc->dtargd_mapping = desc->dtargd_ndx; 206 strlcpy(desc->dtargd_native, argtype->type, 207 sizeof(desc->dtargd_native)); 208 desc->dtargd_xlate[0] = '\0'; /* XXX */ 209 } 210 } 211 } else 212 desc->dtargd_ndx = DTRACE_ARGNONE; 213 } 214 215 static void 216 sdt_destroy(void *arg, dtrace_id_t id, void *parg) 217 { 218 struct sdt_probe *probe; 219 220 probe = parg; 221 TAILQ_REMOVE(&probe->prov->probe_list, probe, probe_entry); 222 } 223 224 /* 225 * Called from the kernel linker when a module is loaded, before 226 * dtrace_module_loaded() is called. This is done so that it's possible to 227 * register new providers when modules are loaded. We cannot do this in the 228 * provide_module method since it's called with the provider lock held 229 * and dtrace_register() will try to acquire it again. 230 */ 231 static void 232 sdt_kld_load(void *arg __unused, struct linker_file *lf) 233 { 234 struct sdt_provider **prov, **begin, **end; 235 struct sdt_probe **probe, **p_begin, **p_end; 236 struct sdt_argtype **argtype, **a_begin, **a_end; 237 238 if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end, NULL)) 239 return; 240 for (prov = begin; prov < end; prov++) 241 sdt_create_provider(*prov); 242 243 if (linker_file_lookup_set(lf, "sdt_probes_set", &p_begin, &p_end, 244 NULL)) 245 return; 246 for (probe = p_begin; probe < p_end; probe++) { 247 (*probe)->sdtp_lf = lf; 248 sdt_create_probe(*probe); 249 TAILQ_INIT(&(*probe)->argtype_list); 250 } 251 252 if (linker_file_lookup_set(lf, "sdt_argtypes_set", &a_begin, &a_end, 253 NULL)) 254 return; 255 for (argtype = a_begin; argtype < a_end; argtype++) { 256 (*argtype)->probe->n_args++; 257 TAILQ_INSERT_TAIL(&(*argtype)->probe->argtype_list, *argtype, 258 argtype_entry); 259 } 260 } 261 262 static void 263 sdt_kld_unload(void *arg __unused, struct linker_file *lf, int *error __unused) 264 { 265 struct sdt_provider *prov, **curr, **begin, **end, *tmp; 266 267 if (*error != 0) 268 /* We already have an error, so don't do anything. */ 269 return; 270 else if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end, NULL)) 271 /* No DTrace providers are declared in this file. */ 272 return; 273 274 /* 275 * Go through all the providers declared in this linker file and 276 * unregister any that aren't declared in another loaded file. 277 */ 278 for (curr = begin; curr < end; curr++) { 279 TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) { 280 if (strcmp(prov->name, (*curr)->name) == 0) { 281 if (prov->sdt_refs == 1) { 282 TAILQ_REMOVE(&sdt_prov_list, prov, 283 prov_entry); 284 dtrace_unregister(prov->id); 285 free(prov->name, M_SDT); 286 free(prov, M_SDT); 287 } else 288 prov->sdt_refs--; 289 break; 290 } 291 } 292 } 293 } 294 295 static int 296 sdt_linker_file_cb(linker_file_t lf, void *arg __unused) 297 { 298 299 sdt_kld_load(NULL, lf); 300 301 return (0); 302 } 303 304 static void 305 sdt_load(void *arg __unused) 306 { 307 308 TAILQ_INIT(&sdt_prov_list); 309 310 /* Create the /dev/dtrace/sdt entry. */ 311 sdt_cdev = make_dev(&sdt_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, 312 "dtrace/sdt"); 313 314 sdt_probe_func = dtrace_probe; 315 316 sdt_kld_load_tag = EVENTHANDLER_REGISTER(kld_load, sdt_kld_load, NULL, 317 EVENTHANDLER_PRI_ANY); 318 sdt_kld_unload_tag = EVENTHANDLER_REGISTER(kld_unload, sdt_kld_unload, 319 NULL, EVENTHANDLER_PRI_ANY); 320 321 /* Pick up probes from the kernel and already-loaded linker files. */ 322 linker_file_foreach(sdt_linker_file_cb, NULL); 323 } 324 325 static int 326 sdt_unload(void *arg __unused) 327 { 328 struct sdt_provider *prov, *tmp; 329 330 EVENTHANDLER_DEREGISTER(kld_load, sdt_kld_load_tag); 331 EVENTHANDLER_DEREGISTER(kld_unload, sdt_kld_unload_tag); 332 333 sdt_probe_func = sdt_probe_stub; 334 335 TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) { 336 TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry); 337 dtrace_unregister(prov->id); 338 free(prov->name, M_SDT); 339 free(prov, M_SDT); 340 } 341 342 destroy_dev(sdt_cdev); 343 344 return (0); 345 } 346 347 /* ARGSUSED */ 348 static int 349 sdt_modevent(module_t mod __unused, int type, void *data __unused) 350 { 351 int error = 0; 352 353 switch (type) { 354 case MOD_LOAD: 355 break; 356 357 case MOD_UNLOAD: 358 break; 359 360 case MOD_SHUTDOWN: 361 break; 362 363 default: 364 error = EOPNOTSUPP; 365 break; 366 } 367 368 return (error); 369 } 370 371 /* ARGSUSED */ 372 static int 373 sdt_open(struct cdev *dev __unused, int oflags __unused, int devtype __unused, 374 struct thread *td __unused) 375 { 376 377 return (0); 378 } 379 380 SYSINIT(sdt_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, sdt_load, NULL); 381 SYSUNINIT(sdt_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, sdt_unload, NULL); 382 383 DEV_MODULE(sdt, sdt_modevent, NULL); 384 MODULE_VERSION(sdt, 1); 385 MODULE_DEPEND(sdt, dtrace, 1, 1, 1); 386 MODULE_DEPEND(sdt, opensolaris, 1, 1, 1); 387