skip-callchain-idx.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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.
  55. */
  56. if (nops != 0 || ops != NULL)
  57. return 0;
  58. /*
  59. * Return address is in LR. Check if a frame was allocated
  60. * but not-yet used.
  61. */
  62. result = dwarf_frame_cfa(frame, &ops, &nops);
  63. if (result < 0) {
  64. pr_debug("dwarf_frame_cfa() returns %d, %s\n", result,
  65. dwarf_errmsg(-1));
  66. return -1;
  67. }
  68. /*
  69. * If call frame address is in r1, no new frame was allocated.
  70. */
  71. if (nops == 1 && ops[0].atom == DW_OP_bregx && ops[0].number == 1 &&
  72. ops[0].number2 == 0)
  73. return 1;
  74. /*
  75. * A new frame was allocated but has not yet been used.
  76. */
  77. return 2;
  78. }
  79. /*
  80. * Get the DWARF frame from the .eh_frame section.
  81. */
  82. static Dwarf_Frame *get_eh_frame(Dwfl_Module *mod, Dwarf_Addr pc)
  83. {
  84. int result;
  85. Dwarf_Addr bias;
  86. Dwarf_CFI *cfi;
  87. Dwarf_Frame *frame;
  88. cfi = dwfl_module_eh_cfi(mod, &bias);
  89. if (!cfi) {
  90. pr_debug("%s(): no CFI - %s\n", __func__, dwfl_errmsg(-1));
  91. return NULL;
  92. }
  93. result = dwarf_cfi_addrframe(cfi, pc-bias, &frame);
  94. if (result) {
  95. pr_debug("%s(): %s\n", __func__, dwfl_errmsg(-1));
  96. return NULL;
  97. }
  98. return frame;
  99. }
  100. /*
  101. * Get the DWARF frame from the .debug_frame section.
  102. */
  103. static Dwarf_Frame *get_dwarf_frame(Dwfl_Module *mod, Dwarf_Addr pc)
  104. {
  105. Dwarf_CFI *cfi;
  106. Dwarf_Addr bias;
  107. Dwarf_Frame *frame;
  108. int result;
  109. cfi = dwfl_module_dwarf_cfi(mod, &bias);
  110. if (!cfi) {
  111. pr_debug("%s(): no CFI - %s\n", __func__, dwfl_errmsg(-1));
  112. return NULL;
  113. }
  114. result = dwarf_cfi_addrframe(cfi, pc-bias, &frame);
  115. if (result) {
  116. pr_debug("%s(): %s\n", __func__, dwfl_errmsg(-1));
  117. return NULL;
  118. }
  119. return frame;
  120. }
  121. /*
  122. * Return:
  123. * 0 if return address for the program counter @pc is on stack
  124. * 1 if return address is in LR and no new stack frame was allocated
  125. * 2 if return address is in LR and a new frame was allocated (but not
  126. * yet used)
  127. * -1 in case of errors
  128. */
  129. static int check_return_addr(struct dso *dso, u64 map_start, Dwarf_Addr pc)
  130. {
  131. int rc = -1;
  132. Dwfl *dwfl;
  133. Dwfl_Module *mod;
  134. Dwarf_Frame *frame;
  135. int ra_regno;
  136. Dwarf_Addr start = pc;
  137. Dwarf_Addr end = pc;
  138. bool signalp;
  139. const char *exec_file = dso->long_name;
  140. dwfl = dso->dwfl;
  141. if (!dwfl) {
  142. dwfl = dwfl_begin(&offline_callbacks);
  143. if (!dwfl) {
  144. pr_debug("dwfl_begin() failed: %s\n", dwarf_errmsg(-1));
  145. return -1;
  146. }
  147. mod = dwfl_report_elf(dwfl, exec_file, exec_file, -1,
  148. map_start, false);
  149. if (!mod) {
  150. pr_debug("dwfl_report_elf() failed %s\n",
  151. dwarf_errmsg(-1));
  152. /*
  153. * We normally cache the DWARF debug info and never
  154. * call dwfl_end(). But to prevent fd leak, free in
  155. * case of error.
  156. */
  157. dwfl_end(dwfl);
  158. goto out;
  159. }
  160. dso->dwfl = dwfl;
  161. }
  162. mod = dwfl_addrmodule(dwfl, pc);
  163. if (!mod) {
  164. pr_debug("dwfl_addrmodule() failed, %s\n", dwarf_errmsg(-1));
  165. goto out;
  166. }
  167. /*
  168. * To work with split debug info files (eg: glibc), check both
  169. * .eh_frame and .debug_frame sections of the ELF header.
  170. */
  171. frame = get_eh_frame(mod, pc);
  172. if (!frame) {
  173. frame = get_dwarf_frame(mod, pc);
  174. if (!frame)
  175. goto out;
  176. }
  177. ra_regno = dwarf_frame_info(frame, &start, &end, &signalp);
  178. if (ra_regno < 0) {
  179. pr_debug("Return address register unavailable: %s\n",
  180. dwarf_errmsg(-1));
  181. goto out;
  182. }
  183. rc = check_return_reg(ra_regno, frame);
  184. out:
  185. return rc;
  186. }
  187. /*
  188. * The callchain saved by the kernel always includes the link register (LR).
  189. *
  190. * 0: PERF_CONTEXT_USER
  191. * 1: Program counter (Next instruction pointer)
  192. * 2: LR value
  193. * 3: Caller's caller
  194. * 4: ...
  195. *
  196. * The value in LR is only needed when it holds a return address. If the
  197. * return address is on the stack, we should ignore the LR value.
  198. *
  199. * Further, when the return address is in the LR, if a new frame was just
  200. * allocated but the LR was not saved into it, then the LR contains the
  201. * caller, slot 4: contains the caller's caller and the contents of slot 3:
  202. * (chain->ips[3]) is undefined and must be ignored.
  203. *
  204. * Use DWARF debug information to determine if any entries need to be skipped.
  205. *
  206. * Return:
  207. * index: of callchain entry that needs to be ignored (if any)
  208. * -1 if no entry needs to be ignored or in case of errors
  209. */
  210. int arch_skip_callchain_idx(struct thread *thread, struct ip_callchain *chain)
  211. {
  212. struct addr_location al;
  213. struct dso *dso = NULL;
  214. int rc;
  215. u64 ip;
  216. u64 skip_slot = -1;
  217. if (chain->nr < 3)
  218. return skip_slot;
  219. ip = chain->ips[2];
  220. thread__find_addr_location(thread, PERF_RECORD_MISC_USER,
  221. MAP__FUNCTION, ip, &al);
  222. if (al.map)
  223. dso = al.map->dso;
  224. if (!dso) {
  225. pr_debug("%" PRIx64 " dso is NULL\n", ip);
  226. return skip_slot;
  227. }
  228. rc = check_return_addr(dso, al.map->start, ip);
  229. pr_debug("[DSO %s, sym %s, ip 0x%" PRIx64 "] rc %d\n",
  230. dso->long_name, al.sym->name, ip, rc);
  231. if (rc == 0) {
  232. /*
  233. * Return address on stack. Ignore LR value in callchain
  234. */
  235. skip_slot = 2;
  236. } else if (rc == 2) {
  237. /*
  238. * New frame allocated but return address still in LR.
  239. * Ignore the caller's caller entry in callchain.
  240. */
  241. skip_slot = 3;
  242. }
  243. return skip_slot;
  244. }