sessiond: Add --extra-kmod-probes option
[lttng-tools.git] / src / bin / lttng-sessiond / modprobe.c
1 /*
2 * Copyright (C) 2011 - David Goulet <dgoulet@efficios.com>
3 * 2014 - Jan Glauber <jan.glauber@gmail.com>
4 *
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.
8 *
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.
13 *
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.
17 */
18
19 #define _GNU_SOURCE
20 #include <assert.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <sys/wait.h>
24
25 #include <common/common.h>
26 #include <common/utils.h>
27
28 #include "modprobe.h"
29 #include "kern-modules.h"
30
31 #define LTTNG_MOD_REQUIRED 1
32 #define LTTNG_MOD_OPTIONAL 0
33
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" },
44 };
45
46 /* LTTng kernel tracer optional base modules list */
47 struct kern_modules_param kern_modules_control_opt[] = {
48 { "lttng-types" },
49 { "lttng-ftrace" },
50 { "lttng-kprobes" },
51 { "lttng-kretprobes" },
52 };
53
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" },
94 };
95
96 /* dynamic probe modules list */
97 static struct kern_modules_param *probes;
98 static int nr_probes;
99 static int probes_capacity;
100
101 void modprobe_remove_lttng(const struct kern_modules_param *modules,
102 int entries, int required)
103 {
104 int ret = 0, i;
105 char modprobe[256];
106
107 for (i = entries - 1; i >= 0; i--) {
108 ret = snprintf(modprobe, sizeof(modprobe),
109 "/sbin/modprobe -r -q %s",
110 modules[i].name);
111 if (ret < 0) {
112 PERROR("snprintf modprobe -r");
113 return;
114 }
115 modprobe[sizeof(modprobe) - 1] = '\0';
116 ret = system(modprobe);
117 if (ret == -1) {
118 ERR("Unable to launch modprobe -r for module %s",
119 modules[i].name);
120 } else if (required && WEXITSTATUS(ret) != 0) {
121 ERR("Unable to remove module %s",
122 modules[i].name);
123 } else {
124 DBG("Modprobe removal successful %s",
125 modules[i].name);
126 }
127 }
128 }
129
130 /*
131 * Remove control kernel module(s) in reverse load order.
132 */
133 void modprobe_remove_lttng_control(void)
134 {
135 modprobe_remove_lttng(kern_modules_control_opt,
136 ARRAY_SIZE(kern_modules_control_opt),
137 LTTNG_MOD_OPTIONAL);
138 modprobe_remove_lttng(kern_modules_control_core,
139 ARRAY_SIZE(kern_modules_control_core),
140 LTTNG_MOD_REQUIRED);
141 }
142
143 /*
144 * Remove data kernel modules in reverse load order.
145 */
146 void modprobe_remove_lttng_data(void)
147 {
148 int i;
149
150 if (probes) {
151 modprobe_remove_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL);
152
153 for (i = 0; i < nr_probes; ++i) {
154 free(probes[i].name);
155 }
156
157 free(probes);
158 probes = NULL;
159 }
160 }
161
162 /*
163 * Remove all kernel modules in reverse order.
164 */
165 void modprobe_remove_lttng_all(void)
166 {
167 modprobe_remove_lttng_data();
168 modprobe_remove_lttng_control();
169 }
170
171 #if HAVE_KMOD
172 #include <libkmod.h>
173 static void log_kmod(void *data, int priority, const char *file, int line,
174 const char *fn, const char *format, va_list args)
175 {
176 char *str;
177
178 if (vasprintf(&str, format, args) < 0) {
179 return;
180 }
181
182 DBG("libkmod: %s", str);
183 free(str);
184 }
185 static int modprobe_lttng(struct kern_modules_param *modules,
186 int entries, int required)
187 {
188 int ret = 0, i;
189 struct kmod_ctx *ctx;
190
191 ctx = kmod_new(NULL, NULL);
192 if (!ctx) {
193 PERROR("Unable to create kmod library context");
194 ret = -ENOMEM;
195 goto error;
196 }
197
198 kmod_set_log_fn(ctx, log_kmod, NULL);
199 kmod_load_resources(ctx);
200
201 for (i = 0; i < entries; i++) {
202 struct kmod_module *mod = NULL;
203
204 ret = kmod_module_new_from_name(ctx, modules[i].name, &mod);
205 if (ret < 0) {
206 PERROR("Failed to create kmod module for %s", modules[i].name);
207 goto error;
208 }
209
210 ret = kmod_module_probe_insert_module(mod, KMOD_PROBE_IGNORE_LOADED,
211 NULL, NULL, NULL, NULL);
212 if (required && ret < 0) {
213 ERR("Unable to load module %s", modules[i].name);
214 } else {
215 DBG("Modprobe successfully %s", modules[i].name);
216 }
217
218 kmod_module_unref(mod);
219 }
220
221 error:
222 if (ctx) {
223 kmod_unref(ctx);
224 }
225 return ret;
226 }
227
228 #else /* HAVE_KMOD */
229
230 static int modprobe_lttng(struct kern_modules_param *modules,
231 int entries, int required)
232 {
233 int ret = 0, i;
234 char modprobe[256];
235
236 for (i = 0; i < entries; i++) {
237 ret = snprintf(modprobe, sizeof(modprobe),
238 "/sbin/modprobe %s%s",
239 required ? "" : "-q ",
240 modules[i].name);
241 if (ret < 0) {
242 PERROR("snprintf modprobe");
243 goto error;
244 }
245 modprobe[sizeof(modprobe) - 1] = '\0';
246 ret = system(modprobe);
247 if (ret == -1) {
248 ERR("Unable to launch modprobe for module %s",
249 modules[i].name);
250 } else if (required && WEXITSTATUS(ret) != 0) {
251 ERR("Unable to load module %s", modules[i].name);
252 } else {
253 DBG("Modprobe successfully %s", modules[i].name);
254 }
255 }
256
257 error:
258 return ret;
259 }
260
261 #endif /* HAVE_KMOD */
262
263 /*
264 * Load control kernel module(s).
265 */
266 int modprobe_lttng_control(void)
267 {
268 int ret;
269
270 ret = modprobe_lttng(kern_modules_control_core,
271 ARRAY_SIZE(kern_modules_control_core),
272 LTTNG_MOD_REQUIRED);
273 if (ret != 0)
274 return ret;
275 ret = modprobe_lttng(kern_modules_control_opt,
276 ARRAY_SIZE(kern_modules_control_opt),
277 LTTNG_MOD_OPTIONAL);
278 return ret;
279 }
280
281 /**
282 * Grow global list of probes (double capacity or set it to 1 if
283 * currently 0 and copy existing data).
284 */
285 static int grow_probes(void)
286 {
287 int i;
288 struct kern_modules_param *tmp_probes;
289
290 /* Initialize capacity to 1 if 0. */
291 if (probes_capacity == 0) {
292 probes = zmalloc(sizeof(*probes));
293 if (!probes) {
294 PERROR("malloc probe list");
295 return -ENOMEM;
296 }
297
298 probes_capacity = 1;
299 return 0;
300 }
301
302 /* Double size. */
303 probes_capacity *= 2;
304
305 tmp_probes = zmalloc(sizeof(*tmp_probes) * probes_capacity);
306 if (!tmp_probes) {
307 PERROR("malloc probe list");
308 return -ENOMEM;
309 }
310
311 for (i = 0; i < nr_probes; ++i) {
312 /* Move name pointer. */
313 tmp_probes[i].name = probes[i].name;
314 }
315
316 /* Replace probes with larger copy. */
317 free(probes);
318 probes = tmp_probes;
319
320 return 0;
321 }
322
323 /*
324 * Appends a comma-separated list of probes to the global list
325 * of probes.
326 */
327 static int append_list_to_probes(const char *list)
328 {
329 char *next;
330 int index = nr_probes, ret;
331
332 assert(list);
333
334 char *tmp_list = strdup(list);
335 if (!tmp_list) {
336 PERROR("strdup temp list");
337 return -ENOMEM;
338 }
339
340 for (;;) {
341 size_t name_len;
342 struct kern_modules_param *cur_mod;
343
344 next = strtok(tmp_list, ",");
345 if (!next) {
346 break;
347 }
348 tmp_list = NULL;
349
350 /* filter leading spaces */
351 while (*next == ' ') {
352 next++;
353 }
354
355 if (probes_capacity <= nr_probes) {
356 ret = grow_probes();
357 if (ret) {
358 return ret;
359 }
360 }
361
362 /* Length 13 is "lttng-probe-" + \0 */
363 name_len = strlen(next) + 13;
364
365 cur_mod = &probes[index];
366 cur_mod->name = zmalloc(name_len);
367 if (!cur_mod->name) {
368 PERROR("malloc probe list");
369 return -ENOMEM;
370 }
371
372 ret = snprintf(cur_mod->name, name_len, "lttng-probe-%s", next);
373 if (ret < 0) {
374 PERROR("snprintf modprobe name");
375 return -ENOMEM;
376 }
377
378 cur_mod++;
379 nr_probes++;
380 }
381
382 free(tmp_list);
383
384 return 0;
385 }
386
387 /*
388 * Load data kernel module(s).
389 */
390 int modprobe_lttng_data(void)
391 {
392 int ret, i;
393 char *list;
394
395 /*
396 * Base probes: either from command line option, environment
397 * variable or default list.
398 */
399 if (kmod_probes_list) {
400 list = kmod_probes_list;
401 } else {
402 list = utils_get_kmod_probes_list();
403 }
404
405 if (list) {
406 /* User-specified probes. */
407 ret = append_list_to_probes(list);
408
409 if (ret) {
410 return ret;
411 }
412 } else {
413 /* Default probes. */
414 int def_len = ARRAY_SIZE(kern_modules_probes_default);
415 probes = zmalloc(sizeof(*probes) * def_len);
416
417 if (!probes) {
418 PERROR("malloc probe list");
419 return -ENOMEM;
420 }
421
422 nr_probes = probes_capacity = def_len;
423
424 for (i = 0; i < def_len; ++i) {
425 char* name = strdup(kern_modules_probes_default[i].name);
426
427 if (!name) {
428 PERROR("strdup probe item");
429 return -ENOMEM;
430 }
431
432 probes[i].name = name;
433 }
434 }
435
436 /*
437 * Extra modules? Append them to current probes list.
438 */
439 if (kmod_extra_probes_list) {
440 list = kmod_extra_probes_list;
441 } else {
442 list = utils_get_extra_kmod_probes_list();
443 }
444
445 if (list) {
446 ret = append_list_to_probes(list);
447 if (ret) {
448 return ret;
449 }
450 }
451
452 /*
453 * Load probes modules now.
454 */
455 return modprobe_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL);
456 }
This page took 0.038812 seconds and 5 git commands to generate.