blob: 023834ef582e9fd0c837cabafeac60a6244faa7d [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Helge Deller548f1172009-02-06 21:50:39 +01002/*
3 * Stack trace management functions
4 *
Helge Delleraeb1e832021-05-04 14:49:14 +02005 * Copyright (C) 2009-2021 Helge Deller <deller@gmx.de>
Helge Deller548f1172009-02-06 21:50:39 +01006 * based on arch/x86/kernel/stacktrace.c by Ingo Molnar <mingo@redhat.com>
7 * and parisc unwind functions by Randolph Chung <tausq@debian.org>
8 *
9 * TODO: Userspace stacktrace (CONFIG_USER_STACKTRACE_SUPPORT)
10 */
Helge Dellerf0d1cfa2021-11-09 22:47:24 +010011#include <linux/kernel.h>
Helge Deller548f1172009-02-06 21:50:39 +010012#include <linux/stacktrace.h>
13
14#include <asm/unwind.h>
15
Helge Delleraeb1e832021-05-04 14:49:14 +020016static void notrace walk_stackframe(struct task_struct *task,
17 struct pt_regs *regs, bool (*fn)(void *, unsigned long), void *cookie)
Helge Deller548f1172009-02-06 21:50:39 +010018{
19 struct unwind_frame_info info;
20
Helge Deller9e0d5c42018-08-17 17:00:08 +020021 unwind_frame_init_task(&info, task, NULL);
Helge Delleraeb1e832021-05-04 14:49:14 +020022 while (1) {
Helge Deller548f1172009-02-06 21:50:39 +010023 if (unwind_once(&info) < 0 || info.ip == 0)
24 break;
25
26 if (__kernel_text_address(info.ip))
Helge Delleraeb1e832021-05-04 14:49:14 +020027 if (!fn(cookie, info.ip))
28 break;
Helge Deller548f1172009-02-06 21:50:39 +010029 }
30}
31
Helge Delleraeb1e832021-05-04 14:49:14 +020032void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
33 struct task_struct *task, struct pt_regs *regs)
Helge Deller548f1172009-02-06 21:50:39 +010034{
Helge Delleraeb1e832021-05-04 14:49:14 +020035 walk_stackframe(task, regs, consume_entry, cookie);
Helge Deller548f1172009-02-06 21:50:39 +010036}
Helge Deller548f1172009-02-06 21:50:39 +010037
Helge Delleraeb1e832021-05-04 14:49:14 +020038int arch_stack_walk_reliable(stack_trace_consume_fn consume_entry, void *cookie,
39 struct task_struct *task)
Helge Deller548f1172009-02-06 21:50:39 +010040{
Helge Delleraeb1e832021-05-04 14:49:14 +020041 walk_stackframe(task, NULL, consume_entry, cookie);
42 return 1;
Helge Deller548f1172009-02-06 21:50:39 +010043}