sessiond: Add --extra-kmod-probes option
[lttng-tools.git] / src / bin / lttng-sessiond / modprobe.c
CommitLineData
096102bd
DG
1/*
2 * Copyright (C) 2011 - David Goulet <dgoulet@efficios.com>
fbb9748b 3 * 2014 - Jan Glauber <jan.glauber@gmail.com>
096102bd 4 *
d14d33bf
AM
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.
096102bd 8 *
d14d33bf
AM
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.
096102bd 13 *
d14d33bf
AM
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.
096102bd
DG
17 */
18
19#define _GNU_SOURCE
c9d42407 20#include <assert.h>
096102bd
DG
21#include <stdio.h>
22#include <stdlib.h>
23#include <sys/wait.h>
24
25#include <common/common.h>
fbb9748b 26#include <common/utils.h>
096102bd
DG
27
28#include "modprobe.h"
29#include "kern-modules.h"
30
ab57d7d3
JG
31#define LTTNG_MOD_REQUIRED 1
32#define LTTNG_MOD_OPTIONAL 0
33
34/* LTTng kernel tracer mandatory core modules list */
35struct 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 */
47struct kern_modules_param kern_modules_control_opt[] = {
48 { "lttng-types" },
49 { "lttng-ftrace" },
50 { "lttng-kprobes" },
51 { "lttng-kretprobes" },
3fa9646c
JG
52};
53
54/* LTTng kernel tracer probe modules list */
fbb9748b 55struct kern_modules_param kern_modules_probes_default[] = {
ab57d7d3
JG
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" },
096102bd
DG
94};
95
fbb9748b
JG
96/* dynamic probe modules list */
97static struct kern_modules_param *probes;
98static int nr_probes;
c9d42407 99static int probes_capacity;
fbb9748b 100
e23b81ed 101void modprobe_remove_lttng(const struct kern_modules_param *modules,
ab57d7d3 102 int entries, int required)
096102bd
DG
103{
104 int ret = 0, i;
105 char modprobe[256];
106
e23b81ed 107 for (i = entries - 1; i >= 0; i--) {
096102bd
DG
108 ret = snprintf(modprobe, sizeof(modprobe),
109 "/sbin/modprobe -r -q %s",
e23b81ed 110 modules[i].name);
096102bd
DG
111 if (ret < 0) {
112 PERROR("snprintf modprobe -r");
ab57d7d3 113 return;
096102bd
DG
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",
ab57d7d3
JG
119 modules[i].name);
120 } else if (required && WEXITSTATUS(ret) != 0) {
096102bd 121 ERR("Unable to remove module %s",
ab57d7d3 122 modules[i].name);
096102bd
DG
123 } else {
124 DBG("Modprobe removal successful %s",
ab57d7d3 125 modules[i].name);
096102bd
DG
126 }
127 }
e23b81ed
JG
128}
129
130/*
131 * Remove control kernel module(s) in reverse load order.
132 */
133void modprobe_remove_lttng_control(void)
134{
ab57d7d3 135 modprobe_remove_lttng(kern_modules_control_opt,
fbb9748b
JG
136 ARRAY_SIZE(kern_modules_control_opt),
137 LTTNG_MOD_OPTIONAL);
ab57d7d3 138 modprobe_remove_lttng(kern_modules_control_core,
fbb9748b
JG
139 ARRAY_SIZE(kern_modules_control_core),
140 LTTNG_MOD_REQUIRED);
096102bd
DG
141}
142
143/*
144 * Remove data kernel modules in reverse load order.
145 */
146void modprobe_remove_lttng_data(void)
147{
c9d42407
PP
148 int i;
149
fbb9748b
JG
150 if (probes) {
151 modprobe_remove_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL);
c9d42407
PP
152
153 for (i = 0; i < nr_probes; ++i) {
154 free(probes[i].name);
155 }
156
fbb9748b
JG
157 free(probes);
158 probes = NULL;
c9d42407 159 }
096102bd
DG
160}
161
162/*
163 * Remove all kernel modules in reverse order.
164 */
165void modprobe_remove_lttng_all(void)
166{
167 modprobe_remove_lttng_data();
168 modprobe_remove_lttng_control();
169}
170
234170ac
UTL
171#if HAVE_KMOD
172#include <libkmod.h>
173static 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}
185static 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
221error:
222 if (ctx) {
223 kmod_unref(ctx);
224 }
225 return ret;
226}
227
228#else /* HAVE_KMOD */
229
fbb9748b 230static int modprobe_lttng(struct kern_modules_param *modules,
234170ac 231 int entries, int required)
096102bd
DG
232{
233 int ret = 0, i;
234 char modprobe[256];
235
e23b81ed 236 for (i = 0; i < entries; i++) {
096102bd
DG
237 ret = snprintf(modprobe, sizeof(modprobe),
238 "/sbin/modprobe %s%s",
ab57d7d3 239 required ? "" : "-q ",
e23b81ed 240 modules[i].name);
096102bd
DG
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",
e23b81ed 249 modules[i].name);
ab57d7d3
JG
250 } else if (required && WEXITSTATUS(ret) != 0) {
251 ERR("Unable to load module %s", modules[i].name);
096102bd 252 } else {
ab57d7d3 253 DBG("Modprobe successfully %s", modules[i].name);
096102bd
DG
254 }
255 }
256
257error:
258 return ret;
259}
260
234170ac
UTL
261#endif /* HAVE_KMOD */
262
e23b81ed
JG
263/*
264 * Load control kernel module(s).
265 */
266int modprobe_lttng_control(void)
267{
ab57d7d3
JG
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,
fbb9748b
JG
276 ARRAY_SIZE(kern_modules_control_opt),
277 LTTNG_MOD_OPTIONAL);
ab57d7d3 278 return ret;
e23b81ed 279}
ab57d7d3 280
c9d42407
PP
281/**
282 * Grow global list of probes (double capacity or set it to 1 if
283 * currently 0 and copy existing data).
096102bd 284 */
c9d42407 285static int grow_probes(void)
096102bd 286{
c9d42407
PP
287 int i;
288 struct kern_modules_param *tmp_probes;
fbb9748b 289
c9d42407
PP
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;
fbb9748b
JG
300 }
301
c9d42407
PP
302 /* Double size. */
303 probes_capacity *= 2;
304
305 tmp_probes = zmalloc(sizeof(*tmp_probes) * probes_capacity);
306 if (!tmp_probes) {
fbb9748b
JG
307 PERROR("malloc probe list");
308 return -ENOMEM;
309 }
310
c9d42407
PP
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 */
327static 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 (;;) {
fbb9748b 341 size_t name_len;
c9d42407 342 struct kern_modules_param *cur_mod;
fbb9748b 343
c9d42407 344 next = strtok(tmp_list, ",");
fbb9748b 345 if (!next) {
c9d42407 346 break;
fbb9748b 347 }
c9d42407 348 tmp_list = NULL;
fbb9748b
JG
349
350 /* filter leading spaces */
351 while (*next == ' ') {
352 next++;
353 }
354
c9d42407
PP
355 if (probes_capacity <= nr_probes) {
356 ret = grow_probes();
357 if (ret) {
358 return ret;
359 }
360 }
361
fbb9748b
JG
362 /* Length 13 is "lttng-probe-" + \0 */
363 name_len = strlen(next) + 13;
364
c9d42407
PP
365 cur_mod = &probes[index];
366 cur_mod->name = zmalloc(name_len);
367 if (!cur_mod->name) {
fbb9748b
JG
368 PERROR("malloc probe list");
369 return -ENOMEM;
370 }
371
c9d42407 372 ret = snprintf(cur_mod->name, name_len, "lttng-probe-%s", next);
fbb9748b
JG
373 if (ret < 0) {
374 PERROR("snprintf modprobe name");
c9d42407 375 return -ENOMEM;
fbb9748b 376 }
c9d42407
PP
377
378 cur_mod++;
379 nr_probes++;
fbb9748b
JG
380 }
381
c9d42407
PP
382 free(tmp_list);
383
384 return 0;
385}
386
387/*
388 * Load data kernel module(s).
389 */
390int 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 */
fbb9748b 455 return modprobe_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL);
096102bd 456}
This page took 0.052893 seconds and 4 git commands to generate.