skip-callchain-idx.c 6.4 KB

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