stacktrace.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Copyright (C) 2013 Richard Weinberger <richard@nod.at>
  4. * Copyright (C) 2014 Google Inc., Author: Daniel Walter <dwalter@google.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/kallsyms.h>
  11. #include <linux/kernel.h>
  12. #include <linux/sched.h>
  13. #include <linux/stacktrace.h>
  14. #include <linux/module.h>
  15. #include <linux/uaccess.h>
  16. #include <asm/stacktrace.h>
  17. void dump_trace(struct task_struct *tsk,
  18. const struct stacktrace_ops *ops,
  19. void *data)
  20. {
  21. int reliable = 0;
  22. unsigned long *sp, bp, addr;
  23. struct pt_regs *segv_regs = tsk->thread.segv_regs;
  24. struct stack_frame *frame;
  25. bp = get_frame_pointer(tsk, segv_regs);
  26. sp = get_stack_pointer(tsk, segv_regs);
  27. frame = (struct stack_frame *)bp;
  28. while (((long) sp & (THREAD_SIZE-1)) != 0) {
  29. addr = *sp;
  30. if (__kernel_text_address(addr)) {
  31. reliable = 0;
  32. if ((unsigned long) sp == bp + sizeof(long)) {
  33. frame = frame ? frame->next_frame : NULL;
  34. bp = (unsigned long)frame;
  35. reliable = 1;
  36. }
  37. ops->address(data, addr, reliable);
  38. }
  39. sp++;
  40. }
  41. }
  42. static void save_addr(void *data, unsigned long address, int reliable)
  43. {
  44. struct stack_trace *trace = data;
  45. if (!reliable)
  46. return;
  47. if (trace->nr_entries >= trace->max_entries)
  48. return;
  49. trace->entries[trace->nr_entries++] = address;
  50. }
  51. static const struct stacktrace_ops dump_ops = {
  52. .address = save_addr
  53. };
  54. static void __save_stack_trace(struct task_struct *tsk, struct stack_trace *trace)
  55. {
  56. dump_trace(tsk, &dump_ops, trace);
  57. if (trace->nr_entries < trace->max_entries)
  58. trace->entries[trace->nr_entries++] = ULONG_MAX;
  59. }
  60. void save_stack_trace(struct stack_trace *trace)
  61. {
  62. __save_stack_trace(current, trace);
  63. }
  64. EXPORT_SYMBOL_GPL(save_stack_trace);
  65. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  66. {
  67. __save_stack_trace(tsk, trace);
  68. }
  69. EXPORT_SYMBOL_GPL(save_stack_trace_tsk);