probe-event.c 81 KB

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