probe-event.c 84 KB

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