stacktrace.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Stack trace management functions
  4. *
  5. * Copyright IBM Corp. 2006
  6. * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
  7. */
  8. #include <linux/sched.h>
  9. #include <linux/sched/debug.h>
  10. #include <linux/stacktrace.h>
  11. #include <linux/kallsyms.h>
  12. #include <linux/export.h>
  13. static int __save_address(void *data, unsigned long address, int nosched)
  14. {
  15. struct stack_trace *trace = data;
  16. if (nosched && in_sched_functions(address))
  17. return 0;
  18. if (trace->skip > 0) {
  19. trace->skip--;
  20. return 0;
  21. }
  22. if (trace->nr_entries < trace->max_entries) {
  23. trace->entries[trace->nr_entries++] = address;
  24. return 0;
  25. }
  26. return 1;
  27. }
  28. static int save_address(void *data, unsigned long address, int reliable)
  29. {
  30. return __save_address(data, address, 0);
  31. }
  32. static int save_address_nosched(void *data, unsigned long address, int reliable)
  33. {
  34. return __save_address(data, address, 1);
  35. }
  36. void save_stack_trace(struct stack_trace *trace)
  37. {
  38. unsigned long sp;
  39. sp = current_stack_pointer();
  40. dump_trace(save_address, trace, NULL, sp);
  41. if (trace->nr_entries < trace->max_entries)
  42. trace->entries[trace->nr_entries++] = ULONG_MAX;
  43. }
  44. EXPORT_SYMBOL_GPL(save_stack_trace);
  45. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  46. {
  47. unsigned long sp;
  48. sp = tsk->thread.ksp;
  49. if (tsk == current)
  50. sp = current_stack_pointer();
  51. dump_trace(save_address_nosched, trace, tsk, sp);
  52. if (trace->nr_entries < trace->max_entries)
  53. trace->entries[trace->nr_entries++] = ULONG_MAX;
  54. }
  55. EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
  56. void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
  57. {
  58. unsigned long sp;
  59. sp = kernel_stack_pointer(regs);
  60. dump_trace(save_address, trace, NULL, sp);
  61. if (trace->nr_entries < trace->max_entries)
  62. trace->entries[trace->nr_entries++] = ULONG_MAX;
  63. }
  64. EXPORT_SYMBOL_GPL(save_stack_trace_regs);