probe-event.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827
  1. /*
  2. * probe-event.c : perf-probe definition to probe_events format converter
  3. *
  4. * Written by Masami Hiramatsu <mhiramat@redhat.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. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *
  20. */
  21. #define _GNU_SOURCE
  22. #include <sys/utsname.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include <errno.h>
  27. #include <stdio.h>
  28. #include <unistd.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdarg.h>
  32. #include <limits.h>
  33. #undef _GNU_SOURCE
  34. #include "util.h"
  35. #include "event.h"
  36. #include "string.h"
  37. #include "strlist.h"
  38. #include "debug.h"
  39. #include "cache.h"
  40. #include "color.h"
  41. #include "symbol.h"
  42. #include "thread.h"
  43. #include "debugfs.h"
  44. #include "trace-event.h" /* For __unused */
  45. #include "probe-event.h"
  46. #include "probe-finder.h"
  47. #define MAX_CMDLEN 256
  48. #define MAX_PROBE_ARGS 128
  49. #define PERFPROBE_GROUP "probe"
  50. bool probe_event_dry_run; /* Dry run flag */
  51. #define semantic_error(msg ...) pr_err("Semantic error :" msg)
  52. /* If there is no space to write, returns -E2BIG. */
  53. static int e_snprintf(char *str, size_t size, const char *format, ...)
  54. __attribute__((format(printf, 3, 4)));
  55. static int e_snprintf(char *str, size_t size, const char *format, ...)
  56. {
  57. int ret;
  58. va_list ap;
  59. va_start(ap, format);
  60. ret = vsnprintf(str, size, format, ap);
  61. va_end(ap);
  62. if (ret >= (int)size)
  63. ret = -E2BIG;
  64. return ret;
  65. }
  66. static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
  67. static struct machine machine;
  68. /* Initialize symbol maps and path of vmlinux */
  69. static int init_vmlinux(void)
  70. {
  71. struct dso *kernel;
  72. int ret;
  73. symbol_conf.sort_by_name = true;
  74. if (symbol_conf.vmlinux_name == NULL)
  75. symbol_conf.try_vmlinux_path = true;
  76. else
  77. pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
  78. ret = symbol__init();
  79. if (ret < 0) {
  80. pr_debug("Failed to init symbol map.\n");
  81. goto out;
  82. }
  83. ret = machine__init(&machine, "/", 0);
  84. if (ret < 0)
  85. goto out;
  86. kernel = dso__new_kernel(symbol_conf.vmlinux_name);
  87. if (kernel == NULL)
  88. die("Failed to create kernel dso.");
  89. ret = __machine__create_kernel_maps(&machine, kernel);
  90. if (ret < 0)
  91. pr_debug("Failed to create kernel maps.\n");
  92. out:
  93. if (ret < 0)
  94. pr_warning("Failed to init vmlinux path.\n");
  95. return ret;
  96. }
  97. #ifdef DWARF_SUPPORT
  98. static int open_vmlinux(void)
  99. {
  100. if (map__load(machine.vmlinux_maps[MAP__FUNCTION], NULL) < 0) {
  101. pr_debug("Failed to load kernel map.\n");
  102. return -EINVAL;
  103. }
  104. pr_debug("Try to open %s\n", machine.vmlinux_maps[MAP__FUNCTION]->dso->long_name);
  105. return open(machine.vmlinux_maps[MAP__FUNCTION]->dso->long_name, O_RDONLY);
  106. }
  107. /*
  108. * Convert trace point to probe point with debuginfo
  109. * Currently only handles kprobes.
  110. */
  111. static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
  112. struct perf_probe_point *pp)
  113. {
  114. struct symbol *sym;
  115. int fd, ret = -ENOENT;
  116. sym = map__find_symbol_by_name(machine.vmlinux_maps[MAP__FUNCTION],
  117. tp->symbol, NULL);
  118. if (sym) {
  119. fd = open_vmlinux();
  120. if (fd >= 0) {
  121. ret = find_perf_probe_point(fd,
  122. sym->start + tp->offset, pp);
  123. close(fd);
  124. }
  125. }
  126. if (ret <= 0) {
  127. pr_debug("Failed to find corresponding probes from "
  128. "debuginfo. Use kprobe event information.\n");
  129. pp->function = strdup(tp->symbol);
  130. if (pp->function == NULL)
  131. return -ENOMEM;
  132. pp->offset = tp->offset;
  133. }
  134. pp->retprobe = tp->retprobe;
  135. return 0;
  136. }
  137. /* Try to find perf_probe_event with debuginfo */
  138. static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
  139. struct probe_trace_event **tevs,
  140. int max_tevs)
  141. {
  142. bool need_dwarf = perf_probe_event_need_dwarf(pev);
  143. int fd, ntevs;
  144. fd = open_vmlinux();
  145. if (fd < 0) {
  146. if (need_dwarf) {
  147. pr_warning("Failed to open debuginfo file.\n");
  148. return fd;
  149. }
  150. pr_debug("Could not open vmlinux. Try to use symbols.\n");
  151. return 0;
  152. }
  153. /* Searching trace events corresponding to probe event */
  154. ntevs = find_probe_trace_events(fd, pev, tevs, max_tevs);
  155. close(fd);
  156. if (ntevs > 0) { /* Succeeded to find trace events */
  157. pr_debug("find %d probe_trace_events.\n", ntevs);
  158. return ntevs;
  159. }
  160. if (ntevs == 0) { /* No error but failed to find probe point. */
  161. pr_warning("Probe point '%s' not found.\n",
  162. synthesize_perf_probe_point(&pev->point));
  163. return -ENOENT;
  164. }
  165. /* Error path : ntevs < 0 */
  166. pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
  167. if (ntevs == -EBADF) {
  168. pr_warning("Warning: No dwarf info found in the vmlinux - "
  169. "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
  170. if (!need_dwarf) {
  171. pr_debug("Trying to use symbols.\nn");
  172. return 0;
  173. }
  174. }
  175. return ntevs;
  176. }
  177. /*
  178. * Find a src file from a DWARF tag path. Prepend optional source path prefix
  179. * and chop off leading directories that do not exist. Result is passed back as
  180. * a newly allocated path on success.
  181. * Return 0 if file was found and readable, -errno otherwise.
  182. */
  183. static int get_real_path(const char *raw_path, const char *comp_dir,
  184. char **new_path)
  185. {
  186. const char *prefix = symbol_conf.source_prefix;
  187. if (!prefix) {
  188. if (raw_path[0] != '/' && comp_dir)
  189. /* If not an absolute path, try to use comp_dir */
  190. prefix = comp_dir;
  191. else {
  192. if (access(raw_path, R_OK) == 0) {
  193. *new_path = strdup(raw_path);
  194. return 0;
  195. } else
  196. return -errno;
  197. }
  198. }
  199. *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
  200. if (!*new_path)
  201. return -ENOMEM;
  202. for (;;) {
  203. sprintf(*new_path, "%s/%s", prefix, raw_path);
  204. if (access(*new_path, R_OK) == 0)
  205. return 0;
  206. if (!symbol_conf.source_prefix)
  207. /* In case of searching comp_dir, don't retry */
  208. return -errno;
  209. switch (errno) {
  210. case ENAMETOOLONG:
  211. case ENOENT:
  212. case EROFS:
  213. case EFAULT:
  214. raw_path = strchr(++raw_path, '/');
  215. if (!raw_path) {
  216. free(*new_path);
  217. *new_path = NULL;
  218. return -ENOENT;
  219. }
  220. continue;
  221. default:
  222. free(*new_path);
  223. *new_path = NULL;
  224. return -errno;
  225. }
  226. }
  227. }
  228. #define LINEBUF_SIZE 256
  229. #define NR_ADDITIONAL_LINES 2
  230. static int show_one_line(FILE *fp, int l, bool skip, bool show_num)
  231. {
  232. char buf[LINEBUF_SIZE];
  233. const char *color = PERF_COLOR_BLUE;
  234. if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
  235. goto error;
  236. if (!skip) {
  237. if (show_num)
  238. fprintf(stdout, "%7d %s", l, buf);
  239. else
  240. color_fprintf(stdout, color, " %s", buf);
  241. }
  242. while (strlen(buf) == LINEBUF_SIZE - 1 &&
  243. buf[LINEBUF_SIZE - 2] != '\n') {
  244. if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
  245. goto error;
  246. if (!skip) {
  247. if (show_num)
  248. fprintf(stdout, "%s", buf);
  249. else
  250. color_fprintf(stdout, color, "%s", buf);
  251. }
  252. }
  253. return 0;
  254. error:
  255. if (feof(fp))
  256. pr_warning("Source file is shorter than expected.\n");
  257. else
  258. pr_warning("File read error: %s\n", strerror(errno));
  259. return -1;
  260. }
  261. /*
  262. * Show line-range always requires debuginfo to find source file and
  263. * line number.
  264. */
  265. int show_line_range(struct line_range *lr)
  266. {
  267. int l = 1;
  268. struct line_node *ln;
  269. FILE *fp;
  270. int fd, ret;
  271. char *tmp;
  272. /* Search a line range */
  273. ret = init_vmlinux();
  274. if (ret < 0)
  275. return ret;
  276. fd = open_vmlinux();
  277. if (fd < 0) {
  278. pr_warning("Failed to open debuginfo file.\n");
  279. return fd;
  280. }
  281. ret = find_line_range(fd, lr);
  282. close(fd);
  283. if (ret == 0) {
  284. pr_warning("Specified source line is not found.\n");
  285. return -ENOENT;
  286. } else if (ret < 0) {
  287. pr_warning("Debuginfo analysis failed. (%d)\n", ret);
  288. return ret;
  289. }
  290. /* Convert source file path */
  291. tmp = lr->path;
  292. ret = get_real_path(tmp, lr->comp_dir, &lr->path);
  293. free(tmp); /* Free old path */
  294. if (ret < 0) {
  295. pr_warning("Failed to find source file. (%d)\n", ret);
  296. return ret;
  297. }
  298. setup_pager();
  299. if (lr->function)
  300. fprintf(stdout, "<%s:%d>\n", lr->function,
  301. lr->start - lr->offset);
  302. else
  303. fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
  304. fp = fopen(lr->path, "r");
  305. if (fp == NULL) {
  306. pr_warning("Failed to open %s: %s\n", lr->path,
  307. strerror(errno));
  308. return -errno;
  309. }
  310. /* Skip to starting line number */
  311. while (l < lr->start && ret >= 0)
  312. ret = show_one_line(fp, l++, true, false);
  313. if (ret < 0)
  314. goto end;
  315. list_for_each_entry(ln, &lr->line_list, list) {
  316. while (ln->line > l && ret >= 0)
  317. ret = show_one_line(fp, (l++) - lr->offset,
  318. false, false);
  319. if (ret >= 0)
  320. ret = show_one_line(fp, (l++) - lr->offset,
  321. false, true);
  322. if (ret < 0)
  323. goto end;
  324. }
  325. if (lr->end == INT_MAX)
  326. lr->end = l + NR_ADDITIONAL_LINES;
  327. while (l <= lr->end && !feof(fp) && ret >= 0)
  328. ret = show_one_line(fp, (l++) - lr->offset, false, false);
  329. end:
  330. fclose(fp);
  331. return ret;
  332. }
  333. static int show_available_vars_at(int fd, struct perf_probe_event *pev,
  334. int max_vls)
  335. {
  336. char *buf;
  337. int ret, i;
  338. struct str_node *node;
  339. struct variable_list *vls = NULL, *vl;
  340. buf = synthesize_perf_probe_point(&pev->point);
  341. if (!buf)
  342. return -EINVAL;
  343. pr_debug("Searching variables at %s\n", buf);
  344. ret = find_available_vars_at(fd, pev, &vls, max_vls);
  345. if (ret > 0) {
  346. /* Some variables were found */
  347. fprintf(stdout, "Available variables at %s\n", buf);
  348. for (i = 0; i < ret; i++) {
  349. vl = &vls[i];
  350. /*
  351. * A probe point might be converted to
  352. * several trace points.
  353. */
  354. fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
  355. vl->point.offset);
  356. free(vl->point.symbol);
  357. if (vl->vars) {
  358. strlist__for_each(node, vl->vars)
  359. fprintf(stdout, "\t\t%s\n", node->s);
  360. strlist__delete(vl->vars);
  361. } else
  362. fprintf(stdout, "(No variables)\n");
  363. }
  364. free(vls);
  365. } else
  366. pr_err("Failed to find variables at %s (%d)\n", buf, ret);
  367. free(buf);
  368. return ret;
  369. }
  370. /* Show available variables on given probe point */
  371. int show_available_vars(struct perf_probe_event *pevs, int npevs,
  372. int max_vls)
  373. {
  374. int i, fd, ret = 0;
  375. ret = init_vmlinux();
  376. if (ret < 0)
  377. return ret;
  378. fd = open_vmlinux();
  379. if (fd < 0) {
  380. pr_warning("Failed to open debuginfo file.\n");
  381. return fd;
  382. }
  383. setup_pager();
  384. for (i = 0; i < npevs && ret >= 0; i++)
  385. ret = show_available_vars_at(fd, &pevs[i], max_vls);
  386. close(fd);
  387. return ret;
  388. }
  389. #else /* !DWARF_SUPPORT */
  390. static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
  391. struct perf_probe_point *pp)
  392. {
  393. pp->function = strdup(tp->symbol);
  394. if (pp->function == NULL)
  395. return -ENOMEM;
  396. pp->offset = tp->offset;
  397. pp->retprobe = tp->retprobe;
  398. return 0;
  399. }
  400. static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
  401. struct probe_trace_event **tevs __unused,
  402. int max_tevs __unused)
  403. {
  404. if (perf_probe_event_need_dwarf(pev)) {
  405. pr_warning("Debuginfo-analysis is not supported.\n");
  406. return -ENOSYS;
  407. }
  408. return 0;
  409. }
  410. int show_line_range(struct line_range *lr __unused)
  411. {
  412. pr_warning("Debuginfo-analysis is not supported.\n");
  413. return -ENOSYS;
  414. }
  415. int show_available_vars(struct perf_probe_event *pevs __unused,
  416. int npevs __unused, int max_probe_points __unused)
  417. {
  418. pr_warning("Debuginfo-analysis is not supported.\n");
  419. return -ENOSYS;
  420. }
  421. #endif
  422. int parse_line_range_desc(const char *arg, struct line_range *lr)
  423. {
  424. const char *ptr;
  425. char *tmp;
  426. /*
  427. * <Syntax>
  428. * SRC:SLN[+NUM|-ELN]
  429. * FUNC[:SLN[+NUM|-ELN]]
  430. */
  431. ptr = strchr(arg, ':');
  432. if (ptr) {
  433. lr->start = (int)strtoul(ptr + 1, &tmp, 0);
  434. if (*tmp == '+') {
  435. lr->end = lr->start + (int)strtoul(tmp + 1, &tmp, 0);
  436. lr->end--; /*
  437. * Adjust the number of lines here.
  438. * If the number of lines == 1, the
  439. * the end of line should be equal to
  440. * the start of line.
  441. */
  442. } else if (*tmp == '-')
  443. lr->end = (int)strtoul(tmp + 1, &tmp, 0);
  444. else
  445. lr->end = INT_MAX;
  446. pr_debug("Line range is %d to %d\n", lr->start, lr->end);
  447. if (lr->start > lr->end) {
  448. semantic_error("Start line must be smaller"
  449. " than end line.\n");
  450. return -EINVAL;
  451. }
  452. if (*tmp != '\0') {
  453. semantic_error("Tailing with invalid character '%d'.\n",
  454. *tmp);
  455. return -EINVAL;
  456. }
  457. tmp = strndup(arg, (ptr - arg));
  458. } else {
  459. tmp = strdup(arg);
  460. lr->end = INT_MAX;
  461. }
  462. if (tmp == NULL)
  463. return -ENOMEM;
  464. if (strchr(tmp, '.'))
  465. lr->file = tmp;
  466. else
  467. lr->function = tmp;
  468. return 0;
  469. }
  470. /* Check the name is good for event/group */
  471. static bool check_event_name(const char *name)
  472. {
  473. if (!isalpha(*name) && *name != '_')
  474. return false;
  475. while (*++name != '\0') {
  476. if (!isalpha(*name) && !isdigit(*name) && *name != '_')
  477. return false;
  478. }
  479. return true;
  480. }
  481. /* Parse probepoint definition. */
  482. static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
  483. {
  484. struct perf_probe_point *pp = &pev->point;
  485. char *ptr, *tmp;
  486. char c, nc = 0;
  487. /*
  488. * <Syntax>
  489. * perf probe [EVENT=]SRC[:LN|;PTN]
  490. * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
  491. *
  492. * TODO:Group name support
  493. */
  494. ptr = strpbrk(arg, ";=@+%");
  495. if (ptr && *ptr == '=') { /* Event name */
  496. *ptr = '\0';
  497. tmp = ptr + 1;
  498. if (strchr(arg, ':')) {
  499. semantic_error("Group name is not supported yet.\n");
  500. return -ENOTSUP;
  501. }
  502. if (!check_event_name(arg)) {
  503. semantic_error("%s is bad for event name -it must "
  504. "follow C symbol-naming rule.\n", arg);
  505. return -EINVAL;
  506. }
  507. pev->event = strdup(arg);
  508. if (pev->event == NULL)
  509. return -ENOMEM;
  510. pev->group = NULL;
  511. arg = tmp;
  512. }
  513. ptr = strpbrk(arg, ";:+@%");
  514. if (ptr) {
  515. nc = *ptr;
  516. *ptr++ = '\0';
  517. }
  518. tmp = strdup(arg);
  519. if (tmp == NULL)
  520. return -ENOMEM;
  521. /* Check arg is function or file and copy it */
  522. if (strchr(tmp, '.')) /* File */
  523. pp->file = tmp;
  524. else /* Function */
  525. pp->function = tmp;
  526. /* Parse other options */
  527. while (ptr) {
  528. arg = ptr;
  529. c = nc;
  530. if (c == ';') { /* Lazy pattern must be the last part */
  531. pp->lazy_line = strdup(arg);
  532. if (pp->lazy_line == NULL)
  533. return -ENOMEM;
  534. break;
  535. }
  536. ptr = strpbrk(arg, ";:+@%");
  537. if (ptr) {
  538. nc = *ptr;
  539. *ptr++ = '\0';
  540. }
  541. switch (c) {
  542. case ':': /* Line number */
  543. pp->line = strtoul(arg, &tmp, 0);
  544. if (*tmp != '\0') {
  545. semantic_error("There is non-digit char"
  546. " in line number.\n");
  547. return -EINVAL;
  548. }
  549. break;
  550. case '+': /* Byte offset from a symbol */
  551. pp->offset = strtoul(arg, &tmp, 0);
  552. if (*tmp != '\0') {
  553. semantic_error("There is non-digit character"
  554. " in offset.\n");
  555. return -EINVAL;
  556. }
  557. break;
  558. case '@': /* File name */
  559. if (pp->file) {
  560. semantic_error("SRC@SRC is not allowed.\n");
  561. return -EINVAL;
  562. }
  563. pp->file = strdup(arg);
  564. if (pp->file == NULL)
  565. return -ENOMEM;
  566. break;
  567. case '%': /* Probe places */
  568. if (strcmp(arg, "return") == 0) {
  569. pp->retprobe = 1;
  570. } else { /* Others not supported yet */
  571. semantic_error("%%%s is not supported.\n", arg);
  572. return -ENOTSUP;
  573. }
  574. break;
  575. default: /* Buggy case */
  576. pr_err("This program has a bug at %s:%d.\n",
  577. __FILE__, __LINE__);
  578. return -ENOTSUP;
  579. break;
  580. }
  581. }
  582. /* Exclusion check */
  583. if (pp->lazy_line && pp->line) {
  584. semantic_error("Lazy pattern can't be used with line number.");
  585. return -EINVAL;
  586. }
  587. if (pp->lazy_line && pp->offset) {
  588. semantic_error("Lazy pattern can't be used with offset.");
  589. return -EINVAL;
  590. }
  591. if (pp->line && pp->offset) {
  592. semantic_error("Offset can't be used with line number.");
  593. return -EINVAL;
  594. }
  595. if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
  596. semantic_error("File always requires line number or "
  597. "lazy pattern.");
  598. return -EINVAL;
  599. }
  600. if (pp->offset && !pp->function) {
  601. semantic_error("Offset requires an entry function.");
  602. return -EINVAL;
  603. }
  604. if (pp->retprobe && !pp->function) {
  605. semantic_error("Return probe requires an entry function.");
  606. return -EINVAL;
  607. }
  608. if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
  609. semantic_error("Offset/Line/Lazy pattern can't be used with "
  610. "return probe.");
  611. return -EINVAL;
  612. }
  613. pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
  614. pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
  615. pp->lazy_line);
  616. return 0;
  617. }
  618. /* Parse perf-probe event argument */
  619. static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
  620. {
  621. char *tmp, *goodname;
  622. struct perf_probe_arg_field **fieldp;
  623. pr_debug("parsing arg: %s into ", str);
  624. tmp = strchr(str, '=');
  625. if (tmp) {
  626. arg->name = strndup(str, tmp - str);
  627. if (arg->name == NULL)
  628. return -ENOMEM;
  629. pr_debug("name:%s ", arg->name);
  630. str = tmp + 1;
  631. }
  632. tmp = strchr(str, ':');
  633. if (tmp) { /* Type setting */
  634. *tmp = '\0';
  635. arg->type = strdup(tmp + 1);
  636. if (arg->type == NULL)
  637. return -ENOMEM;
  638. pr_debug("type:%s ", arg->type);
  639. }
  640. tmp = strpbrk(str, "-.[");
  641. if (!is_c_varname(str) || !tmp) {
  642. /* A variable, register, symbol or special value */
  643. arg->var = strdup(str);
  644. if (arg->var == NULL)
  645. return -ENOMEM;
  646. pr_debug("%s\n", arg->var);
  647. return 0;
  648. }
  649. /* Structure fields or array element */
  650. arg->var = strndup(str, tmp - str);
  651. if (arg->var == NULL)
  652. return -ENOMEM;
  653. goodname = arg->var;
  654. pr_debug("%s, ", arg->var);
  655. fieldp = &arg->field;
  656. do {
  657. *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
  658. if (*fieldp == NULL)
  659. return -ENOMEM;
  660. if (*tmp == '[') { /* Array */
  661. str = tmp;
  662. (*fieldp)->index = strtol(str + 1, &tmp, 0);
  663. (*fieldp)->ref = true;
  664. if (*tmp != ']' || tmp == str + 1) {
  665. semantic_error("Array index must be a"
  666. " number.\n");
  667. return -EINVAL;
  668. }
  669. tmp++;
  670. if (*tmp == '\0')
  671. tmp = NULL;
  672. } else { /* Structure */
  673. if (*tmp == '.') {
  674. str = tmp + 1;
  675. (*fieldp)->ref = false;
  676. } else if (tmp[1] == '>') {
  677. str = tmp + 2;
  678. (*fieldp)->ref = true;
  679. } else {
  680. semantic_error("Argument parse error: %s\n",
  681. str);
  682. return -EINVAL;
  683. }
  684. tmp = strpbrk(str, "-.[");
  685. }
  686. if (tmp) {
  687. (*fieldp)->name = strndup(str, tmp - str);
  688. if ((*fieldp)->name == NULL)
  689. return -ENOMEM;
  690. if (*str != '[')
  691. goodname = (*fieldp)->name;
  692. pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
  693. fieldp = &(*fieldp)->next;
  694. }
  695. } while (tmp);
  696. (*fieldp)->name = strdup(str);
  697. if ((*fieldp)->name == NULL)
  698. return -ENOMEM;
  699. if (*str != '[')
  700. goodname = (*fieldp)->name;
  701. pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
  702. /* If no name is specified, set the last field name (not array index)*/
  703. if (!arg->name) {
  704. arg->name = strdup(goodname);
  705. if (arg->name == NULL)
  706. return -ENOMEM;
  707. }
  708. return 0;
  709. }
  710. /* Parse perf-probe event command */
  711. int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
  712. {
  713. char **argv;
  714. int argc, i, ret = 0;
  715. argv = argv_split(cmd, &argc);
  716. if (!argv) {
  717. pr_debug("Failed to split arguments.\n");
  718. return -ENOMEM;
  719. }
  720. if (argc - 1 > MAX_PROBE_ARGS) {
  721. semantic_error("Too many probe arguments (%d).\n", argc - 1);
  722. ret = -ERANGE;
  723. goto out;
  724. }
  725. /* Parse probe point */
  726. ret = parse_perf_probe_point(argv[0], pev);
  727. if (ret < 0)
  728. goto out;
  729. /* Copy arguments and ensure return probe has no C argument */
  730. pev->nargs = argc - 1;
  731. pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  732. if (pev->args == NULL) {
  733. ret = -ENOMEM;
  734. goto out;
  735. }
  736. for (i = 0; i < pev->nargs && ret >= 0; i++) {
  737. ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
  738. if (ret >= 0 &&
  739. is_c_varname(pev->args[i].var) && pev->point.retprobe) {
  740. semantic_error("You can't specify local variable for"
  741. " kretprobe.\n");
  742. ret = -EINVAL;
  743. }
  744. }
  745. out:
  746. argv_free(argv);
  747. return ret;
  748. }
  749. /* Return true if this perf_probe_event requires debuginfo */
  750. bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
  751. {
  752. int i;
  753. if (pev->point.file || pev->point.line || pev->point.lazy_line)
  754. return true;
  755. for (i = 0; i < pev->nargs; i++)
  756. if (is_c_varname(pev->args[i].var))
  757. return true;
  758. return false;
  759. }
  760. /* Parse probe_events event into struct probe_point */
  761. static int parse_probe_trace_command(const char *cmd,
  762. struct probe_trace_event *tev)
  763. {
  764. struct probe_trace_point *tp = &tev->point;
  765. char pr;
  766. char *p;
  767. int ret, i, argc;
  768. char **argv;
  769. pr_debug("Parsing probe_events: %s\n", cmd);
  770. argv = argv_split(cmd, &argc);
  771. if (!argv) {
  772. pr_debug("Failed to split arguments.\n");
  773. return -ENOMEM;
  774. }
  775. if (argc < 2) {
  776. semantic_error("Too few probe arguments.\n");
  777. ret = -ERANGE;
  778. goto out;
  779. }
  780. /* Scan event and group name. */
  781. ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
  782. &pr, (float *)(void *)&tev->group,
  783. (float *)(void *)&tev->event);
  784. if (ret != 3) {
  785. semantic_error("Failed to parse event name: %s\n", argv[0]);
  786. ret = -EINVAL;
  787. goto out;
  788. }
  789. pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
  790. tp->retprobe = (pr == 'r');
  791. /* Scan function name and offset */
  792. ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
  793. &tp->offset);
  794. if (ret == 1)
  795. tp->offset = 0;
  796. tev->nargs = argc - 2;
  797. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  798. if (tev->args == NULL) {
  799. ret = -ENOMEM;
  800. goto out;
  801. }
  802. for (i = 0; i < tev->nargs; i++) {
  803. p = strchr(argv[i + 2], '=');
  804. if (p) /* We don't need which register is assigned. */
  805. *p++ = '\0';
  806. else
  807. p = argv[i + 2];
  808. tev->args[i].name = strdup(argv[i + 2]);
  809. /* TODO: parse regs and offset */
  810. tev->args[i].value = strdup(p);
  811. if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
  812. ret = -ENOMEM;
  813. goto out;
  814. }
  815. }
  816. ret = 0;
  817. out:
  818. argv_free(argv);
  819. return ret;
  820. }
  821. /* Compose only probe arg */
  822. int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
  823. {
  824. struct perf_probe_arg_field *field = pa->field;
  825. int ret;
  826. char *tmp = buf;
  827. if (pa->name && pa->var)
  828. ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
  829. else
  830. ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
  831. if (ret <= 0)
  832. goto error;
  833. tmp += ret;
  834. len -= ret;
  835. while (field) {
  836. if (field->name[0] == '[')
  837. ret = e_snprintf(tmp, len, "%s", field->name);
  838. else
  839. ret = e_snprintf(tmp, len, "%s%s",
  840. field->ref ? "->" : ".", field->name);
  841. if (ret <= 0)
  842. goto error;
  843. tmp += ret;
  844. len -= ret;
  845. field = field->next;
  846. }
  847. if (pa->type) {
  848. ret = e_snprintf(tmp, len, ":%s", pa->type);
  849. if (ret <= 0)
  850. goto error;
  851. tmp += ret;
  852. len -= ret;
  853. }
  854. return tmp - buf;
  855. error:
  856. pr_debug("Failed to synthesize perf probe argument: %s",
  857. strerror(-ret));
  858. return ret;
  859. }
  860. /* Compose only probe point (not argument) */
  861. static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
  862. {
  863. char *buf, *tmp;
  864. char offs[32] = "", line[32] = "", file[32] = "";
  865. int ret, len;
  866. buf = zalloc(MAX_CMDLEN);
  867. if (buf == NULL) {
  868. ret = -ENOMEM;
  869. goto error;
  870. }
  871. if (pp->offset) {
  872. ret = e_snprintf(offs, 32, "+%lu", pp->offset);
  873. if (ret <= 0)
  874. goto error;
  875. }
  876. if (pp->line) {
  877. ret = e_snprintf(line, 32, ":%d", pp->line);
  878. if (ret <= 0)
  879. goto error;
  880. }
  881. if (pp->file) {
  882. len = strlen(pp->file) - 31;
  883. if (len < 0)
  884. len = 0;
  885. tmp = strchr(pp->file + len, '/');
  886. if (!tmp)
  887. tmp = pp->file + len;
  888. ret = e_snprintf(file, 32, "@%s", tmp + 1);
  889. if (ret <= 0)
  890. goto error;
  891. }
  892. if (pp->function)
  893. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
  894. offs, pp->retprobe ? "%return" : "", line,
  895. file);
  896. else
  897. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
  898. if (ret <= 0)
  899. goto error;
  900. return buf;
  901. error:
  902. pr_debug("Failed to synthesize perf probe point: %s",
  903. strerror(-ret));
  904. if (buf)
  905. free(buf);
  906. return NULL;
  907. }
  908. #if 0
  909. char *synthesize_perf_probe_command(struct perf_probe_event *pev)
  910. {
  911. char *buf;
  912. int i, len, ret;
  913. buf = synthesize_perf_probe_point(&pev->point);
  914. if (!buf)
  915. return NULL;
  916. len = strlen(buf);
  917. for (i = 0; i < pev->nargs; i++) {
  918. ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
  919. pev->args[i].name);
  920. if (ret <= 0) {
  921. free(buf);
  922. return NULL;
  923. }
  924. len += ret;
  925. }
  926. return buf;
  927. }
  928. #endif
  929. static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
  930. char **buf, size_t *buflen,
  931. int depth)
  932. {
  933. int ret;
  934. if (ref->next) {
  935. depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
  936. buflen, depth + 1);
  937. if (depth < 0)
  938. goto out;
  939. }
  940. ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
  941. if (ret < 0)
  942. depth = ret;
  943. else {
  944. *buf += ret;
  945. *buflen -= ret;
  946. }
  947. out:
  948. return depth;
  949. }
  950. static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
  951. char *buf, size_t buflen)
  952. {
  953. struct probe_trace_arg_ref *ref = arg->ref;
  954. int ret, depth = 0;
  955. char *tmp = buf;
  956. /* Argument name or separator */
  957. if (arg->name)
  958. ret = e_snprintf(buf, buflen, " %s=", arg->name);
  959. else
  960. ret = e_snprintf(buf, buflen, " ");
  961. if (ret < 0)
  962. return ret;
  963. buf += ret;
  964. buflen -= ret;
  965. /* Special case: @XXX */
  966. if (arg->value[0] == '@' && arg->ref)
  967. ref = ref->next;
  968. /* Dereferencing arguments */
  969. if (ref) {
  970. depth = __synthesize_probe_trace_arg_ref(ref, &buf,
  971. &buflen, 1);
  972. if (depth < 0)
  973. return depth;
  974. }
  975. /* Print argument value */
  976. if (arg->value[0] == '@' && arg->ref)
  977. ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
  978. arg->ref->offset);
  979. else
  980. ret = e_snprintf(buf, buflen, "%s", arg->value);
  981. if (ret < 0)
  982. return ret;
  983. buf += ret;
  984. buflen -= ret;
  985. /* Closing */
  986. while (depth--) {
  987. ret = e_snprintf(buf, buflen, ")");
  988. if (ret < 0)
  989. return ret;
  990. buf += ret;
  991. buflen -= ret;
  992. }
  993. /* Print argument type */
  994. if (arg->type) {
  995. ret = e_snprintf(buf, buflen, ":%s", arg->type);
  996. if (ret <= 0)
  997. return ret;
  998. buf += ret;
  999. }
  1000. return buf - tmp;
  1001. }
  1002. char *synthesize_probe_trace_command(struct probe_trace_event *tev)
  1003. {
  1004. struct probe_trace_point *tp = &tev->point;
  1005. char *buf;
  1006. int i, len, ret;
  1007. buf = zalloc(MAX_CMDLEN);
  1008. if (buf == NULL)
  1009. return NULL;
  1010. len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
  1011. tp->retprobe ? 'r' : 'p',
  1012. tev->group, tev->event,
  1013. tp->symbol, tp->offset);
  1014. if (len <= 0)
  1015. goto error;
  1016. for (i = 0; i < tev->nargs; i++) {
  1017. ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
  1018. MAX_CMDLEN - len);
  1019. if (ret <= 0)
  1020. goto error;
  1021. len += ret;
  1022. }
  1023. return buf;
  1024. error:
  1025. free(buf);
  1026. return NULL;
  1027. }
  1028. static int convert_to_perf_probe_event(struct probe_trace_event *tev,
  1029. struct perf_probe_event *pev)
  1030. {
  1031. char buf[64] = "";
  1032. int i, ret;
  1033. /* Convert event/group name */
  1034. pev->event = strdup(tev->event);
  1035. pev->group = strdup(tev->group);
  1036. if (pev->event == NULL || pev->group == NULL)
  1037. return -ENOMEM;
  1038. /* Convert trace_point to probe_point */
  1039. ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
  1040. if (ret < 0)
  1041. return ret;
  1042. /* Convert trace_arg to probe_arg */
  1043. pev->nargs = tev->nargs;
  1044. pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  1045. if (pev->args == NULL)
  1046. return -ENOMEM;
  1047. for (i = 0; i < tev->nargs && ret >= 0; i++) {
  1048. if (tev->args[i].name)
  1049. pev->args[i].name = strdup(tev->args[i].name);
  1050. else {
  1051. ret = synthesize_probe_trace_arg(&tev->args[i],
  1052. buf, 64);
  1053. pev->args[i].name = strdup(buf);
  1054. }
  1055. if (pev->args[i].name == NULL && ret >= 0)
  1056. ret = -ENOMEM;
  1057. }
  1058. if (ret < 0)
  1059. clear_perf_probe_event(pev);
  1060. return ret;
  1061. }
  1062. void clear_perf_probe_event(struct perf_probe_event *pev)
  1063. {
  1064. struct perf_probe_point *pp = &pev->point;
  1065. struct perf_probe_arg_field *field, *next;
  1066. int i;
  1067. if (pev->event)
  1068. free(pev->event);
  1069. if (pev->group)
  1070. free(pev->group);
  1071. if (pp->file)
  1072. free(pp->file);
  1073. if (pp->function)
  1074. free(pp->function);
  1075. if (pp->lazy_line)
  1076. free(pp->lazy_line);
  1077. for (i = 0; i < pev->nargs; i++) {
  1078. if (pev->args[i].name)
  1079. free(pev->args[i].name);
  1080. if (pev->args[i].var)
  1081. free(pev->args[i].var);
  1082. if (pev->args[i].type)
  1083. free(pev->args[i].type);
  1084. field = pev->args[i].field;
  1085. while (field) {
  1086. next = field->next;
  1087. if (field->name)
  1088. free(field->name);
  1089. free(field);
  1090. field = next;
  1091. }
  1092. }
  1093. if (pev->args)
  1094. free(pev->args);
  1095. memset(pev, 0, sizeof(*pev));
  1096. }
  1097. static void clear_probe_trace_event(struct probe_trace_event *tev)
  1098. {
  1099. struct probe_trace_arg_ref *ref, *next;
  1100. int i;
  1101. if (tev->event)
  1102. free(tev->event);
  1103. if (tev->group)
  1104. free(tev->group);
  1105. if (tev->point.symbol)
  1106. free(tev->point.symbol);
  1107. for (i = 0; i < tev->nargs; i++) {
  1108. if (tev->args[i].name)
  1109. free(tev->args[i].name);
  1110. if (tev->args[i].value)
  1111. free(tev->args[i].value);
  1112. if (tev->args[i].type)
  1113. free(tev->args[i].type);
  1114. ref = tev->args[i].ref;
  1115. while (ref) {
  1116. next = ref->next;
  1117. free(ref);
  1118. ref = next;
  1119. }
  1120. }
  1121. if (tev->args)
  1122. free(tev->args);
  1123. memset(tev, 0, sizeof(*tev));
  1124. }
  1125. static int open_kprobe_events(bool readwrite)
  1126. {
  1127. char buf[PATH_MAX];
  1128. const char *__debugfs;
  1129. int ret;
  1130. __debugfs = debugfs_find_mountpoint();
  1131. if (__debugfs == NULL) {
  1132. pr_warning("Debugfs is not mounted.\n");
  1133. return -ENOENT;
  1134. }
  1135. ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
  1136. if (ret >= 0) {
  1137. pr_debug("Opening %s write=%d\n", buf, readwrite);
  1138. if (readwrite && !probe_event_dry_run)
  1139. ret = open(buf, O_RDWR, O_APPEND);
  1140. else
  1141. ret = open(buf, O_RDONLY, 0);
  1142. }
  1143. if (ret < 0) {
  1144. if (errno == ENOENT)
  1145. pr_warning("kprobe_events file does not exist - please"
  1146. " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
  1147. else
  1148. pr_warning("Failed to open kprobe_events file: %s\n",
  1149. strerror(errno));
  1150. }
  1151. return ret;
  1152. }
  1153. /* Get raw string list of current kprobe_events */
  1154. static struct strlist *get_probe_trace_command_rawlist(int fd)
  1155. {
  1156. int ret, idx;
  1157. FILE *fp;
  1158. char buf[MAX_CMDLEN];
  1159. char *p;
  1160. struct strlist *sl;
  1161. sl = strlist__new(true, NULL);
  1162. fp = fdopen(dup(fd), "r");
  1163. while (!feof(fp)) {
  1164. p = fgets(buf, MAX_CMDLEN, fp);
  1165. if (!p)
  1166. break;
  1167. idx = strlen(p) - 1;
  1168. if (p[idx] == '\n')
  1169. p[idx] = '\0';
  1170. ret = strlist__add(sl, buf);
  1171. if (ret < 0) {
  1172. pr_debug("strlist__add failed: %s\n", strerror(-ret));
  1173. strlist__delete(sl);
  1174. return NULL;
  1175. }
  1176. }
  1177. fclose(fp);
  1178. return sl;
  1179. }
  1180. /* Show an event */
  1181. static int show_perf_probe_event(struct perf_probe_event *pev)
  1182. {
  1183. int i, ret;
  1184. char buf[128];
  1185. char *place;
  1186. /* Synthesize only event probe point */
  1187. place = synthesize_perf_probe_point(&pev->point);
  1188. if (!place)
  1189. return -EINVAL;
  1190. ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
  1191. if (ret < 0)
  1192. return ret;
  1193. printf(" %-20s (on %s", buf, place);
  1194. if (pev->nargs > 0) {
  1195. printf(" with");
  1196. for (i = 0; i < pev->nargs; i++) {
  1197. ret = synthesize_perf_probe_arg(&pev->args[i],
  1198. buf, 128);
  1199. if (ret < 0)
  1200. break;
  1201. printf(" %s", buf);
  1202. }
  1203. }
  1204. printf(")\n");
  1205. free(place);
  1206. return ret;
  1207. }
  1208. /* List up current perf-probe events */
  1209. int show_perf_probe_events(void)
  1210. {
  1211. int fd, ret;
  1212. struct probe_trace_event tev;
  1213. struct perf_probe_event pev;
  1214. struct strlist *rawlist;
  1215. struct str_node *ent;
  1216. setup_pager();
  1217. ret = init_vmlinux();
  1218. if (ret < 0)
  1219. return ret;
  1220. memset(&tev, 0, sizeof(tev));
  1221. memset(&pev, 0, sizeof(pev));
  1222. fd = open_kprobe_events(false);
  1223. if (fd < 0)
  1224. return fd;
  1225. rawlist = get_probe_trace_command_rawlist(fd);
  1226. close(fd);
  1227. if (!rawlist)
  1228. return -ENOENT;
  1229. strlist__for_each(ent, rawlist) {
  1230. ret = parse_probe_trace_command(ent->s, &tev);
  1231. if (ret >= 0) {
  1232. ret = convert_to_perf_probe_event(&tev, &pev);
  1233. if (ret >= 0)
  1234. ret = show_perf_probe_event(&pev);
  1235. }
  1236. clear_perf_probe_event(&pev);
  1237. clear_probe_trace_event(&tev);
  1238. if (ret < 0)
  1239. break;
  1240. }
  1241. strlist__delete(rawlist);
  1242. return ret;
  1243. }
  1244. /* Get current perf-probe event names */
  1245. static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
  1246. {
  1247. char buf[128];
  1248. struct strlist *sl, *rawlist;
  1249. struct str_node *ent;
  1250. struct probe_trace_event tev;
  1251. int ret = 0;
  1252. memset(&tev, 0, sizeof(tev));
  1253. rawlist = get_probe_trace_command_rawlist(fd);
  1254. sl = strlist__new(true, NULL);
  1255. strlist__for_each(ent, rawlist) {
  1256. ret = parse_probe_trace_command(ent->s, &tev);
  1257. if (ret < 0)
  1258. break;
  1259. if (include_group) {
  1260. ret = e_snprintf(buf, 128, "%s:%s", tev.group,
  1261. tev.event);
  1262. if (ret >= 0)
  1263. ret = strlist__add(sl, buf);
  1264. } else
  1265. ret = strlist__add(sl, tev.event);
  1266. clear_probe_trace_event(&tev);
  1267. if (ret < 0)
  1268. break;
  1269. }
  1270. strlist__delete(rawlist);
  1271. if (ret < 0) {
  1272. strlist__delete(sl);
  1273. return NULL;
  1274. }
  1275. return sl;
  1276. }
  1277. static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
  1278. {
  1279. int ret = 0;
  1280. char *buf = synthesize_probe_trace_command(tev);
  1281. if (!buf) {
  1282. pr_debug("Failed to synthesize probe trace event.\n");
  1283. return -EINVAL;
  1284. }
  1285. pr_debug("Writing event: %s\n", buf);
  1286. if (!probe_event_dry_run) {
  1287. ret = write(fd, buf, strlen(buf));
  1288. if (ret <= 0)
  1289. pr_warning("Failed to write event: %s\n",
  1290. strerror(errno));
  1291. }
  1292. free(buf);
  1293. return ret;
  1294. }
  1295. static int get_new_event_name(char *buf, size_t len, const char *base,
  1296. struct strlist *namelist, bool allow_suffix)
  1297. {
  1298. int i, ret;
  1299. /* Try no suffix */
  1300. ret = e_snprintf(buf, len, "%s", base);
  1301. if (ret < 0) {
  1302. pr_debug("snprintf() failed: %s\n", strerror(-ret));
  1303. return ret;
  1304. }
  1305. if (!strlist__has_entry(namelist, buf))
  1306. return 0;
  1307. if (!allow_suffix) {
  1308. pr_warning("Error: event \"%s\" already exists. "
  1309. "(Use -f to force duplicates.)\n", base);
  1310. return -EEXIST;
  1311. }
  1312. /* Try to add suffix */
  1313. for (i = 1; i < MAX_EVENT_INDEX; i++) {
  1314. ret = e_snprintf(buf, len, "%s_%d", base, i);
  1315. if (ret < 0) {
  1316. pr_debug("snprintf() failed: %s\n", strerror(-ret));
  1317. return ret;
  1318. }
  1319. if (!strlist__has_entry(namelist, buf))
  1320. break;
  1321. }
  1322. if (i == MAX_EVENT_INDEX) {
  1323. pr_warning("Too many events are on the same function.\n");
  1324. ret = -ERANGE;
  1325. }
  1326. return ret;
  1327. }
  1328. static int __add_probe_trace_events(struct perf_probe_event *pev,
  1329. struct probe_trace_event *tevs,
  1330. int ntevs, bool allow_suffix)
  1331. {
  1332. int i, fd, ret;
  1333. struct probe_trace_event *tev = NULL;
  1334. char buf[64];
  1335. const char *event, *group;
  1336. struct strlist *namelist;
  1337. fd = open_kprobe_events(true);
  1338. if (fd < 0)
  1339. return fd;
  1340. /* Get current event names */
  1341. namelist = get_probe_trace_event_names(fd, false);
  1342. if (!namelist) {
  1343. pr_debug("Failed to get current event list.\n");
  1344. return -EIO;
  1345. }
  1346. ret = 0;
  1347. printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
  1348. for (i = 0; i < ntevs; i++) {
  1349. tev = &tevs[i];
  1350. if (pev->event)
  1351. event = pev->event;
  1352. else
  1353. if (pev->point.function)
  1354. event = pev->point.function;
  1355. else
  1356. event = tev->point.symbol;
  1357. if (pev->group)
  1358. group = pev->group;
  1359. else
  1360. group = PERFPROBE_GROUP;
  1361. /* Get an unused new event name */
  1362. ret = get_new_event_name(buf, 64, event,
  1363. namelist, allow_suffix);
  1364. if (ret < 0)
  1365. break;
  1366. event = buf;
  1367. tev->event = strdup(event);
  1368. tev->group = strdup(group);
  1369. if (tev->event == NULL || tev->group == NULL) {
  1370. ret = -ENOMEM;
  1371. break;
  1372. }
  1373. ret = write_probe_trace_event(fd, tev);
  1374. if (ret < 0)
  1375. break;
  1376. /* Add added event name to namelist */
  1377. strlist__add(namelist, event);
  1378. /* Trick here - save current event/group */
  1379. event = pev->event;
  1380. group = pev->group;
  1381. pev->event = tev->event;
  1382. pev->group = tev->group;
  1383. show_perf_probe_event(pev);
  1384. /* Trick here - restore current event/group */
  1385. pev->event = (char *)event;
  1386. pev->group = (char *)group;
  1387. /*
  1388. * Probes after the first probe which comes from same
  1389. * user input are always allowed to add suffix, because
  1390. * there might be several addresses corresponding to
  1391. * one code line.
  1392. */
  1393. allow_suffix = true;
  1394. }
  1395. if (ret >= 0) {
  1396. /* Show how to use the event. */
  1397. printf("\nYou can now use it on all perf tools, such as:\n\n");
  1398. printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
  1399. tev->event);
  1400. }
  1401. strlist__delete(namelist);
  1402. close(fd);
  1403. return ret;
  1404. }
  1405. static int convert_to_probe_trace_events(struct perf_probe_event *pev,
  1406. struct probe_trace_event **tevs,
  1407. int max_tevs)
  1408. {
  1409. struct symbol *sym;
  1410. int ret = 0, i;
  1411. struct probe_trace_event *tev;
  1412. /* Convert perf_probe_event with debuginfo */
  1413. ret = try_to_find_probe_trace_events(pev, tevs, max_tevs);
  1414. if (ret != 0)
  1415. return ret;
  1416. /* Allocate trace event buffer */
  1417. tev = *tevs = zalloc(sizeof(struct probe_trace_event));
  1418. if (tev == NULL)
  1419. return -ENOMEM;
  1420. /* Copy parameters */
  1421. tev->point.symbol = strdup(pev->point.function);
  1422. if (tev->point.symbol == NULL) {
  1423. ret = -ENOMEM;
  1424. goto error;
  1425. }
  1426. tev->point.offset = pev->point.offset;
  1427. tev->point.retprobe = pev->point.retprobe;
  1428. tev->nargs = pev->nargs;
  1429. if (tev->nargs) {
  1430. tev->args = zalloc(sizeof(struct probe_trace_arg)
  1431. * tev->nargs);
  1432. if (tev->args == NULL) {
  1433. ret = -ENOMEM;
  1434. goto error;
  1435. }
  1436. for (i = 0; i < tev->nargs; i++) {
  1437. if (pev->args[i].name) {
  1438. tev->args[i].name = strdup(pev->args[i].name);
  1439. if (tev->args[i].name == NULL) {
  1440. ret = -ENOMEM;
  1441. goto error;
  1442. }
  1443. }
  1444. tev->args[i].value = strdup(pev->args[i].var);
  1445. if (tev->args[i].value == NULL) {
  1446. ret = -ENOMEM;
  1447. goto error;
  1448. }
  1449. if (pev->args[i].type) {
  1450. tev->args[i].type = strdup(pev->args[i].type);
  1451. if (tev->args[i].type == NULL) {
  1452. ret = -ENOMEM;
  1453. goto error;
  1454. }
  1455. }
  1456. }
  1457. }
  1458. /* Currently just checking function name from symbol map */
  1459. sym = map__find_symbol_by_name(machine.vmlinux_maps[MAP__FUNCTION],
  1460. tev->point.symbol, NULL);
  1461. if (!sym) {
  1462. pr_warning("Kernel symbol \'%s\' not found.\n",
  1463. tev->point.symbol);
  1464. ret = -ENOENT;
  1465. goto error;
  1466. }
  1467. return 1;
  1468. error:
  1469. clear_probe_trace_event(tev);
  1470. free(tev);
  1471. *tevs = NULL;
  1472. return ret;
  1473. }
  1474. struct __event_package {
  1475. struct perf_probe_event *pev;
  1476. struct probe_trace_event *tevs;
  1477. int ntevs;
  1478. };
  1479. int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
  1480. int max_tevs, bool force_add)
  1481. {
  1482. int i, j, ret;
  1483. struct __event_package *pkgs;
  1484. pkgs = zalloc(sizeof(struct __event_package) * npevs);
  1485. if (pkgs == NULL)
  1486. return -ENOMEM;
  1487. /* Init vmlinux path */
  1488. ret = init_vmlinux();
  1489. if (ret < 0) {
  1490. free(pkgs);
  1491. return ret;
  1492. }
  1493. /* Loop 1: convert all events */
  1494. for (i = 0; i < npevs; i++) {
  1495. pkgs[i].pev = &pevs[i];
  1496. /* Convert with or without debuginfo */
  1497. ret = convert_to_probe_trace_events(pkgs[i].pev,
  1498. &pkgs[i].tevs, max_tevs);
  1499. if (ret < 0)
  1500. goto end;
  1501. pkgs[i].ntevs = ret;
  1502. }
  1503. /* Loop 2: add all events */
  1504. for (i = 0; i < npevs && ret >= 0; i++)
  1505. ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
  1506. pkgs[i].ntevs, force_add);
  1507. end:
  1508. /* Loop 3: cleanup and free trace events */
  1509. for (i = 0; i < npevs; i++) {
  1510. for (j = 0; j < pkgs[i].ntevs; j++)
  1511. clear_probe_trace_event(&pkgs[i].tevs[j]);
  1512. free(pkgs[i].tevs);
  1513. }
  1514. free(pkgs);
  1515. return ret;
  1516. }
  1517. static int __del_trace_probe_event(int fd, struct str_node *ent)
  1518. {
  1519. char *p;
  1520. char buf[128];
  1521. int ret;
  1522. /* Convert from perf-probe event to trace-probe event */
  1523. ret = e_snprintf(buf, 128, "-:%s", ent->s);
  1524. if (ret < 0)
  1525. goto error;
  1526. p = strchr(buf + 2, ':');
  1527. if (!p) {
  1528. pr_debug("Internal error: %s should have ':' but not.\n",
  1529. ent->s);
  1530. ret = -ENOTSUP;
  1531. goto error;
  1532. }
  1533. *p = '/';
  1534. pr_debug("Writing event: %s\n", buf);
  1535. ret = write(fd, buf, strlen(buf));
  1536. if (ret < 0)
  1537. goto error;
  1538. printf("Remove event: %s\n", ent->s);
  1539. return 0;
  1540. error:
  1541. pr_warning("Failed to delete event: %s\n", strerror(-ret));
  1542. return ret;
  1543. }
  1544. static int del_trace_probe_event(int fd, const char *group,
  1545. const char *event, struct strlist *namelist)
  1546. {
  1547. char buf[128];
  1548. struct str_node *ent, *n;
  1549. int found = 0, ret = 0;
  1550. ret = e_snprintf(buf, 128, "%s:%s", group, event);
  1551. if (ret < 0) {
  1552. pr_err("Failed to copy event.");
  1553. return ret;
  1554. }
  1555. if (strpbrk(buf, "*?")) { /* Glob-exp */
  1556. strlist__for_each_safe(ent, n, namelist)
  1557. if (strglobmatch(ent->s, buf)) {
  1558. found++;
  1559. ret = __del_trace_probe_event(fd, ent);
  1560. if (ret < 0)
  1561. break;
  1562. strlist__remove(namelist, ent);
  1563. }
  1564. } else {
  1565. ent = strlist__find(namelist, buf);
  1566. if (ent) {
  1567. found++;
  1568. ret = __del_trace_probe_event(fd, ent);
  1569. if (ret >= 0)
  1570. strlist__remove(namelist, ent);
  1571. }
  1572. }
  1573. if (found == 0 && ret >= 0)
  1574. pr_info("Info: Event \"%s\" does not exist.\n", buf);
  1575. return ret;
  1576. }
  1577. int del_perf_probe_events(struct strlist *dellist)
  1578. {
  1579. int fd, ret = 0;
  1580. const char *group, *event;
  1581. char *p, *str;
  1582. struct str_node *ent;
  1583. struct strlist *namelist;
  1584. fd = open_kprobe_events(true);
  1585. if (fd < 0)
  1586. return fd;
  1587. /* Get current event names */
  1588. namelist = get_probe_trace_event_names(fd, true);
  1589. if (namelist == NULL)
  1590. return -EINVAL;
  1591. strlist__for_each(ent, dellist) {
  1592. str = strdup(ent->s);
  1593. if (str == NULL) {
  1594. ret = -ENOMEM;
  1595. break;
  1596. }
  1597. pr_debug("Parsing: %s\n", str);
  1598. p = strchr(str, ':');
  1599. if (p) {
  1600. group = str;
  1601. *p = '\0';
  1602. event = p + 1;
  1603. } else {
  1604. group = "*";
  1605. event = str;
  1606. }
  1607. pr_debug("Group: %s, Event: %s\n", group, event);
  1608. ret = del_trace_probe_event(fd, group, event, namelist);
  1609. free(str);
  1610. if (ret < 0)
  1611. break;
  1612. }
  1613. strlist__delete(namelist);
  1614. close(fd);
  1615. return ret;
  1616. }