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.
24 #include <common/common.h>
25 #include <common/utils.h>
28 #include "kern-modules.h"
30 #define LTTNG_MOD_REQUIRED 1
31 #define LTTNG_MOD_OPTIONAL 0
33 /* LTTng kernel tracer mandatory core modules list */
34 struct kern_modules_param kern_modules_control_core
[] = {
35 { "lttng-tracer" }, /* MUST be loaded first so keep at top */
36 { "lttng-lib-ring-buffer" },
37 { "lttng-ring-buffer-client-discard" },
38 { "lttng-ring-buffer-client-overwrite" },
39 { "lttng-ring-buffer-metadata-client" },
40 { "lttng-ring-buffer-client-mmap-discard" },
41 { "lttng-ring-buffer-client-mmap-overwrite" },
42 { "lttng-ring-buffer-metadata-mmap-client" },
45 /* LTTng kernel tracer optional base modules list */
46 struct kern_modules_param kern_modules_control_opt
[] = {
50 { "lttng-kretprobes" },
53 /* LTTng kernel tracer probe modules list */
54 struct kern_modules_param kern_modules_probes_default
[] = {
55 { "lttng-probe-asoc" },
56 { "lttng-probe-block" },
57 { "lttng-probe-btrfs" },
58 { "lttng-probe-compaction" },
59 { "lttng-probe-ext3" },
60 { "lttng-probe-ext4" },
61 { "lttng-probe-gpio" },
62 { "lttng-probe-irq" },
63 { "lttng-probe-jbd" },
64 { "lttng-probe-jbd2" },
65 { "lttng-probe-kmem" },
66 { "lttng-probe-kvm" },
67 { "lttng-probe-kvm-x86" },
68 { "lttng-probe-kvm-x86-mmu" },
69 { "lttng-probe-lock" },
70 { "lttng-probe-module" },
71 { "lttng-probe-napi" },
72 { "lttng-probe-net" },
73 { "lttng-probe-power" },
74 { "lttng-probe-printk" },
75 { "lttng-probe-random" },
76 { "lttng-probe-rcu" },
77 { "lttng-probe-regmap" },
78 { "lttng-probe-regulator" },
79 { "lttng-probe-rpm" },
80 { "lttng-probe-sched" },
81 { "lttng-probe-scsi" },
82 { "lttng-probe-signal" },
83 { "lttng-probe-skb" },
84 { "lttng-probe-sock" },
85 { "lttng-probe-statedump" },
86 { "lttng-probe-sunrpc" },
87 { "lttng-probe-timer" },
88 { "lttng-probe-udp" },
89 { "lttng-probe-vmscan" },
90 { "lttng-probe-v4l2" },
91 { "lttng-probe-workqueue" },
92 { "lttng-probe-writeback" },
95 /* dynamic probe modules list */
96 static struct kern_modules_param
*probes
;
99 void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
100 int entries
, int required
)
105 for (i
= entries
- 1; i
>= 0; i
--) {
106 ret
= snprintf(modprobe
, sizeof(modprobe
),
107 "/sbin/modprobe -r -q %s",
110 PERROR("snprintf modprobe -r");
113 modprobe
[sizeof(modprobe
) - 1] = '\0';
114 ret
= system(modprobe
);
116 ERR("Unable to launch modprobe -r for module %s",
118 } else if (required
&& WEXITSTATUS(ret
) != 0) {
119 ERR("Unable to remove module %s",
122 DBG("Modprobe removal successful %s",
126 free(probes
[i
].name
);
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)
149 modprobe_remove_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);
153 modprobe_remove_lttng(kern_modules_probes_default
,
154 ARRAY_SIZE(kern_modules_probes_default
),
159 * Remove all kernel modules in reverse order.
161 void modprobe_remove_lttng_all(void)
163 modprobe_remove_lttng_data();
164 modprobe_remove_lttng_control();
167 static int modprobe_lttng(struct kern_modules_param
*modules
,
168 int entries
, int required
)
173 for (i
= 0; i
< entries
; i
++) {
174 ret
= snprintf(modprobe
, sizeof(modprobe
),
175 "/sbin/modprobe %s%s",
176 required
? "" : "-q ",
179 PERROR("snprintf modprobe");
182 modprobe
[sizeof(modprobe
) - 1] = '\0';
183 ret
= system(modprobe
);
185 ERR("Unable to launch modprobe for module %s",
187 } else if (required
&& WEXITSTATUS(ret
) != 0) {
188 ERR("Unable to load module %s", modules
[i
].name
);
190 DBG("Modprobe successfully %s", modules
[i
].name
);
199 * Load control kernel module(s).
201 int modprobe_lttng_control(void)
205 ret
= modprobe_lttng(kern_modules_control_core
,
206 ARRAY_SIZE(kern_modules_control_core
),
210 ret
= modprobe_lttng(kern_modules_control_opt
,
211 ARRAY_SIZE(kern_modules_control_opt
),
217 * Load data kernel module(s).
219 int modprobe_lttng_data(void)
222 int entries
= ARRAY_SIZE(kern_modules_probes_default
);
226 * First take command line option, if not available take environment
229 if (kmod_probes_list
) {
230 list
= kmod_probes_list
;
232 list
= utils_get_kmod_probes_list();
234 /* The default is to load ALL probes */
236 return modprobe_lttng(kern_modules_probes_default
, entries
,
241 * A probe list is available, so use it.
242 * The number of probes is limited by the number of probes in the
245 probes
= zmalloc(sizeof(struct kern_modules_param
*) * entries
);
247 PERROR("malloc probe list");
251 for (i
= 0; i
< entries
; i
++) {
254 next
= strtok(list
, ",");
260 /* filter leading spaces */
261 while (*next
== ' ') {
265 /* Length 13 is "lttng-probe-" + \0 */
266 name_len
= strlen(next
) + 13;
268 probes
[i
].name
= zmalloc(name_len
);
269 if (!probes
[i
].name
) {
270 PERROR("malloc probe list");
274 ret
= snprintf(probes
[i
].name
, name_len
, "lttng-probe-%s", next
);
276 PERROR("snprintf modprobe name");
283 return modprobe_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);