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