probe-event.c 56 KB

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