| 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 <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <sys/wait.h> |
| 23 | |
| 24 | #include <common/common.h> |
| 25 | #include <common/utils.h> |
| 26 | |
| 27 | #include "modprobe.h" |
| 28 | #include "kern-modules.h" |
| 29 | |
| 30 | #define LTTNG_MOD_REQUIRED 1 |
| 31 | #define LTTNG_MOD_OPTIONAL 0 |
| 32 | |
| 33 | /* LTTng kernel tracer mandatory core modules list */ |
| 34 | struct kern_modules_param kern_modules_control_core[] = { |
| 35 | { "lttng-tracer" }, /* MUST be loaded first so keep at top */ |
| 36 | { "lttng-lib-ring-buffer" }, |
| 37 | { "lttng-ring-buffer-client-discard" }, |
| 38 | { "lttng-ring-buffer-client-overwrite" }, |
| 39 | { "lttng-ring-buffer-metadata-client" }, |
| 40 | { "lttng-ring-buffer-client-mmap-discard" }, |
| 41 | { "lttng-ring-buffer-client-mmap-overwrite" }, |
| 42 | { "lttng-ring-buffer-metadata-mmap-client" }, |
| 43 | }; |
| 44 | |
| 45 | /* LTTng kernel tracer optional base modules list */ |
| 46 | struct kern_modules_param kern_modules_control_opt[] = { |
| 47 | { "lttng-types" }, |
| 48 | { "lttng-ftrace" }, |
| 49 | { "lttng-kprobes" }, |
| 50 | { "lttng-kretprobes" }, |
| 51 | }; |
| 52 | |
| 53 | /* LTTng kernel tracer probe modules list */ |
| 54 | struct kern_modules_param kern_modules_probes_default[] = { |
| 55 | { "lttng-probe-asoc" }, |
| 56 | { "lttng-probe-block" }, |
| 57 | { "lttng-probe-btrfs" }, |
| 58 | { "lttng-probe-compaction" }, |
| 59 | { "lttng-probe-ext3" }, |
| 60 | { "lttng-probe-ext4" }, |
| 61 | { "lttng-probe-gpio" }, |
| 62 | { "lttng-probe-irq" }, |
| 63 | { "lttng-probe-jbd" }, |
| 64 | { "lttng-probe-jbd2" }, |
| 65 | { "lttng-probe-kmem" }, |
| 66 | { "lttng-probe-kvm" }, |
| 67 | { "lttng-probe-kvm-x86" }, |
| 68 | { "lttng-probe-kvm-x86-mmu" }, |
| 69 | { "lttng-probe-lock" }, |
| 70 | { "lttng-probe-module" }, |
| 71 | { "lttng-probe-napi" }, |
| 72 | { "lttng-probe-net" }, |
| 73 | { "lttng-probe-power" }, |
| 74 | { "lttng-probe-printk" }, |
| 75 | { "lttng-probe-random" }, |
| 76 | { "lttng-probe-rcu" }, |
| 77 | { "lttng-probe-regmap" }, |
| 78 | { "lttng-probe-regulator" }, |
| 79 | { "lttng-probe-rpm" }, |
| 80 | { "lttng-probe-sched" }, |
| 81 | { "lttng-probe-scsi" }, |
| 82 | { "lttng-probe-signal" }, |
| 83 | { "lttng-probe-skb" }, |
| 84 | { "lttng-probe-sock" }, |
| 85 | { "lttng-probe-statedump" }, |
| 86 | { "lttng-probe-sunrpc" }, |
| 87 | { "lttng-probe-timer" }, |
| 88 | { "lttng-probe-udp" }, |
| 89 | { "lttng-probe-vmscan" }, |
| 90 | { "lttng-probe-v4l2" }, |
| 91 | { "lttng-probe-workqueue" }, |
| 92 | { "lttng-probe-writeback" }, |
| 93 | }; |
| 94 | |
| 95 | /* dynamic probe modules list */ |
| 96 | static struct kern_modules_param *probes; |
| 97 | static int nr_probes; |
| 98 | |
| 99 | void modprobe_remove_lttng(const struct kern_modules_param *modules, |
| 100 | int entries, int required) |
| 101 | { |
| 102 | int ret = 0, i; |
| 103 | char modprobe[256]; |
| 104 | |
| 105 | for (i = entries - 1; i >= 0; i--) { |
| 106 | ret = snprintf(modprobe, sizeof(modprobe), |
| 107 | "/sbin/modprobe -r -q %s", |
| 108 | modules[i].name); |
| 109 | if (ret < 0) { |
| 110 | PERROR("snprintf modprobe -r"); |
| 111 | return; |
| 112 | } |
| 113 | modprobe[sizeof(modprobe) - 1] = '\0'; |
| 114 | ret = system(modprobe); |
| 115 | if (ret == -1) { |
| 116 | ERR("Unable to launch modprobe -r for module %s", |
| 117 | modules[i].name); |
| 118 | } else if (required && WEXITSTATUS(ret) != 0) { |
| 119 | ERR("Unable to remove module %s", |
| 120 | modules[i].name); |
| 121 | } else { |
| 122 | DBG("Modprobe removal successful %s", |
| 123 | modules[i].name); |
| 124 | } |
| 125 | if (probes) |
| 126 | free(probes[i].name); |
| 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 | if (probes) { |
| 149 | modprobe_remove_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL); |
| 150 | free(probes); |
| 151 | probes = NULL; |
| 152 | } else |
| 153 | modprobe_remove_lttng(kern_modules_probes_default, |
| 154 | ARRAY_SIZE(kern_modules_probes_default), |
| 155 | LTTNG_MOD_OPTIONAL); |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Remove all kernel modules in reverse order. |
| 160 | */ |
| 161 | void modprobe_remove_lttng_all(void) |
| 162 | { |
| 163 | modprobe_remove_lttng_data(); |
| 164 | modprobe_remove_lttng_control(); |
| 165 | } |
| 166 | |
| 167 | #if HAVE_KMOD |
| 168 | #include <libkmod.h> |
| 169 | static void log_kmod(void *data, int priority, const char *file, int line, |
| 170 | const char *fn, const char *format, va_list args) |
| 171 | { |
| 172 | char *str; |
| 173 | |
| 174 | if (vasprintf(&str, format, args) < 0) { |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | DBG("libkmod: %s", str); |
| 179 | free(str); |
| 180 | } |
| 181 | static int modprobe_lttng(struct kern_modules_param *modules, |
| 182 | int entries, int required) |
| 183 | { |
| 184 | int ret = 0, i; |
| 185 | struct kmod_ctx *ctx; |
| 186 | |
| 187 | ctx = kmod_new(NULL, NULL); |
| 188 | if (!ctx) { |
| 189 | PERROR("Unable to create kmod library context"); |
| 190 | ret = -ENOMEM; |
| 191 | goto error; |
| 192 | } |
| 193 | |
| 194 | kmod_set_log_fn(ctx, log_kmod, NULL); |
| 195 | kmod_load_resources(ctx); |
| 196 | |
| 197 | for (i = 0; i < entries; i++) { |
| 198 | struct kmod_module *mod = NULL; |
| 199 | |
| 200 | ret = kmod_module_new_from_name(ctx, modules[i].name, &mod); |
| 201 | if (ret < 0) { |
| 202 | PERROR("Failed to create kmod module for %s", modules[i].name); |
| 203 | goto error; |
| 204 | } |
| 205 | |
| 206 | ret = kmod_module_probe_insert_module(mod, KMOD_PROBE_IGNORE_LOADED, |
| 207 | NULL, NULL, NULL, NULL); |
| 208 | if (required && ret < 0) { |
| 209 | ERR("Unable to load module %s", modules[i].name); |
| 210 | } else { |
| 211 | DBG("Modprobe successfully %s", modules[i].name); |
| 212 | } |
| 213 | |
| 214 | kmod_module_unref(mod); |
| 215 | } |
| 216 | |
| 217 | error: |
| 218 | if (ctx) { |
| 219 | kmod_unref(ctx); |
| 220 | } |
| 221 | return ret; |
| 222 | } |
| 223 | |
| 224 | #else /* HAVE_KMOD */ |
| 225 | |
| 226 | static int modprobe_lttng(struct kern_modules_param *modules, |
| 227 | int entries, int required) |
| 228 | { |
| 229 | int ret = 0, i; |
| 230 | char modprobe[256]; |
| 231 | |
| 232 | for (i = 0; i < entries; i++) { |
| 233 | ret = snprintf(modprobe, sizeof(modprobe), |
| 234 | "/sbin/modprobe %s%s", |
| 235 | required ? "" : "-q ", |
| 236 | modules[i].name); |
| 237 | if (ret < 0) { |
| 238 | PERROR("snprintf modprobe"); |
| 239 | goto error; |
| 240 | } |
| 241 | modprobe[sizeof(modprobe) - 1] = '\0'; |
| 242 | ret = system(modprobe); |
| 243 | if (ret == -1) { |
| 244 | ERR("Unable to launch modprobe for module %s", |
| 245 | modules[i].name); |
| 246 | } else if (required && WEXITSTATUS(ret) != 0) { |
| 247 | ERR("Unable to load module %s", modules[i].name); |
| 248 | } else { |
| 249 | DBG("Modprobe successfully %s", modules[i].name); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | error: |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | #endif /* HAVE_KMOD */ |
| 258 | |
| 259 | /* |
| 260 | * Load control kernel module(s). |
| 261 | */ |
| 262 | int modprobe_lttng_control(void) |
| 263 | { |
| 264 | int ret; |
| 265 | |
| 266 | ret = modprobe_lttng(kern_modules_control_core, |
| 267 | ARRAY_SIZE(kern_modules_control_core), |
| 268 | LTTNG_MOD_REQUIRED); |
| 269 | if (ret != 0) |
| 270 | return ret; |
| 271 | ret = modprobe_lttng(kern_modules_control_opt, |
| 272 | ARRAY_SIZE(kern_modules_control_opt), |
| 273 | LTTNG_MOD_OPTIONAL); |
| 274 | return ret; |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * Load data kernel module(s). |
| 279 | */ |
| 280 | int modprobe_lttng_data(void) |
| 281 | { |
| 282 | int i, ret; |
| 283 | int entries = ARRAY_SIZE(kern_modules_probes_default); |
| 284 | char *list, *next; |
| 285 | |
| 286 | /* |
| 287 | * First take command line option, if not available take environment |
| 288 | * variable. |
| 289 | */ |
| 290 | if (kmod_probes_list) { |
| 291 | list = kmod_probes_list; |
| 292 | } else { |
| 293 | list = utils_get_kmod_probes_list(); |
| 294 | } |
| 295 | /* The default is to load ALL probes */ |
| 296 | if (!list) { |
| 297 | return modprobe_lttng(kern_modules_probes_default, entries, |
| 298 | LTTNG_MOD_OPTIONAL); |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * A probe list is available, so use it. |
| 303 | * The number of probes is limited by the number of probes in the |
| 304 | * default list. |
| 305 | */ |
| 306 | probes = zmalloc(sizeof(struct kern_modules_param *) * entries); |
| 307 | if (!probes) { |
| 308 | PERROR("malloc probe list"); |
| 309 | return -ENOMEM; |
| 310 | } |
| 311 | |
| 312 | for (i = 0; i < entries; i++) { |
| 313 | size_t name_len; |
| 314 | |
| 315 | next = strtok(list, ","); |
| 316 | if (!next) { |
| 317 | goto out; |
| 318 | } |
| 319 | list = NULL; |
| 320 | |
| 321 | /* filter leading spaces */ |
| 322 | while (*next == ' ') { |
| 323 | next++; |
| 324 | } |
| 325 | |
| 326 | /* Length 13 is "lttng-probe-" + \0 */ |
| 327 | name_len = strlen(next) + 13; |
| 328 | |
| 329 | probes[i].name = zmalloc(name_len); |
| 330 | if (!probes[i].name) { |
| 331 | PERROR("malloc probe list"); |
| 332 | return -ENOMEM; |
| 333 | } |
| 334 | |
| 335 | ret = snprintf(probes[i].name, name_len, "lttng-probe-%s", next); |
| 336 | if (ret < 0) { |
| 337 | PERROR("snprintf modprobe name"); |
| 338 | goto out; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | out: |
| 343 | nr_probes = i; |
| 344 | return modprobe_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL); |
| 345 | } |