probe-file.c 24 KB

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