blob: 436b64731f65112f3c624a70a077f7370532da5a [file] [log] [blame]
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09001/*
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 Hiramatsudd975492016-06-15 12:28:30 +090017#include <sys/uio.h>
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090018#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 Olsafbf99622015-09-02 09:56:45 +020026#include <api/fs/tracing_path.h>
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090027#include "probe-event.h"
28#include "probe-file.h"
29#include "session.h"
30
31#define MAX_CMDLEN 256
32
33static 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',
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030053 str_error_r(-err, sbuf, sizeof(sbuf)));
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090054}
55
56static 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",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030067 str_error_r(-kerr, sbuf, sizeof(sbuf)));
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090068 pr_warning("Failed to open uprobe events: %s.\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030069 str_error_r(-uerr, sbuf, sizeof(sbuf)));
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090070 }
71}
72
73static int open_probe_events(const char *trace_file, bool readwrite)
74{
75 char buf[PATH_MAX];
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090076 int ret;
77
Masami Hiramatsu04e11962016-08-24 14:57:58 +090078 ret = e_snprintf(buf, PATH_MAX, "%s/%s",
79 tracing_path, trace_file);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090080 if (ret >= 0) {
81 pr_debug("Opening %s write=%d\n", buf, readwrite);
82 if (readwrite && !probe_event_dry_run)
83 ret = open(buf, O_RDWR | O_APPEND, 0);
84 else
85 ret = open(buf, O_RDONLY, 0);
86
87 if (ret < 0)
88 ret = -errno;
89 }
90 return ret;
91}
92
93static int open_kprobe_events(bool readwrite)
94{
95 return open_probe_events("kprobe_events", readwrite);
96}
97
98static int open_uprobe_events(bool readwrite)
99{
100 return open_probe_events("uprobe_events", readwrite);
101}
102
103int probe_file__open(int flag)
104{
105 int fd;
106
107 if (flag & PF_FL_UPROBE)
108 fd = open_uprobe_events(flag & PF_FL_RW);
109 else
110 fd = open_kprobe_events(flag & PF_FL_RW);
111 if (fd < 0)
112 print_open_warning(fd, flag & PF_FL_UPROBE);
113
114 return fd;
115}
116
117int probe_file__open_both(int *kfd, int *ufd, int flag)
118{
119 if (!kfd || !ufd)
120 return -EINVAL;
121
122 *kfd = open_kprobe_events(flag & PF_FL_RW);
123 *ufd = open_uprobe_events(flag & PF_FL_RW);
124 if (*kfd < 0 && *ufd < 0) {
125 print_both_open_warning(*kfd, *ufd);
126 return *kfd;
127 }
128
129 return 0;
130}
131
132/* Get raw string list of current kprobe_events or uprobe_events */
133struct strlist *probe_file__get_rawlist(int fd)
134{
Colin Ian King03258622016-08-12 22:44:56 +0100135 int ret, idx, fddup;
Masami Hiramatsu92f6c722015-07-15 18:14:07 +0900136 FILE *fp;
137 char buf[MAX_CMDLEN];
138 char *p;
139 struct strlist *sl;
140
Wang Nan421fd082015-11-06 09:50:15 +0000141 if (fd < 0)
142 return NULL;
143
Masami Hiramatsu92f6c722015-07-15 18:14:07 +0900144 sl = strlist__new(NULL, NULL);
Arnaldo Carvalho de Melo60ebc152016-08-15 17:06:47 -0300145 if (sl == NULL)
146 return NULL;
Masami Hiramatsu92f6c722015-07-15 18:14:07 +0900147
Colin Ian King03258622016-08-12 22:44:56 +0100148 fddup = dup(fd);
149 if (fddup < 0)
150 goto out_free_sl;
151
152 fp = fdopen(fddup, "r");
153 if (!fp)
154 goto out_close_fddup;
155
Masami Hiramatsu92f6c722015-07-15 18:14:07 +0900156 while (!feof(fp)) {
157 p = fgets(buf, MAX_CMDLEN, fp);
158 if (!p)
159 break;
160
161 idx = strlen(p) - 1;
162 if (p[idx] == '\n')
163 p[idx] = '\0';
164 ret = strlist__add(sl, buf);
165 if (ret < 0) {
166 pr_debug("strlist__add failed (%d)\n", ret);
Arnaldo Carvalho de Melo60ebc152016-08-15 17:06:47 -0300167 goto out_close_fp;
Masami Hiramatsu92f6c722015-07-15 18:14:07 +0900168 }
169 }
170 fclose(fp);
171
172 return sl;
Colin Ian King03258622016-08-12 22:44:56 +0100173
Arnaldo Carvalho de Melo60ebc152016-08-15 17:06:47 -0300174out_close_fp:
175 fclose(fp);
176 goto out_free_sl;
Colin Ian King03258622016-08-12 22:44:56 +0100177out_close_fddup:
178 close(fddup);
179out_free_sl:
180 strlist__delete(sl);
181 return NULL;
Masami Hiramatsu92f6c722015-07-15 18:14:07 +0900182}
183
184static struct strlist *__probe_file__get_namelist(int fd, bool include_group)
185{
186 char buf[128];
187 struct strlist *sl, *rawlist;
188 struct str_node *ent;
189 struct probe_trace_event tev;
190 int ret = 0;
191
192 memset(&tev, 0, sizeof(tev));
193 rawlist = probe_file__get_rawlist(fd);
194 if (!rawlist)
195 return NULL;
196 sl = strlist__new(NULL, NULL);
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -0300197 strlist__for_each_entry(ent, rawlist) {
Masami Hiramatsu92f6c722015-07-15 18:14:07 +0900198 ret = parse_probe_trace_command(ent->s, &tev);
199 if (ret < 0)
200 break;
201 if (include_group) {
202 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
203 tev.event);
204 if (ret >= 0)
205 ret = strlist__add(sl, buf);
206 } else
207 ret = strlist__add(sl, tev.event);
208 clear_probe_trace_event(&tev);
209 if (ret < 0)
210 break;
211 }
212 strlist__delete(rawlist);
213
214 if (ret < 0) {
215 strlist__delete(sl);
216 return NULL;
217 }
218 return sl;
219}
220
221/* Get current perf-probe event names */
222struct strlist *probe_file__get_namelist(int fd)
223{
224 return __probe_file__get_namelist(fd, false);
225}
226
227int probe_file__add_event(int fd, struct probe_trace_event *tev)
228{
229 int ret = 0;
230 char *buf = synthesize_probe_trace_command(tev);
231 char sbuf[STRERR_BUFSIZE];
232
233 if (!buf) {
234 pr_debug("Failed to synthesize probe trace event.\n");
235 return -EINVAL;
236 }
237
238 pr_debug("Writing event: %s\n", buf);
239 if (!probe_event_dry_run) {
Masami Hiramatsu6ed07202016-04-26 18:03:04 +0900240 if (write(fd, buf, strlen(buf)) < (int)strlen(buf)) {
Masami Hiramatsu92f6c722015-07-15 18:14:07 +0900241 ret = -errno;
242 pr_warning("Failed to write event: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300243 str_error_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu92f6c722015-07-15 18:14:07 +0900244 }
245 }
246 free(buf);
247
248 return ret;
249}
250
251static int __del_trace_probe_event(int fd, struct str_node *ent)
252{
253 char *p;
254 char buf[128];
255 int ret;
256
257 /* Convert from perf-probe event to trace-probe event */
258 ret = e_snprintf(buf, 128, "-:%s", ent->s);
259 if (ret < 0)
260 goto error;
261
262 p = strchr(buf + 2, ':');
263 if (!p) {
264 pr_debug("Internal error: %s should have ':' but not.\n",
265 ent->s);
266 ret = -ENOTSUP;
267 goto error;
268 }
269 *p = '/';
270
271 pr_debug("Writing event: %s\n", buf);
272 ret = write(fd, buf, strlen(buf));
273 if (ret < 0) {
274 ret = -errno;
275 goto error;
276 }
277
Masami Hiramatsu92f6c722015-07-15 18:14:07 +0900278 return 0;
279error:
280 pr_warning("Failed to delete event: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300281 str_error_r(-ret, buf, sizeof(buf)));
Masami Hiramatsu92f6c722015-07-15 18:14:07 +0900282 return ret;
283}
284
Namhyung Kime607f142015-09-04 21:16:03 +0900285int probe_file__get_events(int fd, struct strfilter *filter,
286 struct strlist *plist)
Masami Hiramatsu92f6c722015-07-15 18:14:07 +0900287{
288 struct strlist *namelist;
289 struct str_node *ent;
290 const char *p;
291 int ret = -ENOENT;
292
Wang Nan421fd082015-11-06 09:50:15 +0000293 if (!plist)
294 return -EINVAL;
295
Masami Hiramatsu92f6c722015-07-15 18:14:07 +0900296 namelist = __probe_file__get_namelist(fd, true);
297 if (!namelist)
298 return -ENOENT;
299
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -0300300 strlist__for_each_entry(ent, namelist) {
Masami Hiramatsu92f6c722015-07-15 18:14:07 +0900301 p = strchr(ent->s, ':');
302 if ((p && strfilter__compare(filter, p + 1)) ||
303 strfilter__compare(filter, ent->s)) {
Namhyung Kime7895e42015-09-04 21:16:02 +0900304 strlist__add(plist, ent->s);
305 ret = 0;
Masami Hiramatsu92f6c722015-07-15 18:14:07 +0900306 }
307 }
308 strlist__delete(namelist);
309
310 return ret;
311}
Namhyung Kime7895e42015-09-04 21:16:02 +0900312
Namhyung Kime607f142015-09-04 21:16:03 +0900313int probe_file__del_strlist(int fd, struct strlist *namelist)
Namhyung Kime7895e42015-09-04 21:16:02 +0900314{
315 int ret = 0;
316 struct str_node *ent;
317
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -0300318 strlist__for_each_entry(ent, namelist) {
Namhyung Kime7895e42015-09-04 21:16:02 +0900319 ret = __del_trace_probe_event(fd, ent);
320 if (ret < 0)
321 break;
322 }
323 return ret;
324}
325
326int probe_file__del_events(int fd, struct strfilter *filter)
327{
328 struct strlist *namelist;
329 int ret;
330
331 namelist = strlist__new(NULL, NULL);
332 if (!namelist)
333 return -ENOMEM;
334
335 ret = probe_file__get_events(fd, filter, namelist);
336 if (ret < 0)
337 return ret;
338
339 ret = probe_file__del_strlist(fd, namelist);
340 strlist__delete(namelist);
341
342 return ret;
343}
Masami Hiramatsudd975492016-06-15 12:28:30 +0900344
345/* Caller must ensure to remove this entry from list */
346static void probe_cache_entry__delete(struct probe_cache_entry *entry)
347{
348 if (entry) {
349 BUG_ON(!list_empty(&entry->node));
350
351 strlist__delete(entry->tevlist);
352 clear_perf_probe_event(&entry->pev);
353 zfree(&entry->spev);
354 free(entry);
355 }
356}
357
358static struct probe_cache_entry *
359probe_cache_entry__new(struct perf_probe_event *pev)
360{
361 struct probe_cache_entry *entry = zalloc(sizeof(*entry));
362
363 if (entry) {
364 INIT_LIST_HEAD(&entry->node);
365 entry->tevlist = strlist__new(NULL, NULL);
366 if (!entry->tevlist)
367 zfree(&entry);
368 else if (pev) {
369 entry->spev = synthesize_perf_probe_command(pev);
370 if (!entry->spev ||
371 perf_probe_event__copy(&entry->pev, pev) < 0) {
372 probe_cache_entry__delete(entry);
373 return NULL;
374 }
375 }
376 }
377
378 return entry;
379}
380
Masami Hiramatsu42bba262016-07-12 19:05:18 +0900381int probe_cache_entry__get_event(struct probe_cache_entry *entry,
382 struct probe_trace_event **tevs)
383{
384 struct probe_trace_event *tev;
385 struct str_node *node;
386 int ret, i;
387
388 ret = strlist__nr_entries(entry->tevlist);
389 if (ret > probe_conf.max_probes)
390 return -E2BIG;
391
392 *tevs = zalloc(ret * sizeof(*tev));
393 if (!*tevs)
394 return -ENOMEM;
395
396 i = 0;
397 strlist__for_each_entry(node, entry->tevlist) {
398 tev = &(*tevs)[i++];
399 ret = parse_probe_trace_command(node->s, tev);
400 if (ret < 0)
401 break;
402 }
403 return i;
404}
405
406/* For the kernel probe caches, pass target = NULL or DSO__NAME_KALLSYMS */
Masami Hiramatsudd975492016-06-15 12:28:30 +0900407static int probe_cache__open(struct probe_cache *pcache, const char *target)
408{
409 char cpath[PATH_MAX];
410 char sbuildid[SBUILD_ID_SIZE];
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900411 char *dir_name = NULL;
Masami Hiramatsu42bba262016-07-12 19:05:18 +0900412 bool is_kallsyms = false;
Masami Hiramatsudd975492016-06-15 12:28:30 +0900413 int ret, fd;
414
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900415 if (target && build_id_cache__cached(target)) {
416 /* This is a cached buildid */
417 strncpy(sbuildid, target, SBUILD_ID_SIZE);
418 dir_name = build_id_cache__linkname(sbuildid, NULL, 0);
419 goto found;
420 }
421
Masami Hiramatsu42bba262016-07-12 19:05:18 +0900422 if (!target || !strcmp(target, DSO__NAME_KALLSYMS)) {
Masami Hiramatsudd975492016-06-15 12:28:30 +0900423 target = DSO__NAME_KALLSYMS;
Masami Hiramatsu42bba262016-07-12 19:05:18 +0900424 is_kallsyms = true;
Masami Hiramatsudd975492016-06-15 12:28:30 +0900425 ret = sysfs__sprintf_build_id("/", sbuildid);
Masami Hiramatsu42bba262016-07-12 19:05:18 +0900426 } else
427 ret = filename__sprintf_build_id(target, sbuildid);
428
Masami Hiramatsudd975492016-06-15 12:28:30 +0900429 if (ret < 0) {
430 pr_debug("Failed to get build-id from %s.\n", target);
431 return ret;
432 }
433
434 /* If we have no buildid cache, make it */
435 if (!build_id_cache__cached(sbuildid)) {
436 ret = build_id_cache__add_s(sbuildid, target,
437 is_kallsyms, NULL);
438 if (ret < 0) {
439 pr_debug("Failed to add build-id cache: %s\n", target);
440 return ret;
441 }
442 }
443
444 dir_name = build_id_cache__cachedir(sbuildid, target, is_kallsyms,
445 false);
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900446found:
447 if (!dir_name) {
448 pr_debug("Failed to get cache from %s\n", target);
Masami Hiramatsudd975492016-06-15 12:28:30 +0900449 return -ENOMEM;
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900450 }
Masami Hiramatsudd975492016-06-15 12:28:30 +0900451
452 snprintf(cpath, PATH_MAX, "%s/probes", dir_name);
453 fd = open(cpath, O_CREAT | O_RDWR, 0644);
454 if (fd < 0)
455 pr_debug("Failed to open cache(%d): %s\n", fd, cpath);
456 free(dir_name);
457 pcache->fd = fd;
458
459 return fd;
460}
461
462static int probe_cache__load(struct probe_cache *pcache)
463{
464 struct probe_cache_entry *entry = NULL;
465 char buf[MAX_CMDLEN], *p;
Colin Ian King03258622016-08-12 22:44:56 +0100466 int ret = 0, fddup;
Masami Hiramatsudd975492016-06-15 12:28:30 +0900467 FILE *fp;
468
Colin Ian King03258622016-08-12 22:44:56 +0100469 fddup = dup(pcache->fd);
470 if (fddup < 0)
471 return -errno;
472 fp = fdopen(fddup, "r");
Arnaldo Carvalho de Melo60ebc152016-08-15 17:06:47 -0300473 if (!fp) {
474 close(fddup);
Masami Hiramatsudd975492016-06-15 12:28:30 +0900475 return -EINVAL;
Arnaldo Carvalho de Melo60ebc152016-08-15 17:06:47 -0300476 }
Masami Hiramatsudd975492016-06-15 12:28:30 +0900477
478 while (!feof(fp)) {
479 if (!fgets(buf, MAX_CMDLEN, fp))
480 break;
481 p = strchr(buf, '\n');
482 if (p)
483 *p = '\0';
Masami Hiramatsu6430a942016-07-01 17:04:10 +0900484 /* #perf_probe_event or %sdt_event */
485 if (buf[0] == '#' || buf[0] == '%') {
Masami Hiramatsudd975492016-06-15 12:28:30 +0900486 entry = probe_cache_entry__new(NULL);
487 if (!entry) {
488 ret = -ENOMEM;
489 goto out;
490 }
Masami Hiramatsu6430a942016-07-01 17:04:10 +0900491 if (buf[0] == '%')
492 entry->sdt = true;
Masami Hiramatsudd975492016-06-15 12:28:30 +0900493 entry->spev = strdup(buf + 1);
494 if (entry->spev)
495 ret = parse_perf_probe_command(buf + 1,
496 &entry->pev);
497 else
498 ret = -ENOMEM;
499 if (ret < 0) {
500 probe_cache_entry__delete(entry);
501 goto out;
502 }
503 list_add_tail(&entry->node, &pcache->entries);
504 } else { /* trace_probe_event */
505 if (!entry) {
506 ret = -EINVAL;
507 goto out;
508 }
509 strlist__add(entry->tevlist, buf);
510 }
511 }
512out:
513 fclose(fp);
514 return ret;
515}
516
517static struct probe_cache *probe_cache__alloc(void)
518{
519 struct probe_cache *pcache = zalloc(sizeof(*pcache));
520
521 if (pcache) {
522 INIT_LIST_HEAD(&pcache->entries);
523 pcache->fd = -EINVAL;
524 }
525 return pcache;
526}
527
528void probe_cache__purge(struct probe_cache *pcache)
529{
530 struct probe_cache_entry *entry, *n;
531
532 list_for_each_entry_safe(entry, n, &pcache->entries, node) {
533 list_del_init(&entry->node);
534 probe_cache_entry__delete(entry);
535 }
536}
537
538void probe_cache__delete(struct probe_cache *pcache)
539{
540 if (!pcache)
541 return;
542
543 probe_cache__purge(pcache);
544 if (pcache->fd > 0)
545 close(pcache->fd);
546 free(pcache);
547}
548
549struct probe_cache *probe_cache__new(const char *target)
550{
551 struct probe_cache *pcache = probe_cache__alloc();
552 int ret;
553
554 if (!pcache)
555 return NULL;
556
557 ret = probe_cache__open(pcache, target);
558 if (ret < 0) {
559 pr_debug("Cache open error: %d\n", ret);
560 goto out_err;
561 }
562
563 ret = probe_cache__load(pcache);
564 if (ret < 0) {
565 pr_debug("Cache read error: %d\n", ret);
566 goto out_err;
567 }
568
569 return pcache;
570
571out_err:
572 probe_cache__delete(pcache);
573 return NULL;
574}
575
576static bool streql(const char *a, const char *b)
577{
578 if (a == b)
579 return true;
580
581 if (!a || !b)
582 return false;
583
584 return !strcmp(a, b);
585}
586
Masami Hiramatsubc062232016-07-01 17:03:12 +0900587struct probe_cache_entry *
Masami Hiramatsudd975492016-06-15 12:28:30 +0900588probe_cache__find(struct probe_cache *pcache, struct perf_probe_event *pev)
589{
590 struct probe_cache_entry *entry = NULL;
591 char *cmd = synthesize_perf_probe_command(pev);
592
593 if (!cmd)
594 return NULL;
595
Masami Hiramatsu05bf2c82016-07-12 19:05:04 +0900596 for_each_probe_cache_entry(entry, pcache) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +0900597 if (pev->sdt) {
598 if (entry->pev.event &&
599 streql(entry->pev.event, pev->event) &&
600 (!pev->group ||
601 streql(entry->pev.group, pev->group)))
602 goto found;
603
604 continue;
605 }
Masami Hiramatsudd975492016-06-15 12:28:30 +0900606 /* Hit if same event name or same command-string */
607 if ((pev->event &&
608 (streql(entry->pev.group, pev->group) &&
609 streql(entry->pev.event, pev->event))) ||
610 (!strcmp(entry->spev, cmd)))
611 goto found;
612 }
613 entry = NULL;
614
615found:
616 free(cmd);
617 return entry;
618}
619
Masami Hiramatsubc062232016-07-01 17:03:12 +0900620struct probe_cache_entry *
621probe_cache__find_by_name(struct probe_cache *pcache,
622 const char *group, const char *event)
623{
624 struct probe_cache_entry *entry = NULL;
625
Masami Hiramatsu05bf2c82016-07-12 19:05:04 +0900626 for_each_probe_cache_entry(entry, pcache) {
Masami Hiramatsubc062232016-07-01 17:03:12 +0900627 /* Hit if same event name or same command-string */
628 if (streql(entry->pev.group, group) &&
629 streql(entry->pev.event, event))
630 goto found;
631 }
632 entry = NULL;
633
634found:
635 return entry;
636}
637
Masami Hiramatsudd975492016-06-15 12:28:30 +0900638int probe_cache__add_entry(struct probe_cache *pcache,
639 struct perf_probe_event *pev,
640 struct probe_trace_event *tevs, int ntevs)
641{
642 struct probe_cache_entry *entry = NULL;
643 char *command;
644 int i, ret = 0;
645
646 if (!pcache || !pev || !tevs || ntevs <= 0) {
647 ret = -EINVAL;
648 goto out_err;
649 }
650
651 /* Remove old cache entry */
652 entry = probe_cache__find(pcache, pev);
653 if (entry) {
654 list_del_init(&entry->node);
655 probe_cache_entry__delete(entry);
656 }
657
658 ret = -ENOMEM;
659 entry = probe_cache_entry__new(pev);
660 if (!entry)
661 goto out_err;
662
663 for (i = 0; i < ntevs; i++) {
664 if (!tevs[i].point.symbol)
665 continue;
666
667 command = synthesize_probe_trace_command(&tevs[i]);
668 if (!command)
669 goto out_err;
670 strlist__add(entry->tevlist, command);
671 free(command);
672 }
673 list_add_tail(&entry->node, &pcache->entries);
674 pr_debug("Added probe cache: %d\n", ntevs);
675 return 0;
676
677out_err:
678 pr_debug("Failed to add probe caches\n");
679 probe_cache_entry__delete(entry);
680 return ret;
681}
682
Arnaldo Carvalho de Melo1c1a3a42016-07-12 12:19:09 -0300683#ifdef HAVE_GELF_GETNOTE_SUPPORT
Masami Hiramatsu6430a942016-07-01 17:04:10 +0900684static unsigned long long sdt_note__get_addr(struct sdt_note *note)
685{
686 return note->bit32 ? (unsigned long long)note->addr.a32[0]
687 : (unsigned long long)note->addr.a64[0];
688}
689
690int probe_cache__scan_sdt(struct probe_cache *pcache, const char *pathname)
691{
692 struct probe_cache_entry *entry = NULL;
693 struct list_head sdtlist;
694 struct sdt_note *note;
695 char *buf;
696 char sdtgrp[64];
697 int ret;
698
699 INIT_LIST_HEAD(&sdtlist);
700 ret = get_sdt_note_list(&sdtlist, pathname);
701 if (ret < 0) {
Adrian Hunterf9655202016-09-23 17:38:40 +0300702 pr_debug4("Failed to get sdt note: %d\n", ret);
Masami Hiramatsu6430a942016-07-01 17:04:10 +0900703 return ret;
704 }
705 list_for_each_entry(note, &sdtlist, note_list) {
706 ret = snprintf(sdtgrp, 64, "sdt_%s", note->provider);
707 if (ret < 0)
708 break;
709 /* Try to find same-name entry */
710 entry = probe_cache__find_by_name(pcache, sdtgrp, note->name);
711 if (!entry) {
712 entry = probe_cache_entry__new(NULL);
713 if (!entry) {
714 ret = -ENOMEM;
715 break;
716 }
717 entry->sdt = true;
718 ret = asprintf(&entry->spev, "%s:%s=%s", sdtgrp,
719 note->name, note->name);
720 if (ret < 0)
721 break;
722 entry->pev.event = strdup(note->name);
723 entry->pev.group = strdup(sdtgrp);
724 list_add_tail(&entry->node, &pcache->entries);
725 }
726 ret = asprintf(&buf, "p:%s/%s %s:0x%llx",
727 sdtgrp, note->name, pathname,
728 sdt_note__get_addr(note));
729 if (ret < 0)
730 break;
731 strlist__add(entry->tevlist, buf);
732 free(buf);
733 entry = NULL;
734 }
735 if (entry) {
736 list_del_init(&entry->node);
737 probe_cache_entry__delete(entry);
738 }
739 cleanup_sdt_note_list(&sdtlist);
740 return ret;
741}
Arnaldo Carvalho de Melo1c1a3a42016-07-12 12:19:09 -0300742#endif
Masami Hiramatsu6430a942016-07-01 17:04:10 +0900743
Masami Hiramatsudd975492016-06-15 12:28:30 +0900744static int probe_cache_entry__write(struct probe_cache_entry *entry, int fd)
745{
746 struct str_node *snode;
747 struct stat st;
748 struct iovec iov[3];
Masami Hiramatsu6430a942016-07-01 17:04:10 +0900749 const char *prefix = entry->sdt ? "%" : "#";
Masami Hiramatsudd975492016-06-15 12:28:30 +0900750 int ret;
751 /* Save stat for rollback */
752 ret = fstat(fd, &st);
753 if (ret < 0)
754 return ret;
755
Masami Hiramatsu6430a942016-07-01 17:04:10 +0900756 pr_debug("Writing cache: %s%s\n", prefix, entry->spev);
757 iov[0].iov_base = (void *)prefix; iov[0].iov_len = 1;
Masami Hiramatsudd975492016-06-15 12:28:30 +0900758 iov[1].iov_base = entry->spev; iov[1].iov_len = strlen(entry->spev);
759 iov[2].iov_base = (void *)"\n"; iov[2].iov_len = 1;
760 ret = writev(fd, iov, 3);
761 if (ret < (int)iov[1].iov_len + 2)
762 goto rollback;
763
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -0300764 strlist__for_each_entry(snode, entry->tevlist) {
Masami Hiramatsudd975492016-06-15 12:28:30 +0900765 iov[0].iov_base = (void *)snode->s;
766 iov[0].iov_len = strlen(snode->s);
767 iov[1].iov_base = (void *)"\n"; iov[1].iov_len = 1;
768 ret = writev(fd, iov, 2);
769 if (ret < (int)iov[0].iov_len + 1)
770 goto rollback;
771 }
772 return 0;
773
774rollback:
775 /* Rollback to avoid cache file corruption */
776 if (ret > 0)
777 ret = -1;
778 if (ftruncate(fd, st.st_size) < 0)
779 ret = -2;
780
781 return ret;
782}
783
784int probe_cache__commit(struct probe_cache *pcache)
785{
786 struct probe_cache_entry *entry;
787 int ret = 0;
788
789 /* TBD: if we do not update existing entries, skip it */
790 ret = lseek(pcache->fd, 0, SEEK_SET);
791 if (ret < 0)
792 goto out;
793
794 ret = ftruncate(pcache->fd, 0);
795 if (ret < 0)
796 goto out;
797
Masami Hiramatsu05bf2c82016-07-12 19:05:04 +0900798 for_each_probe_cache_entry(entry, pcache) {
Masami Hiramatsudd975492016-06-15 12:28:30 +0900799 ret = probe_cache_entry__write(entry, pcache->fd);
800 pr_debug("Cache committed: %d\n", ret);
801 if (ret < 0)
802 break;
803 }
804out:
805 return ret;
806}
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900807
Masami Hiramatsu4a0f65c2016-07-01 17:03:36 +0900808static bool probe_cache_entry__compare(struct probe_cache_entry *entry,
809 struct strfilter *filter)
810{
811 char buf[128], *ptr = entry->spev;
812
813 if (entry->pev.event) {
814 snprintf(buf, 128, "%s:%s", entry->pev.group, entry->pev.event);
815 ptr = buf;
816 }
817 return strfilter__compare(filter, ptr);
818}
819
820int probe_cache__filter_purge(struct probe_cache *pcache,
821 struct strfilter *filter)
822{
823 struct probe_cache_entry *entry, *tmp;
824
825 list_for_each_entry_safe(entry, tmp, &pcache->entries, node) {
826 if (probe_cache_entry__compare(entry, filter)) {
827 pr_info("Removed cached event: %s\n", entry->spev);
828 list_del_init(&entry->node);
829 probe_cache_entry__delete(entry);
830 }
831 }
832 return 0;
833}
834
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900835static int probe_cache__show_entries(struct probe_cache *pcache,
836 struct strfilter *filter)
837{
838 struct probe_cache_entry *entry;
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900839
Masami Hiramatsu05bf2c82016-07-12 19:05:04 +0900840 for_each_probe_cache_entry(entry, pcache) {
Masami Hiramatsu4a0f65c2016-07-01 17:03:36 +0900841 if (probe_cache_entry__compare(entry, filter))
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900842 printf("%s\n", entry->spev);
843 }
844 return 0;
845}
846
847/* Show all cached probes */
848int probe_cache__show_all_caches(struct strfilter *filter)
849{
850 struct probe_cache *pcache;
851 struct strlist *bidlist;
852 struct str_node *nd;
853 char *buf = strfilter__string(filter);
854
855 pr_debug("list cache with filter: %s\n", buf);
856 free(buf);
857
Masami Hiramatsuc3492a32016-07-12 19:04:54 +0900858 bidlist = build_id_cache__list_all(true);
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900859 if (!bidlist) {
860 pr_debug("Failed to get buildids: %d\n", errno);
861 return -EINVAL;
862 }
863 strlist__for_each_entry(nd, bidlist) {
864 pcache = probe_cache__new(nd->s);
865 if (!pcache)
866 continue;
867 if (!list_empty(&pcache->entries)) {
868 buf = build_id_cache__origname(nd->s);
869 printf("%s (%s):\n", buf, nd->s);
870 free(buf);
871 probe_cache__show_entries(pcache, filter);
872 }
873 probe_cache__delete(pcache);
874 }
875 strlist__delete(bidlist);
876
877 return 0;
878}
Masami Hiramatsu180b2062016-08-18 17:58:31 +0900879
880static struct {
881 const char *pattern;
882 bool avail;
883 bool checked;
884} probe_type_table[] = {
885#define DEFINE_TYPE(idx, pat, def_avail) \
886 [idx] = {.pattern = pat, .avail = (def_avail)}
887 DEFINE_TYPE(PROBE_TYPE_U, "* u8/16/32/64,*", true),
888 DEFINE_TYPE(PROBE_TYPE_S, "* s8/16/32/64,*", true),
889 DEFINE_TYPE(PROBE_TYPE_X, "* x8/16/32/64,*", false),
890 DEFINE_TYPE(PROBE_TYPE_STRING, "* string,*", true),
891 DEFINE_TYPE(PROBE_TYPE_BITFIELD,
892 "* b<bit-width>@<bit-offset>/<container-size>", true),
893};
894
895bool probe_type_is_available(enum probe_type type)
896{
897 FILE *fp;
898 char *buf = NULL;
899 size_t len = 0;
900 bool target_line = false;
901 bool ret = probe_type_table[type].avail;
902
903 if (type >= PROBE_TYPE_END)
904 return false;
905 /* We don't have to check the type which supported by default */
906 if (ret || probe_type_table[type].checked)
907 return ret;
908
909 if (asprintf(&buf, "%s/README", tracing_path) < 0)
910 return ret;
911
912 fp = fopen(buf, "r");
913 if (!fp)
914 goto end;
915
916 zfree(&buf);
917 while (getline(&buf, &len, fp) > 0 && !ret) {
918 if (!target_line) {
919 target_line = !!strstr(buf, " type: ");
920 if (!target_line)
921 continue;
922 } else if (strstr(buf, "\t ") != buf)
923 break;
924 ret = strglobmatch(buf, probe_type_table[type].pattern);
925 }
926 /* Cache the result */
927 probe_type_table[type].checked = true;
928 probe_type_table[type].avail = ret;
929
930 fclose(fp);
931end:
932 free(buf);
933
934 return ret;
935}