probe-finder.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861
  1. /*
  2. * probe-finder.c : C expression to kprobe event 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 <getopt.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdarg.h>
  32. #include <ctype.h>
  33. #include <dwarf-regs.h>
  34. #include "event.h"
  35. #include "debug.h"
  36. #include "util.h"
  37. #include "symbol.h"
  38. #include "probe-finder.h"
  39. /* Kprobe tracer basic type is up to u64 */
  40. #define MAX_BASIC_TYPE_BITS 64
  41. /*
  42. * Compare the tail of two strings.
  43. * Return 0 if whole of either string is same as another's tail part.
  44. */
  45. static int strtailcmp(const char *s1, const char *s2)
  46. {
  47. int i1 = strlen(s1);
  48. int i2 = strlen(s2);
  49. while (--i1 >= 0 && --i2 >= 0) {
  50. if (s1[i1] != s2[i2])
  51. return s1[i1] - s2[i2];
  52. }
  53. return 0;
  54. }
  55. /* Line number list operations */
  56. /* Add a line to line number list */
  57. static int line_list__add_line(struct list_head *head, int line)
  58. {
  59. struct line_node *ln;
  60. struct list_head *p;
  61. /* Reverse search, because new line will be the last one */
  62. list_for_each_entry_reverse(ln, head, list) {
  63. if (ln->line < line) {
  64. p = &ln->list;
  65. goto found;
  66. } else if (ln->line == line) /* Already exist */
  67. return 1;
  68. }
  69. /* List is empty, or the smallest entry */
  70. p = head;
  71. found:
  72. pr_debug("line list: add a line %u\n", line);
  73. ln = zalloc(sizeof(struct line_node));
  74. if (ln == NULL)
  75. return -ENOMEM;
  76. ln->line = line;
  77. INIT_LIST_HEAD(&ln->list);
  78. list_add(&ln->list, p);
  79. return 0;
  80. }
  81. /* Check if the line in line number list */
  82. static int line_list__has_line(struct list_head *head, int line)
  83. {
  84. struct line_node *ln;
  85. /* Reverse search, because new line will be the last one */
  86. list_for_each_entry(ln, head, list)
  87. if (ln->line == line)
  88. return 1;
  89. return 0;
  90. }
  91. /* Init line number list */
  92. static void line_list__init(struct list_head *head)
  93. {
  94. INIT_LIST_HEAD(head);
  95. }
  96. /* Free line number list */
  97. static void line_list__free(struct list_head *head)
  98. {
  99. struct line_node *ln;
  100. while (!list_empty(head)) {
  101. ln = list_first_entry(head, struct line_node, list);
  102. list_del(&ln->list);
  103. free(ln);
  104. }
  105. }
  106. /* Dwarf FL wrappers */
  107. static char *debuginfo_path; /* Currently dummy */
  108. static const Dwfl_Callbacks offline_callbacks = {
  109. .find_debuginfo = dwfl_standard_find_debuginfo,
  110. .debuginfo_path = &debuginfo_path,
  111. .section_address = dwfl_offline_section_address,
  112. /* We use this table for core files too. */
  113. .find_elf = dwfl_build_id_find_elf,
  114. };
  115. /* Get a Dwarf from offline image */
  116. static Dwarf *dwfl_init_offline_dwarf(int fd, Dwfl **dwflp, Dwarf_Addr *bias)
  117. {
  118. Dwfl_Module *mod;
  119. Dwarf *dbg = NULL;
  120. if (!dwflp)
  121. return NULL;
  122. *dwflp = dwfl_begin(&offline_callbacks);
  123. if (!*dwflp)
  124. return NULL;
  125. mod = dwfl_report_offline(*dwflp, "", "", fd);
  126. if (!mod)
  127. goto error;
  128. dbg = dwfl_module_getdwarf(mod, bias);
  129. if (!dbg) {
  130. error:
  131. dwfl_end(*dwflp);
  132. *dwflp = NULL;
  133. }
  134. return dbg;
  135. }
  136. #if _ELFUTILS_PREREQ(0, 148)
  137. /* This method is buggy if elfutils is older than 0.148 */
  138. static int __linux_kernel_find_elf(Dwfl_Module *mod,
  139. void **userdata,
  140. const char *module_name,
  141. Dwarf_Addr base,
  142. char **file_name, Elf **elfp)
  143. {
  144. int fd;
  145. const char *path = kernel_get_module_path(module_name);
  146. pr_debug2("Use file %s for %s\n", path, module_name);
  147. if (path) {
  148. fd = open(path, O_RDONLY);
  149. if (fd >= 0) {
  150. *file_name = strdup(path);
  151. return fd;
  152. }
  153. }
  154. /* If failed, try to call standard method */
  155. return dwfl_linux_kernel_find_elf(mod, userdata, module_name, base,
  156. file_name, elfp);
  157. }
  158. static const Dwfl_Callbacks kernel_callbacks = {
  159. .find_debuginfo = dwfl_standard_find_debuginfo,
  160. .debuginfo_path = &debuginfo_path,
  161. .find_elf = __linux_kernel_find_elf,
  162. .section_address = dwfl_linux_kernel_module_section_address,
  163. };
  164. /* Get a Dwarf from live kernel image */
  165. static Dwarf *dwfl_init_live_kernel_dwarf(Dwarf_Addr addr, Dwfl **dwflp,
  166. Dwarf_Addr *bias)
  167. {
  168. Dwarf *dbg;
  169. if (!dwflp)
  170. return NULL;
  171. *dwflp = dwfl_begin(&kernel_callbacks);
  172. if (!*dwflp)
  173. return NULL;
  174. /* Load the kernel dwarves: Don't care the result here */
  175. dwfl_linux_kernel_report_kernel(*dwflp);
  176. dwfl_linux_kernel_report_modules(*dwflp);
  177. dbg = dwfl_addrdwarf(*dwflp, addr, bias);
  178. /* Here, check whether we could get a real dwarf */
  179. if (!dbg) {
  180. pr_debug("Failed to find kernel dwarf at %lx\n",
  181. (unsigned long)addr);
  182. dwfl_end(*dwflp);
  183. *dwflp = NULL;
  184. }
  185. return dbg;
  186. }
  187. #else
  188. /* With older elfutils, this just support kernel module... */
  189. static Dwarf *dwfl_init_live_kernel_dwarf(Dwarf_Addr addr __used, Dwfl **dwflp,
  190. Dwarf_Addr *bias)
  191. {
  192. int fd;
  193. const char *path = kernel_get_module_path("kernel");
  194. if (!path) {
  195. pr_err("Failed to find vmlinux path\n");
  196. return NULL;
  197. }
  198. pr_debug2("Use file %s for debuginfo\n", path);
  199. fd = open(path, O_RDONLY);
  200. if (fd < 0)
  201. return NULL;
  202. return dwfl_init_offline_dwarf(fd, dwflp, bias);
  203. }
  204. #endif
  205. /* Dwarf wrappers */
  206. /* Find the realpath of the target file. */
  207. static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
  208. {
  209. Dwarf_Files *files;
  210. size_t nfiles, i;
  211. const char *src = NULL;
  212. int ret;
  213. if (!fname)
  214. return NULL;
  215. ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
  216. if (ret != 0)
  217. return NULL;
  218. for (i = 0; i < nfiles; i++) {
  219. src = dwarf_filesrc(files, i, NULL, NULL);
  220. if (strtailcmp(src, fname) == 0)
  221. break;
  222. }
  223. if (i == nfiles)
  224. return NULL;
  225. return src;
  226. }
  227. /* Get DW_AT_comp_dir (should be NULL with older gcc) */
  228. static const char *cu_get_comp_dir(Dwarf_Die *cu_die)
  229. {
  230. Dwarf_Attribute attr;
  231. if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL)
  232. return NULL;
  233. return dwarf_formstring(&attr);
  234. }
  235. /* Compare diename and tname */
  236. static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
  237. {
  238. const char *name;
  239. name = dwarf_diename(dw_die);
  240. return name ? (strcmp(tname, name) == 0) : false;
  241. }
  242. /* Get type die */
  243. static Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
  244. {
  245. Dwarf_Attribute attr;
  246. if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) &&
  247. dwarf_formref_die(&attr, die_mem))
  248. return die_mem;
  249. else
  250. return NULL;
  251. }
  252. /* Get a type die, but skip qualifiers */
  253. static Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
  254. {
  255. int tag;
  256. do {
  257. vr_die = die_get_type(vr_die, die_mem);
  258. if (!vr_die)
  259. break;
  260. tag = dwarf_tag(vr_die);
  261. } while (tag == DW_TAG_const_type ||
  262. tag == DW_TAG_restrict_type ||
  263. tag == DW_TAG_volatile_type ||
  264. tag == DW_TAG_shared_type);
  265. return vr_die;
  266. }
  267. /* Get a type die, but skip qualifiers and typedef */
  268. static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
  269. {
  270. do {
  271. vr_die = __die_get_real_type(vr_die, die_mem);
  272. } while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef);
  273. return vr_die;
  274. }
  275. static bool die_is_signed_type(Dwarf_Die *tp_die)
  276. {
  277. Dwarf_Attribute attr;
  278. Dwarf_Word ret;
  279. if (dwarf_attr(tp_die, DW_AT_encoding, &attr) == NULL ||
  280. dwarf_formudata(&attr, &ret) != 0)
  281. return false;
  282. return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
  283. ret == DW_ATE_signed_fixed);
  284. }
  285. static int die_get_byte_size(Dwarf_Die *tp_die)
  286. {
  287. Dwarf_Attribute attr;
  288. Dwarf_Word ret;
  289. if (dwarf_attr(tp_die, DW_AT_byte_size, &attr) == NULL ||
  290. dwarf_formudata(&attr, &ret) != 0)
  291. return 0;
  292. return (int)ret;
  293. }
  294. /* Get data_member_location offset */
  295. static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
  296. {
  297. Dwarf_Attribute attr;
  298. Dwarf_Op *expr;
  299. size_t nexpr;
  300. int ret;
  301. if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
  302. return -ENOENT;
  303. if (dwarf_formudata(&attr, offs) != 0) {
  304. /* DW_AT_data_member_location should be DW_OP_plus_uconst */
  305. ret = dwarf_getlocation(&attr, &expr, &nexpr);
  306. if (ret < 0 || nexpr == 0)
  307. return -ENOENT;
  308. if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
  309. pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
  310. expr[0].atom, nexpr);
  311. return -ENOTSUP;
  312. }
  313. *offs = (Dwarf_Word)expr[0].number;
  314. }
  315. return 0;
  316. }
  317. /* Return values for die_find callbacks */
  318. enum {
  319. DIE_FIND_CB_FOUND = 0, /* End of Search */
  320. DIE_FIND_CB_CHILD = 1, /* Search only children */
  321. DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
  322. DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
  323. };
  324. /* Search a child die */
  325. static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
  326. int (*callback)(Dwarf_Die *, void *),
  327. void *data, Dwarf_Die *die_mem)
  328. {
  329. Dwarf_Die child_die;
  330. int ret;
  331. ret = dwarf_child(rt_die, die_mem);
  332. if (ret != 0)
  333. return NULL;
  334. do {
  335. ret = callback(die_mem, data);
  336. if (ret == DIE_FIND_CB_FOUND)
  337. return die_mem;
  338. if ((ret & DIE_FIND_CB_CHILD) &&
  339. die_find_child(die_mem, callback, data, &child_die)) {
  340. memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
  341. return die_mem;
  342. }
  343. } while ((ret & DIE_FIND_CB_SIBLING) &&
  344. dwarf_siblingof(die_mem, die_mem) == 0);
  345. return NULL;
  346. }
  347. struct __addr_die_search_param {
  348. Dwarf_Addr addr;
  349. Dwarf_Die *die_mem;
  350. };
  351. static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
  352. {
  353. struct __addr_die_search_param *ad = data;
  354. if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
  355. dwarf_haspc(fn_die, ad->addr)) {
  356. memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
  357. return DWARF_CB_ABORT;
  358. }
  359. return DWARF_CB_OK;
  360. }
  361. /* Search a real subprogram including this line, */
  362. static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
  363. Dwarf_Die *die_mem)
  364. {
  365. struct __addr_die_search_param ad;
  366. ad.addr = addr;
  367. ad.die_mem = die_mem;
  368. /* dwarf_getscopes can't find subprogram. */
  369. if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
  370. return NULL;
  371. else
  372. return die_mem;
  373. }
  374. /* die_find callback for inline function search */
  375. static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
  376. {
  377. Dwarf_Addr *addr = data;
  378. if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
  379. dwarf_haspc(die_mem, *addr))
  380. return DIE_FIND_CB_FOUND;
  381. return DIE_FIND_CB_CONTINUE;
  382. }
  383. /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
  384. static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
  385. Dwarf_Die *die_mem)
  386. {
  387. return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
  388. }
  389. struct __find_variable_param {
  390. const char *name;
  391. Dwarf_Addr addr;
  392. };
  393. static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
  394. {
  395. struct __find_variable_param *fvp = data;
  396. int tag;
  397. tag = dwarf_tag(die_mem);
  398. if ((tag == DW_TAG_formal_parameter ||
  399. tag == DW_TAG_variable) &&
  400. die_compare_name(die_mem, fvp->name))
  401. return DIE_FIND_CB_FOUND;
  402. if (dwarf_haspc(die_mem, fvp->addr))
  403. return DIE_FIND_CB_CONTINUE;
  404. else
  405. return DIE_FIND_CB_SIBLING;
  406. }
  407. /* Find a variable called 'name' at given address */
  408. static Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
  409. Dwarf_Addr addr, Dwarf_Die *die_mem)
  410. {
  411. struct __find_variable_param fvp = { .name = name, .addr = addr};
  412. return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp,
  413. die_mem);
  414. }
  415. static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
  416. {
  417. const char *name = data;
  418. if ((dwarf_tag(die_mem) == DW_TAG_member) &&
  419. die_compare_name(die_mem, name))
  420. return DIE_FIND_CB_FOUND;
  421. return DIE_FIND_CB_SIBLING;
  422. }
  423. /* Find a member called 'name' */
  424. static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
  425. Dwarf_Die *die_mem)
  426. {
  427. return die_find_child(st_die, __die_find_member_cb, (void *)name,
  428. die_mem);
  429. }
  430. /* Get the name of given variable DIE */
  431. static int die_get_typename(Dwarf_Die *vr_die, char *buf, int len)
  432. {
  433. Dwarf_Die type;
  434. int tag, ret, ret2;
  435. const char *tmp = "";
  436. if (__die_get_real_type(vr_die, &type) == NULL)
  437. return -ENOENT;
  438. tag = dwarf_tag(&type);
  439. if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)
  440. tmp = "*";
  441. else if (tag == DW_TAG_subroutine_type) {
  442. /* Function pointer */
  443. ret = snprintf(buf, len, "(function_type)");
  444. return (ret >= len) ? -E2BIG : ret;
  445. } else {
  446. if (!dwarf_diename(&type))
  447. return -ENOENT;
  448. if (tag == DW_TAG_union_type)
  449. tmp = "union ";
  450. else if (tag == DW_TAG_structure_type)
  451. tmp = "struct ";
  452. /* Write a base name */
  453. ret = snprintf(buf, len, "%s%s", tmp, dwarf_diename(&type));
  454. return (ret >= len) ? -E2BIG : ret;
  455. }
  456. ret = die_get_typename(&type, buf, len);
  457. if (ret > 0) {
  458. ret2 = snprintf(buf + ret, len - ret, "%s", tmp);
  459. ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
  460. }
  461. return ret;
  462. }
  463. /* Get the name and type of given variable DIE, stored as "type\tname" */
  464. static int die_get_varname(Dwarf_Die *vr_die, char *buf, int len)
  465. {
  466. int ret, ret2;
  467. ret = die_get_typename(vr_die, buf, len);
  468. if (ret < 0) {
  469. pr_debug("Failed to get type, make it unknown.\n");
  470. ret = snprintf(buf, len, "(unknown_type)");
  471. }
  472. if (ret > 0) {
  473. ret2 = snprintf(buf + ret, len - ret, "\t%s",
  474. dwarf_diename(vr_die));
  475. ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
  476. }
  477. return ret;
  478. }
  479. /*
  480. * Probe finder related functions
  481. */
  482. static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
  483. {
  484. struct probe_trace_arg_ref *ref;
  485. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  486. if (ref != NULL)
  487. ref->offset = offs;
  488. return ref;
  489. }
  490. /*
  491. * Convert a location into trace_arg.
  492. * If tvar == NULL, this just checks variable can be converted.
  493. */
  494. static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr,
  495. Dwarf_Op *fb_ops,
  496. struct probe_trace_arg *tvar)
  497. {
  498. Dwarf_Attribute attr;
  499. Dwarf_Op *op;
  500. size_t nops;
  501. unsigned int regn;
  502. Dwarf_Word offs = 0;
  503. bool ref = false;
  504. const char *regs;
  505. int ret;
  506. if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
  507. goto static_var;
  508. /* TODO: handle more than 1 exprs */
  509. if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL ||
  510. dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0 ||
  511. nops == 0) {
  512. /* TODO: Support const_value */
  513. return -ENOENT;
  514. }
  515. if (op->atom == DW_OP_addr) {
  516. static_var:
  517. if (!tvar)
  518. return 0;
  519. /* Static variables on memory (not stack), make @varname */
  520. ret = strlen(dwarf_diename(vr_die));
  521. tvar->value = zalloc(ret + 2);
  522. if (tvar->value == NULL)
  523. return -ENOMEM;
  524. snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
  525. tvar->ref = alloc_trace_arg_ref((long)offs);
  526. if (tvar->ref == NULL)
  527. return -ENOMEM;
  528. return 0;
  529. }
  530. /* If this is based on frame buffer, set the offset */
  531. if (op->atom == DW_OP_fbreg) {
  532. if (fb_ops == NULL)
  533. return -ENOTSUP;
  534. ref = true;
  535. offs = op->number;
  536. op = &fb_ops[0];
  537. }
  538. if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
  539. regn = op->atom - DW_OP_breg0;
  540. offs += op->number;
  541. ref = true;
  542. } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
  543. regn = op->atom - DW_OP_reg0;
  544. } else if (op->atom == DW_OP_bregx) {
  545. regn = op->number;
  546. offs += op->number2;
  547. ref = true;
  548. } else if (op->atom == DW_OP_regx) {
  549. regn = op->number;
  550. } else {
  551. pr_debug("DW_OP %x is not supported.\n", op->atom);
  552. return -ENOTSUP;
  553. }
  554. if (!tvar)
  555. return 0;
  556. regs = get_arch_regstr(regn);
  557. if (!regs) {
  558. /* This should be a bug in DWARF or this tool */
  559. pr_warning("Mapping for the register number %u "
  560. "missing on this architecture.\n", regn);
  561. return -ERANGE;
  562. }
  563. tvar->value = strdup(regs);
  564. if (tvar->value == NULL)
  565. return -ENOMEM;
  566. if (ref) {
  567. tvar->ref = alloc_trace_arg_ref((long)offs);
  568. if (tvar->ref == NULL)
  569. return -ENOMEM;
  570. }
  571. return 0;
  572. }
  573. static int convert_variable_type(Dwarf_Die *vr_die,
  574. struct probe_trace_arg *tvar,
  575. const char *cast)
  576. {
  577. struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
  578. Dwarf_Die type;
  579. char buf[16];
  580. int ret;
  581. /* TODO: check all types */
  582. if (cast && strcmp(cast, "string") != 0) {
  583. /* Non string type is OK */
  584. tvar->type = strdup(cast);
  585. return (tvar->type == NULL) ? -ENOMEM : 0;
  586. }
  587. if (die_get_real_type(vr_die, &type) == NULL) {
  588. pr_warning("Failed to get a type information of %s.\n",
  589. dwarf_diename(vr_die));
  590. return -ENOENT;
  591. }
  592. pr_debug("%s type is %s.\n",
  593. dwarf_diename(vr_die), dwarf_diename(&type));
  594. if (cast && strcmp(cast, "string") == 0) { /* String type */
  595. ret = dwarf_tag(&type);
  596. if (ret != DW_TAG_pointer_type &&
  597. ret != DW_TAG_array_type) {
  598. pr_warning("Failed to cast into string: "
  599. "%s(%s) is not a pointer nor array.\n",
  600. dwarf_diename(vr_die), dwarf_diename(&type));
  601. return -EINVAL;
  602. }
  603. if (ret == DW_TAG_pointer_type) {
  604. if (die_get_real_type(&type, &type) == NULL) {
  605. pr_warning("Failed to get a type"
  606. " information.\n");
  607. return -ENOENT;
  608. }
  609. while (*ref_ptr)
  610. ref_ptr = &(*ref_ptr)->next;
  611. /* Add new reference with offset +0 */
  612. *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
  613. if (*ref_ptr == NULL) {
  614. pr_warning("Out of memory error\n");
  615. return -ENOMEM;
  616. }
  617. }
  618. if (!die_compare_name(&type, "char") &&
  619. !die_compare_name(&type, "unsigned char")) {
  620. pr_warning("Failed to cast into string: "
  621. "%s is not (unsigned) char *.\n",
  622. dwarf_diename(vr_die));
  623. return -EINVAL;
  624. }
  625. tvar->type = strdup(cast);
  626. return (tvar->type == NULL) ? -ENOMEM : 0;
  627. }
  628. ret = die_get_byte_size(&type) * 8;
  629. if (ret) {
  630. /* Check the bitwidth */
  631. if (ret > MAX_BASIC_TYPE_BITS) {
  632. pr_info("%s exceeds max-bitwidth."
  633. " Cut down to %d bits.\n",
  634. dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
  635. ret = MAX_BASIC_TYPE_BITS;
  636. }
  637. ret = snprintf(buf, 16, "%c%d",
  638. die_is_signed_type(&type) ? 's' : 'u', ret);
  639. if (ret < 0 || ret >= 16) {
  640. if (ret >= 16)
  641. ret = -E2BIG;
  642. pr_warning("Failed to convert variable type: %s\n",
  643. strerror(-ret));
  644. return ret;
  645. }
  646. tvar->type = strdup(buf);
  647. if (tvar->type == NULL)
  648. return -ENOMEM;
  649. }
  650. return 0;
  651. }
  652. static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
  653. struct perf_probe_arg_field *field,
  654. struct probe_trace_arg_ref **ref_ptr,
  655. Dwarf_Die *die_mem)
  656. {
  657. struct probe_trace_arg_ref *ref = *ref_ptr;
  658. Dwarf_Die type;
  659. Dwarf_Word offs;
  660. int ret, tag;
  661. pr_debug("converting %s in %s\n", field->name, varname);
  662. if (die_get_real_type(vr_die, &type) == NULL) {
  663. pr_warning("Failed to get the type of %s.\n", varname);
  664. return -ENOENT;
  665. }
  666. pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
  667. tag = dwarf_tag(&type);
  668. if (field->name[0] == '[' &&
  669. (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
  670. if (field->next)
  671. /* Save original type for next field */
  672. memcpy(die_mem, &type, sizeof(*die_mem));
  673. /* Get the type of this array */
  674. if (die_get_real_type(&type, &type) == NULL) {
  675. pr_warning("Failed to get the type of %s.\n", varname);
  676. return -ENOENT;
  677. }
  678. pr_debug2("Array real type: (%x)\n",
  679. (unsigned)dwarf_dieoffset(&type));
  680. if (tag == DW_TAG_pointer_type) {
  681. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  682. if (ref == NULL)
  683. return -ENOMEM;
  684. if (*ref_ptr)
  685. (*ref_ptr)->next = ref;
  686. else
  687. *ref_ptr = ref;
  688. }
  689. ref->offset += die_get_byte_size(&type) * field->index;
  690. if (!field->next)
  691. /* Save vr_die for converting types */
  692. memcpy(die_mem, vr_die, sizeof(*die_mem));
  693. goto next;
  694. } else if (tag == DW_TAG_pointer_type) {
  695. /* Check the pointer and dereference */
  696. if (!field->ref) {
  697. pr_err("Semantic error: %s must be referred by '->'\n",
  698. field->name);
  699. return -EINVAL;
  700. }
  701. /* Get the type pointed by this pointer */
  702. if (die_get_real_type(&type, &type) == NULL) {
  703. pr_warning("Failed to get the type of %s.\n", varname);
  704. return -ENOENT;
  705. }
  706. /* Verify it is a data structure */
  707. if (dwarf_tag(&type) != DW_TAG_structure_type) {
  708. pr_warning("%s is not a data structure.\n", varname);
  709. return -EINVAL;
  710. }
  711. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  712. if (ref == NULL)
  713. return -ENOMEM;
  714. if (*ref_ptr)
  715. (*ref_ptr)->next = ref;
  716. else
  717. *ref_ptr = ref;
  718. } else {
  719. /* Verify it is a data structure */
  720. if (tag != DW_TAG_structure_type) {
  721. pr_warning("%s is not a data structure.\n", varname);
  722. return -EINVAL;
  723. }
  724. if (field->name[0] == '[') {
  725. pr_err("Semantic error: %s is not a pointor"
  726. " nor array.\n", varname);
  727. return -EINVAL;
  728. }
  729. if (field->ref) {
  730. pr_err("Semantic error: %s must be referred by '.'\n",
  731. field->name);
  732. return -EINVAL;
  733. }
  734. if (!ref) {
  735. pr_warning("Structure on a register is not "
  736. "supported yet.\n");
  737. return -ENOTSUP;
  738. }
  739. }
  740. if (die_find_member(&type, field->name, die_mem) == NULL) {
  741. pr_warning("%s(tyep:%s) has no member %s.\n", varname,
  742. dwarf_diename(&type), field->name);
  743. return -EINVAL;
  744. }
  745. /* Get the offset of the field */
  746. ret = die_get_data_member_location(die_mem, &offs);
  747. if (ret < 0) {
  748. pr_warning("Failed to get the offset of %s.\n", field->name);
  749. return ret;
  750. }
  751. ref->offset += (long)offs;
  752. next:
  753. /* Converting next field */
  754. if (field->next)
  755. return convert_variable_fields(die_mem, field->name,
  756. field->next, &ref, die_mem);
  757. else
  758. return 0;
  759. }
  760. /* Show a variables in kprobe event format */
  761. static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
  762. {
  763. Dwarf_Die die_mem;
  764. int ret;
  765. pr_debug("Converting variable %s into trace event.\n",
  766. dwarf_diename(vr_die));
  767. ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops,
  768. pf->tvar);
  769. if (ret == -ENOENT)
  770. pr_err("Failed to find the location of %s at this address.\n"
  771. " Perhaps, it has been optimized out.\n", pf->pvar->var);
  772. else if (ret == -ENOTSUP)
  773. pr_err("Sorry, we don't support this variable location yet.\n");
  774. else if (pf->pvar->field) {
  775. ret = convert_variable_fields(vr_die, pf->pvar->var,
  776. pf->pvar->field, &pf->tvar->ref,
  777. &die_mem);
  778. vr_die = &die_mem;
  779. }
  780. if (ret == 0)
  781. ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
  782. /* *expr will be cached in libdw. Don't free it. */
  783. return ret;
  784. }
  785. /* Find a variable in a subprogram die */
  786. static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
  787. {
  788. Dwarf_Die vr_die, *scopes;
  789. char buf[32], *ptr;
  790. int ret, nscopes;
  791. if (!is_c_varname(pf->pvar->var)) {
  792. /* Copy raw parameters */
  793. pf->tvar->value = strdup(pf->pvar->var);
  794. if (pf->tvar->value == NULL)
  795. return -ENOMEM;
  796. if (pf->pvar->type) {
  797. pf->tvar->type = strdup(pf->pvar->type);
  798. if (pf->tvar->type == NULL)
  799. return -ENOMEM;
  800. }
  801. if (pf->pvar->name) {
  802. pf->tvar->name = strdup(pf->pvar->name);
  803. if (pf->tvar->name == NULL)
  804. return -ENOMEM;
  805. } else
  806. pf->tvar->name = NULL;
  807. return 0;
  808. }
  809. if (pf->pvar->name)
  810. pf->tvar->name = strdup(pf->pvar->name);
  811. else {
  812. ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
  813. if (ret < 0)
  814. return ret;
  815. ptr = strchr(buf, ':'); /* Change type separator to _ */
  816. if (ptr)
  817. *ptr = '_';
  818. pf->tvar->name = strdup(buf);
  819. }
  820. if (pf->tvar->name == NULL)
  821. return -ENOMEM;
  822. pr_debug("Searching '%s' variable in context.\n",
  823. pf->pvar->var);
  824. /* Search child die for local variables and parameters. */
  825. if (die_find_variable_at(sp_die, pf->pvar->var, pf->addr, &vr_die))
  826. ret = convert_variable(&vr_die, pf);
  827. else {
  828. /* Search upper class */
  829. nscopes = dwarf_getscopes_die(sp_die, &scopes);
  830. while (nscopes-- > 1) {
  831. pr_debug("Searching variables in %s\n",
  832. dwarf_diename(&scopes[nscopes]));
  833. /* We should check this scope, so give dummy address */
  834. if (die_find_variable_at(&scopes[nscopes],
  835. pf->pvar->var, 0,
  836. &vr_die)) {
  837. ret = convert_variable(&vr_die, pf);
  838. goto found;
  839. }
  840. }
  841. if (scopes)
  842. free(scopes);
  843. ret = -ENOENT;
  844. }
  845. found:
  846. if (ret < 0)
  847. pr_warning("Failed to find '%s' in this function.\n",
  848. pf->pvar->var);
  849. return ret;
  850. }
  851. /* Convert subprogram DIE to trace point */
  852. static int convert_to_trace_point(Dwarf_Die *sp_die, Dwarf_Addr paddr,
  853. bool retprobe, struct probe_trace_point *tp)
  854. {
  855. Dwarf_Addr eaddr;
  856. const char *name;
  857. /* Copy the name of probe point */
  858. name = dwarf_diename(sp_die);
  859. if (name) {
  860. if (dwarf_entrypc(sp_die, &eaddr) != 0) {
  861. pr_warning("Failed to get entry address of %s\n",
  862. dwarf_diename(sp_die));
  863. return -ENOENT;
  864. }
  865. tp->symbol = strdup(name);
  866. if (tp->symbol == NULL)
  867. return -ENOMEM;
  868. tp->offset = (unsigned long)(paddr - eaddr);
  869. } else
  870. /* This function has no name. */
  871. tp->offset = (unsigned long)paddr;
  872. /* Return probe must be on the head of a subprogram */
  873. if (retprobe) {
  874. if (eaddr != paddr) {
  875. pr_warning("Return probe must be on the head of"
  876. " a real function.\n");
  877. return -EINVAL;
  878. }
  879. tp->retprobe = true;
  880. }
  881. return 0;
  882. }
  883. /* Call probe_finder callback with real subprogram DIE */
  884. static int call_probe_finder(Dwarf_Die *sp_die, struct probe_finder *pf)
  885. {
  886. Dwarf_Die die_mem;
  887. Dwarf_Attribute fb_attr;
  888. size_t nops;
  889. int ret;
  890. /* If no real subprogram, find a real one */
  891. if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
  892. sp_die = die_find_real_subprogram(&pf->cu_die,
  893. pf->addr, &die_mem);
  894. if (!sp_die) {
  895. pr_warning("Failed to find probe point in any "
  896. "functions.\n");
  897. return -ENOENT;
  898. }
  899. }
  900. /* Get the frame base attribute/ops */
  901. dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
  902. ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
  903. if (ret <= 0 || nops == 0) {
  904. pf->fb_ops = NULL;
  905. #if _ELFUTILS_PREREQ(0, 142)
  906. } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
  907. pf->cfi != NULL) {
  908. Dwarf_Frame *frame;
  909. if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
  910. dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
  911. pr_warning("Failed to get call frame on 0x%jx\n",
  912. (uintmax_t)pf->addr);
  913. return -ENOENT;
  914. }
  915. #endif
  916. }
  917. /* Call finder's callback handler */
  918. ret = pf->callback(sp_die, pf);
  919. /* *pf->fb_ops will be cached in libdw. Don't free it. */
  920. pf->fb_ops = NULL;
  921. return ret;
  922. }
  923. /* Find probe point from its line number */
  924. static int find_probe_point_by_line(struct probe_finder *pf)
  925. {
  926. Dwarf_Lines *lines;
  927. Dwarf_Line *line;
  928. size_t nlines, i;
  929. Dwarf_Addr addr;
  930. int lineno;
  931. int ret = 0;
  932. if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
  933. pr_warning("No source lines found.\n");
  934. return -ENOENT;
  935. }
  936. for (i = 0; i < nlines && ret == 0; i++) {
  937. line = dwarf_onesrcline(lines, i);
  938. if (dwarf_lineno(line, &lineno) != 0 ||
  939. lineno != pf->lno)
  940. continue;
  941. /* TODO: Get fileno from line, but how? */
  942. if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
  943. continue;
  944. if (dwarf_lineaddr(line, &addr) != 0) {
  945. pr_warning("Failed to get the address of the line.\n");
  946. return -ENOENT;
  947. }
  948. pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
  949. (int)i, lineno, (uintmax_t)addr);
  950. pf->addr = addr;
  951. ret = call_probe_finder(NULL, pf);
  952. /* Continuing, because target line might be inlined. */
  953. }
  954. return ret;
  955. }
  956. /* Find lines which match lazy pattern */
  957. static int find_lazy_match_lines(struct list_head *head,
  958. const char *fname, const char *pat)
  959. {
  960. char *fbuf, *p1, *p2;
  961. int fd, line, nlines = -1;
  962. struct stat st;
  963. fd = open(fname, O_RDONLY);
  964. if (fd < 0) {
  965. pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
  966. return -errno;
  967. }
  968. if (fstat(fd, &st) < 0) {
  969. pr_warning("Failed to get the size of %s: %s\n",
  970. fname, strerror(errno));
  971. nlines = -errno;
  972. goto out_close;
  973. }
  974. nlines = -ENOMEM;
  975. fbuf = malloc(st.st_size + 2);
  976. if (fbuf == NULL)
  977. goto out_close;
  978. if (read(fd, fbuf, st.st_size) < 0) {
  979. pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
  980. nlines = -errno;
  981. goto out_free_fbuf;
  982. }
  983. fbuf[st.st_size] = '\n'; /* Dummy line */
  984. fbuf[st.st_size + 1] = '\0';
  985. p1 = fbuf;
  986. line = 1;
  987. nlines = 0;
  988. while ((p2 = strchr(p1, '\n')) != NULL) {
  989. *p2 = '\0';
  990. if (strlazymatch(p1, pat)) {
  991. line_list__add_line(head, line);
  992. nlines++;
  993. }
  994. line++;
  995. p1 = p2 + 1;
  996. }
  997. out_free_fbuf:
  998. free(fbuf);
  999. out_close:
  1000. close(fd);
  1001. return nlines;
  1002. }
  1003. /* Find probe points from lazy pattern */
  1004. static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
  1005. {
  1006. Dwarf_Lines *lines;
  1007. Dwarf_Line *line;
  1008. size_t nlines, i;
  1009. Dwarf_Addr addr;
  1010. Dwarf_Die die_mem;
  1011. int lineno;
  1012. int ret = 0;
  1013. if (list_empty(&pf->lcache)) {
  1014. /* Matching lazy line pattern */
  1015. ret = find_lazy_match_lines(&pf->lcache, pf->fname,
  1016. pf->pev->point.lazy_line);
  1017. if (ret == 0) {
  1018. pr_debug("No matched lines found in %s.\n", pf->fname);
  1019. return 0;
  1020. } else if (ret < 0)
  1021. return ret;
  1022. }
  1023. if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
  1024. pr_warning("No source lines found.\n");
  1025. return -ENOENT;
  1026. }
  1027. for (i = 0; i < nlines && ret >= 0; i++) {
  1028. line = dwarf_onesrcline(lines, i);
  1029. if (dwarf_lineno(line, &lineno) != 0 ||
  1030. !line_list__has_line(&pf->lcache, lineno))
  1031. continue;
  1032. /* TODO: Get fileno from line, but how? */
  1033. if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
  1034. continue;
  1035. if (dwarf_lineaddr(line, &addr) != 0) {
  1036. pr_debug("Failed to get the address of line %d.\n",
  1037. lineno);
  1038. continue;
  1039. }
  1040. if (sp_die) {
  1041. /* Address filtering 1: does sp_die include addr? */
  1042. if (!dwarf_haspc(sp_die, addr))
  1043. continue;
  1044. /* Address filtering 2: No child include addr? */
  1045. if (die_find_inlinefunc(sp_die, addr, &die_mem))
  1046. continue;
  1047. }
  1048. pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
  1049. (int)i, lineno, (unsigned long long)addr);
  1050. pf->addr = addr;
  1051. ret = call_probe_finder(sp_die, pf);
  1052. /* Continuing, because target line might be inlined. */
  1053. }
  1054. /* TODO: deallocate lines, but how? */
  1055. return ret;
  1056. }
  1057. /* Callback parameter with return value */
  1058. struct dwarf_callback_param {
  1059. void *data;
  1060. int retval;
  1061. };
  1062. static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
  1063. {
  1064. struct dwarf_callback_param *param = data;
  1065. struct probe_finder *pf = param->data;
  1066. struct perf_probe_point *pp = &pf->pev->point;
  1067. Dwarf_Addr addr;
  1068. if (pp->lazy_line)
  1069. param->retval = find_probe_point_lazy(in_die, pf);
  1070. else {
  1071. /* Get probe address */
  1072. if (dwarf_entrypc(in_die, &addr) != 0) {
  1073. pr_warning("Failed to get entry address of %s.\n",
  1074. dwarf_diename(in_die));
  1075. param->retval = -ENOENT;
  1076. return DWARF_CB_ABORT;
  1077. }
  1078. pf->addr = addr;
  1079. pf->addr += pp->offset;
  1080. pr_debug("found inline addr: 0x%jx\n",
  1081. (uintmax_t)pf->addr);
  1082. param->retval = call_probe_finder(in_die, pf);
  1083. if (param->retval < 0)
  1084. return DWARF_CB_ABORT;
  1085. }
  1086. return DWARF_CB_OK;
  1087. }
  1088. /* Search function from function name */
  1089. static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
  1090. {
  1091. struct dwarf_callback_param *param = data;
  1092. struct probe_finder *pf = param->data;
  1093. struct perf_probe_point *pp = &pf->pev->point;
  1094. /* Check tag and diename */
  1095. if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
  1096. !die_compare_name(sp_die, pp->function))
  1097. return DWARF_CB_OK;
  1098. pf->fname = dwarf_decl_file(sp_die);
  1099. if (pp->line) { /* Function relative line */
  1100. dwarf_decl_line(sp_die, &pf->lno);
  1101. pf->lno += pp->line;
  1102. param->retval = find_probe_point_by_line(pf);
  1103. } else if (!dwarf_func_inline(sp_die)) {
  1104. /* Real function */
  1105. if (pp->lazy_line)
  1106. param->retval = find_probe_point_lazy(sp_die, pf);
  1107. else {
  1108. if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
  1109. pr_warning("Failed to get entry address of "
  1110. "%s.\n", dwarf_diename(sp_die));
  1111. param->retval = -ENOENT;
  1112. return DWARF_CB_ABORT;
  1113. }
  1114. pf->addr += pp->offset;
  1115. /* TODO: Check the address in this function */
  1116. param->retval = call_probe_finder(sp_die, pf);
  1117. }
  1118. } else {
  1119. struct dwarf_callback_param _param = {.data = (void *)pf,
  1120. .retval = 0};
  1121. /* Inlined function: search instances */
  1122. dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
  1123. &_param);
  1124. param->retval = _param.retval;
  1125. }
  1126. return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
  1127. }
  1128. static int find_probe_point_by_func(struct probe_finder *pf)
  1129. {
  1130. struct dwarf_callback_param _param = {.data = (void *)pf,
  1131. .retval = 0};
  1132. dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
  1133. return _param.retval;
  1134. }
  1135. /* Find probe points from debuginfo */
  1136. static int find_probes(int fd, struct probe_finder *pf)
  1137. {
  1138. struct perf_probe_point *pp = &pf->pev->point;
  1139. Dwarf_Off off, noff;
  1140. size_t cuhl;
  1141. Dwarf_Die *diep;
  1142. Dwarf *dbg = NULL;
  1143. Dwfl *dwfl;
  1144. Dwarf_Addr bias; /* Currently ignored */
  1145. int ret = 0;
  1146. dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
  1147. if (!dbg) {
  1148. pr_warning("No debug information found in the vmlinux - "
  1149. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  1150. return -EBADF;
  1151. }
  1152. #if _ELFUTILS_PREREQ(0, 142)
  1153. /* Get the call frame information from this dwarf */
  1154. pf->cfi = dwarf_getcfi(dbg);
  1155. #endif
  1156. off = 0;
  1157. line_list__init(&pf->lcache);
  1158. /* Loop on CUs (Compilation Unit) */
  1159. while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) &&
  1160. ret >= 0) {
  1161. /* Get the DIE(Debugging Information Entry) of this CU */
  1162. diep = dwarf_offdie(dbg, off + cuhl, &pf->cu_die);
  1163. if (!diep)
  1164. continue;
  1165. /* Check if target file is included. */
  1166. if (pp->file)
  1167. pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
  1168. else
  1169. pf->fname = NULL;
  1170. if (!pp->file || pf->fname) {
  1171. if (pp->function)
  1172. ret = find_probe_point_by_func(pf);
  1173. else if (pp->lazy_line)
  1174. ret = find_probe_point_lazy(NULL, pf);
  1175. else {
  1176. pf->lno = pp->line;
  1177. ret = find_probe_point_by_line(pf);
  1178. }
  1179. }
  1180. off = noff;
  1181. }
  1182. line_list__free(&pf->lcache);
  1183. if (dwfl)
  1184. dwfl_end(dwfl);
  1185. return ret;
  1186. }
  1187. /* Add a found probe point into trace event list */
  1188. static int add_probe_trace_event(Dwarf_Die *sp_die, struct probe_finder *pf)
  1189. {
  1190. struct trace_event_finder *tf =
  1191. container_of(pf, struct trace_event_finder, pf);
  1192. struct probe_trace_event *tev;
  1193. int ret, i;
  1194. /* Check number of tevs */
  1195. if (tf->ntevs == tf->max_tevs) {
  1196. pr_warning("Too many( > %d) probe point found.\n",
  1197. tf->max_tevs);
  1198. return -ERANGE;
  1199. }
  1200. tev = &tf->tevs[tf->ntevs++];
  1201. ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
  1202. &tev->point);
  1203. if (ret < 0)
  1204. return ret;
  1205. pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
  1206. tev->point.offset);
  1207. /* Find each argument */
  1208. tev->nargs = pf->pev->nargs;
  1209. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  1210. if (tev->args == NULL)
  1211. return -ENOMEM;
  1212. for (i = 0; i < pf->pev->nargs; i++) {
  1213. pf->pvar = &pf->pev->args[i];
  1214. pf->tvar = &tev->args[i];
  1215. ret = find_variable(sp_die, pf);
  1216. if (ret != 0)
  1217. return ret;
  1218. }
  1219. return 0;
  1220. }
  1221. /* Find probe_trace_events specified by perf_probe_event from debuginfo */
  1222. int find_probe_trace_events(int fd, struct perf_probe_event *pev,
  1223. struct probe_trace_event **tevs, int max_tevs)
  1224. {
  1225. struct trace_event_finder tf = {
  1226. .pf = {.pev = pev, .callback = add_probe_trace_event},
  1227. .max_tevs = max_tevs};
  1228. int ret;
  1229. /* Allocate result tevs array */
  1230. *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
  1231. if (*tevs == NULL)
  1232. return -ENOMEM;
  1233. tf.tevs = *tevs;
  1234. tf.ntevs = 0;
  1235. ret = find_probes(fd, &tf.pf);
  1236. if (ret < 0) {
  1237. free(*tevs);
  1238. *tevs = NULL;
  1239. return ret;
  1240. }
  1241. return (ret < 0) ? ret : tf.ntevs;
  1242. }
  1243. #define MAX_VAR_LEN 64
  1244. /* Collect available variables in this scope */
  1245. static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
  1246. {
  1247. struct available_var_finder *af = data;
  1248. struct variable_list *vl;
  1249. char buf[MAX_VAR_LEN];
  1250. int tag, ret;
  1251. vl = &af->vls[af->nvls - 1];
  1252. tag = dwarf_tag(die_mem);
  1253. if (tag == DW_TAG_formal_parameter ||
  1254. tag == DW_TAG_variable) {
  1255. ret = convert_variable_location(die_mem, af->pf.addr,
  1256. af->pf.fb_ops, NULL);
  1257. if (ret == 0) {
  1258. ret = die_get_varname(die_mem, buf, MAX_VAR_LEN);
  1259. pr_debug2("Add new var: %s\n", buf);
  1260. if (ret > 0)
  1261. strlist__add(vl->vars, buf);
  1262. }
  1263. }
  1264. if (af->child && dwarf_haspc(die_mem, af->pf.addr))
  1265. return DIE_FIND_CB_CONTINUE;
  1266. else
  1267. return DIE_FIND_CB_SIBLING;
  1268. }
  1269. /* Add a found vars into available variables list */
  1270. static int add_available_vars(Dwarf_Die *sp_die, struct probe_finder *pf)
  1271. {
  1272. struct available_var_finder *af =
  1273. container_of(pf, struct available_var_finder, pf);
  1274. struct variable_list *vl;
  1275. Dwarf_Die die_mem, *scopes = NULL;
  1276. int ret, nscopes;
  1277. /* Check number of tevs */
  1278. if (af->nvls == af->max_vls) {
  1279. pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
  1280. return -ERANGE;
  1281. }
  1282. vl = &af->vls[af->nvls++];
  1283. ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
  1284. &vl->point);
  1285. if (ret < 0)
  1286. return ret;
  1287. pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
  1288. vl->point.offset);
  1289. /* Find local variables */
  1290. vl->vars = strlist__new(true, NULL);
  1291. if (vl->vars == NULL)
  1292. return -ENOMEM;
  1293. af->child = true;
  1294. die_find_child(sp_die, collect_variables_cb, (void *)af, &die_mem);
  1295. /* Find external variables */
  1296. if (!af->externs)
  1297. goto out;
  1298. /* Don't need to search child DIE for externs. */
  1299. af->child = false;
  1300. nscopes = dwarf_getscopes_die(sp_die, &scopes);
  1301. while (nscopes-- > 1)
  1302. die_find_child(&scopes[nscopes], collect_variables_cb,
  1303. (void *)af, &die_mem);
  1304. if (scopes)
  1305. free(scopes);
  1306. out:
  1307. if (strlist__empty(vl->vars)) {
  1308. strlist__delete(vl->vars);
  1309. vl->vars = NULL;
  1310. }
  1311. return ret;
  1312. }
  1313. /* Find available variables at given probe point */
  1314. int find_available_vars_at(int fd, struct perf_probe_event *pev,
  1315. struct variable_list **vls, int max_vls,
  1316. bool externs)
  1317. {
  1318. struct available_var_finder af = {
  1319. .pf = {.pev = pev, .callback = add_available_vars},
  1320. .max_vls = max_vls, .externs = externs};
  1321. int ret;
  1322. /* Allocate result vls array */
  1323. *vls = zalloc(sizeof(struct variable_list) * max_vls);
  1324. if (*vls == NULL)
  1325. return -ENOMEM;
  1326. af.vls = *vls;
  1327. af.nvls = 0;
  1328. ret = find_probes(fd, &af.pf);
  1329. if (ret < 0) {
  1330. /* Free vlist for error */
  1331. while (af.nvls--) {
  1332. if (af.vls[af.nvls].point.symbol)
  1333. free(af.vls[af.nvls].point.symbol);
  1334. if (af.vls[af.nvls].vars)
  1335. strlist__delete(af.vls[af.nvls].vars);
  1336. }
  1337. free(af.vls);
  1338. *vls = NULL;
  1339. return ret;
  1340. }
  1341. return (ret < 0) ? ret : af.nvls;
  1342. }
  1343. /* Reverse search */
  1344. int find_perf_probe_point(unsigned long addr, struct perf_probe_point *ppt)
  1345. {
  1346. Dwarf_Die cudie, spdie, indie;
  1347. Dwarf *dbg = NULL;
  1348. Dwfl *dwfl = NULL;
  1349. Dwarf_Line *line;
  1350. Dwarf_Addr laddr, eaddr, bias = 0;
  1351. const char *tmp;
  1352. int lineno, ret = 0;
  1353. bool found = false;
  1354. /* Open the live linux kernel */
  1355. dbg = dwfl_init_live_kernel_dwarf(addr, &dwfl, &bias);
  1356. if (!dbg) {
  1357. pr_warning("No debug information found in the vmlinux - "
  1358. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  1359. ret = -EINVAL;
  1360. goto end;
  1361. }
  1362. /* Adjust address with bias */
  1363. addr += bias;
  1364. /* Find cu die */
  1365. if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr - bias, &cudie)) {
  1366. pr_warning("Failed to find debug information for address %lx\n",
  1367. addr);
  1368. ret = -EINVAL;
  1369. goto end;
  1370. }
  1371. /* Find a corresponding line */
  1372. line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
  1373. if (line) {
  1374. if (dwarf_lineaddr(line, &laddr) == 0 &&
  1375. (Dwarf_Addr)addr == laddr &&
  1376. dwarf_lineno(line, &lineno) == 0) {
  1377. tmp = dwarf_linesrc(line, NULL, NULL);
  1378. if (tmp) {
  1379. ppt->line = lineno;
  1380. ppt->file = strdup(tmp);
  1381. if (ppt->file == NULL) {
  1382. ret = -ENOMEM;
  1383. goto end;
  1384. }
  1385. found = true;
  1386. }
  1387. }
  1388. }
  1389. /* Find a corresponding function */
  1390. if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
  1391. tmp = dwarf_diename(&spdie);
  1392. if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0)
  1393. goto end;
  1394. if (ppt->line) {
  1395. if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
  1396. &indie)) {
  1397. /* addr in an inline function */
  1398. tmp = dwarf_diename(&indie);
  1399. if (!tmp)
  1400. goto end;
  1401. ret = dwarf_decl_line(&indie, &lineno);
  1402. } else {
  1403. if (eaddr == addr) { /* Function entry */
  1404. lineno = ppt->line;
  1405. ret = 0;
  1406. } else
  1407. ret = dwarf_decl_line(&spdie, &lineno);
  1408. }
  1409. if (ret == 0) {
  1410. /* Make a relative line number */
  1411. ppt->line -= lineno;
  1412. goto found;
  1413. }
  1414. }
  1415. /* We don't have a line number, let's use offset */
  1416. ppt->offset = addr - (unsigned long)eaddr;
  1417. found:
  1418. ppt->function = strdup(tmp);
  1419. if (ppt->function == NULL) {
  1420. ret = -ENOMEM;
  1421. goto end;
  1422. }
  1423. found = true;
  1424. }
  1425. end:
  1426. if (dwfl)
  1427. dwfl_end(dwfl);
  1428. if (ret >= 0)
  1429. ret = found ? 1 : 0;
  1430. return ret;
  1431. }
  1432. /* Add a line and store the src path */
  1433. static int line_range_add_line(const char *src, unsigned int lineno,
  1434. struct line_range *lr)
  1435. {
  1436. /* Copy source path */
  1437. if (!lr->path) {
  1438. lr->path = strdup(src);
  1439. if (lr->path == NULL)
  1440. return -ENOMEM;
  1441. }
  1442. return line_list__add_line(&lr->line_list, lineno);
  1443. }
  1444. /* Search function declaration lines */
  1445. static int line_range_funcdecl_cb(Dwarf_Die *sp_die, void *data)
  1446. {
  1447. struct dwarf_callback_param *param = data;
  1448. struct line_finder *lf = param->data;
  1449. const char *src;
  1450. int lineno;
  1451. src = dwarf_decl_file(sp_die);
  1452. if (src && strtailcmp(src, lf->fname) != 0)
  1453. return DWARF_CB_OK;
  1454. if (dwarf_decl_line(sp_die, &lineno) != 0 ||
  1455. (lf->lno_s > lineno || lf->lno_e < lineno))
  1456. return DWARF_CB_OK;
  1457. param->retval = line_range_add_line(src, lineno, lf->lr);
  1458. if (param->retval < 0)
  1459. return DWARF_CB_ABORT;
  1460. return DWARF_CB_OK;
  1461. }
  1462. static int find_line_range_func_decl_lines(struct line_finder *lf)
  1463. {
  1464. struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
  1465. dwarf_getfuncs(&lf->cu_die, line_range_funcdecl_cb, &param, 0);
  1466. return param.retval;
  1467. }
  1468. /* Find line range from its line number */
  1469. static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
  1470. {
  1471. Dwarf_Lines *lines;
  1472. Dwarf_Line *line;
  1473. size_t nlines, i;
  1474. Dwarf_Addr addr;
  1475. int lineno, ret = 0;
  1476. const char *src;
  1477. Dwarf_Die die_mem;
  1478. line_list__init(&lf->lr->line_list);
  1479. if (dwarf_getsrclines(&lf->cu_die, &lines, &nlines) != 0) {
  1480. pr_warning("No source lines found.\n");
  1481. return -ENOENT;
  1482. }
  1483. /* Search probable lines on lines list */
  1484. for (i = 0; i < nlines; i++) {
  1485. line = dwarf_onesrcline(lines, i);
  1486. if (dwarf_lineno(line, &lineno) != 0 ||
  1487. (lf->lno_s > lineno || lf->lno_e < lineno))
  1488. continue;
  1489. if (sp_die) {
  1490. /* Address filtering 1: does sp_die include addr? */
  1491. if (dwarf_lineaddr(line, &addr) != 0 ||
  1492. !dwarf_haspc(sp_die, addr))
  1493. continue;
  1494. /* Address filtering 2: No child include addr? */
  1495. if (die_find_inlinefunc(sp_die, addr, &die_mem))
  1496. continue;
  1497. }
  1498. /* TODO: Get fileno from line, but how? */
  1499. src = dwarf_linesrc(line, NULL, NULL);
  1500. if (strtailcmp(src, lf->fname) != 0)
  1501. continue;
  1502. ret = line_range_add_line(src, lineno, lf->lr);
  1503. if (ret < 0)
  1504. return ret;
  1505. }
  1506. /*
  1507. * Dwarf lines doesn't include function declarations. We have to
  1508. * check functions list or given function.
  1509. */
  1510. if (sp_die) {
  1511. src = dwarf_decl_file(sp_die);
  1512. if (src && dwarf_decl_line(sp_die, &lineno) == 0 &&
  1513. (lf->lno_s <= lineno && lf->lno_e >= lineno))
  1514. ret = line_range_add_line(src, lineno, lf->lr);
  1515. } else
  1516. ret = find_line_range_func_decl_lines(lf);
  1517. /* Update status */
  1518. if (ret >= 0)
  1519. if (!list_empty(&lf->lr->line_list))
  1520. ret = lf->found = 1;
  1521. else
  1522. ret = 0; /* Lines are not found */
  1523. else {
  1524. free(lf->lr->path);
  1525. lf->lr->path = NULL;
  1526. }
  1527. return ret;
  1528. }
  1529. static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
  1530. {
  1531. struct dwarf_callback_param *param = data;
  1532. param->retval = find_line_range_by_line(in_die, param->data);
  1533. return DWARF_CB_ABORT; /* No need to find other instances */
  1534. }
  1535. /* Search function from function name */
  1536. static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
  1537. {
  1538. struct dwarf_callback_param *param = data;
  1539. struct line_finder *lf = param->data;
  1540. struct line_range *lr = lf->lr;
  1541. pr_debug("find (%llx) %s\n",
  1542. (unsigned long long)dwarf_dieoffset(sp_die),
  1543. dwarf_diename(sp_die));
  1544. if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
  1545. die_compare_name(sp_die, lr->function)) {
  1546. lf->fname = dwarf_decl_file(sp_die);
  1547. dwarf_decl_line(sp_die, &lr->offset);
  1548. pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
  1549. lf->lno_s = lr->offset + lr->start;
  1550. if (lf->lno_s < 0) /* Overflow */
  1551. lf->lno_s = INT_MAX;
  1552. lf->lno_e = lr->offset + lr->end;
  1553. if (lf->lno_e < 0) /* Overflow */
  1554. lf->lno_e = INT_MAX;
  1555. pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
  1556. lr->start = lf->lno_s;
  1557. lr->end = lf->lno_e;
  1558. if (dwarf_func_inline(sp_die)) {
  1559. struct dwarf_callback_param _param;
  1560. _param.data = (void *)lf;
  1561. _param.retval = 0;
  1562. dwarf_func_inline_instances(sp_die,
  1563. line_range_inline_cb,
  1564. &_param);
  1565. param->retval = _param.retval;
  1566. } else
  1567. param->retval = find_line_range_by_line(sp_die, lf);
  1568. return DWARF_CB_ABORT;
  1569. }
  1570. return DWARF_CB_OK;
  1571. }
  1572. static int find_line_range_by_func(struct line_finder *lf)
  1573. {
  1574. struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
  1575. dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
  1576. return param.retval;
  1577. }
  1578. int find_line_range(int fd, struct line_range *lr)
  1579. {
  1580. struct line_finder lf = {.lr = lr, .found = 0};
  1581. int ret = 0;
  1582. Dwarf_Off off = 0, noff;
  1583. size_t cuhl;
  1584. Dwarf_Die *diep;
  1585. Dwarf *dbg = NULL;
  1586. Dwfl *dwfl;
  1587. Dwarf_Addr bias; /* Currently ignored */
  1588. const char *comp_dir;
  1589. dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
  1590. if (!dbg) {
  1591. pr_warning("No debug information found in the vmlinux - "
  1592. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  1593. return -EBADF;
  1594. }
  1595. /* Loop on CUs (Compilation Unit) */
  1596. while (!lf.found && ret >= 0) {
  1597. if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
  1598. break;
  1599. /* Get the DIE(Debugging Information Entry) of this CU */
  1600. diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
  1601. if (!diep)
  1602. continue;
  1603. /* Check if target file is included. */
  1604. if (lr->file)
  1605. lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
  1606. else
  1607. lf.fname = 0;
  1608. if (!lr->file || lf.fname) {
  1609. if (lr->function)
  1610. ret = find_line_range_by_func(&lf);
  1611. else {
  1612. lf.lno_s = lr->start;
  1613. lf.lno_e = lr->end;
  1614. ret = find_line_range_by_line(NULL, &lf);
  1615. }
  1616. }
  1617. off = noff;
  1618. }
  1619. /* Store comp_dir */
  1620. if (lf.found) {
  1621. comp_dir = cu_get_comp_dir(&lf.cu_die);
  1622. if (comp_dir) {
  1623. lr->comp_dir = strdup(comp_dir);
  1624. if (!lr->comp_dir)
  1625. ret = -ENOMEM;
  1626. }
  1627. }
  1628. pr_debug("path: %s\n", lr->path);
  1629. dwfl_end(dwfl);
  1630. return (ret < 0) ? ret : lf.found;
  1631. }