probe-finder.c 30 KB

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