probe-event.c 41 KB

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