probe-event.c 58 KB

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