stacktrace.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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_tsk or
  48. * save_stack_trace_regs get this weak alias and a once-per-bootup warning
  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. }