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[] = { | |
ab57d7d3 JG |
36 | { "lttng-ring-buffer-client-discard" }, |
37 | { "lttng-ring-buffer-client-overwrite" }, | |
38 | { "lttng-ring-buffer-metadata-client" }, | |
39 | { "lttng-ring-buffer-client-mmap-discard" }, | |
40 | { "lttng-ring-buffer-client-mmap-overwrite" }, | |
41 | { "lttng-ring-buffer-metadata-mmap-client" }, | |
42 | }; | |
43 | ||
3fa9646c | 44 | /* LTTng kernel tracer probe modules list */ |
fbb9748b | 45 | struct kern_modules_param kern_modules_probes_default[] = { |
ab57d7d3 JG |
46 | { "lttng-probe-asoc" }, |
47 | { "lttng-probe-block" }, | |
48 | { "lttng-probe-btrfs" }, | |
49 | { "lttng-probe-compaction" }, | |
50 | { "lttng-probe-ext3" }, | |
51 | { "lttng-probe-ext4" }, | |
52 | { "lttng-probe-gpio" }, | |
53 | { "lttng-probe-irq" }, | |
54 | { "lttng-probe-jbd" }, | |
55 | { "lttng-probe-jbd2" }, | |
56 | { "lttng-probe-kmem" }, | |
57 | { "lttng-probe-kvm" }, | |
58 | { "lttng-probe-kvm-x86" }, | |
59 | { "lttng-probe-kvm-x86-mmu" }, | |
60 | { "lttng-probe-lock" }, | |
61 | { "lttng-probe-module" }, | |
62 | { "lttng-probe-napi" }, | |
63 | { "lttng-probe-net" }, | |
64 | { "lttng-probe-power" }, | |
65 | { "lttng-probe-printk" }, | |
66 | { "lttng-probe-random" }, | |
67 | { "lttng-probe-rcu" }, | |
68 | { "lttng-probe-regmap" }, | |
69 | { "lttng-probe-regulator" }, | |
70 | { "lttng-probe-rpm" }, | |
71 | { "lttng-probe-sched" }, | |
72 | { "lttng-probe-scsi" }, | |
73 | { "lttng-probe-signal" }, | |
74 | { "lttng-probe-skb" }, | |
75 | { "lttng-probe-sock" }, | |
76 | { "lttng-probe-statedump" }, | |
77 | { "lttng-probe-sunrpc" }, | |
78 | { "lttng-probe-timer" }, | |
79 | { "lttng-probe-udp" }, | |
80 | { "lttng-probe-vmscan" }, | |
81 | { "lttng-probe-v4l2" }, | |
82 | { "lttng-probe-workqueue" }, | |
83 | { "lttng-probe-writeback" }, | |
c66dfab8 | 84 | { "lttng-probe-x86-irq-vectors" }, |
037e00be | 85 | { "lttng-probe-x86-exceptions" }, |
096102bd DG |
86 | }; |
87 | ||
fbb9748b JG |
88 | /* dynamic probe modules list */ |
89 | static struct kern_modules_param *probes; | |
90 | static int nr_probes; | |
c9d42407 | 91 | static int probes_capacity; |
fbb9748b | 92 | |
e6421494 MD |
93 | static void modprobe_remove_lttng(const struct kern_modules_param *modules, |
94 | int entries, int required) | |
096102bd DG |
95 | { |
96 | int ret = 0, i; | |
97 | char modprobe[256]; | |
98 | ||
e23b81ed | 99 | for (i = entries - 1; i >= 0; i--) { |
096102bd DG |
100 | ret = snprintf(modprobe, sizeof(modprobe), |
101 | "/sbin/modprobe -r -q %s", | |
e23b81ed | 102 | modules[i].name); |
096102bd DG |
103 | if (ret < 0) { |
104 | PERROR("snprintf modprobe -r"); | |
ab57d7d3 | 105 | return; |
096102bd DG |
106 | } |
107 | modprobe[sizeof(modprobe) - 1] = '\0'; | |
108 | ret = system(modprobe); | |
109 | if (ret == -1) { | |
110 | ERR("Unable to launch modprobe -r for module %s", | |
ab57d7d3 JG |
111 | modules[i].name); |
112 | } else if (required && WEXITSTATUS(ret) != 0) { | |
096102bd | 113 | ERR("Unable to remove module %s", |
ab57d7d3 | 114 | modules[i].name); |
096102bd DG |
115 | } else { |
116 | DBG("Modprobe removal successful %s", | |
ab57d7d3 | 117 | modules[i].name); |
096102bd DG |
118 | } |
119 | } | |
e23b81ed JG |
120 | } |
121 | ||
122 | /* | |
123 | * Remove control kernel module(s) in reverse load order. | |
124 | */ | |
125 | void modprobe_remove_lttng_control(void) | |
126 | { | |
ab57d7d3 | 127 | modprobe_remove_lttng(kern_modules_control_core, |
fbb9748b JG |
128 | ARRAY_SIZE(kern_modules_control_core), |
129 | LTTNG_MOD_REQUIRED); | |
096102bd DG |
130 | } |
131 | ||
398d5459 MD |
132 | static void free_probes(void) |
133 | { | |
134 | int i; | |
135 | ||
136 | if (!probes) { | |
137 | return; | |
138 | } | |
139 | for (i = 0; i < nr_probes; ++i) { | |
140 | free(probes[i].name); | |
141 | } | |
142 | free(probes); | |
143 | probes = NULL; | |
144 | nr_probes = 0; | |
145 | } | |
146 | ||
096102bd DG |
147 | /* |
148 | * Remove data kernel modules in reverse load order. | |
149 | */ | |
150 | void modprobe_remove_lttng_data(void) | |
151 | { | |
398d5459 MD |
152 | if (!probes) { |
153 | return; | |
c9d42407 | 154 | } |
398d5459 MD |
155 | modprobe_remove_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL); |
156 | free_probes(); | |
096102bd DG |
157 | } |
158 | ||
159 | /* | |
160 | * Remove all kernel modules in reverse order. | |
161 | */ | |
162 | void modprobe_remove_lttng_all(void) | |
163 | { | |
164 | modprobe_remove_lttng_data(); | |
165 | modprobe_remove_lttng_control(); | |
166 | } | |
167 | ||
234170ac UTL |
168 | #if HAVE_KMOD |
169 | #include <libkmod.h> | |
170 | static void log_kmod(void *data, int priority, const char *file, int line, | |
171 | const char *fn, const char *format, va_list args) | |
172 | { | |
173 | char *str; | |
174 | ||
175 | if (vasprintf(&str, format, args) < 0) { | |
176 | return; | |
177 | } | |
178 | ||
179 | DBG("libkmod: %s", str); | |
180 | free(str); | |
181 | } | |
182 | static int modprobe_lttng(struct kern_modules_param *modules, | |
183 | int entries, int required) | |
184 | { | |
185 | int ret = 0, i; | |
186 | struct kmod_ctx *ctx; | |
187 | ||
188 | ctx = kmod_new(NULL, NULL); | |
189 | if (!ctx) { | |
190 | PERROR("Unable to create kmod library context"); | |
191 | ret = -ENOMEM; | |
192 | goto error; | |
193 | } | |
194 | ||
195 | kmod_set_log_fn(ctx, log_kmod, NULL); | |
196 | kmod_load_resources(ctx); | |
197 | ||
198 | for (i = 0; i < entries; i++) { | |
199 | struct kmod_module *mod = NULL; | |
200 | ||
201 | ret = kmod_module_new_from_name(ctx, modules[i].name, &mod); | |
202 | if (ret < 0) { | |
203 | PERROR("Failed to create kmod module for %s", modules[i].name); | |
204 | goto error; | |
205 | } | |
206 | ||
207 | ret = kmod_module_probe_insert_module(mod, KMOD_PROBE_IGNORE_LOADED, | |
208 | NULL, NULL, NULL, NULL); | |
16c2e854 PP |
209 | if (ret < 0) { |
210 | if (required) { | |
211 | ERR("Unable to load required module %s", | |
212 | modules[i].name); | |
213 | goto error; | |
214 | } else { | |
215 | DBG("Unable to load optional module %s; continuing", | |
216 | modules[i].name); | |
217 | ret = 0; | |
218 | } | |
234170ac UTL |
219 | } else { |
220 | DBG("Modprobe successfully %s", modules[i].name); | |
221 | } | |
222 | ||
223 | kmod_module_unref(mod); | |
224 | } | |
225 | ||
226 | error: | |
227 | if (ctx) { | |
228 | kmod_unref(ctx); | |
229 | } | |
230 | return ret; | |
231 | } | |
232 | ||
233 | #else /* HAVE_KMOD */ | |
234 | ||
fbb9748b | 235 | static int modprobe_lttng(struct kern_modules_param *modules, |
234170ac | 236 | int entries, int required) |
096102bd DG |
237 | { |
238 | int ret = 0, i; | |
239 | char modprobe[256]; | |
240 | ||
e23b81ed | 241 | for (i = 0; i < entries; i++) { |
096102bd DG |
242 | ret = snprintf(modprobe, sizeof(modprobe), |
243 | "/sbin/modprobe %s%s", | |
ab57d7d3 | 244 | required ? "" : "-q ", |
e23b81ed | 245 | modules[i].name); |
096102bd DG |
246 | if (ret < 0) { |
247 | PERROR("snprintf modprobe"); | |
248 | goto error; | |
249 | } | |
250 | modprobe[sizeof(modprobe) - 1] = '\0'; | |
251 | ret = system(modprobe); | |
252 | if (ret == -1) { | |
16c2e854 PP |
253 | if (required) { |
254 | ERR("Unable to launch modprobe for required module %s", | |
255 | modules[i].name); | |
256 | goto error; | |
257 | } else { | |
258 | DBG("Unable to launch modprobe for optional module %s; continuing", | |
259 | modules[i].name); | |
260 | ret = 0; | |
261 | } | |
262 | } else if (WEXITSTATUS(ret) != 0) { | |
263 | if (required) { | |
264 | ERR("Unable to load required module %s", | |
265 | modules[i].name); | |
266 | goto error; | |
267 | } else { | |
268 | DBG("Unable to load optional module %s; continuing", | |
269 | modules[i].name); | |
270 | ret = 0; | |
271 | } | |
096102bd | 272 | } else { |
ab57d7d3 | 273 | DBG("Modprobe successfully %s", modules[i].name); |
096102bd DG |
274 | } |
275 | } | |
276 | ||
277 | error: | |
278 | return ret; | |
279 | } | |
280 | ||
234170ac UTL |
281 | #endif /* HAVE_KMOD */ |
282 | ||
e23b81ed JG |
283 | /* |
284 | * Load control kernel module(s). | |
285 | */ | |
286 | int modprobe_lttng_control(void) | |
287 | { | |
ab57d7d3 JG |
288 | int ret; |
289 | ||
290 | ret = modprobe_lttng(kern_modules_control_core, | |
291 | ARRAY_SIZE(kern_modules_control_core), | |
292 | LTTNG_MOD_REQUIRED); | |
ab57d7d3 | 293 | return ret; |
e23b81ed | 294 | } |
ab57d7d3 | 295 | |
c9d42407 PP |
296 | /** |
297 | * Grow global list of probes (double capacity or set it to 1 if | |
298 | * currently 0 and copy existing data). | |
096102bd | 299 | */ |
c9d42407 | 300 | static int grow_probes(void) |
096102bd | 301 | { |
c9d42407 PP |
302 | int i; |
303 | struct kern_modules_param *tmp_probes; | |
fbb9748b | 304 | |
c9d42407 PP |
305 | /* Initialize capacity to 1 if 0. */ |
306 | if (probes_capacity == 0) { | |
307 | probes = zmalloc(sizeof(*probes)); | |
308 | if (!probes) { | |
309 | PERROR("malloc probe list"); | |
310 | return -ENOMEM; | |
311 | } | |
312 | ||
313 | probes_capacity = 1; | |
314 | return 0; | |
fbb9748b JG |
315 | } |
316 | ||
c9d42407 PP |
317 | /* Double size. */ |
318 | probes_capacity *= 2; | |
319 | ||
320 | tmp_probes = zmalloc(sizeof(*tmp_probes) * probes_capacity); | |
321 | if (!tmp_probes) { | |
fbb9748b JG |
322 | PERROR("malloc probe list"); |
323 | return -ENOMEM; | |
324 | } | |
325 | ||
c9d42407 PP |
326 | for (i = 0; i < nr_probes; ++i) { |
327 | /* Move name pointer. */ | |
328 | tmp_probes[i].name = probes[i].name; | |
329 | } | |
330 | ||
331 | /* Replace probes with larger copy. */ | |
332 | free(probes); | |
333 | probes = tmp_probes; | |
334 | ||
335 | return 0; | |
336 | } | |
337 | ||
338 | /* | |
339 | * Appends a comma-separated list of probes to the global list | |
340 | * of probes. | |
341 | */ | |
342 | static int append_list_to_probes(const char *list) | |
343 | { | |
344 | char *next; | |
d3c04b7c | 345 | int ret; |
44603c80 | 346 | char *tmp_list, *cur_list; |
c9d42407 PP |
347 | |
348 | assert(list); | |
349 | ||
44603c80 | 350 | cur_list = tmp_list = strdup(list); |
c9d42407 PP |
351 | if (!tmp_list) { |
352 | PERROR("strdup temp list"); | |
353 | return -ENOMEM; | |
354 | } | |
355 | ||
356 | for (;;) { | |
fbb9748b | 357 | size_t name_len; |
c9d42407 | 358 | struct kern_modules_param *cur_mod; |
fbb9748b | 359 | |
44603c80 | 360 | next = strtok(cur_list, ","); |
fbb9748b | 361 | if (!next) { |
c9d42407 | 362 | break; |
fbb9748b | 363 | } |
44603c80 | 364 | cur_list = NULL; |
fbb9748b JG |
365 | |
366 | /* filter leading spaces */ | |
367 | while (*next == ' ') { | |
368 | next++; | |
369 | } | |
370 | ||
c9d42407 PP |
371 | if (probes_capacity <= nr_probes) { |
372 | ret = grow_probes(); | |
373 | if (ret) { | |
398d5459 | 374 | goto error; |
c9d42407 PP |
375 | } |
376 | } | |
377 | ||
fbb9748b JG |
378 | /* Length 13 is "lttng-probe-" + \0 */ |
379 | name_len = strlen(next) + 13; | |
380 | ||
d3c04b7c | 381 | cur_mod = &probes[nr_probes]; |
c9d42407 PP |
382 | cur_mod->name = zmalloc(name_len); |
383 | if (!cur_mod->name) { | |
fbb9748b | 384 | PERROR("malloc probe list"); |
398d5459 MD |
385 | ret = -ENOMEM; |
386 | goto error; | |
fbb9748b JG |
387 | } |
388 | ||
c9d42407 | 389 | ret = snprintf(cur_mod->name, name_len, "lttng-probe-%s", next); |
fbb9748b JG |
390 | if (ret < 0) { |
391 | PERROR("snprintf modprobe name"); | |
398d5459 MD |
392 | ret = -ENOMEM; |
393 | goto error; | |
fbb9748b | 394 | } |
c9d42407 | 395 | |
c9d42407 | 396 | nr_probes++; |
fbb9748b JG |
397 | } |
398 | ||
c9d42407 | 399 | free(tmp_list); |
c9d42407 | 400 | return 0; |
398d5459 MD |
401 | |
402 | error: | |
403 | free(tmp_list); | |
404 | free_probes(); | |
405 | return ret; | |
c9d42407 PP |
406 | } |
407 | ||
408 | /* | |
409 | * Load data kernel module(s). | |
410 | */ | |
411 | int modprobe_lttng_data(void) | |
412 | { | |
413 | int ret, i; | |
414 | char *list; | |
415 | ||
416 | /* | |
417 | * Base probes: either from command line option, environment | |
418 | * variable or default list. | |
419 | */ | |
420 | if (kmod_probes_list) { | |
421 | list = kmod_probes_list; | |
422 | } else { | |
423 | list = utils_get_kmod_probes_list(); | |
424 | } | |
425 | ||
426 | if (list) { | |
427 | /* User-specified probes. */ | |
428 | ret = append_list_to_probes(list); | |
c9d42407 PP |
429 | if (ret) { |
430 | return ret; | |
431 | } | |
432 | } else { | |
433 | /* Default probes. */ | |
434 | int def_len = ARRAY_SIZE(kern_modules_probes_default); | |
c9d42407 | 435 | |
62e0422e | 436 | probes = zmalloc(sizeof(*probes) * def_len); |
c9d42407 PP |
437 | if (!probes) { |
438 | PERROR("malloc probe list"); | |
439 | return -ENOMEM; | |
440 | } | |
441 | ||
442 | nr_probes = probes_capacity = def_len; | |
443 | ||
444 | for (i = 0; i < def_len; ++i) { | |
445 | char* name = strdup(kern_modules_probes_default[i].name); | |
446 | ||
447 | if (!name) { | |
448 | PERROR("strdup probe item"); | |
398d5459 MD |
449 | ret = -ENOMEM; |
450 | goto error; | |
c9d42407 PP |
451 | } |
452 | ||
453 | probes[i].name = name; | |
454 | } | |
455 | } | |
456 | ||
457 | /* | |
458 | * Extra modules? Append them to current probes list. | |
459 | */ | |
460 | if (kmod_extra_probes_list) { | |
461 | list = kmod_extra_probes_list; | |
462 | } else { | |
463 | list = utils_get_extra_kmod_probes_list(); | |
464 | } | |
465 | ||
466 | if (list) { | |
467 | ret = append_list_to_probes(list); | |
468 | if (ret) { | |
398d5459 | 469 | goto error; |
c9d42407 PP |
470 | } |
471 | } | |
472 | ||
473 | /* | |
474 | * Load probes modules now. | |
475 | */ | |
398d5459 MD |
476 | ret = modprobe_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL); |
477 | if (ret) { | |
478 | goto error; | |
479 | } | |
480 | return ret; | |
481 | ||
482 | error: | |
483 | free_probes(); | |
484 | return ret; | |
096102bd | 485 | } |