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