stacktrace.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Stack trace management functions
  4. *
  5. * Copyright (C) 2009 Helge Deller <deller@gmx.de>
  6. * 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. */
  11. #include <linux/module.h>
  12. #include <linux/stacktrace.h>
  13. #include <asm/unwind.h>
  14. static void dump_trace(struct task_struct *task, struct stack_trace *trace)
  15. {
  16. struct unwind_frame_info info;
  17. unwind_frame_init_task(&info, task, NULL);
  18. /* unwind stack and save entries in stack_trace struct */
  19. trace->nr_entries = 0;
  20. while (trace->nr_entries < trace->max_entries) {
  21. if (unwind_once(&info) < 0 || info.ip == 0)
  22. break;
  23. if (__kernel_text_address(info.ip))
  24. trace->entries[trace->nr_entries++] = info.ip;
  25. }
  26. }
  27. /*
  28. * Save stack-backtrace addresses into a stack_trace buffer.
  29. */
  30. void save_stack_trace(struct stack_trace *trace)
  31. {
  32. dump_trace(current, trace);
  33. }
  34. EXPORT_SYMBOL_GPL(save_stack_trace);
  35. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  36. {
  37. dump_trace(tsk, trace);
  38. }
  39. EXPORT_SYMBOL_GPL(save_stack_trace_tsk);