probe-finder.c 49 KB

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