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