probe-file.c 18 KB

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