probe-event.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497
  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/fs.h>
  42. #include "trace-event.h" /* For __maybe_unused */
  43. #include "probe-event.h"
  44. #include "probe-finder.h"
  45. #include "probe-file.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. int e_snprintf(char *str, size_t size, const char *format, ...)
  53. {
  54. int ret;
  55. va_list ap;
  56. va_start(ap, format);
  57. ret = vsnprintf(str, size, format, ap);
  58. va_end(ap);
  59. if (ret >= (int)size)
  60. ret = -E2BIG;
  61. return ret;
  62. }
  63. static struct machine *host_machine;
  64. /* Initialize symbol maps and path of vmlinux/modules */
  65. int init_probe_symbol_maps(bool user_only)
  66. {
  67. int ret;
  68. symbol_conf.sort_by_name = true;
  69. symbol_conf.allow_aliases = true;
  70. ret = symbol__init(NULL);
  71. if (ret < 0) {
  72. pr_debug("Failed to init symbol map.\n");
  73. goto out;
  74. }
  75. if (host_machine || user_only) /* already initialized */
  76. return 0;
  77. if (symbol_conf.vmlinux_name)
  78. pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
  79. host_machine = machine__new_host();
  80. if (!host_machine) {
  81. pr_debug("machine__new_host() failed.\n");
  82. symbol__exit();
  83. ret = -1;
  84. }
  85. out:
  86. if (ret < 0)
  87. pr_warning("Failed to init vmlinux path.\n");
  88. return ret;
  89. }
  90. void exit_probe_symbol_maps(void)
  91. {
  92. machine__delete(host_machine);
  93. host_machine = NULL;
  94. symbol__exit();
  95. }
  96. static struct symbol *__find_kernel_function_by_name(const char *name,
  97. struct map **mapp)
  98. {
  99. return machine__find_kernel_function_by_name(host_machine, name, mapp);
  100. }
  101. static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
  102. {
  103. return machine__find_kernel_function(host_machine, addr, mapp);
  104. }
  105. static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
  106. {
  107. /* kmap->ref_reloc_sym should be set if host_machine is initialized */
  108. struct kmap *kmap;
  109. struct map *map = machine__kernel_map(host_machine);
  110. if (map__load(map) < 0)
  111. return NULL;
  112. kmap = map__kmap(map);
  113. if (!kmap)
  114. return NULL;
  115. return kmap->ref_reloc_sym;
  116. }
  117. static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
  118. bool reloc, bool reladdr)
  119. {
  120. struct ref_reloc_sym *reloc_sym;
  121. struct symbol *sym;
  122. struct map *map;
  123. /* ref_reloc_sym is just a label. Need a special fix*/
  124. reloc_sym = kernel_get_ref_reloc_sym();
  125. if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
  126. *addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
  127. else {
  128. sym = __find_kernel_function_by_name(name, &map);
  129. if (!sym)
  130. return -ENOENT;
  131. *addr = map->unmap_ip(map, sym->start) -
  132. ((reloc) ? 0 : map->reloc) -
  133. ((reladdr) ? map->start : 0);
  134. }
  135. return 0;
  136. }
  137. static struct map *kernel_get_module_map(const char *module)
  138. {
  139. struct map_groups *grp = &host_machine->kmaps;
  140. struct maps *maps = &grp->maps[MAP__FUNCTION];
  141. struct map *pos;
  142. /* A file path -- this is an offline module */
  143. if (module && strchr(module, '/'))
  144. return dso__new_map(module);
  145. if (!module)
  146. module = "kernel";
  147. for (pos = maps__first(maps); pos; pos = map__next(pos)) {
  148. /* short_name is "[module]" */
  149. if (strncmp(pos->dso->short_name + 1, module,
  150. pos->dso->short_name_len - 2) == 0 &&
  151. module[pos->dso->short_name_len - 2] == '\0') {
  152. map__get(pos);
  153. return pos;
  154. }
  155. }
  156. return NULL;
  157. }
  158. struct map *get_target_map(const char *target, bool user)
  159. {
  160. /* Init maps of given executable or kernel */
  161. if (user)
  162. return dso__new_map(target);
  163. else
  164. return kernel_get_module_map(target);
  165. }
  166. static int convert_exec_to_group(const char *exec, char **result)
  167. {
  168. char *ptr1, *ptr2, *exec_copy;
  169. char buf[64];
  170. int ret;
  171. exec_copy = strdup(exec);
  172. if (!exec_copy)
  173. return -ENOMEM;
  174. ptr1 = basename(exec_copy);
  175. if (!ptr1) {
  176. ret = -EINVAL;
  177. goto out;
  178. }
  179. for (ptr2 = ptr1; *ptr2 != '\0'; ptr2++) {
  180. if (!isalnum(*ptr2) && *ptr2 != '_') {
  181. *ptr2 = '\0';
  182. break;
  183. }
  184. }
  185. ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
  186. if (ret < 0)
  187. goto out;
  188. *result = strdup(buf);
  189. ret = *result ? 0 : -ENOMEM;
  190. out:
  191. free(exec_copy);
  192. return ret;
  193. }
  194. static void clear_perf_probe_point(struct perf_probe_point *pp)
  195. {
  196. free(pp->file);
  197. free(pp->function);
  198. free(pp->lazy_line);
  199. }
  200. static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
  201. {
  202. int i;
  203. for (i = 0; i < ntevs; i++)
  204. clear_probe_trace_event(tevs + i);
  205. }
  206. static bool kprobe_blacklist__listed(unsigned long address);
  207. static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
  208. {
  209. u64 etext_addr = 0;
  210. int ret;
  211. /* Get the address of _etext for checking non-probable text symbol */
  212. ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
  213. false, false);
  214. if (ret == 0 && etext_addr < address)
  215. pr_warning("%s is out of .text, skip it.\n", symbol);
  216. else if (kprobe_blacklist__listed(address))
  217. pr_warning("%s is blacklisted function, skip it.\n", symbol);
  218. else
  219. return false;
  220. return true;
  221. }
  222. /*
  223. * @module can be module name of module file path. In case of path,
  224. * inspect elf and find out what is actual module name.
  225. * Caller has to free mod_name after using it.
  226. */
  227. static char *find_module_name(const char *module)
  228. {
  229. int fd;
  230. Elf *elf;
  231. GElf_Ehdr ehdr;
  232. GElf_Shdr shdr;
  233. Elf_Data *data;
  234. Elf_Scn *sec;
  235. char *mod_name = NULL;
  236. int name_offset;
  237. fd = open(module, O_RDONLY);
  238. if (fd < 0)
  239. return NULL;
  240. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  241. if (elf == NULL)
  242. goto elf_err;
  243. if (gelf_getehdr(elf, &ehdr) == NULL)
  244. goto ret_err;
  245. sec = elf_section_by_name(elf, &ehdr, &shdr,
  246. ".gnu.linkonce.this_module", NULL);
  247. if (!sec)
  248. goto ret_err;
  249. data = elf_getdata(sec, NULL);
  250. if (!data || !data->d_buf)
  251. goto ret_err;
  252. /*
  253. * NOTE:
  254. * '.gnu.linkonce.this_module' section of kernel module elf directly
  255. * maps to 'struct module' from linux/module.h. This section contains
  256. * actual module name which will be used by kernel after loading it.
  257. * But, we cannot use 'struct module' here since linux/module.h is not
  258. * exposed to user-space. Offset of 'name' has remained same from long
  259. * time, so hardcoding it here.
  260. */
  261. if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
  262. name_offset = 12;
  263. else /* expect ELFCLASS64 by default */
  264. name_offset = 24;
  265. mod_name = strdup((char *)data->d_buf + name_offset);
  266. ret_err:
  267. elf_end(elf);
  268. elf_err:
  269. close(fd);
  270. return mod_name;
  271. }
  272. #ifdef HAVE_DWARF_SUPPORT
  273. static int kernel_get_module_dso(const char *module, struct dso **pdso)
  274. {
  275. struct dso *dso;
  276. struct map *map;
  277. const char *vmlinux_name;
  278. int ret = 0;
  279. if (module) {
  280. char module_name[128];
  281. snprintf(module_name, sizeof(module_name), "[%s]", module);
  282. map = map_groups__find_by_name(&host_machine->kmaps, MAP__FUNCTION, module_name);
  283. if (map) {
  284. dso = map->dso;
  285. goto found;
  286. }
  287. pr_debug("Failed to find module %s.\n", module);
  288. return -ENOENT;
  289. }
  290. map = machine__kernel_map(host_machine);
  291. dso = map->dso;
  292. vmlinux_name = symbol_conf.vmlinux_name;
  293. dso->load_errno = 0;
  294. if (vmlinux_name)
  295. ret = dso__load_vmlinux(dso, map, vmlinux_name, false);
  296. else
  297. ret = dso__load_vmlinux_path(dso, map);
  298. found:
  299. *pdso = dso;
  300. return ret;
  301. }
  302. /*
  303. * Some binaries like glibc have special symbols which are on the symbol
  304. * table, but not in the debuginfo. If we can find the address of the
  305. * symbol from map, we can translate the address back to the probe point.
  306. */
  307. static int find_alternative_probe_point(struct debuginfo *dinfo,
  308. struct perf_probe_point *pp,
  309. struct perf_probe_point *result,
  310. const char *target, bool uprobes)
  311. {
  312. struct map *map = NULL;
  313. struct symbol *sym;
  314. u64 address = 0;
  315. int ret = -ENOENT;
  316. /* This can work only for function-name based one */
  317. if (!pp->function || pp->file)
  318. return -ENOTSUP;
  319. map = get_target_map(target, uprobes);
  320. if (!map)
  321. return -EINVAL;
  322. /* Find the address of given function */
  323. map__for_each_symbol_by_name(map, pp->function, sym) {
  324. if (uprobes)
  325. address = sym->start;
  326. else
  327. address = map->unmap_ip(map, sym->start) - map->reloc;
  328. break;
  329. }
  330. if (!address) {
  331. ret = -ENOENT;
  332. goto out;
  333. }
  334. pr_debug("Symbol %s address found : %" PRIx64 "\n",
  335. pp->function, address);
  336. ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
  337. result);
  338. if (ret <= 0)
  339. ret = (!ret) ? -ENOENT : ret;
  340. else {
  341. result->offset += pp->offset;
  342. result->line += pp->line;
  343. result->retprobe = pp->retprobe;
  344. ret = 0;
  345. }
  346. out:
  347. map__put(map);
  348. return ret;
  349. }
  350. static int get_alternative_probe_event(struct debuginfo *dinfo,
  351. struct perf_probe_event *pev,
  352. struct perf_probe_point *tmp)
  353. {
  354. int ret;
  355. memcpy(tmp, &pev->point, sizeof(*tmp));
  356. memset(&pev->point, 0, sizeof(pev->point));
  357. ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
  358. pev->target, pev->uprobes);
  359. if (ret < 0)
  360. memcpy(&pev->point, tmp, sizeof(*tmp));
  361. return ret;
  362. }
  363. static int get_alternative_line_range(struct debuginfo *dinfo,
  364. struct line_range *lr,
  365. const char *target, bool user)
  366. {
  367. struct perf_probe_point pp = { .function = lr->function,
  368. .file = lr->file,
  369. .line = lr->start };
  370. struct perf_probe_point result;
  371. int ret, len = 0;
  372. memset(&result, 0, sizeof(result));
  373. if (lr->end != INT_MAX)
  374. len = lr->end - lr->start;
  375. ret = find_alternative_probe_point(dinfo, &pp, &result,
  376. target, user);
  377. if (!ret) {
  378. lr->function = result.function;
  379. lr->file = result.file;
  380. lr->start = result.line;
  381. if (lr->end != INT_MAX)
  382. lr->end = lr->start + len;
  383. clear_perf_probe_point(&pp);
  384. }
  385. return ret;
  386. }
  387. /* Open new debuginfo of given module */
  388. static struct debuginfo *open_debuginfo(const char *module, bool silent)
  389. {
  390. const char *path = module;
  391. char reason[STRERR_BUFSIZE];
  392. struct debuginfo *ret = NULL;
  393. struct dso *dso = NULL;
  394. int err;
  395. if (!module || !strchr(module, '/')) {
  396. err = kernel_get_module_dso(module, &dso);
  397. if (err < 0) {
  398. if (!dso || dso->load_errno == 0) {
  399. if (!str_error_r(-err, reason, STRERR_BUFSIZE))
  400. strcpy(reason, "(unknown)");
  401. } else
  402. dso__strerror_load(dso, reason, STRERR_BUFSIZE);
  403. if (!silent)
  404. pr_err("Failed to find the path for %s: %s\n",
  405. module ?: "kernel", reason);
  406. return NULL;
  407. }
  408. path = dso->long_name;
  409. }
  410. ret = debuginfo__new(path);
  411. if (!ret && !silent) {
  412. pr_warning("The %s file has no debug information.\n", path);
  413. if (!module || !strtailcmp(path, ".ko"))
  414. pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
  415. else
  416. pr_warning("Rebuild with -g, ");
  417. pr_warning("or install an appropriate debuginfo package.\n");
  418. }
  419. return ret;
  420. }
  421. /* For caching the last debuginfo */
  422. static struct debuginfo *debuginfo_cache;
  423. static char *debuginfo_cache_path;
  424. static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
  425. {
  426. const char *path = module;
  427. /* If the module is NULL, it should be the kernel. */
  428. if (!module)
  429. path = "kernel";
  430. if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
  431. goto out;
  432. /* Copy module path */
  433. free(debuginfo_cache_path);
  434. debuginfo_cache_path = strdup(path);
  435. if (!debuginfo_cache_path) {
  436. debuginfo__delete(debuginfo_cache);
  437. debuginfo_cache = NULL;
  438. goto out;
  439. }
  440. debuginfo_cache = open_debuginfo(module, silent);
  441. if (!debuginfo_cache)
  442. zfree(&debuginfo_cache_path);
  443. out:
  444. return debuginfo_cache;
  445. }
  446. static void debuginfo_cache__exit(void)
  447. {
  448. debuginfo__delete(debuginfo_cache);
  449. debuginfo_cache = NULL;
  450. zfree(&debuginfo_cache_path);
  451. }
  452. static int get_text_start_address(const char *exec, unsigned long *address)
  453. {
  454. Elf *elf;
  455. GElf_Ehdr ehdr;
  456. GElf_Shdr shdr;
  457. int fd, ret = -ENOENT;
  458. fd = open(exec, O_RDONLY);
  459. if (fd < 0)
  460. return -errno;
  461. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  462. if (elf == NULL) {
  463. ret = -EINVAL;
  464. goto out_close;
  465. }
  466. if (gelf_getehdr(elf, &ehdr) == NULL)
  467. goto out;
  468. if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
  469. goto out;
  470. *address = shdr.sh_addr - shdr.sh_offset;
  471. ret = 0;
  472. out:
  473. elf_end(elf);
  474. out_close:
  475. close(fd);
  476. return ret;
  477. }
  478. /*
  479. * Convert trace point to probe point with debuginfo
  480. */
  481. static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
  482. struct perf_probe_point *pp,
  483. bool is_kprobe)
  484. {
  485. struct debuginfo *dinfo = NULL;
  486. unsigned long stext = 0;
  487. u64 addr = tp->address;
  488. int ret = -ENOENT;
  489. /* convert the address to dwarf address */
  490. if (!is_kprobe) {
  491. if (!addr) {
  492. ret = -EINVAL;
  493. goto error;
  494. }
  495. ret = get_text_start_address(tp->module, &stext);
  496. if (ret < 0)
  497. goto error;
  498. addr += stext;
  499. } else if (tp->symbol) {
  500. /* If the module is given, this returns relative address */
  501. ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
  502. false, !!tp->module);
  503. if (ret != 0)
  504. goto error;
  505. addr += tp->offset;
  506. }
  507. pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
  508. tp->module ? : "kernel");
  509. dinfo = debuginfo_cache__open(tp->module, verbose == 0);
  510. if (dinfo)
  511. ret = debuginfo__find_probe_point(dinfo,
  512. (unsigned long)addr, pp);
  513. else
  514. ret = -ENOENT;
  515. if (ret > 0) {
  516. pp->retprobe = tp->retprobe;
  517. return 0;
  518. }
  519. error:
  520. pr_debug("Failed to find corresponding probes from debuginfo.\n");
  521. return ret ? : -ENOENT;
  522. }
  523. /* Adjust symbol name and address */
  524. static int post_process_probe_trace_point(struct probe_trace_point *tp,
  525. struct map *map, unsigned long offs)
  526. {
  527. struct symbol *sym;
  528. u64 addr = tp->address + tp->offset - offs;
  529. sym = map__find_symbol(map, addr);
  530. if (!sym)
  531. return -ENOENT;
  532. if (strcmp(sym->name, tp->symbol)) {
  533. /* If we have no realname, use symbol for it */
  534. if (!tp->realname)
  535. tp->realname = tp->symbol;
  536. else
  537. free(tp->symbol);
  538. tp->symbol = strdup(sym->name);
  539. if (!tp->symbol)
  540. return -ENOMEM;
  541. }
  542. tp->offset = addr - sym->start;
  543. tp->address -= offs;
  544. return 0;
  545. }
  546. /*
  547. * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions
  548. * and generate new symbols with suffixes such as .constprop.N or .isra.N
  549. * etc. Since those symbols are not recorded in DWARF, we have to find
  550. * correct generated symbols from offline ELF binary.
  551. * For online kernel or uprobes we don't need this because those are
  552. * rebased on _text, or already a section relative address.
  553. */
  554. static int
  555. post_process_offline_probe_trace_events(struct probe_trace_event *tevs,
  556. int ntevs, const char *pathname)
  557. {
  558. struct map *map;
  559. unsigned long stext = 0;
  560. int i, ret = 0;
  561. /* Prepare a map for offline binary */
  562. map = dso__new_map(pathname);
  563. if (!map || get_text_start_address(pathname, &stext) < 0) {
  564. pr_warning("Failed to get ELF symbols for %s\n", pathname);
  565. return -EINVAL;
  566. }
  567. for (i = 0; i < ntevs; i++) {
  568. ret = post_process_probe_trace_point(&tevs[i].point,
  569. map, stext);
  570. if (ret < 0)
  571. break;
  572. }
  573. map__put(map);
  574. return ret;
  575. }
  576. static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
  577. int ntevs, const char *exec)
  578. {
  579. int i, ret = 0;
  580. unsigned long stext = 0;
  581. if (!exec)
  582. return 0;
  583. ret = get_text_start_address(exec, &stext);
  584. if (ret < 0)
  585. return ret;
  586. for (i = 0; i < ntevs && ret >= 0; i++) {
  587. /* point.address is the addres of point.symbol + point.offset */
  588. tevs[i].point.address -= stext;
  589. tevs[i].point.module = strdup(exec);
  590. if (!tevs[i].point.module) {
  591. ret = -ENOMEM;
  592. break;
  593. }
  594. tevs[i].uprobes = true;
  595. }
  596. return ret;
  597. }
  598. static int
  599. post_process_module_probe_trace_events(struct probe_trace_event *tevs,
  600. int ntevs, const char *module,
  601. struct debuginfo *dinfo)
  602. {
  603. Dwarf_Addr text_offs = 0;
  604. int i, ret = 0;
  605. char *mod_name = NULL;
  606. struct map *map;
  607. if (!module)
  608. return 0;
  609. map = get_target_map(module, false);
  610. if (!map || debuginfo__get_text_offset(dinfo, &text_offs, true) < 0) {
  611. pr_warning("Failed to get ELF symbols for %s\n", module);
  612. return -EINVAL;
  613. }
  614. mod_name = find_module_name(module);
  615. for (i = 0; i < ntevs; i++) {
  616. ret = post_process_probe_trace_point(&tevs[i].point,
  617. map, (unsigned long)text_offs);
  618. if (ret < 0)
  619. break;
  620. tevs[i].point.module =
  621. strdup(mod_name ? mod_name : module);
  622. if (!tevs[i].point.module) {
  623. ret = -ENOMEM;
  624. break;
  625. }
  626. }
  627. free(mod_name);
  628. map__put(map);
  629. return ret;
  630. }
  631. static int
  632. post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,
  633. int ntevs)
  634. {
  635. struct ref_reloc_sym *reloc_sym;
  636. char *tmp;
  637. int i, skipped = 0;
  638. /* Skip post process if the target is an offline kernel */
  639. if (symbol_conf.ignore_vmlinux_buildid)
  640. return post_process_offline_probe_trace_events(tevs, ntevs,
  641. symbol_conf.vmlinux_name);
  642. reloc_sym = kernel_get_ref_reloc_sym();
  643. if (!reloc_sym) {
  644. pr_warning("Relocated base symbol is not found!\n");
  645. return -EINVAL;
  646. }
  647. for (i = 0; i < ntevs; i++) {
  648. if (!tevs[i].point.address || tevs[i].point.retprobe)
  649. continue;
  650. /* If we found a wrong one, mark it by NULL symbol */
  651. if (kprobe_warn_out_range(tevs[i].point.symbol,
  652. tevs[i].point.address)) {
  653. tmp = NULL;
  654. skipped++;
  655. } else {
  656. tmp = strdup(reloc_sym->name);
  657. if (!tmp)
  658. return -ENOMEM;
  659. }
  660. /* If we have no realname, use symbol for it */
  661. if (!tevs[i].point.realname)
  662. tevs[i].point.realname = tevs[i].point.symbol;
  663. else
  664. free(tevs[i].point.symbol);
  665. tevs[i].point.symbol = tmp;
  666. tevs[i].point.offset = tevs[i].point.address -
  667. reloc_sym->unrelocated_addr;
  668. }
  669. return skipped;
  670. }
  671. void __weak
  672. arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused,
  673. int ntevs __maybe_unused)
  674. {
  675. }
  676. /* Post processing the probe events */
  677. static int post_process_probe_trace_events(struct perf_probe_event *pev,
  678. struct probe_trace_event *tevs,
  679. int ntevs, const char *module,
  680. bool uprobe, struct debuginfo *dinfo)
  681. {
  682. int ret;
  683. if (uprobe)
  684. ret = add_exec_to_probe_trace_events(tevs, ntevs, module);
  685. else if (module)
  686. /* Currently ref_reloc_sym based probe is not for drivers */
  687. ret = post_process_module_probe_trace_events(tevs, ntevs,
  688. module, dinfo);
  689. else
  690. ret = post_process_kernel_probe_trace_events(tevs, ntevs);
  691. if (ret >= 0)
  692. arch__post_process_probe_trace_events(pev, ntevs);
  693. return ret;
  694. }
  695. /* Try to find perf_probe_event with debuginfo */
  696. static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
  697. struct probe_trace_event **tevs)
  698. {
  699. bool need_dwarf = perf_probe_event_need_dwarf(pev);
  700. struct perf_probe_point tmp;
  701. struct debuginfo *dinfo;
  702. int ntevs, ret = 0;
  703. dinfo = open_debuginfo(pev->target, !need_dwarf);
  704. if (!dinfo) {
  705. if (need_dwarf)
  706. return -ENOENT;
  707. pr_debug("Could not open debuginfo. Try to use symbols.\n");
  708. return 0;
  709. }
  710. pr_debug("Try to find probe point from debuginfo.\n");
  711. /* Searching trace events corresponding to a probe event */
  712. ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
  713. if (ntevs == 0) { /* Not found, retry with an alternative */
  714. ret = get_alternative_probe_event(dinfo, pev, &tmp);
  715. if (!ret) {
  716. ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
  717. /*
  718. * Write back to the original probe_event for
  719. * setting appropriate (user given) event name
  720. */
  721. clear_perf_probe_point(&pev->point);
  722. memcpy(&pev->point, &tmp, sizeof(tmp));
  723. }
  724. }
  725. if (ntevs > 0) { /* Succeeded to find trace events */
  726. pr_debug("Found %d probe_trace_events.\n", ntevs);
  727. ret = post_process_probe_trace_events(pev, *tevs, ntevs,
  728. pev->target, pev->uprobes, dinfo);
  729. if (ret < 0 || ret == ntevs) {
  730. pr_debug("Post processing failed or all events are skipped. (%d)\n", ret);
  731. clear_probe_trace_events(*tevs, ntevs);
  732. zfree(tevs);
  733. ntevs = 0;
  734. }
  735. }
  736. debuginfo__delete(dinfo);
  737. if (ntevs == 0) { /* No error but failed to find probe point. */
  738. pr_warning("Probe point '%s' not found.\n",
  739. synthesize_perf_probe_point(&pev->point));
  740. return -ENOENT;
  741. } else if (ntevs < 0) {
  742. /* Error path : ntevs < 0 */
  743. pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
  744. if (ntevs == -EBADF)
  745. pr_warning("Warning: No dwarf info found in the vmlinux - "
  746. "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
  747. if (!need_dwarf) {
  748. pr_debug("Trying to use symbols.\n");
  749. return 0;
  750. }
  751. }
  752. return ntevs;
  753. }
  754. #define LINEBUF_SIZE 256
  755. #define NR_ADDITIONAL_LINES 2
  756. static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
  757. {
  758. char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
  759. const char *color = show_num ? "" : PERF_COLOR_BLUE;
  760. const char *prefix = NULL;
  761. do {
  762. if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
  763. goto error;
  764. if (skip)
  765. continue;
  766. if (!prefix) {
  767. prefix = show_num ? "%7d " : " ";
  768. color_fprintf(stdout, color, prefix, l);
  769. }
  770. color_fprintf(stdout, color, "%s", buf);
  771. } while (strchr(buf, '\n') == NULL);
  772. return 1;
  773. error:
  774. if (ferror(fp)) {
  775. pr_warning("File read error: %s\n",
  776. str_error_r(errno, sbuf, sizeof(sbuf)));
  777. return -1;
  778. }
  779. return 0;
  780. }
  781. static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
  782. {
  783. int rv = __show_one_line(fp, l, skip, show_num);
  784. if (rv == 0) {
  785. pr_warning("Source file is shorter than expected.\n");
  786. rv = -1;
  787. }
  788. return rv;
  789. }
  790. #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
  791. #define show_one_line(f,l) _show_one_line(f,l,false,false)
  792. #define skip_one_line(f,l) _show_one_line(f,l,true,false)
  793. #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
  794. /*
  795. * Show line-range always requires debuginfo to find source file and
  796. * line number.
  797. */
  798. static int __show_line_range(struct line_range *lr, const char *module,
  799. bool user)
  800. {
  801. int l = 1;
  802. struct int_node *ln;
  803. struct debuginfo *dinfo;
  804. FILE *fp;
  805. int ret;
  806. char *tmp;
  807. char sbuf[STRERR_BUFSIZE];
  808. /* Search a line range */
  809. dinfo = open_debuginfo(module, false);
  810. if (!dinfo)
  811. return -ENOENT;
  812. ret = debuginfo__find_line_range(dinfo, lr);
  813. if (!ret) { /* Not found, retry with an alternative */
  814. ret = get_alternative_line_range(dinfo, lr, module, user);
  815. if (!ret)
  816. ret = debuginfo__find_line_range(dinfo, lr);
  817. }
  818. debuginfo__delete(dinfo);
  819. if (ret == 0 || ret == -ENOENT) {
  820. pr_warning("Specified source line is not found.\n");
  821. return -ENOENT;
  822. } else if (ret < 0) {
  823. pr_warning("Debuginfo analysis failed.\n");
  824. return ret;
  825. }
  826. /* Convert source file path */
  827. tmp = lr->path;
  828. ret = get_real_path(tmp, lr->comp_dir, &lr->path);
  829. /* Free old path when new path is assigned */
  830. if (tmp != lr->path)
  831. free(tmp);
  832. if (ret < 0) {
  833. pr_warning("Failed to find source file path.\n");
  834. return ret;
  835. }
  836. setup_pager();
  837. if (lr->function)
  838. fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
  839. lr->start - lr->offset);
  840. else
  841. fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
  842. fp = fopen(lr->path, "r");
  843. if (fp == NULL) {
  844. pr_warning("Failed to open %s: %s\n", lr->path,
  845. str_error_r(errno, sbuf, sizeof(sbuf)));
  846. return -errno;
  847. }
  848. /* Skip to starting line number */
  849. while (l < lr->start) {
  850. ret = skip_one_line(fp, l++);
  851. if (ret < 0)
  852. goto end;
  853. }
  854. intlist__for_each_entry(ln, lr->line_list) {
  855. for (; ln->i > l; l++) {
  856. ret = show_one_line(fp, l - lr->offset);
  857. if (ret < 0)
  858. goto end;
  859. }
  860. ret = show_one_line_with_num(fp, l++ - lr->offset);
  861. if (ret < 0)
  862. goto end;
  863. }
  864. if (lr->end == INT_MAX)
  865. lr->end = l + NR_ADDITIONAL_LINES;
  866. while (l <= lr->end) {
  867. ret = show_one_line_or_eof(fp, l++ - lr->offset);
  868. if (ret <= 0)
  869. break;
  870. }
  871. end:
  872. fclose(fp);
  873. return ret;
  874. }
  875. int show_line_range(struct line_range *lr, const char *module, bool user)
  876. {
  877. int ret;
  878. ret = init_probe_symbol_maps(user);
  879. if (ret < 0)
  880. return ret;
  881. ret = __show_line_range(lr, module, user);
  882. exit_probe_symbol_maps();
  883. return ret;
  884. }
  885. static int show_available_vars_at(struct debuginfo *dinfo,
  886. struct perf_probe_event *pev,
  887. struct strfilter *_filter)
  888. {
  889. char *buf;
  890. int ret, i, nvars;
  891. struct str_node *node;
  892. struct variable_list *vls = NULL, *vl;
  893. struct perf_probe_point tmp;
  894. const char *var;
  895. buf = synthesize_perf_probe_point(&pev->point);
  896. if (!buf)
  897. return -EINVAL;
  898. pr_debug("Searching variables at %s\n", buf);
  899. ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
  900. if (!ret) { /* Not found, retry with an alternative */
  901. ret = get_alternative_probe_event(dinfo, pev, &tmp);
  902. if (!ret) {
  903. ret = debuginfo__find_available_vars_at(dinfo, pev,
  904. &vls);
  905. /* Release the old probe_point */
  906. clear_perf_probe_point(&tmp);
  907. }
  908. }
  909. if (ret <= 0) {
  910. if (ret == 0 || ret == -ENOENT) {
  911. pr_err("Failed to find the address of %s\n", buf);
  912. ret = -ENOENT;
  913. } else
  914. pr_warning("Debuginfo analysis failed.\n");
  915. goto end;
  916. }
  917. /* Some variables are found */
  918. fprintf(stdout, "Available variables at %s\n", buf);
  919. for (i = 0; i < ret; i++) {
  920. vl = &vls[i];
  921. /*
  922. * A probe point might be converted to
  923. * several trace points.
  924. */
  925. fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
  926. vl->point.offset);
  927. zfree(&vl->point.symbol);
  928. nvars = 0;
  929. if (vl->vars) {
  930. strlist__for_each_entry(node, vl->vars) {
  931. var = strchr(node->s, '\t') + 1;
  932. if (strfilter__compare(_filter, var)) {
  933. fprintf(stdout, "\t\t%s\n", node->s);
  934. nvars++;
  935. }
  936. }
  937. strlist__delete(vl->vars);
  938. }
  939. if (nvars == 0)
  940. fprintf(stdout, "\t\t(No matched variables)\n");
  941. }
  942. free(vls);
  943. end:
  944. free(buf);
  945. return ret;
  946. }
  947. /* Show available variables on given probe point */
  948. int show_available_vars(struct perf_probe_event *pevs, int npevs,
  949. struct strfilter *_filter)
  950. {
  951. int i, ret = 0;
  952. struct debuginfo *dinfo;
  953. ret = init_probe_symbol_maps(pevs->uprobes);
  954. if (ret < 0)
  955. return ret;
  956. dinfo = open_debuginfo(pevs->target, false);
  957. if (!dinfo) {
  958. ret = -ENOENT;
  959. goto out;
  960. }
  961. setup_pager();
  962. for (i = 0; i < npevs && ret >= 0; i++)
  963. ret = show_available_vars_at(dinfo, &pevs[i], _filter);
  964. debuginfo__delete(dinfo);
  965. out:
  966. exit_probe_symbol_maps();
  967. return ret;
  968. }
  969. #else /* !HAVE_DWARF_SUPPORT */
  970. static void debuginfo_cache__exit(void)
  971. {
  972. }
  973. static int
  974. find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
  975. struct perf_probe_point *pp __maybe_unused,
  976. bool is_kprobe __maybe_unused)
  977. {
  978. return -ENOSYS;
  979. }
  980. static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
  981. struct probe_trace_event **tevs __maybe_unused)
  982. {
  983. if (perf_probe_event_need_dwarf(pev)) {
  984. pr_warning("Debuginfo-analysis is not supported.\n");
  985. return -ENOSYS;
  986. }
  987. return 0;
  988. }
  989. int show_line_range(struct line_range *lr __maybe_unused,
  990. const char *module __maybe_unused,
  991. bool user __maybe_unused)
  992. {
  993. pr_warning("Debuginfo-analysis is not supported.\n");
  994. return -ENOSYS;
  995. }
  996. int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
  997. int npevs __maybe_unused,
  998. struct strfilter *filter __maybe_unused)
  999. {
  1000. pr_warning("Debuginfo-analysis is not supported.\n");
  1001. return -ENOSYS;
  1002. }
  1003. #endif
  1004. void line_range__clear(struct line_range *lr)
  1005. {
  1006. free(lr->function);
  1007. free(lr->file);
  1008. free(lr->path);
  1009. free(lr->comp_dir);
  1010. intlist__delete(lr->line_list);
  1011. memset(lr, 0, sizeof(*lr));
  1012. }
  1013. int line_range__init(struct line_range *lr)
  1014. {
  1015. memset(lr, 0, sizeof(*lr));
  1016. lr->line_list = intlist__new(NULL);
  1017. if (!lr->line_list)
  1018. return -ENOMEM;
  1019. else
  1020. return 0;
  1021. }
  1022. static int parse_line_num(char **ptr, int *val, const char *what)
  1023. {
  1024. const char *start = *ptr;
  1025. errno = 0;
  1026. *val = strtol(*ptr, ptr, 0);
  1027. if (errno || *ptr == start) {
  1028. semantic_error("'%s' is not a valid number.\n", what);
  1029. return -EINVAL;
  1030. }
  1031. return 0;
  1032. }
  1033. /* Check the name is good for event, group or function */
  1034. static bool is_c_func_name(const char *name)
  1035. {
  1036. if (!isalpha(*name) && *name != '_')
  1037. return false;
  1038. while (*++name != '\0') {
  1039. if (!isalpha(*name) && !isdigit(*name) && *name != '_')
  1040. return false;
  1041. }
  1042. return true;
  1043. }
  1044. /*
  1045. * Stuff 'lr' according to the line range described by 'arg'.
  1046. * The line range syntax is described by:
  1047. *
  1048. * SRC[:SLN[+NUM|-ELN]]
  1049. * FNC[@SRC][:SLN[+NUM|-ELN]]
  1050. */
  1051. int parse_line_range_desc(const char *arg, struct line_range *lr)
  1052. {
  1053. char *range, *file, *name = strdup(arg);
  1054. int err;
  1055. if (!name)
  1056. return -ENOMEM;
  1057. lr->start = 0;
  1058. lr->end = INT_MAX;
  1059. range = strchr(name, ':');
  1060. if (range) {
  1061. *range++ = '\0';
  1062. err = parse_line_num(&range, &lr->start, "start line");
  1063. if (err)
  1064. goto err;
  1065. if (*range == '+' || *range == '-') {
  1066. const char c = *range++;
  1067. err = parse_line_num(&range, &lr->end, "end line");
  1068. if (err)
  1069. goto err;
  1070. if (c == '+') {
  1071. lr->end += lr->start;
  1072. /*
  1073. * Adjust the number of lines here.
  1074. * If the number of lines == 1, the
  1075. * the end of line should be equal to
  1076. * the start of line.
  1077. */
  1078. lr->end--;
  1079. }
  1080. }
  1081. pr_debug("Line range is %d to %d\n", lr->start, lr->end);
  1082. err = -EINVAL;
  1083. if (lr->start > lr->end) {
  1084. semantic_error("Start line must be smaller"
  1085. " than end line.\n");
  1086. goto err;
  1087. }
  1088. if (*range != '\0') {
  1089. semantic_error("Tailing with invalid str '%s'.\n", range);
  1090. goto err;
  1091. }
  1092. }
  1093. file = strchr(name, '@');
  1094. if (file) {
  1095. *file = '\0';
  1096. lr->file = strdup(++file);
  1097. if (lr->file == NULL) {
  1098. err = -ENOMEM;
  1099. goto err;
  1100. }
  1101. lr->function = name;
  1102. } else if (strchr(name, '/') || strchr(name, '.'))
  1103. lr->file = name;
  1104. else if (is_c_func_name(name))/* We reuse it for checking funcname */
  1105. lr->function = name;
  1106. else { /* Invalid name */
  1107. semantic_error("'%s' is not a valid function name.\n", name);
  1108. err = -EINVAL;
  1109. goto err;
  1110. }
  1111. return 0;
  1112. err:
  1113. free(name);
  1114. return err;
  1115. }
  1116. static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
  1117. {
  1118. char *ptr;
  1119. ptr = strchr(*arg, ':');
  1120. if (ptr) {
  1121. *ptr = '\0';
  1122. if (!pev->sdt && !is_c_func_name(*arg))
  1123. goto ng_name;
  1124. pev->group = strdup(*arg);
  1125. if (!pev->group)
  1126. return -ENOMEM;
  1127. *arg = ptr + 1;
  1128. } else
  1129. pev->group = NULL;
  1130. if (!pev->sdt && !is_c_func_name(*arg)) {
  1131. ng_name:
  1132. semantic_error("%s is bad for event name -it must "
  1133. "follow C symbol-naming rule.\n", *arg);
  1134. return -EINVAL;
  1135. }
  1136. pev->event = strdup(*arg);
  1137. if (pev->event == NULL)
  1138. return -ENOMEM;
  1139. return 0;
  1140. }
  1141. /* Parse probepoint definition. */
  1142. static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
  1143. {
  1144. struct perf_probe_point *pp = &pev->point;
  1145. char *ptr, *tmp;
  1146. char c, nc = 0;
  1147. bool file_spec = false;
  1148. int ret;
  1149. /*
  1150. * <Syntax>
  1151. * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
  1152. * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
  1153. * perf probe %[GRP:]SDT_EVENT
  1154. */
  1155. if (!arg)
  1156. return -EINVAL;
  1157. /*
  1158. * If the probe point starts with '%',
  1159. * or starts with "sdt_" and has a ':' but no '=',
  1160. * then it should be a SDT/cached probe point.
  1161. */
  1162. if (arg[0] == '%' ||
  1163. (!strncmp(arg, "sdt_", 4) &&
  1164. !!strchr(arg, ':') && !strchr(arg, '='))) {
  1165. pev->sdt = true;
  1166. if (arg[0] == '%')
  1167. arg++;
  1168. }
  1169. ptr = strpbrk(arg, ";=@+%");
  1170. if (pev->sdt) {
  1171. if (ptr) {
  1172. if (*ptr != '@') {
  1173. semantic_error("%s must be an SDT name.\n",
  1174. arg);
  1175. return -EINVAL;
  1176. }
  1177. /* This must be a target file name or build id */
  1178. tmp = build_id_cache__complement(ptr + 1);
  1179. if (tmp) {
  1180. pev->target = build_id_cache__origname(tmp);
  1181. free(tmp);
  1182. } else
  1183. pev->target = strdup(ptr + 1);
  1184. if (!pev->target)
  1185. return -ENOMEM;
  1186. *ptr = '\0';
  1187. }
  1188. ret = parse_perf_probe_event_name(&arg, pev);
  1189. if (ret == 0) {
  1190. if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
  1191. ret = -errno;
  1192. }
  1193. return ret;
  1194. }
  1195. if (ptr && *ptr == '=') { /* Event name */
  1196. *ptr = '\0';
  1197. tmp = ptr + 1;
  1198. ret = parse_perf_probe_event_name(&arg, pev);
  1199. if (ret < 0)
  1200. return ret;
  1201. arg = tmp;
  1202. }
  1203. /*
  1204. * Check arg is function or file name and copy it.
  1205. *
  1206. * We consider arg to be a file spec if and only if it satisfies
  1207. * all of the below criteria::
  1208. * - it does not include any of "+@%",
  1209. * - it includes one of ":;", and
  1210. * - it has a period '.' in the name.
  1211. *
  1212. * Otherwise, we consider arg to be a function specification.
  1213. */
  1214. if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
  1215. /* This is a file spec if it includes a '.' before ; or : */
  1216. if (memchr(arg, '.', ptr - arg))
  1217. file_spec = true;
  1218. }
  1219. ptr = strpbrk(arg, ";:+@%");
  1220. if (ptr) {
  1221. nc = *ptr;
  1222. *ptr++ = '\0';
  1223. }
  1224. if (arg[0] == '\0')
  1225. tmp = NULL;
  1226. else {
  1227. tmp = strdup(arg);
  1228. if (tmp == NULL)
  1229. return -ENOMEM;
  1230. }
  1231. if (file_spec)
  1232. pp->file = tmp;
  1233. else {
  1234. pp->function = tmp;
  1235. /*
  1236. * Keep pp->function even if this is absolute address,
  1237. * so it can mark whether abs_address is valid.
  1238. * Which make 'perf probe lib.bin 0x0' possible.
  1239. *
  1240. * Note that checking length of tmp is not needed
  1241. * because when we access tmp[1] we know tmp[0] is '0',
  1242. * so tmp[1] should always valid (but could be '\0').
  1243. */
  1244. if (tmp && !strncmp(tmp, "0x", 2)) {
  1245. pp->abs_address = strtoul(pp->function, &tmp, 0);
  1246. if (*tmp != '\0') {
  1247. semantic_error("Invalid absolute address.\n");
  1248. return -EINVAL;
  1249. }
  1250. }
  1251. }
  1252. /* Parse other options */
  1253. while (ptr) {
  1254. arg = ptr;
  1255. c = nc;
  1256. if (c == ';') { /* Lazy pattern must be the last part */
  1257. pp->lazy_line = strdup(arg);
  1258. if (pp->lazy_line == NULL)
  1259. return -ENOMEM;
  1260. break;
  1261. }
  1262. ptr = strpbrk(arg, ";:+@%");
  1263. if (ptr) {
  1264. nc = *ptr;
  1265. *ptr++ = '\0';
  1266. }
  1267. switch (c) {
  1268. case ':': /* Line number */
  1269. pp->line = strtoul(arg, &tmp, 0);
  1270. if (*tmp != '\0') {
  1271. semantic_error("There is non-digit char"
  1272. " in line number.\n");
  1273. return -EINVAL;
  1274. }
  1275. break;
  1276. case '+': /* Byte offset from a symbol */
  1277. pp->offset = strtoul(arg, &tmp, 0);
  1278. if (*tmp != '\0') {
  1279. semantic_error("There is non-digit character"
  1280. " in offset.\n");
  1281. return -EINVAL;
  1282. }
  1283. break;
  1284. case '@': /* File name */
  1285. if (pp->file) {
  1286. semantic_error("SRC@SRC is not allowed.\n");
  1287. return -EINVAL;
  1288. }
  1289. pp->file = strdup(arg);
  1290. if (pp->file == NULL)
  1291. return -ENOMEM;
  1292. break;
  1293. case '%': /* Probe places */
  1294. if (strcmp(arg, "return") == 0) {
  1295. pp->retprobe = 1;
  1296. } else { /* Others not supported yet */
  1297. semantic_error("%%%s is not supported.\n", arg);
  1298. return -ENOTSUP;
  1299. }
  1300. break;
  1301. default: /* Buggy case */
  1302. pr_err("This program has a bug at %s:%d.\n",
  1303. __FILE__, __LINE__);
  1304. return -ENOTSUP;
  1305. break;
  1306. }
  1307. }
  1308. /* Exclusion check */
  1309. if (pp->lazy_line && pp->line) {
  1310. semantic_error("Lazy pattern can't be used with"
  1311. " line number.\n");
  1312. return -EINVAL;
  1313. }
  1314. if (pp->lazy_line && pp->offset) {
  1315. semantic_error("Lazy pattern can't be used with offset.\n");
  1316. return -EINVAL;
  1317. }
  1318. if (pp->line && pp->offset) {
  1319. semantic_error("Offset can't be used with line number.\n");
  1320. return -EINVAL;
  1321. }
  1322. if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
  1323. semantic_error("File always requires line number or "
  1324. "lazy pattern.\n");
  1325. return -EINVAL;
  1326. }
  1327. if (pp->offset && !pp->function) {
  1328. semantic_error("Offset requires an entry function.\n");
  1329. return -EINVAL;
  1330. }
  1331. if (pp->retprobe && !pp->function) {
  1332. semantic_error("Return probe requires an entry function.\n");
  1333. return -EINVAL;
  1334. }
  1335. if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
  1336. semantic_error("Offset/Line/Lazy pattern can't be used with "
  1337. "return probe.\n");
  1338. return -EINVAL;
  1339. }
  1340. pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
  1341. pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
  1342. pp->lazy_line);
  1343. return 0;
  1344. }
  1345. /* Parse perf-probe event argument */
  1346. static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
  1347. {
  1348. char *tmp, *goodname;
  1349. struct perf_probe_arg_field **fieldp;
  1350. pr_debug("parsing arg: %s into ", str);
  1351. tmp = strchr(str, '=');
  1352. if (tmp) {
  1353. arg->name = strndup(str, tmp - str);
  1354. if (arg->name == NULL)
  1355. return -ENOMEM;
  1356. pr_debug("name:%s ", arg->name);
  1357. str = tmp + 1;
  1358. }
  1359. tmp = strchr(str, ':');
  1360. if (tmp) { /* Type setting */
  1361. *tmp = '\0';
  1362. arg->type = strdup(tmp + 1);
  1363. if (arg->type == NULL)
  1364. return -ENOMEM;
  1365. pr_debug("type:%s ", arg->type);
  1366. }
  1367. tmp = strpbrk(str, "-.[");
  1368. if (!is_c_varname(str) || !tmp) {
  1369. /* A variable, register, symbol or special value */
  1370. arg->var = strdup(str);
  1371. if (arg->var == NULL)
  1372. return -ENOMEM;
  1373. pr_debug("%s\n", arg->var);
  1374. return 0;
  1375. }
  1376. /* Structure fields or array element */
  1377. arg->var = strndup(str, tmp - str);
  1378. if (arg->var == NULL)
  1379. return -ENOMEM;
  1380. goodname = arg->var;
  1381. pr_debug("%s, ", arg->var);
  1382. fieldp = &arg->field;
  1383. do {
  1384. *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
  1385. if (*fieldp == NULL)
  1386. return -ENOMEM;
  1387. if (*tmp == '[') { /* Array */
  1388. str = tmp;
  1389. (*fieldp)->index = strtol(str + 1, &tmp, 0);
  1390. (*fieldp)->ref = true;
  1391. if (*tmp != ']' || tmp == str + 1) {
  1392. semantic_error("Array index must be a"
  1393. " number.\n");
  1394. return -EINVAL;
  1395. }
  1396. tmp++;
  1397. if (*tmp == '\0')
  1398. tmp = NULL;
  1399. } else { /* Structure */
  1400. if (*tmp == '.') {
  1401. str = tmp + 1;
  1402. (*fieldp)->ref = false;
  1403. } else if (tmp[1] == '>') {
  1404. str = tmp + 2;
  1405. (*fieldp)->ref = true;
  1406. } else {
  1407. semantic_error("Argument parse error: %s\n",
  1408. str);
  1409. return -EINVAL;
  1410. }
  1411. tmp = strpbrk(str, "-.[");
  1412. }
  1413. if (tmp) {
  1414. (*fieldp)->name = strndup(str, tmp - str);
  1415. if ((*fieldp)->name == NULL)
  1416. return -ENOMEM;
  1417. if (*str != '[')
  1418. goodname = (*fieldp)->name;
  1419. pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
  1420. fieldp = &(*fieldp)->next;
  1421. }
  1422. } while (tmp);
  1423. (*fieldp)->name = strdup(str);
  1424. if ((*fieldp)->name == NULL)
  1425. return -ENOMEM;
  1426. if (*str != '[')
  1427. goodname = (*fieldp)->name;
  1428. pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
  1429. /* If no name is specified, set the last field name (not array index)*/
  1430. if (!arg->name) {
  1431. arg->name = strdup(goodname);
  1432. if (arg->name == NULL)
  1433. return -ENOMEM;
  1434. }
  1435. return 0;
  1436. }
  1437. /* Parse perf-probe event command */
  1438. int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
  1439. {
  1440. char **argv;
  1441. int argc, i, ret = 0;
  1442. argv = argv_split(cmd, &argc);
  1443. if (!argv) {
  1444. pr_debug("Failed to split arguments.\n");
  1445. return -ENOMEM;
  1446. }
  1447. if (argc - 1 > MAX_PROBE_ARGS) {
  1448. semantic_error("Too many probe arguments (%d).\n", argc - 1);
  1449. ret = -ERANGE;
  1450. goto out;
  1451. }
  1452. /* Parse probe point */
  1453. ret = parse_perf_probe_point(argv[0], pev);
  1454. if (ret < 0)
  1455. goto out;
  1456. /* Copy arguments and ensure return probe has no C argument */
  1457. pev->nargs = argc - 1;
  1458. pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  1459. if (pev->args == NULL) {
  1460. ret = -ENOMEM;
  1461. goto out;
  1462. }
  1463. for (i = 0; i < pev->nargs && ret >= 0; i++) {
  1464. ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
  1465. if (ret >= 0 &&
  1466. is_c_varname(pev->args[i].var) && pev->point.retprobe) {
  1467. semantic_error("You can't specify local variable for"
  1468. " kretprobe.\n");
  1469. ret = -EINVAL;
  1470. }
  1471. }
  1472. out:
  1473. argv_free(argv);
  1474. return ret;
  1475. }
  1476. /* Returns true if *any* ARG is either C variable, $params or $vars. */
  1477. bool perf_probe_with_var(struct perf_probe_event *pev)
  1478. {
  1479. int i = 0;
  1480. for (i = 0; i < pev->nargs; i++)
  1481. if (is_c_varname(pev->args[i].var) ||
  1482. !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) ||
  1483. !strcmp(pev->args[i].var, PROBE_ARG_VARS))
  1484. return true;
  1485. return false;
  1486. }
  1487. /* Return true if this perf_probe_event requires debuginfo */
  1488. bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
  1489. {
  1490. if (pev->point.file || pev->point.line || pev->point.lazy_line)
  1491. return true;
  1492. if (perf_probe_with_var(pev))
  1493. return true;
  1494. return false;
  1495. }
  1496. /* Parse probe_events event into struct probe_point */
  1497. int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
  1498. {
  1499. struct probe_trace_point *tp = &tev->point;
  1500. char pr;
  1501. char *p;
  1502. char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
  1503. int ret, i, argc;
  1504. char **argv;
  1505. pr_debug("Parsing probe_events: %s\n", cmd);
  1506. argv = argv_split(cmd, &argc);
  1507. if (!argv) {
  1508. pr_debug("Failed to split arguments.\n");
  1509. return -ENOMEM;
  1510. }
  1511. if (argc < 2) {
  1512. semantic_error("Too few probe arguments.\n");
  1513. ret = -ERANGE;
  1514. goto out;
  1515. }
  1516. /* Scan event and group name. */
  1517. argv0_str = strdup(argv[0]);
  1518. if (argv0_str == NULL) {
  1519. ret = -ENOMEM;
  1520. goto out;
  1521. }
  1522. fmt1_str = strtok_r(argv0_str, ":", &fmt);
  1523. fmt2_str = strtok_r(NULL, "/", &fmt);
  1524. fmt3_str = strtok_r(NULL, " \t", &fmt);
  1525. if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
  1526. || fmt3_str == NULL) {
  1527. semantic_error("Failed to parse event name: %s\n", argv[0]);
  1528. ret = -EINVAL;
  1529. goto out;
  1530. }
  1531. pr = fmt1_str[0];
  1532. tev->group = strdup(fmt2_str);
  1533. tev->event = strdup(fmt3_str);
  1534. if (tev->group == NULL || tev->event == NULL) {
  1535. ret = -ENOMEM;
  1536. goto out;
  1537. }
  1538. pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
  1539. tp->retprobe = (pr == 'r');
  1540. /* Scan module name(if there), function name and offset */
  1541. p = strchr(argv[1], ':');
  1542. if (p) {
  1543. tp->module = strndup(argv[1], p - argv[1]);
  1544. if (!tp->module) {
  1545. ret = -ENOMEM;
  1546. goto out;
  1547. }
  1548. tev->uprobes = (tp->module[0] == '/');
  1549. p++;
  1550. } else
  1551. p = argv[1];
  1552. fmt1_str = strtok_r(p, "+", &fmt);
  1553. /* only the address started with 0x */
  1554. if (fmt1_str[0] == '0') {
  1555. /*
  1556. * Fix a special case:
  1557. * if address == 0, kernel reports something like:
  1558. * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
  1559. * Newer kernel may fix that, but we want to
  1560. * support old kernel also.
  1561. */
  1562. if (strcmp(fmt1_str, "0x") == 0) {
  1563. if (!argv[2] || strcmp(argv[2], "(null)")) {
  1564. ret = -EINVAL;
  1565. goto out;
  1566. }
  1567. tp->address = 0;
  1568. free(argv[2]);
  1569. for (i = 2; argv[i + 1] != NULL; i++)
  1570. argv[i] = argv[i + 1];
  1571. argv[i] = NULL;
  1572. argc -= 1;
  1573. } else
  1574. tp->address = strtoul(fmt1_str, NULL, 0);
  1575. } else {
  1576. /* Only the symbol-based probe has offset */
  1577. tp->symbol = strdup(fmt1_str);
  1578. if (tp->symbol == NULL) {
  1579. ret = -ENOMEM;
  1580. goto out;
  1581. }
  1582. fmt2_str = strtok_r(NULL, "", &fmt);
  1583. if (fmt2_str == NULL)
  1584. tp->offset = 0;
  1585. else
  1586. tp->offset = strtoul(fmt2_str, NULL, 10);
  1587. }
  1588. tev->nargs = argc - 2;
  1589. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  1590. if (tev->args == NULL) {
  1591. ret = -ENOMEM;
  1592. goto out;
  1593. }
  1594. for (i = 0; i < tev->nargs; i++) {
  1595. p = strchr(argv[i + 2], '=');
  1596. if (p) /* We don't need which register is assigned. */
  1597. *p++ = '\0';
  1598. else
  1599. p = argv[i + 2];
  1600. tev->args[i].name = strdup(argv[i + 2]);
  1601. /* TODO: parse regs and offset */
  1602. tev->args[i].value = strdup(p);
  1603. if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
  1604. ret = -ENOMEM;
  1605. goto out;
  1606. }
  1607. }
  1608. ret = 0;
  1609. out:
  1610. free(argv0_str);
  1611. argv_free(argv);
  1612. return ret;
  1613. }
  1614. /* Compose only probe arg */
  1615. char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
  1616. {
  1617. struct perf_probe_arg_field *field = pa->field;
  1618. struct strbuf buf;
  1619. char *ret = NULL;
  1620. int err;
  1621. if (strbuf_init(&buf, 64) < 0)
  1622. return NULL;
  1623. if (pa->name && pa->var)
  1624. err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
  1625. else
  1626. err = strbuf_addstr(&buf, pa->name ?: pa->var);
  1627. if (err)
  1628. goto out;
  1629. while (field) {
  1630. if (field->name[0] == '[')
  1631. err = strbuf_addstr(&buf, field->name);
  1632. else
  1633. err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
  1634. field->name);
  1635. field = field->next;
  1636. if (err)
  1637. goto out;
  1638. }
  1639. if (pa->type)
  1640. if (strbuf_addf(&buf, ":%s", pa->type) < 0)
  1641. goto out;
  1642. ret = strbuf_detach(&buf, NULL);
  1643. out:
  1644. strbuf_release(&buf);
  1645. return ret;
  1646. }
  1647. /* Compose only probe point (not argument) */
  1648. char *synthesize_perf_probe_point(struct perf_probe_point *pp)
  1649. {
  1650. struct strbuf buf;
  1651. char *tmp, *ret = NULL;
  1652. int len, err = 0;
  1653. if (strbuf_init(&buf, 64) < 0)
  1654. return NULL;
  1655. if (pp->function) {
  1656. if (strbuf_addstr(&buf, pp->function) < 0)
  1657. goto out;
  1658. if (pp->offset)
  1659. err = strbuf_addf(&buf, "+%lu", pp->offset);
  1660. else if (pp->line)
  1661. err = strbuf_addf(&buf, ":%d", pp->line);
  1662. else if (pp->retprobe)
  1663. err = strbuf_addstr(&buf, "%return");
  1664. if (err)
  1665. goto out;
  1666. }
  1667. if (pp->file) {
  1668. tmp = pp->file;
  1669. len = strlen(tmp);
  1670. if (len > 30) {
  1671. tmp = strchr(pp->file + len - 30, '/');
  1672. tmp = tmp ? tmp + 1 : pp->file + len - 30;
  1673. }
  1674. err = strbuf_addf(&buf, "@%s", tmp);
  1675. if (!err && !pp->function && pp->line)
  1676. err = strbuf_addf(&buf, ":%d", pp->line);
  1677. }
  1678. if (!err)
  1679. ret = strbuf_detach(&buf, NULL);
  1680. out:
  1681. strbuf_release(&buf);
  1682. return ret;
  1683. }
  1684. char *synthesize_perf_probe_command(struct perf_probe_event *pev)
  1685. {
  1686. struct strbuf buf;
  1687. char *tmp, *ret = NULL;
  1688. int i;
  1689. if (strbuf_init(&buf, 64))
  1690. return NULL;
  1691. if (pev->event)
  1692. if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
  1693. pev->event) < 0)
  1694. goto out;
  1695. tmp = synthesize_perf_probe_point(&pev->point);
  1696. if (!tmp || strbuf_addstr(&buf, tmp) < 0)
  1697. goto out;
  1698. free(tmp);
  1699. for (i = 0; i < pev->nargs; i++) {
  1700. tmp = synthesize_perf_probe_arg(pev->args + i);
  1701. if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
  1702. goto out;
  1703. free(tmp);
  1704. }
  1705. ret = strbuf_detach(&buf, NULL);
  1706. out:
  1707. strbuf_release(&buf);
  1708. return ret;
  1709. }
  1710. static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
  1711. struct strbuf *buf, int depth)
  1712. {
  1713. int err;
  1714. if (ref->next) {
  1715. depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
  1716. depth + 1);
  1717. if (depth < 0)
  1718. return depth;
  1719. }
  1720. err = strbuf_addf(buf, "%+ld(", ref->offset);
  1721. return (err < 0) ? err : depth;
  1722. }
  1723. static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
  1724. struct strbuf *buf)
  1725. {
  1726. struct probe_trace_arg_ref *ref = arg->ref;
  1727. int depth = 0, err;
  1728. /* Argument name or separator */
  1729. if (arg->name)
  1730. err = strbuf_addf(buf, " %s=", arg->name);
  1731. else
  1732. err = strbuf_addch(buf, ' ');
  1733. if (err)
  1734. return err;
  1735. /* Special case: @XXX */
  1736. if (arg->value[0] == '@' && arg->ref)
  1737. ref = ref->next;
  1738. /* Dereferencing arguments */
  1739. if (ref) {
  1740. depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
  1741. if (depth < 0)
  1742. return depth;
  1743. }
  1744. /* Print argument value */
  1745. if (arg->value[0] == '@' && arg->ref)
  1746. err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
  1747. else
  1748. err = strbuf_addstr(buf, arg->value);
  1749. /* Closing */
  1750. while (!err && depth--)
  1751. err = strbuf_addch(buf, ')');
  1752. /* Print argument type */
  1753. if (!err && arg->type)
  1754. err = strbuf_addf(buf, ":%s", arg->type);
  1755. return err;
  1756. }
  1757. char *synthesize_probe_trace_command(struct probe_trace_event *tev)
  1758. {
  1759. struct probe_trace_point *tp = &tev->point;
  1760. struct strbuf buf;
  1761. char *ret = NULL;
  1762. int i, err;
  1763. /* Uprobes must have tp->module */
  1764. if (tev->uprobes && !tp->module)
  1765. return NULL;
  1766. if (strbuf_init(&buf, 32) < 0)
  1767. return NULL;
  1768. if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
  1769. tev->group, tev->event) < 0)
  1770. goto error;
  1771. /*
  1772. * If tp->address == 0, then this point must be a
  1773. * absolute address uprobe.
  1774. * try_to_find_absolute_address() should have made
  1775. * tp->symbol to "0x0".
  1776. */
  1777. if (tev->uprobes && !tp->address) {
  1778. if (!tp->symbol || strcmp(tp->symbol, "0x0"))
  1779. goto error;
  1780. }
  1781. /* Use the tp->address for uprobes */
  1782. if (tev->uprobes)
  1783. err = strbuf_addf(&buf, "%s:0x%lx", tp->module, tp->address);
  1784. else if (!strncmp(tp->symbol, "0x", 2))
  1785. /* Absolute address. See try_to_find_absolute_address() */
  1786. err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
  1787. tp->module ? ":" : "", tp->address);
  1788. else
  1789. err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
  1790. tp->module ? ":" : "", tp->symbol, tp->offset);
  1791. if (err)
  1792. goto error;
  1793. for (i = 0; i < tev->nargs; i++)
  1794. if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
  1795. goto error;
  1796. ret = strbuf_detach(&buf, NULL);
  1797. error:
  1798. strbuf_release(&buf);
  1799. return ret;
  1800. }
  1801. static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
  1802. struct perf_probe_point *pp,
  1803. bool is_kprobe)
  1804. {
  1805. struct symbol *sym = NULL;
  1806. struct map *map;
  1807. u64 addr = tp->address;
  1808. int ret = -ENOENT;
  1809. if (!is_kprobe) {
  1810. map = dso__new_map(tp->module);
  1811. if (!map)
  1812. goto out;
  1813. sym = map__find_symbol(map, addr);
  1814. } else {
  1815. if (tp->symbol && !addr) {
  1816. if (kernel_get_symbol_address_by_name(tp->symbol,
  1817. &addr, true, false) < 0)
  1818. goto out;
  1819. }
  1820. if (addr) {
  1821. addr += tp->offset;
  1822. sym = __find_kernel_function(addr, &map);
  1823. }
  1824. }
  1825. if (!sym)
  1826. goto out;
  1827. pp->retprobe = tp->retprobe;
  1828. pp->offset = addr - map->unmap_ip(map, sym->start);
  1829. pp->function = strdup(sym->name);
  1830. ret = pp->function ? 0 : -ENOMEM;
  1831. out:
  1832. if (map && !is_kprobe) {
  1833. map__put(map);
  1834. }
  1835. return ret;
  1836. }
  1837. static int convert_to_perf_probe_point(struct probe_trace_point *tp,
  1838. struct perf_probe_point *pp,
  1839. bool is_kprobe)
  1840. {
  1841. char buf[128];
  1842. int ret;
  1843. ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
  1844. if (!ret)
  1845. return 0;
  1846. ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
  1847. if (!ret)
  1848. return 0;
  1849. pr_debug("Failed to find probe point from both of dwarf and map.\n");
  1850. if (tp->symbol) {
  1851. pp->function = strdup(tp->symbol);
  1852. pp->offset = tp->offset;
  1853. } else {
  1854. ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
  1855. if (ret < 0)
  1856. return ret;
  1857. pp->function = strdup(buf);
  1858. pp->offset = 0;
  1859. }
  1860. if (pp->function == NULL)
  1861. return -ENOMEM;
  1862. pp->retprobe = tp->retprobe;
  1863. return 0;
  1864. }
  1865. static int convert_to_perf_probe_event(struct probe_trace_event *tev,
  1866. struct perf_probe_event *pev, bool is_kprobe)
  1867. {
  1868. struct strbuf buf = STRBUF_INIT;
  1869. int i, ret;
  1870. /* Convert event/group name */
  1871. pev->event = strdup(tev->event);
  1872. pev->group = strdup(tev->group);
  1873. if (pev->event == NULL || pev->group == NULL)
  1874. return -ENOMEM;
  1875. /* Convert trace_point to probe_point */
  1876. ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
  1877. if (ret < 0)
  1878. return ret;
  1879. /* Convert trace_arg to probe_arg */
  1880. pev->nargs = tev->nargs;
  1881. pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  1882. if (pev->args == NULL)
  1883. return -ENOMEM;
  1884. for (i = 0; i < tev->nargs && ret >= 0; i++) {
  1885. if (tev->args[i].name)
  1886. pev->args[i].name = strdup(tev->args[i].name);
  1887. else {
  1888. if ((ret = strbuf_init(&buf, 32)) < 0)
  1889. goto error;
  1890. ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
  1891. pev->args[i].name = strbuf_detach(&buf, NULL);
  1892. }
  1893. if (pev->args[i].name == NULL && ret >= 0)
  1894. ret = -ENOMEM;
  1895. }
  1896. error:
  1897. if (ret < 0)
  1898. clear_perf_probe_event(pev);
  1899. return ret;
  1900. }
  1901. void clear_perf_probe_event(struct perf_probe_event *pev)
  1902. {
  1903. struct perf_probe_arg_field *field, *next;
  1904. int i;
  1905. free(pev->event);
  1906. free(pev->group);
  1907. free(pev->target);
  1908. clear_perf_probe_point(&pev->point);
  1909. for (i = 0; i < pev->nargs; i++) {
  1910. free(pev->args[i].name);
  1911. free(pev->args[i].var);
  1912. free(pev->args[i].type);
  1913. field = pev->args[i].field;
  1914. while (field) {
  1915. next = field->next;
  1916. zfree(&field->name);
  1917. free(field);
  1918. field = next;
  1919. }
  1920. }
  1921. free(pev->args);
  1922. memset(pev, 0, sizeof(*pev));
  1923. }
  1924. #define strdup_or_goto(str, label) \
  1925. ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
  1926. static int perf_probe_point__copy(struct perf_probe_point *dst,
  1927. struct perf_probe_point *src)
  1928. {
  1929. dst->file = strdup_or_goto(src->file, out_err);
  1930. dst->function = strdup_or_goto(src->function, out_err);
  1931. dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
  1932. dst->line = src->line;
  1933. dst->retprobe = src->retprobe;
  1934. dst->offset = src->offset;
  1935. return 0;
  1936. out_err:
  1937. clear_perf_probe_point(dst);
  1938. return -ENOMEM;
  1939. }
  1940. static int perf_probe_arg__copy(struct perf_probe_arg *dst,
  1941. struct perf_probe_arg *src)
  1942. {
  1943. struct perf_probe_arg_field *field, **ppfield;
  1944. dst->name = strdup_or_goto(src->name, out_err);
  1945. dst->var = strdup_or_goto(src->var, out_err);
  1946. dst->type = strdup_or_goto(src->type, out_err);
  1947. field = src->field;
  1948. ppfield = &(dst->field);
  1949. while (field) {
  1950. *ppfield = zalloc(sizeof(*field));
  1951. if (!*ppfield)
  1952. goto out_err;
  1953. (*ppfield)->name = strdup_or_goto(field->name, out_err);
  1954. (*ppfield)->index = field->index;
  1955. (*ppfield)->ref = field->ref;
  1956. field = field->next;
  1957. ppfield = &((*ppfield)->next);
  1958. }
  1959. return 0;
  1960. out_err:
  1961. return -ENOMEM;
  1962. }
  1963. int perf_probe_event__copy(struct perf_probe_event *dst,
  1964. struct perf_probe_event *src)
  1965. {
  1966. int i;
  1967. dst->event = strdup_or_goto(src->event, out_err);
  1968. dst->group = strdup_or_goto(src->group, out_err);
  1969. dst->target = strdup_or_goto(src->target, out_err);
  1970. dst->uprobes = src->uprobes;
  1971. if (perf_probe_point__copy(&dst->point, &src->point) < 0)
  1972. goto out_err;
  1973. dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
  1974. if (!dst->args)
  1975. goto out_err;
  1976. dst->nargs = src->nargs;
  1977. for (i = 0; i < src->nargs; i++)
  1978. if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
  1979. goto out_err;
  1980. return 0;
  1981. out_err:
  1982. clear_perf_probe_event(dst);
  1983. return -ENOMEM;
  1984. }
  1985. void clear_probe_trace_event(struct probe_trace_event *tev)
  1986. {
  1987. struct probe_trace_arg_ref *ref, *next;
  1988. int i;
  1989. free(tev->event);
  1990. free(tev->group);
  1991. free(tev->point.symbol);
  1992. free(tev->point.realname);
  1993. free(tev->point.module);
  1994. for (i = 0; i < tev->nargs; i++) {
  1995. free(tev->args[i].name);
  1996. free(tev->args[i].value);
  1997. free(tev->args[i].type);
  1998. ref = tev->args[i].ref;
  1999. while (ref) {
  2000. next = ref->next;
  2001. free(ref);
  2002. ref = next;
  2003. }
  2004. }
  2005. free(tev->args);
  2006. memset(tev, 0, sizeof(*tev));
  2007. }
  2008. struct kprobe_blacklist_node {
  2009. struct list_head list;
  2010. unsigned long start;
  2011. unsigned long end;
  2012. char *symbol;
  2013. };
  2014. static void kprobe_blacklist__delete(struct list_head *blacklist)
  2015. {
  2016. struct kprobe_blacklist_node *node;
  2017. while (!list_empty(blacklist)) {
  2018. node = list_first_entry(blacklist,
  2019. struct kprobe_blacklist_node, list);
  2020. list_del(&node->list);
  2021. free(node->symbol);
  2022. free(node);
  2023. }
  2024. }
  2025. static int kprobe_blacklist__load(struct list_head *blacklist)
  2026. {
  2027. struct kprobe_blacklist_node *node;
  2028. const char *__debugfs = debugfs__mountpoint();
  2029. char buf[PATH_MAX], *p;
  2030. FILE *fp;
  2031. int ret;
  2032. if (__debugfs == NULL)
  2033. return -ENOTSUP;
  2034. ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
  2035. if (ret < 0)
  2036. return ret;
  2037. fp = fopen(buf, "r");
  2038. if (!fp)
  2039. return -errno;
  2040. ret = 0;
  2041. while (fgets(buf, PATH_MAX, fp)) {
  2042. node = zalloc(sizeof(*node));
  2043. if (!node) {
  2044. ret = -ENOMEM;
  2045. break;
  2046. }
  2047. INIT_LIST_HEAD(&node->list);
  2048. list_add_tail(&node->list, blacklist);
  2049. if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
  2050. ret = -EINVAL;
  2051. break;
  2052. }
  2053. p = strchr(buf, '\t');
  2054. if (p) {
  2055. p++;
  2056. if (p[strlen(p) - 1] == '\n')
  2057. p[strlen(p) - 1] = '\0';
  2058. } else
  2059. p = (char *)"unknown";
  2060. node->symbol = strdup(p);
  2061. if (!node->symbol) {
  2062. ret = -ENOMEM;
  2063. break;
  2064. }
  2065. pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
  2066. node->start, node->end, node->symbol);
  2067. ret++;
  2068. }
  2069. if (ret < 0)
  2070. kprobe_blacklist__delete(blacklist);
  2071. fclose(fp);
  2072. return ret;
  2073. }
  2074. static struct kprobe_blacklist_node *
  2075. kprobe_blacklist__find_by_address(struct list_head *blacklist,
  2076. unsigned long address)
  2077. {
  2078. struct kprobe_blacklist_node *node;
  2079. list_for_each_entry(node, blacklist, list) {
  2080. if (node->start <= address && address <= node->end)
  2081. return node;
  2082. }
  2083. return NULL;
  2084. }
  2085. static LIST_HEAD(kprobe_blacklist);
  2086. static void kprobe_blacklist__init(void)
  2087. {
  2088. if (!list_empty(&kprobe_blacklist))
  2089. return;
  2090. if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
  2091. pr_debug("No kprobe blacklist support, ignored\n");
  2092. }
  2093. static void kprobe_blacklist__release(void)
  2094. {
  2095. kprobe_blacklist__delete(&kprobe_blacklist);
  2096. }
  2097. static bool kprobe_blacklist__listed(unsigned long address)
  2098. {
  2099. return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
  2100. }
  2101. static int perf_probe_event__sprintf(const char *group, const char *event,
  2102. struct perf_probe_event *pev,
  2103. const char *module,
  2104. struct strbuf *result)
  2105. {
  2106. int i, ret;
  2107. char *buf;
  2108. if (asprintf(&buf, "%s:%s", group, event) < 0)
  2109. return -errno;
  2110. ret = strbuf_addf(result, " %-20s (on ", buf);
  2111. free(buf);
  2112. if (ret)
  2113. return ret;
  2114. /* Synthesize only event probe point */
  2115. buf = synthesize_perf_probe_point(&pev->point);
  2116. if (!buf)
  2117. return -ENOMEM;
  2118. ret = strbuf_addstr(result, buf);
  2119. free(buf);
  2120. if (!ret && module)
  2121. ret = strbuf_addf(result, " in %s", module);
  2122. if (!ret && pev->nargs > 0) {
  2123. ret = strbuf_add(result, " with", 5);
  2124. for (i = 0; !ret && i < pev->nargs; i++) {
  2125. buf = synthesize_perf_probe_arg(&pev->args[i]);
  2126. if (!buf)
  2127. return -ENOMEM;
  2128. ret = strbuf_addf(result, " %s", buf);
  2129. free(buf);
  2130. }
  2131. }
  2132. if (!ret)
  2133. ret = strbuf_addch(result, ')');
  2134. return ret;
  2135. }
  2136. /* Show an event */
  2137. int show_perf_probe_event(const char *group, const char *event,
  2138. struct perf_probe_event *pev,
  2139. const char *module, bool use_stdout)
  2140. {
  2141. struct strbuf buf = STRBUF_INIT;
  2142. int ret;
  2143. ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
  2144. if (ret >= 0) {
  2145. if (use_stdout)
  2146. printf("%s\n", buf.buf);
  2147. else
  2148. pr_info("%s\n", buf.buf);
  2149. }
  2150. strbuf_release(&buf);
  2151. return ret;
  2152. }
  2153. static bool filter_probe_trace_event(struct probe_trace_event *tev,
  2154. struct strfilter *filter)
  2155. {
  2156. char tmp[128];
  2157. /* At first, check the event name itself */
  2158. if (strfilter__compare(filter, tev->event))
  2159. return true;
  2160. /* Next, check the combination of name and group */
  2161. if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
  2162. return false;
  2163. return strfilter__compare(filter, tmp);
  2164. }
  2165. static int __show_perf_probe_events(int fd, bool is_kprobe,
  2166. struct strfilter *filter)
  2167. {
  2168. int ret = 0;
  2169. struct probe_trace_event tev;
  2170. struct perf_probe_event pev;
  2171. struct strlist *rawlist;
  2172. struct str_node *ent;
  2173. memset(&tev, 0, sizeof(tev));
  2174. memset(&pev, 0, sizeof(pev));
  2175. rawlist = probe_file__get_rawlist(fd);
  2176. if (!rawlist)
  2177. return -ENOMEM;
  2178. strlist__for_each_entry(ent, rawlist) {
  2179. ret = parse_probe_trace_command(ent->s, &tev);
  2180. if (ret >= 0) {
  2181. if (!filter_probe_trace_event(&tev, filter))
  2182. goto next;
  2183. ret = convert_to_perf_probe_event(&tev, &pev,
  2184. is_kprobe);
  2185. if (ret < 0)
  2186. goto next;
  2187. ret = show_perf_probe_event(pev.group, pev.event,
  2188. &pev, tev.point.module,
  2189. true);
  2190. }
  2191. next:
  2192. clear_perf_probe_event(&pev);
  2193. clear_probe_trace_event(&tev);
  2194. if (ret < 0)
  2195. break;
  2196. }
  2197. strlist__delete(rawlist);
  2198. /* Cleanup cached debuginfo if needed */
  2199. debuginfo_cache__exit();
  2200. return ret;
  2201. }
  2202. /* List up current perf-probe events */
  2203. int show_perf_probe_events(struct strfilter *filter)
  2204. {
  2205. int kp_fd, up_fd, ret;
  2206. setup_pager();
  2207. if (probe_conf.cache)
  2208. return probe_cache__show_all_caches(filter);
  2209. ret = init_probe_symbol_maps(false);
  2210. if (ret < 0)
  2211. return ret;
  2212. ret = probe_file__open_both(&kp_fd, &up_fd, 0);
  2213. if (ret < 0)
  2214. return ret;
  2215. if (kp_fd >= 0)
  2216. ret = __show_perf_probe_events(kp_fd, true, filter);
  2217. if (up_fd >= 0 && ret >= 0)
  2218. ret = __show_perf_probe_events(up_fd, false, filter);
  2219. if (kp_fd > 0)
  2220. close(kp_fd);
  2221. if (up_fd > 0)
  2222. close(up_fd);
  2223. exit_probe_symbol_maps();
  2224. return ret;
  2225. }
  2226. static int get_new_event_name(char *buf, size_t len, const char *base,
  2227. struct strlist *namelist, bool allow_suffix)
  2228. {
  2229. int i, ret;
  2230. char *p, *nbase;
  2231. if (*base == '.')
  2232. base++;
  2233. nbase = strdup(base);
  2234. if (!nbase)
  2235. return -ENOMEM;
  2236. /* Cut off the dot suffixes (e.g. .const, .isra)*/
  2237. p = strchr(nbase, '.');
  2238. if (p && p != nbase)
  2239. *p = '\0';
  2240. /* Try no suffix number */
  2241. ret = e_snprintf(buf, len, "%s", nbase);
  2242. if (ret < 0) {
  2243. pr_debug("snprintf() failed: %d\n", ret);
  2244. goto out;
  2245. }
  2246. if (!strlist__has_entry(namelist, buf))
  2247. goto out;
  2248. if (!allow_suffix) {
  2249. pr_warning("Error: event \"%s\" already exists.\n"
  2250. " Hint: Remove existing event by 'perf probe -d'\n"
  2251. " or force duplicates by 'perf probe -f'\n"
  2252. " or set 'force=yes' in BPF source.\n",
  2253. buf);
  2254. ret = -EEXIST;
  2255. goto out;
  2256. }
  2257. /* Try to add suffix */
  2258. for (i = 1; i < MAX_EVENT_INDEX; i++) {
  2259. ret = e_snprintf(buf, len, "%s_%d", nbase, i);
  2260. if (ret < 0) {
  2261. pr_debug("snprintf() failed: %d\n", ret);
  2262. goto out;
  2263. }
  2264. if (!strlist__has_entry(namelist, buf))
  2265. break;
  2266. }
  2267. if (i == MAX_EVENT_INDEX) {
  2268. pr_warning("Too many events are on the same function.\n");
  2269. ret = -ERANGE;
  2270. }
  2271. out:
  2272. free(nbase);
  2273. /* Final validation */
  2274. if (ret >= 0 && !is_c_func_name(buf)) {
  2275. pr_warning("Internal error: \"%s\" is an invalid event name.\n",
  2276. buf);
  2277. ret = -EINVAL;
  2278. }
  2279. return ret;
  2280. }
  2281. /* Warn if the current kernel's uprobe implementation is old */
  2282. static void warn_uprobe_event_compat(struct probe_trace_event *tev)
  2283. {
  2284. int i;
  2285. char *buf = synthesize_probe_trace_command(tev);
  2286. /* Old uprobe event doesn't support memory dereference */
  2287. if (!tev->uprobes || tev->nargs == 0 || !buf)
  2288. goto out;
  2289. for (i = 0; i < tev->nargs; i++)
  2290. if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
  2291. pr_warning("Please upgrade your kernel to at least "
  2292. "3.14 to have access to feature %s\n",
  2293. tev->args[i].value);
  2294. break;
  2295. }
  2296. out:
  2297. free(buf);
  2298. }
  2299. /* Set new name from original perf_probe_event and namelist */
  2300. static int probe_trace_event__set_name(struct probe_trace_event *tev,
  2301. struct perf_probe_event *pev,
  2302. struct strlist *namelist,
  2303. bool allow_suffix)
  2304. {
  2305. const char *event, *group;
  2306. char buf[64];
  2307. int ret;
  2308. /* If probe_event or trace_event already have the name, reuse it */
  2309. if (pev->event && !pev->sdt)
  2310. event = pev->event;
  2311. else if (tev->event)
  2312. event = tev->event;
  2313. else {
  2314. /* Or generate new one from probe point */
  2315. if (pev->point.function &&
  2316. (strncmp(pev->point.function, "0x", 2) != 0) &&
  2317. !strisglob(pev->point.function))
  2318. event = pev->point.function;
  2319. else
  2320. event = tev->point.realname;
  2321. }
  2322. if (pev->group && !pev->sdt)
  2323. group = pev->group;
  2324. else if (tev->group)
  2325. group = tev->group;
  2326. else
  2327. group = PERFPROBE_GROUP;
  2328. /* Get an unused new event name */
  2329. ret = get_new_event_name(buf, 64, event,
  2330. namelist, allow_suffix);
  2331. if (ret < 0)
  2332. return ret;
  2333. event = buf;
  2334. tev->event = strdup(event);
  2335. tev->group = strdup(group);
  2336. if (tev->event == NULL || tev->group == NULL)
  2337. return -ENOMEM;
  2338. /* Add added event name to namelist */
  2339. strlist__add(namelist, event);
  2340. return 0;
  2341. }
  2342. static int __open_probe_file_and_namelist(bool uprobe,
  2343. struct strlist **namelist)
  2344. {
  2345. int fd;
  2346. fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
  2347. if (fd < 0)
  2348. return fd;
  2349. /* Get current event names */
  2350. *namelist = probe_file__get_namelist(fd);
  2351. if (!(*namelist)) {
  2352. pr_debug("Failed to get current event list.\n");
  2353. close(fd);
  2354. return -ENOMEM;
  2355. }
  2356. return fd;
  2357. }
  2358. static int __add_probe_trace_events(struct perf_probe_event *pev,
  2359. struct probe_trace_event *tevs,
  2360. int ntevs, bool allow_suffix)
  2361. {
  2362. int i, fd[2] = {-1, -1}, up, ret;
  2363. struct probe_trace_event *tev = NULL;
  2364. struct probe_cache *cache = NULL;
  2365. struct strlist *namelist[2] = {NULL, NULL};
  2366. up = pev->uprobes ? 1 : 0;
  2367. fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
  2368. if (fd[up] < 0)
  2369. return fd[up];
  2370. ret = 0;
  2371. for (i = 0; i < ntevs; i++) {
  2372. tev = &tevs[i];
  2373. up = tev->uprobes ? 1 : 0;
  2374. if (fd[up] == -1) { /* Open the kprobe/uprobe_events */
  2375. fd[up] = __open_probe_file_and_namelist(up,
  2376. &namelist[up]);
  2377. if (fd[up] < 0)
  2378. goto close_out;
  2379. }
  2380. /* Skip if the symbol is out of .text or blacklisted */
  2381. if (!tev->point.symbol && !pev->uprobes)
  2382. continue;
  2383. /* Set new name for tev (and update namelist) */
  2384. ret = probe_trace_event__set_name(tev, pev, namelist[up],
  2385. allow_suffix);
  2386. if (ret < 0)
  2387. break;
  2388. ret = probe_file__add_event(fd[up], tev);
  2389. if (ret < 0)
  2390. break;
  2391. /*
  2392. * Probes after the first probe which comes from same
  2393. * user input are always allowed to add suffix, because
  2394. * there might be several addresses corresponding to
  2395. * one code line.
  2396. */
  2397. allow_suffix = true;
  2398. }
  2399. if (ret == -EINVAL && pev->uprobes)
  2400. warn_uprobe_event_compat(tev);
  2401. if (ret == 0 && probe_conf.cache) {
  2402. cache = probe_cache__new(pev->target);
  2403. if (!cache ||
  2404. probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
  2405. probe_cache__commit(cache) < 0)
  2406. pr_warning("Failed to add event to probe cache\n");
  2407. probe_cache__delete(cache);
  2408. }
  2409. close_out:
  2410. for (up = 0; up < 2; up++) {
  2411. strlist__delete(namelist[up]);
  2412. if (fd[up] >= 0)
  2413. close(fd[up]);
  2414. }
  2415. return ret;
  2416. }
  2417. static int find_probe_functions(struct map *map, char *name,
  2418. struct symbol **syms)
  2419. {
  2420. int found = 0;
  2421. struct symbol *sym;
  2422. struct rb_node *tmp;
  2423. if (map__load(map) < 0)
  2424. return 0;
  2425. map__for_each_symbol(map, sym, tmp) {
  2426. if (strglobmatch(sym->name, name)) {
  2427. found++;
  2428. if (syms && found < probe_conf.max_probes)
  2429. syms[found - 1] = sym;
  2430. }
  2431. }
  2432. return found;
  2433. }
  2434. void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
  2435. struct probe_trace_event *tev __maybe_unused,
  2436. struct map *map __maybe_unused,
  2437. struct symbol *sym __maybe_unused) { }
  2438. /*
  2439. * Find probe function addresses from map.
  2440. * Return an error or the number of found probe_trace_event
  2441. */
  2442. static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
  2443. struct probe_trace_event **tevs)
  2444. {
  2445. struct map *map = NULL;
  2446. struct ref_reloc_sym *reloc_sym = NULL;
  2447. struct symbol *sym;
  2448. struct symbol **syms = NULL;
  2449. struct probe_trace_event *tev;
  2450. struct perf_probe_point *pp = &pev->point;
  2451. struct probe_trace_point *tp;
  2452. int num_matched_functions;
  2453. int ret, i, j, skipped = 0;
  2454. char *mod_name;
  2455. map = get_target_map(pev->target, pev->uprobes);
  2456. if (!map) {
  2457. ret = -EINVAL;
  2458. goto out;
  2459. }
  2460. syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
  2461. if (!syms) {
  2462. ret = -ENOMEM;
  2463. goto out;
  2464. }
  2465. /*
  2466. * Load matched symbols: Since the different local symbols may have
  2467. * same name but different addresses, this lists all the symbols.
  2468. */
  2469. num_matched_functions = find_probe_functions(map, pp->function, syms);
  2470. if (num_matched_functions == 0) {
  2471. pr_err("Failed to find symbol %s in %s\n", pp->function,
  2472. pev->target ? : "kernel");
  2473. ret = -ENOENT;
  2474. goto out;
  2475. } else if (num_matched_functions > probe_conf.max_probes) {
  2476. pr_err("Too many functions matched in %s\n",
  2477. pev->target ? : "kernel");
  2478. ret = -E2BIG;
  2479. goto out;
  2480. }
  2481. /* Note that the symbols in the kmodule are not relocated */
  2482. if (!pev->uprobes && !pp->retprobe && !pev->target) {
  2483. reloc_sym = kernel_get_ref_reloc_sym();
  2484. if (!reloc_sym) {
  2485. pr_warning("Relocated base symbol is not found!\n");
  2486. ret = -EINVAL;
  2487. goto out;
  2488. }
  2489. }
  2490. /* Setup result trace-probe-events */
  2491. *tevs = zalloc(sizeof(*tev) * num_matched_functions);
  2492. if (!*tevs) {
  2493. ret = -ENOMEM;
  2494. goto out;
  2495. }
  2496. ret = 0;
  2497. for (j = 0; j < num_matched_functions; j++) {
  2498. sym = syms[j];
  2499. tev = (*tevs) + ret;
  2500. tp = &tev->point;
  2501. if (ret == num_matched_functions) {
  2502. pr_warning("Too many symbols are listed. Skip it.\n");
  2503. break;
  2504. }
  2505. ret++;
  2506. if (pp->offset > sym->end - sym->start) {
  2507. pr_warning("Offset %ld is bigger than the size of %s\n",
  2508. pp->offset, sym->name);
  2509. ret = -ENOENT;
  2510. goto err_out;
  2511. }
  2512. /* Add one probe point */
  2513. tp->address = map->unmap_ip(map, sym->start) + pp->offset;
  2514. /* Check the kprobe (not in module) is within .text */
  2515. if (!pev->uprobes && !pev->target &&
  2516. kprobe_warn_out_range(sym->name, tp->address)) {
  2517. tp->symbol = NULL; /* Skip it */
  2518. skipped++;
  2519. } else if (reloc_sym) {
  2520. tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
  2521. tp->offset = tp->address - reloc_sym->addr;
  2522. } else {
  2523. tp->symbol = strdup_or_goto(sym->name, nomem_out);
  2524. tp->offset = pp->offset;
  2525. }
  2526. tp->realname = strdup_or_goto(sym->name, nomem_out);
  2527. tp->retprobe = pp->retprobe;
  2528. if (pev->target) {
  2529. if (pev->uprobes) {
  2530. tev->point.module = strdup_or_goto(pev->target,
  2531. nomem_out);
  2532. } else {
  2533. mod_name = find_module_name(pev->target);
  2534. tev->point.module =
  2535. strdup(mod_name ? mod_name : pev->target);
  2536. free(mod_name);
  2537. if (!tev->point.module)
  2538. goto nomem_out;
  2539. }
  2540. }
  2541. tev->uprobes = pev->uprobes;
  2542. tev->nargs = pev->nargs;
  2543. if (tev->nargs) {
  2544. tev->args = zalloc(sizeof(struct probe_trace_arg) *
  2545. tev->nargs);
  2546. if (tev->args == NULL)
  2547. goto nomem_out;
  2548. }
  2549. for (i = 0; i < tev->nargs; i++) {
  2550. if (pev->args[i].name)
  2551. tev->args[i].name =
  2552. strdup_or_goto(pev->args[i].name,
  2553. nomem_out);
  2554. tev->args[i].value = strdup_or_goto(pev->args[i].var,
  2555. nomem_out);
  2556. if (pev->args[i].type)
  2557. tev->args[i].type =
  2558. strdup_or_goto(pev->args[i].type,
  2559. nomem_out);
  2560. }
  2561. arch__fix_tev_from_maps(pev, tev, map, sym);
  2562. }
  2563. if (ret == skipped) {
  2564. ret = -ENOENT;
  2565. goto err_out;
  2566. }
  2567. out:
  2568. map__put(map);
  2569. free(syms);
  2570. return ret;
  2571. nomem_out:
  2572. ret = -ENOMEM;
  2573. err_out:
  2574. clear_probe_trace_events(*tevs, num_matched_functions);
  2575. zfree(tevs);
  2576. goto out;
  2577. }
  2578. static int try_to_find_absolute_address(struct perf_probe_event *pev,
  2579. struct probe_trace_event **tevs)
  2580. {
  2581. struct perf_probe_point *pp = &pev->point;
  2582. struct probe_trace_event *tev;
  2583. struct probe_trace_point *tp;
  2584. int i, err;
  2585. if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
  2586. return -EINVAL;
  2587. if (perf_probe_event_need_dwarf(pev))
  2588. return -EINVAL;
  2589. /*
  2590. * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
  2591. * absolute address.
  2592. *
  2593. * Only one tev can be generated by this.
  2594. */
  2595. *tevs = zalloc(sizeof(*tev));
  2596. if (!*tevs)
  2597. return -ENOMEM;
  2598. tev = *tevs;
  2599. tp = &tev->point;
  2600. /*
  2601. * Don't use tp->offset, use address directly, because
  2602. * in synthesize_probe_trace_command() address cannot be
  2603. * zero.
  2604. */
  2605. tp->address = pev->point.abs_address;
  2606. tp->retprobe = pp->retprobe;
  2607. tev->uprobes = pev->uprobes;
  2608. err = -ENOMEM;
  2609. /*
  2610. * Give it a '0x' leading symbol name.
  2611. * In __add_probe_trace_events, a NULL symbol is interpreted as
  2612. * invalud.
  2613. */
  2614. if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
  2615. goto errout;
  2616. /* For kprobe, check range */
  2617. if ((!tev->uprobes) &&
  2618. (kprobe_warn_out_range(tev->point.symbol,
  2619. tev->point.address))) {
  2620. err = -EACCES;
  2621. goto errout;
  2622. }
  2623. if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
  2624. goto errout;
  2625. if (pev->target) {
  2626. tp->module = strdup(pev->target);
  2627. if (!tp->module)
  2628. goto errout;
  2629. }
  2630. if (tev->group) {
  2631. tev->group = strdup(pev->group);
  2632. if (!tev->group)
  2633. goto errout;
  2634. }
  2635. if (pev->event) {
  2636. tev->event = strdup(pev->event);
  2637. if (!tev->event)
  2638. goto errout;
  2639. }
  2640. tev->nargs = pev->nargs;
  2641. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  2642. if (!tev->args) {
  2643. err = -ENOMEM;
  2644. goto errout;
  2645. }
  2646. for (i = 0; i < tev->nargs; i++)
  2647. copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
  2648. return 1;
  2649. errout:
  2650. if (*tevs) {
  2651. clear_probe_trace_events(*tevs, 1);
  2652. *tevs = NULL;
  2653. }
  2654. return err;
  2655. }
  2656. /* Concatinate two arrays */
  2657. static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
  2658. {
  2659. void *ret;
  2660. ret = malloc(sz_a + sz_b);
  2661. if (ret) {
  2662. memcpy(ret, a, sz_a);
  2663. memcpy(ret + sz_a, b, sz_b);
  2664. }
  2665. return ret;
  2666. }
  2667. static int
  2668. concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
  2669. struct probe_trace_event **tevs2, int ntevs2)
  2670. {
  2671. struct probe_trace_event *new_tevs;
  2672. int ret = 0;
  2673. if (*ntevs == 0) {
  2674. *tevs = *tevs2;
  2675. *ntevs = ntevs2;
  2676. *tevs2 = NULL;
  2677. return 0;
  2678. }
  2679. if (*ntevs + ntevs2 > probe_conf.max_probes)
  2680. ret = -E2BIG;
  2681. else {
  2682. /* Concatinate the array of probe_trace_event */
  2683. new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
  2684. *tevs2, ntevs2 * sizeof(**tevs2));
  2685. if (!new_tevs)
  2686. ret = -ENOMEM;
  2687. else {
  2688. free(*tevs);
  2689. *tevs = new_tevs;
  2690. *ntevs += ntevs2;
  2691. }
  2692. }
  2693. if (ret < 0)
  2694. clear_probe_trace_events(*tevs2, ntevs2);
  2695. zfree(tevs2);
  2696. return ret;
  2697. }
  2698. /*
  2699. * Try to find probe_trace_event from given probe caches. Return the number
  2700. * of cached events found, if an error occurs return the error.
  2701. */
  2702. static int find_cached_events(struct perf_probe_event *pev,
  2703. struct probe_trace_event **tevs,
  2704. const char *target)
  2705. {
  2706. struct probe_cache *cache;
  2707. struct probe_cache_entry *entry;
  2708. struct probe_trace_event *tmp_tevs = NULL;
  2709. int ntevs = 0;
  2710. int ret = 0;
  2711. cache = probe_cache__new(target);
  2712. /* Return 0 ("not found") if the target has no probe cache. */
  2713. if (!cache)
  2714. return 0;
  2715. for_each_probe_cache_entry(entry, cache) {
  2716. /* Skip the cache entry which has no name */
  2717. if (!entry->pev.event || !entry->pev.group)
  2718. continue;
  2719. if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
  2720. strglobmatch(entry->pev.event, pev->event)) {
  2721. ret = probe_cache_entry__get_event(entry, &tmp_tevs);
  2722. if (ret > 0)
  2723. ret = concat_probe_trace_events(tevs, &ntevs,
  2724. &tmp_tevs, ret);
  2725. if (ret < 0)
  2726. break;
  2727. }
  2728. }
  2729. probe_cache__delete(cache);
  2730. if (ret < 0) {
  2731. clear_probe_trace_events(*tevs, ntevs);
  2732. zfree(tevs);
  2733. } else {
  2734. ret = ntevs;
  2735. if (ntevs > 0 && target && target[0] == '/')
  2736. pev->uprobes = true;
  2737. }
  2738. return ret;
  2739. }
  2740. /* Try to find probe_trace_event from all probe caches */
  2741. static int find_cached_events_all(struct perf_probe_event *pev,
  2742. struct probe_trace_event **tevs)
  2743. {
  2744. struct probe_trace_event *tmp_tevs = NULL;
  2745. struct strlist *bidlist;
  2746. struct str_node *nd;
  2747. char *pathname;
  2748. int ntevs = 0;
  2749. int ret;
  2750. /* Get the buildid list of all valid caches */
  2751. bidlist = build_id_cache__list_all(true);
  2752. if (!bidlist) {
  2753. ret = -errno;
  2754. pr_debug("Failed to get buildids: %d\n", ret);
  2755. return ret;
  2756. }
  2757. ret = 0;
  2758. strlist__for_each_entry(nd, bidlist) {
  2759. pathname = build_id_cache__origname(nd->s);
  2760. ret = find_cached_events(pev, &tmp_tevs, pathname);
  2761. /* In the case of cnt == 0, we just skip it */
  2762. if (ret > 0)
  2763. ret = concat_probe_trace_events(tevs, &ntevs,
  2764. &tmp_tevs, ret);
  2765. free(pathname);
  2766. if (ret < 0)
  2767. break;
  2768. }
  2769. strlist__delete(bidlist);
  2770. if (ret < 0) {
  2771. clear_probe_trace_events(*tevs, ntevs);
  2772. zfree(tevs);
  2773. } else
  2774. ret = ntevs;
  2775. return ret;
  2776. }
  2777. static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
  2778. struct probe_trace_event **tevs)
  2779. {
  2780. struct probe_cache *cache;
  2781. struct probe_cache_entry *entry;
  2782. struct probe_trace_event *tev;
  2783. struct str_node *node;
  2784. int ret, i;
  2785. if (pev->sdt) {
  2786. /* For SDT/cached events, we use special search functions */
  2787. if (!pev->target)
  2788. return find_cached_events_all(pev, tevs);
  2789. else
  2790. return find_cached_events(pev, tevs, pev->target);
  2791. }
  2792. cache = probe_cache__new(pev->target);
  2793. if (!cache)
  2794. return 0;
  2795. entry = probe_cache__find(cache, pev);
  2796. if (!entry) {
  2797. /* SDT must be in the cache */
  2798. ret = pev->sdt ? -ENOENT : 0;
  2799. goto out;
  2800. }
  2801. ret = strlist__nr_entries(entry->tevlist);
  2802. if (ret > probe_conf.max_probes) {
  2803. pr_debug("Too many entries matched in the cache of %s\n",
  2804. pev->target ? : "kernel");
  2805. ret = -E2BIG;
  2806. goto out;
  2807. }
  2808. *tevs = zalloc(ret * sizeof(*tev));
  2809. if (!*tevs) {
  2810. ret = -ENOMEM;
  2811. goto out;
  2812. }
  2813. i = 0;
  2814. strlist__for_each_entry(node, entry->tevlist) {
  2815. tev = &(*tevs)[i++];
  2816. ret = parse_probe_trace_command(node->s, tev);
  2817. if (ret < 0)
  2818. goto out;
  2819. /* Set the uprobes attribute as same as original */
  2820. tev->uprobes = pev->uprobes;
  2821. }
  2822. ret = i;
  2823. out:
  2824. probe_cache__delete(cache);
  2825. return ret;
  2826. }
  2827. static int convert_to_probe_trace_events(struct perf_probe_event *pev,
  2828. struct probe_trace_event **tevs)
  2829. {
  2830. int ret;
  2831. if (!pev->group && !pev->sdt) {
  2832. /* Set group name if not given */
  2833. if (!pev->uprobes) {
  2834. pev->group = strdup(PERFPROBE_GROUP);
  2835. ret = pev->group ? 0 : -ENOMEM;
  2836. } else
  2837. ret = convert_exec_to_group(pev->target, &pev->group);
  2838. if (ret != 0) {
  2839. pr_warning("Failed to make a group name.\n");
  2840. return ret;
  2841. }
  2842. }
  2843. ret = try_to_find_absolute_address(pev, tevs);
  2844. if (ret > 0)
  2845. return ret;
  2846. /* At first, we need to lookup cache entry */
  2847. ret = find_probe_trace_events_from_cache(pev, tevs);
  2848. if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */
  2849. return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
  2850. /* Convert perf_probe_event with debuginfo */
  2851. ret = try_to_find_probe_trace_events(pev, tevs);
  2852. if (ret != 0)
  2853. return ret; /* Found in debuginfo or got an error */
  2854. return find_probe_trace_events_from_map(pev, tevs);
  2855. }
  2856. int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
  2857. {
  2858. int i, ret;
  2859. /* Loop 1: convert all events */
  2860. for (i = 0; i < npevs; i++) {
  2861. /* Init kprobe blacklist if needed */
  2862. if (!pevs[i].uprobes)
  2863. kprobe_blacklist__init();
  2864. /* Convert with or without debuginfo */
  2865. ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
  2866. if (ret < 0)
  2867. return ret;
  2868. pevs[i].ntevs = ret;
  2869. }
  2870. /* This just release blacklist only if allocated */
  2871. kprobe_blacklist__release();
  2872. return 0;
  2873. }
  2874. static int show_probe_trace_event(struct probe_trace_event *tev)
  2875. {
  2876. char *buf = synthesize_probe_trace_command(tev);
  2877. if (!buf) {
  2878. pr_debug("Failed to synthesize probe trace event.\n");
  2879. return -EINVAL;
  2880. }
  2881. /* Showing definition always go stdout */
  2882. printf("%s\n", buf);
  2883. free(buf);
  2884. return 0;
  2885. }
  2886. int show_probe_trace_events(struct perf_probe_event *pevs, int npevs)
  2887. {
  2888. struct strlist *namelist = strlist__new(NULL, NULL);
  2889. struct probe_trace_event *tev;
  2890. struct perf_probe_event *pev;
  2891. int i, j, ret = 0;
  2892. if (!namelist)
  2893. return -ENOMEM;
  2894. for (j = 0; j < npevs && !ret; j++) {
  2895. pev = &pevs[j];
  2896. for (i = 0; i < pev->ntevs && !ret; i++) {
  2897. tev = &pev->tevs[i];
  2898. /* Skip if the symbol is out of .text or blacklisted */
  2899. if (!tev->point.symbol && !pev->uprobes)
  2900. continue;
  2901. /* Set new name for tev (and update namelist) */
  2902. ret = probe_trace_event__set_name(tev, pev,
  2903. namelist, true);
  2904. if (!ret)
  2905. ret = show_probe_trace_event(tev);
  2906. }
  2907. }
  2908. strlist__delete(namelist);
  2909. return ret;
  2910. }
  2911. int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
  2912. {
  2913. int i, ret = 0;
  2914. /* Loop 2: add all events */
  2915. for (i = 0; i < npevs; i++) {
  2916. ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
  2917. pevs[i].ntevs,
  2918. probe_conf.force_add);
  2919. if (ret < 0)
  2920. break;
  2921. }
  2922. return ret;
  2923. }
  2924. void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
  2925. {
  2926. int i, j;
  2927. /* Loop 3: cleanup and free trace events */
  2928. for (i = 0; i < npevs; i++) {
  2929. for (j = 0; j < pevs[i].ntevs; j++)
  2930. clear_probe_trace_event(&pevs[i].tevs[j]);
  2931. zfree(&pevs[i].tevs);
  2932. pevs[i].ntevs = 0;
  2933. clear_perf_probe_event(&pevs[i]);
  2934. }
  2935. }
  2936. int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
  2937. {
  2938. int ret;
  2939. ret = init_probe_symbol_maps(pevs->uprobes);
  2940. if (ret < 0)
  2941. return ret;
  2942. ret = convert_perf_probe_events(pevs, npevs);
  2943. if (ret == 0)
  2944. ret = apply_perf_probe_events(pevs, npevs);
  2945. cleanup_perf_probe_events(pevs, npevs);
  2946. exit_probe_symbol_maps();
  2947. return ret;
  2948. }
  2949. int del_perf_probe_events(struct strfilter *filter)
  2950. {
  2951. int ret, ret2, ufd = -1, kfd = -1;
  2952. char *str = strfilter__string(filter);
  2953. if (!str)
  2954. return -EINVAL;
  2955. /* Get current event names */
  2956. ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
  2957. if (ret < 0)
  2958. goto out;
  2959. ret = probe_file__del_events(kfd, filter);
  2960. if (ret < 0 && ret != -ENOENT)
  2961. goto error;
  2962. ret2 = probe_file__del_events(ufd, filter);
  2963. if (ret2 < 0 && ret2 != -ENOENT) {
  2964. ret = ret2;
  2965. goto error;
  2966. }
  2967. ret = 0;
  2968. error:
  2969. if (kfd >= 0)
  2970. close(kfd);
  2971. if (ufd >= 0)
  2972. close(ufd);
  2973. out:
  2974. free(str);
  2975. return ret;
  2976. }
  2977. int show_available_funcs(const char *target, struct strfilter *_filter,
  2978. bool user)
  2979. {
  2980. struct rb_node *nd;
  2981. struct map *map;
  2982. int ret;
  2983. ret = init_probe_symbol_maps(user);
  2984. if (ret < 0)
  2985. return ret;
  2986. /* Get a symbol map */
  2987. map = get_target_map(target, user);
  2988. if (!map) {
  2989. pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
  2990. return -EINVAL;
  2991. }
  2992. ret = map__load(map);
  2993. if (ret) {
  2994. if (ret == -2) {
  2995. char *str = strfilter__string(_filter);
  2996. pr_err("Failed to find symbols matched to \"%s\"\n",
  2997. str);
  2998. free(str);
  2999. } else
  3000. pr_err("Failed to load symbols in %s\n",
  3001. (target) ? : "kernel");
  3002. goto end;
  3003. }
  3004. if (!dso__sorted_by_name(map->dso, map->type))
  3005. dso__sort_by_name(map->dso, map->type);
  3006. /* Show all (filtered) symbols */
  3007. setup_pager();
  3008. for (nd = rb_first(&map->dso->symbol_names[map->type]); nd; nd = rb_next(nd)) {
  3009. struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
  3010. if (strfilter__compare(_filter, pos->sym.name))
  3011. printf("%s\n", pos->sym.name);
  3012. }
  3013. end:
  3014. map__put(map);
  3015. exit_probe_symbol_maps();
  3016. return ret;
  3017. }
  3018. int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
  3019. struct perf_probe_arg *pvar)
  3020. {
  3021. tvar->value = strdup(pvar->var);
  3022. if (tvar->value == NULL)
  3023. return -ENOMEM;
  3024. if (pvar->type) {
  3025. tvar->type = strdup(pvar->type);
  3026. if (tvar->type == NULL)
  3027. return -ENOMEM;
  3028. }
  3029. if (pvar->name) {
  3030. tvar->name = strdup(pvar->name);
  3031. if (tvar->name == NULL)
  3032. return -ENOMEM;
  3033. } else
  3034. tvar->name = NULL;
  3035. return 0;
  3036. }