probe-event.c 65 KB

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