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.
26 #include <common/common.h>
27 #include <common/utils.h>
30 #include "kern-modules.h"
32 #define LTTNG_MOD_REQUIRED 1
33 #define LTTNG_MOD_OPTIONAL 0
35 /* LTTng kernel tracer mandatory core modules list */
36 struct kern_modules_param kern_modules_control_core
[] = {
37 { "lttng-tracer" }, /* MUST be loaded first so keep at top */
38 { "lttng-lib-ring-buffer" },
39 { "lttng-ring-buffer-client-discard" },
40 { "lttng-ring-buffer-client-overwrite" },
41 { "lttng-ring-buffer-metadata-client" },
42 { "lttng-ring-buffer-client-mmap-discard" },
43 { "lttng-ring-buffer-client-mmap-overwrite" },
44 { "lttng-ring-buffer-metadata-mmap-client" },
47 /* LTTng kernel tracer optional base modules list */
48 struct kern_modules_param kern_modules_control_opt
[] = {
52 { "lttng-kretprobes" },
55 /* LTTng kernel tracer probe modules list */
56 struct kern_modules_param kern_modules_probes_default
[] = {
57 { "lttng-probe-asoc" },
58 { "lttng-probe-block" },
59 { "lttng-probe-btrfs" },
60 { "lttng-probe-compaction" },
61 { "lttng-probe-ext3" },
62 { "lttng-probe-ext4" },
63 { "lttng-probe-gpio" },
64 { "lttng-probe-irq" },
65 { "lttng-probe-jbd" },
66 { "lttng-probe-jbd2" },
67 { "lttng-probe-kmem" },
68 { "lttng-probe-kvm" },
69 { "lttng-probe-kvm-x86" },
70 { "lttng-probe-kvm-x86-mmu" },
71 { "lttng-probe-lock" },
72 { "lttng-probe-module" },
73 { "lttng-probe-napi" },
74 { "lttng-probe-net" },
75 { "lttng-probe-power" },
76 { "lttng-probe-printk" },
77 { "lttng-probe-random" },
78 { "lttng-probe-rcu" },
79 { "lttng-probe-regmap" },
80 { "lttng-probe-regulator" },
81 { "lttng-probe-rpm" },
82 { "lttng-probe-sched" },
83 { "lttng-probe-scsi" },
84 { "lttng-probe-signal" },
85 { "lttng-probe-skb" },
86 { "lttng-probe-sock" },
87 { "lttng-probe-statedump" },
88 { "lttng-probe-sunrpc" },
89 { "lttng-probe-timer" },
90 { "lttng-probe-udp" },
91 { "lttng-probe-vmscan" },
92 { "lttng-probe-v4l2" },
93 { "lttng-probe-workqueue" },
94 { "lttng-probe-writeback" },
97 /* dynamic probe modules list */
98 static struct kern_modules_param
*probes
;
100 static int probes_capacity
;
102 void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
103 int entries
, int required
)
108 for (i
= entries
- 1; i
>= 0; i
--) {
109 ret
= snprintf(modprobe
, sizeof(modprobe
),
110 "/sbin/modprobe -r -q %s",
113 PERROR("snprintf modprobe -r");
116 modprobe
[sizeof(modprobe
) - 1] = '\0';
117 ret
= system(modprobe
);
119 ERR("Unable to launch modprobe -r for module %s",
121 } else if (required
&& WEXITSTATUS(ret
) != 0) {
122 ERR("Unable to remove module %s",
125 DBG("Modprobe removal successful %s",
132 * Remove control kernel module(s) in reverse load order.
134 void modprobe_remove_lttng_control(void)
136 modprobe_remove_lttng(kern_modules_control_opt
,
137 ARRAY_SIZE(kern_modules_control_opt
),
139 modprobe_remove_lttng(kern_modules_control_core
,
140 ARRAY_SIZE(kern_modules_control_core
),
145 * Remove data kernel modules in reverse load order.
147 void modprobe_remove_lttng_data(void)
152 modprobe_remove_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);
154 for (i
= 0; i
< nr_probes
; ++i
) {
155 free(probes
[i
].name
);
164 * Remove all kernel modules in reverse order.
166 void modprobe_remove_lttng_all(void)
168 modprobe_remove_lttng_data();
169 modprobe_remove_lttng_control();
174 static void log_kmod(void *data
, int priority
, const char *file
, int line
,
175 const char *fn
, const char *format
, va_list args
)
179 if (vasprintf(&str
, format
, args
) < 0) {
183 DBG("libkmod: %s", str
);
186 static int modprobe_lttng(struct kern_modules_param
*modules
,
187 int entries
, int required
)
190 struct kmod_ctx
*ctx
;
192 ctx
= kmod_new(NULL
, NULL
);
194 PERROR("Unable to create kmod library context");
199 kmod_set_log_fn(ctx
, log_kmod
, NULL
);
200 kmod_load_resources(ctx
);
202 for (i
= 0; i
< entries
; i
++) {
203 struct kmod_module
*mod
= NULL
;
205 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
207 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
211 ret
= kmod_module_probe_insert_module(mod
, KMOD_PROBE_IGNORE_LOADED
,
212 NULL
, NULL
, NULL
, NULL
);
215 ERR("Unable to load required module %s",
219 DBG("Unable to load optional module %s; continuing",
224 DBG("Modprobe successfully %s", modules
[i
].name
);
227 kmod_module_unref(mod
);
237 #else /* HAVE_KMOD */
239 static int modprobe_lttng(struct kern_modules_param
*modules
,
240 int entries
, int required
)
245 for (i
= 0; i
< entries
; i
++) {
246 ret
= snprintf(modprobe
, sizeof(modprobe
),
247 "/sbin/modprobe %s%s",
248 required
? "" : "-q ",
251 PERROR("snprintf modprobe");
254 modprobe
[sizeof(modprobe
) - 1] = '\0';
255 ret
= system(modprobe
);
258 ERR("Unable to launch modprobe for required module %s",
262 DBG("Unable to launch modprobe for optional module %s; continuing",
266 } else if (WEXITSTATUS(ret
) != 0) {
268 ERR("Unable to load required module %s",
272 DBG("Unable to load optional module %s; continuing",
277 DBG("Modprobe successfully %s", modules
[i
].name
);
285 #endif /* HAVE_KMOD */
288 * Load control kernel module(s).
290 int modprobe_lttng_control(void)
294 ret
= modprobe_lttng(kern_modules_control_core
,
295 ARRAY_SIZE(kern_modules_control_core
),
299 ret
= modprobe_lttng(kern_modules_control_opt
,
300 ARRAY_SIZE(kern_modules_control_opt
),
306 * Grow global list of probes (double capacity or set it to 1 if
307 * currently 0 and copy existing data).
309 static int grow_probes(void)
312 struct kern_modules_param
*tmp_probes
;
314 /* Initialize capacity to 1 if 0. */
315 if (probes_capacity
== 0) {
316 probes
= zmalloc(sizeof(*probes
));
318 PERROR("malloc probe list");
327 probes_capacity
*= 2;
329 tmp_probes
= zmalloc(sizeof(*tmp_probes
) * probes_capacity
);
331 PERROR("malloc probe list");
335 for (i
= 0; i
< nr_probes
; ++i
) {
336 /* Move name pointer. */
337 tmp_probes
[i
].name
= probes
[i
].name
;
340 /* Replace probes with larger copy. */
348 * Appends a comma-separated list of probes to the global list
351 static int append_list_to_probes(const char *list
)
354 int index
= nr_probes
, ret
;
358 char *tmp_list
= strdup(list
);
360 PERROR("strdup temp list");
366 struct kern_modules_param
*cur_mod
;
368 next
= strtok(tmp_list
, ",");
374 /* filter leading spaces */
375 while (*next
== ' ') {
379 if (probes_capacity
<= nr_probes
) {
386 /* Length 13 is "lttng-probe-" + \0 */
387 name_len
= strlen(next
) + 13;
389 cur_mod
= &probes
[index
];
390 cur_mod
->name
= zmalloc(name_len
);
391 if (!cur_mod
->name
) {
392 PERROR("malloc probe list");
396 ret
= snprintf(cur_mod
->name
, name_len
, "lttng-probe-%s", next
);
398 PERROR("snprintf modprobe name");
412 * Load data kernel module(s).
414 int modprobe_lttng_data(void)
420 * Base probes: either from command line option, environment
421 * variable or default list.
423 if (kmod_probes_list
) {
424 list
= kmod_probes_list
;
426 list
= utils_get_kmod_probes_list();
430 /* User-specified probes. */
431 ret
= append_list_to_probes(list
);
437 /* Default probes. */
438 int def_len
= ARRAY_SIZE(kern_modules_probes_default
);
439 probes
= zmalloc(sizeof(*probes
) * def_len
);
442 PERROR("malloc probe list");
446 nr_probes
= probes_capacity
= def_len
;
448 for (i
= 0; i
< def_len
; ++i
) {
449 char* name
= strdup(kern_modules_probes_default
[i
].name
);
452 PERROR("strdup probe item");
456 probes
[i
].name
= name
;
461 * Extra modules? Append them to current probes list.
463 if (kmod_extra_probes_list
) {
464 list
= kmod_extra_probes_list
;
466 list
= utils_get_extra_kmod_probes_list();
470 ret
= append_list_to_probes(list
);
477 * Load probes modules now.
479 return modprobe_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);