probe-finder.c 36 KB

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