probe-event.c 41 KB

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