skip-callchain-idx.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Use DWARF Debug information to skip unnecessary callchain entries.
  3. *
  4. * Copyright (C) 2014 Sukadev Bhattiprolu, IBM Corporation.
  5. * Copyright (C) 2014 Ulrich Weigand, IBM Corporation.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <inttypes.h>
  13. #include <dwarf.h>
  14. #include <elfutils/libdwfl.h>
  15. #include "util/thread.h"
  16. #include "util/callchain.h"
  17. #include "util/debug.h"
  18. /*
  19. * When saving the callchain on Power, the kernel conservatively saves
  20. * excess entries in the callchain. A few of these entries are needed
  21. * in some cases but not others. If the unnecessary entries are not
  22. * ignored, we end up with duplicate arcs in the call-graphs. Use
  23. * DWARF debug information to skip over any unnecessary callchain
  24. * entries.
  25. *
  26. * See function header for arch_adjust_callchain() below for more details.
  27. *
  28. * The libdwfl code in this file is based on code from elfutils
  29. * (libdwfl/argp-std.c, libdwfl/tests/addrcfi.c, etc).
  30. */
  31. static char *debuginfo_path;
  32. static const Dwfl_Callbacks offline_callbacks = {
  33. .debuginfo_path = &debuginfo_path,
  34. .find_debuginfo = dwfl_standard_find_debuginfo,
  35. .section_address = dwfl_offline_section_address,
  36. };
  37. /*
  38. * Use the DWARF expression for the Call-frame-address and determine
  39. * if return address is in LR and if a new frame was allocated.
  40. */
  41. static int check_return_reg(int ra_regno, Dwarf_Frame *frame)
  42. {
  43. Dwarf_Op ops_mem[2];
  44. Dwarf_Op dummy;
  45. Dwarf_Op *ops = &dummy;
  46. size_t nops;
  47. int result;
  48. result = dwarf_frame_register(frame, ra_regno, ops_mem, &ops, &nops);
  49. if (result < 0) {
  50. pr_debug("dwarf_frame_register() %s\n", dwarf_errmsg(-1));
  51. return -1;
  52. }
  53. /*
  54. * Check if return address is on the stack. If return address
  55. * is in a register (typically R0), it is yet to be saved on
  56. * the stack.
  57. */
  58. if ((nops != 0 || ops != NULL) &&
  59. !(nops == 1 && ops[0].atom == DW_OP_regx &&
  60. ops[0].number2 == 0 && ops[0].offset == 0))
  61. return 0;
  62. /*
  63. * Return address is in LR. Check if a frame was allocated
  64. * but not-yet used.
  65. */
  66. result = dwarf_frame_cfa(frame, &ops, &nops);
  67. if (result < 0) {
  68. pr_debug("dwarf_frame_cfa() returns %d, %s\n", result,
  69. dwarf_errmsg(-1));
  70. return -1;
  71. }
  72. /*
  73. * If call frame address is in r1, no new frame was allocated.
  74. */
  75. if (nops == 1 && ops[0].atom == DW_OP_bregx && ops[0].number == 1 &&
  76. ops[0].number2 == 0)
  77. return 1;
  78. /*
  79. * A new frame was allocated but has not yet been used.
  80. */
  81. return 2;
  82. }
  83. /*
  84. * Get the DWARF frame from the .eh_frame section.
  85. */
  86. static Dwarf_Frame *get_eh_frame(Dwfl_Module *mod, Dwarf_Addr pc)
  87. {
  88. int result;
  89. Dwarf_Addr bias;
  90. Dwarf_CFI *cfi;
  91. Dwarf_Frame *frame;
  92. cfi = dwfl_module_eh_cfi(mod, &bias);
  93. if (!cfi) {
  94. pr_debug("%s(): no CFI - %s\n", __func__, dwfl_errmsg(-1));
  95. return NULL;
  96. }
  97. result = dwarf_cfi_addrframe(cfi, pc-bias, &frame);
  98. if (result) {
  99. pr_debug("%s(): %s\n", __func__, dwfl_errmsg(-1));
  100. return NULL;
  101. }
  102. return frame;
  103. }
  104. /*
  105. * Get the DWARF frame from the .debug_frame section.
  106. */
  107. static Dwarf_Frame *get_dwarf_frame(Dwfl_Module *mod, Dwarf_Addr pc)
  108. {
  109. Dwarf_CFI *cfi;
  110. Dwarf_Addr bias;
  111. Dwarf_Frame *frame;
  112. int result;
  113. cfi = dwfl_module_dwarf_cfi(mod, &bias);
  114. if (!cfi) {
  115. pr_debug("%s(): no CFI - %s\n", __func__, dwfl_errmsg(-1));
  116. return NULL;
  117. }
  118. result = dwarf_cfi_addrframe(cfi, pc-bias, &frame);
  119. if (result) {
  120. pr_debug("%s(): %s\n", __func__, dwfl_errmsg(-1));
  121. return NULL;
  122. }
  123. return frame;
  124. }
  125. /*
  126. * Return:
  127. * 0 if return address for the program counter @pc is on stack
  128. * 1 if return address is in LR and no new stack frame was allocated
  129. * 2 if return address is in LR and a new frame was allocated (but not
  130. * yet used)
  131. * -1 in case of errors
  132. */
  133. static int check_return_addr(struct dso *dso, u64 map_start, Dwarf_Addr pc)
  134. {
  135. int rc = -1;
  136. Dwfl *dwfl;
  137. Dwfl_Module *mod;
  138. Dwarf_Frame *frame;
  139. int ra_regno;
  140. Dwarf_Addr start = pc;
  141. Dwarf_Addr end = pc;
  142. bool signalp;
  143. const char *exec_file = dso->long_name;
  144. dwfl = dso->dwfl;
  145. if (!dwfl) {
  146. dwfl = dwfl_begin(&offline_callbacks);
  147. if (!dwfl) {
  148. pr_debug("dwfl_begin() failed: %s\n", dwarf_errmsg(-1));
  149. return -1;
  150. }
  151. mod = dwfl_report_elf(dwfl, exec_file, exec_file, -1,
  152. map_start, false);
  153. if (!mod) {
  154. pr_debug("dwfl_report_elf() failed %s\n",
  155. dwarf_errmsg(-1));
  156. /*
  157. * We normally cache the DWARF debug info and never
  158. * call dwfl_end(). But to prevent fd leak, free in
  159. * case of error.
  160. */
  161. dwfl_end(dwfl);
  162. goto out;
  163. }
  164. dso->dwfl = dwfl;
  165. }
  166. mod = dwfl_addrmodule(dwfl, pc);
  167. if (!mod) {
  168. pr_debug("dwfl_addrmodule() failed, %s\n", dwarf_errmsg(-1));
  169. goto out;
  170. }
  171. /*
  172. * To work with split debug info files (eg: glibc), check both
  173. * .eh_frame and .debug_frame sections of the ELF header.
  174. */
  175. frame = get_eh_frame(mod, pc);
  176. if (!frame) {
  177. frame = get_dwarf_frame(mod, pc);
  178. if (!frame)
  179. goto out;
  180. }
  181. ra_regno = dwarf_frame_info(frame, &start, &end, &signalp);
  182. if (ra_regno < 0) {
  183. pr_debug("Return address register unavailable: %s\n",
  184. dwarf_errmsg(-1));
  185. goto out;
  186. }
  187. rc = check_return_reg(ra_regno, frame);
  188. out:
  189. return rc;
  190. }
  191. /*
  192. * The callchain saved by the kernel always includes the link register (LR).
  193. *
  194. * 0: PERF_CONTEXT_USER
  195. * 1: Program counter (Next instruction pointer)
  196. * 2: LR value
  197. * 3: Caller's caller
  198. * 4: ...
  199. *
  200. * The value in LR is only needed when it holds a return address. If the
  201. * return address is on the stack, we should ignore the LR value.
  202. *
  203. * Further, when the return address is in the LR, if a new frame was just
  204. * allocated but the LR was not saved into it, then the LR contains the
  205. * caller, slot 4: contains the caller's caller and the contents of slot 3:
  206. * (chain->ips[3]) is undefined and must be ignored.
  207. *
  208. * Use DWARF debug information to determine if any entries need to be skipped.
  209. *
  210. * Return:
  211. * index: of callchain entry that needs to be ignored (if any)
  212. * -1 if no entry needs to be ignored or in case of errors
  213. */
  214. int arch_skip_callchain_idx(struct thread *thread, struct ip_callchain *chain)
  215. {
  216. struct addr_location al;
  217. struct dso *dso = NULL;
  218. int rc;
  219. u64 ip;
  220. u64 skip_slot = -1;
  221. if (!chain || chain->nr < 3)
  222. return skip_slot;
  223. ip = chain->ips[1];
  224. thread__find_symbol(thread, PERF_RECORD_MISC_USER, ip, &al);
  225. if (al.map)
  226. dso = al.map->dso;
  227. if (!dso) {
  228. pr_debug("%" PRIx64 " dso is NULL\n", ip);
  229. return skip_slot;
  230. }
  231. rc = check_return_addr(dso, al.map->start, ip);
  232. pr_debug("[DSO %s, sym %s, ip 0x%" PRIx64 "] rc %d\n",
  233. dso->long_name, al.sym->name, ip, rc);
  234. if (rc == 0) {
  235. /*
  236. * Return address on stack. Ignore LR value in callchain
  237. */
  238. skip_slot = 2;
  239. } else if (rc == 2) {
  240. /*
  241. * New frame allocated but return address still in LR.
  242. * Ignore the caller's caller entry in callchain.
  243. */
  244. skip_slot = 3;
  245. }
  246. return skip_slot;
  247. }