probe-finder.c 48 KB

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