probe-event.c 38 KB

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