probe-event.c 51 KB

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