linux/tools/perf/tests/dwarf-unwind.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2#include <linux/compiler.h>
   3#include <linux/types.h>
   4#include <linux/zalloc.h>
   5#include <inttypes.h>
   6#include <unistd.h>
   7#include "tests.h"
   8#include "debug.h"
   9#include "machine.h"
  10#include "event.h"
  11#include "../util/unwind.h"
  12#include "perf_regs.h"
  13#include "map.h"
  14#include "symbol.h"
  15#include "thread.h"
  16#include "callchain.h"
  17
  18#if defined (__x86_64__) || defined (__i386__) || defined (__powerpc__)
  19#include "arch-tests.h"
  20#endif
  21
  22/* For bsearch. We try to unwind functions in shared object. */
  23#include <stdlib.h>
  24
  25static int mmap_handler(struct perf_tool *tool __maybe_unused,
  26                        union perf_event *event,
  27                        struct perf_sample *sample,
  28                        struct machine *machine)
  29{
  30        return machine__process_mmap2_event(machine, event, sample);
  31}
  32
  33static int init_live_machine(struct machine *machine)
  34{
  35        union perf_event event;
  36        pid_t pid = getpid();
  37
  38        return perf_event__synthesize_mmap_events(NULL, &event, pid, pid,
  39                                                  mmap_handler, machine, true);
  40}
  41
  42/*
  43 * We need to keep these functions global, despite the
  44 * fact that they are used only locally in this object,
  45 * in order to keep them around even if the binary is
  46 * stripped. If they are gone, the unwind check for
  47 * symbol fails.
  48 */
  49int test_dwarf_unwind__thread(struct thread *thread);
  50int test_dwarf_unwind__compare(void *p1, void *p2);
  51int test_dwarf_unwind__krava_3(struct thread *thread);
  52int test_dwarf_unwind__krava_2(struct thread *thread);
  53int test_dwarf_unwind__krava_1(struct thread *thread);
  54
  55#define MAX_STACK 8
  56
  57static int unwind_entry(struct unwind_entry *entry, void *arg)
  58{
  59        unsigned long *cnt = (unsigned long *) arg;
  60        char *symbol = entry->sym ? entry->sym->name : NULL;
  61        static const char *funcs[MAX_STACK] = {
  62                "test__arch_unwind_sample",
  63                "test_dwarf_unwind__thread",
  64                "test_dwarf_unwind__compare",
  65                "bsearch",
  66                "test_dwarf_unwind__krava_3",
  67                "test_dwarf_unwind__krava_2",
  68                "test_dwarf_unwind__krava_1",
  69                "test__dwarf_unwind"
  70        };
  71        /*
  72         * The funcs[MAX_STACK] array index, based on the
  73         * callchain order setup.
  74         */
  75        int idx = callchain_param.order == ORDER_CALLER ?
  76                  MAX_STACK - *cnt - 1 : *cnt;
  77
  78        if (*cnt >= MAX_STACK) {
  79                pr_debug("failed: crossed the max stack value %d\n", MAX_STACK);
  80                return -1;
  81        }
  82
  83        if (!symbol) {
  84                pr_debug("failed: got unresolved address 0x%" PRIx64 "\n",
  85                         entry->ip);
  86                return -1;
  87        }
  88
  89        (*cnt)++;
  90        pr_debug("got: %s 0x%" PRIx64 ", expecting %s\n",
  91                 symbol, entry->ip, funcs[idx]);
  92        return strcmp((const char *) symbol, funcs[idx]);
  93}
  94
  95noinline int test_dwarf_unwind__thread(struct thread *thread)
  96{
  97        struct perf_sample sample;
  98        unsigned long cnt = 0;
  99        int err = -1;
 100
 101        memset(&sample, 0, sizeof(sample));
 102
 103        if (test__arch_unwind_sample(&sample, thread)) {
 104                pr_debug("failed to get unwind sample\n");
 105                goto out;
 106        }
 107
 108        err = unwind__get_entries(unwind_entry, &cnt, thread,
 109                                  &sample, MAX_STACK);
 110        if (err)
 111                pr_debug("unwind failed\n");
 112        else if (cnt != MAX_STACK) {
 113                pr_debug("got wrong number of stack entries %lu != %d\n",
 114                         cnt, MAX_STACK);
 115                err = -1;
 116        }
 117
 118 out:
 119        zfree(&sample.user_stack.data);
 120        zfree(&sample.user_regs.regs);
 121        return err;
 122}
 123
 124static int global_unwind_retval = -INT_MAX;
 125
 126noinline int test_dwarf_unwind__compare(void *p1, void *p2)
 127{
 128        /* Any possible value should be 'thread' */
 129        struct thread *thread = *(struct thread **)p1;
 130
 131        if (global_unwind_retval == -INT_MAX) {
 132                /* Call unwinder twice for both callchain orders. */
 133                callchain_param.order = ORDER_CALLER;
 134
 135                global_unwind_retval = test_dwarf_unwind__thread(thread);
 136                if (!global_unwind_retval) {
 137                        callchain_param.order = ORDER_CALLEE;
 138                        global_unwind_retval = test_dwarf_unwind__thread(thread);
 139                }
 140        }
 141
 142        return p1 - p2;
 143}
 144
 145noinline int test_dwarf_unwind__krava_3(struct thread *thread)
 146{
 147        struct thread *array[2] = {thread, thread};
 148        void *fp = &bsearch;
 149        /*
 150         * make _bsearch a volatile function pointer to
 151         * prevent potential optimization, which may expand
 152         * bsearch and call compare directly from this function,
 153         * instead of libc shared object.
 154         */
 155        void *(*volatile _bsearch)(void *, void *, size_t,
 156                        size_t, int (*)(void *, void *));
 157
 158        _bsearch = fp;
 159        _bsearch(array, &thread, 2, sizeof(struct thread **),
 160                 test_dwarf_unwind__compare);
 161        return global_unwind_retval;
 162}
 163
 164noinline int test_dwarf_unwind__krava_2(struct thread *thread)
 165{
 166        return test_dwarf_unwind__krava_3(thread);
 167}
 168
 169noinline int test_dwarf_unwind__krava_1(struct thread *thread)
 170{
 171        return test_dwarf_unwind__krava_2(thread);
 172}
 173
 174int test__dwarf_unwind(struct test *test __maybe_unused, int subtest __maybe_unused)
 175{
 176        struct machine *machine;
 177        struct thread *thread;
 178        int err = -1;
 179
 180        machine = machine__new_host();
 181        if (!machine) {
 182                pr_err("Could not get machine\n");
 183                return -1;
 184        }
 185
 186        if (machine__create_kernel_maps(machine)) {
 187                pr_err("Failed to create kernel maps\n");
 188                return -1;
 189        }
 190
 191        callchain_param.record_mode = CALLCHAIN_DWARF;
 192        dwarf_callchain_users = true;
 193
 194        if (init_live_machine(machine)) {
 195                pr_err("Could not init machine\n");
 196                goto out;
 197        }
 198
 199        if (verbose > 1)
 200                machine__fprintf(machine, stderr);
 201
 202        thread = machine__find_thread(machine, getpid(), getpid());
 203        if (!thread) {
 204                pr_err("Could not get thread\n");
 205                goto out;
 206        }
 207
 208        err = test_dwarf_unwind__krava_1(thread);
 209        thread__put(thread);
 210
 211 out:
 212        machine__delete_threads(machine);
 213        machine__delete(machine);
 214        return err;
 215}
 216