probe-event.c 63 KB

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