]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - tools/perf/util/probe-file.c
perf tools: Remove needless evlist.h include directives
[mirror_ubuntu-jammy-kernel.git] / tools / perf / util / probe-file.c
CommitLineData
c942fddf 1// SPDX-License-Identifier: GPL-2.0-or-later
92f6c72e
MH
2/*
3 * probe-file.c : operate ftrace k/uprobe events files
4 *
5 * Written by Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
92f6c72e 6 */
a43783ae 7#include <errno.h>
c23c2a0f 8#include <fcntl.h>
7a8ef4c4
ACM
9#include <sys/stat.h>
10#include <sys/types.h>
dd975497 11#include <sys/uio.h>
7a8ef4c4 12#include <unistd.h>
7f7c536f 13#include <linux/zalloc.h>
40f3b2d2 14#include "namespaces.h"
92f6c72e
MH
15#include "event.h"
16#include "strlist.h"
8ec20b17 17#include "strfilter.h"
92f6c72e 18#include "debug.h"
fac583fd 19#include "dso.h"
92f6c72e
MH
20#include "cache.h"
21#include "color.h"
22#include "symbol.h"
fbf99625 23#include <api/fs/tracing_path.h>
92f6c72e
MH
24#include "probe-event.h"
25#include "probe-file.h"
26#include "session.h"
3b1f8311 27#include "perf_regs.h"
a067558e 28#include "string2.h"
92f6c72e 29
2e1f8f78
RB
30/* 4096 - 2 ('\n' + '\0') */
31#define MAX_CMDLEN 4094
92f6c72e
MH
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',
c8b5f2c9 53 str_error_r(-err, sbuf, sizeof(sbuf)));
92f6c72e
MH
54}
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",
c8b5f2c9 67 str_error_r(-kerr, sbuf, sizeof(sbuf)));
92f6c72e 68 pr_warning("Failed to open uprobe events: %s.\n",
c8b5f2c9 69 str_error_r(-uerr, sbuf, sizeof(sbuf)));
92f6c72e
MH
70 }
71}
72
e491bc2f 73int open_trace_file(const char *trace_file, bool readwrite)
92f6c72e
MH
74{
75 char buf[PATH_MAX];
92f6c72e
MH
76 int ret;
77
17c257e8 78 ret = e_snprintf(buf, PATH_MAX, "%s/%s", tracing_path_mount(), trace_file);
92f6c72e
MH
79 if (ret >= 0) {
80 pr_debug("Opening %s write=%d\n", buf, readwrite);
81 if (readwrite && !probe_event_dry_run)
82 ret = open(buf, O_RDWR | O_APPEND, 0);
83 else
84 ret = open(buf, O_RDONLY, 0);
85
86 if (ret < 0)
87 ret = -errno;
88 }
89 return ret;
90}
91
92static int open_kprobe_events(bool readwrite)
93{
e491bc2f 94 return open_trace_file("kprobe_events", readwrite);
92f6c72e
MH
95}
96
97static int open_uprobe_events(bool readwrite)
98{
e491bc2f 99 return open_trace_file("uprobe_events", readwrite);
92f6c72e
MH
100}
101
102int probe_file__open(int flag)
103{
104 int fd;
105
106 if (flag & PF_FL_UPROBE)
107 fd = open_uprobe_events(flag & PF_FL_RW);
108 else
109 fd = open_kprobe_events(flag & PF_FL_RW);
110 if (fd < 0)
111 print_open_warning(fd, flag & PF_FL_UPROBE);
112
113 return fd;
114}
115
116int probe_file__open_both(int *kfd, int *ufd, int flag)
117{
118 if (!kfd || !ufd)
119 return -EINVAL;
120
121 *kfd = open_kprobe_events(flag & PF_FL_RW);
122 *ufd = open_uprobe_events(flag & PF_FL_RW);
123 if (*kfd < 0 && *ufd < 0) {
124 print_both_open_warning(*kfd, *ufd);
125 return *kfd;
126 }
127
128 return 0;
129}
130
131/* Get raw string list of current kprobe_events or uprobe_events */
132struct strlist *probe_file__get_rawlist(int fd)
133{
0325862d 134 int ret, idx, fddup;
92f6c72e
MH
135 FILE *fp;
136 char buf[MAX_CMDLEN];
137 char *p;
138 struct strlist *sl;
139
421fd084
WN
140 if (fd < 0)
141 return NULL;
142
92f6c72e 143 sl = strlist__new(NULL, NULL);
60ebc159
ACM
144 if (sl == NULL)
145 return NULL;
92f6c72e 146
0325862d
CIK
147 fddup = dup(fd);
148 if (fddup < 0)
149 goto out_free_sl;
150
151 fp = fdopen(fddup, "r");
152 if (!fp)
153 goto out_close_fddup;
154
92f6c72e
MH
155 while (!feof(fp)) {
156 p = fgets(buf, MAX_CMDLEN, fp);
157 if (!p)
158 break;
159
160 idx = strlen(p) - 1;
161 if (p[idx] == '\n')
162 p[idx] = '\0';
163 ret = strlist__add(sl, buf);
164 if (ret < 0) {
165 pr_debug("strlist__add failed (%d)\n", ret);
60ebc159 166 goto out_close_fp;
92f6c72e
MH
167 }
168 }
169 fclose(fp);
170
171 return sl;
0325862d 172
60ebc159
ACM
173out_close_fp:
174 fclose(fp);
175 goto out_free_sl;
0325862d
CIK
176out_close_fddup:
177 close(fddup);
178out_free_sl:
179 strlist__delete(sl);
180 return NULL;
92f6c72e
MH
181}
182
183static struct strlist *__probe_file__get_namelist(int fd, bool include_group)
184{
185 char buf[128];
186 struct strlist *sl, *rawlist;
187 struct str_node *ent;
188 struct probe_trace_event tev;
189 int ret = 0;
190
191 memset(&tev, 0, sizeof(tev));
192 rawlist = probe_file__get_rawlist(fd);
193 if (!rawlist)
194 return NULL;
195 sl = strlist__new(NULL, NULL);
602a1f4d 196 strlist__for_each_entry(ent, rawlist) {
92f6c72e
MH
197 ret = parse_probe_trace_command(ent->s, &tev);
198 if (ret < 0)
199 break;
200 if (include_group) {
201 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
202 tev.event);
203 if (ret >= 0)
204 ret = strlist__add(sl, buf);
205 } else
206 ret = strlist__add(sl, tev.event);
207 clear_probe_trace_event(&tev);
208 if (ret < 0)
209 break;
210 }
211 strlist__delete(rawlist);
212
213 if (ret < 0) {
214 strlist__delete(sl);
215 return NULL;
216 }
217 return sl;
218}
219
220/* Get current perf-probe event names */
221struct strlist *probe_file__get_namelist(int fd)
222{
223 return __probe_file__get_namelist(fd, false);
224}
225
226int probe_file__add_event(int fd, struct probe_trace_event *tev)
227{
228 int ret = 0;
229 char *buf = synthesize_probe_trace_command(tev);
230 char sbuf[STRERR_BUFSIZE];
231
232 if (!buf) {
233 pr_debug("Failed to synthesize probe trace event.\n");
234 return -EINVAL;
235 }
236
237 pr_debug("Writing event: %s\n", buf);
238 if (!probe_event_dry_run) {
6ed0720a 239 if (write(fd, buf, strlen(buf)) < (int)strlen(buf)) {
92f6c72e
MH
240 ret = -errno;
241 pr_warning("Failed to write event: %s\n",
c8b5f2c9 242 str_error_r(errno, sbuf, sizeof(sbuf)));
92f6c72e
MH
243 }
244 }
245 free(buf);
246
247 return ret;
248}
249
250static int __del_trace_probe_event(int fd, struct str_node *ent)
251{
252 char *p;
253 char buf[128];
254 int ret;
255
256 /* Convert from perf-probe event to trace-probe event */
257 ret = e_snprintf(buf, 128, "-:%s", ent->s);
258 if (ret < 0)
259 goto error;
260
261 p = strchr(buf + 2, ':');
262 if (!p) {
263 pr_debug("Internal error: %s should have ':' but not.\n",
264 ent->s);
265 ret = -ENOTSUP;
266 goto error;
267 }
268 *p = '/';
269
270 pr_debug("Writing event: %s\n", buf);
271 ret = write(fd, buf, strlen(buf));
272 if (ret < 0) {
273 ret = -errno;
274 goto error;
275 }
276
92f6c72e
MH
277 return 0;
278error:
279 pr_warning("Failed to delete event: %s\n",
c8b5f2c9 280 str_error_r(-ret, buf, sizeof(buf)));
92f6c72e
MH
281 return ret;
282}
283
e607f142
NK
284int probe_file__get_events(int fd, struct strfilter *filter,
285 struct strlist *plist)
92f6c72e
MH
286{
287 struct strlist *namelist;
288 struct str_node *ent;
289 const char *p;
290 int ret = -ENOENT;
291
421fd084
WN
292 if (!plist)
293 return -EINVAL;
294
92f6c72e
MH
295 namelist = __probe_file__get_namelist(fd, true);
296 if (!namelist)
297 return -ENOENT;
298
602a1f4d 299 strlist__for_each_entry(ent, namelist) {
92f6c72e
MH
300 p = strchr(ent->s, ':');
301 if ((p && strfilter__compare(filter, p + 1)) ||
302 strfilter__compare(filter, ent->s)) {
e7895e42
NK
303 strlist__add(plist, ent->s);
304 ret = 0;
92f6c72e
MH
305 }
306 }
307 strlist__delete(namelist);
308
309 return ret;
310}
e7895e42 311
e607f142 312int probe_file__del_strlist(int fd, struct strlist *namelist)
e7895e42
NK
313{
314 int ret = 0;
315 struct str_node *ent;
316
602a1f4d 317 strlist__for_each_entry(ent, namelist) {
e7895e42
NK
318 ret = __del_trace_probe_event(fd, ent);
319 if (ret < 0)
320 break;
321 }
322 return ret;
323}
324
325int probe_file__del_events(int fd, struct strfilter *filter)
326{
327 struct strlist *namelist;
328 int ret;
329
330 namelist = strlist__new(NULL, NULL);
331 if (!namelist)
332 return -ENOMEM;
333
334 ret = probe_file__get_events(fd, filter, namelist);
335 if (ret < 0)
336 return ret;
337
338 ret = probe_file__del_strlist(fd, namelist);
339 strlist__delete(namelist);
340
341 return ret;
342}
dd975497
MH
343
344/* Caller must ensure to remove this entry from list */
345static void probe_cache_entry__delete(struct probe_cache_entry *entry)
346{
347 if (entry) {
348 BUG_ON(!list_empty(&entry->node));
349
350 strlist__delete(entry->tevlist);
351 clear_perf_probe_event(&entry->pev);
352 zfree(&entry->spev);
353 free(entry);
354 }
355}
356
357static struct probe_cache_entry *
358probe_cache_entry__new(struct perf_probe_event *pev)
359{
360 struct probe_cache_entry *entry = zalloc(sizeof(*entry));
361
362 if (entry) {
363 INIT_LIST_HEAD(&entry->node);
364 entry->tevlist = strlist__new(NULL, NULL);
365 if (!entry->tevlist)
366 zfree(&entry);
367 else if (pev) {
368 entry->spev = synthesize_perf_probe_command(pev);
369 if (!entry->spev ||
370 perf_probe_event__copy(&entry->pev, pev) < 0) {
371 probe_cache_entry__delete(entry);
372 return NULL;
373 }
374 }
375 }
376
377 return entry;
378}
379
42bba263
MH
380int probe_cache_entry__get_event(struct probe_cache_entry *entry,
381 struct probe_trace_event **tevs)
382{
383 struct probe_trace_event *tev;
384 struct str_node *node;
385 int ret, i;
386
387 ret = strlist__nr_entries(entry->tevlist);
388 if (ret > probe_conf.max_probes)
389 return -E2BIG;
390
391 *tevs = zalloc(ret * sizeof(*tev));
392 if (!*tevs)
393 return -ENOMEM;
394
395 i = 0;
396 strlist__for_each_entry(node, entry->tevlist) {
397 tev = &(*tevs)[i++];
398 ret = parse_probe_trace_command(node->s, tev);
399 if (ret < 0)
400 break;
401 }
402 return i;
403}
404
405/* For the kernel probe caches, pass target = NULL or DSO__NAME_KALLSYMS */
f045b8c4
KJ
406static int probe_cache__open(struct probe_cache *pcache, const char *target,
407 struct nsinfo *nsi)
dd975497
MH
408{
409 char cpath[PATH_MAX];
410 char sbuildid[SBUILD_ID_SIZE];
1f3736c9 411 char *dir_name = NULL;
42bba263 412 bool is_kallsyms = false;
dd975497 413 int ret, fd;
f045b8c4 414 struct nscookie nsc;
dd975497 415
1f3736c9
MH
416 if (target && build_id_cache__cached(target)) {
417 /* This is a cached buildid */
bef0b897 418 strlcpy(sbuildid, target, SBUILD_ID_SIZE);
1f3736c9
MH
419 dir_name = build_id_cache__linkname(sbuildid, NULL, 0);
420 goto found;
421 }
422
42bba263 423 if (!target || !strcmp(target, DSO__NAME_KALLSYMS)) {
dd975497 424 target = DSO__NAME_KALLSYMS;
42bba263 425 is_kallsyms = true;
dd975497 426 ret = sysfs__sprintf_build_id("/", sbuildid);
f045b8c4
KJ
427 } else {
428 nsinfo__mountns_enter(nsi, &nsc);
42bba263 429 ret = filename__sprintf_build_id(target, sbuildid);
f045b8c4
KJ
430 nsinfo__mountns_exit(&nsc);
431 }
42bba263 432
dd975497
MH
433 if (ret < 0) {
434 pr_debug("Failed to get build-id from %s.\n", target);
435 return ret;
436 }
437
438 /* If we have no buildid cache, make it */
439 if (!build_id_cache__cached(sbuildid)) {
f045b8c4 440 ret = build_id_cache__add_s(sbuildid, target, nsi,
dd975497
MH
441 is_kallsyms, NULL);
442 if (ret < 0) {
443 pr_debug("Failed to add build-id cache: %s\n", target);
444 return ret;
445 }
446 }
447
f045b8c4 448 dir_name = build_id_cache__cachedir(sbuildid, target, nsi, is_kallsyms,
dd975497 449 false);
1f3736c9
MH
450found:
451 if (!dir_name) {
452 pr_debug("Failed to get cache from %s\n", target);
dd975497 453 return -ENOMEM;
1f3736c9 454 }
dd975497
MH
455
456 snprintf(cpath, PATH_MAX, "%s/probes", dir_name);
457 fd = open(cpath, O_CREAT | O_RDWR, 0644);
458 if (fd < 0)
459 pr_debug("Failed to open cache(%d): %s\n", fd, cpath);
460 free(dir_name);
461 pcache->fd = fd;
462
463 return fd;
464}
465
466static int probe_cache__load(struct probe_cache *pcache)
467{
468 struct probe_cache_entry *entry = NULL;
469 char buf[MAX_CMDLEN], *p;
0325862d 470 int ret = 0, fddup;
dd975497
MH
471 FILE *fp;
472
0325862d
CIK
473 fddup = dup(pcache->fd);
474 if (fddup < 0)
475 return -errno;
476 fp = fdopen(fddup, "r");
60ebc159
ACM
477 if (!fp) {
478 close(fddup);
dd975497 479 return -EINVAL;
60ebc159 480 }
dd975497
MH
481
482 while (!feof(fp)) {
483 if (!fgets(buf, MAX_CMDLEN, fp))
484 break;
485 p = strchr(buf, '\n');
486 if (p)
487 *p = '\0';
6430a94e
MH
488 /* #perf_probe_event or %sdt_event */
489 if (buf[0] == '#' || buf[0] == '%') {
dd975497
MH
490 entry = probe_cache_entry__new(NULL);
491 if (!entry) {
492 ret = -ENOMEM;
493 goto out;
494 }
6430a94e
MH
495 if (buf[0] == '%')
496 entry->sdt = true;
dd975497
MH
497 entry->spev = strdup(buf + 1);
498 if (entry->spev)
499 ret = parse_perf_probe_command(buf + 1,
500 &entry->pev);
501 else
502 ret = -ENOMEM;
503 if (ret < 0) {
504 probe_cache_entry__delete(entry);
505 goto out;
506 }
507 list_add_tail(&entry->node, &pcache->entries);
508 } else { /* trace_probe_event */
509 if (!entry) {
510 ret = -EINVAL;
511 goto out;
512 }
513 strlist__add(entry->tevlist, buf);
514 }
515 }
516out:
517 fclose(fp);
518 return ret;
519}
520
521static struct probe_cache *probe_cache__alloc(void)
522{
523 struct probe_cache *pcache = zalloc(sizeof(*pcache));
524
525 if (pcache) {
526 INIT_LIST_HEAD(&pcache->entries);
527 pcache->fd = -EINVAL;
528 }
529 return pcache;
530}
531
532void probe_cache__purge(struct probe_cache *pcache)
533{
534 struct probe_cache_entry *entry, *n;
535
536 list_for_each_entry_safe(entry, n, &pcache->entries, node) {
537 list_del_init(&entry->node);
538 probe_cache_entry__delete(entry);
539 }
540}
541
542void probe_cache__delete(struct probe_cache *pcache)
543{
544 if (!pcache)
545 return;
546
547 probe_cache__purge(pcache);
548 if (pcache->fd > 0)
549 close(pcache->fd);
550 free(pcache);
551}
552
f045b8c4 553struct probe_cache *probe_cache__new(const char *target, struct nsinfo *nsi)
dd975497
MH
554{
555 struct probe_cache *pcache = probe_cache__alloc();
556 int ret;
557
558 if (!pcache)
559 return NULL;
560
f045b8c4 561 ret = probe_cache__open(pcache, target, nsi);
dd975497
MH
562 if (ret < 0) {
563 pr_debug("Cache open error: %d\n", ret);
564 goto out_err;
565 }
566
567 ret = probe_cache__load(pcache);
568 if (ret < 0) {
569 pr_debug("Cache read error: %d\n", ret);
570 goto out_err;
571 }
572
573 return pcache;
574
575out_err:
576 probe_cache__delete(pcache);
577 return NULL;
578}
579
580static bool streql(const char *a, const char *b)
581{
582 if (a == b)
583 return true;
584
585 if (!a || !b)
586 return false;
587
588 return !strcmp(a, b);
589}
590
bc062230 591struct probe_cache_entry *
dd975497
MH
592probe_cache__find(struct probe_cache *pcache, struct perf_probe_event *pev)
593{
594 struct probe_cache_entry *entry = NULL;
595 char *cmd = synthesize_perf_probe_command(pev);
596
597 if (!cmd)
598 return NULL;
599
05bf2c8a 600 for_each_probe_cache_entry(entry, pcache) {
36a009fe
MH
601 if (pev->sdt) {
602 if (entry->pev.event &&
603 streql(entry->pev.event, pev->event) &&
604 (!pev->group ||
605 streql(entry->pev.group, pev->group)))
606 goto found;
607
608 continue;
609 }
dd975497
MH
610 /* Hit if same event name or same command-string */
611 if ((pev->event &&
612 (streql(entry->pev.group, pev->group) &&
613 streql(entry->pev.event, pev->event))) ||
614 (!strcmp(entry->spev, cmd)))
615 goto found;
616 }
617 entry = NULL;
618
619found:
620 free(cmd);
621 return entry;
622}
623
bc062230
MH
624struct probe_cache_entry *
625probe_cache__find_by_name(struct probe_cache *pcache,
626 const char *group, const char *event)
627{
628 struct probe_cache_entry *entry = NULL;
629
05bf2c8a 630 for_each_probe_cache_entry(entry, pcache) {
bc062230
MH
631 /* Hit if same event name or same command-string */
632 if (streql(entry->pev.group, group) &&
633 streql(entry->pev.event, event))
634 goto found;
635 }
636 entry = NULL;
637
638found:
639 return entry;
640}
641
dd975497
MH
642int probe_cache__add_entry(struct probe_cache *pcache,
643 struct perf_probe_event *pev,
644 struct probe_trace_event *tevs, int ntevs)
645{
646 struct probe_cache_entry *entry = NULL;
647 char *command;
648 int i, ret = 0;
649
650 if (!pcache || !pev || !tevs || ntevs <= 0) {
651 ret = -EINVAL;
652 goto out_err;
653 }
654
655 /* Remove old cache entry */
656 entry = probe_cache__find(pcache, pev);
657 if (entry) {
658 list_del_init(&entry->node);
659 probe_cache_entry__delete(entry);
660 }
661
662 ret = -ENOMEM;
663 entry = probe_cache_entry__new(pev);
664 if (!entry)
665 goto out_err;
666
667 for (i = 0; i < ntevs; i++) {
668 if (!tevs[i].point.symbol)
669 continue;
670
671 command = synthesize_probe_trace_command(&tevs[i]);
672 if (!command)
673 goto out_err;
674 strlist__add(entry->tevlist, command);
675 free(command);
676 }
677 list_add_tail(&entry->node, &pcache->entries);
678 pr_debug("Added probe cache: %d\n", ntevs);
679 return 0;
680
681out_err:
682 pr_debug("Failed to add probe caches\n");
683 probe_cache_entry__delete(entry);
684 return ret;
685}
686
1c1a3a47 687#ifdef HAVE_GELF_GETNOTE_SUPPORT
6430a94e
MH
688static unsigned long long sdt_note__get_addr(struct sdt_note *note)
689{
5a5e3d3c
RB
690 return note->bit32 ?
691 (unsigned long long)note->addr.a32[SDT_NOTE_IDX_LOC] :
692 (unsigned long long)note->addr.a64[SDT_NOTE_IDX_LOC];
693}
694
695static unsigned long long sdt_note__get_ref_ctr_offset(struct sdt_note *note)
696{
697 return note->bit32 ?
698 (unsigned long long)note->addr.a32[SDT_NOTE_IDX_REFCTR] :
699 (unsigned long long)note->addr.a64[SDT_NOTE_IDX_REFCTR];
6430a94e
MH
700}
701
3b1f8311
AB
702static const char * const type_to_suffix[] = {
703 ":s64", "", "", "", ":s32", "", ":s16", ":s8",
704 "", ":u8", ":u16", "", ":u32", "", "", "", ":u64"
705};
706
d451a205
RB
707/*
708 * Isolate the string number and convert it into a decimal value;
709 * this will be an index to get suffix of the uprobe name (defining
710 * the type)
711 */
712static int sdt_arg_parse_size(char *n_ptr, const char **suffix)
713{
714 long type_idx;
715
716 type_idx = strtol(n_ptr, NULL, 10);
717 if (type_idx < -8 || type_idx > 8) {
718 pr_debug4("Failed to get a valid sdt type\n");
719 return -1;
720 }
721
722 *suffix = type_to_suffix[type_idx + 8];
723 return 0;
724}
725
3b1f8311
AB
726static int synthesize_sdt_probe_arg(struct strbuf *buf, int i, const char *arg)
727{
d451a205
RB
728 char *op, *desc = strdup(arg), *new_op = NULL;
729 const char *suffix = "";
3b1f8311
AB
730 int ret = -1;
731
732 if (desc == NULL) {
733 pr_debug4("Allocation error\n");
734 return ret;
735 }
736
3b1f8311 737 /*
d451a205
RB
738 * Argument is in N@OP format. N is size of the argument and OP is
739 * the actual assembly operand. N can be omitted; in that case
740 * argument is just OP(without @).
3b1f8311 741 */
d451a205
RB
742 op = strchr(desc, '@');
743 if (op) {
744 op[0] = '\0';
745 op++;
3b1f8311 746
d451a205
RB
747 if (sdt_arg_parse_size(desc, &suffix))
748 goto error;
749 } else {
750 op = desc;
3b1f8311
AB
751 }
752
d451a205 753 ret = arch_sdt_arg_parse_op(op, &new_op);
3b1f8311 754
d451a205
RB
755 if (ret < 0)
756 goto error;
3b1f8311 757
d451a205
RB
758 if (ret == SDT_ARG_VALID) {
759 ret = strbuf_addf(buf, " arg%d=%s%s", i + 1, new_op, suffix);
3b1f8311
AB
760 if (ret < 0)
761 goto error;
3b1f8311
AB
762 }
763
3b1f8311
AB
764 ret = 0;
765error:
766 free(desc);
d451a205 767 free(new_op);
3b1f8311
AB
768 return ret;
769}
770
771static char *synthesize_sdt_probe_command(struct sdt_note *note,
772 const char *pathname,
773 const char *sdtgrp)
774{
775 struct strbuf buf;
776 char *ret = NULL, **args;
5a5e3d3c
RB
777 int i, args_count, err;
778 unsigned long long ref_ctr_offset;
3b1f8311
AB
779
780 if (strbuf_init(&buf, 32) < 0)
781 return NULL;
782
5a5e3d3c
RB
783 err = strbuf_addf(&buf, "p:%s/%s %s:0x%llx",
784 sdtgrp, note->name, pathname,
785 sdt_note__get_addr(note));
786
787 ref_ctr_offset = sdt_note__get_ref_ctr_offset(note);
788 if (ref_ctr_offset && err >= 0)
789 err = strbuf_addf(&buf, "(0x%llx)", ref_ctr_offset);
790
791 if (err < 0)
3b1f8311
AB
792 goto error;
793
794 if (!note->args)
795 goto out;
796
797 if (note->args) {
798 args = argv_split(note->args, &args_count);
799
800 for (i = 0; i < args_count; ++i) {
801 if (synthesize_sdt_probe_arg(&buf, i, args[i]) < 0)
802 goto error;
803 }
804 }
805
806out:
807 ret = strbuf_detach(&buf, NULL);
808error:
809 strbuf_release(&buf);
810 return ret;
811}
812
6430a94e
MH
813int probe_cache__scan_sdt(struct probe_cache *pcache, const char *pathname)
814{
815 struct probe_cache_entry *entry = NULL;
816 struct list_head sdtlist;
817 struct sdt_note *note;
818 char *buf;
819 char sdtgrp[64];
820 int ret;
821
822 INIT_LIST_HEAD(&sdtlist);
823 ret = get_sdt_note_list(&sdtlist, pathname);
824 if (ret < 0) {
f9655200 825 pr_debug4("Failed to get sdt note: %d\n", ret);
6430a94e
MH
826 return ret;
827 }
828 list_for_each_entry(note, &sdtlist, note_list) {
829 ret = snprintf(sdtgrp, 64, "sdt_%s", note->provider);
830 if (ret < 0)
831 break;
832 /* Try to find same-name entry */
833 entry = probe_cache__find_by_name(pcache, sdtgrp, note->name);
834 if (!entry) {
835 entry = probe_cache_entry__new(NULL);
836 if (!entry) {
837 ret = -ENOMEM;
838 break;
839 }
840 entry->sdt = true;
841 ret = asprintf(&entry->spev, "%s:%s=%s", sdtgrp,
842 note->name, note->name);
843 if (ret < 0)
844 break;
845 entry->pev.event = strdup(note->name);
846 entry->pev.group = strdup(sdtgrp);
847 list_add_tail(&entry->node, &pcache->entries);
848 }
3b1f8311
AB
849 buf = synthesize_sdt_probe_command(note, pathname, sdtgrp);
850 if (!buf) {
851 ret = -ENOMEM;
6430a94e 852 break;
3b1f8311
AB
853 }
854
6430a94e
MH
855 strlist__add(entry->tevlist, buf);
856 free(buf);
857 entry = NULL;
858 }
859 if (entry) {
860 list_del_init(&entry->node);
861 probe_cache_entry__delete(entry);
862 }
863 cleanup_sdt_note_list(&sdtlist);
864 return ret;
865}
1c1a3a47 866#endif
6430a94e 867
dd975497
MH
868static int probe_cache_entry__write(struct probe_cache_entry *entry, int fd)
869{
870 struct str_node *snode;
871 struct stat st;
872 struct iovec iov[3];
6430a94e 873 const char *prefix = entry->sdt ? "%" : "#";
dd975497
MH
874 int ret;
875 /* Save stat for rollback */
876 ret = fstat(fd, &st);
877 if (ret < 0)
878 return ret;
879
6430a94e
MH
880 pr_debug("Writing cache: %s%s\n", prefix, entry->spev);
881 iov[0].iov_base = (void *)prefix; iov[0].iov_len = 1;
dd975497
MH
882 iov[1].iov_base = entry->spev; iov[1].iov_len = strlen(entry->spev);
883 iov[2].iov_base = (void *)"\n"; iov[2].iov_len = 1;
884 ret = writev(fd, iov, 3);
885 if (ret < (int)iov[1].iov_len + 2)
886 goto rollback;
887
602a1f4d 888 strlist__for_each_entry(snode, entry->tevlist) {
dd975497
MH
889 iov[0].iov_base = (void *)snode->s;
890 iov[0].iov_len = strlen(snode->s);
891 iov[1].iov_base = (void *)"\n"; iov[1].iov_len = 1;
892 ret = writev(fd, iov, 2);
893 if (ret < (int)iov[0].iov_len + 1)
894 goto rollback;
895 }
896 return 0;
897
898rollback:
899 /* Rollback to avoid cache file corruption */
900 if (ret > 0)
901 ret = -1;
902 if (ftruncate(fd, st.st_size) < 0)
903 ret = -2;
904
905 return ret;
906}
907
908int probe_cache__commit(struct probe_cache *pcache)
909{
910 struct probe_cache_entry *entry;
911 int ret = 0;
912
913 /* TBD: if we do not update existing entries, skip it */
914 ret = lseek(pcache->fd, 0, SEEK_SET);
915 if (ret < 0)
916 goto out;
917
918 ret = ftruncate(pcache->fd, 0);
919 if (ret < 0)
920 goto out;
921
05bf2c8a 922 for_each_probe_cache_entry(entry, pcache) {
dd975497
MH
923 ret = probe_cache_entry__write(entry, pcache->fd);
924 pr_debug("Cache committed: %d\n", ret);
925 if (ret < 0)
926 break;
927 }
928out:
929 return ret;
930}
1f3736c9 931
4a0f65c1
MH
932static bool probe_cache_entry__compare(struct probe_cache_entry *entry,
933 struct strfilter *filter)
934{
935 char buf[128], *ptr = entry->spev;
936
937 if (entry->pev.event) {
938 snprintf(buf, 128, "%s:%s", entry->pev.group, entry->pev.event);
939 ptr = buf;
940 }
941 return strfilter__compare(filter, ptr);
942}
943
944int probe_cache__filter_purge(struct probe_cache *pcache,
945 struct strfilter *filter)
946{
947 struct probe_cache_entry *entry, *tmp;
948
949 list_for_each_entry_safe(entry, tmp, &pcache->entries, node) {
950 if (probe_cache_entry__compare(entry, filter)) {
951 pr_info("Removed cached event: %s\n", entry->spev);
952 list_del_init(&entry->node);
953 probe_cache_entry__delete(entry);
954 }
955 }
956 return 0;
957}
958
1f3736c9
MH
959static int probe_cache__show_entries(struct probe_cache *pcache,
960 struct strfilter *filter)
961{
962 struct probe_cache_entry *entry;
1f3736c9 963
05bf2c8a 964 for_each_probe_cache_entry(entry, pcache) {
4a0f65c1 965 if (probe_cache_entry__compare(entry, filter))
1f3736c9
MH
966 printf("%s\n", entry->spev);
967 }
968 return 0;
969}
970
971/* Show all cached probes */
972int probe_cache__show_all_caches(struct strfilter *filter)
973{
974 struct probe_cache *pcache;
975 struct strlist *bidlist;
976 struct str_node *nd;
977 char *buf = strfilter__string(filter);
978
979 pr_debug("list cache with filter: %s\n", buf);
980 free(buf);
981
c3492a3a 982 bidlist = build_id_cache__list_all(true);
1f3736c9
MH
983 if (!bidlist) {
984 pr_debug("Failed to get buildids: %d\n", errno);
985 return -EINVAL;
986 }
987 strlist__for_each_entry(nd, bidlist) {
f045b8c4 988 pcache = probe_cache__new(nd->s, NULL);
1f3736c9
MH
989 if (!pcache)
990 continue;
991 if (!list_empty(&pcache->entries)) {
992 buf = build_id_cache__origname(nd->s);
993 printf("%s (%s):\n", buf, nd->s);
994 free(buf);
995 probe_cache__show_entries(pcache, filter);
996 }
997 probe_cache__delete(pcache);
998 }
999 strlist__delete(bidlist);
1000
1001 return 0;
1002}
180b2061 1003
3da3ea7a
NR
1004enum ftrace_readme {
1005 FTRACE_README_PROBE_TYPE_X = 0,
7ab31d94 1006 FTRACE_README_KRETPROBE_OFFSET,
5a5e3d3c 1007 FTRACE_README_UPROBE_REF_CTR,
1e032f7c 1008 FTRACE_README_USER_ACCESS,
3da3ea7a
NR
1009 FTRACE_README_END,
1010};
1011
180b2061
MH
1012static struct {
1013 const char *pattern;
3da3ea7a
NR
1014 bool avail;
1015} ftrace_readme_table[] = {
1016#define DEFINE_TYPE(idx, pat) \
1017 [idx] = {.pattern = pat, .avail = false}
1018 DEFINE_TYPE(FTRACE_README_PROBE_TYPE_X, "*type: * x8/16/32/64,*"),
7ab31d94 1019 DEFINE_TYPE(FTRACE_README_KRETPROBE_OFFSET, "*place (kretprobe): *"),
5a5e3d3c 1020 DEFINE_TYPE(FTRACE_README_UPROBE_REF_CTR, "*ref_ctr_offset*"),
1e032f7c 1021 DEFINE_TYPE(FTRACE_README_USER_ACCESS, "*[u]<offset>*"),
180b2061
MH
1022};
1023
3da3ea7a 1024static bool scan_ftrace_readme(enum ftrace_readme type)
180b2061 1025{
3da3ea7a 1026 int fd;
180b2061
MH
1027 FILE *fp;
1028 char *buf = NULL;
1029 size_t len = 0;
3da3ea7a
NR
1030 bool ret = false;
1031 static bool scanned = false;
180b2061 1032
3da3ea7a
NR
1033 if (scanned)
1034 goto result;
180b2061 1035
e491bc2f
NR
1036 fd = open_trace_file("README", false);
1037 if (fd < 0)
180b2061
MH
1038 return ret;
1039
e491bc2f
NR
1040 fp = fdopen(fd, "r");
1041 if (!fp) {
1042 close(fd);
1043 return ret;
1044 }
180b2061 1045
3da3ea7a
NR
1046 while (getline(&buf, &len, fp) > 0)
1047 for (enum ftrace_readme i = 0; i < FTRACE_README_END; i++)
1048 if (!ftrace_readme_table[i].avail)
1049 ftrace_readme_table[i].avail =
1050 strglobmatch(buf, ftrace_readme_table[i].pattern);
1051 scanned = true;
180b2061
MH
1052
1053 fclose(fp);
180b2061
MH
1054 free(buf);
1055
3da3ea7a
NR
1056result:
1057 if (type >= FTRACE_README_END)
1058 return false;
1059
1060 return ftrace_readme_table[type].avail;
1061}
1062
1063bool probe_type_is_available(enum probe_type type)
1064{
1065 if (type >= PROBE_TYPE_END)
1066 return false;
1067 else if (type == PROBE_TYPE_X)
1068 return scan_ftrace_readme(FTRACE_README_PROBE_TYPE_X);
1069
1070 return true;
180b2061 1071}
7ab31d94
NR
1072
1073bool kretprobe_offset_is_supported(void)
1074{
1075 return scan_ftrace_readme(FTRACE_README_KRETPROBE_OFFSET);
1076}
5a5e3d3c
RB
1077
1078bool uprobe_ref_ctr_is_supported(void)
1079{
1080 return scan_ftrace_readme(FTRACE_README_UPROBE_REF_CTR);
1081}
1e032f7c
MH
1082
1083bool user_access_is_supported(void)
1084{
1085 return scan_ftrace_readme(FTRACE_README_USER_ACCESS);
1086}