probe-finder.c 36 KB

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