probe-event.c 65 KB

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