Linux Audio

Check our new training course

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