stacktrace.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __LINUX_STACKTRACE_H
  3. #define __LINUX_STACKTRACE_H
  4. #include <linux/types.h>
  5. #include <asm/errno.h>
  6. struct task_struct;
  7. struct pt_regs;
  8. #ifdef CONFIG_ARCH_STACKWALK
  9. /**
  10. * stack_trace_consume_fn - Callback for arch_stack_walk()
  11. * @cookie: Caller supplied pointer handed back by arch_stack_walk()
  12. * @addr: The stack entry address to consume
  13. *
  14. * Return: True, if the entry was consumed or skipped
  15. * False, if there is no space left to store
  16. */
  17. typedef bool (*stack_trace_consume_fn)(void *cookie, unsigned long addr);
  18. /**
  19. * arch_stack_walk - Architecture specific function to walk the stack
  20. * @consume_entry: Callback which is invoked by the architecture code for
  21. * each entry.
  22. * @cookie: Caller supplied pointer which is handed back to
  23. * @consume_entry
  24. * @task: Pointer to a task struct, can be NULL
  25. * @regs: Pointer to registers, can be NULL
  26. *
  27. * ============ ======= ============================================
  28. * task regs
  29. * ============ ======= ============================================
  30. * task NULL Stack trace from task (can be current)
  31. * current regs Stack trace starting on regs->stackpointer
  32. * ============ ======= ============================================
  33. */
  34. void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
  35. struct task_struct *task, struct pt_regs *regs);
  36. /**
  37. * arch_stack_walk_reliable - Architecture specific function to walk the
  38. * stack reliably
  39. *
  40. * @consume_entry: Callback which is invoked by the architecture code for
  41. * each entry.
  42. * @cookie: Caller supplied pointer which is handed back to
  43. * @consume_entry
  44. * @task: Pointer to a task struct, can be NULL
  45. *
  46. * This function returns an error if it detects any unreliable
  47. * features of the stack. Otherwise it guarantees that the stack
  48. * trace is reliable.
  49. *
  50. * If the task is not 'current', the caller *must* ensure the task is
  51. * inactive and its stack is pinned.
  52. */
  53. int arch_stack_walk_reliable(stack_trace_consume_fn consume_entry, void *cookie,
  54. struct task_struct *task);
  55. void arch_stack_walk_user(stack_trace_consume_fn consume_entry, void *cookie,
  56. const struct pt_regs *regs);
  57. #endif /* CONFIG_ARCH_STACKWALK */
  58. #ifdef CONFIG_STACKTRACE
  59. void stack_trace_print(const unsigned long *trace, unsigned int nr_entries,
  60. int spaces);
  61. int stack_trace_snprint(char *buf, size_t size, const unsigned long *entries,
  62. unsigned int nr_entries, int spaces);
  63. unsigned int stack_trace_save(unsigned long *store, unsigned int size,
  64. unsigned int skipnr);
  65. unsigned int stack_trace_save_tsk(struct task_struct *task,
  66. unsigned long *store, unsigned int size,
  67. unsigned int skipnr);
  68. unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
  69. unsigned int size, unsigned int skipnr);
  70. unsigned int stack_trace_save_user(unsigned long *store, unsigned int size);
  71. unsigned int filter_irq_stacks(unsigned long *entries, unsigned int nr_entries);
  72. #ifndef CONFIG_ARCH_STACKWALK
  73. /* Internal interfaces. Do not use in generic code */
  74. struct stack_trace {
  75. unsigned int nr_entries, max_entries;
  76. unsigned long *entries;
  77. unsigned int skip; /* input argument: How many entries to skip */
  78. };
  79. extern void save_stack_trace(struct stack_trace *trace);
  80. extern void save_stack_trace_regs(struct pt_regs *regs,
  81. struct stack_trace *trace);
  82. extern void save_stack_trace_tsk(struct task_struct *tsk,
  83. struct stack_trace *trace);
  84. extern int save_stack_trace_tsk_reliable(struct task_struct *tsk,
  85. struct stack_trace *trace);
  86. extern void save_stack_trace_user(struct stack_trace *trace);
  87. #endif /* !CONFIG_ARCH_STACKWALK */
  88. #endif /* CONFIG_STACKTRACE */
  89. #if defined(CONFIG_STACKTRACE) && defined(CONFIG_HAVE_RELIABLE_STACKTRACE)
  90. int stack_trace_save_tsk_reliable(struct task_struct *tsk, unsigned long *store,
  91. unsigned int size);
  92. #else
  93. static inline int stack_trace_save_tsk_reliable(struct task_struct *tsk,
  94. unsigned long *store,
  95. unsigned int size)
  96. {
  97. return -ENOSYS;
  98. }
  99. #endif
  100. #endif /* __LINUX_STACKTRACE_H */