Commit | Line | Data |
---|---|---|
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 | ||
6c1c0768 | 19 | #define _LGPL_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 */ | |
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" }, | |
3fa9646c JG |
52 | }; |
53 | ||
54 | /* LTTng kernel tracer probe modules list */ | |
fbb9748b | 55 | struct 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" }, | |
c66dfab8 | 94 | { "lttng-probe-x86-irq-vectors" }, |
037e00be | 95 | { "lttng-probe-x86-exceptions" }, |
096102bd DG |
96 | }; |
97 | ||
fbb9748b JG |
98 | /* dynamic probe modules list */ |
99 | static struct kern_modules_param *probes; | |
100 | static int nr_probes; | |
c9d42407 | 101 | static int probes_capacity; |
fbb9748b | 102 | |
e6421494 MD |
103 | static void modprobe_remove_lttng(const struct kern_modules_param *modules, |
104 | int entries, int required) | |
096102bd DG |
105 | { |
106 | int ret = 0, i; | |
107 | char modprobe[256]; | |
108 | ||
e23b81ed | 109 | for (i = entries - 1; i >= 0; i--) { |
096102bd DG |
110 | ret = snprintf(modprobe, sizeof(modprobe), |
111 | "/sbin/modprobe -r -q %s", | |
e23b81ed | 112 | modules[i].name); |
096102bd DG |
113 | if (ret < 0) { |
114 | PERROR("snprintf modprobe -r"); | |
ab57d7d3 | 115 | return; |
096102bd DG |
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", | |
ab57d7d3 JG |
121 | modules[i].name); |
122 | } else if (required && WEXITSTATUS(ret) != 0) { | |
096102bd | 123 | ERR("Unable to remove module %s", |
ab57d7d3 | 124 | modules[i].name); |
096102bd DG |
125 | } else { |
126 | DBG("Modprobe removal successful %s", | |
ab57d7d3 | 127 | modules[i].name); |
096102bd DG |
128 | } |
129 | } | |
e23b81ed JG |
130 | } |
131 | ||
132 | /* | |
133 | * Remove control kernel module(s) in reverse load order. | |
134 | */ | |
135 | void modprobe_remove_lttng_control(void) | |
136 | { | |
ab57d7d3 | 137 | modprobe_remove_lttng(kern_modules_control_opt, |
fbb9748b JG |
138 | ARRAY_SIZE(kern_modules_control_opt), |
139 | LTTNG_MOD_OPTIONAL); | |
ab57d7d3 | 140 | modprobe_remove_lttng(kern_modules_control_core, |
fbb9748b JG |
141 | ARRAY_SIZE(kern_modules_control_core), |
142 | LTTNG_MOD_REQUIRED); | |
096102bd DG |
143 | } |
144 | ||
398d5459 MD |
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 | ||
096102bd DG |
160 | /* |
161 | * Remove data kernel modules in reverse load order. | |
162 | */ | |
163 | void modprobe_remove_lttng_data(void) | |
164 | { | |
398d5459 MD |
165 | if (!probes) { |
166 | return; | |
c9d42407 | 167 | } |
398d5459 MD |
168 | modprobe_remove_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL); |
169 | free_probes(); | |
096102bd DG |
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 | ||
234170ac UTL |
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); | |
16c2e854 PP |
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 | } | |
234170ac UTL |
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 | ||
fbb9748b | 248 | static int modprobe_lttng(struct kern_modules_param *modules, |
234170ac | 249 | int entries, int required) |
096102bd DG |
250 | { |
251 | int ret = 0, i; | |
252 | char modprobe[256]; | |
253 | ||
e23b81ed | 254 | for (i = 0; i < entries; i++) { |
096102bd DG |
255 | ret = snprintf(modprobe, sizeof(modprobe), |
256 | "/sbin/modprobe %s%s", | |
ab57d7d3 | 257 | required ? "" : "-q ", |
e23b81ed | 258 | modules[i].name); |
096102bd DG |
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) { | |
16c2e854 PP |
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 | } | |
096102bd | 285 | } else { |
ab57d7d3 | 286 | DBG("Modprobe successfully %s", modules[i].name); |
096102bd DG |
287 | } |
288 | } | |
289 | ||
290 | error: | |
291 | return ret; | |
292 | } | |
293 | ||
234170ac UTL |
294 | #endif /* HAVE_KMOD */ |
295 | ||
e23b81ed JG |
296 | /* |
297 | * Load control kernel module(s). | |
298 | */ | |
299 | int modprobe_lttng_control(void) | |
300 | { | |
ab57d7d3 JG |
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, | |
fbb9748b JG |
309 | ARRAY_SIZE(kern_modules_control_opt), |
310 | LTTNG_MOD_OPTIONAL); | |
ab57d7d3 | 311 | return ret; |
e23b81ed | 312 | } |
ab57d7d3 | 313 | |
c9d42407 PP |
314 | /** |
315 | * Grow global list of probes (double capacity or set it to 1 if | |
316 | * currently 0 and copy existing data). | |
096102bd | 317 | */ |
c9d42407 | 318 | static int grow_probes(void) |
096102bd | 319 | { |
c9d42407 PP |
320 | int i; |
321 | struct kern_modules_param *tmp_probes; | |
fbb9748b | 322 | |
c9d42407 PP |
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; | |
fbb9748b JG |
333 | } |
334 | ||
c9d42407 PP |
335 | /* Double size. */ |
336 | probes_capacity *= 2; | |
337 | ||
338 | tmp_probes = zmalloc(sizeof(*tmp_probes) * probes_capacity); | |
339 | if (!tmp_probes) { | |
fbb9748b JG |
340 | PERROR("malloc probe list"); |
341 | return -ENOMEM; | |
342 | } | |
343 | ||
c9d42407 PP |
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; | |
d3c04b7c | 363 | int ret; |
44603c80 | 364 | char *tmp_list, *cur_list; |
c9d42407 PP |
365 | |
366 | assert(list); | |
367 | ||
44603c80 | 368 | cur_list = tmp_list = strdup(list); |
c9d42407 PP |
369 | if (!tmp_list) { |
370 | PERROR("strdup temp list"); | |
371 | return -ENOMEM; | |
372 | } | |
373 | ||
374 | for (;;) { | |
fbb9748b | 375 | size_t name_len; |
c9d42407 | 376 | struct kern_modules_param *cur_mod; |
fbb9748b | 377 | |
44603c80 | 378 | next = strtok(cur_list, ","); |
fbb9748b | 379 | if (!next) { |
c9d42407 | 380 | break; |
fbb9748b | 381 | } |
44603c80 | 382 | cur_list = NULL; |
fbb9748b JG |
383 | |
384 | /* filter leading spaces */ | |
385 | while (*next == ' ') { | |
386 | next++; | |
387 | } | |
388 | ||
c9d42407 PP |
389 | if (probes_capacity <= nr_probes) { |
390 | ret = grow_probes(); | |
391 | if (ret) { | |
398d5459 | 392 | goto error; |
c9d42407 PP |
393 | } |
394 | } | |
395 | ||
fbb9748b JG |
396 | /* Length 13 is "lttng-probe-" + \0 */ |
397 | name_len = strlen(next) + 13; | |
398 | ||
d3c04b7c | 399 | cur_mod = &probes[nr_probes]; |
c9d42407 PP |
400 | cur_mod->name = zmalloc(name_len); |
401 | if (!cur_mod->name) { | |
fbb9748b | 402 | PERROR("malloc probe list"); |
398d5459 MD |
403 | ret = -ENOMEM; |
404 | goto error; | |
fbb9748b JG |
405 | } |
406 | ||
c9d42407 | 407 | ret = snprintf(cur_mod->name, name_len, "lttng-probe-%s", next); |
fbb9748b JG |
408 | if (ret < 0) { |
409 | PERROR("snprintf modprobe name"); | |
398d5459 MD |
410 | ret = -ENOMEM; |
411 | goto error; | |
fbb9748b | 412 | } |
c9d42407 | 413 | |
c9d42407 | 414 | nr_probes++; |
fbb9748b JG |
415 | } |
416 | ||
c9d42407 | 417 | free(tmp_list); |
c9d42407 | 418 | return 0; |
398d5459 MD |
419 | |
420 | error: | |
421 | free(tmp_list); | |
422 | free_probes(); | |
423 | return ret; | |
c9d42407 PP |
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); | |
c9d42407 PP |
447 | if (ret) { |
448 | return ret; | |
449 | } | |
450 | } else { | |
451 | /* Default probes. */ | |
452 | int def_len = ARRAY_SIZE(kern_modules_probes_default); | |
c9d42407 | 453 | |
62e0422e | 454 | probes = zmalloc(sizeof(*probes) * def_len); |
c9d42407 PP |
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"); | |
398d5459 MD |
467 | ret = -ENOMEM; |
468 | goto error; | |
c9d42407 PP |
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) { | |
398d5459 | 487 | goto error; |
c9d42407 PP |
488 | } |
489 | } | |
490 | ||
491 | /* | |
492 | * Load probes modules now. | |
493 | */ | |
398d5459 MD |
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; | |
096102bd | 503 | } |