2 * Copyright (C) 2011 - David Goulet <dgoulet@efficios.com>
3 * 2014 - Jan Glauber <jan.glauber@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <common/common.h>
26 #include <common/utils.h>
29 #include "kern-modules.h"
31 #define LTTNG_MOD_REQUIRED 1
32 #define LTTNG_MOD_OPTIONAL 0
34 /* LTTng kernel tracer mandatory core modules list */
35 struct kern_modules_param kern_modules_control_core
[] = {
36 { "lttng-tracer" }, /* MUST be loaded first so keep at top */
37 { "lttng-lib-ring-buffer" },
38 { "lttng-ring-buffer-client-discard" },
39 { "lttng-ring-buffer-client-overwrite" },
40 { "lttng-ring-buffer-metadata-client" },
41 { "lttng-ring-buffer-client-mmap-discard" },
42 { "lttng-ring-buffer-client-mmap-overwrite" },
43 { "lttng-ring-buffer-metadata-mmap-client" },
46 /* LTTng kernel tracer optional base modules list */
47 struct kern_modules_param kern_modules_control_opt
[] = {
51 { "lttng-kretprobes" },
54 /* LTTng kernel tracer probe modules list */
55 struct kern_modules_param kern_modules_probes_default
[] = {
56 { "lttng-probe-asoc" },
57 { "lttng-probe-block" },
58 { "lttng-probe-btrfs" },
59 { "lttng-probe-compaction" },
60 { "lttng-probe-ext3" },
61 { "lttng-probe-ext4" },
62 { "lttng-probe-gpio" },
63 { "lttng-probe-irq" },
64 { "lttng-probe-jbd" },
65 { "lttng-probe-jbd2" },
66 { "lttng-probe-kmem" },
67 { "lttng-probe-kvm" },
68 { "lttng-probe-kvm-x86" },
69 { "lttng-probe-kvm-x86-mmu" },
70 { "lttng-probe-lock" },
71 { "lttng-probe-module" },
72 { "lttng-probe-napi" },
73 { "lttng-probe-net" },
74 { "lttng-probe-power" },
75 { "lttng-probe-printk" },
76 { "lttng-probe-random" },
77 { "lttng-probe-rcu" },
78 { "lttng-probe-regmap" },
79 { "lttng-probe-regulator" },
80 { "lttng-probe-rpm" },
81 { "lttng-probe-sched" },
82 { "lttng-probe-scsi" },
83 { "lttng-probe-signal" },
84 { "lttng-probe-skb" },
85 { "lttng-probe-sock" },
86 { "lttng-probe-statedump" },
87 { "lttng-probe-sunrpc" },
88 { "lttng-probe-timer" },
89 { "lttng-probe-udp" },
90 { "lttng-probe-vmscan" },
91 { "lttng-probe-v4l2" },
92 { "lttng-probe-workqueue" },
93 { "lttng-probe-writeback" },
94 { "lttng-probe-x86-irq-vectors" },
95 { "lttng-probe-x86-exceptions" },
98 /* dynamic probe modules list */
99 static struct kern_modules_param
*probes
;
100 static int nr_probes
;
101 static int probes_capacity
;
103 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
104 int entries
, int required
)
109 for (i
= entries
- 1; i
>= 0; i
--) {
110 ret
= snprintf(modprobe
, sizeof(modprobe
),
111 "/sbin/modprobe -r -q %s",
114 PERROR("snprintf modprobe -r");
117 modprobe
[sizeof(modprobe
) - 1] = '\0';
118 ret
= system(modprobe
);
120 ERR("Unable to launch modprobe -r for module %s",
122 } else if (required
&& WEXITSTATUS(ret
) != 0) {
123 ERR("Unable to remove module %s",
126 DBG("Modprobe removal successful %s",
133 * Remove control kernel module(s) in reverse load order.
135 void modprobe_remove_lttng_control(void)
137 modprobe_remove_lttng(kern_modules_control_opt
,
138 ARRAY_SIZE(kern_modules_control_opt
),
140 modprobe_remove_lttng(kern_modules_control_core
,
141 ARRAY_SIZE(kern_modules_control_core
),
145 static void free_probes(void)
152 for (i
= 0; i
< nr_probes
; ++i
) {
153 free(probes
[i
].name
);
161 * Remove data kernel modules in reverse load order.
163 void modprobe_remove_lttng_data(void)
168 modprobe_remove_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);
173 * Remove all kernel modules in reverse order.
175 void modprobe_remove_lttng_all(void)
177 modprobe_remove_lttng_data();
178 modprobe_remove_lttng_control();
183 static void log_kmod(void *data
, int priority
, const char *file
, int line
,
184 const char *fn
, const char *format
, va_list args
)
188 if (vasprintf(&str
, format
, args
) < 0) {
192 DBG("libkmod: %s", str
);
195 static int modprobe_lttng(struct kern_modules_param
*modules
,
196 int entries
, int required
)
199 struct kmod_ctx
*ctx
;
201 ctx
= kmod_new(NULL
, NULL
);
203 PERROR("Unable to create kmod library context");
208 kmod_set_log_fn(ctx
, log_kmod
, NULL
);
209 kmod_load_resources(ctx
);
211 for (i
= 0; i
< entries
; i
++) {
212 struct kmod_module
*mod
= NULL
;
214 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
216 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
220 ret
= kmod_module_probe_insert_module(mod
, KMOD_PROBE_IGNORE_LOADED
,
221 NULL
, NULL
, NULL
, NULL
);
224 ERR("Unable to load required module %s",
228 DBG("Unable to load optional module %s; continuing",
233 DBG("Modprobe successfully %s", modules
[i
].name
);
236 kmod_module_unref(mod
);
246 #else /* HAVE_KMOD */
248 static int modprobe_lttng(struct kern_modules_param
*modules
,
249 int entries
, int required
)
254 for (i
= 0; i
< entries
; i
++) {
255 ret
= snprintf(modprobe
, sizeof(modprobe
),
256 "/sbin/modprobe %s%s",
257 required
? "" : "-q ",
260 PERROR("snprintf modprobe");
263 modprobe
[sizeof(modprobe
) - 1] = '\0';
264 ret
= system(modprobe
);
267 ERR("Unable to launch modprobe for required module %s",
271 DBG("Unable to launch modprobe for optional module %s; continuing",
275 } else if (WEXITSTATUS(ret
) != 0) {
277 ERR("Unable to load required module %s",
281 DBG("Unable to load optional module %s; continuing",
286 DBG("Modprobe successfully %s", modules
[i
].name
);
294 #endif /* HAVE_KMOD */
297 * Load control kernel module(s).
299 int modprobe_lttng_control(void)
303 ret
= modprobe_lttng(kern_modules_control_core
,
304 ARRAY_SIZE(kern_modules_control_core
),
308 ret
= modprobe_lttng(kern_modules_control_opt
,
309 ARRAY_SIZE(kern_modules_control_opt
),
315 * Grow global list of probes (double capacity or set it to 1 if
316 * currently 0 and copy existing data).
318 static int grow_probes(void)
321 struct kern_modules_param
*tmp_probes
;
323 /* Initialize capacity to 1 if 0. */
324 if (probes_capacity
== 0) {
325 probes
= zmalloc(sizeof(*probes
));
327 PERROR("malloc probe list");
336 probes_capacity
*= 2;
338 tmp_probes
= zmalloc(sizeof(*tmp_probes
) * probes_capacity
);
340 PERROR("malloc probe list");
344 for (i
= 0; i
< nr_probes
; ++i
) {
345 /* Move name pointer. */
346 tmp_probes
[i
].name
= probes
[i
].name
;
349 /* Replace probes with larger copy. */
357 * Appends a comma-separated list of probes to the global list
360 static int append_list_to_probes(const char *list
)
364 char *tmp_list
, *cur_list
;
368 cur_list
= tmp_list
= strdup(list
);
370 PERROR("strdup temp list");
376 struct kern_modules_param
*cur_mod
;
378 next
= strtok(cur_list
, ",");
384 /* filter leading spaces */
385 while (*next
== ' ') {
389 if (probes_capacity
<= nr_probes
) {
396 /* Length 13 is "lttng-probe-" + \0 */
397 name_len
= strlen(next
) + 13;
399 cur_mod
= &probes
[nr_probes
];
400 cur_mod
->name
= zmalloc(name_len
);
401 if (!cur_mod
->name
) {
402 PERROR("malloc probe list");
407 ret
= snprintf(cur_mod
->name
, name_len
, "lttng-probe-%s", next
);
409 PERROR("snprintf modprobe name");
427 * Load data kernel module(s).
429 int modprobe_lttng_data(void)
435 * Base probes: either from command line option, environment
436 * variable or default list.
438 if (kmod_probes_list
) {
439 list
= kmod_probes_list
;
441 list
= utils_get_kmod_probes_list();
445 /* User-specified probes. */
446 ret
= append_list_to_probes(list
);
451 /* Default probes. */
452 int def_len
= ARRAY_SIZE(kern_modules_probes_default
);
454 probes
= zmalloc(sizeof(*probes
) * def_len
);
456 PERROR("malloc probe list");
460 nr_probes
= probes_capacity
= def_len
;
462 for (i
= 0; i
< def_len
; ++i
) {
463 char* name
= strdup(kern_modules_probes_default
[i
].name
);
466 PERROR("strdup probe item");
471 probes
[i
].name
= name
;
476 * Extra modules? Append them to current probes list.
478 if (kmod_extra_probes_list
) {
479 list
= kmod_extra_probes_list
;
481 list
= utils_get_extra_kmod_probes_list();
485 ret
= append_list_to_probes(list
);
492 * Load probes modules now.
494 ret
= modprobe_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);