probe-file.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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. int ret;
  70. ret = e_snprintf(buf, PATH_MAX, "%s/%s",
  71. tracing_path, 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_probe_events("kprobe_events", readwrite);
  86. }
  87. static int open_uprobe_events(bool readwrite)
  88. {
  89. return open_probe_events("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. if (ret < 0)
  182. break;
  183. }
  184. strlist__delete(rawlist);
  185. if (ret < 0) {
  186. strlist__delete(sl);
  187. return NULL;
  188. }
  189. return sl;
  190. }
  191. /* Get current perf-probe event names */
  192. struct strlist *probe_file__get_namelist(int fd)
  193. {
  194. return __probe_file__get_namelist(fd, false);
  195. }
  196. int probe_file__add_event(int fd, struct probe_trace_event *tev)
  197. {
  198. int ret = 0;
  199. char *buf = synthesize_probe_trace_command(tev);
  200. char sbuf[STRERR_BUFSIZE];
  201. if (!buf) {
  202. pr_debug("Failed to synthesize probe trace event.\n");
  203. return -EINVAL;
  204. }
  205. pr_debug("Writing event: %s\n", buf);
  206. if (!probe_event_dry_run) {
  207. if (write(fd, buf, strlen(buf)) < (int)strlen(buf)) {
  208. ret = -errno;
  209. pr_warning("Failed to write event: %s\n",
  210. str_error_r(errno, sbuf, sizeof(sbuf)));
  211. }
  212. }
  213. free(buf);
  214. return ret;
  215. }
  216. static int __del_trace_probe_event(int fd, struct str_node *ent)
  217. {
  218. char *p;
  219. char buf[128];
  220. int ret;
  221. /* Convert from perf-probe event to trace-probe event */
  222. ret = e_snprintf(buf, 128, "-:%s", ent->s);
  223. if (ret < 0)
  224. goto error;
  225. p = strchr(buf + 2, ':');
  226. if (!p) {
  227. pr_debug("Internal error: %s should have ':' but not.\n",
  228. ent->s);
  229. ret = -ENOTSUP;
  230. goto error;
  231. }
  232. *p = '/';
  233. pr_debug("Writing event: %s\n", buf);
  234. ret = write(fd, buf, strlen(buf));
  235. if (ret < 0) {
  236. ret = -errno;
  237. goto error;
  238. }
  239. return 0;
  240. error:
  241. pr_warning("Failed to delete event: %s\n",
  242. str_error_r(-ret, buf, sizeof(buf)));
  243. return ret;
  244. }
  245. int probe_file__get_events(int fd, struct strfilter *filter,
  246. struct strlist *plist)
  247. {
  248. struct strlist *namelist;
  249. struct str_node *ent;
  250. const char *p;
  251. int ret = -ENOENT;
  252. if (!plist)
  253. return -EINVAL;
  254. namelist = __probe_file__get_namelist(fd, true);
  255. if (!namelist)
  256. return -ENOENT;
  257. strlist__for_each_entry(ent, namelist) {
  258. p = strchr(ent->s, ':');
  259. if ((p && strfilter__compare(filter, p + 1)) ||
  260. strfilter__compare(filter, ent->s)) {
  261. strlist__add(plist, ent->s);
  262. ret = 0;
  263. }
  264. }
  265. strlist__delete(namelist);
  266. return ret;
  267. }
  268. int probe_file__del_strlist(int fd, struct strlist *namelist)
  269. {
  270. int ret = 0;
  271. struct str_node *ent;
  272. strlist__for_each_entry(ent, namelist) {
  273. ret = __del_trace_probe_event(fd, ent);
  274. if (ret < 0)
  275. break;
  276. }
  277. return ret;
  278. }
  279. int probe_file__del_events(int fd, struct strfilter *filter)
  280. {
  281. struct strlist *namelist;
  282. int ret;
  283. namelist = strlist__new(NULL, NULL);
  284. if (!namelist)
  285. return -ENOMEM;
  286. ret = probe_file__get_events(fd, filter, namelist);
  287. if (ret < 0)
  288. return ret;
  289. ret = probe_file__del_strlist(fd, namelist);
  290. strlist__delete(namelist);
  291. return ret;
  292. }
  293. /* Caller must ensure to remove this entry from list */
  294. static void probe_cache_entry__delete(struct probe_cache_entry *entry)
  295. {
  296. if (entry) {
  297. BUG_ON(!list_empty(&entry->node));
  298. strlist__delete(entry->tevlist);
  299. clear_perf_probe_event(&entry->pev);
  300. zfree(&entry->spev);
  301. free(entry);
  302. }
  303. }
  304. static struct probe_cache_entry *
  305. probe_cache_entry__new(struct perf_probe_event *pev)
  306. {
  307. struct probe_cache_entry *entry = zalloc(sizeof(*entry));
  308. if (entry) {
  309. INIT_LIST_HEAD(&entry->node);
  310. entry->tevlist = strlist__new(NULL, NULL);
  311. if (!entry->tevlist)
  312. zfree(&entry);
  313. else if (pev) {
  314. entry->spev = synthesize_perf_probe_command(pev);
  315. if (!entry->spev ||
  316. perf_probe_event__copy(&entry->pev, pev) < 0) {
  317. probe_cache_entry__delete(entry);
  318. return NULL;
  319. }
  320. }
  321. }
  322. return entry;
  323. }
  324. int probe_cache_entry__get_event(struct probe_cache_entry *entry,
  325. struct probe_trace_event **tevs)
  326. {
  327. struct probe_trace_event *tev;
  328. struct str_node *node;
  329. int ret, i;
  330. ret = strlist__nr_entries(entry->tevlist);
  331. if (ret > probe_conf.max_probes)
  332. return -E2BIG;
  333. *tevs = zalloc(ret * sizeof(*tev));
  334. if (!*tevs)
  335. return -ENOMEM;
  336. i = 0;
  337. strlist__for_each_entry(node, entry->tevlist) {
  338. tev = &(*tevs)[i++];
  339. ret = parse_probe_trace_command(node->s, tev);
  340. if (ret < 0)
  341. break;
  342. }
  343. return i;
  344. }
  345. /* For the kernel probe caches, pass target = NULL or DSO__NAME_KALLSYMS */
  346. static int probe_cache__open(struct probe_cache *pcache, const char *target)
  347. {
  348. char cpath[PATH_MAX];
  349. char sbuildid[SBUILD_ID_SIZE];
  350. char *dir_name = NULL;
  351. bool is_kallsyms = false;
  352. int ret, fd;
  353. if (target && build_id_cache__cached(target)) {
  354. /* This is a cached buildid */
  355. strncpy(sbuildid, target, SBUILD_ID_SIZE);
  356. dir_name = build_id_cache__linkname(sbuildid, NULL, 0);
  357. goto found;
  358. }
  359. if (!target || !strcmp(target, DSO__NAME_KALLSYMS)) {
  360. target = DSO__NAME_KALLSYMS;
  361. is_kallsyms = true;
  362. ret = sysfs__sprintf_build_id("/", sbuildid);
  363. } else
  364. ret = filename__sprintf_build_id(target, sbuildid);
  365. if (ret < 0) {
  366. pr_debug("Failed to get build-id from %s.\n", target);
  367. return ret;
  368. }
  369. /* If we have no buildid cache, make it */
  370. if (!build_id_cache__cached(sbuildid)) {
  371. ret = build_id_cache__add_s(sbuildid, target,
  372. is_kallsyms, NULL);
  373. if (ret < 0) {
  374. pr_debug("Failed to add build-id cache: %s\n", target);
  375. return ret;
  376. }
  377. }
  378. dir_name = build_id_cache__cachedir(sbuildid, target, is_kallsyms,
  379. false);
  380. found:
  381. if (!dir_name) {
  382. pr_debug("Failed to get cache from %s\n", target);
  383. return -ENOMEM;
  384. }
  385. snprintf(cpath, PATH_MAX, "%s/probes", dir_name);
  386. fd = open(cpath, O_CREAT | O_RDWR, 0644);
  387. if (fd < 0)
  388. pr_debug("Failed to open cache(%d): %s\n", fd, cpath);
  389. free(dir_name);
  390. pcache->fd = fd;
  391. return fd;
  392. }
  393. static int probe_cache__load(struct probe_cache *pcache)
  394. {
  395. struct probe_cache_entry *entry = NULL;
  396. char buf[MAX_CMDLEN], *p;
  397. int ret = 0, fddup;
  398. FILE *fp;
  399. fddup = dup(pcache->fd);
  400. if (fddup < 0)
  401. return -errno;
  402. fp = fdopen(fddup, "r");
  403. if (!fp) {
  404. close(fddup);
  405. return -EINVAL;
  406. }
  407. while (!feof(fp)) {
  408. if (!fgets(buf, MAX_CMDLEN, fp))
  409. break;
  410. p = strchr(buf, '\n');
  411. if (p)
  412. *p = '\0';
  413. /* #perf_probe_event or %sdt_event */
  414. if (buf[0] == '#' || buf[0] == '%') {
  415. entry = probe_cache_entry__new(NULL);
  416. if (!entry) {
  417. ret = -ENOMEM;
  418. goto out;
  419. }
  420. if (buf[0] == '%')
  421. entry->sdt = true;
  422. entry->spev = strdup(buf + 1);
  423. if (entry->spev)
  424. ret = parse_perf_probe_command(buf + 1,
  425. &entry->pev);
  426. else
  427. ret = -ENOMEM;
  428. if (ret < 0) {
  429. probe_cache_entry__delete(entry);
  430. goto out;
  431. }
  432. list_add_tail(&entry->node, &pcache->entries);
  433. } else { /* trace_probe_event */
  434. if (!entry) {
  435. ret = -EINVAL;
  436. goto out;
  437. }
  438. strlist__add(entry->tevlist, buf);
  439. }
  440. }
  441. out:
  442. fclose(fp);
  443. return ret;
  444. }
  445. static struct probe_cache *probe_cache__alloc(void)
  446. {
  447. struct probe_cache *pcache = zalloc(sizeof(*pcache));
  448. if (pcache) {
  449. INIT_LIST_HEAD(&pcache->entries);
  450. pcache->fd = -EINVAL;
  451. }
  452. return pcache;
  453. }
  454. void probe_cache__purge(struct probe_cache *pcache)
  455. {
  456. struct probe_cache_entry *entry, *n;
  457. list_for_each_entry_safe(entry, n, &pcache->entries, node) {
  458. list_del_init(&entry->node);
  459. probe_cache_entry__delete(entry);
  460. }
  461. }
  462. void probe_cache__delete(struct probe_cache *pcache)
  463. {
  464. if (!pcache)
  465. return;
  466. probe_cache__purge(pcache);
  467. if (pcache->fd > 0)
  468. close(pcache->fd);
  469. free(pcache);
  470. }
  471. struct probe_cache *probe_cache__new(const char *target)
  472. {
  473. struct probe_cache *pcache = probe_cache__alloc();
  474. int ret;
  475. if (!pcache)
  476. return NULL;
  477. ret = probe_cache__open(pcache, target);
  478. if (ret < 0) {
  479. pr_debug("Cache open error: %d\n", ret);
  480. goto out_err;
  481. }
  482. ret = probe_cache__load(pcache);
  483. if (ret < 0) {
  484. pr_debug("Cache read error: %d\n", ret);
  485. goto out_err;
  486. }
  487. return pcache;
  488. out_err:
  489. probe_cache__delete(pcache);
  490. return NULL;
  491. }
  492. static bool streql(const char *a, const char *b)
  493. {
  494. if (a == b)
  495. return true;
  496. if (!a || !b)
  497. return false;
  498. return !strcmp(a, b);
  499. }
  500. struct probe_cache_entry *
  501. probe_cache__find(struct probe_cache *pcache, struct perf_probe_event *pev)
  502. {
  503. struct probe_cache_entry *entry = NULL;
  504. char *cmd = synthesize_perf_probe_command(pev);
  505. if (!cmd)
  506. return NULL;
  507. for_each_probe_cache_entry(entry, pcache) {
  508. if (pev->sdt) {
  509. if (entry->pev.event &&
  510. streql(entry->pev.event, pev->event) &&
  511. (!pev->group ||
  512. streql(entry->pev.group, pev->group)))
  513. goto found;
  514. continue;
  515. }
  516. /* Hit if same event name or same command-string */
  517. if ((pev->event &&
  518. (streql(entry->pev.group, pev->group) &&
  519. streql(entry->pev.event, pev->event))) ||
  520. (!strcmp(entry->spev, cmd)))
  521. goto found;
  522. }
  523. entry = NULL;
  524. found:
  525. free(cmd);
  526. return entry;
  527. }
  528. struct probe_cache_entry *
  529. probe_cache__find_by_name(struct probe_cache *pcache,
  530. const char *group, const char *event)
  531. {
  532. struct probe_cache_entry *entry = NULL;
  533. for_each_probe_cache_entry(entry, pcache) {
  534. /* Hit if same event name or same command-string */
  535. if (streql(entry->pev.group, group) &&
  536. streql(entry->pev.event, event))
  537. goto found;
  538. }
  539. entry = NULL;
  540. found:
  541. return entry;
  542. }
  543. int probe_cache__add_entry(struct probe_cache *pcache,
  544. struct perf_probe_event *pev,
  545. struct probe_trace_event *tevs, int ntevs)
  546. {
  547. struct probe_cache_entry *entry = NULL;
  548. char *command;
  549. int i, ret = 0;
  550. if (!pcache || !pev || !tevs || ntevs <= 0) {
  551. ret = -EINVAL;
  552. goto out_err;
  553. }
  554. /* Remove old cache entry */
  555. entry = probe_cache__find(pcache, pev);
  556. if (entry) {
  557. list_del_init(&entry->node);
  558. probe_cache_entry__delete(entry);
  559. }
  560. ret = -ENOMEM;
  561. entry = probe_cache_entry__new(pev);
  562. if (!entry)
  563. goto out_err;
  564. for (i = 0; i < ntevs; i++) {
  565. if (!tevs[i].point.symbol)
  566. continue;
  567. command = synthesize_probe_trace_command(&tevs[i]);
  568. if (!command)
  569. goto out_err;
  570. strlist__add(entry->tevlist, command);
  571. free(command);
  572. }
  573. list_add_tail(&entry->node, &pcache->entries);
  574. pr_debug("Added probe cache: %d\n", ntevs);
  575. return 0;
  576. out_err:
  577. pr_debug("Failed to add probe caches\n");
  578. probe_cache_entry__delete(entry);
  579. return ret;
  580. }
  581. #ifdef HAVE_GELF_GETNOTE_SUPPORT
  582. static unsigned long long sdt_note__get_addr(struct sdt_note *note)
  583. {
  584. return note->bit32 ? (unsigned long long)note->addr.a32[0]
  585. : (unsigned long long)note->addr.a64[0];
  586. }
  587. int probe_cache__scan_sdt(struct probe_cache *pcache, const char *pathname)
  588. {
  589. struct probe_cache_entry *entry = NULL;
  590. struct list_head sdtlist;
  591. struct sdt_note *note;
  592. char *buf;
  593. char sdtgrp[64];
  594. int ret;
  595. INIT_LIST_HEAD(&sdtlist);
  596. ret = get_sdt_note_list(&sdtlist, pathname);
  597. if (ret < 0) {
  598. pr_debug4("Failed to get sdt note: %d\n", ret);
  599. return ret;
  600. }
  601. list_for_each_entry(note, &sdtlist, note_list) {
  602. ret = snprintf(sdtgrp, 64, "sdt_%s", note->provider);
  603. if (ret < 0)
  604. break;
  605. /* Try to find same-name entry */
  606. entry = probe_cache__find_by_name(pcache, sdtgrp, note->name);
  607. if (!entry) {
  608. entry = probe_cache_entry__new(NULL);
  609. if (!entry) {
  610. ret = -ENOMEM;
  611. break;
  612. }
  613. entry->sdt = true;
  614. ret = asprintf(&entry->spev, "%s:%s=%s", sdtgrp,
  615. note->name, note->name);
  616. if (ret < 0)
  617. break;
  618. entry->pev.event = strdup(note->name);
  619. entry->pev.group = strdup(sdtgrp);
  620. list_add_tail(&entry->node, &pcache->entries);
  621. }
  622. ret = asprintf(&buf, "p:%s/%s %s:0x%llx",
  623. sdtgrp, note->name, pathname,
  624. sdt_note__get_addr(note));
  625. if (ret < 0)
  626. break;
  627. strlist__add(entry->tevlist, buf);
  628. free(buf);
  629. entry = NULL;
  630. }
  631. if (entry) {
  632. list_del_init(&entry->node);
  633. probe_cache_entry__delete(entry);
  634. }
  635. cleanup_sdt_note_list(&sdtlist);
  636. return ret;
  637. }
  638. #endif
  639. static int probe_cache_entry__write(struct probe_cache_entry *entry, int fd)
  640. {
  641. struct str_node *snode;
  642. struct stat st;
  643. struct iovec iov[3];
  644. const char *prefix = entry->sdt ? "%" : "#";
  645. int ret;
  646. /* Save stat for rollback */
  647. ret = fstat(fd, &st);
  648. if (ret < 0)
  649. return ret;
  650. pr_debug("Writing cache: %s%s\n", prefix, entry->spev);
  651. iov[0].iov_base = (void *)prefix; iov[0].iov_len = 1;
  652. iov[1].iov_base = entry->spev; iov[1].iov_len = strlen(entry->spev);
  653. iov[2].iov_base = (void *)"\n"; iov[2].iov_len = 1;
  654. ret = writev(fd, iov, 3);
  655. if (ret < (int)iov[1].iov_len + 2)
  656. goto rollback;
  657. strlist__for_each_entry(snode, entry->tevlist) {
  658. iov[0].iov_base = (void *)snode->s;
  659. iov[0].iov_len = strlen(snode->s);
  660. iov[1].iov_base = (void *)"\n"; iov[1].iov_len = 1;
  661. ret = writev(fd, iov, 2);
  662. if (ret < (int)iov[0].iov_len + 1)
  663. goto rollback;
  664. }
  665. return 0;
  666. rollback:
  667. /* Rollback to avoid cache file corruption */
  668. if (ret > 0)
  669. ret = -1;
  670. if (ftruncate(fd, st.st_size) < 0)
  671. ret = -2;
  672. return ret;
  673. }
  674. int probe_cache__commit(struct probe_cache *pcache)
  675. {
  676. struct probe_cache_entry *entry;
  677. int ret = 0;
  678. /* TBD: if we do not update existing entries, skip it */
  679. ret = lseek(pcache->fd, 0, SEEK_SET);
  680. if (ret < 0)
  681. goto out;
  682. ret = ftruncate(pcache->fd, 0);
  683. if (ret < 0)
  684. goto out;
  685. for_each_probe_cache_entry(entry, pcache) {
  686. ret = probe_cache_entry__write(entry, pcache->fd);
  687. pr_debug("Cache committed: %d\n", ret);
  688. if (ret < 0)
  689. break;
  690. }
  691. out:
  692. return ret;
  693. }
  694. static bool probe_cache_entry__compare(struct probe_cache_entry *entry,
  695. struct strfilter *filter)
  696. {
  697. char buf[128], *ptr = entry->spev;
  698. if (entry->pev.event) {
  699. snprintf(buf, 128, "%s:%s", entry->pev.group, entry->pev.event);
  700. ptr = buf;
  701. }
  702. return strfilter__compare(filter, ptr);
  703. }
  704. int probe_cache__filter_purge(struct probe_cache *pcache,
  705. struct strfilter *filter)
  706. {
  707. struct probe_cache_entry *entry, *tmp;
  708. list_for_each_entry_safe(entry, tmp, &pcache->entries, node) {
  709. if (probe_cache_entry__compare(entry, filter)) {
  710. pr_info("Removed cached event: %s\n", entry->spev);
  711. list_del_init(&entry->node);
  712. probe_cache_entry__delete(entry);
  713. }
  714. }
  715. return 0;
  716. }
  717. static int probe_cache__show_entries(struct probe_cache *pcache,
  718. struct strfilter *filter)
  719. {
  720. struct probe_cache_entry *entry;
  721. for_each_probe_cache_entry(entry, pcache) {
  722. if (probe_cache_entry__compare(entry, filter))
  723. printf("%s\n", entry->spev);
  724. }
  725. return 0;
  726. }
  727. /* Show all cached probes */
  728. int probe_cache__show_all_caches(struct strfilter *filter)
  729. {
  730. struct probe_cache *pcache;
  731. struct strlist *bidlist;
  732. struct str_node *nd;
  733. char *buf = strfilter__string(filter);
  734. pr_debug("list cache with filter: %s\n", buf);
  735. free(buf);
  736. bidlist = build_id_cache__list_all(true);
  737. if (!bidlist) {
  738. pr_debug("Failed to get buildids: %d\n", errno);
  739. return -EINVAL;
  740. }
  741. strlist__for_each_entry(nd, bidlist) {
  742. pcache = probe_cache__new(nd->s);
  743. if (!pcache)
  744. continue;
  745. if (!list_empty(&pcache->entries)) {
  746. buf = build_id_cache__origname(nd->s);
  747. printf("%s (%s):\n", buf, nd->s);
  748. free(buf);
  749. probe_cache__show_entries(pcache, filter);
  750. }
  751. probe_cache__delete(pcache);
  752. }
  753. strlist__delete(bidlist);
  754. return 0;
  755. }
  756. static struct {
  757. const char *pattern;
  758. bool avail;
  759. bool checked;
  760. } probe_type_table[] = {
  761. #define DEFINE_TYPE(idx, pat, def_avail) \
  762. [idx] = {.pattern = pat, .avail = (def_avail)}
  763. DEFINE_TYPE(PROBE_TYPE_U, "* u8/16/32/64,*", true),
  764. DEFINE_TYPE(PROBE_TYPE_S, "* s8/16/32/64,*", true),
  765. DEFINE_TYPE(PROBE_TYPE_X, "* x8/16/32/64,*", false),
  766. DEFINE_TYPE(PROBE_TYPE_STRING, "* string,*", true),
  767. DEFINE_TYPE(PROBE_TYPE_BITFIELD,
  768. "* b<bit-width>@<bit-offset>/<container-size>", true),
  769. };
  770. bool probe_type_is_available(enum probe_type type)
  771. {
  772. FILE *fp;
  773. char *buf = NULL;
  774. size_t len = 0;
  775. bool target_line = false;
  776. bool ret = probe_type_table[type].avail;
  777. if (type >= PROBE_TYPE_END)
  778. return false;
  779. /* We don't have to check the type which supported by default */
  780. if (ret || probe_type_table[type].checked)
  781. return ret;
  782. if (asprintf(&buf, "%s/README", tracing_path) < 0)
  783. return ret;
  784. fp = fopen(buf, "r");
  785. if (!fp)
  786. goto end;
  787. zfree(&buf);
  788. while (getline(&buf, &len, fp) > 0 && !ret) {
  789. if (!target_line) {
  790. target_line = !!strstr(buf, " type: ");
  791. if (!target_line)
  792. continue;
  793. } else if (strstr(buf, "\t ") != buf)
  794. break;
  795. ret = strglobmatch(buf, probe_type_table[type].pattern);
  796. }
  797. /* Cache the result */
  798. probe_type_table[type].checked = true;
  799. probe_type_table[type].avail = ret;
  800. fclose(fp);
  801. end:
  802. free(buf);
  803. return ret;
  804. }