probe-event.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950
  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. #ifdef HAVE_DWARF_SUPPORT
  228. static int kernel_get_module_dso(const char *module, struct dso **pdso)
  229. {
  230. struct dso *dso;
  231. struct map *map;
  232. const char *vmlinux_name;
  233. int ret = 0;
  234. if (module) {
  235. char module_name[128];
  236. snprintf(module_name, sizeof(module_name), "[%s]", module);
  237. map = map_groups__find_by_name(&host_machine->kmaps, MAP__FUNCTION, module_name);
  238. if (map) {
  239. dso = map->dso;
  240. goto found;
  241. }
  242. pr_debug("Failed to find module %s.\n", module);
  243. return -ENOENT;
  244. }
  245. map = machine__kernel_map(host_machine);
  246. dso = map->dso;
  247. vmlinux_name = symbol_conf.vmlinux_name;
  248. dso->load_errno = 0;
  249. if (vmlinux_name)
  250. ret = dso__load_vmlinux(dso, map, vmlinux_name, false, NULL);
  251. else
  252. ret = dso__load_vmlinux_path(dso, map, NULL);
  253. found:
  254. *pdso = dso;
  255. return ret;
  256. }
  257. /*
  258. * Some binaries like glibc have special symbols which are on the symbol
  259. * table, but not in the debuginfo. If we can find the address of the
  260. * symbol from map, we can translate the address back to the probe point.
  261. */
  262. static int find_alternative_probe_point(struct debuginfo *dinfo,
  263. struct perf_probe_point *pp,
  264. struct perf_probe_point *result,
  265. const char *target, bool uprobes)
  266. {
  267. struct map *map = NULL;
  268. struct symbol *sym;
  269. u64 address = 0;
  270. int ret = -ENOENT;
  271. /* This can work only for function-name based one */
  272. if (!pp->function || pp->file)
  273. return -ENOTSUP;
  274. map = get_target_map(target, uprobes);
  275. if (!map)
  276. return -EINVAL;
  277. /* Find the address of given function */
  278. map__for_each_symbol_by_name(map, pp->function, sym) {
  279. if (uprobes)
  280. address = sym->start;
  281. else
  282. address = map->unmap_ip(map, sym->start);
  283. break;
  284. }
  285. if (!address) {
  286. ret = -ENOENT;
  287. goto out;
  288. }
  289. pr_debug("Symbol %s address found : %" PRIx64 "\n",
  290. pp->function, address);
  291. ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
  292. result);
  293. if (ret <= 0)
  294. ret = (!ret) ? -ENOENT : ret;
  295. else {
  296. result->offset += pp->offset;
  297. result->line += pp->line;
  298. result->retprobe = pp->retprobe;
  299. ret = 0;
  300. }
  301. out:
  302. put_target_map(map, uprobes);
  303. return ret;
  304. }
  305. static int get_alternative_probe_event(struct debuginfo *dinfo,
  306. struct perf_probe_event *pev,
  307. struct perf_probe_point *tmp)
  308. {
  309. int ret;
  310. memcpy(tmp, &pev->point, sizeof(*tmp));
  311. memset(&pev->point, 0, sizeof(pev->point));
  312. ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
  313. pev->target, pev->uprobes);
  314. if (ret < 0)
  315. memcpy(&pev->point, tmp, sizeof(*tmp));
  316. return ret;
  317. }
  318. static int get_alternative_line_range(struct debuginfo *dinfo,
  319. struct line_range *lr,
  320. const char *target, bool user)
  321. {
  322. struct perf_probe_point pp = { .function = lr->function,
  323. .file = lr->file,
  324. .line = lr->start };
  325. struct perf_probe_point result;
  326. int ret, len = 0;
  327. memset(&result, 0, sizeof(result));
  328. if (lr->end != INT_MAX)
  329. len = lr->end - lr->start;
  330. ret = find_alternative_probe_point(dinfo, &pp, &result,
  331. target, user);
  332. if (!ret) {
  333. lr->function = result.function;
  334. lr->file = result.file;
  335. lr->start = result.line;
  336. if (lr->end != INT_MAX)
  337. lr->end = lr->start + len;
  338. clear_perf_probe_point(&pp);
  339. }
  340. return ret;
  341. }
  342. /* Open new debuginfo of given module */
  343. static struct debuginfo *open_debuginfo(const char *module, bool silent)
  344. {
  345. const char *path = module;
  346. char reason[STRERR_BUFSIZE];
  347. struct debuginfo *ret = NULL;
  348. struct dso *dso = NULL;
  349. int err;
  350. if (!module || !strchr(module, '/')) {
  351. err = kernel_get_module_dso(module, &dso);
  352. if (err < 0) {
  353. if (!dso || dso->load_errno == 0) {
  354. if (!strerror_r(-err, reason, STRERR_BUFSIZE))
  355. strcpy(reason, "(unknown)");
  356. } else
  357. dso__strerror_load(dso, reason, STRERR_BUFSIZE);
  358. if (!silent)
  359. pr_err("Failed to find the path for %s: %s\n",
  360. module ?: "kernel", reason);
  361. return NULL;
  362. }
  363. path = dso->long_name;
  364. }
  365. ret = debuginfo__new(path);
  366. if (!ret && !silent) {
  367. pr_warning("The %s file has no debug information.\n", path);
  368. if (!module || !strtailcmp(path, ".ko"))
  369. pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
  370. else
  371. pr_warning("Rebuild with -g, ");
  372. pr_warning("or install an appropriate debuginfo package.\n");
  373. }
  374. return ret;
  375. }
  376. /* For caching the last debuginfo */
  377. static struct debuginfo *debuginfo_cache;
  378. static char *debuginfo_cache_path;
  379. static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
  380. {
  381. const char *path = module;
  382. /* If the module is NULL, it should be the kernel. */
  383. if (!module)
  384. path = "kernel";
  385. if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
  386. goto out;
  387. /* Copy module path */
  388. free(debuginfo_cache_path);
  389. debuginfo_cache_path = strdup(path);
  390. if (!debuginfo_cache_path) {
  391. debuginfo__delete(debuginfo_cache);
  392. debuginfo_cache = NULL;
  393. goto out;
  394. }
  395. debuginfo_cache = open_debuginfo(module, silent);
  396. if (!debuginfo_cache)
  397. zfree(&debuginfo_cache_path);
  398. out:
  399. return debuginfo_cache;
  400. }
  401. static void debuginfo_cache__exit(void)
  402. {
  403. debuginfo__delete(debuginfo_cache);
  404. debuginfo_cache = NULL;
  405. zfree(&debuginfo_cache_path);
  406. }
  407. static int get_text_start_address(const char *exec, unsigned long *address)
  408. {
  409. Elf *elf;
  410. GElf_Ehdr ehdr;
  411. GElf_Shdr shdr;
  412. int fd, ret = -ENOENT;
  413. fd = open(exec, O_RDONLY);
  414. if (fd < 0)
  415. return -errno;
  416. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  417. if (elf == NULL)
  418. return -EINVAL;
  419. if (gelf_getehdr(elf, &ehdr) == NULL)
  420. goto out;
  421. if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
  422. goto out;
  423. *address = shdr.sh_addr - shdr.sh_offset;
  424. ret = 0;
  425. out:
  426. elf_end(elf);
  427. return ret;
  428. }
  429. /*
  430. * Convert trace point to probe point with debuginfo
  431. */
  432. static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
  433. struct perf_probe_point *pp,
  434. bool is_kprobe)
  435. {
  436. struct debuginfo *dinfo = NULL;
  437. unsigned long stext = 0;
  438. u64 addr = tp->address;
  439. int ret = -ENOENT;
  440. /* convert the address to dwarf address */
  441. if (!is_kprobe) {
  442. if (!addr) {
  443. ret = -EINVAL;
  444. goto error;
  445. }
  446. ret = get_text_start_address(tp->module, &stext);
  447. if (ret < 0)
  448. goto error;
  449. addr += stext;
  450. } else if (tp->symbol) {
  451. /* If the module is given, this returns relative address */
  452. ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
  453. false, !!tp->module);
  454. if (ret != 0)
  455. goto error;
  456. addr += tp->offset;
  457. }
  458. pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
  459. tp->module ? : "kernel");
  460. dinfo = debuginfo_cache__open(tp->module, verbose == 0);
  461. if (dinfo)
  462. ret = debuginfo__find_probe_point(dinfo,
  463. (unsigned long)addr, pp);
  464. else
  465. ret = -ENOENT;
  466. if (ret > 0) {
  467. pp->retprobe = tp->retprobe;
  468. return 0;
  469. }
  470. error:
  471. pr_debug("Failed to find corresponding probes from debuginfo.\n");
  472. return ret ? : -ENOENT;
  473. }
  474. static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
  475. int ntevs, const char *exec)
  476. {
  477. int i, ret = 0;
  478. unsigned long stext = 0;
  479. if (!exec)
  480. return 0;
  481. ret = get_text_start_address(exec, &stext);
  482. if (ret < 0)
  483. return ret;
  484. for (i = 0; i < ntevs && ret >= 0; i++) {
  485. /* point.address is the addres of point.symbol + point.offset */
  486. tevs[i].point.address -= stext;
  487. tevs[i].point.module = strdup(exec);
  488. if (!tevs[i].point.module) {
  489. ret = -ENOMEM;
  490. break;
  491. }
  492. tevs[i].uprobes = true;
  493. }
  494. return ret;
  495. }
  496. static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
  497. int ntevs, const char *module)
  498. {
  499. int i, ret = 0;
  500. char *tmp;
  501. if (!module)
  502. return 0;
  503. tmp = strrchr(module, '/');
  504. if (tmp) {
  505. /* This is a module path -- get the module name */
  506. module = strdup(tmp + 1);
  507. if (!module)
  508. return -ENOMEM;
  509. tmp = strchr(module, '.');
  510. if (tmp)
  511. *tmp = '\0';
  512. tmp = (char *)module; /* For free() */
  513. }
  514. for (i = 0; i < ntevs; i++) {
  515. tevs[i].point.module = strdup(module);
  516. if (!tevs[i].point.module) {
  517. ret = -ENOMEM;
  518. break;
  519. }
  520. }
  521. free(tmp);
  522. return ret;
  523. }
  524. /* Post processing the probe events */
  525. static int post_process_probe_trace_events(struct probe_trace_event *tevs,
  526. int ntevs, const char *module,
  527. bool uprobe)
  528. {
  529. struct ref_reloc_sym *reloc_sym;
  530. char *tmp;
  531. int i, skipped = 0;
  532. if (uprobe)
  533. return add_exec_to_probe_trace_events(tevs, ntevs, module);
  534. /* Note that currently ref_reloc_sym based probe is not for drivers */
  535. if (module)
  536. return add_module_to_probe_trace_events(tevs, ntevs, module);
  537. reloc_sym = kernel_get_ref_reloc_sym();
  538. if (!reloc_sym) {
  539. pr_warning("Relocated base symbol is not found!\n");
  540. return -EINVAL;
  541. }
  542. for (i = 0; i < ntevs; i++) {
  543. if (!tevs[i].point.address || tevs[i].point.retprobe)
  544. continue;
  545. /* If we found a wrong one, mark it by NULL symbol */
  546. if (kprobe_warn_out_range(tevs[i].point.symbol,
  547. tevs[i].point.address)) {
  548. tmp = NULL;
  549. skipped++;
  550. } else {
  551. tmp = strdup(reloc_sym->name);
  552. if (!tmp)
  553. return -ENOMEM;
  554. }
  555. /* If we have no realname, use symbol for it */
  556. if (!tevs[i].point.realname)
  557. tevs[i].point.realname = tevs[i].point.symbol;
  558. else
  559. free(tevs[i].point.symbol);
  560. tevs[i].point.symbol = tmp;
  561. tevs[i].point.offset = tevs[i].point.address -
  562. reloc_sym->unrelocated_addr;
  563. }
  564. return skipped;
  565. }
  566. /* Try to find perf_probe_event with debuginfo */
  567. static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
  568. struct probe_trace_event **tevs)
  569. {
  570. bool need_dwarf = perf_probe_event_need_dwarf(pev);
  571. struct perf_probe_point tmp;
  572. struct debuginfo *dinfo;
  573. int ntevs, ret = 0;
  574. dinfo = open_debuginfo(pev->target, !need_dwarf);
  575. if (!dinfo) {
  576. if (need_dwarf)
  577. return -ENOENT;
  578. pr_debug("Could not open debuginfo. Try to use symbols.\n");
  579. return 0;
  580. }
  581. pr_debug("Try to find probe point from debuginfo.\n");
  582. /* Searching trace events corresponding to a probe event */
  583. ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
  584. if (ntevs == 0) { /* Not found, retry with an alternative */
  585. ret = get_alternative_probe_event(dinfo, pev, &tmp);
  586. if (!ret) {
  587. ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
  588. /*
  589. * Write back to the original probe_event for
  590. * setting appropriate (user given) event name
  591. */
  592. clear_perf_probe_point(&pev->point);
  593. memcpy(&pev->point, &tmp, sizeof(tmp));
  594. }
  595. }
  596. debuginfo__delete(dinfo);
  597. if (ntevs > 0) { /* Succeeded to find trace events */
  598. pr_debug("Found %d probe_trace_events.\n", ntevs);
  599. ret = post_process_probe_trace_events(*tevs, ntevs,
  600. pev->target, pev->uprobes);
  601. if (ret < 0 || ret == ntevs) {
  602. clear_probe_trace_events(*tevs, ntevs);
  603. zfree(tevs);
  604. }
  605. if (ret != ntevs)
  606. return ret < 0 ? ret : ntevs;
  607. ntevs = 0;
  608. /* Fall through */
  609. }
  610. if (ntevs == 0) { /* No error but failed to find probe point. */
  611. pr_warning("Probe point '%s' not found.\n",
  612. synthesize_perf_probe_point(&pev->point));
  613. return -ENOENT;
  614. }
  615. /* Error path : ntevs < 0 */
  616. pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
  617. if (ntevs < 0) {
  618. if (ntevs == -EBADF)
  619. pr_warning("Warning: No dwarf info found in the vmlinux - "
  620. "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
  621. if (!need_dwarf) {
  622. pr_debug("Trying to use symbols.\n");
  623. return 0;
  624. }
  625. }
  626. return ntevs;
  627. }
  628. #define LINEBUF_SIZE 256
  629. #define NR_ADDITIONAL_LINES 2
  630. static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
  631. {
  632. char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
  633. const char *color = show_num ? "" : PERF_COLOR_BLUE;
  634. const char *prefix = NULL;
  635. do {
  636. if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
  637. goto error;
  638. if (skip)
  639. continue;
  640. if (!prefix) {
  641. prefix = show_num ? "%7d " : " ";
  642. color_fprintf(stdout, color, prefix, l);
  643. }
  644. color_fprintf(stdout, color, "%s", buf);
  645. } while (strchr(buf, '\n') == NULL);
  646. return 1;
  647. error:
  648. if (ferror(fp)) {
  649. pr_warning("File read error: %s\n",
  650. strerror_r(errno, sbuf, sizeof(sbuf)));
  651. return -1;
  652. }
  653. return 0;
  654. }
  655. static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
  656. {
  657. int rv = __show_one_line(fp, l, skip, show_num);
  658. if (rv == 0) {
  659. pr_warning("Source file is shorter than expected.\n");
  660. rv = -1;
  661. }
  662. return rv;
  663. }
  664. #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
  665. #define show_one_line(f,l) _show_one_line(f,l,false,false)
  666. #define skip_one_line(f,l) _show_one_line(f,l,true,false)
  667. #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
  668. /*
  669. * Show line-range always requires debuginfo to find source file and
  670. * line number.
  671. */
  672. static int __show_line_range(struct line_range *lr, const char *module,
  673. bool user)
  674. {
  675. int l = 1;
  676. struct int_node *ln;
  677. struct debuginfo *dinfo;
  678. FILE *fp;
  679. int ret;
  680. char *tmp;
  681. char sbuf[STRERR_BUFSIZE];
  682. /* Search a line range */
  683. dinfo = open_debuginfo(module, false);
  684. if (!dinfo)
  685. return -ENOENT;
  686. ret = debuginfo__find_line_range(dinfo, lr);
  687. if (!ret) { /* Not found, retry with an alternative */
  688. ret = get_alternative_line_range(dinfo, lr, module, user);
  689. if (!ret)
  690. ret = debuginfo__find_line_range(dinfo, lr);
  691. }
  692. debuginfo__delete(dinfo);
  693. if (ret == 0 || ret == -ENOENT) {
  694. pr_warning("Specified source line is not found.\n");
  695. return -ENOENT;
  696. } else if (ret < 0) {
  697. pr_warning("Debuginfo analysis failed.\n");
  698. return ret;
  699. }
  700. /* Convert source file path */
  701. tmp = lr->path;
  702. ret = get_real_path(tmp, lr->comp_dir, &lr->path);
  703. /* Free old path when new path is assigned */
  704. if (tmp != lr->path)
  705. free(tmp);
  706. if (ret < 0) {
  707. pr_warning("Failed to find source file path.\n");
  708. return ret;
  709. }
  710. setup_pager();
  711. if (lr->function)
  712. fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
  713. lr->start - lr->offset);
  714. else
  715. fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
  716. fp = fopen(lr->path, "r");
  717. if (fp == NULL) {
  718. pr_warning("Failed to open %s: %s\n", lr->path,
  719. strerror_r(errno, sbuf, sizeof(sbuf)));
  720. return -errno;
  721. }
  722. /* Skip to starting line number */
  723. while (l < lr->start) {
  724. ret = skip_one_line(fp, l++);
  725. if (ret < 0)
  726. goto end;
  727. }
  728. intlist__for_each(ln, lr->line_list) {
  729. for (; ln->i > l; l++) {
  730. ret = show_one_line(fp, l - lr->offset);
  731. if (ret < 0)
  732. goto end;
  733. }
  734. ret = show_one_line_with_num(fp, l++ - lr->offset);
  735. if (ret < 0)
  736. goto end;
  737. }
  738. if (lr->end == INT_MAX)
  739. lr->end = l + NR_ADDITIONAL_LINES;
  740. while (l <= lr->end) {
  741. ret = show_one_line_or_eof(fp, l++ - lr->offset);
  742. if (ret <= 0)
  743. break;
  744. }
  745. end:
  746. fclose(fp);
  747. return ret;
  748. }
  749. int show_line_range(struct line_range *lr, const char *module, bool user)
  750. {
  751. int ret;
  752. ret = init_probe_symbol_maps(user);
  753. if (ret < 0)
  754. return ret;
  755. ret = __show_line_range(lr, module, user);
  756. exit_probe_symbol_maps();
  757. return ret;
  758. }
  759. static int show_available_vars_at(struct debuginfo *dinfo,
  760. struct perf_probe_event *pev,
  761. struct strfilter *_filter)
  762. {
  763. char *buf;
  764. int ret, i, nvars;
  765. struct str_node *node;
  766. struct variable_list *vls = NULL, *vl;
  767. struct perf_probe_point tmp;
  768. const char *var;
  769. buf = synthesize_perf_probe_point(&pev->point);
  770. if (!buf)
  771. return -EINVAL;
  772. pr_debug("Searching variables at %s\n", buf);
  773. ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
  774. if (!ret) { /* Not found, retry with an alternative */
  775. ret = get_alternative_probe_event(dinfo, pev, &tmp);
  776. if (!ret) {
  777. ret = debuginfo__find_available_vars_at(dinfo, pev,
  778. &vls);
  779. /* Release the old probe_point */
  780. clear_perf_probe_point(&tmp);
  781. }
  782. }
  783. if (ret <= 0) {
  784. if (ret == 0 || ret == -ENOENT) {
  785. pr_err("Failed to find the address of %s\n", buf);
  786. ret = -ENOENT;
  787. } else
  788. pr_warning("Debuginfo analysis failed.\n");
  789. goto end;
  790. }
  791. /* Some variables are found */
  792. fprintf(stdout, "Available variables at %s\n", buf);
  793. for (i = 0; i < ret; i++) {
  794. vl = &vls[i];
  795. /*
  796. * A probe point might be converted to
  797. * several trace points.
  798. */
  799. fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
  800. vl->point.offset);
  801. zfree(&vl->point.symbol);
  802. nvars = 0;
  803. if (vl->vars) {
  804. strlist__for_each(node, vl->vars) {
  805. var = strchr(node->s, '\t') + 1;
  806. if (strfilter__compare(_filter, var)) {
  807. fprintf(stdout, "\t\t%s\n", node->s);
  808. nvars++;
  809. }
  810. }
  811. strlist__delete(vl->vars);
  812. }
  813. if (nvars == 0)
  814. fprintf(stdout, "\t\t(No matched variables)\n");
  815. }
  816. free(vls);
  817. end:
  818. free(buf);
  819. return ret;
  820. }
  821. /* Show available variables on given probe point */
  822. int show_available_vars(struct perf_probe_event *pevs, int npevs,
  823. struct strfilter *_filter)
  824. {
  825. int i, ret = 0;
  826. struct debuginfo *dinfo;
  827. ret = init_probe_symbol_maps(pevs->uprobes);
  828. if (ret < 0)
  829. return ret;
  830. dinfo = open_debuginfo(pevs->target, false);
  831. if (!dinfo) {
  832. ret = -ENOENT;
  833. goto out;
  834. }
  835. setup_pager();
  836. for (i = 0; i < npevs && ret >= 0; i++)
  837. ret = show_available_vars_at(dinfo, &pevs[i], _filter);
  838. debuginfo__delete(dinfo);
  839. out:
  840. exit_probe_symbol_maps();
  841. return ret;
  842. }
  843. #else /* !HAVE_DWARF_SUPPORT */
  844. static void debuginfo_cache__exit(void)
  845. {
  846. }
  847. static int
  848. find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
  849. struct perf_probe_point *pp __maybe_unused,
  850. bool is_kprobe __maybe_unused)
  851. {
  852. return -ENOSYS;
  853. }
  854. static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
  855. struct probe_trace_event **tevs __maybe_unused)
  856. {
  857. if (perf_probe_event_need_dwarf(pev)) {
  858. pr_warning("Debuginfo-analysis is not supported.\n");
  859. return -ENOSYS;
  860. }
  861. return 0;
  862. }
  863. int show_line_range(struct line_range *lr __maybe_unused,
  864. const char *module __maybe_unused,
  865. bool user __maybe_unused)
  866. {
  867. pr_warning("Debuginfo-analysis is not supported.\n");
  868. return -ENOSYS;
  869. }
  870. int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
  871. int npevs __maybe_unused,
  872. struct strfilter *filter __maybe_unused)
  873. {
  874. pr_warning("Debuginfo-analysis is not supported.\n");
  875. return -ENOSYS;
  876. }
  877. #endif
  878. void line_range__clear(struct line_range *lr)
  879. {
  880. free(lr->function);
  881. free(lr->file);
  882. free(lr->path);
  883. free(lr->comp_dir);
  884. intlist__delete(lr->line_list);
  885. memset(lr, 0, sizeof(*lr));
  886. }
  887. int line_range__init(struct line_range *lr)
  888. {
  889. memset(lr, 0, sizeof(*lr));
  890. lr->line_list = intlist__new(NULL);
  891. if (!lr->line_list)
  892. return -ENOMEM;
  893. else
  894. return 0;
  895. }
  896. static int parse_line_num(char **ptr, int *val, const char *what)
  897. {
  898. const char *start = *ptr;
  899. errno = 0;
  900. *val = strtol(*ptr, ptr, 0);
  901. if (errno || *ptr == start) {
  902. semantic_error("'%s' is not a valid number.\n", what);
  903. return -EINVAL;
  904. }
  905. return 0;
  906. }
  907. /* Check the name is good for event, group or function */
  908. static bool is_c_func_name(const char *name)
  909. {
  910. if (!isalpha(*name) && *name != '_')
  911. return false;
  912. while (*++name != '\0') {
  913. if (!isalpha(*name) && !isdigit(*name) && *name != '_')
  914. return false;
  915. }
  916. return true;
  917. }
  918. /*
  919. * Stuff 'lr' according to the line range described by 'arg'.
  920. * The line range syntax is described by:
  921. *
  922. * SRC[:SLN[+NUM|-ELN]]
  923. * FNC[@SRC][:SLN[+NUM|-ELN]]
  924. */
  925. int parse_line_range_desc(const char *arg, struct line_range *lr)
  926. {
  927. char *range, *file, *name = strdup(arg);
  928. int err;
  929. if (!name)
  930. return -ENOMEM;
  931. lr->start = 0;
  932. lr->end = INT_MAX;
  933. range = strchr(name, ':');
  934. if (range) {
  935. *range++ = '\0';
  936. err = parse_line_num(&range, &lr->start, "start line");
  937. if (err)
  938. goto err;
  939. if (*range == '+' || *range == '-') {
  940. const char c = *range++;
  941. err = parse_line_num(&range, &lr->end, "end line");
  942. if (err)
  943. goto err;
  944. if (c == '+') {
  945. lr->end += lr->start;
  946. /*
  947. * Adjust the number of lines here.
  948. * If the number of lines == 1, the
  949. * the end of line should be equal to
  950. * the start of line.
  951. */
  952. lr->end--;
  953. }
  954. }
  955. pr_debug("Line range is %d to %d\n", lr->start, lr->end);
  956. err = -EINVAL;
  957. if (lr->start > lr->end) {
  958. semantic_error("Start line must be smaller"
  959. " than end line.\n");
  960. goto err;
  961. }
  962. if (*range != '\0') {
  963. semantic_error("Tailing with invalid str '%s'.\n", range);
  964. goto err;
  965. }
  966. }
  967. file = strchr(name, '@');
  968. if (file) {
  969. *file = '\0';
  970. lr->file = strdup(++file);
  971. if (lr->file == NULL) {
  972. err = -ENOMEM;
  973. goto err;
  974. }
  975. lr->function = name;
  976. } else if (strchr(name, '/') || strchr(name, '.'))
  977. lr->file = name;
  978. else if (is_c_func_name(name))/* We reuse it for checking funcname */
  979. lr->function = name;
  980. else { /* Invalid name */
  981. semantic_error("'%s' is not a valid function name.\n", name);
  982. err = -EINVAL;
  983. goto err;
  984. }
  985. return 0;
  986. err:
  987. free(name);
  988. return err;
  989. }
  990. /* Parse probepoint definition. */
  991. static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
  992. {
  993. struct perf_probe_point *pp = &pev->point;
  994. char *ptr, *tmp;
  995. char c, nc = 0;
  996. bool file_spec = false;
  997. /*
  998. * <Syntax>
  999. * perf probe [EVENT=]SRC[:LN|;PTN]
  1000. * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
  1001. *
  1002. * TODO:Group name support
  1003. */
  1004. if (!arg)
  1005. return -EINVAL;
  1006. ptr = strpbrk(arg, ";=@+%");
  1007. if (ptr && *ptr == '=') { /* Event name */
  1008. *ptr = '\0';
  1009. tmp = ptr + 1;
  1010. if (strchr(arg, ':')) {
  1011. semantic_error("Group name is not supported yet.\n");
  1012. return -ENOTSUP;
  1013. }
  1014. if (!is_c_func_name(arg)) {
  1015. semantic_error("%s is bad for event name -it must "
  1016. "follow C symbol-naming rule.\n", arg);
  1017. return -EINVAL;
  1018. }
  1019. pev->event = strdup(arg);
  1020. if (pev->event == NULL)
  1021. return -ENOMEM;
  1022. pev->group = NULL;
  1023. arg = tmp;
  1024. }
  1025. /*
  1026. * Check arg is function or file name and copy it.
  1027. *
  1028. * We consider arg to be a file spec if and only if it satisfies
  1029. * all of the below criteria::
  1030. * - it does not include any of "+@%",
  1031. * - it includes one of ":;", and
  1032. * - it has a period '.' in the name.
  1033. *
  1034. * Otherwise, we consider arg to be a function specification.
  1035. */
  1036. if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
  1037. /* This is a file spec if it includes a '.' before ; or : */
  1038. if (memchr(arg, '.', ptr - arg))
  1039. file_spec = true;
  1040. }
  1041. ptr = strpbrk(arg, ";:+@%");
  1042. if (ptr) {
  1043. nc = *ptr;
  1044. *ptr++ = '\0';
  1045. }
  1046. if (arg[0] == '\0')
  1047. tmp = NULL;
  1048. else {
  1049. tmp = strdup(arg);
  1050. if (tmp == NULL)
  1051. return -ENOMEM;
  1052. }
  1053. if (file_spec)
  1054. pp->file = tmp;
  1055. else {
  1056. pp->function = tmp;
  1057. /*
  1058. * Keep pp->function even if this is absolute address,
  1059. * so it can mark whether abs_address is valid.
  1060. * Which make 'perf probe lib.bin 0x0' possible.
  1061. *
  1062. * Note that checking length of tmp is not needed
  1063. * because when we access tmp[1] we know tmp[0] is '0',
  1064. * so tmp[1] should always valid (but could be '\0').
  1065. */
  1066. if (tmp && !strncmp(tmp, "0x", 2)) {
  1067. pp->abs_address = strtoul(pp->function, &tmp, 0);
  1068. if (*tmp != '\0') {
  1069. semantic_error("Invalid absolute address.\n");
  1070. return -EINVAL;
  1071. }
  1072. }
  1073. }
  1074. /* Parse other options */
  1075. while (ptr) {
  1076. arg = ptr;
  1077. c = nc;
  1078. if (c == ';') { /* Lazy pattern must be the last part */
  1079. pp->lazy_line = strdup(arg);
  1080. if (pp->lazy_line == NULL)
  1081. return -ENOMEM;
  1082. break;
  1083. }
  1084. ptr = strpbrk(arg, ";:+@%");
  1085. if (ptr) {
  1086. nc = *ptr;
  1087. *ptr++ = '\0';
  1088. }
  1089. switch (c) {
  1090. case ':': /* Line number */
  1091. pp->line = strtoul(arg, &tmp, 0);
  1092. if (*tmp != '\0') {
  1093. semantic_error("There is non-digit char"
  1094. " in line number.\n");
  1095. return -EINVAL;
  1096. }
  1097. break;
  1098. case '+': /* Byte offset from a symbol */
  1099. pp->offset = strtoul(arg, &tmp, 0);
  1100. if (*tmp != '\0') {
  1101. semantic_error("There is non-digit character"
  1102. " in offset.\n");
  1103. return -EINVAL;
  1104. }
  1105. break;
  1106. case '@': /* File name */
  1107. if (pp->file) {
  1108. semantic_error("SRC@SRC is not allowed.\n");
  1109. return -EINVAL;
  1110. }
  1111. pp->file = strdup(arg);
  1112. if (pp->file == NULL)
  1113. return -ENOMEM;
  1114. break;
  1115. case '%': /* Probe places */
  1116. if (strcmp(arg, "return") == 0) {
  1117. pp->retprobe = 1;
  1118. } else { /* Others not supported yet */
  1119. semantic_error("%%%s is not supported.\n", arg);
  1120. return -ENOTSUP;
  1121. }
  1122. break;
  1123. default: /* Buggy case */
  1124. pr_err("This program has a bug at %s:%d.\n",
  1125. __FILE__, __LINE__);
  1126. return -ENOTSUP;
  1127. break;
  1128. }
  1129. }
  1130. /* Exclusion check */
  1131. if (pp->lazy_line && pp->line) {
  1132. semantic_error("Lazy pattern can't be used with"
  1133. " line number.\n");
  1134. return -EINVAL;
  1135. }
  1136. if (pp->lazy_line && pp->offset) {
  1137. semantic_error("Lazy pattern can't be used with offset.\n");
  1138. return -EINVAL;
  1139. }
  1140. if (pp->line && pp->offset) {
  1141. semantic_error("Offset can't be used with line number.\n");
  1142. return -EINVAL;
  1143. }
  1144. if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
  1145. semantic_error("File always requires line number or "
  1146. "lazy pattern.\n");
  1147. return -EINVAL;
  1148. }
  1149. if (pp->offset && !pp->function) {
  1150. semantic_error("Offset requires an entry function.\n");
  1151. return -EINVAL;
  1152. }
  1153. if (pp->retprobe && !pp->function) {
  1154. semantic_error("Return probe requires an entry function.\n");
  1155. return -EINVAL;
  1156. }
  1157. if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
  1158. semantic_error("Offset/Line/Lazy pattern can't be used with "
  1159. "return probe.\n");
  1160. return -EINVAL;
  1161. }
  1162. pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
  1163. pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
  1164. pp->lazy_line);
  1165. return 0;
  1166. }
  1167. /* Parse perf-probe event argument */
  1168. static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
  1169. {
  1170. char *tmp, *goodname;
  1171. struct perf_probe_arg_field **fieldp;
  1172. pr_debug("parsing arg: %s into ", str);
  1173. tmp = strchr(str, '=');
  1174. if (tmp) {
  1175. arg->name = strndup(str, tmp - str);
  1176. if (arg->name == NULL)
  1177. return -ENOMEM;
  1178. pr_debug("name:%s ", arg->name);
  1179. str = tmp + 1;
  1180. }
  1181. tmp = strchr(str, ':');
  1182. if (tmp) { /* Type setting */
  1183. *tmp = '\0';
  1184. arg->type = strdup(tmp + 1);
  1185. if (arg->type == NULL)
  1186. return -ENOMEM;
  1187. pr_debug("type:%s ", arg->type);
  1188. }
  1189. tmp = strpbrk(str, "-.[");
  1190. if (!is_c_varname(str) || !tmp) {
  1191. /* A variable, register, symbol or special value */
  1192. arg->var = strdup(str);
  1193. if (arg->var == NULL)
  1194. return -ENOMEM;
  1195. pr_debug("%s\n", arg->var);
  1196. return 0;
  1197. }
  1198. /* Structure fields or array element */
  1199. arg->var = strndup(str, tmp - str);
  1200. if (arg->var == NULL)
  1201. return -ENOMEM;
  1202. goodname = arg->var;
  1203. pr_debug("%s, ", arg->var);
  1204. fieldp = &arg->field;
  1205. do {
  1206. *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
  1207. if (*fieldp == NULL)
  1208. return -ENOMEM;
  1209. if (*tmp == '[') { /* Array */
  1210. str = tmp;
  1211. (*fieldp)->index = strtol(str + 1, &tmp, 0);
  1212. (*fieldp)->ref = true;
  1213. if (*tmp != ']' || tmp == str + 1) {
  1214. semantic_error("Array index must be a"
  1215. " number.\n");
  1216. return -EINVAL;
  1217. }
  1218. tmp++;
  1219. if (*tmp == '\0')
  1220. tmp = NULL;
  1221. } else { /* Structure */
  1222. if (*tmp == '.') {
  1223. str = tmp + 1;
  1224. (*fieldp)->ref = false;
  1225. } else if (tmp[1] == '>') {
  1226. str = tmp + 2;
  1227. (*fieldp)->ref = true;
  1228. } else {
  1229. semantic_error("Argument parse error: %s\n",
  1230. str);
  1231. return -EINVAL;
  1232. }
  1233. tmp = strpbrk(str, "-.[");
  1234. }
  1235. if (tmp) {
  1236. (*fieldp)->name = strndup(str, tmp - str);
  1237. if ((*fieldp)->name == NULL)
  1238. return -ENOMEM;
  1239. if (*str != '[')
  1240. goodname = (*fieldp)->name;
  1241. pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
  1242. fieldp = &(*fieldp)->next;
  1243. }
  1244. } while (tmp);
  1245. (*fieldp)->name = strdup(str);
  1246. if ((*fieldp)->name == NULL)
  1247. return -ENOMEM;
  1248. if (*str != '[')
  1249. goodname = (*fieldp)->name;
  1250. pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
  1251. /* If no name is specified, set the last field name (not array index)*/
  1252. if (!arg->name) {
  1253. arg->name = strdup(goodname);
  1254. if (arg->name == NULL)
  1255. return -ENOMEM;
  1256. }
  1257. return 0;
  1258. }
  1259. /* Parse perf-probe event command */
  1260. int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
  1261. {
  1262. char **argv;
  1263. int argc, i, ret = 0;
  1264. argv = argv_split(cmd, &argc);
  1265. if (!argv) {
  1266. pr_debug("Failed to split arguments.\n");
  1267. return -ENOMEM;
  1268. }
  1269. if (argc - 1 > MAX_PROBE_ARGS) {
  1270. semantic_error("Too many probe arguments (%d).\n", argc - 1);
  1271. ret = -ERANGE;
  1272. goto out;
  1273. }
  1274. /* Parse probe point */
  1275. ret = parse_perf_probe_point(argv[0], pev);
  1276. if (ret < 0)
  1277. goto out;
  1278. /* Copy arguments and ensure return probe has no C argument */
  1279. pev->nargs = argc - 1;
  1280. pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  1281. if (pev->args == NULL) {
  1282. ret = -ENOMEM;
  1283. goto out;
  1284. }
  1285. for (i = 0; i < pev->nargs && ret >= 0; i++) {
  1286. ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
  1287. if (ret >= 0 &&
  1288. is_c_varname(pev->args[i].var) && pev->point.retprobe) {
  1289. semantic_error("You can't specify local variable for"
  1290. " kretprobe.\n");
  1291. ret = -EINVAL;
  1292. }
  1293. }
  1294. out:
  1295. argv_free(argv);
  1296. return ret;
  1297. }
  1298. /* Return true if this perf_probe_event requires debuginfo */
  1299. bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
  1300. {
  1301. int i;
  1302. if (pev->point.file || pev->point.line || pev->point.lazy_line)
  1303. return true;
  1304. for (i = 0; i < pev->nargs; i++)
  1305. if (is_c_varname(pev->args[i].var))
  1306. return true;
  1307. return false;
  1308. }
  1309. /* Parse probe_events event into struct probe_point */
  1310. int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
  1311. {
  1312. struct probe_trace_point *tp = &tev->point;
  1313. char pr;
  1314. char *p;
  1315. char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
  1316. int ret, i, argc;
  1317. char **argv;
  1318. pr_debug("Parsing probe_events: %s\n", cmd);
  1319. argv = argv_split(cmd, &argc);
  1320. if (!argv) {
  1321. pr_debug("Failed to split arguments.\n");
  1322. return -ENOMEM;
  1323. }
  1324. if (argc < 2) {
  1325. semantic_error("Too few probe arguments.\n");
  1326. ret = -ERANGE;
  1327. goto out;
  1328. }
  1329. /* Scan event and group name. */
  1330. argv0_str = strdup(argv[0]);
  1331. if (argv0_str == NULL) {
  1332. ret = -ENOMEM;
  1333. goto out;
  1334. }
  1335. fmt1_str = strtok_r(argv0_str, ":", &fmt);
  1336. fmt2_str = strtok_r(NULL, "/", &fmt);
  1337. fmt3_str = strtok_r(NULL, " \t", &fmt);
  1338. if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
  1339. || fmt3_str == NULL) {
  1340. semantic_error("Failed to parse event name: %s\n", argv[0]);
  1341. ret = -EINVAL;
  1342. goto out;
  1343. }
  1344. pr = fmt1_str[0];
  1345. tev->group = strdup(fmt2_str);
  1346. tev->event = strdup(fmt3_str);
  1347. if (tev->group == NULL || tev->event == NULL) {
  1348. ret = -ENOMEM;
  1349. goto out;
  1350. }
  1351. pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
  1352. tp->retprobe = (pr == 'r');
  1353. /* Scan module name(if there), function name and offset */
  1354. p = strchr(argv[1], ':');
  1355. if (p) {
  1356. tp->module = strndup(argv[1], p - argv[1]);
  1357. p++;
  1358. } else
  1359. p = argv[1];
  1360. fmt1_str = strtok_r(p, "+", &fmt);
  1361. /* only the address started with 0x */
  1362. if (fmt1_str[0] == '0') {
  1363. /*
  1364. * Fix a special case:
  1365. * if address == 0, kernel reports something like:
  1366. * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
  1367. * Newer kernel may fix that, but we want to
  1368. * support old kernel also.
  1369. */
  1370. if (strcmp(fmt1_str, "0x") == 0) {
  1371. if (!argv[2] || strcmp(argv[2], "(null)")) {
  1372. ret = -EINVAL;
  1373. goto out;
  1374. }
  1375. tp->address = 0;
  1376. free(argv[2]);
  1377. for (i = 2; argv[i + 1] != NULL; i++)
  1378. argv[i] = argv[i + 1];
  1379. argv[i] = NULL;
  1380. argc -= 1;
  1381. } else
  1382. tp->address = strtoul(fmt1_str, NULL, 0);
  1383. } else {
  1384. /* Only the symbol-based probe has offset */
  1385. tp->symbol = strdup(fmt1_str);
  1386. if (tp->symbol == NULL) {
  1387. ret = -ENOMEM;
  1388. goto out;
  1389. }
  1390. fmt2_str = strtok_r(NULL, "", &fmt);
  1391. if (fmt2_str == NULL)
  1392. tp->offset = 0;
  1393. else
  1394. tp->offset = strtoul(fmt2_str, NULL, 10);
  1395. }
  1396. tev->nargs = argc - 2;
  1397. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  1398. if (tev->args == NULL) {
  1399. ret = -ENOMEM;
  1400. goto out;
  1401. }
  1402. for (i = 0; i < tev->nargs; i++) {
  1403. p = strchr(argv[i + 2], '=');
  1404. if (p) /* We don't need which register is assigned. */
  1405. *p++ = '\0';
  1406. else
  1407. p = argv[i + 2];
  1408. tev->args[i].name = strdup(argv[i + 2]);
  1409. /* TODO: parse regs and offset */
  1410. tev->args[i].value = strdup(p);
  1411. if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
  1412. ret = -ENOMEM;
  1413. goto out;
  1414. }
  1415. }
  1416. ret = 0;
  1417. out:
  1418. free(argv0_str);
  1419. argv_free(argv);
  1420. return ret;
  1421. }
  1422. /* Compose only probe arg */
  1423. int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
  1424. {
  1425. struct perf_probe_arg_field *field = pa->field;
  1426. int ret;
  1427. char *tmp = buf;
  1428. if (pa->name && pa->var)
  1429. ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
  1430. else
  1431. ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
  1432. if (ret <= 0)
  1433. goto error;
  1434. tmp += ret;
  1435. len -= ret;
  1436. while (field) {
  1437. if (field->name[0] == '[')
  1438. ret = e_snprintf(tmp, len, "%s", field->name);
  1439. else
  1440. ret = e_snprintf(tmp, len, "%s%s",
  1441. field->ref ? "->" : ".", field->name);
  1442. if (ret <= 0)
  1443. goto error;
  1444. tmp += ret;
  1445. len -= ret;
  1446. field = field->next;
  1447. }
  1448. if (pa->type) {
  1449. ret = e_snprintf(tmp, len, ":%s", pa->type);
  1450. if (ret <= 0)
  1451. goto error;
  1452. tmp += ret;
  1453. len -= ret;
  1454. }
  1455. return tmp - buf;
  1456. error:
  1457. pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
  1458. return ret;
  1459. }
  1460. /* Compose only probe point (not argument) */
  1461. static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
  1462. {
  1463. char *buf, *tmp;
  1464. char offs[32] = "", line[32] = "", file[32] = "";
  1465. int ret, len;
  1466. buf = zalloc(MAX_CMDLEN);
  1467. if (buf == NULL) {
  1468. ret = -ENOMEM;
  1469. goto error;
  1470. }
  1471. if (pp->offset) {
  1472. ret = e_snprintf(offs, 32, "+%lu", pp->offset);
  1473. if (ret <= 0)
  1474. goto error;
  1475. }
  1476. if (pp->line) {
  1477. ret = e_snprintf(line, 32, ":%d", pp->line);
  1478. if (ret <= 0)
  1479. goto error;
  1480. }
  1481. if (pp->file) {
  1482. tmp = pp->file;
  1483. len = strlen(tmp);
  1484. if (len > 30) {
  1485. tmp = strchr(pp->file + len - 30, '/');
  1486. tmp = tmp ? tmp + 1 : pp->file + len - 30;
  1487. }
  1488. ret = e_snprintf(file, 32, "@%s", tmp);
  1489. if (ret <= 0)
  1490. goto error;
  1491. }
  1492. if (pp->function)
  1493. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
  1494. offs, pp->retprobe ? "%return" : "", line,
  1495. file);
  1496. else
  1497. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
  1498. if (ret <= 0)
  1499. goto error;
  1500. return buf;
  1501. error:
  1502. pr_debug("Failed to synthesize perf probe point: %d\n", ret);
  1503. free(buf);
  1504. return NULL;
  1505. }
  1506. #if 0
  1507. char *synthesize_perf_probe_command(struct perf_probe_event *pev)
  1508. {
  1509. char *buf;
  1510. int i, len, ret;
  1511. buf = synthesize_perf_probe_point(&pev->point);
  1512. if (!buf)
  1513. return NULL;
  1514. len = strlen(buf);
  1515. for (i = 0; i < pev->nargs; i++) {
  1516. ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
  1517. pev->args[i].name);
  1518. if (ret <= 0) {
  1519. free(buf);
  1520. return NULL;
  1521. }
  1522. len += ret;
  1523. }
  1524. return buf;
  1525. }
  1526. #endif
  1527. static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
  1528. char **buf, size_t *buflen,
  1529. int depth)
  1530. {
  1531. int ret;
  1532. if (ref->next) {
  1533. depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
  1534. buflen, depth + 1);
  1535. if (depth < 0)
  1536. goto out;
  1537. }
  1538. ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
  1539. if (ret < 0)
  1540. depth = ret;
  1541. else {
  1542. *buf += ret;
  1543. *buflen -= ret;
  1544. }
  1545. out:
  1546. return depth;
  1547. }
  1548. static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
  1549. char *buf, size_t buflen)
  1550. {
  1551. struct probe_trace_arg_ref *ref = arg->ref;
  1552. int ret, depth = 0;
  1553. char *tmp = buf;
  1554. /* Argument name or separator */
  1555. if (arg->name)
  1556. ret = e_snprintf(buf, buflen, " %s=", arg->name);
  1557. else
  1558. ret = e_snprintf(buf, buflen, " ");
  1559. if (ret < 0)
  1560. return ret;
  1561. buf += ret;
  1562. buflen -= ret;
  1563. /* Special case: @XXX */
  1564. if (arg->value[0] == '@' && arg->ref)
  1565. ref = ref->next;
  1566. /* Dereferencing arguments */
  1567. if (ref) {
  1568. depth = __synthesize_probe_trace_arg_ref(ref, &buf,
  1569. &buflen, 1);
  1570. if (depth < 0)
  1571. return depth;
  1572. }
  1573. /* Print argument value */
  1574. if (arg->value[0] == '@' && arg->ref)
  1575. ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
  1576. arg->ref->offset);
  1577. else
  1578. ret = e_snprintf(buf, buflen, "%s", arg->value);
  1579. if (ret < 0)
  1580. return ret;
  1581. buf += ret;
  1582. buflen -= ret;
  1583. /* Closing */
  1584. while (depth--) {
  1585. ret = e_snprintf(buf, buflen, ")");
  1586. if (ret < 0)
  1587. return ret;
  1588. buf += ret;
  1589. buflen -= ret;
  1590. }
  1591. /* Print argument type */
  1592. if (arg->type) {
  1593. ret = e_snprintf(buf, buflen, ":%s", arg->type);
  1594. if (ret <= 0)
  1595. return ret;
  1596. buf += ret;
  1597. }
  1598. return buf - tmp;
  1599. }
  1600. char *synthesize_probe_trace_command(struct probe_trace_event *tev)
  1601. {
  1602. struct probe_trace_point *tp = &tev->point;
  1603. char *buf;
  1604. int i, len, ret;
  1605. buf = zalloc(MAX_CMDLEN);
  1606. if (buf == NULL)
  1607. return NULL;
  1608. len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
  1609. tev->group, tev->event);
  1610. if (len <= 0)
  1611. goto error;
  1612. /* Uprobes must have tp->module */
  1613. if (tev->uprobes && !tp->module)
  1614. goto error;
  1615. /*
  1616. * If tp->address == 0, then this point must be a
  1617. * absolute address uprobe.
  1618. * try_to_find_absolute_address() should have made
  1619. * tp->symbol to "0x0".
  1620. */
  1621. if (tev->uprobes && !tp->address) {
  1622. if (!tp->symbol || strcmp(tp->symbol, "0x0"))
  1623. goto error;
  1624. }
  1625. /* Use the tp->address for uprobes */
  1626. if (tev->uprobes)
  1627. ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
  1628. tp->module, tp->address);
  1629. else if (!strncmp(tp->symbol, "0x", 2))
  1630. /* Absolute address. See try_to_find_absolute_address() */
  1631. ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s0x%lx",
  1632. tp->module ?: "", tp->module ? ":" : "",
  1633. tp->address);
  1634. else
  1635. ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
  1636. tp->module ?: "", tp->module ? ":" : "",
  1637. tp->symbol, tp->offset);
  1638. if (ret <= 0)
  1639. goto error;
  1640. len += ret;
  1641. for (i = 0; i < tev->nargs; i++) {
  1642. ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
  1643. MAX_CMDLEN - len);
  1644. if (ret <= 0)
  1645. goto error;
  1646. len += ret;
  1647. }
  1648. return buf;
  1649. error:
  1650. free(buf);
  1651. return NULL;
  1652. }
  1653. static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
  1654. struct perf_probe_point *pp,
  1655. bool is_kprobe)
  1656. {
  1657. struct symbol *sym = NULL;
  1658. struct map *map;
  1659. u64 addr = tp->address;
  1660. int ret = -ENOENT;
  1661. if (!is_kprobe) {
  1662. map = dso__new_map(tp->module);
  1663. if (!map)
  1664. goto out;
  1665. sym = map__find_symbol(map, addr, NULL);
  1666. } else {
  1667. if (tp->symbol && !addr) {
  1668. if (kernel_get_symbol_address_by_name(tp->symbol,
  1669. &addr, true, false) < 0)
  1670. goto out;
  1671. }
  1672. if (addr) {
  1673. addr += tp->offset;
  1674. sym = __find_kernel_function(addr, &map);
  1675. }
  1676. }
  1677. if (!sym)
  1678. goto out;
  1679. pp->retprobe = tp->retprobe;
  1680. pp->offset = addr - map->unmap_ip(map, sym->start);
  1681. pp->function = strdup(sym->name);
  1682. ret = pp->function ? 0 : -ENOMEM;
  1683. out:
  1684. if (map && !is_kprobe) {
  1685. map__put(map);
  1686. }
  1687. return ret;
  1688. }
  1689. static int convert_to_perf_probe_point(struct probe_trace_point *tp,
  1690. struct perf_probe_point *pp,
  1691. bool is_kprobe)
  1692. {
  1693. char buf[128];
  1694. int ret;
  1695. ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
  1696. if (!ret)
  1697. return 0;
  1698. ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
  1699. if (!ret)
  1700. return 0;
  1701. pr_debug("Failed to find probe point from both of dwarf and map.\n");
  1702. if (tp->symbol) {
  1703. pp->function = strdup(tp->symbol);
  1704. pp->offset = tp->offset;
  1705. } else {
  1706. ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
  1707. if (ret < 0)
  1708. return ret;
  1709. pp->function = strdup(buf);
  1710. pp->offset = 0;
  1711. }
  1712. if (pp->function == NULL)
  1713. return -ENOMEM;
  1714. pp->retprobe = tp->retprobe;
  1715. return 0;
  1716. }
  1717. static int convert_to_perf_probe_event(struct probe_trace_event *tev,
  1718. struct perf_probe_event *pev, bool is_kprobe)
  1719. {
  1720. char buf[64] = "";
  1721. int i, ret;
  1722. /* Convert event/group name */
  1723. pev->event = strdup(tev->event);
  1724. pev->group = strdup(tev->group);
  1725. if (pev->event == NULL || pev->group == NULL)
  1726. return -ENOMEM;
  1727. /* Convert trace_point to probe_point */
  1728. ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
  1729. if (ret < 0)
  1730. return ret;
  1731. /* Convert trace_arg to probe_arg */
  1732. pev->nargs = tev->nargs;
  1733. pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  1734. if (pev->args == NULL)
  1735. return -ENOMEM;
  1736. for (i = 0; i < tev->nargs && ret >= 0; i++) {
  1737. if (tev->args[i].name)
  1738. pev->args[i].name = strdup(tev->args[i].name);
  1739. else {
  1740. ret = synthesize_probe_trace_arg(&tev->args[i],
  1741. buf, 64);
  1742. pev->args[i].name = strdup(buf);
  1743. }
  1744. if (pev->args[i].name == NULL && ret >= 0)
  1745. ret = -ENOMEM;
  1746. }
  1747. if (ret < 0)
  1748. clear_perf_probe_event(pev);
  1749. return ret;
  1750. }
  1751. void clear_perf_probe_event(struct perf_probe_event *pev)
  1752. {
  1753. struct perf_probe_arg_field *field, *next;
  1754. int i;
  1755. free(pev->event);
  1756. free(pev->group);
  1757. free(pev->target);
  1758. clear_perf_probe_point(&pev->point);
  1759. for (i = 0; i < pev->nargs; i++) {
  1760. free(pev->args[i].name);
  1761. free(pev->args[i].var);
  1762. free(pev->args[i].type);
  1763. field = pev->args[i].field;
  1764. while (field) {
  1765. next = field->next;
  1766. zfree(&field->name);
  1767. free(field);
  1768. field = next;
  1769. }
  1770. }
  1771. free(pev->args);
  1772. memset(pev, 0, sizeof(*pev));
  1773. }
  1774. void clear_probe_trace_event(struct probe_trace_event *tev)
  1775. {
  1776. struct probe_trace_arg_ref *ref, *next;
  1777. int i;
  1778. free(tev->event);
  1779. free(tev->group);
  1780. free(tev->point.symbol);
  1781. free(tev->point.realname);
  1782. free(tev->point.module);
  1783. for (i = 0; i < tev->nargs; i++) {
  1784. free(tev->args[i].name);
  1785. free(tev->args[i].value);
  1786. free(tev->args[i].type);
  1787. ref = tev->args[i].ref;
  1788. while (ref) {
  1789. next = ref->next;
  1790. free(ref);
  1791. ref = next;
  1792. }
  1793. }
  1794. free(tev->args);
  1795. memset(tev, 0, sizeof(*tev));
  1796. }
  1797. struct kprobe_blacklist_node {
  1798. struct list_head list;
  1799. unsigned long start;
  1800. unsigned long end;
  1801. char *symbol;
  1802. };
  1803. static void kprobe_blacklist__delete(struct list_head *blacklist)
  1804. {
  1805. struct kprobe_blacklist_node *node;
  1806. while (!list_empty(blacklist)) {
  1807. node = list_first_entry(blacklist,
  1808. struct kprobe_blacklist_node, list);
  1809. list_del(&node->list);
  1810. free(node->symbol);
  1811. free(node);
  1812. }
  1813. }
  1814. static int kprobe_blacklist__load(struct list_head *blacklist)
  1815. {
  1816. struct kprobe_blacklist_node *node;
  1817. const char *__debugfs = debugfs__mountpoint();
  1818. char buf[PATH_MAX], *p;
  1819. FILE *fp;
  1820. int ret;
  1821. if (__debugfs == NULL)
  1822. return -ENOTSUP;
  1823. ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
  1824. if (ret < 0)
  1825. return ret;
  1826. fp = fopen(buf, "r");
  1827. if (!fp)
  1828. return -errno;
  1829. ret = 0;
  1830. while (fgets(buf, PATH_MAX, fp)) {
  1831. node = zalloc(sizeof(*node));
  1832. if (!node) {
  1833. ret = -ENOMEM;
  1834. break;
  1835. }
  1836. INIT_LIST_HEAD(&node->list);
  1837. list_add_tail(&node->list, blacklist);
  1838. if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
  1839. ret = -EINVAL;
  1840. break;
  1841. }
  1842. p = strchr(buf, '\t');
  1843. if (p) {
  1844. p++;
  1845. if (p[strlen(p) - 1] == '\n')
  1846. p[strlen(p) - 1] = '\0';
  1847. } else
  1848. p = (char *)"unknown";
  1849. node->symbol = strdup(p);
  1850. if (!node->symbol) {
  1851. ret = -ENOMEM;
  1852. break;
  1853. }
  1854. pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
  1855. node->start, node->end, node->symbol);
  1856. ret++;
  1857. }
  1858. if (ret < 0)
  1859. kprobe_blacklist__delete(blacklist);
  1860. fclose(fp);
  1861. return ret;
  1862. }
  1863. static struct kprobe_blacklist_node *
  1864. kprobe_blacklist__find_by_address(struct list_head *blacklist,
  1865. unsigned long address)
  1866. {
  1867. struct kprobe_blacklist_node *node;
  1868. list_for_each_entry(node, blacklist, list) {
  1869. if (node->start <= address && address <= node->end)
  1870. return node;
  1871. }
  1872. return NULL;
  1873. }
  1874. static LIST_HEAD(kprobe_blacklist);
  1875. static void kprobe_blacklist__init(void)
  1876. {
  1877. if (!list_empty(&kprobe_blacklist))
  1878. return;
  1879. if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
  1880. pr_debug("No kprobe blacklist support, ignored\n");
  1881. }
  1882. static void kprobe_blacklist__release(void)
  1883. {
  1884. kprobe_blacklist__delete(&kprobe_blacklist);
  1885. }
  1886. static bool kprobe_blacklist__listed(unsigned long address)
  1887. {
  1888. return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
  1889. }
  1890. static int perf_probe_event__sprintf(const char *group, const char *event,
  1891. struct perf_probe_event *pev,
  1892. const char *module,
  1893. struct strbuf *result)
  1894. {
  1895. int i, ret;
  1896. char buf[128];
  1897. char *place;
  1898. /* Synthesize only event probe point */
  1899. place = synthesize_perf_probe_point(&pev->point);
  1900. if (!place)
  1901. return -EINVAL;
  1902. ret = e_snprintf(buf, 128, "%s:%s", group, event);
  1903. if (ret < 0)
  1904. goto out;
  1905. strbuf_addf(result, " %-20s (on %s", buf, place);
  1906. if (module)
  1907. strbuf_addf(result, " in %s", module);
  1908. if (pev->nargs > 0) {
  1909. strbuf_addstr(result, " with");
  1910. for (i = 0; i < pev->nargs; i++) {
  1911. ret = synthesize_perf_probe_arg(&pev->args[i],
  1912. buf, 128);
  1913. if (ret < 0)
  1914. goto out;
  1915. strbuf_addf(result, " %s", buf);
  1916. }
  1917. }
  1918. strbuf_addch(result, ')');
  1919. out:
  1920. free(place);
  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. /*
  2183. * Find probe function addresses from map.
  2184. * Return an error or the number of found probe_trace_event
  2185. */
  2186. static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
  2187. struct probe_trace_event **tevs)
  2188. {
  2189. struct map *map = NULL;
  2190. struct ref_reloc_sym *reloc_sym = NULL;
  2191. struct symbol *sym;
  2192. struct symbol **syms = NULL;
  2193. struct probe_trace_event *tev;
  2194. struct perf_probe_point *pp = &pev->point;
  2195. struct probe_trace_point *tp;
  2196. int num_matched_functions;
  2197. int ret, i, j, skipped = 0;
  2198. map = get_target_map(pev->target, pev->uprobes);
  2199. if (!map) {
  2200. ret = -EINVAL;
  2201. goto out;
  2202. }
  2203. syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
  2204. if (!syms) {
  2205. ret = -ENOMEM;
  2206. goto out;
  2207. }
  2208. /*
  2209. * Load matched symbols: Since the different local symbols may have
  2210. * same name but different addresses, this lists all the symbols.
  2211. */
  2212. num_matched_functions = find_probe_functions(map, pp->function, syms);
  2213. if (num_matched_functions == 0) {
  2214. pr_err("Failed to find symbol %s in %s\n", pp->function,
  2215. pev->target ? : "kernel");
  2216. ret = -ENOENT;
  2217. goto out;
  2218. } else if (num_matched_functions > probe_conf.max_probes) {
  2219. pr_err("Too many functions matched in %s\n",
  2220. pev->target ? : "kernel");
  2221. ret = -E2BIG;
  2222. goto out;
  2223. }
  2224. /* Note that the symbols in the kmodule are not relocated */
  2225. if (!pev->uprobes && !pp->retprobe && !pev->target) {
  2226. reloc_sym = kernel_get_ref_reloc_sym();
  2227. if (!reloc_sym) {
  2228. pr_warning("Relocated base symbol is not found!\n");
  2229. ret = -EINVAL;
  2230. goto out;
  2231. }
  2232. }
  2233. /* Setup result trace-probe-events */
  2234. *tevs = zalloc(sizeof(*tev) * num_matched_functions);
  2235. if (!*tevs) {
  2236. ret = -ENOMEM;
  2237. goto out;
  2238. }
  2239. ret = 0;
  2240. for (j = 0; j < num_matched_functions; j++) {
  2241. sym = syms[j];
  2242. tev = (*tevs) + ret;
  2243. tp = &tev->point;
  2244. if (ret == num_matched_functions) {
  2245. pr_warning("Too many symbols are listed. Skip it.\n");
  2246. break;
  2247. }
  2248. ret++;
  2249. if (pp->offset > sym->end - sym->start) {
  2250. pr_warning("Offset %ld is bigger than the size of %s\n",
  2251. pp->offset, sym->name);
  2252. ret = -ENOENT;
  2253. goto err_out;
  2254. }
  2255. /* Add one probe point */
  2256. tp->address = map->unmap_ip(map, sym->start) + pp->offset;
  2257. /* Check the kprobe (not in module) is within .text */
  2258. if (!pev->uprobes && !pev->target &&
  2259. kprobe_warn_out_range(sym->name, tp->address)) {
  2260. tp->symbol = NULL; /* Skip it */
  2261. skipped++;
  2262. } else if (reloc_sym) {
  2263. tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
  2264. tp->offset = tp->address - reloc_sym->addr;
  2265. } else {
  2266. tp->symbol = strdup_or_goto(sym->name, nomem_out);
  2267. tp->offset = pp->offset;
  2268. }
  2269. tp->realname = strdup_or_goto(sym->name, nomem_out);
  2270. tp->retprobe = pp->retprobe;
  2271. if (pev->target)
  2272. tev->point.module = strdup_or_goto(pev->target,
  2273. nomem_out);
  2274. tev->uprobes = pev->uprobes;
  2275. tev->nargs = pev->nargs;
  2276. if (tev->nargs) {
  2277. tev->args = zalloc(sizeof(struct probe_trace_arg) *
  2278. tev->nargs);
  2279. if (tev->args == NULL)
  2280. goto nomem_out;
  2281. }
  2282. for (i = 0; i < tev->nargs; i++) {
  2283. if (pev->args[i].name)
  2284. tev->args[i].name =
  2285. strdup_or_goto(pev->args[i].name,
  2286. nomem_out);
  2287. tev->args[i].value = strdup_or_goto(pev->args[i].var,
  2288. nomem_out);
  2289. if (pev->args[i].type)
  2290. tev->args[i].type =
  2291. strdup_or_goto(pev->args[i].type,
  2292. nomem_out);
  2293. }
  2294. arch__fix_tev_from_maps(pev, tev, map);
  2295. }
  2296. if (ret == skipped) {
  2297. ret = -ENOENT;
  2298. goto err_out;
  2299. }
  2300. out:
  2301. put_target_map(map, pev->uprobes);
  2302. free(syms);
  2303. return ret;
  2304. nomem_out:
  2305. ret = -ENOMEM;
  2306. err_out:
  2307. clear_probe_trace_events(*tevs, num_matched_functions);
  2308. zfree(tevs);
  2309. goto out;
  2310. }
  2311. static int try_to_find_absolute_address(struct perf_probe_event *pev,
  2312. struct probe_trace_event **tevs)
  2313. {
  2314. struct perf_probe_point *pp = &pev->point;
  2315. struct probe_trace_event *tev;
  2316. struct probe_trace_point *tp;
  2317. int i, err;
  2318. if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
  2319. return -EINVAL;
  2320. if (perf_probe_event_need_dwarf(pev))
  2321. return -EINVAL;
  2322. /*
  2323. * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
  2324. * absolute address.
  2325. *
  2326. * Only one tev can be generated by this.
  2327. */
  2328. *tevs = zalloc(sizeof(*tev));
  2329. if (!*tevs)
  2330. return -ENOMEM;
  2331. tev = *tevs;
  2332. tp = &tev->point;
  2333. /*
  2334. * Don't use tp->offset, use address directly, because
  2335. * in synthesize_probe_trace_command() address cannot be
  2336. * zero.
  2337. */
  2338. tp->address = pev->point.abs_address;
  2339. tp->retprobe = pp->retprobe;
  2340. tev->uprobes = pev->uprobes;
  2341. err = -ENOMEM;
  2342. /*
  2343. * Give it a '0x' leading symbol name.
  2344. * In __add_probe_trace_events, a NULL symbol is interpreted as
  2345. * invalud.
  2346. */
  2347. if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
  2348. goto errout;
  2349. /* For kprobe, check range */
  2350. if ((!tev->uprobes) &&
  2351. (kprobe_warn_out_range(tev->point.symbol,
  2352. tev->point.address))) {
  2353. err = -EACCES;
  2354. goto errout;
  2355. }
  2356. if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
  2357. goto errout;
  2358. if (pev->target) {
  2359. tp->module = strdup(pev->target);
  2360. if (!tp->module)
  2361. goto errout;
  2362. }
  2363. if (tev->group) {
  2364. tev->group = strdup(pev->group);
  2365. if (!tev->group)
  2366. goto errout;
  2367. }
  2368. if (pev->event) {
  2369. tev->event = strdup(pev->event);
  2370. if (!tev->event)
  2371. goto errout;
  2372. }
  2373. tev->nargs = pev->nargs;
  2374. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  2375. if (!tev->args) {
  2376. err = -ENOMEM;
  2377. goto errout;
  2378. }
  2379. for (i = 0; i < tev->nargs; i++)
  2380. copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
  2381. return 1;
  2382. errout:
  2383. if (*tevs) {
  2384. clear_probe_trace_events(*tevs, 1);
  2385. *tevs = NULL;
  2386. }
  2387. return err;
  2388. }
  2389. bool __weak arch__prefers_symtab(void) { return false; }
  2390. static int convert_to_probe_trace_events(struct perf_probe_event *pev,
  2391. struct probe_trace_event **tevs)
  2392. {
  2393. int ret;
  2394. if (pev->uprobes && !pev->group) {
  2395. /* Replace group name if not given */
  2396. ret = convert_exec_to_group(pev->target, &pev->group);
  2397. if (ret != 0) {
  2398. pr_warning("Failed to make a group name.\n");
  2399. return ret;
  2400. }
  2401. }
  2402. ret = try_to_find_absolute_address(pev, tevs);
  2403. if (ret > 0)
  2404. return ret;
  2405. if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
  2406. ret = find_probe_trace_events_from_map(pev, tevs);
  2407. if (ret > 0)
  2408. return ret; /* Found in symbol table */
  2409. }
  2410. /* Convert perf_probe_event with debuginfo */
  2411. ret = try_to_find_probe_trace_events(pev, tevs);
  2412. if (ret != 0)
  2413. return ret; /* Found in debuginfo or got an error */
  2414. return find_probe_trace_events_from_map(pev, tevs);
  2415. }
  2416. int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
  2417. {
  2418. int i, ret;
  2419. /* Loop 1: convert all events */
  2420. for (i = 0; i < npevs; i++) {
  2421. /* Init kprobe blacklist if needed */
  2422. if (!pevs[i].uprobes)
  2423. kprobe_blacklist__init();
  2424. /* Convert with or without debuginfo */
  2425. ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
  2426. if (ret < 0)
  2427. return ret;
  2428. pevs[i].ntevs = ret;
  2429. }
  2430. /* This just release blacklist only if allocated */
  2431. kprobe_blacklist__release();
  2432. return 0;
  2433. }
  2434. int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
  2435. {
  2436. int i, ret = 0;
  2437. /* Loop 2: add all events */
  2438. for (i = 0; i < npevs; i++) {
  2439. ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
  2440. pevs[i].ntevs,
  2441. probe_conf.force_add);
  2442. if (ret < 0)
  2443. break;
  2444. }
  2445. return ret;
  2446. }
  2447. void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
  2448. {
  2449. int i, j;
  2450. /* Loop 3: cleanup and free trace events */
  2451. for (i = 0; i < npevs; i++) {
  2452. for (j = 0; j < pevs[i].ntevs; j++)
  2453. clear_probe_trace_event(&pevs[i].tevs[j]);
  2454. zfree(&pevs[i].tevs);
  2455. pevs[i].ntevs = 0;
  2456. clear_perf_probe_event(&pevs[i]);
  2457. }
  2458. }
  2459. int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
  2460. {
  2461. int ret;
  2462. ret = init_probe_symbol_maps(pevs->uprobes);
  2463. if (ret < 0)
  2464. return ret;
  2465. ret = convert_perf_probe_events(pevs, npevs);
  2466. if (ret == 0)
  2467. ret = apply_perf_probe_events(pevs, npevs);
  2468. cleanup_perf_probe_events(pevs, npevs);
  2469. exit_probe_symbol_maps();
  2470. return ret;
  2471. }
  2472. int del_perf_probe_events(struct strfilter *filter)
  2473. {
  2474. int ret, ret2, ufd = -1, kfd = -1;
  2475. char *str = strfilter__string(filter);
  2476. if (!str)
  2477. return -EINVAL;
  2478. /* Get current event names */
  2479. ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
  2480. if (ret < 0)
  2481. goto out;
  2482. ret = probe_file__del_events(kfd, filter);
  2483. if (ret < 0 && ret != -ENOENT)
  2484. goto error;
  2485. ret2 = probe_file__del_events(ufd, filter);
  2486. if (ret2 < 0 && ret2 != -ENOENT) {
  2487. ret = ret2;
  2488. goto error;
  2489. }
  2490. ret = 0;
  2491. error:
  2492. if (kfd >= 0)
  2493. close(kfd);
  2494. if (ufd >= 0)
  2495. close(ufd);
  2496. out:
  2497. free(str);
  2498. return ret;
  2499. }
  2500. /* TODO: don't use a global variable for filter ... */
  2501. static struct strfilter *available_func_filter;
  2502. /*
  2503. * If a symbol corresponds to a function with global binding and
  2504. * matches filter return 0. For all others return 1.
  2505. */
  2506. static int filter_available_functions(struct map *map __maybe_unused,
  2507. struct symbol *sym)
  2508. {
  2509. if (strfilter__compare(available_func_filter, sym->name))
  2510. return 0;
  2511. return 1;
  2512. }
  2513. int show_available_funcs(const char *target, struct strfilter *_filter,
  2514. bool user)
  2515. {
  2516. struct map *map;
  2517. int ret;
  2518. ret = init_probe_symbol_maps(user);
  2519. if (ret < 0)
  2520. return ret;
  2521. /* Get a symbol map */
  2522. if (user)
  2523. map = dso__new_map(target);
  2524. else
  2525. map = kernel_get_module_map(target);
  2526. if (!map) {
  2527. pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
  2528. return -EINVAL;
  2529. }
  2530. /* Load symbols with given filter */
  2531. available_func_filter = _filter;
  2532. if (map__load(map, filter_available_functions)) {
  2533. pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
  2534. goto end;
  2535. }
  2536. if (!dso__sorted_by_name(map->dso, map->type))
  2537. dso__sort_by_name(map->dso, map->type);
  2538. /* Show all (filtered) symbols */
  2539. setup_pager();
  2540. dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
  2541. end:
  2542. if (user) {
  2543. map__put(map);
  2544. }
  2545. exit_probe_symbol_maps();
  2546. return ret;
  2547. }
  2548. int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
  2549. struct perf_probe_arg *pvar)
  2550. {
  2551. tvar->value = strdup(pvar->var);
  2552. if (tvar->value == NULL)
  2553. return -ENOMEM;
  2554. if (pvar->type) {
  2555. tvar->type = strdup(pvar->type);
  2556. if (tvar->type == NULL)
  2557. return -ENOMEM;
  2558. }
  2559. if (pvar->name) {
  2560. tvar->name = strdup(pvar->name);
  2561. if (tvar->name == NULL)
  2562. return -ENOMEM;
  2563. } else
  2564. tvar->name = NULL;
  2565. return 0;
  2566. }