probe-event.c 43 KB

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