probe-finder.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940
  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. return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
  418. }
  419. /* Walker on lines (Note: line number will not be sorted) */
  420. typedef int (* line_walk_handler_t) (const char *fname, int lineno,
  421. Dwarf_Addr addr, void *data);
  422. struct __line_walk_param {
  423. const char *fname;
  424. line_walk_handler_t handler;
  425. void *data;
  426. int retval;
  427. };
  428. static int __die_walk_funclines_cb(Dwarf_Die *in_die, void *data)
  429. {
  430. struct __line_walk_param *lw = data;
  431. Dwarf_Addr addr;
  432. int lineno;
  433. if (dwarf_tag(in_die) == DW_TAG_inlined_subroutine) {
  434. lineno = die_get_call_lineno(in_die);
  435. if (lineno > 0 && dwarf_entrypc(in_die, &addr) == 0) {
  436. lw->retval = lw->handler(lw->fname, lineno, addr,
  437. lw->data);
  438. if (lw->retval != 0)
  439. return DIE_FIND_CB_FOUND;
  440. }
  441. }
  442. return DIE_FIND_CB_SIBLING;
  443. }
  444. /* Walk on lines of blocks included in given DIE */
  445. static int __die_walk_funclines(Dwarf_Die *sp_die,
  446. line_walk_handler_t handler, void *data)
  447. {
  448. struct __line_walk_param lw = {
  449. .handler = handler,
  450. .data = data,
  451. .retval = 0,
  452. };
  453. Dwarf_Die die_mem;
  454. Dwarf_Addr addr;
  455. int lineno;
  456. /* Handle function declaration line */
  457. lw.fname = dwarf_decl_file(sp_die);
  458. if (lw.fname && dwarf_decl_line(sp_die, &lineno) == 0 &&
  459. dwarf_entrypc(sp_die, &addr) == 0) {
  460. lw.retval = handler(lw.fname, lineno, addr, data);
  461. if (lw.retval != 0)
  462. goto done;
  463. }
  464. die_find_child(sp_die, __die_walk_funclines_cb, &lw, &die_mem);
  465. done:
  466. return lw.retval;
  467. }
  468. static int __die_walk_culines_cb(Dwarf_Die *sp_die, void *data)
  469. {
  470. struct __line_walk_param *lw = data;
  471. lw->retval = __die_walk_funclines(sp_die, lw->handler, lw->data);
  472. if (lw->retval != 0)
  473. return DWARF_CB_ABORT;
  474. return DWARF_CB_OK;
  475. }
  476. /*
  477. * Walk on lines inside given PDIE. If the PDIE is subprogram, walk only on
  478. * the lines inside the subprogram, otherwise PDIE must be a CU DIE.
  479. */
  480. static int die_walk_lines(Dwarf_Die *pdie, line_walk_handler_t handler,
  481. void *data)
  482. {
  483. Dwarf_Lines *lines;
  484. Dwarf_Line *line;
  485. Dwarf_Addr addr;
  486. const char *fname;
  487. int lineno, ret = 0;
  488. Dwarf_Die die_mem, *cu_die;
  489. size_t nlines, i;
  490. /* Get the CU die */
  491. if (dwarf_tag(pdie) == DW_TAG_subprogram)
  492. cu_die = dwarf_diecu(pdie, &die_mem, NULL, NULL);
  493. else
  494. cu_die = pdie;
  495. if (!cu_die) {
  496. pr_debug2("Failed to get CU from subprogram\n");
  497. return -EINVAL;
  498. }
  499. /* Get lines list in the CU */
  500. if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0) {
  501. pr_debug2("Failed to get source lines on this CU.\n");
  502. return -ENOENT;
  503. }
  504. pr_debug2("Get %zd lines from this CU\n", nlines);
  505. /* Walk on the lines on lines list */
  506. for (i = 0; i < nlines; i++) {
  507. line = dwarf_onesrcline(lines, i);
  508. if (line == NULL ||
  509. dwarf_lineno(line, &lineno) != 0 ||
  510. dwarf_lineaddr(line, &addr) != 0) {
  511. pr_debug2("Failed to get line info. "
  512. "Possible error in debuginfo.\n");
  513. continue;
  514. }
  515. /* Filter lines based on address */
  516. if (pdie != cu_die)
  517. /*
  518. * Address filtering
  519. * The line is included in given function, and
  520. * no inline block includes it.
  521. */
  522. if (!dwarf_haspc(pdie, addr) ||
  523. die_find_inlinefunc(pdie, addr, &die_mem))
  524. continue;
  525. /* Get source line */
  526. fname = dwarf_linesrc(line, NULL, NULL);
  527. ret = handler(fname, lineno, addr, data);
  528. if (ret != 0)
  529. return ret;
  530. }
  531. /*
  532. * Dwarf lines doesn't include function declarations and inlined
  533. * subroutines. We have to check functions list or given function.
  534. */
  535. if (pdie != cu_die)
  536. ret = __die_walk_funclines(pdie, handler, data);
  537. else {
  538. struct __line_walk_param param = {
  539. .handler = handler,
  540. .data = data,
  541. .retval = 0,
  542. };
  543. dwarf_getfuncs(cu_die, __die_walk_culines_cb, &param, 0);
  544. ret = param.retval;
  545. }
  546. return ret;
  547. }
  548. struct __find_variable_param {
  549. const char *name;
  550. Dwarf_Addr addr;
  551. };
  552. static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
  553. {
  554. struct __find_variable_param *fvp = data;
  555. int tag;
  556. tag = dwarf_tag(die_mem);
  557. if ((tag == DW_TAG_formal_parameter ||
  558. tag == DW_TAG_variable) &&
  559. die_compare_name(die_mem, fvp->name))
  560. return DIE_FIND_CB_FOUND;
  561. if (dwarf_haspc(die_mem, fvp->addr))
  562. return DIE_FIND_CB_CONTINUE;
  563. else
  564. return DIE_FIND_CB_SIBLING;
  565. }
  566. /* Find a variable called 'name' at given address */
  567. static Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
  568. Dwarf_Addr addr, Dwarf_Die *die_mem)
  569. {
  570. struct __find_variable_param fvp = { .name = name, .addr = addr};
  571. return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp,
  572. die_mem);
  573. }
  574. static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
  575. {
  576. const char *name = data;
  577. if ((dwarf_tag(die_mem) == DW_TAG_member) &&
  578. die_compare_name(die_mem, name))
  579. return DIE_FIND_CB_FOUND;
  580. return DIE_FIND_CB_SIBLING;
  581. }
  582. /* Find a member called 'name' */
  583. static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
  584. Dwarf_Die *die_mem)
  585. {
  586. return die_find_child(st_die, __die_find_member_cb, (void *)name,
  587. die_mem);
  588. }
  589. /* Get the name of given variable DIE */
  590. static int die_get_typename(Dwarf_Die *vr_die, char *buf, int len)
  591. {
  592. Dwarf_Die type;
  593. int tag, ret, ret2;
  594. const char *tmp = "";
  595. if (__die_get_real_type(vr_die, &type) == NULL)
  596. return -ENOENT;
  597. tag = dwarf_tag(&type);
  598. if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)
  599. tmp = "*";
  600. else if (tag == DW_TAG_subroutine_type) {
  601. /* Function pointer */
  602. ret = snprintf(buf, len, "(function_type)");
  603. return (ret >= len) ? -E2BIG : ret;
  604. } else {
  605. if (!dwarf_diename(&type))
  606. return -ENOENT;
  607. if (tag == DW_TAG_union_type)
  608. tmp = "union ";
  609. else if (tag == DW_TAG_structure_type)
  610. tmp = "struct ";
  611. /* Write a base name */
  612. ret = snprintf(buf, len, "%s%s", tmp, dwarf_diename(&type));
  613. return (ret >= len) ? -E2BIG : ret;
  614. }
  615. ret = die_get_typename(&type, buf, len);
  616. if (ret > 0) {
  617. ret2 = snprintf(buf + ret, len - ret, "%s", tmp);
  618. ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
  619. }
  620. return ret;
  621. }
  622. /* Get the name and type of given variable DIE, stored as "type\tname" */
  623. static int die_get_varname(Dwarf_Die *vr_die, char *buf, int len)
  624. {
  625. int ret, ret2;
  626. ret = die_get_typename(vr_die, buf, len);
  627. if (ret < 0) {
  628. pr_debug("Failed to get type, make it unknown.\n");
  629. ret = snprintf(buf, len, "(unknown_type)");
  630. }
  631. if (ret > 0) {
  632. ret2 = snprintf(buf + ret, len - ret, "\t%s",
  633. dwarf_diename(vr_die));
  634. ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
  635. }
  636. return ret;
  637. }
  638. /*
  639. * Probe finder related functions
  640. */
  641. static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
  642. {
  643. struct probe_trace_arg_ref *ref;
  644. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  645. if (ref != NULL)
  646. ref->offset = offs;
  647. return ref;
  648. }
  649. /*
  650. * Convert a location into trace_arg.
  651. * If tvar == NULL, this just checks variable can be converted.
  652. */
  653. static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr,
  654. Dwarf_Op *fb_ops,
  655. struct probe_trace_arg *tvar)
  656. {
  657. Dwarf_Attribute attr;
  658. Dwarf_Op *op;
  659. size_t nops;
  660. unsigned int regn;
  661. Dwarf_Word offs = 0;
  662. bool ref = false;
  663. const char *regs;
  664. int ret;
  665. if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
  666. goto static_var;
  667. /* TODO: handle more than 1 exprs */
  668. if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL ||
  669. dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0 ||
  670. nops == 0) {
  671. /* TODO: Support const_value */
  672. return -ENOENT;
  673. }
  674. if (op->atom == DW_OP_addr) {
  675. static_var:
  676. if (!tvar)
  677. return 0;
  678. /* Static variables on memory (not stack), make @varname */
  679. ret = strlen(dwarf_diename(vr_die));
  680. tvar->value = zalloc(ret + 2);
  681. if (tvar->value == NULL)
  682. return -ENOMEM;
  683. snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
  684. tvar->ref = alloc_trace_arg_ref((long)offs);
  685. if (tvar->ref == NULL)
  686. return -ENOMEM;
  687. return 0;
  688. }
  689. /* If this is based on frame buffer, set the offset */
  690. if (op->atom == DW_OP_fbreg) {
  691. if (fb_ops == NULL)
  692. return -ENOTSUP;
  693. ref = true;
  694. offs = op->number;
  695. op = &fb_ops[0];
  696. }
  697. if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
  698. regn = op->atom - DW_OP_breg0;
  699. offs += op->number;
  700. ref = true;
  701. } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
  702. regn = op->atom - DW_OP_reg0;
  703. } else if (op->atom == DW_OP_bregx) {
  704. regn = op->number;
  705. offs += op->number2;
  706. ref = true;
  707. } else if (op->atom == DW_OP_regx) {
  708. regn = op->number;
  709. } else {
  710. pr_debug("DW_OP %x is not supported.\n", op->atom);
  711. return -ENOTSUP;
  712. }
  713. if (!tvar)
  714. return 0;
  715. regs = get_arch_regstr(regn);
  716. if (!regs) {
  717. /* This should be a bug in DWARF or this tool */
  718. pr_warning("Mapping for the register number %u "
  719. "missing on this architecture.\n", regn);
  720. return -ERANGE;
  721. }
  722. tvar->value = strdup(regs);
  723. if (tvar->value == NULL)
  724. return -ENOMEM;
  725. if (ref) {
  726. tvar->ref = alloc_trace_arg_ref((long)offs);
  727. if (tvar->ref == NULL)
  728. return -ENOMEM;
  729. }
  730. return 0;
  731. }
  732. #define BYTES_TO_BITS(nb) ((nb) * BITS_PER_LONG / sizeof(long))
  733. static int convert_variable_type(Dwarf_Die *vr_die,
  734. struct probe_trace_arg *tvar,
  735. const char *cast)
  736. {
  737. struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
  738. Dwarf_Die type;
  739. char buf[16];
  740. int ret;
  741. /* TODO: check all types */
  742. if (cast && strcmp(cast, "string") != 0) {
  743. /* Non string type is OK */
  744. tvar->type = strdup(cast);
  745. return (tvar->type == NULL) ? -ENOMEM : 0;
  746. }
  747. if (die_get_bit_size(vr_die) != 0) {
  748. /* This is a bitfield */
  749. ret = snprintf(buf, 16, "b%d@%d/%zd", die_get_bit_size(vr_die),
  750. die_get_bit_offset(vr_die),
  751. BYTES_TO_BITS(die_get_byte_size(vr_die)));
  752. goto formatted;
  753. }
  754. if (die_get_real_type(vr_die, &type) == NULL) {
  755. pr_warning("Failed to get a type information of %s.\n",
  756. dwarf_diename(vr_die));
  757. return -ENOENT;
  758. }
  759. pr_debug("%s type is %s.\n",
  760. dwarf_diename(vr_die), dwarf_diename(&type));
  761. if (cast && strcmp(cast, "string") == 0) { /* String type */
  762. ret = dwarf_tag(&type);
  763. if (ret != DW_TAG_pointer_type &&
  764. ret != DW_TAG_array_type) {
  765. pr_warning("Failed to cast into string: "
  766. "%s(%s) is not a pointer nor array.\n",
  767. dwarf_diename(vr_die), dwarf_diename(&type));
  768. return -EINVAL;
  769. }
  770. if (ret == DW_TAG_pointer_type) {
  771. if (die_get_real_type(&type, &type) == NULL) {
  772. pr_warning("Failed to get a type"
  773. " information.\n");
  774. return -ENOENT;
  775. }
  776. while (*ref_ptr)
  777. ref_ptr = &(*ref_ptr)->next;
  778. /* Add new reference with offset +0 */
  779. *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
  780. if (*ref_ptr == NULL) {
  781. pr_warning("Out of memory error\n");
  782. return -ENOMEM;
  783. }
  784. }
  785. if (!die_compare_name(&type, "char") &&
  786. !die_compare_name(&type, "unsigned char")) {
  787. pr_warning("Failed to cast into string: "
  788. "%s is not (unsigned) char *.\n",
  789. dwarf_diename(vr_die));
  790. return -EINVAL;
  791. }
  792. tvar->type = strdup(cast);
  793. return (tvar->type == NULL) ? -ENOMEM : 0;
  794. }
  795. ret = BYTES_TO_BITS(die_get_byte_size(&type));
  796. if (!ret)
  797. /* No size ... try to use default type */
  798. return 0;
  799. /* Check the bitwidth */
  800. if (ret > MAX_BASIC_TYPE_BITS) {
  801. pr_info("%s exceeds max-bitwidth. Cut down to %d bits.\n",
  802. dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
  803. ret = MAX_BASIC_TYPE_BITS;
  804. }
  805. ret = snprintf(buf, 16, "%c%d",
  806. die_is_signed_type(&type) ? 's' : 'u', ret);
  807. formatted:
  808. if (ret < 0 || ret >= 16) {
  809. if (ret >= 16)
  810. ret = -E2BIG;
  811. pr_warning("Failed to convert variable type: %s\n",
  812. strerror(-ret));
  813. return ret;
  814. }
  815. tvar->type = strdup(buf);
  816. if (tvar->type == NULL)
  817. return -ENOMEM;
  818. return 0;
  819. }
  820. static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
  821. struct perf_probe_arg_field *field,
  822. struct probe_trace_arg_ref **ref_ptr,
  823. Dwarf_Die *die_mem)
  824. {
  825. struct probe_trace_arg_ref *ref = *ref_ptr;
  826. Dwarf_Die type;
  827. Dwarf_Word offs;
  828. int ret, tag;
  829. pr_debug("converting %s in %s\n", field->name, varname);
  830. if (die_get_real_type(vr_die, &type) == NULL) {
  831. pr_warning("Failed to get the type of %s.\n", varname);
  832. return -ENOENT;
  833. }
  834. pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
  835. tag = dwarf_tag(&type);
  836. if (field->name[0] == '[' &&
  837. (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
  838. if (field->next)
  839. /* Save original type for next field */
  840. memcpy(die_mem, &type, sizeof(*die_mem));
  841. /* Get the type of this array */
  842. if (die_get_real_type(&type, &type) == NULL) {
  843. pr_warning("Failed to get the type of %s.\n", varname);
  844. return -ENOENT;
  845. }
  846. pr_debug2("Array real type: (%x)\n",
  847. (unsigned)dwarf_dieoffset(&type));
  848. if (tag == DW_TAG_pointer_type) {
  849. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  850. if (ref == NULL)
  851. return -ENOMEM;
  852. if (*ref_ptr)
  853. (*ref_ptr)->next = ref;
  854. else
  855. *ref_ptr = ref;
  856. }
  857. ref->offset += die_get_byte_size(&type) * field->index;
  858. if (!field->next)
  859. /* Save vr_die for converting types */
  860. memcpy(die_mem, vr_die, sizeof(*die_mem));
  861. goto next;
  862. } else if (tag == DW_TAG_pointer_type) {
  863. /* Check the pointer and dereference */
  864. if (!field->ref) {
  865. pr_err("Semantic error: %s must be referred by '->'\n",
  866. field->name);
  867. return -EINVAL;
  868. }
  869. /* Get the type pointed by this pointer */
  870. if (die_get_real_type(&type, &type) == NULL) {
  871. pr_warning("Failed to get the type of %s.\n", varname);
  872. return -ENOENT;
  873. }
  874. /* Verify it is a data structure */
  875. if (dwarf_tag(&type) != DW_TAG_structure_type) {
  876. pr_warning("%s is not a data structure.\n", varname);
  877. return -EINVAL;
  878. }
  879. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  880. if (ref == NULL)
  881. return -ENOMEM;
  882. if (*ref_ptr)
  883. (*ref_ptr)->next = ref;
  884. else
  885. *ref_ptr = ref;
  886. } else {
  887. /* Verify it is a data structure */
  888. if (tag != DW_TAG_structure_type) {
  889. pr_warning("%s is not a data structure.\n", varname);
  890. return -EINVAL;
  891. }
  892. if (field->name[0] == '[') {
  893. pr_err("Semantic error: %s is not a pointor"
  894. " nor array.\n", varname);
  895. return -EINVAL;
  896. }
  897. if (field->ref) {
  898. pr_err("Semantic error: %s must be referred by '.'\n",
  899. field->name);
  900. return -EINVAL;
  901. }
  902. if (!ref) {
  903. pr_warning("Structure on a register is not "
  904. "supported yet.\n");
  905. return -ENOTSUP;
  906. }
  907. }
  908. if (die_find_member(&type, field->name, die_mem) == NULL) {
  909. pr_warning("%s(tyep:%s) has no member %s.\n", varname,
  910. dwarf_diename(&type), field->name);
  911. return -EINVAL;
  912. }
  913. /* Get the offset of the field */
  914. ret = die_get_data_member_location(die_mem, &offs);
  915. if (ret < 0) {
  916. pr_warning("Failed to get the offset of %s.\n", field->name);
  917. return ret;
  918. }
  919. ref->offset += (long)offs;
  920. next:
  921. /* Converting next field */
  922. if (field->next)
  923. return convert_variable_fields(die_mem, field->name,
  924. field->next, &ref, die_mem);
  925. else
  926. return 0;
  927. }
  928. /* Show a variables in kprobe event format */
  929. static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
  930. {
  931. Dwarf_Die die_mem;
  932. int ret;
  933. pr_debug("Converting variable %s into trace event.\n",
  934. dwarf_diename(vr_die));
  935. ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops,
  936. pf->tvar);
  937. if (ret == -ENOENT)
  938. pr_err("Failed to find the location of %s at this address.\n"
  939. " Perhaps, it has been optimized out.\n", pf->pvar->var);
  940. else if (ret == -ENOTSUP)
  941. pr_err("Sorry, we don't support this variable location yet.\n");
  942. else if (pf->pvar->field) {
  943. ret = convert_variable_fields(vr_die, pf->pvar->var,
  944. pf->pvar->field, &pf->tvar->ref,
  945. &die_mem);
  946. vr_die = &die_mem;
  947. }
  948. if (ret == 0)
  949. ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
  950. /* *expr will be cached in libdw. Don't free it. */
  951. return ret;
  952. }
  953. /* Find a variable in a subprogram die */
  954. static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
  955. {
  956. Dwarf_Die vr_die, *scopes;
  957. char buf[32], *ptr;
  958. int ret, nscopes;
  959. if (!is_c_varname(pf->pvar->var)) {
  960. /* Copy raw parameters */
  961. pf->tvar->value = strdup(pf->pvar->var);
  962. if (pf->tvar->value == NULL)
  963. return -ENOMEM;
  964. if (pf->pvar->type) {
  965. pf->tvar->type = strdup(pf->pvar->type);
  966. if (pf->tvar->type == NULL)
  967. return -ENOMEM;
  968. }
  969. if (pf->pvar->name) {
  970. pf->tvar->name = strdup(pf->pvar->name);
  971. if (pf->tvar->name == NULL)
  972. return -ENOMEM;
  973. } else
  974. pf->tvar->name = NULL;
  975. return 0;
  976. }
  977. if (pf->pvar->name)
  978. pf->tvar->name = strdup(pf->pvar->name);
  979. else {
  980. ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
  981. if (ret < 0)
  982. return ret;
  983. ptr = strchr(buf, ':'); /* Change type separator to _ */
  984. if (ptr)
  985. *ptr = '_';
  986. pf->tvar->name = strdup(buf);
  987. }
  988. if (pf->tvar->name == NULL)
  989. return -ENOMEM;
  990. pr_debug("Searching '%s' variable in context.\n",
  991. pf->pvar->var);
  992. /* Search child die for local variables and parameters. */
  993. if (die_find_variable_at(sp_die, pf->pvar->var, pf->addr, &vr_die))
  994. ret = convert_variable(&vr_die, pf);
  995. else {
  996. /* Search upper class */
  997. nscopes = dwarf_getscopes_die(sp_die, &scopes);
  998. while (nscopes-- > 1) {
  999. pr_debug("Searching variables in %s\n",
  1000. dwarf_diename(&scopes[nscopes]));
  1001. /* We should check this scope, so give dummy address */
  1002. if (die_find_variable_at(&scopes[nscopes],
  1003. pf->pvar->var, 0,
  1004. &vr_die)) {
  1005. ret = convert_variable(&vr_die, pf);
  1006. goto found;
  1007. }
  1008. }
  1009. if (scopes)
  1010. free(scopes);
  1011. ret = -ENOENT;
  1012. }
  1013. found:
  1014. if (ret < 0)
  1015. pr_warning("Failed to find '%s' in this function.\n",
  1016. pf->pvar->var);
  1017. return ret;
  1018. }
  1019. /* Convert subprogram DIE to trace point */
  1020. static int convert_to_trace_point(Dwarf_Die *sp_die, Dwarf_Addr paddr,
  1021. bool retprobe, struct probe_trace_point *tp)
  1022. {
  1023. Dwarf_Addr eaddr;
  1024. const char *name;
  1025. /* Copy the name of probe point */
  1026. name = dwarf_diename(sp_die);
  1027. if (name) {
  1028. if (dwarf_entrypc(sp_die, &eaddr) != 0) {
  1029. pr_warning("Failed to get entry address of %s\n",
  1030. dwarf_diename(sp_die));
  1031. return -ENOENT;
  1032. }
  1033. tp->symbol = strdup(name);
  1034. if (tp->symbol == NULL)
  1035. return -ENOMEM;
  1036. tp->offset = (unsigned long)(paddr - eaddr);
  1037. } else
  1038. /* This function has no name. */
  1039. tp->offset = (unsigned long)paddr;
  1040. /* Return probe must be on the head of a subprogram */
  1041. if (retprobe) {
  1042. if (eaddr != paddr) {
  1043. pr_warning("Return probe must be on the head of"
  1044. " a real function.\n");
  1045. return -EINVAL;
  1046. }
  1047. tp->retprobe = true;
  1048. }
  1049. return 0;
  1050. }
  1051. /* Call probe_finder callback with real subprogram DIE */
  1052. static int call_probe_finder(Dwarf_Die *sp_die, struct probe_finder *pf)
  1053. {
  1054. Dwarf_Die die_mem;
  1055. Dwarf_Attribute fb_attr;
  1056. size_t nops;
  1057. int ret;
  1058. /* If no real subprogram, find a real one */
  1059. if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
  1060. sp_die = die_find_real_subprogram(&pf->cu_die,
  1061. pf->addr, &die_mem);
  1062. if (!sp_die) {
  1063. pr_warning("Failed to find probe point in any "
  1064. "functions.\n");
  1065. return -ENOENT;
  1066. }
  1067. }
  1068. /* Get the frame base attribute/ops */
  1069. dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
  1070. ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
  1071. if (ret <= 0 || nops == 0) {
  1072. pf->fb_ops = NULL;
  1073. #if _ELFUTILS_PREREQ(0, 142)
  1074. } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
  1075. pf->cfi != NULL) {
  1076. Dwarf_Frame *frame;
  1077. if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
  1078. dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
  1079. pr_warning("Failed to get call frame on 0x%jx\n",
  1080. (uintmax_t)pf->addr);
  1081. return -ENOENT;
  1082. }
  1083. #endif
  1084. }
  1085. /* Call finder's callback handler */
  1086. ret = pf->callback(sp_die, pf);
  1087. /* *pf->fb_ops will be cached in libdw. Don't free it. */
  1088. pf->fb_ops = NULL;
  1089. return ret;
  1090. }
  1091. static int probe_point_line_walker(const char *fname, int lineno,
  1092. Dwarf_Addr addr, void *data)
  1093. {
  1094. struct probe_finder *pf = data;
  1095. int ret;
  1096. if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0)
  1097. return 0;
  1098. pf->addr = addr;
  1099. ret = call_probe_finder(NULL, pf);
  1100. /* Continue if no error, because the line will be in inline function */
  1101. return ret < 0 ? ret : 0;
  1102. }
  1103. /* Find probe point from its line number */
  1104. static int find_probe_point_by_line(struct probe_finder *pf)
  1105. {
  1106. return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf);
  1107. }
  1108. /* Find lines which match lazy pattern */
  1109. static int find_lazy_match_lines(struct list_head *head,
  1110. const char *fname, const char *pat)
  1111. {
  1112. FILE *fp;
  1113. char *line = NULL;
  1114. size_t line_len;
  1115. ssize_t len;
  1116. int count = 0, linenum = 1;
  1117. fp = fopen(fname, "r");
  1118. if (!fp) {
  1119. pr_warning("Failed to open %s: %s\n", fname, strerror(errno));
  1120. return -errno;
  1121. }
  1122. while ((len = getline(&line, &line_len, fp)) > 0) {
  1123. if (line[len - 1] == '\n')
  1124. line[len - 1] = '\0';
  1125. if (strlazymatch(line, pat)) {
  1126. line_list__add_line(head, linenum);
  1127. count++;
  1128. }
  1129. linenum++;
  1130. }
  1131. if (ferror(fp))
  1132. count = -errno;
  1133. free(line);
  1134. fclose(fp);
  1135. if (count == 0)
  1136. pr_debug("No matched lines found in %s.\n", fname);
  1137. return count;
  1138. }
  1139. static int probe_point_lazy_walker(const char *fname, int lineno,
  1140. Dwarf_Addr addr, void *data)
  1141. {
  1142. struct probe_finder *pf = data;
  1143. int ret;
  1144. if (!line_list__has_line(&pf->lcache, lineno) ||
  1145. strtailcmp(fname, pf->fname) != 0)
  1146. return 0;
  1147. pr_debug("Probe line found: line:%d addr:0x%llx\n",
  1148. lineno, (unsigned long long)addr);
  1149. pf->addr = addr;
  1150. ret = call_probe_finder(NULL, pf);
  1151. /*
  1152. * Continue if no error, because the lazy pattern will match
  1153. * to other lines
  1154. */
  1155. return ret < 0 ? ret : 0;
  1156. }
  1157. /* Find probe points from lazy pattern */
  1158. static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
  1159. {
  1160. int ret = 0;
  1161. if (list_empty(&pf->lcache)) {
  1162. /* Matching lazy line pattern */
  1163. ret = find_lazy_match_lines(&pf->lcache, pf->fname,
  1164. pf->pev->point.lazy_line);
  1165. if (ret <= 0)
  1166. return ret;
  1167. }
  1168. return die_walk_lines(sp_die, probe_point_lazy_walker, pf);
  1169. }
  1170. /* Callback parameter with return value */
  1171. struct dwarf_callback_param {
  1172. void *data;
  1173. int retval;
  1174. };
  1175. static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
  1176. {
  1177. struct dwarf_callback_param *param = data;
  1178. struct probe_finder *pf = param->data;
  1179. struct perf_probe_point *pp = &pf->pev->point;
  1180. Dwarf_Addr addr;
  1181. if (pp->lazy_line)
  1182. param->retval = find_probe_point_lazy(in_die, pf);
  1183. else {
  1184. /* Get probe address */
  1185. if (dwarf_entrypc(in_die, &addr) != 0) {
  1186. pr_warning("Failed to get entry address of %s.\n",
  1187. dwarf_diename(in_die));
  1188. param->retval = -ENOENT;
  1189. return DWARF_CB_ABORT;
  1190. }
  1191. pf->addr = addr;
  1192. pf->addr += pp->offset;
  1193. pr_debug("found inline addr: 0x%jx\n",
  1194. (uintmax_t)pf->addr);
  1195. param->retval = call_probe_finder(in_die, pf);
  1196. if (param->retval < 0)
  1197. return DWARF_CB_ABORT;
  1198. }
  1199. return DWARF_CB_OK;
  1200. }
  1201. /* Search function from function name */
  1202. static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
  1203. {
  1204. struct dwarf_callback_param *param = data;
  1205. struct probe_finder *pf = param->data;
  1206. struct perf_probe_point *pp = &pf->pev->point;
  1207. /* Check tag and diename */
  1208. if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
  1209. !die_compare_name(sp_die, pp->function))
  1210. return DWARF_CB_OK;
  1211. pf->fname = dwarf_decl_file(sp_die);
  1212. if (pp->line) { /* Function relative line */
  1213. dwarf_decl_line(sp_die, &pf->lno);
  1214. pf->lno += pp->line;
  1215. param->retval = find_probe_point_by_line(pf);
  1216. } else if (!dwarf_func_inline(sp_die)) {
  1217. /* Real function */
  1218. if (pp->lazy_line)
  1219. param->retval = find_probe_point_lazy(sp_die, pf);
  1220. else {
  1221. if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
  1222. pr_warning("Failed to get entry address of "
  1223. "%s.\n", dwarf_diename(sp_die));
  1224. param->retval = -ENOENT;
  1225. return DWARF_CB_ABORT;
  1226. }
  1227. pf->addr += pp->offset;
  1228. /* TODO: Check the address in this function */
  1229. param->retval = call_probe_finder(sp_die, pf);
  1230. }
  1231. } else {
  1232. struct dwarf_callback_param _param = {.data = (void *)pf,
  1233. .retval = 0};
  1234. /* Inlined function: search instances */
  1235. dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
  1236. &_param);
  1237. param->retval = _param.retval;
  1238. }
  1239. return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
  1240. }
  1241. static int find_probe_point_by_func(struct probe_finder *pf)
  1242. {
  1243. struct dwarf_callback_param _param = {.data = (void *)pf,
  1244. .retval = 0};
  1245. dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
  1246. return _param.retval;
  1247. }
  1248. /* Find probe points from debuginfo */
  1249. static int find_probes(int fd, struct probe_finder *pf)
  1250. {
  1251. struct perf_probe_point *pp = &pf->pev->point;
  1252. Dwarf_Off off, noff;
  1253. size_t cuhl;
  1254. Dwarf_Die *diep;
  1255. Dwarf *dbg = NULL;
  1256. Dwfl *dwfl;
  1257. Dwarf_Addr bias; /* Currently ignored */
  1258. int ret = 0;
  1259. dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
  1260. if (!dbg) {
  1261. pr_warning("No debug information found in the vmlinux - "
  1262. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  1263. return -EBADF;
  1264. }
  1265. #if _ELFUTILS_PREREQ(0, 142)
  1266. /* Get the call frame information from this dwarf */
  1267. pf->cfi = dwarf_getcfi(dbg);
  1268. #endif
  1269. off = 0;
  1270. line_list__init(&pf->lcache);
  1271. /* Loop on CUs (Compilation Unit) */
  1272. while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
  1273. /* Get the DIE(Debugging Information Entry) of this CU */
  1274. diep = dwarf_offdie(dbg, off + cuhl, &pf->cu_die);
  1275. if (!diep)
  1276. continue;
  1277. /* Check if target file is included. */
  1278. if (pp->file)
  1279. pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
  1280. else
  1281. pf->fname = NULL;
  1282. if (!pp->file || pf->fname) {
  1283. if (pp->function)
  1284. ret = find_probe_point_by_func(pf);
  1285. else if (pp->lazy_line)
  1286. ret = find_probe_point_lazy(NULL, pf);
  1287. else {
  1288. pf->lno = pp->line;
  1289. ret = find_probe_point_by_line(pf);
  1290. }
  1291. if (ret < 0)
  1292. break;
  1293. }
  1294. off = noff;
  1295. }
  1296. line_list__free(&pf->lcache);
  1297. if (dwfl)
  1298. dwfl_end(dwfl);
  1299. return ret;
  1300. }
  1301. /* Add a found probe point into trace event list */
  1302. static int add_probe_trace_event(Dwarf_Die *sp_die, struct probe_finder *pf)
  1303. {
  1304. struct trace_event_finder *tf =
  1305. container_of(pf, struct trace_event_finder, pf);
  1306. struct probe_trace_event *tev;
  1307. int ret, i;
  1308. /* Check number of tevs */
  1309. if (tf->ntevs == tf->max_tevs) {
  1310. pr_warning("Too many( > %d) probe point found.\n",
  1311. tf->max_tevs);
  1312. return -ERANGE;
  1313. }
  1314. tev = &tf->tevs[tf->ntevs++];
  1315. ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
  1316. &tev->point);
  1317. if (ret < 0)
  1318. return ret;
  1319. pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
  1320. tev->point.offset);
  1321. /* Find each argument */
  1322. tev->nargs = pf->pev->nargs;
  1323. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  1324. if (tev->args == NULL)
  1325. return -ENOMEM;
  1326. for (i = 0; i < pf->pev->nargs; i++) {
  1327. pf->pvar = &pf->pev->args[i];
  1328. pf->tvar = &tev->args[i];
  1329. ret = find_variable(sp_die, pf);
  1330. if (ret != 0)
  1331. return ret;
  1332. }
  1333. return 0;
  1334. }
  1335. /* Find probe_trace_events specified by perf_probe_event from debuginfo */
  1336. int find_probe_trace_events(int fd, struct perf_probe_event *pev,
  1337. struct probe_trace_event **tevs, int max_tevs)
  1338. {
  1339. struct trace_event_finder tf = {
  1340. .pf = {.pev = pev, .callback = add_probe_trace_event},
  1341. .max_tevs = max_tevs};
  1342. int ret;
  1343. /* Allocate result tevs array */
  1344. *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
  1345. if (*tevs == NULL)
  1346. return -ENOMEM;
  1347. tf.tevs = *tevs;
  1348. tf.ntevs = 0;
  1349. ret = find_probes(fd, &tf.pf);
  1350. if (ret < 0) {
  1351. free(*tevs);
  1352. *tevs = NULL;
  1353. return ret;
  1354. }
  1355. return (ret < 0) ? ret : tf.ntevs;
  1356. }
  1357. #define MAX_VAR_LEN 64
  1358. /* Collect available variables in this scope */
  1359. static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
  1360. {
  1361. struct available_var_finder *af = data;
  1362. struct variable_list *vl;
  1363. char buf[MAX_VAR_LEN];
  1364. int tag, ret;
  1365. vl = &af->vls[af->nvls - 1];
  1366. tag = dwarf_tag(die_mem);
  1367. if (tag == DW_TAG_formal_parameter ||
  1368. tag == DW_TAG_variable) {
  1369. ret = convert_variable_location(die_mem, af->pf.addr,
  1370. af->pf.fb_ops, NULL);
  1371. if (ret == 0) {
  1372. ret = die_get_varname(die_mem, buf, MAX_VAR_LEN);
  1373. pr_debug2("Add new var: %s\n", buf);
  1374. if (ret > 0)
  1375. strlist__add(vl->vars, buf);
  1376. }
  1377. }
  1378. if (af->child && dwarf_haspc(die_mem, af->pf.addr))
  1379. return DIE_FIND_CB_CONTINUE;
  1380. else
  1381. return DIE_FIND_CB_SIBLING;
  1382. }
  1383. /* Add a found vars into available variables list */
  1384. static int add_available_vars(Dwarf_Die *sp_die, struct probe_finder *pf)
  1385. {
  1386. struct available_var_finder *af =
  1387. container_of(pf, struct available_var_finder, pf);
  1388. struct variable_list *vl;
  1389. Dwarf_Die die_mem, *scopes = NULL;
  1390. int ret, nscopes;
  1391. /* Check number of tevs */
  1392. if (af->nvls == af->max_vls) {
  1393. pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
  1394. return -ERANGE;
  1395. }
  1396. vl = &af->vls[af->nvls++];
  1397. ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
  1398. &vl->point);
  1399. if (ret < 0)
  1400. return ret;
  1401. pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
  1402. vl->point.offset);
  1403. /* Find local variables */
  1404. vl->vars = strlist__new(true, NULL);
  1405. if (vl->vars == NULL)
  1406. return -ENOMEM;
  1407. af->child = true;
  1408. die_find_child(sp_die, collect_variables_cb, (void *)af, &die_mem);
  1409. /* Find external variables */
  1410. if (!af->externs)
  1411. goto out;
  1412. /* Don't need to search child DIE for externs. */
  1413. af->child = false;
  1414. nscopes = dwarf_getscopes_die(sp_die, &scopes);
  1415. while (nscopes-- > 1)
  1416. die_find_child(&scopes[nscopes], collect_variables_cb,
  1417. (void *)af, &die_mem);
  1418. if (scopes)
  1419. free(scopes);
  1420. out:
  1421. if (strlist__empty(vl->vars)) {
  1422. strlist__delete(vl->vars);
  1423. vl->vars = NULL;
  1424. }
  1425. return ret;
  1426. }
  1427. /* Find available variables at given probe point */
  1428. int find_available_vars_at(int fd, struct perf_probe_event *pev,
  1429. struct variable_list **vls, int max_vls,
  1430. bool externs)
  1431. {
  1432. struct available_var_finder af = {
  1433. .pf = {.pev = pev, .callback = add_available_vars},
  1434. .max_vls = max_vls, .externs = externs};
  1435. int ret;
  1436. /* Allocate result vls array */
  1437. *vls = zalloc(sizeof(struct variable_list) * max_vls);
  1438. if (*vls == NULL)
  1439. return -ENOMEM;
  1440. af.vls = *vls;
  1441. af.nvls = 0;
  1442. ret = find_probes(fd, &af.pf);
  1443. if (ret < 0) {
  1444. /* Free vlist for error */
  1445. while (af.nvls--) {
  1446. if (af.vls[af.nvls].point.symbol)
  1447. free(af.vls[af.nvls].point.symbol);
  1448. if (af.vls[af.nvls].vars)
  1449. strlist__delete(af.vls[af.nvls].vars);
  1450. }
  1451. free(af.vls);
  1452. *vls = NULL;
  1453. return ret;
  1454. }
  1455. return (ret < 0) ? ret : af.nvls;
  1456. }
  1457. /* Reverse search */
  1458. int find_perf_probe_point(unsigned long addr, struct perf_probe_point *ppt)
  1459. {
  1460. Dwarf_Die cudie, spdie, indie;
  1461. Dwarf *dbg = NULL;
  1462. Dwfl *dwfl = NULL;
  1463. Dwarf_Line *line;
  1464. Dwarf_Addr laddr, eaddr, bias = 0;
  1465. const char *tmp;
  1466. int lineno, ret = 0;
  1467. bool found = false;
  1468. /* Open the live linux kernel */
  1469. dbg = dwfl_init_live_kernel_dwarf(addr, &dwfl, &bias);
  1470. if (!dbg) {
  1471. pr_warning("No debug information found in the vmlinux - "
  1472. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  1473. ret = -EINVAL;
  1474. goto end;
  1475. }
  1476. /* Adjust address with bias */
  1477. addr += bias;
  1478. /* Find cu die */
  1479. if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr - bias, &cudie)) {
  1480. pr_warning("Failed to find debug information for address %lx\n",
  1481. addr);
  1482. ret = -EINVAL;
  1483. goto end;
  1484. }
  1485. /* Find a corresponding line */
  1486. line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
  1487. if (line) {
  1488. if (dwarf_lineaddr(line, &laddr) == 0 &&
  1489. (Dwarf_Addr)addr == laddr &&
  1490. dwarf_lineno(line, &lineno) == 0) {
  1491. tmp = dwarf_linesrc(line, NULL, NULL);
  1492. if (tmp) {
  1493. ppt->line = lineno;
  1494. ppt->file = strdup(tmp);
  1495. if (ppt->file == NULL) {
  1496. ret = -ENOMEM;
  1497. goto end;
  1498. }
  1499. found = true;
  1500. }
  1501. }
  1502. }
  1503. /* Find a corresponding function */
  1504. if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
  1505. tmp = dwarf_diename(&spdie);
  1506. if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0)
  1507. goto end;
  1508. if (ppt->line) {
  1509. if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
  1510. &indie)) {
  1511. /* addr in an inline function */
  1512. tmp = dwarf_diename(&indie);
  1513. if (!tmp)
  1514. goto end;
  1515. ret = dwarf_decl_line(&indie, &lineno);
  1516. } else {
  1517. if (eaddr == addr) { /* Function entry */
  1518. lineno = ppt->line;
  1519. ret = 0;
  1520. } else
  1521. ret = dwarf_decl_line(&spdie, &lineno);
  1522. }
  1523. if (ret == 0) {
  1524. /* Make a relative line number */
  1525. ppt->line -= lineno;
  1526. goto found;
  1527. }
  1528. }
  1529. /* We don't have a line number, let's use offset */
  1530. ppt->offset = addr - (unsigned long)eaddr;
  1531. found:
  1532. ppt->function = strdup(tmp);
  1533. if (ppt->function == NULL) {
  1534. ret = -ENOMEM;
  1535. goto end;
  1536. }
  1537. found = true;
  1538. }
  1539. end:
  1540. if (dwfl)
  1541. dwfl_end(dwfl);
  1542. if (ret >= 0)
  1543. ret = found ? 1 : 0;
  1544. return ret;
  1545. }
  1546. /* Add a line and store the src path */
  1547. static int line_range_add_line(const char *src, unsigned int lineno,
  1548. struct line_range *lr)
  1549. {
  1550. /* Copy source path */
  1551. if (!lr->path) {
  1552. lr->path = strdup(src);
  1553. if (lr->path == NULL)
  1554. return -ENOMEM;
  1555. }
  1556. return line_list__add_line(&lr->line_list, lineno);
  1557. }
  1558. static int line_range_walk_cb(const char *fname, int lineno,
  1559. Dwarf_Addr addr __used,
  1560. void *data)
  1561. {
  1562. struct line_finder *lf = data;
  1563. if ((strtailcmp(fname, lf->fname) != 0) ||
  1564. (lf->lno_s > lineno || lf->lno_e < lineno))
  1565. return 0;
  1566. if (line_range_add_line(fname, lineno, lf->lr) < 0)
  1567. return -EINVAL;
  1568. return 0;
  1569. }
  1570. /* Find line range from its line number */
  1571. static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
  1572. {
  1573. int ret;
  1574. ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf);
  1575. /* Update status */
  1576. if (ret >= 0)
  1577. if (!list_empty(&lf->lr->line_list))
  1578. ret = lf->found = 1;
  1579. else
  1580. ret = 0; /* Lines are not found */
  1581. else {
  1582. free(lf->lr->path);
  1583. lf->lr->path = NULL;
  1584. }
  1585. return ret;
  1586. }
  1587. static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
  1588. {
  1589. struct dwarf_callback_param *param = data;
  1590. param->retval = find_line_range_by_line(in_die, param->data);
  1591. return DWARF_CB_ABORT; /* No need to find other instances */
  1592. }
  1593. /* Search function from function name */
  1594. static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
  1595. {
  1596. struct dwarf_callback_param *param = data;
  1597. struct line_finder *lf = param->data;
  1598. struct line_range *lr = lf->lr;
  1599. if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
  1600. die_compare_name(sp_die, lr->function)) {
  1601. lf->fname = dwarf_decl_file(sp_die);
  1602. dwarf_decl_line(sp_die, &lr->offset);
  1603. pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
  1604. lf->lno_s = lr->offset + lr->start;
  1605. if (lf->lno_s < 0) /* Overflow */
  1606. lf->lno_s = INT_MAX;
  1607. lf->lno_e = lr->offset + lr->end;
  1608. if (lf->lno_e < 0) /* Overflow */
  1609. lf->lno_e = INT_MAX;
  1610. pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
  1611. lr->start = lf->lno_s;
  1612. lr->end = lf->lno_e;
  1613. if (dwarf_func_inline(sp_die)) {
  1614. struct dwarf_callback_param _param;
  1615. _param.data = (void *)lf;
  1616. _param.retval = 0;
  1617. dwarf_func_inline_instances(sp_die,
  1618. line_range_inline_cb,
  1619. &_param);
  1620. param->retval = _param.retval;
  1621. } else
  1622. param->retval = find_line_range_by_line(sp_die, lf);
  1623. return DWARF_CB_ABORT;
  1624. }
  1625. return DWARF_CB_OK;
  1626. }
  1627. static int find_line_range_by_func(struct line_finder *lf)
  1628. {
  1629. struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
  1630. dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
  1631. return param.retval;
  1632. }
  1633. int find_line_range(int fd, struct line_range *lr)
  1634. {
  1635. struct line_finder lf = {.lr = lr, .found = 0};
  1636. int ret = 0;
  1637. Dwarf_Off off = 0, noff;
  1638. size_t cuhl;
  1639. Dwarf_Die *diep;
  1640. Dwarf *dbg = NULL;
  1641. Dwfl *dwfl;
  1642. Dwarf_Addr bias; /* Currently ignored */
  1643. const char *comp_dir;
  1644. dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
  1645. if (!dbg) {
  1646. pr_warning("No debug information found in the vmlinux - "
  1647. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  1648. return -EBADF;
  1649. }
  1650. /* Loop on CUs (Compilation Unit) */
  1651. while (!lf.found && ret >= 0) {
  1652. if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
  1653. break;
  1654. /* Get the DIE(Debugging Information Entry) of this CU */
  1655. diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
  1656. if (!diep)
  1657. continue;
  1658. /* Check if target file is included. */
  1659. if (lr->file)
  1660. lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
  1661. else
  1662. lf.fname = 0;
  1663. if (!lr->file || lf.fname) {
  1664. if (lr->function)
  1665. ret = find_line_range_by_func(&lf);
  1666. else {
  1667. lf.lno_s = lr->start;
  1668. lf.lno_e = lr->end;
  1669. ret = find_line_range_by_line(NULL, &lf);
  1670. }
  1671. }
  1672. off = noff;
  1673. }
  1674. /* Store comp_dir */
  1675. if (lf.found) {
  1676. comp_dir = cu_get_comp_dir(&lf.cu_die);
  1677. if (comp_dir) {
  1678. lr->comp_dir = strdup(comp_dir);
  1679. if (!lr->comp_dir)
  1680. ret = -ENOMEM;
  1681. }
  1682. }
  1683. pr_debug("path: %s\n", lr->path);
  1684. dwfl_end(dwfl);
  1685. return (ret < 0) ? ret : lf.found;
  1686. }