Masami Hiramatsu | 92f6c72 | 2015-07-15 18:14:07 +0900 | [diff] [blame] | 1 | /* |
| 2 | * probe-file.c : operate ftrace k/uprobe events files |
| 3 | * |
| 4 | * Written by Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | */ |
Masami Hiramatsu | dd97549 | 2016-06-15 12:28:30 +0900 | [diff] [blame] | 17 | #include <sys/uio.h> |
Masami Hiramatsu | 92f6c72 | 2015-07-15 18:14:07 +0900 | [diff] [blame] | 18 | #include "util.h" |
| 19 | #include "event.h" |
| 20 | #include "strlist.h" |
| 21 | #include "debug.h" |
| 22 | #include "cache.h" |
| 23 | #include "color.h" |
| 24 | #include "symbol.h" |
| 25 | #include "thread.h" |
Jiri Olsa | fbf9962 | 2015-09-02 09:56:45 +0200 | [diff] [blame] | 26 | #include <api/fs/tracing_path.h> |
Masami Hiramatsu | 92f6c72 | 2015-07-15 18:14:07 +0900 | [diff] [blame] | 27 | #include "probe-event.h" |
| 28 | #include "probe-file.h" |
| 29 | #include "session.h" |
| 30 | |
| 31 | #define MAX_CMDLEN 256 |
| 32 | |
| 33 | static void print_open_warning(int err, bool uprobe) |
| 34 | { |
| 35 | char sbuf[STRERR_BUFSIZE]; |
| 36 | |
| 37 | if (err == -ENOENT) { |
| 38 | const char *config; |
| 39 | |
| 40 | if (uprobe) |
| 41 | config = "CONFIG_UPROBE_EVENTS"; |
| 42 | else |
| 43 | config = "CONFIG_KPROBE_EVENTS"; |
| 44 | |
| 45 | pr_warning("%cprobe_events file does not exist" |
| 46 | " - please rebuild kernel with %s.\n", |
| 47 | uprobe ? 'u' : 'k', config); |
| 48 | } else if (err == -ENOTSUP) |
| 49 | pr_warning("Tracefs or debugfs is not mounted.\n"); |
| 50 | else |
| 51 | pr_warning("Failed to open %cprobe_events: %s\n", |
| 52 | uprobe ? 'u' : 'k', |
| 53 | strerror_r(-err, sbuf, sizeof(sbuf))); |
| 54 | } |
| 55 | |
| 56 | static void print_both_open_warning(int kerr, int uerr) |
| 57 | { |
| 58 | /* Both kprobes and uprobes are disabled, warn it. */ |
| 59 | if (kerr == -ENOTSUP && uerr == -ENOTSUP) |
| 60 | pr_warning("Tracefs or debugfs is not mounted.\n"); |
| 61 | else if (kerr == -ENOENT && uerr == -ENOENT) |
| 62 | pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS " |
| 63 | "or/and CONFIG_UPROBE_EVENTS.\n"); |
| 64 | else { |
| 65 | char sbuf[STRERR_BUFSIZE]; |
| 66 | pr_warning("Failed to open kprobe events: %s.\n", |
| 67 | strerror_r(-kerr, sbuf, sizeof(sbuf))); |
| 68 | pr_warning("Failed to open uprobe events: %s.\n", |
| 69 | strerror_r(-uerr, sbuf, sizeof(sbuf))); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | static int open_probe_events(const char *trace_file, bool readwrite) |
| 74 | { |
| 75 | char buf[PATH_MAX]; |
Masami Hiramatsu | 92f6c72 | 2015-07-15 18:14:07 +0900 | [diff] [blame] | 76 | const char *tracing_dir = ""; |
| 77 | int ret; |
| 78 | |
Masami Hiramatsu | 92f6c72 | 2015-07-15 18:14:07 +0900 | [diff] [blame] | 79 | ret = e_snprintf(buf, PATH_MAX, "%s/%s%s", |
Jiri Olsa | fbf9962 | 2015-09-02 09:56:45 +0200 | [diff] [blame] | 80 | tracing_path, tracing_dir, trace_file); |
Masami Hiramatsu | 92f6c72 | 2015-07-15 18:14:07 +0900 | [diff] [blame] | 81 | if (ret >= 0) { |
| 82 | pr_debug("Opening %s write=%d\n", buf, readwrite); |
| 83 | if (readwrite && !probe_event_dry_run) |
| 84 | ret = open(buf, O_RDWR | O_APPEND, 0); |
| 85 | else |
| 86 | ret = open(buf, O_RDONLY, 0); |
| 87 | |
| 88 | if (ret < 0) |
| 89 | ret = -errno; |
| 90 | } |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | static int open_kprobe_events(bool readwrite) |
| 95 | { |
| 96 | return open_probe_events("kprobe_events", readwrite); |
| 97 | } |
| 98 | |
| 99 | static int open_uprobe_events(bool readwrite) |
| 100 | { |
| 101 | return open_probe_events("uprobe_events", readwrite); |
| 102 | } |
| 103 | |
| 104 | int probe_file__open(int flag) |
| 105 | { |
| 106 | int fd; |
| 107 | |
| 108 | if (flag & PF_FL_UPROBE) |
| 109 | fd = open_uprobe_events(flag & PF_FL_RW); |
| 110 | else |
| 111 | fd = open_kprobe_events(flag & PF_FL_RW); |
| 112 | if (fd < 0) |
| 113 | print_open_warning(fd, flag & PF_FL_UPROBE); |
| 114 | |
| 115 | return fd; |
| 116 | } |
| 117 | |
| 118 | int probe_file__open_both(int *kfd, int *ufd, int flag) |
| 119 | { |
| 120 | if (!kfd || !ufd) |
| 121 | return -EINVAL; |
| 122 | |
| 123 | *kfd = open_kprobe_events(flag & PF_FL_RW); |
| 124 | *ufd = open_uprobe_events(flag & PF_FL_RW); |
| 125 | if (*kfd < 0 && *ufd < 0) { |
| 126 | print_both_open_warning(*kfd, *ufd); |
| 127 | return *kfd; |
| 128 | } |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | /* Get raw string list of current kprobe_events or uprobe_events */ |
| 134 | struct strlist *probe_file__get_rawlist(int fd) |
| 135 | { |
| 136 | int ret, idx; |
| 137 | FILE *fp; |
| 138 | char buf[MAX_CMDLEN]; |
| 139 | char *p; |
| 140 | struct strlist *sl; |
| 141 | |
Wang Nan | 421fd08 | 2015-11-06 09:50:15 +0000 | [diff] [blame] | 142 | if (fd < 0) |
| 143 | return NULL; |
| 144 | |
Masami Hiramatsu | 92f6c72 | 2015-07-15 18:14:07 +0900 | [diff] [blame] | 145 | sl = strlist__new(NULL, NULL); |
| 146 | |
| 147 | fp = fdopen(dup(fd), "r"); |
| 148 | while (!feof(fp)) { |
| 149 | p = fgets(buf, MAX_CMDLEN, fp); |
| 150 | if (!p) |
| 151 | break; |
| 152 | |
| 153 | idx = strlen(p) - 1; |
| 154 | if (p[idx] == '\n') |
| 155 | p[idx] = '\0'; |
| 156 | ret = strlist__add(sl, buf); |
| 157 | if (ret < 0) { |
| 158 | pr_debug("strlist__add failed (%d)\n", ret); |
| 159 | strlist__delete(sl); |
| 160 | return NULL; |
| 161 | } |
| 162 | } |
| 163 | fclose(fp); |
| 164 | |
| 165 | return sl; |
| 166 | } |
| 167 | |
| 168 | static struct strlist *__probe_file__get_namelist(int fd, bool include_group) |
| 169 | { |
| 170 | char buf[128]; |
| 171 | struct strlist *sl, *rawlist; |
| 172 | struct str_node *ent; |
| 173 | struct probe_trace_event tev; |
| 174 | int ret = 0; |
| 175 | |
| 176 | memset(&tev, 0, sizeof(tev)); |
| 177 | rawlist = probe_file__get_rawlist(fd); |
| 178 | if (!rawlist) |
| 179 | return NULL; |
| 180 | sl = strlist__new(NULL, NULL); |
Arnaldo Carvalho de Melo | 602a1f4 | 2016-06-23 11:31:20 -0300 | [diff] [blame] | 181 | strlist__for_each_entry(ent, rawlist) { |
Masami Hiramatsu | 92f6c72 | 2015-07-15 18:14:07 +0900 | [diff] [blame] | 182 | ret = parse_probe_trace_command(ent->s, &tev); |
| 183 | if (ret < 0) |
| 184 | break; |
| 185 | if (include_group) { |
| 186 | ret = e_snprintf(buf, 128, "%s:%s", tev.group, |
| 187 | tev.event); |
| 188 | if (ret >= 0) |
| 189 | ret = strlist__add(sl, buf); |
| 190 | } else |
| 191 | ret = strlist__add(sl, tev.event); |
| 192 | clear_probe_trace_event(&tev); |
| 193 | if (ret < 0) |
| 194 | break; |
| 195 | } |
| 196 | strlist__delete(rawlist); |
| 197 | |
| 198 | if (ret < 0) { |
| 199 | strlist__delete(sl); |
| 200 | return NULL; |
| 201 | } |
| 202 | return sl; |
| 203 | } |
| 204 | |
| 205 | /* Get current perf-probe event names */ |
| 206 | struct strlist *probe_file__get_namelist(int fd) |
| 207 | { |
| 208 | return __probe_file__get_namelist(fd, false); |
| 209 | } |
| 210 | |
| 211 | int probe_file__add_event(int fd, struct probe_trace_event *tev) |
| 212 | { |
| 213 | int ret = 0; |
| 214 | char *buf = synthesize_probe_trace_command(tev); |
| 215 | char sbuf[STRERR_BUFSIZE]; |
| 216 | |
| 217 | if (!buf) { |
| 218 | pr_debug("Failed to synthesize probe trace event.\n"); |
| 219 | return -EINVAL; |
| 220 | } |
| 221 | |
| 222 | pr_debug("Writing event: %s\n", buf); |
| 223 | if (!probe_event_dry_run) { |
Masami Hiramatsu | 6ed0720 | 2016-04-26 18:03:04 +0900 | [diff] [blame] | 224 | if (write(fd, buf, strlen(buf)) < (int)strlen(buf)) { |
Masami Hiramatsu | 92f6c72 | 2015-07-15 18:14:07 +0900 | [diff] [blame] | 225 | ret = -errno; |
| 226 | pr_warning("Failed to write event: %s\n", |
| 227 | strerror_r(errno, sbuf, sizeof(sbuf))); |
| 228 | } |
| 229 | } |
| 230 | free(buf); |
| 231 | |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | static int __del_trace_probe_event(int fd, struct str_node *ent) |
| 236 | { |
| 237 | char *p; |
| 238 | char buf[128]; |
| 239 | int ret; |
| 240 | |
| 241 | /* Convert from perf-probe event to trace-probe event */ |
| 242 | ret = e_snprintf(buf, 128, "-:%s", ent->s); |
| 243 | if (ret < 0) |
| 244 | goto error; |
| 245 | |
| 246 | p = strchr(buf + 2, ':'); |
| 247 | if (!p) { |
| 248 | pr_debug("Internal error: %s should have ':' but not.\n", |
| 249 | ent->s); |
| 250 | ret = -ENOTSUP; |
| 251 | goto error; |
| 252 | } |
| 253 | *p = '/'; |
| 254 | |
| 255 | pr_debug("Writing event: %s\n", buf); |
| 256 | ret = write(fd, buf, strlen(buf)); |
| 257 | if (ret < 0) { |
| 258 | ret = -errno; |
| 259 | goto error; |
| 260 | } |
| 261 | |
Masami Hiramatsu | 92f6c72 | 2015-07-15 18:14:07 +0900 | [diff] [blame] | 262 | return 0; |
| 263 | error: |
| 264 | pr_warning("Failed to delete event: %s\n", |
| 265 | strerror_r(-ret, buf, sizeof(buf))); |
| 266 | return ret; |
| 267 | } |
| 268 | |
Namhyung Kim | e607f14 | 2015-09-04 21:16:03 +0900 | [diff] [blame] | 269 | int probe_file__get_events(int fd, struct strfilter *filter, |
| 270 | struct strlist *plist) |
Masami Hiramatsu | 92f6c72 | 2015-07-15 18:14:07 +0900 | [diff] [blame] | 271 | { |
| 272 | struct strlist *namelist; |
| 273 | struct str_node *ent; |
| 274 | const char *p; |
| 275 | int ret = -ENOENT; |
| 276 | |
Wang Nan | 421fd08 | 2015-11-06 09:50:15 +0000 | [diff] [blame] | 277 | if (!plist) |
| 278 | return -EINVAL; |
| 279 | |
Masami Hiramatsu | 92f6c72 | 2015-07-15 18:14:07 +0900 | [diff] [blame] | 280 | namelist = __probe_file__get_namelist(fd, true); |
| 281 | if (!namelist) |
| 282 | return -ENOENT; |
| 283 | |
Arnaldo Carvalho de Melo | 602a1f4 | 2016-06-23 11:31:20 -0300 | [diff] [blame] | 284 | strlist__for_each_entry(ent, namelist) { |
Masami Hiramatsu | 92f6c72 | 2015-07-15 18:14:07 +0900 | [diff] [blame] | 285 | p = strchr(ent->s, ':'); |
| 286 | if ((p && strfilter__compare(filter, p + 1)) || |
| 287 | strfilter__compare(filter, ent->s)) { |
Namhyung Kim | e7895e4 | 2015-09-04 21:16:02 +0900 | [diff] [blame] | 288 | strlist__add(plist, ent->s); |
| 289 | ret = 0; |
Masami Hiramatsu | 92f6c72 | 2015-07-15 18:14:07 +0900 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | strlist__delete(namelist); |
| 293 | |
| 294 | return ret; |
| 295 | } |
Namhyung Kim | e7895e4 | 2015-09-04 21:16:02 +0900 | [diff] [blame] | 296 | |
Namhyung Kim | e607f14 | 2015-09-04 21:16:03 +0900 | [diff] [blame] | 297 | int probe_file__del_strlist(int fd, struct strlist *namelist) |
Namhyung Kim | e7895e4 | 2015-09-04 21:16:02 +0900 | [diff] [blame] | 298 | { |
| 299 | int ret = 0; |
| 300 | struct str_node *ent; |
| 301 | |
Arnaldo Carvalho de Melo | 602a1f4 | 2016-06-23 11:31:20 -0300 | [diff] [blame] | 302 | strlist__for_each_entry(ent, namelist) { |
Namhyung Kim | e7895e4 | 2015-09-04 21:16:02 +0900 | [diff] [blame] | 303 | ret = __del_trace_probe_event(fd, ent); |
| 304 | if (ret < 0) |
| 305 | break; |
| 306 | } |
| 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | int probe_file__del_events(int fd, struct strfilter *filter) |
| 311 | { |
| 312 | struct strlist *namelist; |
| 313 | int ret; |
| 314 | |
| 315 | namelist = strlist__new(NULL, NULL); |
| 316 | if (!namelist) |
| 317 | return -ENOMEM; |
| 318 | |
| 319 | ret = probe_file__get_events(fd, filter, namelist); |
| 320 | if (ret < 0) |
| 321 | return ret; |
| 322 | |
| 323 | ret = probe_file__del_strlist(fd, namelist); |
| 324 | strlist__delete(namelist); |
| 325 | |
| 326 | return ret; |
| 327 | } |
Masami Hiramatsu | dd97549 | 2016-06-15 12:28:30 +0900 | [diff] [blame] | 328 | |
| 329 | /* Caller must ensure to remove this entry from list */ |
| 330 | static void probe_cache_entry__delete(struct probe_cache_entry *entry) |
| 331 | { |
| 332 | if (entry) { |
| 333 | BUG_ON(!list_empty(&entry->node)); |
| 334 | |
| 335 | strlist__delete(entry->tevlist); |
| 336 | clear_perf_probe_event(&entry->pev); |
| 337 | zfree(&entry->spev); |
| 338 | free(entry); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | static struct probe_cache_entry * |
| 343 | probe_cache_entry__new(struct perf_probe_event *pev) |
| 344 | { |
| 345 | struct probe_cache_entry *entry = zalloc(sizeof(*entry)); |
| 346 | |
| 347 | if (entry) { |
| 348 | INIT_LIST_HEAD(&entry->node); |
| 349 | entry->tevlist = strlist__new(NULL, NULL); |
| 350 | if (!entry->tevlist) |
| 351 | zfree(&entry); |
| 352 | else if (pev) { |
| 353 | entry->spev = synthesize_perf_probe_command(pev); |
| 354 | if (!entry->spev || |
| 355 | perf_probe_event__copy(&entry->pev, pev) < 0) { |
| 356 | probe_cache_entry__delete(entry); |
| 357 | return NULL; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | return entry; |
| 363 | } |
| 364 | |
| 365 | /* For the kernel probe caches, pass target = NULL */ |
| 366 | static int probe_cache__open(struct probe_cache *pcache, const char *target) |
| 367 | { |
| 368 | char cpath[PATH_MAX]; |
| 369 | char sbuildid[SBUILD_ID_SIZE]; |
| 370 | char *dir_name; |
| 371 | bool is_kallsyms = !target; |
| 372 | int ret, fd; |
| 373 | |
| 374 | if (target) |
| 375 | ret = filename__sprintf_build_id(target, sbuildid); |
| 376 | else { |
| 377 | target = DSO__NAME_KALLSYMS; |
| 378 | ret = sysfs__sprintf_build_id("/", sbuildid); |
| 379 | } |
| 380 | if (ret < 0) { |
| 381 | pr_debug("Failed to get build-id from %s.\n", target); |
| 382 | return ret; |
| 383 | } |
| 384 | |
| 385 | /* If we have no buildid cache, make it */ |
| 386 | if (!build_id_cache__cached(sbuildid)) { |
| 387 | ret = build_id_cache__add_s(sbuildid, target, |
| 388 | is_kallsyms, NULL); |
| 389 | if (ret < 0) { |
| 390 | pr_debug("Failed to add build-id cache: %s\n", target); |
| 391 | return ret; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | dir_name = build_id_cache__cachedir(sbuildid, target, is_kallsyms, |
| 396 | false); |
| 397 | if (!dir_name) |
| 398 | return -ENOMEM; |
| 399 | |
| 400 | snprintf(cpath, PATH_MAX, "%s/probes", dir_name); |
| 401 | fd = open(cpath, O_CREAT | O_RDWR, 0644); |
| 402 | if (fd < 0) |
| 403 | pr_debug("Failed to open cache(%d): %s\n", fd, cpath); |
| 404 | free(dir_name); |
| 405 | pcache->fd = fd; |
| 406 | |
| 407 | return fd; |
| 408 | } |
| 409 | |
| 410 | static int probe_cache__load(struct probe_cache *pcache) |
| 411 | { |
| 412 | struct probe_cache_entry *entry = NULL; |
| 413 | char buf[MAX_CMDLEN], *p; |
| 414 | int ret = 0; |
| 415 | FILE *fp; |
| 416 | |
| 417 | fp = fdopen(dup(pcache->fd), "r"); |
| 418 | if (!fp) |
| 419 | return -EINVAL; |
| 420 | |
| 421 | while (!feof(fp)) { |
| 422 | if (!fgets(buf, MAX_CMDLEN, fp)) |
| 423 | break; |
| 424 | p = strchr(buf, '\n'); |
| 425 | if (p) |
| 426 | *p = '\0'; |
| 427 | if (buf[0] == '#') { /* #perf_probe_event */ |
| 428 | entry = probe_cache_entry__new(NULL); |
| 429 | if (!entry) { |
| 430 | ret = -ENOMEM; |
| 431 | goto out; |
| 432 | } |
| 433 | entry->spev = strdup(buf + 1); |
| 434 | if (entry->spev) |
| 435 | ret = parse_perf_probe_command(buf + 1, |
| 436 | &entry->pev); |
| 437 | else |
| 438 | ret = -ENOMEM; |
| 439 | if (ret < 0) { |
| 440 | probe_cache_entry__delete(entry); |
| 441 | goto out; |
| 442 | } |
| 443 | list_add_tail(&entry->node, &pcache->entries); |
| 444 | } else { /* trace_probe_event */ |
| 445 | if (!entry) { |
| 446 | ret = -EINVAL; |
| 447 | goto out; |
| 448 | } |
| 449 | strlist__add(entry->tevlist, buf); |
| 450 | } |
| 451 | } |
| 452 | out: |
| 453 | fclose(fp); |
| 454 | return ret; |
| 455 | } |
| 456 | |
| 457 | static struct probe_cache *probe_cache__alloc(void) |
| 458 | { |
| 459 | struct probe_cache *pcache = zalloc(sizeof(*pcache)); |
| 460 | |
| 461 | if (pcache) { |
| 462 | INIT_LIST_HEAD(&pcache->entries); |
| 463 | pcache->fd = -EINVAL; |
| 464 | } |
| 465 | return pcache; |
| 466 | } |
| 467 | |
| 468 | void probe_cache__purge(struct probe_cache *pcache) |
| 469 | { |
| 470 | struct probe_cache_entry *entry, *n; |
| 471 | |
| 472 | list_for_each_entry_safe(entry, n, &pcache->entries, node) { |
| 473 | list_del_init(&entry->node); |
| 474 | probe_cache_entry__delete(entry); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | void probe_cache__delete(struct probe_cache *pcache) |
| 479 | { |
| 480 | if (!pcache) |
| 481 | return; |
| 482 | |
| 483 | probe_cache__purge(pcache); |
| 484 | if (pcache->fd > 0) |
| 485 | close(pcache->fd); |
| 486 | free(pcache); |
| 487 | } |
| 488 | |
| 489 | struct probe_cache *probe_cache__new(const char *target) |
| 490 | { |
| 491 | struct probe_cache *pcache = probe_cache__alloc(); |
| 492 | int ret; |
| 493 | |
| 494 | if (!pcache) |
| 495 | return NULL; |
| 496 | |
| 497 | ret = probe_cache__open(pcache, target); |
| 498 | if (ret < 0) { |
| 499 | pr_debug("Cache open error: %d\n", ret); |
| 500 | goto out_err; |
| 501 | } |
| 502 | |
| 503 | ret = probe_cache__load(pcache); |
| 504 | if (ret < 0) { |
| 505 | pr_debug("Cache read error: %d\n", ret); |
| 506 | goto out_err; |
| 507 | } |
| 508 | |
| 509 | return pcache; |
| 510 | |
| 511 | out_err: |
| 512 | probe_cache__delete(pcache); |
| 513 | return NULL; |
| 514 | } |
| 515 | |
| 516 | static bool streql(const char *a, const char *b) |
| 517 | { |
| 518 | if (a == b) |
| 519 | return true; |
| 520 | |
| 521 | if (!a || !b) |
| 522 | return false; |
| 523 | |
| 524 | return !strcmp(a, b); |
| 525 | } |
| 526 | |
Masami Hiramatsu | bc06223 | 2016-07-01 17:03:12 +0900 | [diff] [blame^] | 527 | struct probe_cache_entry * |
Masami Hiramatsu | dd97549 | 2016-06-15 12:28:30 +0900 | [diff] [blame] | 528 | probe_cache__find(struct probe_cache *pcache, struct perf_probe_event *pev) |
| 529 | { |
| 530 | struct probe_cache_entry *entry = NULL; |
| 531 | char *cmd = synthesize_perf_probe_command(pev); |
| 532 | |
| 533 | if (!cmd) |
| 534 | return NULL; |
| 535 | |
| 536 | list_for_each_entry(entry, &pcache->entries, node) { |
| 537 | /* Hit if same event name or same command-string */ |
| 538 | if ((pev->event && |
| 539 | (streql(entry->pev.group, pev->group) && |
| 540 | streql(entry->pev.event, pev->event))) || |
| 541 | (!strcmp(entry->spev, cmd))) |
| 542 | goto found; |
| 543 | } |
| 544 | entry = NULL; |
| 545 | |
| 546 | found: |
| 547 | free(cmd); |
| 548 | return entry; |
| 549 | } |
| 550 | |
Masami Hiramatsu | bc06223 | 2016-07-01 17:03:12 +0900 | [diff] [blame^] | 551 | struct probe_cache_entry * |
| 552 | probe_cache__find_by_name(struct probe_cache *pcache, |
| 553 | const char *group, const char *event) |
| 554 | { |
| 555 | struct probe_cache_entry *entry = NULL; |
| 556 | |
| 557 | list_for_each_entry(entry, &pcache->entries, node) { |
| 558 | /* Hit if same event name or same command-string */ |
| 559 | if (streql(entry->pev.group, group) && |
| 560 | streql(entry->pev.event, event)) |
| 561 | goto found; |
| 562 | } |
| 563 | entry = NULL; |
| 564 | |
| 565 | found: |
| 566 | return entry; |
| 567 | } |
| 568 | |
Masami Hiramatsu | dd97549 | 2016-06-15 12:28:30 +0900 | [diff] [blame] | 569 | int probe_cache__add_entry(struct probe_cache *pcache, |
| 570 | struct perf_probe_event *pev, |
| 571 | struct probe_trace_event *tevs, int ntevs) |
| 572 | { |
| 573 | struct probe_cache_entry *entry = NULL; |
| 574 | char *command; |
| 575 | int i, ret = 0; |
| 576 | |
| 577 | if (!pcache || !pev || !tevs || ntevs <= 0) { |
| 578 | ret = -EINVAL; |
| 579 | goto out_err; |
| 580 | } |
| 581 | |
| 582 | /* Remove old cache entry */ |
| 583 | entry = probe_cache__find(pcache, pev); |
| 584 | if (entry) { |
| 585 | list_del_init(&entry->node); |
| 586 | probe_cache_entry__delete(entry); |
| 587 | } |
| 588 | |
| 589 | ret = -ENOMEM; |
| 590 | entry = probe_cache_entry__new(pev); |
| 591 | if (!entry) |
| 592 | goto out_err; |
| 593 | |
| 594 | for (i = 0; i < ntevs; i++) { |
| 595 | if (!tevs[i].point.symbol) |
| 596 | continue; |
| 597 | |
| 598 | command = synthesize_probe_trace_command(&tevs[i]); |
| 599 | if (!command) |
| 600 | goto out_err; |
| 601 | strlist__add(entry->tevlist, command); |
| 602 | free(command); |
| 603 | } |
| 604 | list_add_tail(&entry->node, &pcache->entries); |
| 605 | pr_debug("Added probe cache: %d\n", ntevs); |
| 606 | return 0; |
| 607 | |
| 608 | out_err: |
| 609 | pr_debug("Failed to add probe caches\n"); |
| 610 | probe_cache_entry__delete(entry); |
| 611 | return ret; |
| 612 | } |
| 613 | |
| 614 | static int probe_cache_entry__write(struct probe_cache_entry *entry, int fd) |
| 615 | { |
| 616 | struct str_node *snode; |
| 617 | struct stat st; |
| 618 | struct iovec iov[3]; |
| 619 | int ret; |
| 620 | /* Save stat for rollback */ |
| 621 | ret = fstat(fd, &st); |
| 622 | if (ret < 0) |
| 623 | return ret; |
| 624 | |
| 625 | pr_debug("Writing cache: #%s\n", entry->spev); |
| 626 | iov[0].iov_base = (void *)"#"; iov[0].iov_len = 1; |
| 627 | iov[1].iov_base = entry->spev; iov[1].iov_len = strlen(entry->spev); |
| 628 | iov[2].iov_base = (void *)"\n"; iov[2].iov_len = 1; |
| 629 | ret = writev(fd, iov, 3); |
| 630 | if (ret < (int)iov[1].iov_len + 2) |
| 631 | goto rollback; |
| 632 | |
Arnaldo Carvalho de Melo | 602a1f4 | 2016-06-23 11:31:20 -0300 | [diff] [blame] | 633 | strlist__for_each_entry(snode, entry->tevlist) { |
Masami Hiramatsu | dd97549 | 2016-06-15 12:28:30 +0900 | [diff] [blame] | 634 | iov[0].iov_base = (void *)snode->s; |
| 635 | iov[0].iov_len = strlen(snode->s); |
| 636 | iov[1].iov_base = (void *)"\n"; iov[1].iov_len = 1; |
| 637 | ret = writev(fd, iov, 2); |
| 638 | if (ret < (int)iov[0].iov_len + 1) |
| 639 | goto rollback; |
| 640 | } |
| 641 | return 0; |
| 642 | |
| 643 | rollback: |
| 644 | /* Rollback to avoid cache file corruption */ |
| 645 | if (ret > 0) |
| 646 | ret = -1; |
| 647 | if (ftruncate(fd, st.st_size) < 0) |
| 648 | ret = -2; |
| 649 | |
| 650 | return ret; |
| 651 | } |
| 652 | |
| 653 | int probe_cache__commit(struct probe_cache *pcache) |
| 654 | { |
| 655 | struct probe_cache_entry *entry; |
| 656 | int ret = 0; |
| 657 | |
| 658 | /* TBD: if we do not update existing entries, skip it */ |
| 659 | ret = lseek(pcache->fd, 0, SEEK_SET); |
| 660 | if (ret < 0) |
| 661 | goto out; |
| 662 | |
| 663 | ret = ftruncate(pcache->fd, 0); |
| 664 | if (ret < 0) |
| 665 | goto out; |
| 666 | |
| 667 | list_for_each_entry(entry, &pcache->entries, node) { |
| 668 | ret = probe_cache_entry__write(entry, pcache->fd); |
| 669 | pr_debug("Cache committed: %d\n", ret); |
| 670 | if (ret < 0) |
| 671 | break; |
| 672 | } |
| 673 | out: |
| 674 | return ret; |
| 675 | } |