probe-event.c 41 KB

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