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