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" },
96 /* dynamic probe modules list */
97 static struct kern_modules_param
*probes
;
99 static int probes_capacity
;
101 void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
102 int entries
, int required
)
107 for (i
= entries
- 1; i
>= 0; i
--) {
108 ret
= snprintf(modprobe
, sizeof(modprobe
),
109 "/sbin/modprobe -r -q %s",
112 PERROR("snprintf modprobe -r");
115 modprobe
[sizeof(modprobe
) - 1] = '\0';
116 ret
= system(modprobe
);
118 ERR("Unable to launch modprobe -r for module %s",
120 } else if (required
&& WEXITSTATUS(ret
) != 0) {
121 ERR("Unable to remove module %s",
124 DBG("Modprobe removal successful %s",
131 * Remove control kernel module(s) in reverse load order.
133 void modprobe_remove_lttng_control(void)
135 modprobe_remove_lttng(kern_modules_control_opt
,
136 ARRAY_SIZE(kern_modules_control_opt
),
138 modprobe_remove_lttng(kern_modules_control_core
,
139 ARRAY_SIZE(kern_modules_control_core
),
143 static void free_probes(void)
150 for (i
= 0; i
< nr_probes
; ++i
) {
151 free(probes
[i
].name
);
159 * Remove data kernel modules in reverse load order.
161 void modprobe_remove_lttng_data(void)
166 modprobe_remove_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);
171 * Remove all kernel modules in reverse order.
173 void modprobe_remove_lttng_all(void)
175 modprobe_remove_lttng_data();
176 modprobe_remove_lttng_control();
181 static void log_kmod(void *data
, int priority
, const char *file
, int line
,
182 const char *fn
, const char *format
, va_list args
)
186 if (vasprintf(&str
, format
, args
) < 0) {
190 DBG("libkmod: %s", str
);
193 static int modprobe_lttng(struct kern_modules_param
*modules
,
194 int entries
, int required
)
197 struct kmod_ctx
*ctx
;
199 ctx
= kmod_new(NULL
, NULL
);
201 PERROR("Unable to create kmod library context");
206 kmod_set_log_fn(ctx
, log_kmod
, NULL
);
207 kmod_load_resources(ctx
);
209 for (i
= 0; i
< entries
; i
++) {
210 struct kmod_module
*mod
= NULL
;
212 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
214 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
218 ret
= kmod_module_probe_insert_module(mod
, KMOD_PROBE_IGNORE_LOADED
,
219 NULL
, NULL
, NULL
, NULL
);
222 ERR("Unable to load required module %s",
226 DBG("Unable to load optional module %s; continuing",
231 DBG("Modprobe successfully %s", modules
[i
].name
);
234 kmod_module_unref(mod
);
244 #else /* HAVE_KMOD */
246 static int modprobe_lttng(struct kern_modules_param
*modules
,
247 int entries
, int required
)
252 for (i
= 0; i
< entries
; i
++) {
253 ret
= snprintf(modprobe
, sizeof(modprobe
),
254 "/sbin/modprobe %s%s",
255 required
? "" : "-q ",
258 PERROR("snprintf modprobe");
261 modprobe
[sizeof(modprobe
) - 1] = '\0';
262 ret
= system(modprobe
);
265 ERR("Unable to launch modprobe for required module %s",
269 DBG("Unable to launch modprobe for optional module %s; continuing",
273 } else if (WEXITSTATUS(ret
) != 0) {
275 ERR("Unable to load required module %s",
279 DBG("Unable to load optional module %s; continuing",
284 DBG("Modprobe successfully %s", modules
[i
].name
);
292 #endif /* HAVE_KMOD */
295 * Load control kernel module(s).
297 int modprobe_lttng_control(void)
301 ret
= modprobe_lttng(kern_modules_control_core
,
302 ARRAY_SIZE(kern_modules_control_core
),
306 ret
= modprobe_lttng(kern_modules_control_opt
,
307 ARRAY_SIZE(kern_modules_control_opt
),
313 * Grow global list of probes (double capacity or set it to 1 if
314 * currently 0 and copy existing data).
316 static int grow_probes(void)
319 struct kern_modules_param
*tmp_probes
;
321 /* Initialize capacity to 1 if 0. */
322 if (probes_capacity
== 0) {
323 probes
= zmalloc(sizeof(*probes
));
325 PERROR("malloc probe list");
334 probes_capacity
*= 2;
336 tmp_probes
= zmalloc(sizeof(*tmp_probes
) * probes_capacity
);
338 PERROR("malloc probe list");
342 for (i
= 0; i
< nr_probes
; ++i
) {
343 /* Move name pointer. */
344 tmp_probes
[i
].name
= probes
[i
].name
;
347 /* Replace probes with larger copy. */
355 * Appends a comma-separated list of probes to the global list
358 static int append_list_to_probes(const char *list
)
361 int index
= nr_probes
, ret
;
362 char *tmp_list
, *cur_list
;
366 cur_list
= tmp_list
= strdup(list
);
368 PERROR("strdup temp list");
374 struct kern_modules_param
*cur_mod
;
376 next
= strtok(cur_list
, ",");
382 /* filter leading spaces */
383 while (*next
== ' ') {
387 if (probes_capacity
<= nr_probes
) {
394 /* Length 13 is "lttng-probe-" + \0 */
395 name_len
= strlen(next
) + 13;
397 cur_mod
= &probes
[index
++];
398 cur_mod
->name
= zmalloc(name_len
);
399 if (!cur_mod
->name
) {
400 PERROR("malloc probe list");
405 ret
= snprintf(cur_mod
->name
, name_len
, "lttng-probe-%s", next
);
407 PERROR("snprintf modprobe name");
426 * Load data kernel module(s).
428 int modprobe_lttng_data(void)
434 * Base probes: either from command line option, environment
435 * variable or default list.
437 if (kmod_probes_list
) {
438 list
= kmod_probes_list
;
440 list
= utils_get_kmod_probes_list();
444 /* User-specified probes. */
445 ret
= append_list_to_probes(list
);
450 /* Default probes. */
451 int def_len
= ARRAY_SIZE(kern_modules_probes_default
);
452 probes
= zmalloc(sizeof(*probes
) * def_len
);
455 PERROR("malloc probe list");
459 nr_probes
= probes_capacity
= def_len
;
461 for (i
= 0; i
< def_len
; ++i
) {
462 char* name
= strdup(kern_modules_probes_default
[i
].name
);
465 PERROR("strdup probe item");
470 probes
[i
].name
= name
;
475 * Extra modules? Append them to current probes list.
477 if (kmod_extra_probes_list
) {
478 list
= kmod_extra_probes_list
;
480 list
= utils_get_extra_kmod_probes_list();
484 ret
= append_list_to_probes(list
);
491 * Load probes modules now.
493 ret
= modprobe_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);