probe-event.c 81 KB

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