probe-file.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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. #define MAX_CMDLEN 256
  31. static void print_open_warning(int err, bool uprobe)
  32. {
  33. char sbuf[STRERR_BUFSIZE];
  34. if (err == -ENOENT) {
  35. const char *config;
  36. if (uprobe)
  37. config = "CONFIG_UPROBE_EVENTS";
  38. else
  39. config = "CONFIG_KPROBE_EVENTS";
  40. pr_warning("%cprobe_events file does not exist"
  41. " - please rebuild kernel with %s.\n",
  42. uprobe ? 'u' : 'k', config);
  43. } else if (err == -ENOTSUP)
  44. pr_warning("Tracefs or debugfs is not mounted.\n");
  45. else
  46. pr_warning("Failed to open %cprobe_events: %s\n",
  47. uprobe ? 'u' : 'k',
  48. str_error_r(-err, sbuf, sizeof(sbuf)));
  49. }
  50. static void print_both_open_warning(int kerr, int uerr)
  51. {
  52. /* Both kprobes and uprobes are disabled, warn it. */
  53. if (kerr == -ENOTSUP && uerr == -ENOTSUP)
  54. pr_warning("Tracefs or debugfs is not mounted.\n");
  55. else if (kerr == -ENOENT && uerr == -ENOENT)
  56. pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
  57. "or/and CONFIG_UPROBE_EVENTS.\n");
  58. else {
  59. char sbuf[STRERR_BUFSIZE];
  60. pr_warning("Failed to open kprobe events: %s.\n",
  61. str_error_r(-kerr, sbuf, sizeof(sbuf)));
  62. pr_warning("Failed to open uprobe events: %s.\n",
  63. str_error_r(-uerr, sbuf, sizeof(sbuf)));
  64. }
  65. }
  66. static int open_probe_events(const char *trace_file, bool readwrite)
  67. {
  68. char buf[PATH_MAX];
  69. const char *tracing_dir = "";
  70. int ret;
  71. ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
  72. tracing_path, tracing_dir, trace_file);
  73. if (ret >= 0) {
  74. pr_debug("Opening %s write=%d\n", buf, readwrite);
  75. if (readwrite && !probe_event_dry_run)
  76. ret = open(buf, O_RDWR | O_APPEND, 0);
  77. else
  78. ret = open(buf, O_RDONLY, 0);
  79. if (ret < 0)
  80. ret = -errno;
  81. }
  82. return ret;
  83. }
  84. static int open_kprobe_events(bool readwrite)
  85. {
  86. return open_probe_events("kprobe_events", readwrite);
  87. }
  88. static int open_uprobe_events(bool readwrite)
  89. {
  90. return open_probe_events("uprobe_events", readwrite);
  91. }
  92. int probe_file__open(int flag)
  93. {
  94. int fd;
  95. if (flag & PF_FL_UPROBE)
  96. fd = open_uprobe_events(flag & PF_FL_RW);
  97. else
  98. fd = open_kprobe_events(flag & PF_FL_RW);
  99. if (fd < 0)
  100. print_open_warning(fd, flag & PF_FL_UPROBE);
  101. return fd;
  102. }
  103. int probe_file__open_both(int *kfd, int *ufd, int flag)
  104. {
  105. if (!kfd || !ufd)
  106. return -EINVAL;
  107. *kfd = open_kprobe_events(flag & PF_FL_RW);
  108. *ufd = open_uprobe_events(flag & PF_FL_RW);
  109. if (*kfd < 0 && *ufd < 0) {
  110. print_both_open_warning(*kfd, *ufd);
  111. return *kfd;
  112. }
  113. return 0;
  114. }
  115. /* Get raw string list of current kprobe_events or uprobe_events */
  116. struct strlist *probe_file__get_rawlist(int fd)
  117. {
  118. int ret, idx, fddup;
  119. FILE *fp;
  120. char buf[MAX_CMDLEN];
  121. char *p;
  122. struct strlist *sl;
  123. if (fd < 0)
  124. return NULL;
  125. sl = strlist__new(NULL, NULL);
  126. if (sl == NULL)
  127. return NULL;
  128. fddup = dup(fd);
  129. if (fddup < 0)
  130. goto out_free_sl;
  131. fp = fdopen(fddup, "r");
  132. if (!fp)
  133. goto out_close_fddup;
  134. while (!feof(fp)) {
  135. p = fgets(buf, MAX_CMDLEN, fp);
  136. if (!p)
  137. break;
  138. idx = strlen(p) - 1;
  139. if (p[idx] == '\n')
  140. p[idx] = '\0';
  141. ret = strlist__add(sl, buf);
  142. if (ret < 0) {
  143. pr_debug("strlist__add failed (%d)\n", ret);
  144. goto out_close_fp;
  145. }
  146. }
  147. fclose(fp);
  148. return sl;
  149. out_close_fp:
  150. fclose(fp);
  151. goto out_free_sl;
  152. out_close_fddup:
  153. close(fddup);
  154. out_free_sl:
  155. strlist__delete(sl);
  156. return NULL;
  157. }
  158. static struct strlist *__probe_file__get_namelist(int fd, bool include_group)
  159. {
  160. char buf[128];
  161. struct strlist *sl, *rawlist;
  162. struct str_node *ent;
  163. struct probe_trace_event tev;
  164. int ret = 0;
  165. memset(&tev, 0, sizeof(tev));
  166. rawlist = probe_file__get_rawlist(fd);
  167. if (!rawlist)
  168. return NULL;
  169. sl = strlist__new(NULL, NULL);
  170. strlist__for_each_entry(ent, rawlist) {
  171. ret = parse_probe_trace_command(ent->s, &tev);
  172. if (ret < 0)
  173. break;
  174. if (include_group) {
  175. ret = e_snprintf(buf, 128, "%s:%s", tev.group,
  176. tev.event);
  177. if (ret >= 0)
  178. ret = strlist__add(sl, buf);
  179. } else
  180. ret = strlist__add(sl, tev.event);
  181. clear_probe_trace_event(&tev);
  182. if (ret < 0)
  183. break;
  184. }
  185. strlist__delete(rawlist);
  186. if (ret < 0) {
  187. strlist__delete(sl);
  188. return NULL;
  189. }
  190. return sl;
  191. }
  192. /* Get current perf-probe event names */
  193. struct strlist *probe_file__get_namelist(int fd)
  194. {
  195. return __probe_file__get_namelist(fd, false);
  196. }
  197. int probe_file__add_event(int fd, struct probe_trace_event *tev)
  198. {
  199. int ret = 0;
  200. char *buf = synthesize_probe_trace_command(tev);
  201. char sbuf[STRERR_BUFSIZE];
  202. if (!buf) {
  203. pr_debug("Failed to synthesize probe trace event.\n");
  204. return -EINVAL;
  205. }
  206. pr_debug("Writing event: %s\n", buf);
  207. if (!probe_event_dry_run) {
  208. if (write(fd, buf, strlen(buf)) < (int)strlen(buf)) {
  209. ret = -errno;
  210. pr_warning("Failed to write event: %s\n",
  211. str_error_r(errno, sbuf, sizeof(sbuf)));
  212. }
  213. }
  214. free(buf);
  215. return ret;
  216. }
  217. static int __del_trace_probe_event(int fd, struct str_node *ent)
  218. {
  219. char *p;
  220. char buf[128];
  221. int ret;
  222. /* Convert from perf-probe event to trace-probe event */
  223. ret = e_snprintf(buf, 128, "-:%s", ent->s);
  224. if (ret < 0)
  225. goto error;
  226. p = strchr(buf + 2, ':');
  227. if (!p) {
  228. pr_debug("Internal error: %s should have ':' but not.\n",
  229. ent->s);
  230. ret = -ENOTSUP;
  231. goto error;
  232. }
  233. *p = '/';
  234. pr_debug("Writing event: %s\n", buf);
  235. ret = write(fd, buf, strlen(buf));
  236. if (ret < 0) {
  237. ret = -errno;
  238. goto error;
  239. }
  240. return 0;
  241. error:
  242. pr_warning("Failed to delete event: %s\n",
  243. str_error_r(-ret, buf, sizeof(buf)));
  244. return ret;
  245. }
  246. int probe_file__get_events(int fd, struct strfilter *filter,
  247. struct strlist *plist)
  248. {
  249. struct strlist *namelist;
  250. struct str_node *ent;
  251. const char *p;
  252. int ret = -ENOENT;
  253. if (!plist)
  254. return -EINVAL;
  255. namelist = __probe_file__get_namelist(fd, true);
  256. if (!namelist)
  257. return -ENOENT;
  258. strlist__for_each_entry(ent, namelist) {
  259. p = strchr(ent->s, ':');
  260. if ((p && strfilter__compare(filter, p + 1)) ||
  261. strfilter__compare(filter, ent->s)) {
  262. strlist__add(plist, ent->s);
  263. ret = 0;
  264. }
  265. }
  266. strlist__delete(namelist);
  267. return ret;
  268. }
  269. int probe_file__del_strlist(int fd, struct strlist *namelist)
  270. {
  271. int ret = 0;
  272. struct str_node *ent;
  273. strlist__for_each_entry(ent, namelist) {
  274. ret = __del_trace_probe_event(fd, ent);
  275. if (ret < 0)
  276. break;
  277. }
  278. return ret;
  279. }
  280. int probe_file__del_events(int fd, struct strfilter *filter)
  281. {
  282. struct strlist *namelist;
  283. int ret;
  284. namelist = strlist__new(NULL, NULL);
  285. if (!namelist)
  286. return -ENOMEM;
  287. ret = probe_file__get_events(fd, filter, namelist);
  288. if (ret < 0)
  289. return ret;
  290. ret = probe_file__del_strlist(fd, namelist);
  291. strlist__delete(namelist);
  292. return ret;
  293. }
  294. /* Caller must ensure to remove this entry from list */
  295. static void probe_cache_entry__delete(struct probe_cache_entry *entry)
  296. {
  297. if (entry) {
  298. BUG_ON(!list_empty(&entry->node));
  299. strlist__delete(entry->tevlist);
  300. clear_perf_probe_event(&entry->pev);
  301. zfree(&entry->spev);
  302. free(entry);
  303. }
  304. }
  305. static struct probe_cache_entry *
  306. probe_cache_entry__new(struct perf_probe_event *pev)
  307. {
  308. struct probe_cache_entry *entry = zalloc(sizeof(*entry));
  309. if (entry) {
  310. INIT_LIST_HEAD(&entry->node);
  311. entry->tevlist = strlist__new(NULL, NULL);
  312. if (!entry->tevlist)
  313. zfree(&entry);
  314. else if (pev) {
  315. entry->spev = synthesize_perf_probe_command(pev);
  316. if (!entry->spev ||
  317. perf_probe_event__copy(&entry->pev, pev) < 0) {
  318. probe_cache_entry__delete(entry);
  319. return NULL;
  320. }
  321. }
  322. }
  323. return entry;
  324. }
  325. int probe_cache_entry__get_event(struct probe_cache_entry *entry,
  326. struct probe_trace_event **tevs)
  327. {
  328. struct probe_trace_event *tev;
  329. struct str_node *node;
  330. int ret, i;
  331. ret = strlist__nr_entries(entry->tevlist);
  332. if (ret > probe_conf.max_probes)
  333. return -E2BIG;
  334. *tevs = zalloc(ret * sizeof(*tev));
  335. if (!*tevs)
  336. return -ENOMEM;
  337. i = 0;
  338. strlist__for_each_entry(node, entry->tevlist) {
  339. tev = &(*tevs)[i++];
  340. ret = parse_probe_trace_command(node->s, tev);
  341. if (ret < 0)
  342. break;
  343. }
  344. return i;
  345. }
  346. /* For the kernel probe caches, pass target = NULL or DSO__NAME_KALLSYMS */
  347. static int probe_cache__open(struct probe_cache *pcache, const char *target)
  348. {
  349. char cpath[PATH_MAX];
  350. char sbuildid[SBUILD_ID_SIZE];
  351. char *dir_name = NULL;
  352. bool is_kallsyms = false;
  353. int ret, fd;
  354. if (target && build_id_cache__cached(target)) {
  355. /* This is a cached buildid */
  356. strncpy(sbuildid, target, SBUILD_ID_SIZE);
  357. dir_name = build_id_cache__linkname(sbuildid, NULL, 0);
  358. goto found;
  359. }
  360. if (!target || !strcmp(target, DSO__NAME_KALLSYMS)) {
  361. target = DSO__NAME_KALLSYMS;
  362. is_kallsyms = true;
  363. ret = sysfs__sprintf_build_id("/", sbuildid);
  364. } else
  365. ret = filename__sprintf_build_id(target, sbuildid);
  366. if (ret < 0) {
  367. pr_debug("Failed to get build-id from %s.\n", target);
  368. return ret;
  369. }
  370. /* If we have no buildid cache, make it */
  371. if (!build_id_cache__cached(sbuildid)) {
  372. ret = build_id_cache__add_s(sbuildid, target,
  373. is_kallsyms, NULL);
  374. if (ret < 0) {
  375. pr_debug("Failed to add build-id cache: %s\n", target);
  376. return ret;
  377. }
  378. }
  379. dir_name = build_id_cache__cachedir(sbuildid, target, is_kallsyms,
  380. false);
  381. found:
  382. if (!dir_name) {
  383. pr_debug("Failed to get cache from %s\n", target);
  384. return -ENOMEM;
  385. }
  386. snprintf(cpath, PATH_MAX, "%s/probes", dir_name);
  387. fd = open(cpath, O_CREAT | O_RDWR, 0644);
  388. if (fd < 0)
  389. pr_debug("Failed to open cache(%d): %s\n", fd, cpath);
  390. free(dir_name);
  391. pcache->fd = fd;
  392. return fd;
  393. }
  394. static int probe_cache__load(struct probe_cache *pcache)
  395. {
  396. struct probe_cache_entry *entry = NULL;
  397. char buf[MAX_CMDLEN], *p;
  398. int ret = 0, fddup;
  399. FILE *fp;
  400. fddup = dup(pcache->fd);
  401. if (fddup < 0)
  402. return -errno;
  403. fp = fdopen(fddup, "r");
  404. if (!fp) {
  405. close(fddup);
  406. return -EINVAL;
  407. }
  408. while (!feof(fp)) {
  409. if (!fgets(buf, MAX_CMDLEN, fp))
  410. break;
  411. p = strchr(buf, '\n');
  412. if (p)
  413. *p = '\0';
  414. /* #perf_probe_event or %sdt_event */
  415. if (buf[0] == '#' || buf[0] == '%') {
  416. entry = probe_cache_entry__new(NULL);
  417. if (!entry) {
  418. ret = -ENOMEM;
  419. goto out;
  420. }
  421. if (buf[0] == '%')
  422. entry->sdt = true;
  423. entry->spev = strdup(buf + 1);
  424. if (entry->spev)
  425. ret = parse_perf_probe_command(buf + 1,
  426. &entry->pev);
  427. else
  428. ret = -ENOMEM;
  429. if (ret < 0) {
  430. probe_cache_entry__delete(entry);
  431. goto out;
  432. }
  433. list_add_tail(&entry->node, &pcache->entries);
  434. } else { /* trace_probe_event */
  435. if (!entry) {
  436. ret = -EINVAL;
  437. goto out;
  438. }
  439. strlist__add(entry->tevlist, buf);
  440. }
  441. }
  442. out:
  443. fclose(fp);
  444. return ret;
  445. }
  446. static struct probe_cache *probe_cache__alloc(void)
  447. {
  448. struct probe_cache *pcache = zalloc(sizeof(*pcache));
  449. if (pcache) {
  450. INIT_LIST_HEAD(&pcache->entries);
  451. pcache->fd = -EINVAL;
  452. }
  453. return pcache;
  454. }
  455. void probe_cache__purge(struct probe_cache *pcache)
  456. {
  457. struct probe_cache_entry *entry, *n;
  458. list_for_each_entry_safe(entry, n, &pcache->entries, node) {
  459. list_del_init(&entry->node);
  460. probe_cache_entry__delete(entry);
  461. }
  462. }
  463. void probe_cache__delete(struct probe_cache *pcache)
  464. {
  465. if (!pcache)
  466. return;
  467. probe_cache__purge(pcache);
  468. if (pcache->fd > 0)
  469. close(pcache->fd);
  470. free(pcache);
  471. }
  472. struct probe_cache *probe_cache__new(const char *target)
  473. {
  474. struct probe_cache *pcache = probe_cache__alloc();
  475. int ret;
  476. if (!pcache)
  477. return NULL;
  478. ret = probe_cache__open(pcache, target);
  479. if (ret < 0) {
  480. pr_debug("Cache open error: %d\n", ret);
  481. goto out_err;
  482. }
  483. ret = probe_cache__load(pcache);
  484. if (ret < 0) {
  485. pr_debug("Cache read error: %d\n", ret);
  486. goto out_err;
  487. }
  488. return pcache;
  489. out_err:
  490. probe_cache__delete(pcache);
  491. return NULL;
  492. }
  493. static bool streql(const char *a, const char *b)
  494. {
  495. if (a == b)
  496. return true;
  497. if (!a || !b)
  498. return false;
  499. return !strcmp(a, b);
  500. }
  501. struct probe_cache_entry *
  502. probe_cache__find(struct probe_cache *pcache, struct perf_probe_event *pev)
  503. {
  504. struct probe_cache_entry *entry = NULL;
  505. char *cmd = synthesize_perf_probe_command(pev);
  506. if (!cmd)
  507. return NULL;
  508. for_each_probe_cache_entry(entry, pcache) {
  509. if (pev->sdt) {
  510. if (entry->pev.event &&
  511. streql(entry->pev.event, pev->event) &&
  512. (!pev->group ||
  513. streql(entry->pev.group, pev->group)))
  514. goto found;
  515. continue;
  516. }
  517. /* Hit if same event name or same command-string */
  518. if ((pev->event &&
  519. (streql(entry->pev.group, pev->group) &&
  520. streql(entry->pev.event, pev->event))) ||
  521. (!strcmp(entry->spev, cmd)))
  522. goto found;
  523. }
  524. entry = NULL;
  525. found:
  526. free(cmd);
  527. return entry;
  528. }
  529. struct probe_cache_entry *
  530. probe_cache__find_by_name(struct probe_cache *pcache,
  531. const char *group, const char *event)
  532. {
  533. struct probe_cache_entry *entry = NULL;
  534. for_each_probe_cache_entry(entry, pcache) {
  535. /* Hit if same event name or same command-string */
  536. if (streql(entry->pev.group, group) &&
  537. streql(entry->pev.event, event))
  538. goto found;
  539. }
  540. entry = NULL;
  541. found:
  542. return entry;
  543. }
  544. int probe_cache__add_entry(struct probe_cache *pcache,
  545. struct perf_probe_event *pev,
  546. struct probe_trace_event *tevs, int ntevs)
  547. {
  548. struct probe_cache_entry *entry = NULL;
  549. char *command;
  550. int i, ret = 0;
  551. if (!pcache || !pev || !tevs || ntevs <= 0) {
  552. ret = -EINVAL;
  553. goto out_err;
  554. }
  555. /* Remove old cache entry */
  556. entry = probe_cache__find(pcache, pev);
  557. if (entry) {
  558. list_del_init(&entry->node);
  559. probe_cache_entry__delete(entry);
  560. }
  561. ret = -ENOMEM;
  562. entry = probe_cache_entry__new(pev);
  563. if (!entry)
  564. goto out_err;
  565. for (i = 0; i < ntevs; i++) {
  566. if (!tevs[i].point.symbol)
  567. continue;
  568. command = synthesize_probe_trace_command(&tevs[i]);
  569. if (!command)
  570. goto out_err;
  571. strlist__add(entry->tevlist, command);
  572. free(command);
  573. }
  574. list_add_tail(&entry->node, &pcache->entries);
  575. pr_debug("Added probe cache: %d\n", ntevs);
  576. return 0;
  577. out_err:
  578. pr_debug("Failed to add probe caches\n");
  579. probe_cache_entry__delete(entry);
  580. return ret;
  581. }
  582. #ifdef HAVE_GELF_GETNOTE_SUPPORT
  583. static unsigned long long sdt_note__get_addr(struct sdt_note *note)
  584. {
  585. return note->bit32 ? (unsigned long long)note->addr.a32[0]
  586. : (unsigned long long)note->addr.a64[0];
  587. }
  588. int probe_cache__scan_sdt(struct probe_cache *pcache, const char *pathname)
  589. {
  590. struct probe_cache_entry *entry = NULL;
  591. struct list_head sdtlist;
  592. struct sdt_note *note;
  593. char *buf;
  594. char sdtgrp[64];
  595. int ret;
  596. INIT_LIST_HEAD(&sdtlist);
  597. ret = get_sdt_note_list(&sdtlist, pathname);
  598. if (ret < 0) {
  599. pr_debug("Failed to get sdt note: %d\n", ret);
  600. return ret;
  601. }
  602. list_for_each_entry(note, &sdtlist, note_list) {
  603. ret = snprintf(sdtgrp, 64, "sdt_%s", note->provider);
  604. if (ret < 0)
  605. break;
  606. /* Try to find same-name entry */
  607. entry = probe_cache__find_by_name(pcache, sdtgrp, note->name);
  608. if (!entry) {
  609. entry = probe_cache_entry__new(NULL);
  610. if (!entry) {
  611. ret = -ENOMEM;
  612. break;
  613. }
  614. entry->sdt = true;
  615. ret = asprintf(&entry->spev, "%s:%s=%s", sdtgrp,
  616. note->name, note->name);
  617. if (ret < 0)
  618. break;
  619. entry->pev.event = strdup(note->name);
  620. entry->pev.group = strdup(sdtgrp);
  621. list_add_tail(&entry->node, &pcache->entries);
  622. }
  623. ret = asprintf(&buf, "p:%s/%s %s:0x%llx",
  624. sdtgrp, note->name, pathname,
  625. sdt_note__get_addr(note));
  626. if (ret < 0)
  627. break;
  628. strlist__add(entry->tevlist, buf);
  629. free(buf);
  630. entry = NULL;
  631. }
  632. if (entry) {
  633. list_del_init(&entry->node);
  634. probe_cache_entry__delete(entry);
  635. }
  636. cleanup_sdt_note_list(&sdtlist);
  637. return ret;
  638. }
  639. #endif
  640. static int probe_cache_entry__write(struct probe_cache_entry *entry, int fd)
  641. {
  642. struct str_node *snode;
  643. struct stat st;
  644. struct iovec iov[3];
  645. const char *prefix = entry->sdt ? "%" : "#";
  646. int ret;
  647. /* Save stat for rollback */
  648. ret = fstat(fd, &st);
  649. if (ret < 0)
  650. return ret;
  651. pr_debug("Writing cache: %s%s\n", prefix, entry->spev);
  652. iov[0].iov_base = (void *)prefix; iov[0].iov_len = 1;
  653. iov[1].iov_base = entry->spev; iov[1].iov_len = strlen(entry->spev);
  654. iov[2].iov_base = (void *)"\n"; iov[2].iov_len = 1;
  655. ret = writev(fd, iov, 3);
  656. if (ret < (int)iov[1].iov_len + 2)
  657. goto rollback;
  658. strlist__for_each_entry(snode, entry->tevlist) {
  659. iov[0].iov_base = (void *)snode->s;
  660. iov[0].iov_len = strlen(snode->s);
  661. iov[1].iov_base = (void *)"\n"; iov[1].iov_len = 1;
  662. ret = writev(fd, iov, 2);
  663. if (ret < (int)iov[0].iov_len + 1)
  664. goto rollback;
  665. }
  666. return 0;
  667. rollback:
  668. /* Rollback to avoid cache file corruption */
  669. if (ret > 0)
  670. ret = -1;
  671. if (ftruncate(fd, st.st_size) < 0)
  672. ret = -2;
  673. return ret;
  674. }
  675. int probe_cache__commit(struct probe_cache *pcache)
  676. {
  677. struct probe_cache_entry *entry;
  678. int ret = 0;
  679. /* TBD: if we do not update existing entries, skip it */
  680. ret = lseek(pcache->fd, 0, SEEK_SET);
  681. if (ret < 0)
  682. goto out;
  683. ret = ftruncate(pcache->fd, 0);
  684. if (ret < 0)
  685. goto out;
  686. for_each_probe_cache_entry(entry, pcache) {
  687. ret = probe_cache_entry__write(entry, pcache->fd);
  688. pr_debug("Cache committed: %d\n", ret);
  689. if (ret < 0)
  690. break;
  691. }
  692. out:
  693. return ret;
  694. }
  695. static bool probe_cache_entry__compare(struct probe_cache_entry *entry,
  696. struct strfilter *filter)
  697. {
  698. char buf[128], *ptr = entry->spev;
  699. if (entry->pev.event) {
  700. snprintf(buf, 128, "%s:%s", entry->pev.group, entry->pev.event);
  701. ptr = buf;
  702. }
  703. return strfilter__compare(filter, ptr);
  704. }
  705. int probe_cache__filter_purge(struct probe_cache *pcache,
  706. struct strfilter *filter)
  707. {
  708. struct probe_cache_entry *entry, *tmp;
  709. list_for_each_entry_safe(entry, tmp, &pcache->entries, node) {
  710. if (probe_cache_entry__compare(entry, filter)) {
  711. pr_info("Removed cached event: %s\n", entry->spev);
  712. list_del_init(&entry->node);
  713. probe_cache_entry__delete(entry);
  714. }
  715. }
  716. return 0;
  717. }
  718. static int probe_cache__show_entries(struct probe_cache *pcache,
  719. struct strfilter *filter)
  720. {
  721. struct probe_cache_entry *entry;
  722. for_each_probe_cache_entry(entry, pcache) {
  723. if (probe_cache_entry__compare(entry, filter))
  724. printf("%s\n", entry->spev);
  725. }
  726. return 0;
  727. }
  728. /* Show all cached probes */
  729. int probe_cache__show_all_caches(struct strfilter *filter)
  730. {
  731. struct probe_cache *pcache;
  732. struct strlist *bidlist;
  733. struct str_node *nd;
  734. char *buf = strfilter__string(filter);
  735. pr_debug("list cache with filter: %s\n", buf);
  736. free(buf);
  737. bidlist = build_id_cache__list_all(true);
  738. if (!bidlist) {
  739. pr_debug("Failed to get buildids: %d\n", errno);
  740. return -EINVAL;
  741. }
  742. strlist__for_each_entry(nd, bidlist) {
  743. pcache = probe_cache__new(nd->s);
  744. if (!pcache)
  745. continue;
  746. if (!list_empty(&pcache->entries)) {
  747. buf = build_id_cache__origname(nd->s);
  748. printf("%s (%s):\n", buf, nd->s);
  749. free(buf);
  750. probe_cache__show_entries(pcache, filter);
  751. }
  752. probe_cache__delete(pcache);
  753. }
  754. strlist__delete(bidlist);
  755. return 0;
  756. }