probe-file.c 23 KB

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