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