probe-finder.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064
  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. struct pubname_callback_param {
  1278. char *function;
  1279. char *file;
  1280. Dwarf_Die *cu_die;
  1281. Dwarf_Die *sp_die;
  1282. int found;
  1283. };
  1284. static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data)
  1285. {
  1286. struct pubname_callback_param *param = data;
  1287. if (dwarf_offdie(dbg, gl->die_offset, param->sp_die)) {
  1288. if (dwarf_tag(param->sp_die) != DW_TAG_subprogram)
  1289. return DWARF_CB_OK;
  1290. if (die_compare_name(param->sp_die, param->function)) {
  1291. if (!dwarf_offdie(dbg, gl->cu_offset, param->cu_die))
  1292. return DWARF_CB_OK;
  1293. if (param->file &&
  1294. strtailcmp(param->file, dwarf_decl_file(param->sp_die)))
  1295. return DWARF_CB_OK;
  1296. param->found = 1;
  1297. return DWARF_CB_ABORT;
  1298. }
  1299. }
  1300. return DWARF_CB_OK;
  1301. }
  1302. /* Find probe points from debuginfo */
  1303. static int find_probes(int fd, struct probe_finder *pf)
  1304. {
  1305. struct perf_probe_point *pp = &pf->pev->point;
  1306. Dwarf_Off off, noff;
  1307. size_t cuhl;
  1308. Dwarf_Die *diep;
  1309. Dwarf *dbg = NULL;
  1310. Dwfl *dwfl;
  1311. Dwarf_Addr bias; /* Currently ignored */
  1312. int ret = 0;
  1313. dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
  1314. if (!dbg) {
  1315. pr_warning("No debug information found in the vmlinux - "
  1316. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  1317. close(fd); /* Without dwfl_end(), fd isn't closed. */
  1318. return -EBADF;
  1319. }
  1320. #if _ELFUTILS_PREREQ(0, 142)
  1321. /* Get the call frame information from this dwarf */
  1322. pf->cfi = dwarf_getcfi(dbg);
  1323. #endif
  1324. off = 0;
  1325. line_list__init(&pf->lcache);
  1326. /* Fastpath: lookup by function name from .debug_pubnames section */
  1327. if (pp->function) {
  1328. struct pubname_callback_param pubname_param = {
  1329. .function = pp->function,
  1330. .file = pp->file,
  1331. .cu_die = &pf->cu_die,
  1332. .sp_die = &pf->sp_die,
  1333. .found = 0,
  1334. };
  1335. struct dwarf_callback_param probe_param = {
  1336. .data = pf,
  1337. };
  1338. dwarf_getpubnames(dbg, pubname_search_cb, &pubname_param, 0);
  1339. if (pubname_param.found) {
  1340. ret = probe_point_search_cb(&pf->sp_die, &probe_param);
  1341. if (ret)
  1342. goto found;
  1343. }
  1344. }
  1345. /* Loop on CUs (Compilation Unit) */
  1346. while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
  1347. /* Get the DIE(Debugging Information Entry) of this CU */
  1348. diep = dwarf_offdie(dbg, off + cuhl, &pf->cu_die);
  1349. if (!diep)
  1350. continue;
  1351. /* Check if target file is included. */
  1352. if (pp->file)
  1353. pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
  1354. else
  1355. pf->fname = NULL;
  1356. if (!pp->file || pf->fname) {
  1357. if (pp->function)
  1358. ret = find_probe_point_by_func(pf);
  1359. else if (pp->lazy_line)
  1360. ret = find_probe_point_lazy(NULL, pf);
  1361. else {
  1362. pf->lno = pp->line;
  1363. ret = find_probe_point_by_line(pf);
  1364. }
  1365. if (ret < 0)
  1366. break;
  1367. }
  1368. off = noff;
  1369. }
  1370. found:
  1371. line_list__free(&pf->lcache);
  1372. if (dwfl)
  1373. dwfl_end(dwfl);
  1374. return ret;
  1375. }
  1376. /* Add a found probe point into trace event list */
  1377. static int add_probe_trace_event(Dwarf_Die *sp_die, struct probe_finder *pf)
  1378. {
  1379. struct trace_event_finder *tf =
  1380. container_of(pf, struct trace_event_finder, pf);
  1381. struct probe_trace_event *tev;
  1382. int ret, i;
  1383. /* Check number of tevs */
  1384. if (tf->ntevs == tf->max_tevs) {
  1385. pr_warning("Too many( > %d) probe point found.\n",
  1386. tf->max_tevs);
  1387. return -ERANGE;
  1388. }
  1389. tev = &tf->tevs[tf->ntevs++];
  1390. ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
  1391. &tev->point);
  1392. if (ret < 0)
  1393. return ret;
  1394. pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
  1395. tev->point.offset);
  1396. /* Find each argument */
  1397. tev->nargs = pf->pev->nargs;
  1398. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  1399. if (tev->args == NULL)
  1400. return -ENOMEM;
  1401. for (i = 0; i < pf->pev->nargs; i++) {
  1402. pf->pvar = &pf->pev->args[i];
  1403. pf->tvar = &tev->args[i];
  1404. ret = find_variable(sp_die, pf);
  1405. if (ret != 0)
  1406. return ret;
  1407. }
  1408. return 0;
  1409. }
  1410. /* Find probe_trace_events specified by perf_probe_event from debuginfo */
  1411. int find_probe_trace_events(int fd, struct perf_probe_event *pev,
  1412. struct probe_trace_event **tevs, int max_tevs)
  1413. {
  1414. struct trace_event_finder tf = {
  1415. .pf = {.pev = pev, .callback = add_probe_trace_event},
  1416. .max_tevs = max_tevs};
  1417. int ret;
  1418. /* Allocate result tevs array */
  1419. *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
  1420. if (*tevs == NULL)
  1421. return -ENOMEM;
  1422. tf.tevs = *tevs;
  1423. tf.ntevs = 0;
  1424. ret = find_probes(fd, &tf.pf);
  1425. if (ret < 0) {
  1426. free(*tevs);
  1427. *tevs = NULL;
  1428. return ret;
  1429. }
  1430. return (ret < 0) ? ret : tf.ntevs;
  1431. }
  1432. #define MAX_VAR_LEN 64
  1433. /* Collect available variables in this scope */
  1434. static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
  1435. {
  1436. struct available_var_finder *af = data;
  1437. struct variable_list *vl;
  1438. char buf[MAX_VAR_LEN];
  1439. int tag, ret;
  1440. vl = &af->vls[af->nvls - 1];
  1441. tag = dwarf_tag(die_mem);
  1442. if (tag == DW_TAG_formal_parameter ||
  1443. tag == DW_TAG_variable) {
  1444. ret = convert_variable_location(die_mem, af->pf.addr,
  1445. af->pf.fb_ops, NULL);
  1446. if (ret == 0) {
  1447. ret = die_get_varname(die_mem, buf, MAX_VAR_LEN);
  1448. pr_debug2("Add new var: %s\n", buf);
  1449. if (ret > 0)
  1450. strlist__add(vl->vars, buf);
  1451. }
  1452. }
  1453. if (af->child && dwarf_haspc(die_mem, af->pf.addr))
  1454. return DIE_FIND_CB_CONTINUE;
  1455. else
  1456. return DIE_FIND_CB_SIBLING;
  1457. }
  1458. /* Add a found vars into available variables list */
  1459. static int add_available_vars(Dwarf_Die *sp_die, struct probe_finder *pf)
  1460. {
  1461. struct available_var_finder *af =
  1462. container_of(pf, struct available_var_finder, pf);
  1463. struct variable_list *vl;
  1464. Dwarf_Die die_mem, *scopes = NULL;
  1465. int ret, nscopes;
  1466. /* Check number of tevs */
  1467. if (af->nvls == af->max_vls) {
  1468. pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
  1469. return -ERANGE;
  1470. }
  1471. vl = &af->vls[af->nvls++];
  1472. ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
  1473. &vl->point);
  1474. if (ret < 0)
  1475. return ret;
  1476. pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
  1477. vl->point.offset);
  1478. /* Find local variables */
  1479. vl->vars = strlist__new(true, NULL);
  1480. if (vl->vars == NULL)
  1481. return -ENOMEM;
  1482. af->child = true;
  1483. die_find_child(sp_die, collect_variables_cb, (void *)af, &die_mem);
  1484. /* Find external variables */
  1485. if (!af->externs)
  1486. goto out;
  1487. /* Don't need to search child DIE for externs. */
  1488. af->child = false;
  1489. nscopes = dwarf_getscopes_die(sp_die, &scopes);
  1490. while (nscopes-- > 1)
  1491. die_find_child(&scopes[nscopes], collect_variables_cb,
  1492. (void *)af, &die_mem);
  1493. if (scopes)
  1494. free(scopes);
  1495. out:
  1496. if (strlist__empty(vl->vars)) {
  1497. strlist__delete(vl->vars);
  1498. vl->vars = NULL;
  1499. }
  1500. return ret;
  1501. }
  1502. /* Find available variables at given probe point */
  1503. int find_available_vars_at(int fd, struct perf_probe_event *pev,
  1504. struct variable_list **vls, int max_vls,
  1505. bool externs)
  1506. {
  1507. struct available_var_finder af = {
  1508. .pf = {.pev = pev, .callback = add_available_vars},
  1509. .max_vls = max_vls, .externs = externs};
  1510. int ret;
  1511. /* Allocate result vls array */
  1512. *vls = zalloc(sizeof(struct variable_list) * max_vls);
  1513. if (*vls == NULL)
  1514. return -ENOMEM;
  1515. af.vls = *vls;
  1516. af.nvls = 0;
  1517. ret = find_probes(fd, &af.pf);
  1518. if (ret < 0) {
  1519. /* Free vlist for error */
  1520. while (af.nvls--) {
  1521. if (af.vls[af.nvls].point.symbol)
  1522. free(af.vls[af.nvls].point.symbol);
  1523. if (af.vls[af.nvls].vars)
  1524. strlist__delete(af.vls[af.nvls].vars);
  1525. }
  1526. free(af.vls);
  1527. *vls = NULL;
  1528. return ret;
  1529. }
  1530. return (ret < 0) ? ret : af.nvls;
  1531. }
  1532. /* Reverse search */
  1533. int find_perf_probe_point(unsigned long addr, struct perf_probe_point *ppt)
  1534. {
  1535. Dwarf_Die cudie, spdie, indie;
  1536. Dwarf *dbg = NULL;
  1537. Dwfl *dwfl = NULL;
  1538. Dwarf_Addr _addr, baseaddr, bias = 0;
  1539. const char *fname = NULL, *func = NULL, *tmp;
  1540. int baseline = 0, lineno = 0, ret = 0;
  1541. /* Open the live linux kernel */
  1542. dbg = dwfl_init_live_kernel_dwarf(addr, &dwfl, &bias);
  1543. if (!dbg) {
  1544. pr_warning("No debug information found in the vmlinux - "
  1545. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  1546. ret = -EINVAL;
  1547. goto end;
  1548. }
  1549. /* Adjust address with bias */
  1550. addr += bias;
  1551. /* Find cu die */
  1552. if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr - bias, &cudie)) {
  1553. pr_warning("Failed to find debug information for address %lx\n",
  1554. addr);
  1555. ret = -EINVAL;
  1556. goto end;
  1557. }
  1558. /* Find a corresponding line (filename and lineno) */
  1559. cu_find_lineinfo(&cudie, addr, &fname, &lineno);
  1560. /* Don't care whether it failed or not */
  1561. /* Find a corresponding function (name, baseline and baseaddr) */
  1562. if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
  1563. /* Get function entry information */
  1564. tmp = dwarf_diename(&spdie);
  1565. if (!tmp ||
  1566. dwarf_entrypc(&spdie, &baseaddr) != 0 ||
  1567. dwarf_decl_line(&spdie, &baseline) != 0)
  1568. goto post;
  1569. func = tmp;
  1570. if (addr == (unsigned long)baseaddr)
  1571. /* Function entry - Relative line number is 0 */
  1572. lineno = baseline;
  1573. else if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
  1574. &indie)) {
  1575. if (dwarf_entrypc(&indie, &_addr) == 0 &&
  1576. _addr == addr)
  1577. /*
  1578. * addr is at an inline function entry.
  1579. * In this case, lineno should be the call-site
  1580. * line number.
  1581. */
  1582. lineno = die_get_call_lineno(&indie);
  1583. else {
  1584. /*
  1585. * addr is in an inline function body.
  1586. * Since lineno points one of the lines
  1587. * of the inline function, baseline should
  1588. * be the entry line of the inline function.
  1589. */
  1590. tmp = dwarf_diename(&indie);
  1591. if (tmp &&
  1592. dwarf_decl_line(&spdie, &baseline) == 0)
  1593. func = tmp;
  1594. }
  1595. }
  1596. }
  1597. post:
  1598. /* Make a relative line number or an offset */
  1599. if (lineno)
  1600. ppt->line = lineno - baseline;
  1601. else if (func)
  1602. ppt->offset = addr - (unsigned long)baseaddr;
  1603. /* Duplicate strings */
  1604. if (func) {
  1605. ppt->function = strdup(func);
  1606. if (ppt->function == NULL) {
  1607. ret = -ENOMEM;
  1608. goto end;
  1609. }
  1610. }
  1611. if (fname) {
  1612. ppt->file = strdup(fname);
  1613. if (ppt->file == NULL) {
  1614. if (ppt->function) {
  1615. free(ppt->function);
  1616. ppt->function = NULL;
  1617. }
  1618. ret = -ENOMEM;
  1619. goto end;
  1620. }
  1621. }
  1622. end:
  1623. if (dwfl)
  1624. dwfl_end(dwfl);
  1625. if (ret == 0 && (fname || func))
  1626. ret = 1; /* Found a point */
  1627. return ret;
  1628. }
  1629. /* Add a line and store the src path */
  1630. static int line_range_add_line(const char *src, unsigned int lineno,
  1631. struct line_range *lr)
  1632. {
  1633. /* Copy source path */
  1634. if (!lr->path) {
  1635. lr->path = strdup(src);
  1636. if (lr->path == NULL)
  1637. return -ENOMEM;
  1638. }
  1639. return line_list__add_line(&lr->line_list, lineno);
  1640. }
  1641. static int line_range_walk_cb(const char *fname, int lineno,
  1642. Dwarf_Addr addr __used,
  1643. void *data)
  1644. {
  1645. struct line_finder *lf = data;
  1646. if ((strtailcmp(fname, lf->fname) != 0) ||
  1647. (lf->lno_s > lineno || lf->lno_e < lineno))
  1648. return 0;
  1649. if (line_range_add_line(fname, lineno, lf->lr) < 0)
  1650. return -EINVAL;
  1651. return 0;
  1652. }
  1653. /* Find line range from its line number */
  1654. static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
  1655. {
  1656. int ret;
  1657. ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf);
  1658. /* Update status */
  1659. if (ret >= 0)
  1660. if (!list_empty(&lf->lr->line_list))
  1661. ret = lf->found = 1;
  1662. else
  1663. ret = 0; /* Lines are not found */
  1664. else {
  1665. free(lf->lr->path);
  1666. lf->lr->path = NULL;
  1667. }
  1668. return ret;
  1669. }
  1670. static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
  1671. {
  1672. struct dwarf_callback_param *param = data;
  1673. param->retval = find_line_range_by_line(in_die, param->data);
  1674. return DWARF_CB_ABORT; /* No need to find other instances */
  1675. }
  1676. /* Search function from function name */
  1677. static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
  1678. {
  1679. struct dwarf_callback_param *param = data;
  1680. struct line_finder *lf = param->data;
  1681. struct line_range *lr = lf->lr;
  1682. /* Check declared file */
  1683. if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die)))
  1684. return DWARF_CB_OK;
  1685. if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
  1686. die_compare_name(sp_die, lr->function)) {
  1687. lf->fname = dwarf_decl_file(sp_die);
  1688. dwarf_decl_line(sp_die, &lr->offset);
  1689. pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
  1690. lf->lno_s = lr->offset + lr->start;
  1691. if (lf->lno_s < 0) /* Overflow */
  1692. lf->lno_s = INT_MAX;
  1693. lf->lno_e = lr->offset + lr->end;
  1694. if (lf->lno_e < 0) /* Overflow */
  1695. lf->lno_e = INT_MAX;
  1696. pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
  1697. lr->start = lf->lno_s;
  1698. lr->end = lf->lno_e;
  1699. if (dwarf_func_inline(sp_die)) {
  1700. struct dwarf_callback_param _param;
  1701. _param.data = (void *)lf;
  1702. _param.retval = 0;
  1703. dwarf_func_inline_instances(sp_die,
  1704. line_range_inline_cb,
  1705. &_param);
  1706. param->retval = _param.retval;
  1707. } else
  1708. param->retval = find_line_range_by_line(sp_die, lf);
  1709. return DWARF_CB_ABORT;
  1710. }
  1711. return DWARF_CB_OK;
  1712. }
  1713. static int find_line_range_by_func(struct line_finder *lf)
  1714. {
  1715. struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
  1716. dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
  1717. return param.retval;
  1718. }
  1719. int find_line_range(int fd, struct line_range *lr)
  1720. {
  1721. struct line_finder lf = {.lr = lr, .found = 0};
  1722. int ret = 0;
  1723. Dwarf_Off off = 0, noff;
  1724. size_t cuhl;
  1725. Dwarf_Die *diep;
  1726. Dwarf *dbg = NULL;
  1727. Dwfl *dwfl;
  1728. Dwarf_Addr bias; /* Currently ignored */
  1729. const char *comp_dir;
  1730. dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
  1731. if (!dbg) {
  1732. pr_warning("No debug information found in the vmlinux - "
  1733. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  1734. close(fd); /* Without dwfl_end(), fd isn't closed. */
  1735. return -EBADF;
  1736. }
  1737. /* Fastpath: lookup by function name from .debug_pubnames section */
  1738. if (lr->function) {
  1739. struct pubname_callback_param pubname_param = {
  1740. .function = lr->function, .file = lr->file,
  1741. .cu_die = &lf.cu_die, .sp_die = &lf.sp_die, .found = 0};
  1742. struct dwarf_callback_param line_range_param = {
  1743. .data = (void *)&lf, .retval = 0};
  1744. dwarf_getpubnames(dbg, pubname_search_cb, &pubname_param, 0);
  1745. if (pubname_param.found) {
  1746. line_range_search_cb(&lf.sp_die, &line_range_param);
  1747. if (lf.found)
  1748. goto found;
  1749. }
  1750. }
  1751. /* Loop on CUs (Compilation Unit) */
  1752. while (!lf.found && ret >= 0) {
  1753. if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
  1754. break;
  1755. /* Get the DIE(Debugging Information Entry) of this CU */
  1756. diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
  1757. if (!diep)
  1758. continue;
  1759. /* Check if target file is included. */
  1760. if (lr->file)
  1761. lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
  1762. else
  1763. lf.fname = 0;
  1764. if (!lr->file || lf.fname) {
  1765. if (lr->function)
  1766. ret = find_line_range_by_func(&lf);
  1767. else {
  1768. lf.lno_s = lr->start;
  1769. lf.lno_e = lr->end;
  1770. ret = find_line_range_by_line(NULL, &lf);
  1771. }
  1772. }
  1773. off = noff;
  1774. }
  1775. found:
  1776. /* Store comp_dir */
  1777. if (lf.found) {
  1778. comp_dir = cu_get_comp_dir(&lf.cu_die);
  1779. if (comp_dir) {
  1780. lr->comp_dir = strdup(comp_dir);
  1781. if (!lr->comp_dir)
  1782. ret = -ENOMEM;
  1783. }
  1784. }
  1785. pr_debug("path: %s\n", lr->path);
  1786. dwfl_end(dwfl);
  1787. return (ret < 0) ? ret : lf.found;
  1788. }