1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2970e51feSDaniel Walter /* 3970e51feSDaniel Walter * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) 4970e51feSDaniel Walter * Copyright (C) 2013 Richard Weinberger <richard@nod.at> 5970e51feSDaniel Walter * Copyright (C) 2014 Google Inc., Author: Daniel Walter <dwalter@google.com> 6970e51feSDaniel Walter */ 7970e51feSDaniel Walter 8970e51feSDaniel Walter #include <linux/kallsyms.h> 9970e51feSDaniel Walter #include <linux/kernel.h> 10970e51feSDaniel Walter #include <linux/sched.h> 11970e51feSDaniel Walter #include <linux/stacktrace.h> 12970e51feSDaniel Walter #include <linux/module.h> 13970e51feSDaniel Walter #include <linux/uaccess.h> 14970e51feSDaniel Walter #include <asm/stacktrace.h> 15970e51feSDaniel Walter 16970e51feSDaniel Walter void dump_trace(struct task_struct *tsk, 17970e51feSDaniel Walter const struct stacktrace_ops *ops, 18970e51feSDaniel Walter void *data) 19970e51feSDaniel Walter { 20970e51feSDaniel Walter int reliable = 0; 21970e51feSDaniel Walter unsigned long *sp, bp, addr; 22970e51feSDaniel Walter struct pt_regs *segv_regs = tsk->thread.segv_regs; 23970e51feSDaniel Walter struct stack_frame *frame; 24970e51feSDaniel Walter 25970e51feSDaniel Walter bp = get_frame_pointer(tsk, segv_regs); 26970e51feSDaniel Walter sp = get_stack_pointer(tsk, segv_regs); 27970e51feSDaniel Walter 28970e51feSDaniel Walter frame = (struct stack_frame *)bp; 29970e51feSDaniel Walter while (((long) sp & (THREAD_SIZE-1)) != 0) { 30970e51feSDaniel Walter addr = *sp; 31970e51feSDaniel Walter if (__kernel_text_address(addr)) { 32970e51feSDaniel Walter reliable = 0; 33970e51feSDaniel Walter if ((unsigned long) sp == bp + sizeof(long)) { 34970e51feSDaniel Walter frame = frame ? frame->next_frame : NULL; 35970e51feSDaniel Walter bp = (unsigned long)frame; 36970e51feSDaniel Walter reliable = 1; 37970e51feSDaniel Walter } 38970e51feSDaniel Walter ops->address(data, addr, reliable); 39970e51feSDaniel Walter } 40970e51feSDaniel Walter sp++; 41970e51feSDaniel Walter } 42970e51feSDaniel Walter } 43970e51feSDaniel Walter 44970e51feSDaniel Walter static void save_addr(void *data, unsigned long address, int reliable) 45970e51feSDaniel Walter { 46970e51feSDaniel Walter struct stack_trace *trace = data; 47970e51feSDaniel Walter 48970e51feSDaniel Walter if (!reliable) 49970e51feSDaniel Walter return; 50970e51feSDaniel Walter if (trace->nr_entries >= trace->max_entries) 51970e51feSDaniel Walter return; 52970e51feSDaniel Walter 53970e51feSDaniel Walter trace->entries[trace->nr_entries++] = address; 54970e51feSDaniel Walter } 55970e51feSDaniel Walter 56970e51feSDaniel Walter static const struct stacktrace_ops dump_ops = { 57970e51feSDaniel Walter .address = save_addr 58970e51feSDaniel Walter }; 59970e51feSDaniel Walter 60970e51feSDaniel Walter static void __save_stack_trace(struct task_struct *tsk, struct stack_trace *trace) 61970e51feSDaniel Walter { 62970e51feSDaniel Walter dump_trace(tsk, &dump_ops, trace); 63970e51feSDaniel Walter } 64970e51feSDaniel Walter 65970e51feSDaniel Walter void save_stack_trace(struct stack_trace *trace) 66970e51feSDaniel Walter { 67970e51feSDaniel Walter __save_stack_trace(current, trace); 68970e51feSDaniel Walter } 69970e51feSDaniel Walter EXPORT_SYMBOL_GPL(save_stack_trace); 70970e51feSDaniel Walter 71970e51feSDaniel Walter void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace) 72970e51feSDaniel Walter { 73970e51feSDaniel Walter __save_stack_trace(tsk, trace); 74970e51feSDaniel Walter } 75970e51feSDaniel Walter EXPORT_SYMBOL_GPL(save_stack_trace_tsk); 76