probe-event.c 58 KB

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