probe-file.c 23 KB

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