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 static 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
),
144 static void free_probes(void)
151 for (i
= 0; i
< nr_probes
; ++i
) {
152 free(probes
[i
].name
);
160 * Remove data kernel modules in reverse load order.
162 void modprobe_remove_lttng_data(void)
167 modprobe_remove_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);
172 * Remove all kernel modules in reverse order.
174 void modprobe_remove_lttng_all(void)
176 modprobe_remove_lttng_data();
177 modprobe_remove_lttng_control();
182 static void log_kmod(void *data
, int priority
, const char *file
, int line
,
183 const char *fn
, const char *format
, va_list args
)
187 if (vasprintf(&str
, format
, args
) < 0) {
191 DBG("libkmod: %s", str
);
194 static int modprobe_lttng(struct kern_modules_param
*modules
,
195 int entries
, int required
)
198 struct kmod_ctx
*ctx
;
200 ctx
= kmod_new(NULL
, NULL
);
202 PERROR("Unable to create kmod library context");
207 kmod_set_log_fn(ctx
, log_kmod
, NULL
);
208 kmod_load_resources(ctx
);
210 for (i
= 0; i
< entries
; i
++) {
211 struct kmod_module
*mod
= NULL
;
213 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
215 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
219 ret
= kmod_module_probe_insert_module(mod
, KMOD_PROBE_IGNORE_LOADED
,
220 NULL
, NULL
, NULL
, NULL
);
223 ERR("Unable to load required module %s",
227 DBG("Unable to load optional module %s; continuing",
232 DBG("Modprobe successfully %s", modules
[i
].name
);
235 kmod_module_unref(mod
);
245 #else /* HAVE_KMOD */
247 static int modprobe_lttng(struct kern_modules_param
*modules
,
248 int entries
, int required
)
253 for (i
= 0; i
< entries
; i
++) {
254 ret
= snprintf(modprobe
, sizeof(modprobe
),
255 "/sbin/modprobe %s%s",
256 required
? "" : "-q ",
259 PERROR("snprintf modprobe");
262 modprobe
[sizeof(modprobe
) - 1] = '\0';
263 ret
= system(modprobe
);
266 ERR("Unable to launch modprobe for required module %s",
270 DBG("Unable to launch modprobe for optional module %s; continuing",
274 } else if (WEXITSTATUS(ret
) != 0) {
276 ERR("Unable to load required module %s",
280 DBG("Unable to load optional module %s; continuing",
285 DBG("Modprobe successfully %s", modules
[i
].name
);
293 #endif /* HAVE_KMOD */
296 * Load control kernel module(s).
298 int modprobe_lttng_control(void)
302 ret
= modprobe_lttng(kern_modules_control_core
,
303 ARRAY_SIZE(kern_modules_control_core
),
307 ret
= modprobe_lttng(kern_modules_control_opt
,
308 ARRAY_SIZE(kern_modules_control_opt
),
314 * Grow global list of probes (double capacity or set it to 1 if
315 * currently 0 and copy existing data).
317 static int grow_probes(void)
320 struct kern_modules_param
*tmp_probes
;
322 /* Initialize capacity to 1 if 0. */
323 if (probes_capacity
== 0) {
324 probes
= zmalloc(sizeof(*probes
));
326 PERROR("malloc probe list");
335 probes_capacity
*= 2;
337 tmp_probes
= zmalloc(sizeof(*tmp_probes
) * probes_capacity
);
339 PERROR("malloc probe list");
343 for (i
= 0; i
< nr_probes
; ++i
) {
344 /* Move name pointer. */
345 tmp_probes
[i
].name
= probes
[i
].name
;
348 /* Replace probes with larger copy. */
356 * Appends a comma-separated list of probes to the global list
359 static int append_list_to_probes(const char *list
)
362 int index
= nr_probes
, ret
;
367 tmp_list
= strdup(list
);
369 PERROR("strdup temp list");
375 struct kern_modules_param
*cur_mod
;
377 next
= strtok(tmp_list
, ",");
383 /* filter leading spaces */
384 while (*next
== ' ') {
388 if (probes_capacity
<= nr_probes
) {
395 /* Length 13 is "lttng-probe-" + \0 */
396 name_len
= strlen(next
) + 13;
398 cur_mod
= &probes
[index
];
399 cur_mod
->name
= zmalloc(name_len
);
400 if (!cur_mod
->name
) {
401 PERROR("malloc probe list");
406 ret
= snprintf(cur_mod
->name
, name_len
, "lttng-probe-%s", next
);
408 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
);