stacktrace.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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", 1 + spaces, ' ');
  20. print_ip_sym(trace->entries[i]);
  21. }
  22. }
  23. EXPORT_SYMBOL_GPL(print_stack_trace);
  24. int snprint_stack_trace(char *buf, size_t size,
  25. struct stack_trace *trace, int spaces)
  26. {
  27. int i;
  28. unsigned long ip;
  29. int generated;
  30. int total = 0;
  31. if (WARN_ON(!trace->entries))
  32. return 0;
  33. for (i = 0; i < trace->nr_entries; i++) {
  34. ip = trace->entries[i];
  35. generated = snprintf(buf, size, "%*c[<%p>] %pS\n",
  36. 1 + spaces, ' ', (void *) ip, (void *) ip);
  37. total += generated;
  38. /* Assume that generated isn't a negative number */
  39. if (generated >= size) {
  40. buf += size;
  41. size = 0;
  42. } else {
  43. buf += generated;
  44. size -= generated;
  45. }
  46. }
  47. return total;
  48. }
  49. EXPORT_SYMBOL_GPL(snprint_stack_trace);
  50. /*
  51. * Architectures that do not implement save_stack_trace_tsk or
  52. * save_stack_trace_regs get this weak alias and a once-per-bootup warning
  53. * (whenever this facility is utilized - for example by procfs):
  54. */
  55. __weak void
  56. save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  57. {
  58. WARN_ONCE(1, KERN_INFO "save_stack_trace_tsk() not implemented yet.\n");
  59. }
  60. __weak void
  61. save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
  62. {
  63. WARN_ONCE(1, KERN_INFO "save_stack_trace_regs() not implemented yet.\n");
  64. }