linux/arch/x86/oprofile/backtrace.c
<<
>>
Prefs
   1/**
   2 * @file backtrace.c
   3 *
   4 * @remark Copyright 2002 OProfile authors
   5 * @remark Read the file COPYING
   6 *
   7 * @author John Levon
   8 * @author David Smith
   9 */
  10
  11#include <linux/oprofile.h>
  12#include <linux/sched.h>
  13#include <linux/mm.h>
  14#include <linux/compat.h>
  15#include <linux/uaccess.h>
  16
  17#include <asm/ptrace.h>
  18#include <asm/stacktrace.h>
  19#include <asm/unwind.h>
  20
  21#ifdef CONFIG_COMPAT
  22static struct stack_frame_ia32 *
  23dump_user_backtrace_32(struct stack_frame_ia32 *head)
  24{
  25        /* Also check accessibility of one struct frame_head beyond: */
  26        struct stack_frame_ia32 bufhead[2];
  27        struct stack_frame_ia32 *fp;
  28        unsigned long bytes;
  29
  30        bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead));
  31        if (bytes != 0)
  32                return NULL;
  33
  34        fp = (struct stack_frame_ia32 *) compat_ptr(bufhead[0].next_frame);
  35
  36        oprofile_add_trace(bufhead[0].return_address);
  37
  38        /* frame pointers should strictly progress back up the stack
  39        * (towards higher addresses) */
  40        if (head >= fp)
  41                return NULL;
  42
  43        return fp;
  44}
  45
  46static inline int
  47x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
  48{
  49        struct stack_frame_ia32 *head;
  50
  51        /* User process is IA32 */
  52        if (!current || !test_thread_flag(TIF_IA32))
  53                return 0;
  54
  55        head = (struct stack_frame_ia32 *) regs->bp;
  56        while (depth-- && head)
  57                head = dump_user_backtrace_32(head);
  58
  59        return 1;
  60}
  61
  62#else
  63static inline int
  64x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
  65{
  66        return 0;
  67}
  68#endif /* CONFIG_COMPAT */
  69
  70static struct stack_frame *dump_user_backtrace(struct stack_frame *head)
  71{
  72        /* Also check accessibility of one struct frame_head beyond: */
  73        struct stack_frame bufhead[2];
  74        unsigned long bytes;
  75
  76        bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead));
  77        if (bytes != 0)
  78                return NULL;
  79
  80        oprofile_add_trace(bufhead[0].return_address);
  81
  82        /* frame pointers should strictly progress back up the stack
  83         * (towards higher addresses) */
  84        if (head >= bufhead[0].next_frame)
  85                return NULL;
  86
  87        return bufhead[0].next_frame;
  88}
  89
  90void
  91x86_backtrace(struct pt_regs * const regs, unsigned int depth)
  92{
  93        struct stack_frame *head = (struct stack_frame *)frame_pointer(regs);
  94
  95        if (!user_mode(regs)) {
  96                struct unwind_state state;
  97                unsigned long addr;
  98
  99                if (!depth)
 100                        return;
 101
 102                oprofile_add_trace(regs->ip);
 103
 104                if (!--depth)
 105                        return;
 106
 107                for (unwind_start(&state, current, regs, NULL);
 108                     !unwind_done(&state); unwind_next_frame(&state)) {
 109                        addr = unwind_get_return_address(&state);
 110                        if (!addr)
 111                                break;
 112
 113                        oprofile_add_trace(addr);
 114
 115                        if (!--depth)
 116                                break;
 117                }
 118
 119                return;
 120        }
 121
 122        if (x86_backtrace_32(regs, depth))
 123                return;
 124
 125        while (depth-- && head)
 126                head = dump_user_backtrace(head);
 127}
 128