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-ring-buffer-client-discard" },
37 { "lttng-ring-buffer-client-overwrite" },
38 { "lttng-ring-buffer-metadata-client" },
39 { "lttng-ring-buffer-client-mmap-discard" },
40 { "lttng-ring-buffer-client-mmap-overwrite" },
41 { "lttng-ring-buffer-metadata-mmap-client" },
44 /* LTTng kernel tracer probe modules list */
45 struct kern_modules_param kern_modules_probes_default
[] = {
46 { "lttng-probe-asoc" },
47 { "lttng-probe-block" },
48 { "lttng-probe-btrfs" },
49 { "lttng-probe-compaction" },
50 { "lttng-probe-ext3" },
51 { "lttng-probe-ext4" },
52 { "lttng-probe-gpio" },
53 { "lttng-probe-irq" },
54 { "lttng-probe-jbd" },
55 { "lttng-probe-jbd2" },
56 { "lttng-probe-kmem" },
57 { "lttng-probe-kvm" },
58 { "lttng-probe-kvm-x86" },
59 { "lttng-probe-kvm-x86-mmu" },
60 { "lttng-probe-lock" },
61 { "lttng-probe-module" },
62 { "lttng-probe-napi" },
63 { "lttng-probe-net" },
64 { "lttng-probe-power" },
65 { "lttng-probe-printk" },
66 { "lttng-probe-random" },
67 { "lttng-probe-rcu" },
68 { "lttng-probe-regmap" },
69 { "lttng-probe-regulator" },
70 { "lttng-probe-rpm" },
71 { "lttng-probe-sched" },
72 { "lttng-probe-scsi" },
73 { "lttng-probe-signal" },
74 { "lttng-probe-skb" },
75 { "lttng-probe-sock" },
76 { "lttng-probe-statedump" },
77 { "lttng-probe-sunrpc" },
78 { "lttng-probe-timer" },
79 { "lttng-probe-udp" },
80 { "lttng-probe-vmscan" },
81 { "lttng-probe-v4l2" },
82 { "lttng-probe-workqueue" },
83 { "lttng-probe-writeback" },
84 { "lttng-probe-x86-irq-vectors" },
85 { "lttng-probe-x86-exceptions" },
88 /* dynamic probe modules list */
89 static struct kern_modules_param
*probes
;
91 static int probes_capacity
;
95 static void log_kmod(void *data
, int priority
, const char *file
, int line
,
96 const char *fn
, const char *format
, va_list args
)
100 if (vasprintf(&str
, format
, args
) < 0) {
104 DBG("libkmod: %s", str
);
107 static int modprobe_lttng(struct kern_modules_param
*modules
,
108 int entries
, int required
)
111 struct kmod_ctx
*ctx
;
113 ctx
= kmod_new(NULL
, NULL
);
115 PERROR("Unable to create kmod library context");
120 kmod_set_log_fn(ctx
, log_kmod
, NULL
);
121 kmod_load_resources(ctx
);
123 for (i
= 0; i
< entries
; i
++) {
124 struct kmod_module
*mod
= NULL
;
126 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
128 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
132 ret
= kmod_module_probe_insert_module(mod
, KMOD_PROBE_IGNORE_LOADED
,
133 NULL
, NULL
, NULL
, NULL
);
136 ERR("Unable to load required module %s",
140 DBG("Unable to load optional module %s; continuing",
145 DBG("Modprobe successfully %s", modules
[i
].name
);
148 kmod_module_unref(mod
);
158 #else /* HAVE_KMOD */
160 static int modprobe_lttng(struct kern_modules_param
*modules
,
161 int entries
, int required
)
166 for (i
= 0; i
< entries
; i
++) {
167 ret
= snprintf(modprobe
, sizeof(modprobe
),
168 "/sbin/modprobe %s%s",
169 required
? "" : "-q ",
172 PERROR("snprintf modprobe");
175 modprobe
[sizeof(modprobe
) - 1] = '\0';
176 ret
= system(modprobe
);
179 ERR("Unable to launch modprobe for required module %s",
183 DBG("Unable to launch modprobe for optional module %s; continuing",
187 } else if (WEXITSTATUS(ret
) != 0) {
189 ERR("Unable to load required module %s",
193 DBG("Unable to load optional module %s; continuing",
198 DBG("Modprobe successfully %s", modules
[i
].name
);
206 #endif /* HAVE_KMOD */
208 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
209 int entries
, int required
)
214 for (i
= entries
- 1; i
>= 0; i
--) {
215 ret
= snprintf(modprobe
, sizeof(modprobe
),
216 "/sbin/modprobe -r -q %s",
219 PERROR("snprintf modprobe -r");
222 modprobe
[sizeof(modprobe
) - 1] = '\0';
223 ret
= system(modprobe
);
225 ERR("Unable to launch modprobe -r for module %s",
227 } else if (required
&& WEXITSTATUS(ret
) != 0) {
228 ERR("Unable to remove module %s",
231 DBG("Modprobe removal successful %s",
238 * Remove control kernel module(s) in reverse load order.
240 void modprobe_remove_lttng_control(void)
242 modprobe_remove_lttng(kern_modules_control_core
,
243 ARRAY_SIZE(kern_modules_control_core
),
247 static void free_probes(void)
254 for (i
= 0; i
< nr_probes
; ++i
) {
255 free(probes
[i
].name
);
263 * Remove data kernel modules in reverse load order.
265 void modprobe_remove_lttng_data(void)
270 modprobe_remove_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);
275 * Remove all kernel modules in reverse order.
277 void modprobe_remove_lttng_all(void)
279 modprobe_remove_lttng_data();
280 modprobe_remove_lttng_control();
284 * Load control kernel module(s).
286 int modprobe_lttng_control(void)
290 ret
= modprobe_lttng(kern_modules_control_core
,
291 ARRAY_SIZE(kern_modules_control_core
),
297 * Grow global list of probes (double capacity or set it to 1 if
298 * currently 0 and copy existing data).
300 static int grow_probes(void)
303 struct kern_modules_param
*tmp_probes
;
305 /* Initialize capacity to 1 if 0. */
306 if (probes_capacity
== 0) {
307 probes
= zmalloc(sizeof(*probes
));
309 PERROR("malloc probe list");
318 probes_capacity
*= 2;
320 tmp_probes
= zmalloc(sizeof(*tmp_probes
) * probes_capacity
);
322 PERROR("malloc probe list");
326 for (i
= 0; i
< nr_probes
; ++i
) {
327 /* Move name pointer. */
328 tmp_probes
[i
].name
= probes
[i
].name
;
331 /* Replace probes with larger copy. */
339 * Appends a comma-separated list of probes to the global list
342 static int append_list_to_probes(const char *list
)
346 char *tmp_list
, *cur_list
;
350 cur_list
= tmp_list
= strdup(list
);
352 PERROR("strdup temp list");
358 struct kern_modules_param
*cur_mod
;
360 next
= strtok(cur_list
, ",");
366 /* filter leading spaces */
367 while (*next
== ' ') {
371 if (probes_capacity
<= nr_probes
) {
378 /* Length 13 is "lttng-probe-" + \0 */
379 name_len
= strlen(next
) + 13;
381 cur_mod
= &probes
[nr_probes
];
382 cur_mod
->name
= zmalloc(name_len
);
383 if (!cur_mod
->name
) {
384 PERROR("malloc probe list");
389 ret
= snprintf(cur_mod
->name
, name_len
, "lttng-probe-%s", next
);
391 PERROR("snprintf modprobe name");
409 * Load data kernel module(s).
411 int modprobe_lttng_data(void)
417 * Base probes: either from command line option, environment
418 * variable or default list.
420 if (kmod_probes_list
) {
421 list
= kmod_probes_list
;
423 list
= utils_get_kmod_probes_list();
427 /* User-specified probes. */
428 ret
= append_list_to_probes(list
);
433 /* Default probes. */
434 int def_len
= ARRAY_SIZE(kern_modules_probes_default
);
436 probes
= zmalloc(sizeof(*probes
) * def_len
);
438 PERROR("malloc probe list");
442 nr_probes
= probes_capacity
= def_len
;
444 for (i
= 0; i
< def_len
; ++i
) {
445 char* name
= strdup(kern_modules_probes_default
[i
].name
);
448 PERROR("strdup probe item");
453 probes
[i
].name
= name
;
458 * Extra modules? Append them to current probes list.
460 if (kmod_extra_probes_list
) {
461 list
= kmod_extra_probes_list
;
463 list
= utils_get_extra_kmod_probes_list();
467 ret
= append_list_to_probes(list
);
474 * Load probes modules now.
476 ret
= modprobe_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);