probe-finder.c 35 KB

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