probe-event.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755
  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. #else /* !DWARF_SUPPORT */
  334. static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
  335. struct perf_probe_point *pp)
  336. {
  337. pp->function = strdup(tp->symbol);
  338. if (pp->function == NULL)
  339. return -ENOMEM;
  340. pp->offset = tp->offset;
  341. pp->retprobe = tp->retprobe;
  342. return 0;
  343. }
  344. static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
  345. struct probe_trace_event **tevs __unused,
  346. int max_tevs __unused)
  347. {
  348. if (perf_probe_event_need_dwarf(pev)) {
  349. pr_warning("Debuginfo-analysis is not supported.\n");
  350. return -ENOSYS;
  351. }
  352. return 0;
  353. }
  354. int show_line_range(struct line_range *lr __unused)
  355. {
  356. pr_warning("Debuginfo-analysis is not supported.\n");
  357. return -ENOSYS;
  358. }
  359. #endif
  360. int parse_line_range_desc(const char *arg, struct line_range *lr)
  361. {
  362. const char *ptr;
  363. char *tmp;
  364. /*
  365. * <Syntax>
  366. * SRC:SLN[+NUM|-ELN]
  367. * FUNC[:SLN[+NUM|-ELN]]
  368. */
  369. ptr = strchr(arg, ':');
  370. if (ptr) {
  371. lr->start = (int)strtoul(ptr + 1, &tmp, 0);
  372. if (*tmp == '+') {
  373. lr->end = lr->start + (int)strtoul(tmp + 1, &tmp, 0);
  374. lr->end--; /*
  375. * Adjust the number of lines here.
  376. * If the number of lines == 1, the
  377. * the end of line should be equal to
  378. * the start of line.
  379. */
  380. } else if (*tmp == '-')
  381. lr->end = (int)strtoul(tmp + 1, &tmp, 0);
  382. else
  383. lr->end = INT_MAX;
  384. pr_debug("Line range is %d to %d\n", lr->start, lr->end);
  385. if (lr->start > lr->end) {
  386. semantic_error("Start line must be smaller"
  387. " than end line.\n");
  388. return -EINVAL;
  389. }
  390. if (*tmp != '\0') {
  391. semantic_error("Tailing with invalid character '%d'.\n",
  392. *tmp);
  393. return -EINVAL;
  394. }
  395. tmp = strndup(arg, (ptr - arg));
  396. } else {
  397. tmp = strdup(arg);
  398. lr->end = INT_MAX;
  399. }
  400. if (tmp == NULL)
  401. return -ENOMEM;
  402. if (strchr(tmp, '.'))
  403. lr->file = tmp;
  404. else
  405. lr->function = tmp;
  406. return 0;
  407. }
  408. /* Check the name is good for event/group */
  409. static bool check_event_name(const char *name)
  410. {
  411. if (!isalpha(*name) && *name != '_')
  412. return false;
  413. while (*++name != '\0') {
  414. if (!isalpha(*name) && !isdigit(*name) && *name != '_')
  415. return false;
  416. }
  417. return true;
  418. }
  419. /* Parse probepoint definition. */
  420. static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
  421. {
  422. struct perf_probe_point *pp = &pev->point;
  423. char *ptr, *tmp;
  424. char c, nc = 0;
  425. /*
  426. * <Syntax>
  427. * perf probe [EVENT=]SRC[:LN|;PTN]
  428. * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
  429. *
  430. * TODO:Group name support
  431. */
  432. ptr = strpbrk(arg, ";=@+%");
  433. if (ptr && *ptr == '=') { /* Event name */
  434. *ptr = '\0';
  435. tmp = ptr + 1;
  436. if (strchr(arg, ':')) {
  437. semantic_error("Group name is not supported yet.\n");
  438. return -ENOTSUP;
  439. }
  440. if (!check_event_name(arg)) {
  441. semantic_error("%s is bad for event name -it must "
  442. "follow C symbol-naming rule.\n", arg);
  443. return -EINVAL;
  444. }
  445. pev->event = strdup(arg);
  446. if (pev->event == NULL)
  447. return -ENOMEM;
  448. pev->group = NULL;
  449. arg = tmp;
  450. }
  451. ptr = strpbrk(arg, ";:+@%");
  452. if (ptr) {
  453. nc = *ptr;
  454. *ptr++ = '\0';
  455. }
  456. tmp = strdup(arg);
  457. if (tmp == NULL)
  458. return -ENOMEM;
  459. /* Check arg is function or file and copy it */
  460. if (strchr(tmp, '.')) /* File */
  461. pp->file = tmp;
  462. else /* Function */
  463. pp->function = tmp;
  464. /* Parse other options */
  465. while (ptr) {
  466. arg = ptr;
  467. c = nc;
  468. if (c == ';') { /* Lazy pattern must be the last part */
  469. pp->lazy_line = strdup(arg);
  470. if (pp->lazy_line == NULL)
  471. return -ENOMEM;
  472. break;
  473. }
  474. ptr = strpbrk(arg, ";:+@%");
  475. if (ptr) {
  476. nc = *ptr;
  477. *ptr++ = '\0';
  478. }
  479. switch (c) {
  480. case ':': /* Line number */
  481. pp->line = strtoul(arg, &tmp, 0);
  482. if (*tmp != '\0') {
  483. semantic_error("There is non-digit char"
  484. " in line number.\n");
  485. return -EINVAL;
  486. }
  487. break;
  488. case '+': /* Byte offset from a symbol */
  489. pp->offset = strtoul(arg, &tmp, 0);
  490. if (*tmp != '\0') {
  491. semantic_error("There is non-digit character"
  492. " in offset.\n");
  493. return -EINVAL;
  494. }
  495. break;
  496. case '@': /* File name */
  497. if (pp->file) {
  498. semantic_error("SRC@SRC is not allowed.\n");
  499. return -EINVAL;
  500. }
  501. pp->file = strdup(arg);
  502. if (pp->file == NULL)
  503. return -ENOMEM;
  504. break;
  505. case '%': /* Probe places */
  506. if (strcmp(arg, "return") == 0) {
  507. pp->retprobe = 1;
  508. } else { /* Others not supported yet */
  509. semantic_error("%%%s is not supported.\n", arg);
  510. return -ENOTSUP;
  511. }
  512. break;
  513. default: /* Buggy case */
  514. pr_err("This program has a bug at %s:%d.\n",
  515. __FILE__, __LINE__);
  516. return -ENOTSUP;
  517. break;
  518. }
  519. }
  520. /* Exclusion check */
  521. if (pp->lazy_line && pp->line) {
  522. semantic_error("Lazy pattern can't be used with line number.");
  523. return -EINVAL;
  524. }
  525. if (pp->lazy_line && pp->offset) {
  526. semantic_error("Lazy pattern can't be used with offset.");
  527. return -EINVAL;
  528. }
  529. if (pp->line && pp->offset) {
  530. semantic_error("Offset can't be used with line number.");
  531. return -EINVAL;
  532. }
  533. if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
  534. semantic_error("File always requires line number or "
  535. "lazy pattern.");
  536. return -EINVAL;
  537. }
  538. if (pp->offset && !pp->function) {
  539. semantic_error("Offset requires an entry function.");
  540. return -EINVAL;
  541. }
  542. if (pp->retprobe && !pp->function) {
  543. semantic_error("Return probe requires an entry function.");
  544. return -EINVAL;
  545. }
  546. if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
  547. semantic_error("Offset/Line/Lazy pattern can't be used with "
  548. "return probe.");
  549. return -EINVAL;
  550. }
  551. pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
  552. pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
  553. pp->lazy_line);
  554. return 0;
  555. }
  556. /* Parse perf-probe event argument */
  557. static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
  558. {
  559. char *tmp, *goodname;
  560. struct perf_probe_arg_field **fieldp;
  561. pr_debug("parsing arg: %s into ", str);
  562. tmp = strchr(str, '=');
  563. if (tmp) {
  564. arg->name = strndup(str, tmp - str);
  565. if (arg->name == NULL)
  566. return -ENOMEM;
  567. pr_debug("name:%s ", arg->name);
  568. str = tmp + 1;
  569. }
  570. tmp = strchr(str, ':');
  571. if (tmp) { /* Type setting */
  572. *tmp = '\0';
  573. arg->type = strdup(tmp + 1);
  574. if (arg->type == NULL)
  575. return -ENOMEM;
  576. pr_debug("type:%s ", arg->type);
  577. }
  578. tmp = strpbrk(str, "-.[");
  579. if (!is_c_varname(str) || !tmp) {
  580. /* A variable, register, symbol or special value */
  581. arg->var = strdup(str);
  582. if (arg->var == NULL)
  583. return -ENOMEM;
  584. pr_debug("%s\n", arg->var);
  585. return 0;
  586. }
  587. /* Structure fields or array element */
  588. arg->var = strndup(str, tmp - str);
  589. if (arg->var == NULL)
  590. return -ENOMEM;
  591. goodname = arg->var;
  592. pr_debug("%s, ", arg->var);
  593. fieldp = &arg->field;
  594. do {
  595. *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
  596. if (*fieldp == NULL)
  597. return -ENOMEM;
  598. if (*tmp == '[') { /* Array */
  599. str = tmp;
  600. (*fieldp)->index = strtol(str + 1, &tmp, 0);
  601. (*fieldp)->ref = true;
  602. if (*tmp != ']' || tmp == str + 1) {
  603. semantic_error("Array index must be a"
  604. " number.\n");
  605. return -EINVAL;
  606. }
  607. tmp++;
  608. if (*tmp == '\0')
  609. tmp = NULL;
  610. } else { /* Structure */
  611. if (*tmp == '.') {
  612. str = tmp + 1;
  613. (*fieldp)->ref = false;
  614. } else if (tmp[1] == '>') {
  615. str = tmp + 2;
  616. (*fieldp)->ref = true;
  617. } else {
  618. semantic_error("Argument parse error: %s\n",
  619. str);
  620. return -EINVAL;
  621. }
  622. tmp = strpbrk(str, "-.[");
  623. }
  624. if (tmp) {
  625. (*fieldp)->name = strndup(str, tmp - str);
  626. if ((*fieldp)->name == NULL)
  627. return -ENOMEM;
  628. if (*str != '[')
  629. goodname = (*fieldp)->name;
  630. pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
  631. fieldp = &(*fieldp)->next;
  632. }
  633. } while (tmp);
  634. (*fieldp)->name = strdup(str);
  635. if ((*fieldp)->name == NULL)
  636. return -ENOMEM;
  637. if (*str != '[')
  638. goodname = (*fieldp)->name;
  639. pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
  640. /* If no name is specified, set the last field name (not array index)*/
  641. if (!arg->name) {
  642. arg->name = strdup(goodname);
  643. if (arg->name == NULL)
  644. return -ENOMEM;
  645. }
  646. return 0;
  647. }
  648. /* Parse perf-probe event command */
  649. int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
  650. {
  651. char **argv;
  652. int argc, i, ret = 0;
  653. argv = argv_split(cmd, &argc);
  654. if (!argv) {
  655. pr_debug("Failed to split arguments.\n");
  656. return -ENOMEM;
  657. }
  658. if (argc - 1 > MAX_PROBE_ARGS) {
  659. semantic_error("Too many probe arguments (%d).\n", argc - 1);
  660. ret = -ERANGE;
  661. goto out;
  662. }
  663. /* Parse probe point */
  664. ret = parse_perf_probe_point(argv[0], pev);
  665. if (ret < 0)
  666. goto out;
  667. /* Copy arguments and ensure return probe has no C argument */
  668. pev->nargs = argc - 1;
  669. pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  670. if (pev->args == NULL) {
  671. ret = -ENOMEM;
  672. goto out;
  673. }
  674. for (i = 0; i < pev->nargs && ret >= 0; i++) {
  675. ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
  676. if (ret >= 0 &&
  677. is_c_varname(pev->args[i].var) && pev->point.retprobe) {
  678. semantic_error("You can't specify local variable for"
  679. " kretprobe.\n");
  680. ret = -EINVAL;
  681. }
  682. }
  683. out:
  684. argv_free(argv);
  685. return ret;
  686. }
  687. /* Return true if this perf_probe_event requires debuginfo */
  688. bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
  689. {
  690. int i;
  691. if (pev->point.file || pev->point.line || pev->point.lazy_line)
  692. return true;
  693. for (i = 0; i < pev->nargs; i++)
  694. if (is_c_varname(pev->args[i].var))
  695. return true;
  696. return false;
  697. }
  698. /* Parse probe_events event into struct probe_point */
  699. static int parse_probe_trace_command(const char *cmd,
  700. struct probe_trace_event *tev)
  701. {
  702. struct probe_trace_point *tp = &tev->point;
  703. char pr;
  704. char *p;
  705. int ret, i, argc;
  706. char **argv;
  707. pr_debug("Parsing probe_events: %s\n", cmd);
  708. argv = argv_split(cmd, &argc);
  709. if (!argv) {
  710. pr_debug("Failed to split arguments.\n");
  711. return -ENOMEM;
  712. }
  713. if (argc < 2) {
  714. semantic_error("Too few probe arguments.\n");
  715. ret = -ERANGE;
  716. goto out;
  717. }
  718. /* Scan event and group name. */
  719. ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
  720. &pr, (float *)(void *)&tev->group,
  721. (float *)(void *)&tev->event);
  722. if (ret != 3) {
  723. semantic_error("Failed to parse event name: %s\n", argv[0]);
  724. ret = -EINVAL;
  725. goto out;
  726. }
  727. pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
  728. tp->retprobe = (pr == 'r');
  729. /* Scan function name and offset */
  730. ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
  731. &tp->offset);
  732. if (ret == 1)
  733. tp->offset = 0;
  734. tev->nargs = argc - 2;
  735. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  736. if (tev->args == NULL) {
  737. ret = -ENOMEM;
  738. goto out;
  739. }
  740. for (i = 0; i < tev->nargs; i++) {
  741. p = strchr(argv[i + 2], '=');
  742. if (p) /* We don't need which register is assigned. */
  743. *p++ = '\0';
  744. else
  745. p = argv[i + 2];
  746. tev->args[i].name = strdup(argv[i + 2]);
  747. /* TODO: parse regs and offset */
  748. tev->args[i].value = strdup(p);
  749. if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
  750. ret = -ENOMEM;
  751. goto out;
  752. }
  753. }
  754. ret = 0;
  755. out:
  756. argv_free(argv);
  757. return ret;
  758. }
  759. /* Compose only probe arg */
  760. int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
  761. {
  762. struct perf_probe_arg_field *field = pa->field;
  763. int ret;
  764. char *tmp = buf;
  765. if (pa->name && pa->var)
  766. ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
  767. else
  768. ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
  769. if (ret <= 0)
  770. goto error;
  771. tmp += ret;
  772. len -= ret;
  773. while (field) {
  774. if (field->name[0] == '[')
  775. ret = e_snprintf(tmp, len, "%s", field->name);
  776. else
  777. ret = e_snprintf(tmp, len, "%s%s",
  778. field->ref ? "->" : ".", field->name);
  779. if (ret <= 0)
  780. goto error;
  781. tmp += ret;
  782. len -= ret;
  783. field = field->next;
  784. }
  785. if (pa->type) {
  786. ret = e_snprintf(tmp, len, ":%s", pa->type);
  787. if (ret <= 0)
  788. goto error;
  789. tmp += ret;
  790. len -= ret;
  791. }
  792. return tmp - buf;
  793. error:
  794. pr_debug("Failed to synthesize perf probe argument: %s",
  795. strerror(-ret));
  796. return ret;
  797. }
  798. /* Compose only probe point (not argument) */
  799. static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
  800. {
  801. char *buf, *tmp;
  802. char offs[32] = "", line[32] = "", file[32] = "";
  803. int ret, len;
  804. buf = zalloc(MAX_CMDLEN);
  805. if (buf == NULL) {
  806. ret = -ENOMEM;
  807. goto error;
  808. }
  809. if (pp->offset) {
  810. ret = e_snprintf(offs, 32, "+%lu", pp->offset);
  811. if (ret <= 0)
  812. goto error;
  813. }
  814. if (pp->line) {
  815. ret = e_snprintf(line, 32, ":%d", pp->line);
  816. if (ret <= 0)
  817. goto error;
  818. }
  819. if (pp->file) {
  820. len = strlen(pp->file) - 31;
  821. if (len < 0)
  822. len = 0;
  823. tmp = strchr(pp->file + len, '/');
  824. if (!tmp)
  825. tmp = pp->file + len;
  826. ret = e_snprintf(file, 32, "@%s", tmp + 1);
  827. if (ret <= 0)
  828. goto error;
  829. }
  830. if (pp->function)
  831. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
  832. offs, pp->retprobe ? "%return" : "", line,
  833. file);
  834. else
  835. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
  836. if (ret <= 0)
  837. goto error;
  838. return buf;
  839. error:
  840. pr_debug("Failed to synthesize perf probe point: %s",
  841. strerror(-ret));
  842. if (buf)
  843. free(buf);
  844. return NULL;
  845. }
  846. #if 0
  847. char *synthesize_perf_probe_command(struct perf_probe_event *pev)
  848. {
  849. char *buf;
  850. int i, len, ret;
  851. buf = synthesize_perf_probe_point(&pev->point);
  852. if (!buf)
  853. return NULL;
  854. len = strlen(buf);
  855. for (i = 0; i < pev->nargs; i++) {
  856. ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
  857. pev->args[i].name);
  858. if (ret <= 0) {
  859. free(buf);
  860. return NULL;
  861. }
  862. len += ret;
  863. }
  864. return buf;
  865. }
  866. #endif
  867. static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
  868. char **buf, size_t *buflen,
  869. int depth)
  870. {
  871. int ret;
  872. if (ref->next) {
  873. depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
  874. buflen, depth + 1);
  875. if (depth < 0)
  876. goto out;
  877. }
  878. ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
  879. if (ret < 0)
  880. depth = ret;
  881. else {
  882. *buf += ret;
  883. *buflen -= ret;
  884. }
  885. out:
  886. return depth;
  887. }
  888. static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
  889. char *buf, size_t buflen)
  890. {
  891. struct probe_trace_arg_ref *ref = arg->ref;
  892. int ret, depth = 0;
  893. char *tmp = buf;
  894. /* Argument name or separator */
  895. if (arg->name)
  896. ret = e_snprintf(buf, buflen, " %s=", arg->name);
  897. else
  898. ret = e_snprintf(buf, buflen, " ");
  899. if (ret < 0)
  900. return ret;
  901. buf += ret;
  902. buflen -= ret;
  903. /* Special case: @XXX */
  904. if (arg->value[0] == '@' && arg->ref)
  905. ref = ref->next;
  906. /* Dereferencing arguments */
  907. if (ref) {
  908. depth = __synthesize_probe_trace_arg_ref(ref, &buf,
  909. &buflen, 1);
  910. if (depth < 0)
  911. return depth;
  912. }
  913. /* Print argument value */
  914. if (arg->value[0] == '@' && arg->ref)
  915. ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
  916. arg->ref->offset);
  917. else
  918. ret = e_snprintf(buf, buflen, "%s", arg->value);
  919. if (ret < 0)
  920. return ret;
  921. buf += ret;
  922. buflen -= ret;
  923. /* Closing */
  924. while (depth--) {
  925. ret = e_snprintf(buf, buflen, ")");
  926. if (ret < 0)
  927. return ret;
  928. buf += ret;
  929. buflen -= ret;
  930. }
  931. /* Print argument type */
  932. if (arg->type) {
  933. ret = e_snprintf(buf, buflen, ":%s", arg->type);
  934. if (ret <= 0)
  935. return ret;
  936. buf += ret;
  937. }
  938. return buf - tmp;
  939. }
  940. char *synthesize_probe_trace_command(struct probe_trace_event *tev)
  941. {
  942. struct probe_trace_point *tp = &tev->point;
  943. char *buf;
  944. int i, len, ret;
  945. buf = zalloc(MAX_CMDLEN);
  946. if (buf == NULL)
  947. return NULL;
  948. len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
  949. tp->retprobe ? 'r' : 'p',
  950. tev->group, tev->event,
  951. tp->symbol, tp->offset);
  952. if (len <= 0)
  953. goto error;
  954. for (i = 0; i < tev->nargs; i++) {
  955. ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
  956. MAX_CMDLEN - len);
  957. if (ret <= 0)
  958. goto error;
  959. len += ret;
  960. }
  961. return buf;
  962. error:
  963. free(buf);
  964. return NULL;
  965. }
  966. static int convert_to_perf_probe_event(struct probe_trace_event *tev,
  967. struct perf_probe_event *pev)
  968. {
  969. char buf[64] = "";
  970. int i, ret;
  971. /* Convert event/group name */
  972. pev->event = strdup(tev->event);
  973. pev->group = strdup(tev->group);
  974. if (pev->event == NULL || pev->group == NULL)
  975. return -ENOMEM;
  976. /* Convert trace_point to probe_point */
  977. ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
  978. if (ret < 0)
  979. return ret;
  980. /* Convert trace_arg to probe_arg */
  981. pev->nargs = tev->nargs;
  982. pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  983. if (pev->args == NULL)
  984. return -ENOMEM;
  985. for (i = 0; i < tev->nargs && ret >= 0; i++) {
  986. if (tev->args[i].name)
  987. pev->args[i].name = strdup(tev->args[i].name);
  988. else {
  989. ret = synthesize_probe_trace_arg(&tev->args[i],
  990. buf, 64);
  991. pev->args[i].name = strdup(buf);
  992. }
  993. if (pev->args[i].name == NULL && ret >= 0)
  994. ret = -ENOMEM;
  995. }
  996. if (ret < 0)
  997. clear_perf_probe_event(pev);
  998. return ret;
  999. }
  1000. void clear_perf_probe_event(struct perf_probe_event *pev)
  1001. {
  1002. struct perf_probe_point *pp = &pev->point;
  1003. struct perf_probe_arg_field *field, *next;
  1004. int i;
  1005. if (pev->event)
  1006. free(pev->event);
  1007. if (pev->group)
  1008. free(pev->group);
  1009. if (pp->file)
  1010. free(pp->file);
  1011. if (pp->function)
  1012. free(pp->function);
  1013. if (pp->lazy_line)
  1014. free(pp->lazy_line);
  1015. for (i = 0; i < pev->nargs; i++) {
  1016. if (pev->args[i].name)
  1017. free(pev->args[i].name);
  1018. if (pev->args[i].var)
  1019. free(pev->args[i].var);
  1020. if (pev->args[i].type)
  1021. free(pev->args[i].type);
  1022. field = pev->args[i].field;
  1023. while (field) {
  1024. next = field->next;
  1025. if (field->name)
  1026. free(field->name);
  1027. free(field);
  1028. field = next;
  1029. }
  1030. }
  1031. if (pev->args)
  1032. free(pev->args);
  1033. memset(pev, 0, sizeof(*pev));
  1034. }
  1035. static void clear_probe_trace_event(struct probe_trace_event *tev)
  1036. {
  1037. struct probe_trace_arg_ref *ref, *next;
  1038. int i;
  1039. if (tev->event)
  1040. free(tev->event);
  1041. if (tev->group)
  1042. free(tev->group);
  1043. if (tev->point.symbol)
  1044. free(tev->point.symbol);
  1045. for (i = 0; i < tev->nargs; i++) {
  1046. if (tev->args[i].name)
  1047. free(tev->args[i].name);
  1048. if (tev->args[i].value)
  1049. free(tev->args[i].value);
  1050. if (tev->args[i].type)
  1051. free(tev->args[i].type);
  1052. ref = tev->args[i].ref;
  1053. while (ref) {
  1054. next = ref->next;
  1055. free(ref);
  1056. ref = next;
  1057. }
  1058. }
  1059. if (tev->args)
  1060. free(tev->args);
  1061. memset(tev, 0, sizeof(*tev));
  1062. }
  1063. static int open_kprobe_events(bool readwrite)
  1064. {
  1065. char buf[PATH_MAX];
  1066. const char *__debugfs;
  1067. int ret;
  1068. __debugfs = debugfs_find_mountpoint();
  1069. if (__debugfs == NULL) {
  1070. pr_warning("Debugfs is not mounted.\n");
  1071. return -ENOENT;
  1072. }
  1073. ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
  1074. if (ret >= 0) {
  1075. pr_debug("Opening %s write=%d\n", buf, readwrite);
  1076. if (readwrite && !probe_event_dry_run)
  1077. ret = open(buf, O_RDWR, O_APPEND);
  1078. else
  1079. ret = open(buf, O_RDONLY, 0);
  1080. }
  1081. if (ret < 0) {
  1082. if (errno == ENOENT)
  1083. pr_warning("kprobe_events file does not exist - please"
  1084. " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
  1085. else
  1086. pr_warning("Failed to open kprobe_events file: %s\n",
  1087. strerror(errno));
  1088. }
  1089. return ret;
  1090. }
  1091. /* Get raw string list of current kprobe_events */
  1092. static struct strlist *get_probe_trace_command_rawlist(int fd)
  1093. {
  1094. int ret, idx;
  1095. FILE *fp;
  1096. char buf[MAX_CMDLEN];
  1097. char *p;
  1098. struct strlist *sl;
  1099. sl = strlist__new(true, NULL);
  1100. fp = fdopen(dup(fd), "r");
  1101. while (!feof(fp)) {
  1102. p = fgets(buf, MAX_CMDLEN, fp);
  1103. if (!p)
  1104. break;
  1105. idx = strlen(p) - 1;
  1106. if (p[idx] == '\n')
  1107. p[idx] = '\0';
  1108. ret = strlist__add(sl, buf);
  1109. if (ret < 0) {
  1110. pr_debug("strlist__add failed: %s\n", strerror(-ret));
  1111. strlist__delete(sl);
  1112. return NULL;
  1113. }
  1114. }
  1115. fclose(fp);
  1116. return sl;
  1117. }
  1118. /* Show an event */
  1119. static int show_perf_probe_event(struct perf_probe_event *pev)
  1120. {
  1121. int i, ret;
  1122. char buf[128];
  1123. char *place;
  1124. /* Synthesize only event probe point */
  1125. place = synthesize_perf_probe_point(&pev->point);
  1126. if (!place)
  1127. return -EINVAL;
  1128. ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
  1129. if (ret < 0)
  1130. return ret;
  1131. printf(" %-20s (on %s", buf, place);
  1132. if (pev->nargs > 0) {
  1133. printf(" with");
  1134. for (i = 0; i < pev->nargs; i++) {
  1135. ret = synthesize_perf_probe_arg(&pev->args[i],
  1136. buf, 128);
  1137. if (ret < 0)
  1138. break;
  1139. printf(" %s", buf);
  1140. }
  1141. }
  1142. printf(")\n");
  1143. free(place);
  1144. return ret;
  1145. }
  1146. /* List up current perf-probe events */
  1147. int show_perf_probe_events(void)
  1148. {
  1149. int fd, ret;
  1150. struct probe_trace_event tev;
  1151. struct perf_probe_event pev;
  1152. struct strlist *rawlist;
  1153. struct str_node *ent;
  1154. setup_pager();
  1155. ret = init_vmlinux();
  1156. if (ret < 0)
  1157. return ret;
  1158. memset(&tev, 0, sizeof(tev));
  1159. memset(&pev, 0, sizeof(pev));
  1160. fd = open_kprobe_events(false);
  1161. if (fd < 0)
  1162. return fd;
  1163. rawlist = get_probe_trace_command_rawlist(fd);
  1164. close(fd);
  1165. if (!rawlist)
  1166. return -ENOENT;
  1167. strlist__for_each(ent, rawlist) {
  1168. ret = parse_probe_trace_command(ent->s, &tev);
  1169. if (ret >= 0) {
  1170. ret = convert_to_perf_probe_event(&tev, &pev);
  1171. if (ret >= 0)
  1172. ret = show_perf_probe_event(&pev);
  1173. }
  1174. clear_perf_probe_event(&pev);
  1175. clear_probe_trace_event(&tev);
  1176. if (ret < 0)
  1177. break;
  1178. }
  1179. strlist__delete(rawlist);
  1180. return ret;
  1181. }
  1182. /* Get current perf-probe event names */
  1183. static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
  1184. {
  1185. char buf[128];
  1186. struct strlist *sl, *rawlist;
  1187. struct str_node *ent;
  1188. struct probe_trace_event tev;
  1189. int ret = 0;
  1190. memset(&tev, 0, sizeof(tev));
  1191. rawlist = get_probe_trace_command_rawlist(fd);
  1192. sl = strlist__new(true, NULL);
  1193. strlist__for_each(ent, rawlist) {
  1194. ret = parse_probe_trace_command(ent->s, &tev);
  1195. if (ret < 0)
  1196. break;
  1197. if (include_group) {
  1198. ret = e_snprintf(buf, 128, "%s:%s", tev.group,
  1199. tev.event);
  1200. if (ret >= 0)
  1201. ret = strlist__add(sl, buf);
  1202. } else
  1203. ret = strlist__add(sl, tev.event);
  1204. clear_probe_trace_event(&tev);
  1205. if (ret < 0)
  1206. break;
  1207. }
  1208. strlist__delete(rawlist);
  1209. if (ret < 0) {
  1210. strlist__delete(sl);
  1211. return NULL;
  1212. }
  1213. return sl;
  1214. }
  1215. static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
  1216. {
  1217. int ret = 0;
  1218. char *buf = synthesize_probe_trace_command(tev);
  1219. if (!buf) {
  1220. pr_debug("Failed to synthesize probe trace event.\n");
  1221. return -EINVAL;
  1222. }
  1223. pr_debug("Writing event: %s\n", buf);
  1224. if (!probe_event_dry_run) {
  1225. ret = write(fd, buf, strlen(buf));
  1226. if (ret <= 0)
  1227. pr_warning("Failed to write event: %s\n",
  1228. strerror(errno));
  1229. }
  1230. free(buf);
  1231. return ret;
  1232. }
  1233. static int get_new_event_name(char *buf, size_t len, const char *base,
  1234. struct strlist *namelist, bool allow_suffix)
  1235. {
  1236. int i, ret;
  1237. /* Try no suffix */
  1238. ret = e_snprintf(buf, len, "%s", base);
  1239. if (ret < 0) {
  1240. pr_debug("snprintf() failed: %s\n", strerror(-ret));
  1241. return ret;
  1242. }
  1243. if (!strlist__has_entry(namelist, buf))
  1244. return 0;
  1245. if (!allow_suffix) {
  1246. pr_warning("Error: event \"%s\" already exists. "
  1247. "(Use -f to force duplicates.)\n", base);
  1248. return -EEXIST;
  1249. }
  1250. /* Try to add suffix */
  1251. for (i = 1; i < MAX_EVENT_INDEX; i++) {
  1252. ret = e_snprintf(buf, len, "%s_%d", base, i);
  1253. if (ret < 0) {
  1254. pr_debug("snprintf() failed: %s\n", strerror(-ret));
  1255. return ret;
  1256. }
  1257. if (!strlist__has_entry(namelist, buf))
  1258. break;
  1259. }
  1260. if (i == MAX_EVENT_INDEX) {
  1261. pr_warning("Too many events are on the same function.\n");
  1262. ret = -ERANGE;
  1263. }
  1264. return ret;
  1265. }
  1266. static int __add_probe_trace_events(struct perf_probe_event *pev,
  1267. struct probe_trace_event *tevs,
  1268. int ntevs, bool allow_suffix)
  1269. {
  1270. int i, fd, ret;
  1271. struct probe_trace_event *tev = NULL;
  1272. char buf[64];
  1273. const char *event, *group;
  1274. struct strlist *namelist;
  1275. fd = open_kprobe_events(true);
  1276. if (fd < 0)
  1277. return fd;
  1278. /* Get current event names */
  1279. namelist = get_probe_trace_event_names(fd, false);
  1280. if (!namelist) {
  1281. pr_debug("Failed to get current event list.\n");
  1282. return -EIO;
  1283. }
  1284. ret = 0;
  1285. printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
  1286. for (i = 0; i < ntevs; i++) {
  1287. tev = &tevs[i];
  1288. if (pev->event)
  1289. event = pev->event;
  1290. else
  1291. if (pev->point.function)
  1292. event = pev->point.function;
  1293. else
  1294. event = tev->point.symbol;
  1295. if (pev->group)
  1296. group = pev->group;
  1297. else
  1298. group = PERFPROBE_GROUP;
  1299. /* Get an unused new event name */
  1300. ret = get_new_event_name(buf, 64, event,
  1301. namelist, allow_suffix);
  1302. if (ret < 0)
  1303. break;
  1304. event = buf;
  1305. tev->event = strdup(event);
  1306. tev->group = strdup(group);
  1307. if (tev->event == NULL || tev->group == NULL) {
  1308. ret = -ENOMEM;
  1309. break;
  1310. }
  1311. ret = write_probe_trace_event(fd, tev);
  1312. if (ret < 0)
  1313. break;
  1314. /* Add added event name to namelist */
  1315. strlist__add(namelist, event);
  1316. /* Trick here - save current event/group */
  1317. event = pev->event;
  1318. group = pev->group;
  1319. pev->event = tev->event;
  1320. pev->group = tev->group;
  1321. show_perf_probe_event(pev);
  1322. /* Trick here - restore current event/group */
  1323. pev->event = (char *)event;
  1324. pev->group = (char *)group;
  1325. /*
  1326. * Probes after the first probe which comes from same
  1327. * user input are always allowed to add suffix, because
  1328. * there might be several addresses corresponding to
  1329. * one code line.
  1330. */
  1331. allow_suffix = true;
  1332. }
  1333. if (ret >= 0) {
  1334. /* Show how to use the event. */
  1335. printf("\nYou can now use it on all perf tools, such as:\n\n");
  1336. printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
  1337. tev->event);
  1338. }
  1339. strlist__delete(namelist);
  1340. close(fd);
  1341. return ret;
  1342. }
  1343. static int convert_to_probe_trace_events(struct perf_probe_event *pev,
  1344. struct probe_trace_event **tevs,
  1345. int max_tevs)
  1346. {
  1347. struct symbol *sym;
  1348. int ret = 0, i;
  1349. struct probe_trace_event *tev;
  1350. /* Convert perf_probe_event with debuginfo */
  1351. ret = try_to_find_probe_trace_events(pev, tevs, max_tevs);
  1352. if (ret != 0)
  1353. return ret;
  1354. /* Allocate trace event buffer */
  1355. tev = *tevs = zalloc(sizeof(struct probe_trace_event));
  1356. if (tev == NULL)
  1357. return -ENOMEM;
  1358. /* Copy parameters */
  1359. tev->point.symbol = strdup(pev->point.function);
  1360. if (tev->point.symbol == NULL) {
  1361. ret = -ENOMEM;
  1362. goto error;
  1363. }
  1364. tev->point.offset = pev->point.offset;
  1365. tev->point.retprobe = pev->point.retprobe;
  1366. tev->nargs = pev->nargs;
  1367. if (tev->nargs) {
  1368. tev->args = zalloc(sizeof(struct probe_trace_arg)
  1369. * tev->nargs);
  1370. if (tev->args == NULL) {
  1371. ret = -ENOMEM;
  1372. goto error;
  1373. }
  1374. for (i = 0; i < tev->nargs; i++) {
  1375. if (pev->args[i].name) {
  1376. tev->args[i].name = strdup(pev->args[i].name);
  1377. if (tev->args[i].name == NULL) {
  1378. ret = -ENOMEM;
  1379. goto error;
  1380. }
  1381. }
  1382. tev->args[i].value = strdup(pev->args[i].var);
  1383. if (tev->args[i].value == NULL) {
  1384. ret = -ENOMEM;
  1385. goto error;
  1386. }
  1387. if (pev->args[i].type) {
  1388. tev->args[i].type = strdup(pev->args[i].type);
  1389. if (tev->args[i].type == NULL) {
  1390. ret = -ENOMEM;
  1391. goto error;
  1392. }
  1393. }
  1394. }
  1395. }
  1396. /* Currently just checking function name from symbol map */
  1397. sym = map__find_symbol_by_name(machine.vmlinux_maps[MAP__FUNCTION],
  1398. tev->point.symbol, NULL);
  1399. if (!sym) {
  1400. pr_warning("Kernel symbol \'%s\' not found.\n",
  1401. tev->point.symbol);
  1402. ret = -ENOENT;
  1403. goto error;
  1404. }
  1405. return 1;
  1406. error:
  1407. clear_probe_trace_event(tev);
  1408. free(tev);
  1409. *tevs = NULL;
  1410. return ret;
  1411. }
  1412. struct __event_package {
  1413. struct perf_probe_event *pev;
  1414. struct probe_trace_event *tevs;
  1415. int ntevs;
  1416. };
  1417. int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
  1418. bool force_add, int max_tevs)
  1419. {
  1420. int i, j, ret;
  1421. struct __event_package *pkgs;
  1422. pkgs = zalloc(sizeof(struct __event_package) * npevs);
  1423. if (pkgs == NULL)
  1424. return -ENOMEM;
  1425. /* Init vmlinux path */
  1426. ret = init_vmlinux();
  1427. if (ret < 0) {
  1428. free(pkgs);
  1429. return ret;
  1430. }
  1431. /* Loop 1: convert all events */
  1432. for (i = 0; i < npevs; i++) {
  1433. pkgs[i].pev = &pevs[i];
  1434. /* Convert with or without debuginfo */
  1435. ret = convert_to_probe_trace_events(pkgs[i].pev,
  1436. &pkgs[i].tevs, max_tevs);
  1437. if (ret < 0)
  1438. goto end;
  1439. pkgs[i].ntevs = ret;
  1440. }
  1441. /* Loop 2: add all events */
  1442. for (i = 0; i < npevs && ret >= 0; i++)
  1443. ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
  1444. pkgs[i].ntevs, force_add);
  1445. end:
  1446. /* Loop 3: cleanup and free trace events */
  1447. for (i = 0; i < npevs; i++) {
  1448. for (j = 0; j < pkgs[i].ntevs; j++)
  1449. clear_probe_trace_event(&pkgs[i].tevs[j]);
  1450. free(pkgs[i].tevs);
  1451. }
  1452. free(pkgs);
  1453. return ret;
  1454. }
  1455. static int __del_trace_probe_event(int fd, struct str_node *ent)
  1456. {
  1457. char *p;
  1458. char buf[128];
  1459. int ret;
  1460. /* Convert from perf-probe event to trace-probe event */
  1461. ret = e_snprintf(buf, 128, "-:%s", ent->s);
  1462. if (ret < 0)
  1463. goto error;
  1464. p = strchr(buf + 2, ':');
  1465. if (!p) {
  1466. pr_debug("Internal error: %s should have ':' but not.\n",
  1467. ent->s);
  1468. ret = -ENOTSUP;
  1469. goto error;
  1470. }
  1471. *p = '/';
  1472. pr_debug("Writing event: %s\n", buf);
  1473. ret = write(fd, buf, strlen(buf));
  1474. if (ret < 0)
  1475. goto error;
  1476. printf("Remove event: %s\n", ent->s);
  1477. return 0;
  1478. error:
  1479. pr_warning("Failed to delete event: %s\n", strerror(-ret));
  1480. return ret;
  1481. }
  1482. static int del_trace_probe_event(int fd, const char *group,
  1483. const char *event, struct strlist *namelist)
  1484. {
  1485. char buf[128];
  1486. struct str_node *ent, *n;
  1487. int found = 0, ret = 0;
  1488. ret = e_snprintf(buf, 128, "%s:%s", group, event);
  1489. if (ret < 0) {
  1490. pr_err("Failed to copy event.");
  1491. return ret;
  1492. }
  1493. if (strpbrk(buf, "*?")) { /* Glob-exp */
  1494. strlist__for_each_safe(ent, n, namelist)
  1495. if (strglobmatch(ent->s, buf)) {
  1496. found++;
  1497. ret = __del_trace_probe_event(fd, ent);
  1498. if (ret < 0)
  1499. break;
  1500. strlist__remove(namelist, ent);
  1501. }
  1502. } else {
  1503. ent = strlist__find(namelist, buf);
  1504. if (ent) {
  1505. found++;
  1506. ret = __del_trace_probe_event(fd, ent);
  1507. if (ret >= 0)
  1508. strlist__remove(namelist, ent);
  1509. }
  1510. }
  1511. if (found == 0 && ret >= 0)
  1512. pr_info("Info: Event \"%s\" does not exist.\n", buf);
  1513. return ret;
  1514. }
  1515. int del_perf_probe_events(struct strlist *dellist)
  1516. {
  1517. int fd, ret = 0;
  1518. const char *group, *event;
  1519. char *p, *str;
  1520. struct str_node *ent;
  1521. struct strlist *namelist;
  1522. fd = open_kprobe_events(true);
  1523. if (fd < 0)
  1524. return fd;
  1525. /* Get current event names */
  1526. namelist = get_probe_trace_event_names(fd, true);
  1527. if (namelist == NULL)
  1528. return -EINVAL;
  1529. strlist__for_each(ent, dellist) {
  1530. str = strdup(ent->s);
  1531. if (str == NULL) {
  1532. ret = -ENOMEM;
  1533. break;
  1534. }
  1535. pr_debug("Parsing: %s\n", str);
  1536. p = strchr(str, ':');
  1537. if (p) {
  1538. group = str;
  1539. *p = '\0';
  1540. event = p + 1;
  1541. } else {
  1542. group = "*";
  1543. event = str;
  1544. }
  1545. pr_debug("Group: %s, Event: %s\n", group, event);
  1546. ret = del_trace_probe_event(fd, group, event, namelist);
  1547. free(str);
  1548. if (ret < 0)
  1549. break;
  1550. }
  1551. strlist__delete(namelist);
  1552. close(fd);
  1553. return ret;
  1554. }