probe-event.c 67 KB

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