probe-event.c 80 KB

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