probe-event.c 53 KB

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