probe-finder.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  1. /*
  2. * probe-finder.c : C expression to kprobe event converter
  3. *
  4. * Written by Masami Hiramatsu <mhiramat@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *
  20. */
  21. #include <sys/utsname.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #include <errno.h>
  26. #include <stdio.h>
  27. #include <unistd.h>
  28. #include <getopt.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdarg.h>
  32. #include <ctype.h>
  33. #include <dwarf-regs.h>
  34. #include "event.h"
  35. #include "debug.h"
  36. #include "util.h"
  37. #include "symbol.h"
  38. #include "probe-finder.h"
  39. /* Kprobe tracer basic type is up to u64 */
  40. #define MAX_BASIC_TYPE_BITS 64
  41. /*
  42. * Compare the tail of two strings.
  43. * Return 0 if whole of either string is same as another's tail part.
  44. */
  45. static int strtailcmp(const char *s1, const char *s2)
  46. {
  47. int i1 = strlen(s1);
  48. int i2 = strlen(s2);
  49. while (--i1 >= 0 && --i2 >= 0) {
  50. if (s1[i1] != s2[i2])
  51. return s1[i1] - s2[i2];
  52. }
  53. return 0;
  54. }
  55. /* Line number list operations */
  56. /* Add a line to line number list */
  57. static int line_list__add_line(struct list_head *head, int line)
  58. {
  59. struct line_node *ln;
  60. struct list_head *p;
  61. /* Reverse search, because new line will be the last one */
  62. list_for_each_entry_reverse(ln, head, list) {
  63. if (ln->line < line) {
  64. p = &ln->list;
  65. goto found;
  66. } else if (ln->line == line) /* Already exist */
  67. return 1;
  68. }
  69. /* List is empty, or the smallest entry */
  70. p = head;
  71. found:
  72. pr_debug("line list: add a line %u\n", line);
  73. ln = zalloc(sizeof(struct line_node));
  74. if (ln == NULL)
  75. return -ENOMEM;
  76. ln->line = line;
  77. INIT_LIST_HEAD(&ln->list);
  78. list_add(&ln->list, p);
  79. return 0;
  80. }
  81. /* Check if the line in line number list */
  82. static int line_list__has_line(struct list_head *head, int line)
  83. {
  84. struct line_node *ln;
  85. /* Reverse search, because new line will be the last one */
  86. list_for_each_entry(ln, head, list)
  87. if (ln->line == line)
  88. return 1;
  89. return 0;
  90. }
  91. /* Init line number list */
  92. static void line_list__init(struct list_head *head)
  93. {
  94. INIT_LIST_HEAD(head);
  95. }
  96. /* Free line number list */
  97. static void line_list__free(struct list_head *head)
  98. {
  99. struct line_node *ln;
  100. while (!list_empty(head)) {
  101. ln = list_first_entry(head, struct line_node, list);
  102. list_del(&ln->list);
  103. free(ln);
  104. }
  105. }
  106. /* Dwarf wrappers */
  107. /* Find the realpath of the target file. */
  108. static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
  109. {
  110. Dwarf_Files *files;
  111. size_t nfiles, i;
  112. const char *src = NULL;
  113. int ret;
  114. if (!fname)
  115. return NULL;
  116. ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
  117. if (ret != 0)
  118. return NULL;
  119. for (i = 0; i < nfiles; i++) {
  120. src = dwarf_filesrc(files, i, NULL, NULL);
  121. if (strtailcmp(src, fname) == 0)
  122. break;
  123. }
  124. if (i == nfiles)
  125. return NULL;
  126. return src;
  127. }
  128. /* Get DW_AT_comp_dir (should be NULL with older gcc) */
  129. static const char *cu_get_comp_dir(Dwarf_Die *cu_die)
  130. {
  131. Dwarf_Attribute attr;
  132. if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL)
  133. return NULL;
  134. return dwarf_formstring(&attr);
  135. }
  136. /* Compare diename and tname */
  137. static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
  138. {
  139. const char *name;
  140. name = dwarf_diename(dw_die);
  141. return name ? (strcmp(tname, name) == 0) : false;
  142. }
  143. /* Get type die */
  144. static Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
  145. {
  146. Dwarf_Attribute attr;
  147. if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) &&
  148. dwarf_formref_die(&attr, die_mem))
  149. return die_mem;
  150. else
  151. return NULL;
  152. }
  153. /* Get type die, but skip qualifiers and typedef */
  154. static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
  155. {
  156. int tag;
  157. do {
  158. vr_die = die_get_type(vr_die, die_mem);
  159. if (!vr_die)
  160. break;
  161. tag = dwarf_tag(vr_die);
  162. } while (tag == DW_TAG_const_type ||
  163. tag == DW_TAG_restrict_type ||
  164. tag == DW_TAG_volatile_type ||
  165. tag == DW_TAG_shared_type ||
  166. tag == DW_TAG_typedef);
  167. return vr_die;
  168. }
  169. static bool die_is_signed_type(Dwarf_Die *tp_die)
  170. {
  171. Dwarf_Attribute attr;
  172. Dwarf_Word ret;
  173. if (dwarf_attr(tp_die, DW_AT_encoding, &attr) == NULL ||
  174. dwarf_formudata(&attr, &ret) != 0)
  175. return false;
  176. return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
  177. ret == DW_ATE_signed_fixed);
  178. }
  179. static int die_get_byte_size(Dwarf_Die *tp_die)
  180. {
  181. Dwarf_Attribute attr;
  182. Dwarf_Word ret;
  183. if (dwarf_attr(tp_die, DW_AT_byte_size, &attr) == NULL ||
  184. dwarf_formudata(&attr, &ret) != 0)
  185. return 0;
  186. return (int)ret;
  187. }
  188. /* Get data_member_location offset */
  189. static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
  190. {
  191. Dwarf_Attribute attr;
  192. Dwarf_Op *expr;
  193. size_t nexpr;
  194. int ret;
  195. if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
  196. return -ENOENT;
  197. if (dwarf_formudata(&attr, offs) != 0) {
  198. /* DW_AT_data_member_location should be DW_OP_plus_uconst */
  199. ret = dwarf_getlocation(&attr, &expr, &nexpr);
  200. if (ret < 0 || nexpr == 0)
  201. return -ENOENT;
  202. if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
  203. pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
  204. expr[0].atom, nexpr);
  205. return -ENOTSUP;
  206. }
  207. *offs = (Dwarf_Word)expr[0].number;
  208. }
  209. return 0;
  210. }
  211. /* Return values for die_find callbacks */
  212. enum {
  213. DIE_FIND_CB_FOUND = 0, /* End of Search */
  214. DIE_FIND_CB_CHILD = 1, /* Search only children */
  215. DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
  216. DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
  217. };
  218. /* Search a child die */
  219. static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
  220. int (*callback)(Dwarf_Die *, void *),
  221. void *data, Dwarf_Die *die_mem)
  222. {
  223. Dwarf_Die child_die;
  224. int ret;
  225. ret = dwarf_child(rt_die, die_mem);
  226. if (ret != 0)
  227. return NULL;
  228. do {
  229. ret = callback(die_mem, data);
  230. if (ret == DIE_FIND_CB_FOUND)
  231. return die_mem;
  232. if ((ret & DIE_FIND_CB_CHILD) &&
  233. die_find_child(die_mem, callback, data, &child_die)) {
  234. memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
  235. return die_mem;
  236. }
  237. } while ((ret & DIE_FIND_CB_SIBLING) &&
  238. dwarf_siblingof(die_mem, die_mem) == 0);
  239. return NULL;
  240. }
  241. struct __addr_die_search_param {
  242. Dwarf_Addr addr;
  243. Dwarf_Die *die_mem;
  244. };
  245. static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
  246. {
  247. struct __addr_die_search_param *ad = data;
  248. if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
  249. dwarf_haspc(fn_die, ad->addr)) {
  250. memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
  251. return DWARF_CB_ABORT;
  252. }
  253. return DWARF_CB_OK;
  254. }
  255. /* Search a real subprogram including this line, */
  256. static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
  257. Dwarf_Die *die_mem)
  258. {
  259. struct __addr_die_search_param ad;
  260. ad.addr = addr;
  261. ad.die_mem = die_mem;
  262. /* dwarf_getscopes can't find subprogram. */
  263. if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
  264. return NULL;
  265. else
  266. return die_mem;
  267. }
  268. /* die_find callback for inline function search */
  269. static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
  270. {
  271. Dwarf_Addr *addr = data;
  272. if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
  273. dwarf_haspc(die_mem, *addr))
  274. return DIE_FIND_CB_FOUND;
  275. return DIE_FIND_CB_CONTINUE;
  276. }
  277. /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
  278. static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
  279. Dwarf_Die *die_mem)
  280. {
  281. return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
  282. }
  283. struct __find_variable_param {
  284. const char *name;
  285. Dwarf_Addr addr;
  286. };
  287. static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
  288. {
  289. struct __find_variable_param *fvp = data;
  290. int tag;
  291. tag = dwarf_tag(die_mem);
  292. if ((tag == DW_TAG_formal_parameter ||
  293. tag == DW_TAG_variable) &&
  294. die_compare_name(die_mem, fvp->name))
  295. return DIE_FIND_CB_FOUND;
  296. if (dwarf_haspc(die_mem, fvp->addr))
  297. return DIE_FIND_CB_CONTINUE;
  298. else
  299. return DIE_FIND_CB_SIBLING;
  300. }
  301. /* Find a variable called 'name' at given address */
  302. static Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
  303. Dwarf_Addr addr, Dwarf_Die *die_mem)
  304. {
  305. struct __find_variable_param fvp = { .name = name, .addr = addr};
  306. return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp,
  307. die_mem);
  308. }
  309. static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
  310. {
  311. const char *name = data;
  312. if ((dwarf_tag(die_mem) == DW_TAG_member) &&
  313. die_compare_name(die_mem, name))
  314. return DIE_FIND_CB_FOUND;
  315. return DIE_FIND_CB_SIBLING;
  316. }
  317. /* Find a member called 'name' */
  318. static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
  319. Dwarf_Die *die_mem)
  320. {
  321. return die_find_child(st_die, __die_find_member_cb, (void *)name,
  322. die_mem);
  323. }
  324. /*
  325. * Probe finder related functions
  326. */
  327. static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
  328. {
  329. struct probe_trace_arg_ref *ref;
  330. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  331. if (ref != NULL)
  332. ref->offset = offs;
  333. return ref;
  334. }
  335. /* Show a location */
  336. static int convert_variable_location(Dwarf_Die *vr_die, struct probe_finder *pf)
  337. {
  338. Dwarf_Attribute attr;
  339. Dwarf_Op *op;
  340. size_t nops;
  341. unsigned int regn;
  342. Dwarf_Word offs = 0;
  343. bool ref = false;
  344. const char *regs;
  345. struct probe_trace_arg *tvar = pf->tvar;
  346. int ret;
  347. if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
  348. goto static_var;
  349. /* TODO: handle more than 1 exprs */
  350. if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL ||
  351. dwarf_getlocation_addr(&attr, pf->addr, &op, &nops, 1) <= 0 ||
  352. nops == 0) {
  353. /* TODO: Support const_value */
  354. pr_err("Failed to find the location of %s at this address.\n"
  355. " Perhaps, it has been optimized out.\n", pf->pvar->var);
  356. return -ENOENT;
  357. }
  358. if (op->atom == DW_OP_addr) {
  359. static_var:
  360. /* Static variables on memory (not stack), make @varname */
  361. ret = strlen(dwarf_diename(vr_die));
  362. tvar->value = zalloc(ret + 2);
  363. if (tvar->value == NULL)
  364. return -ENOMEM;
  365. snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
  366. tvar->ref = alloc_trace_arg_ref((long)offs);
  367. if (tvar->ref == NULL)
  368. return -ENOMEM;
  369. return 0;
  370. }
  371. /* If this is based on frame buffer, set the offset */
  372. if (op->atom == DW_OP_fbreg) {
  373. if (pf->fb_ops == NULL) {
  374. pr_warning("The attribute of frame base is not "
  375. "supported.\n");
  376. return -ENOTSUP;
  377. }
  378. ref = true;
  379. offs = op->number;
  380. op = &pf->fb_ops[0];
  381. }
  382. if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
  383. regn = op->atom - DW_OP_breg0;
  384. offs += op->number;
  385. ref = true;
  386. } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
  387. regn = op->atom - DW_OP_reg0;
  388. } else if (op->atom == DW_OP_bregx) {
  389. regn = op->number;
  390. offs += op->number2;
  391. ref = true;
  392. } else if (op->atom == DW_OP_regx) {
  393. regn = op->number;
  394. } else {
  395. pr_warning("DW_OP %x is not supported.\n", op->atom);
  396. return -ENOTSUP;
  397. }
  398. regs = get_arch_regstr(regn);
  399. if (!regs) {
  400. pr_warning("Mapping for DWARF register number %u missing on this architecture.", regn);
  401. return -ERANGE;
  402. }
  403. tvar->value = strdup(regs);
  404. if (tvar->value == NULL)
  405. return -ENOMEM;
  406. if (ref) {
  407. tvar->ref = alloc_trace_arg_ref((long)offs);
  408. if (tvar->ref == NULL)
  409. return -ENOMEM;
  410. }
  411. return 0;
  412. }
  413. static int convert_variable_type(Dwarf_Die *vr_die,
  414. struct probe_trace_arg *tvar,
  415. const char *cast)
  416. {
  417. struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
  418. Dwarf_Die type;
  419. char buf[16];
  420. int ret;
  421. /* TODO: check all types */
  422. if (cast && strcmp(cast, "string") != 0) {
  423. /* Non string type is OK */
  424. tvar->type = strdup(cast);
  425. return (tvar->type == NULL) ? -ENOMEM : 0;
  426. }
  427. if (die_get_real_type(vr_die, &type) == NULL) {
  428. pr_warning("Failed to get a type information of %s.\n",
  429. dwarf_diename(vr_die));
  430. return -ENOENT;
  431. }
  432. pr_debug("%s type is %s.\n",
  433. dwarf_diename(vr_die), dwarf_diename(&type));
  434. if (cast && strcmp(cast, "string") == 0) { /* String type */
  435. ret = dwarf_tag(&type);
  436. if (ret != DW_TAG_pointer_type &&
  437. ret != DW_TAG_array_type) {
  438. pr_warning("Failed to cast into string: "
  439. "%s(%s) is not a pointer nor array.",
  440. dwarf_diename(vr_die), dwarf_diename(&type));
  441. return -EINVAL;
  442. }
  443. if (ret == DW_TAG_pointer_type) {
  444. if (die_get_real_type(&type, &type) == NULL) {
  445. pr_warning("Failed to get a type information.");
  446. return -ENOENT;
  447. }
  448. while (*ref_ptr)
  449. ref_ptr = &(*ref_ptr)->next;
  450. /* Add new reference with offset +0 */
  451. *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
  452. if (*ref_ptr == NULL) {
  453. pr_warning("Out of memory error\n");
  454. return -ENOMEM;
  455. }
  456. }
  457. if (!die_compare_name(&type, "char") &&
  458. !die_compare_name(&type, "unsigned char")) {
  459. pr_warning("Failed to cast into string: "
  460. "%s is not (unsigned) char *.",
  461. dwarf_diename(vr_die));
  462. return -EINVAL;
  463. }
  464. tvar->type = strdup(cast);
  465. return (tvar->type == NULL) ? -ENOMEM : 0;
  466. }
  467. ret = die_get_byte_size(&type) * 8;
  468. if (ret) {
  469. /* Check the bitwidth */
  470. if (ret > MAX_BASIC_TYPE_BITS) {
  471. pr_info("%s exceeds max-bitwidth."
  472. " Cut down to %d bits.\n",
  473. dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
  474. ret = MAX_BASIC_TYPE_BITS;
  475. }
  476. ret = snprintf(buf, 16, "%c%d",
  477. die_is_signed_type(&type) ? 's' : 'u', ret);
  478. if (ret < 0 || ret >= 16) {
  479. if (ret >= 16)
  480. ret = -E2BIG;
  481. pr_warning("Failed to convert variable type: %s\n",
  482. strerror(-ret));
  483. return ret;
  484. }
  485. tvar->type = strdup(buf);
  486. if (tvar->type == NULL)
  487. return -ENOMEM;
  488. }
  489. return 0;
  490. }
  491. static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
  492. struct perf_probe_arg_field *field,
  493. struct probe_trace_arg_ref **ref_ptr,
  494. Dwarf_Die *die_mem)
  495. {
  496. struct probe_trace_arg_ref *ref = *ref_ptr;
  497. Dwarf_Die type;
  498. Dwarf_Word offs;
  499. int ret, tag;
  500. pr_debug("converting %s in %s\n", field->name, varname);
  501. if (die_get_real_type(vr_die, &type) == NULL) {
  502. pr_warning("Failed to get the type of %s.\n", varname);
  503. return -ENOENT;
  504. }
  505. pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
  506. tag = dwarf_tag(&type);
  507. if (field->name[0] == '[' &&
  508. (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
  509. if (field->next)
  510. /* Save original type for next field */
  511. memcpy(die_mem, &type, sizeof(*die_mem));
  512. /* Get the type of this array */
  513. if (die_get_real_type(&type, &type) == NULL) {
  514. pr_warning("Failed to get the type of %s.\n", varname);
  515. return -ENOENT;
  516. }
  517. pr_debug2("Array real type: (%x)\n",
  518. (unsigned)dwarf_dieoffset(&type));
  519. if (tag == DW_TAG_pointer_type) {
  520. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  521. if (ref == NULL)
  522. return -ENOMEM;
  523. if (*ref_ptr)
  524. (*ref_ptr)->next = ref;
  525. else
  526. *ref_ptr = ref;
  527. }
  528. ref->offset += die_get_byte_size(&type) * field->index;
  529. if (!field->next)
  530. /* Save vr_die for converting types */
  531. memcpy(die_mem, vr_die, sizeof(*die_mem));
  532. goto next;
  533. } else if (tag == DW_TAG_pointer_type) {
  534. /* Check the pointer and dereference */
  535. if (!field->ref) {
  536. pr_err("Semantic error: %s must be referred by '->'\n",
  537. field->name);
  538. return -EINVAL;
  539. }
  540. /* Get the type pointed by this pointer */
  541. if (die_get_real_type(&type, &type) == NULL) {
  542. pr_warning("Failed to get the type of %s.\n", varname);
  543. return -ENOENT;
  544. }
  545. /* Verify it is a data structure */
  546. if (dwarf_tag(&type) != DW_TAG_structure_type) {
  547. pr_warning("%s is not a data structure.\n", varname);
  548. return -EINVAL;
  549. }
  550. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  551. if (ref == NULL)
  552. return -ENOMEM;
  553. if (*ref_ptr)
  554. (*ref_ptr)->next = ref;
  555. else
  556. *ref_ptr = ref;
  557. } else {
  558. /* Verify it is a data structure */
  559. if (tag != DW_TAG_structure_type) {
  560. pr_warning("%s is not a data structure.\n", varname);
  561. return -EINVAL;
  562. }
  563. if (field->name[0] == '[') {
  564. pr_err("Semantic error: %s is not a pointor nor array.",
  565. varname);
  566. return -EINVAL;
  567. }
  568. if (field->ref) {
  569. pr_err("Semantic error: %s must be referred by '.'\n",
  570. field->name);
  571. return -EINVAL;
  572. }
  573. if (!ref) {
  574. pr_warning("Structure on a register is not "
  575. "supported yet.\n");
  576. return -ENOTSUP;
  577. }
  578. }
  579. if (die_find_member(&type, field->name, die_mem) == NULL) {
  580. pr_warning("%s(tyep:%s) has no member %s.\n", varname,
  581. dwarf_diename(&type), field->name);
  582. return -EINVAL;
  583. }
  584. /* Get the offset of the field */
  585. ret = die_get_data_member_location(die_mem, &offs);
  586. if (ret < 0) {
  587. pr_warning("Failed to get the offset of %s.\n", field->name);
  588. return ret;
  589. }
  590. ref->offset += (long)offs;
  591. next:
  592. /* Converting next field */
  593. if (field->next)
  594. return convert_variable_fields(die_mem, field->name,
  595. field->next, &ref, die_mem);
  596. else
  597. return 0;
  598. }
  599. /* Show a variables in kprobe event format */
  600. static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
  601. {
  602. Dwarf_Die die_mem;
  603. int ret;
  604. pr_debug("Converting variable %s into trace event.\n",
  605. dwarf_diename(vr_die));
  606. ret = convert_variable_location(vr_die, pf);
  607. if (ret == 0 && pf->pvar->field) {
  608. ret = convert_variable_fields(vr_die, pf->pvar->var,
  609. pf->pvar->field, &pf->tvar->ref,
  610. &die_mem);
  611. vr_die = &die_mem;
  612. }
  613. if (ret == 0)
  614. ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
  615. /* *expr will be cached in libdw. Don't free it. */
  616. return ret;
  617. }
  618. /* Find a variable in a subprogram die */
  619. static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
  620. {
  621. Dwarf_Die vr_die, *scopes;
  622. char buf[32], *ptr;
  623. int ret, nscopes;
  624. if (!is_c_varname(pf->pvar->var)) {
  625. /* Copy raw parameters */
  626. pf->tvar->value = strdup(pf->pvar->var);
  627. if (pf->tvar->value == NULL)
  628. return -ENOMEM;
  629. if (pf->pvar->type) {
  630. pf->tvar->type = strdup(pf->pvar->type);
  631. if (pf->tvar->type == NULL)
  632. return -ENOMEM;
  633. }
  634. if (pf->pvar->name) {
  635. pf->tvar->name = strdup(pf->pvar->name);
  636. if (pf->tvar->name == NULL)
  637. return -ENOMEM;
  638. } else
  639. pf->tvar->name = NULL;
  640. return 0;
  641. }
  642. if (pf->pvar->name)
  643. pf->tvar->name = strdup(pf->pvar->name);
  644. else {
  645. ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
  646. if (ret < 0)
  647. return ret;
  648. ptr = strchr(buf, ':'); /* Change type separator to _ */
  649. if (ptr)
  650. *ptr = '_';
  651. pf->tvar->name = strdup(buf);
  652. }
  653. if (pf->tvar->name == NULL)
  654. return -ENOMEM;
  655. pr_debug("Searching '%s' variable in context.\n",
  656. pf->pvar->var);
  657. /* Search child die for local variables and parameters. */
  658. if (die_find_variable_at(sp_die, pf->pvar->var, pf->addr, &vr_die))
  659. ret = convert_variable(&vr_die, pf);
  660. else {
  661. /* Search upper class */
  662. nscopes = dwarf_getscopes_die(sp_die, &scopes);
  663. while (nscopes-- > 1) {
  664. pr_debug("Searching variables in %s\n",
  665. dwarf_diename(&scopes[nscopes]));
  666. /* We should check this scope, so give dummy address */
  667. if (die_find_variable_at(&scopes[nscopes],
  668. pf->pvar->var, 0,
  669. &vr_die)) {
  670. ret = convert_variable(&vr_die, pf);
  671. goto found;
  672. }
  673. }
  674. if (scopes)
  675. free(scopes);
  676. ret = -ENOENT;
  677. }
  678. found:
  679. if (ret < 0)
  680. pr_warning("Failed to find '%s' in this function.\n",
  681. pf->pvar->var);
  682. return ret;
  683. }
  684. /* Show a probe point to output buffer */
  685. static int convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
  686. {
  687. struct probe_trace_event *tev;
  688. Dwarf_Addr eaddr;
  689. Dwarf_Die die_mem;
  690. const char *name;
  691. int ret, i;
  692. Dwarf_Attribute fb_attr;
  693. size_t nops;
  694. if (pf->ntevs == pf->max_tevs) {
  695. pr_warning("Too many( > %d) probe point found.\n",
  696. pf->max_tevs);
  697. return -ERANGE;
  698. }
  699. tev = &pf->tevs[pf->ntevs++];
  700. /* If no real subprogram, find a real one */
  701. if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
  702. sp_die = die_find_real_subprogram(&pf->cu_die,
  703. pf->addr, &die_mem);
  704. if (!sp_die) {
  705. pr_warning("Failed to find probe point in any "
  706. "functions.\n");
  707. return -ENOENT;
  708. }
  709. }
  710. /* Copy the name of probe point */
  711. name = dwarf_diename(sp_die);
  712. if (name) {
  713. if (dwarf_entrypc(sp_die, &eaddr) != 0) {
  714. pr_warning("Failed to get entry pc of %s\n",
  715. dwarf_diename(sp_die));
  716. return -ENOENT;
  717. }
  718. tev->point.symbol = strdup(name);
  719. if (tev->point.symbol == NULL)
  720. return -ENOMEM;
  721. tev->point.offset = (unsigned long)(pf->addr - eaddr);
  722. } else
  723. /* This function has no name. */
  724. tev->point.offset = (unsigned long)pf->addr;
  725. /* Return probe must be on the head of a subprogram */
  726. if (pf->pev->point.retprobe) {
  727. if (tev->point.offset != 0) {
  728. pr_warning("Return probe must be on the head of"
  729. " a real function\n");
  730. return -EINVAL;
  731. }
  732. tev->point.retprobe = true;
  733. }
  734. pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
  735. tev->point.offset);
  736. /* Get the frame base attribute/ops */
  737. dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
  738. ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
  739. if (ret <= 0 || nops == 0) {
  740. pf->fb_ops = NULL;
  741. #if _ELFUTILS_PREREQ(0, 142)
  742. } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
  743. pf->cfi != NULL) {
  744. Dwarf_Frame *frame;
  745. if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
  746. dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
  747. pr_warning("Failed to get CFA on 0x%jx\n",
  748. (uintmax_t)pf->addr);
  749. return -ENOENT;
  750. }
  751. #endif
  752. }
  753. /* Find each argument */
  754. tev->nargs = pf->pev->nargs;
  755. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  756. if (tev->args == NULL)
  757. return -ENOMEM;
  758. for (i = 0; i < pf->pev->nargs; i++) {
  759. pf->pvar = &pf->pev->args[i];
  760. pf->tvar = &tev->args[i];
  761. ret = find_variable(sp_die, pf);
  762. if (ret != 0)
  763. return ret;
  764. }
  765. /* *pf->fb_ops will be cached in libdw. Don't free it. */
  766. pf->fb_ops = NULL;
  767. return 0;
  768. }
  769. /* Find probe point from its line number */
  770. static int find_probe_point_by_line(struct probe_finder *pf)
  771. {
  772. Dwarf_Lines *lines;
  773. Dwarf_Line *line;
  774. size_t nlines, i;
  775. Dwarf_Addr addr;
  776. int lineno;
  777. int ret = 0;
  778. if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
  779. pr_warning("No source lines found in this CU.\n");
  780. return -ENOENT;
  781. }
  782. for (i = 0; i < nlines && ret == 0; i++) {
  783. line = dwarf_onesrcline(lines, i);
  784. if (dwarf_lineno(line, &lineno) != 0 ||
  785. lineno != pf->lno)
  786. continue;
  787. /* TODO: Get fileno from line, but how? */
  788. if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
  789. continue;
  790. if (dwarf_lineaddr(line, &addr) != 0) {
  791. pr_warning("Failed to get the address of the line.\n");
  792. return -ENOENT;
  793. }
  794. pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
  795. (int)i, lineno, (uintmax_t)addr);
  796. pf->addr = addr;
  797. ret = convert_probe_point(NULL, pf);
  798. /* Continuing, because target line might be inlined. */
  799. }
  800. return ret;
  801. }
  802. /* Find lines which match lazy pattern */
  803. static int find_lazy_match_lines(struct list_head *head,
  804. const char *fname, const char *pat)
  805. {
  806. char *fbuf, *p1, *p2;
  807. int fd, line, nlines = -1;
  808. struct stat st;
  809. fd = open(fname, O_RDONLY);
  810. if (fd < 0) {
  811. pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
  812. return -errno;
  813. }
  814. if (fstat(fd, &st) < 0) {
  815. pr_warning("Failed to get the size of %s: %s\n",
  816. fname, strerror(errno));
  817. nlines = -errno;
  818. goto out_close;
  819. }
  820. nlines = -ENOMEM;
  821. fbuf = malloc(st.st_size + 2);
  822. if (fbuf == NULL)
  823. goto out_close;
  824. if (read(fd, fbuf, st.st_size) < 0) {
  825. pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
  826. nlines = -errno;
  827. goto out_free_fbuf;
  828. }
  829. fbuf[st.st_size] = '\n'; /* Dummy line */
  830. fbuf[st.st_size + 1] = '\0';
  831. p1 = fbuf;
  832. line = 1;
  833. nlines = 0;
  834. while ((p2 = strchr(p1, '\n')) != NULL) {
  835. *p2 = '\0';
  836. if (strlazymatch(p1, pat)) {
  837. line_list__add_line(head, line);
  838. nlines++;
  839. }
  840. line++;
  841. p1 = p2 + 1;
  842. }
  843. out_free_fbuf:
  844. free(fbuf);
  845. out_close:
  846. close(fd);
  847. return nlines;
  848. }
  849. /* Find probe points from lazy pattern */
  850. static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
  851. {
  852. Dwarf_Lines *lines;
  853. Dwarf_Line *line;
  854. size_t nlines, i;
  855. Dwarf_Addr addr;
  856. Dwarf_Die die_mem;
  857. int lineno;
  858. int ret = 0;
  859. if (list_empty(&pf->lcache)) {
  860. /* Matching lazy line pattern */
  861. ret = find_lazy_match_lines(&pf->lcache, pf->fname,
  862. pf->pev->point.lazy_line);
  863. if (ret == 0) {
  864. pr_debug("No matched lines found in %s.\n", pf->fname);
  865. return 0;
  866. } else if (ret < 0)
  867. return ret;
  868. }
  869. if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
  870. pr_warning("No source lines found in this CU.\n");
  871. return -ENOENT;
  872. }
  873. for (i = 0; i < nlines && ret >= 0; i++) {
  874. line = dwarf_onesrcline(lines, i);
  875. if (dwarf_lineno(line, &lineno) != 0 ||
  876. !line_list__has_line(&pf->lcache, lineno))
  877. continue;
  878. /* TODO: Get fileno from line, but how? */
  879. if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
  880. continue;
  881. if (dwarf_lineaddr(line, &addr) != 0) {
  882. pr_debug("Failed to get the address of line %d.\n",
  883. lineno);
  884. continue;
  885. }
  886. if (sp_die) {
  887. /* Address filtering 1: does sp_die include addr? */
  888. if (!dwarf_haspc(sp_die, addr))
  889. continue;
  890. /* Address filtering 2: No child include addr? */
  891. if (die_find_inlinefunc(sp_die, addr, &die_mem))
  892. continue;
  893. }
  894. pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
  895. (int)i, lineno, (unsigned long long)addr);
  896. pf->addr = addr;
  897. ret = convert_probe_point(sp_die, pf);
  898. /* Continuing, because target line might be inlined. */
  899. }
  900. /* TODO: deallocate lines, but how? */
  901. return ret;
  902. }
  903. /* Callback parameter with return value */
  904. struct dwarf_callback_param {
  905. void *data;
  906. int retval;
  907. };
  908. static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
  909. {
  910. struct dwarf_callback_param *param = data;
  911. struct probe_finder *pf = param->data;
  912. struct perf_probe_point *pp = &pf->pev->point;
  913. Dwarf_Addr addr;
  914. if (pp->lazy_line)
  915. param->retval = find_probe_point_lazy(in_die, pf);
  916. else {
  917. /* Get probe address */
  918. if (dwarf_entrypc(in_die, &addr) != 0) {
  919. pr_warning("Failed to get entry pc of %s.\n",
  920. dwarf_diename(in_die));
  921. param->retval = -ENOENT;
  922. return DWARF_CB_ABORT;
  923. }
  924. pf->addr = addr;
  925. pf->addr += pp->offset;
  926. pr_debug("found inline addr: 0x%jx\n",
  927. (uintmax_t)pf->addr);
  928. param->retval = convert_probe_point(in_die, pf);
  929. if (param->retval < 0)
  930. return DWARF_CB_ABORT;
  931. }
  932. return DWARF_CB_OK;
  933. }
  934. /* Search function from function name */
  935. static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
  936. {
  937. struct dwarf_callback_param *param = data;
  938. struct probe_finder *pf = param->data;
  939. struct perf_probe_point *pp = &pf->pev->point;
  940. /* Check tag and diename */
  941. if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
  942. !die_compare_name(sp_die, pp->function))
  943. return DWARF_CB_OK;
  944. pf->fname = dwarf_decl_file(sp_die);
  945. if (pp->line) { /* Function relative line */
  946. dwarf_decl_line(sp_die, &pf->lno);
  947. pf->lno += pp->line;
  948. param->retval = find_probe_point_by_line(pf);
  949. } else if (!dwarf_func_inline(sp_die)) {
  950. /* Real function */
  951. if (pp->lazy_line)
  952. param->retval = find_probe_point_lazy(sp_die, pf);
  953. else {
  954. if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
  955. pr_warning("Failed to get entry pc of %s.\n",
  956. dwarf_diename(sp_die));
  957. param->retval = -ENOENT;
  958. return DWARF_CB_ABORT;
  959. }
  960. pf->addr += pp->offset;
  961. /* TODO: Check the address in this function */
  962. param->retval = convert_probe_point(sp_die, pf);
  963. }
  964. } else {
  965. struct dwarf_callback_param _param = {.data = (void *)pf,
  966. .retval = 0};
  967. /* Inlined function: search instances */
  968. dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
  969. &_param);
  970. param->retval = _param.retval;
  971. }
  972. return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
  973. }
  974. static int find_probe_point_by_func(struct probe_finder *pf)
  975. {
  976. struct dwarf_callback_param _param = {.data = (void *)pf,
  977. .retval = 0};
  978. dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
  979. return _param.retval;
  980. }
  981. /* Find probe_trace_events specified by perf_probe_event from debuginfo */
  982. int find_probe_trace_events(int fd, struct perf_probe_event *pev,
  983. struct probe_trace_event **tevs, int max_tevs)
  984. {
  985. struct probe_finder pf = {.pev = pev, .max_tevs = max_tevs};
  986. struct perf_probe_point *pp = &pev->point;
  987. Dwarf_Off off, noff;
  988. size_t cuhl;
  989. Dwarf_Die *diep;
  990. Dwarf *dbg;
  991. int ret = 0;
  992. pf.tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
  993. if (pf.tevs == NULL)
  994. return -ENOMEM;
  995. *tevs = pf.tevs;
  996. pf.ntevs = 0;
  997. dbg = dwarf_begin(fd, DWARF_C_READ);
  998. if (!dbg) {
  999. pr_warning("No dwarf info found in the vmlinux - "
  1000. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  1001. free(pf.tevs);
  1002. *tevs = NULL;
  1003. return -EBADF;
  1004. }
  1005. #if _ELFUTILS_PREREQ(0, 142)
  1006. /* Get the call frame information from this dwarf */
  1007. pf.cfi = dwarf_getcfi(dbg);
  1008. #endif
  1009. off = 0;
  1010. line_list__init(&pf.lcache);
  1011. /* Loop on CUs (Compilation Unit) */
  1012. while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) &&
  1013. ret >= 0) {
  1014. /* Get the DIE(Debugging Information Entry) of this CU */
  1015. diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
  1016. if (!diep)
  1017. continue;
  1018. /* Check if target file is included. */
  1019. if (pp->file)
  1020. pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
  1021. else
  1022. pf.fname = NULL;
  1023. if (!pp->file || pf.fname) {
  1024. if (pp->function)
  1025. ret = find_probe_point_by_func(&pf);
  1026. else if (pp->lazy_line)
  1027. ret = find_probe_point_lazy(NULL, &pf);
  1028. else {
  1029. pf.lno = pp->line;
  1030. ret = find_probe_point_by_line(&pf);
  1031. }
  1032. }
  1033. off = noff;
  1034. }
  1035. line_list__free(&pf.lcache);
  1036. dwarf_end(dbg);
  1037. return (ret < 0) ? ret : pf.ntevs;
  1038. }
  1039. /* Reverse search */
  1040. int find_perf_probe_point(int fd, unsigned long addr,
  1041. struct perf_probe_point *ppt)
  1042. {
  1043. Dwarf_Die cudie, spdie, indie;
  1044. Dwarf *dbg;
  1045. Dwarf_Line *line;
  1046. Dwarf_Addr laddr, eaddr;
  1047. const char *tmp;
  1048. int lineno, ret = 0;
  1049. bool found = false;
  1050. dbg = dwarf_begin(fd, DWARF_C_READ);
  1051. if (!dbg)
  1052. return -EBADF;
  1053. /* Find cu die */
  1054. if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr, &cudie)) {
  1055. ret = -EINVAL;
  1056. goto end;
  1057. }
  1058. /* Find a corresponding line */
  1059. line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
  1060. if (line) {
  1061. if (dwarf_lineaddr(line, &laddr) == 0 &&
  1062. (Dwarf_Addr)addr == laddr &&
  1063. dwarf_lineno(line, &lineno) == 0) {
  1064. tmp = dwarf_linesrc(line, NULL, NULL);
  1065. if (tmp) {
  1066. ppt->line = lineno;
  1067. ppt->file = strdup(tmp);
  1068. if (ppt->file == NULL) {
  1069. ret = -ENOMEM;
  1070. goto end;
  1071. }
  1072. found = true;
  1073. }
  1074. }
  1075. }
  1076. /* Find a corresponding function */
  1077. if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
  1078. tmp = dwarf_diename(&spdie);
  1079. if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0)
  1080. goto end;
  1081. if (ppt->line) {
  1082. if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
  1083. &indie)) {
  1084. /* addr in an inline function */
  1085. tmp = dwarf_diename(&indie);
  1086. if (!tmp)
  1087. goto end;
  1088. ret = dwarf_decl_line(&indie, &lineno);
  1089. } else {
  1090. if (eaddr == addr) { /* Function entry */
  1091. lineno = ppt->line;
  1092. ret = 0;
  1093. } else
  1094. ret = dwarf_decl_line(&spdie, &lineno);
  1095. }
  1096. if (ret == 0) {
  1097. /* Make a relative line number */
  1098. ppt->line -= lineno;
  1099. goto found;
  1100. }
  1101. }
  1102. /* We don't have a line number, let's use offset */
  1103. ppt->offset = addr - (unsigned long)eaddr;
  1104. found:
  1105. ppt->function = strdup(tmp);
  1106. if (ppt->function == NULL) {
  1107. ret = -ENOMEM;
  1108. goto end;
  1109. }
  1110. found = true;
  1111. }
  1112. end:
  1113. dwarf_end(dbg);
  1114. if (ret >= 0)
  1115. ret = found ? 1 : 0;
  1116. return ret;
  1117. }
  1118. /* Add a line and store the src path */
  1119. static int line_range_add_line(const char *src, unsigned int lineno,
  1120. struct line_range *lr)
  1121. {
  1122. /* Copy source path */
  1123. if (!lr->path) {
  1124. lr->path = strdup(src);
  1125. if (lr->path == NULL)
  1126. return -ENOMEM;
  1127. }
  1128. return line_list__add_line(&lr->line_list, lineno);
  1129. }
  1130. /* Search function declaration lines */
  1131. static int line_range_funcdecl_cb(Dwarf_Die *sp_die, void *data)
  1132. {
  1133. struct dwarf_callback_param *param = data;
  1134. struct line_finder *lf = param->data;
  1135. const char *src;
  1136. int lineno;
  1137. src = dwarf_decl_file(sp_die);
  1138. if (src && strtailcmp(src, lf->fname) != 0)
  1139. return DWARF_CB_OK;
  1140. if (dwarf_decl_line(sp_die, &lineno) != 0 ||
  1141. (lf->lno_s > lineno || lf->lno_e < lineno))
  1142. return DWARF_CB_OK;
  1143. param->retval = line_range_add_line(src, lineno, lf->lr);
  1144. if (param->retval < 0)
  1145. return DWARF_CB_ABORT;
  1146. return DWARF_CB_OK;
  1147. }
  1148. static int find_line_range_func_decl_lines(struct line_finder *lf)
  1149. {
  1150. struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
  1151. dwarf_getfuncs(&lf->cu_die, line_range_funcdecl_cb, &param, 0);
  1152. return param.retval;
  1153. }
  1154. /* Find line range from its line number */
  1155. static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
  1156. {
  1157. Dwarf_Lines *lines;
  1158. Dwarf_Line *line;
  1159. size_t nlines, i;
  1160. Dwarf_Addr addr;
  1161. int lineno, ret = 0;
  1162. const char *src;
  1163. Dwarf_Die die_mem;
  1164. line_list__init(&lf->lr->line_list);
  1165. if (dwarf_getsrclines(&lf->cu_die, &lines, &nlines) != 0) {
  1166. pr_warning("No source lines found in this CU.\n");
  1167. return -ENOENT;
  1168. }
  1169. /* Search probable lines on lines list */
  1170. for (i = 0; i < nlines; i++) {
  1171. line = dwarf_onesrcline(lines, i);
  1172. if (dwarf_lineno(line, &lineno) != 0 ||
  1173. (lf->lno_s > lineno || lf->lno_e < lineno))
  1174. continue;
  1175. if (sp_die) {
  1176. /* Address filtering 1: does sp_die include addr? */
  1177. if (dwarf_lineaddr(line, &addr) != 0 ||
  1178. !dwarf_haspc(sp_die, addr))
  1179. continue;
  1180. /* Address filtering 2: No child include addr? */
  1181. if (die_find_inlinefunc(sp_die, addr, &die_mem))
  1182. continue;
  1183. }
  1184. /* TODO: Get fileno from line, but how? */
  1185. src = dwarf_linesrc(line, NULL, NULL);
  1186. if (strtailcmp(src, lf->fname) != 0)
  1187. continue;
  1188. ret = line_range_add_line(src, lineno, lf->lr);
  1189. if (ret < 0)
  1190. return ret;
  1191. }
  1192. /*
  1193. * Dwarf lines doesn't include function declarations. We have to
  1194. * check functions list or given function.
  1195. */
  1196. if (sp_die) {
  1197. src = dwarf_decl_file(sp_die);
  1198. if (src && dwarf_decl_line(sp_die, &lineno) == 0 &&
  1199. (lf->lno_s <= lineno && lf->lno_e >= lineno))
  1200. ret = line_range_add_line(src, lineno, lf->lr);
  1201. } else
  1202. ret = find_line_range_func_decl_lines(lf);
  1203. /* Update status */
  1204. if (ret >= 0)
  1205. if (!list_empty(&lf->lr->line_list))
  1206. ret = lf->found = 1;
  1207. else
  1208. ret = 0; /* Lines are not found */
  1209. else {
  1210. free(lf->lr->path);
  1211. lf->lr->path = NULL;
  1212. }
  1213. return ret;
  1214. }
  1215. static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
  1216. {
  1217. struct dwarf_callback_param *param = data;
  1218. param->retval = find_line_range_by_line(in_die, param->data);
  1219. return DWARF_CB_ABORT; /* No need to find other instances */
  1220. }
  1221. /* Search function from function name */
  1222. static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
  1223. {
  1224. struct dwarf_callback_param *param = data;
  1225. struct line_finder *lf = param->data;
  1226. struct line_range *lr = lf->lr;
  1227. if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
  1228. die_compare_name(sp_die, lr->function)) {
  1229. lf->fname = dwarf_decl_file(sp_die);
  1230. dwarf_decl_line(sp_die, &lr->offset);
  1231. pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
  1232. lf->lno_s = lr->offset + lr->start;
  1233. if (lf->lno_s < 0) /* Overflow */
  1234. lf->lno_s = INT_MAX;
  1235. lf->lno_e = lr->offset + lr->end;
  1236. if (lf->lno_e < 0) /* Overflow */
  1237. lf->lno_e = INT_MAX;
  1238. pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
  1239. lr->start = lf->lno_s;
  1240. lr->end = lf->lno_e;
  1241. if (dwarf_func_inline(sp_die)) {
  1242. struct dwarf_callback_param _param;
  1243. _param.data = (void *)lf;
  1244. _param.retval = 0;
  1245. dwarf_func_inline_instances(sp_die,
  1246. line_range_inline_cb,
  1247. &_param);
  1248. param->retval = _param.retval;
  1249. } else
  1250. param->retval = find_line_range_by_line(sp_die, lf);
  1251. return DWARF_CB_ABORT;
  1252. }
  1253. return DWARF_CB_OK;
  1254. }
  1255. static int find_line_range_by_func(struct line_finder *lf)
  1256. {
  1257. struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
  1258. dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
  1259. return param.retval;
  1260. }
  1261. int find_line_range(int fd, struct line_range *lr)
  1262. {
  1263. struct line_finder lf = {.lr = lr, .found = 0};
  1264. int ret = 0;
  1265. Dwarf_Off off = 0, noff;
  1266. size_t cuhl;
  1267. Dwarf_Die *diep;
  1268. Dwarf *dbg;
  1269. const char *comp_dir;
  1270. dbg = dwarf_begin(fd, DWARF_C_READ);
  1271. if (!dbg) {
  1272. pr_warning("No dwarf info found in the vmlinux - "
  1273. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  1274. return -EBADF;
  1275. }
  1276. /* Loop on CUs (Compilation Unit) */
  1277. while (!lf.found && ret >= 0) {
  1278. if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
  1279. break;
  1280. /* Get the DIE(Debugging Information Entry) of this CU */
  1281. diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
  1282. if (!diep)
  1283. continue;
  1284. /* Check if target file is included. */
  1285. if (lr->file)
  1286. lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
  1287. else
  1288. lf.fname = 0;
  1289. if (!lr->file || lf.fname) {
  1290. if (lr->function)
  1291. ret = find_line_range_by_func(&lf);
  1292. else {
  1293. lf.lno_s = lr->start;
  1294. lf.lno_e = lr->end;
  1295. ret = find_line_range_by_line(NULL, &lf);
  1296. }
  1297. }
  1298. off = noff;
  1299. }
  1300. /* Store comp_dir */
  1301. if (lf.found) {
  1302. comp_dir = cu_get_comp_dir(&lf.cu_die);
  1303. if (comp_dir) {
  1304. lr->comp_dir = strdup(comp_dir);
  1305. if (!lr->comp_dir)
  1306. ret = -ENOMEM;
  1307. }
  1308. }
  1309. pr_debug("path: %s\n", lr->path);
  1310. dwarf_end(dbg);
  1311. return (ret < 0) ? ret : lf.found;
  1312. }