probe-event.c 65 KB

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