probe-finder.c 36 KB

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