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
),
144 * Remove data kernel modules in reverse load order.
146 void modprobe_remove_lttng_data(void)
151 modprobe_remove_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);
153 for (i
= 0; i
< nr_probes
; ++i
) {
154 free(probes
[i
].name
);
163 * Remove all kernel modules in reverse order.
165 void modprobe_remove_lttng_all(void)
167 modprobe_remove_lttng_data();
168 modprobe_remove_lttng_control();
173 static void log_kmod(void *data
, int priority
, const char *file
, int line
,
174 const char *fn
, const char *format
, va_list args
)
178 if (vasprintf(&str
, format
, args
) < 0) {
182 DBG("libkmod: %s", str
);
185 static int modprobe_lttng(struct kern_modules_param
*modules
,
186 int entries
, int required
)
189 struct kmod_ctx
*ctx
;
191 ctx
= kmod_new(NULL
, NULL
);
193 PERROR("Unable to create kmod library context");
198 kmod_set_log_fn(ctx
, log_kmod
, NULL
);
199 kmod_load_resources(ctx
);
201 for (i
= 0; i
< entries
; i
++) {
202 struct kmod_module
*mod
= NULL
;
204 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
206 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
210 ret
= kmod_module_probe_insert_module(mod
, KMOD_PROBE_IGNORE_LOADED
,
211 NULL
, NULL
, NULL
, NULL
);
214 ERR("Unable to load required module %s",
218 DBG("Unable to load optional module %s; continuing",
223 DBG("Modprobe successfully %s", modules
[i
].name
);
226 kmod_module_unref(mod
);
236 #else /* HAVE_KMOD */
238 static int modprobe_lttng(struct kern_modules_param
*modules
,
239 int entries
, int required
)
244 for (i
= 0; i
< entries
; i
++) {
245 ret
= snprintf(modprobe
, sizeof(modprobe
),
246 "/sbin/modprobe %s%s",
247 required
? "" : "-q ",
250 PERROR("snprintf modprobe");
253 modprobe
[sizeof(modprobe
) - 1] = '\0';
254 ret
= system(modprobe
);
257 ERR("Unable to launch modprobe for required module %s",
261 DBG("Unable to launch modprobe for optional module %s; continuing",
265 } else if (WEXITSTATUS(ret
) != 0) {
267 ERR("Unable to load required module %s",
271 DBG("Unable to load optional module %s; continuing",
276 DBG("Modprobe successfully %s", modules
[i
].name
);
284 #endif /* HAVE_KMOD */
287 * Load control kernel module(s).
289 int modprobe_lttng_control(void)
293 ret
= modprobe_lttng(kern_modules_control_core
,
294 ARRAY_SIZE(kern_modules_control_core
),
298 ret
= modprobe_lttng(kern_modules_control_opt
,
299 ARRAY_SIZE(kern_modules_control_opt
),
305 * Grow global list of probes (double capacity or set it to 1 if
306 * currently 0 and copy existing data).
308 static int grow_probes(void)
311 struct kern_modules_param
*tmp_probes
;
313 /* Initialize capacity to 1 if 0. */
314 if (probes_capacity
== 0) {
315 probes
= zmalloc(sizeof(*probes
));
317 PERROR("malloc probe list");
326 probes_capacity
*= 2;
328 tmp_probes
= zmalloc(sizeof(*tmp_probes
) * probes_capacity
);
330 PERROR("malloc probe list");
334 for (i
= 0; i
< nr_probes
; ++i
) {
335 /* Move name pointer. */
336 tmp_probes
[i
].name
= probes
[i
].name
;
339 /* Replace probes with larger copy. */
347 * Appends a comma-separated list of probes to the global list
350 static int append_list_to_probes(const char *list
)
353 int index
= nr_probes
, ret
;
357 char *tmp_list
= strdup(list
);
359 PERROR("strdup temp list");
365 struct kern_modules_param
*cur_mod
;
367 next
= strtok(tmp_list
, ",");
373 /* filter leading spaces */
374 while (*next
== ' ') {
378 if (probes_capacity
<= nr_probes
) {
385 /* Length 13 is "lttng-probe-" + \0 */
386 name_len
= strlen(next
) + 13;
388 cur_mod
= &probes
[index
];
389 cur_mod
->name
= zmalloc(name_len
);
390 if (!cur_mod
->name
) {
391 PERROR("malloc probe list");
395 ret
= snprintf(cur_mod
->name
, name_len
, "lttng-probe-%s", next
);
397 PERROR("snprintf modprobe name");
411 * Load data kernel module(s).
413 int modprobe_lttng_data(void)
419 * Base probes: either from command line option, environment
420 * variable or default list.
422 if (kmod_probes_list
) {
423 list
= kmod_probes_list
;
425 list
= utils_get_kmod_probes_list();
429 /* User-specified probes. */
430 ret
= append_list_to_probes(list
);
436 /* Default probes. */
437 int def_len
= ARRAY_SIZE(kern_modules_probes_default
);
438 probes
= zmalloc(sizeof(*probes
) * def_len
);
441 PERROR("malloc probe list");
445 nr_probes
= probes_capacity
= def_len
;
447 for (i
= 0; i
< def_len
; ++i
) {
448 char* name
= strdup(kern_modules_probes_default
[i
].name
);
451 PERROR("strdup probe item");
455 probes
[i
].name
= name
;
460 * Extra modules? Append them to current probes list.
462 if (kmod_extra_probes_list
) {
463 list
= kmod_extra_probes_list
;
465 list
= utils_get_extra_kmod_probes_list();
469 ret
= append_list_to_probes(list
);
476 * Load probes modules now.
478 return modprobe_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);