probe-file.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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. strncpy(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 ?
  598. (unsigned long long)note->addr.a32[SDT_NOTE_IDX_LOC] :
  599. (unsigned long long)note->addr.a64[SDT_NOTE_IDX_LOC];
  600. }
  601. static unsigned long long sdt_note__get_ref_ctr_offset(struct sdt_note *note)
  602. {
  603. return note->bit32 ?
  604. (unsigned long long)note->addr.a32[SDT_NOTE_IDX_REFCTR] :
  605. (unsigned long long)note->addr.a64[SDT_NOTE_IDX_REFCTR];
  606. }
  607. static const char * const type_to_suffix[] = {
  608. ":s64", "", "", "", ":s32", "", ":s16", ":s8",
  609. "", ":u8", ":u16", "", ":u32", "", "", "", ":u64"
  610. };
  611. /*
  612. * Isolate the string number and convert it into a decimal value;
  613. * this will be an index to get suffix of the uprobe name (defining
  614. * the type)
  615. */
  616. static int sdt_arg_parse_size(char *n_ptr, const char **suffix)
  617. {
  618. long type_idx;
  619. type_idx = strtol(n_ptr, NULL, 10);
  620. if (type_idx < -8 || type_idx > 8) {
  621. pr_debug4("Failed to get a valid sdt type\n");
  622. return -1;
  623. }
  624. *suffix = type_to_suffix[type_idx + 8];
  625. return 0;
  626. }
  627. static int synthesize_sdt_probe_arg(struct strbuf *buf, int i, const char *arg)
  628. {
  629. char *op, *desc = strdup(arg), *new_op = NULL;
  630. const char *suffix = "";
  631. int ret = -1;
  632. if (desc == NULL) {
  633. pr_debug4("Allocation error\n");
  634. return ret;
  635. }
  636. /*
  637. * Argument is in N@OP format. N is size of the argument and OP is
  638. * the actual assembly operand. N can be omitted; in that case
  639. * argument is just OP(without @).
  640. */
  641. op = strchr(desc, '@');
  642. if (op) {
  643. op[0] = '\0';
  644. op++;
  645. if (sdt_arg_parse_size(desc, &suffix))
  646. goto error;
  647. } else {
  648. op = desc;
  649. }
  650. ret = arch_sdt_arg_parse_op(op, &new_op);
  651. if (ret < 0)
  652. goto error;
  653. if (ret == SDT_ARG_VALID) {
  654. ret = strbuf_addf(buf, " arg%d=%s%s", i + 1, new_op, suffix);
  655. if (ret < 0)
  656. goto error;
  657. }
  658. ret = 0;
  659. error:
  660. free(desc);
  661. free(new_op);
  662. return ret;
  663. }
  664. static char *synthesize_sdt_probe_command(struct sdt_note *note,
  665. const char *pathname,
  666. const char *sdtgrp)
  667. {
  668. struct strbuf buf;
  669. char *ret = NULL, **args;
  670. int i, args_count, err;
  671. unsigned long long ref_ctr_offset;
  672. if (strbuf_init(&buf, 32) < 0)
  673. return NULL;
  674. err = strbuf_addf(&buf, "p:%s/%s %s:0x%llx",
  675. sdtgrp, note->name, pathname,
  676. sdt_note__get_addr(note));
  677. ref_ctr_offset = sdt_note__get_ref_ctr_offset(note);
  678. if (ref_ctr_offset && err >= 0)
  679. err = strbuf_addf(&buf, "(0x%llx)", ref_ctr_offset);
  680. if (err < 0)
  681. goto error;
  682. if (!note->args)
  683. goto out;
  684. if (note->args) {
  685. args = argv_split(note->args, &args_count);
  686. for (i = 0; i < args_count; ++i) {
  687. if (synthesize_sdt_probe_arg(&buf, i, args[i]) < 0)
  688. goto error;
  689. }
  690. }
  691. out:
  692. ret = strbuf_detach(&buf, NULL);
  693. error:
  694. strbuf_release(&buf);
  695. return ret;
  696. }
  697. int probe_cache__scan_sdt(struct probe_cache *pcache, const char *pathname)
  698. {
  699. struct probe_cache_entry *entry = NULL;
  700. struct list_head sdtlist;
  701. struct sdt_note *note;
  702. char *buf;
  703. char sdtgrp[64];
  704. int ret;
  705. INIT_LIST_HEAD(&sdtlist);
  706. ret = get_sdt_note_list(&sdtlist, pathname);
  707. if (ret < 0) {
  708. pr_debug4("Failed to get sdt note: %d\n", ret);
  709. return ret;
  710. }
  711. list_for_each_entry(note, &sdtlist, note_list) {
  712. ret = snprintf(sdtgrp, 64, "sdt_%s", note->provider);
  713. if (ret < 0)
  714. break;
  715. /* Try to find same-name entry */
  716. entry = probe_cache__find_by_name(pcache, sdtgrp, note->name);
  717. if (!entry) {
  718. entry = probe_cache_entry__new(NULL);
  719. if (!entry) {
  720. ret = -ENOMEM;
  721. break;
  722. }
  723. entry->sdt = true;
  724. ret = asprintf(&entry->spev, "%s:%s=%s", sdtgrp,
  725. note->name, note->name);
  726. if (ret < 0)
  727. break;
  728. entry->pev.event = strdup(note->name);
  729. entry->pev.group = strdup(sdtgrp);
  730. list_add_tail(&entry->node, &pcache->entries);
  731. }
  732. buf = synthesize_sdt_probe_command(note, pathname, sdtgrp);
  733. if (!buf) {
  734. ret = -ENOMEM;
  735. break;
  736. }
  737. strlist__add(entry->tevlist, buf);
  738. free(buf);
  739. entry = NULL;
  740. }
  741. if (entry) {
  742. list_del_init(&entry->node);
  743. probe_cache_entry__delete(entry);
  744. }
  745. cleanup_sdt_note_list(&sdtlist);
  746. return ret;
  747. }
  748. #endif
  749. static int probe_cache_entry__write(struct probe_cache_entry *entry, int fd)
  750. {
  751. struct str_node *snode;
  752. struct stat st;
  753. struct iovec iov[3];
  754. const char *prefix = entry->sdt ? "%" : "#";
  755. int ret;
  756. /* Save stat for rollback */
  757. ret = fstat(fd, &st);
  758. if (ret < 0)
  759. return ret;
  760. pr_debug("Writing cache: %s%s\n", prefix, entry->spev);
  761. iov[0].iov_base = (void *)prefix; iov[0].iov_len = 1;
  762. iov[1].iov_base = entry->spev; iov[1].iov_len = strlen(entry->spev);
  763. iov[2].iov_base = (void *)"\n"; iov[2].iov_len = 1;
  764. ret = writev(fd, iov, 3);
  765. if (ret < (int)iov[1].iov_len + 2)
  766. goto rollback;
  767. strlist__for_each_entry(snode, entry->tevlist) {
  768. iov[0].iov_base = (void *)snode->s;
  769. iov[0].iov_len = strlen(snode->s);
  770. iov[1].iov_base = (void *)"\n"; iov[1].iov_len = 1;
  771. ret = writev(fd, iov, 2);
  772. if (ret < (int)iov[0].iov_len + 1)
  773. goto rollback;
  774. }
  775. return 0;
  776. rollback:
  777. /* Rollback to avoid cache file corruption */
  778. if (ret > 0)
  779. ret = -1;
  780. if (ftruncate(fd, st.st_size) < 0)
  781. ret = -2;
  782. return ret;
  783. }
  784. int probe_cache__commit(struct probe_cache *pcache)
  785. {
  786. struct probe_cache_entry *entry;
  787. int ret = 0;
  788. /* TBD: if we do not update existing entries, skip it */
  789. ret = lseek(pcache->fd, 0, SEEK_SET);
  790. if (ret < 0)
  791. goto out;
  792. ret = ftruncate(pcache->fd, 0);
  793. if (ret < 0)
  794. goto out;
  795. for_each_probe_cache_entry(entry, pcache) {
  796. ret = probe_cache_entry__write(entry, pcache->fd);
  797. pr_debug("Cache committed: %d\n", ret);
  798. if (ret < 0)
  799. break;
  800. }
  801. out:
  802. return ret;
  803. }
  804. static bool probe_cache_entry__compare(struct probe_cache_entry *entry,
  805. struct strfilter *filter)
  806. {
  807. char buf[128], *ptr = entry->spev;
  808. if (entry->pev.event) {
  809. snprintf(buf, 128, "%s:%s", entry->pev.group, entry->pev.event);
  810. ptr = buf;
  811. }
  812. return strfilter__compare(filter, ptr);
  813. }
  814. int probe_cache__filter_purge(struct probe_cache *pcache,
  815. struct strfilter *filter)
  816. {
  817. struct probe_cache_entry *entry, *tmp;
  818. list_for_each_entry_safe(entry, tmp, &pcache->entries, node) {
  819. if (probe_cache_entry__compare(entry, filter)) {
  820. pr_info("Removed cached event: %s\n", entry->spev);
  821. list_del_init(&entry->node);
  822. probe_cache_entry__delete(entry);
  823. }
  824. }
  825. return 0;
  826. }
  827. static int probe_cache__show_entries(struct probe_cache *pcache,
  828. struct strfilter *filter)
  829. {
  830. struct probe_cache_entry *entry;
  831. for_each_probe_cache_entry(entry, pcache) {
  832. if (probe_cache_entry__compare(entry, filter))
  833. printf("%s\n", entry->spev);
  834. }
  835. return 0;
  836. }
  837. /* Show all cached probes */
  838. int probe_cache__show_all_caches(struct strfilter *filter)
  839. {
  840. struct probe_cache *pcache;
  841. struct strlist *bidlist;
  842. struct str_node *nd;
  843. char *buf = strfilter__string(filter);
  844. pr_debug("list cache with filter: %s\n", buf);
  845. free(buf);
  846. bidlist = build_id_cache__list_all(true);
  847. if (!bidlist) {
  848. pr_debug("Failed to get buildids: %d\n", errno);
  849. return -EINVAL;
  850. }
  851. strlist__for_each_entry(nd, bidlist) {
  852. pcache = probe_cache__new(nd->s, NULL);
  853. if (!pcache)
  854. continue;
  855. if (!list_empty(&pcache->entries)) {
  856. buf = build_id_cache__origname(nd->s);
  857. printf("%s (%s):\n", buf, nd->s);
  858. free(buf);
  859. probe_cache__show_entries(pcache, filter);
  860. }
  861. probe_cache__delete(pcache);
  862. }
  863. strlist__delete(bidlist);
  864. return 0;
  865. }
  866. enum ftrace_readme {
  867. FTRACE_README_PROBE_TYPE_X = 0,
  868. FTRACE_README_KRETPROBE_OFFSET,
  869. FTRACE_README_UPROBE_REF_CTR,
  870. FTRACE_README_END,
  871. };
  872. static struct {
  873. const char *pattern;
  874. bool avail;
  875. } ftrace_readme_table[] = {
  876. #define DEFINE_TYPE(idx, pat) \
  877. [idx] = {.pattern = pat, .avail = false}
  878. DEFINE_TYPE(FTRACE_README_PROBE_TYPE_X, "*type: * x8/16/32/64,*"),
  879. DEFINE_TYPE(FTRACE_README_KRETPROBE_OFFSET, "*place (kretprobe): *"),
  880. DEFINE_TYPE(FTRACE_README_UPROBE_REF_CTR, "*ref_ctr_offset*"),
  881. };
  882. static bool scan_ftrace_readme(enum ftrace_readme type)
  883. {
  884. int fd;
  885. FILE *fp;
  886. char *buf = NULL;
  887. size_t len = 0;
  888. bool ret = false;
  889. static bool scanned = false;
  890. if (scanned)
  891. goto result;
  892. fd = open_trace_file("README", false);
  893. if (fd < 0)
  894. return ret;
  895. fp = fdopen(fd, "r");
  896. if (!fp) {
  897. close(fd);
  898. return ret;
  899. }
  900. while (getline(&buf, &len, fp) > 0)
  901. for (enum ftrace_readme i = 0; i < FTRACE_README_END; i++)
  902. if (!ftrace_readme_table[i].avail)
  903. ftrace_readme_table[i].avail =
  904. strglobmatch(buf, ftrace_readme_table[i].pattern);
  905. scanned = true;
  906. fclose(fp);
  907. free(buf);
  908. result:
  909. if (type >= FTRACE_README_END)
  910. return false;
  911. return ftrace_readme_table[type].avail;
  912. }
  913. bool probe_type_is_available(enum probe_type type)
  914. {
  915. if (type >= PROBE_TYPE_END)
  916. return false;
  917. else if (type == PROBE_TYPE_X)
  918. return scan_ftrace_readme(FTRACE_README_PROBE_TYPE_X);
  919. return true;
  920. }
  921. bool kretprobe_offset_is_supported(void)
  922. {
  923. return scan_ftrace_readme(FTRACE_README_KRETPROBE_OFFSET);
  924. }
  925. bool uprobe_ref_ctr_is_supported(void)
  926. {
  927. return scan_ftrace_readme(FTRACE_README_UPROBE_REF_CTR);
  928. }