stacktrace.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * kernel/stacktrace.c
  3. *
  4. * Stack trace management functions
  5. *
  6. * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  7. */
  8. #include <linux/sched.h>
  9. #include <linux/kernel.h>
  10. #include <linux/export.h>
  11. #include <linux/kallsyms.h>
  12. #include <linux/stacktrace.h>
  13. void print_stack_trace(struct stack_trace *trace, int spaces)
  14. {
  15. int i;
  16. if (WARN_ON(!trace->entries))
  17. return;
  18. for (i = 0; i < trace->nr_entries; i++)
  19. printk("%*c%pS\n", 1 + spaces, ' ', (void *)trace->entries[i]);
  20. }
  21. EXPORT_SYMBOL_GPL(print_stack_trace);
  22. int snprint_stack_trace(char *buf, size_t size,
  23. struct stack_trace *trace, int spaces)
  24. {
  25. int i;
  26. int generated;
  27. int total = 0;
  28. if (WARN_ON(!trace->entries))
  29. return 0;
  30. for (i = 0; i < trace->nr_entries; i++) {
  31. generated = snprintf(buf, size, "%*c%pS\n", 1 + spaces, ' ',
  32. (void *)trace->entries[i]);
  33. total += generated;
  34. /* Assume that generated isn't a negative number */
  35. if (generated >= size) {
  36. buf += size;
  37. size = 0;
  38. } else {
  39. buf += generated;
  40. size -= generated;
  41. }
  42. }
  43. return total;
  44. }
  45. EXPORT_SYMBOL_GPL(snprint_stack_trace);
  46. /*
  47. * Architectures that do not implement save_stack_trace_*()
  48. * get these weak aliases and once-per-bootup warnings
  49. * (whenever this facility is utilized - for example by procfs):
  50. */
  51. __weak void
  52. save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  53. {
  54. WARN_ONCE(1, KERN_INFO "save_stack_trace_tsk() not implemented yet.\n");
  55. }
  56. __weak void
  57. save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
  58. {
  59. WARN_ONCE(1, KERN_INFO "save_stack_trace_regs() not implemented yet.\n");
  60. }
  61. __weak int
  62. save_stack_trace_tsk_reliable(struct task_struct *tsk,
  63. struct stack_trace *trace)
  64. {
  65. WARN_ONCE(1, KERN_INFO "save_stack_tsk_reliable() not implemented yet.\n");
  66. return -ENOSYS;
  67. }