stacktrace.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Stack trace management functions
  4. *
  5. * Copyright IBM Corp. 2006
  6. */
  7. #include <linux/stacktrace.h>
  8. #include <asm/stacktrace.h>
  9. #include <asm/unwind.h>
  10. #include <asm/kprobes.h>
  11. void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
  12. struct task_struct *task, struct pt_regs *regs)
  13. {
  14. struct unwind_state state;
  15. unsigned long addr;
  16. unwind_for_each_frame(&state, task, regs, 0) {
  17. addr = unwind_get_return_address(&state);
  18. if (!addr || !consume_entry(cookie, addr))
  19. break;
  20. }
  21. }
  22. int arch_stack_walk_reliable(stack_trace_consume_fn consume_entry,
  23. void *cookie, struct task_struct *task)
  24. {
  25. struct unwind_state state;
  26. unsigned long addr;
  27. unwind_for_each_frame(&state, task, NULL, 0) {
  28. if (state.stack_info.type != STACK_TYPE_TASK)
  29. return -EINVAL;
  30. if (state.regs)
  31. return -EINVAL;
  32. addr = unwind_get_return_address(&state);
  33. if (!addr)
  34. return -EINVAL;
  35. #ifdef CONFIG_KPROBES
  36. /*
  37. * Mark stacktraces with kretprobed functions on them
  38. * as unreliable.
  39. */
  40. if (state.ip == (unsigned long)__kretprobe_trampoline)
  41. return -EINVAL;
  42. #endif
  43. if (!consume_entry(cookie, addr))
  44. return -EINVAL;
  45. }
  46. /* Check for stack corruption */
  47. if (unwind_error(&state))
  48. return -EINVAL;
  49. return 0;
  50. }