probe-event.c 58 KB

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