trace_stack.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
  3. *
  4. */
  5. #include <linux/stacktrace.h>
  6. #include <linux/kallsyms.h>
  7. #include <linux/seq_file.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/ftrace.h>
  11. #include <linux/module.h>
  12. #include <linux/sysctl.h>
  13. #include <linux/init.h>
  14. #include <asm/setup.h>
  15. #include "trace.h"
  16. static unsigned long stack_dump_trace[STACK_TRACE_ENTRIES+1] =
  17. { [0 ... (STACK_TRACE_ENTRIES)] = ULONG_MAX };
  18. unsigned stack_trace_index[STACK_TRACE_ENTRIES];
  19. /*
  20. * Reserve one entry for the passed in ip. This will allow
  21. * us to remove most or all of the stack size overhead
  22. * added by the stack tracer itself.
  23. */
  24. struct stack_trace stack_trace_max = {
  25. .max_entries = STACK_TRACE_ENTRIES - 1,
  26. .entries = &stack_dump_trace[0],
  27. };
  28. unsigned long stack_trace_max_size;
  29. arch_spinlock_t stack_trace_max_lock =
  30. (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  31. static DEFINE_PER_CPU(int, trace_active);
  32. static DEFINE_MUTEX(stack_sysctl_mutex);
  33. int stack_tracer_enabled;
  34. static int last_stack_tracer_enabled;
  35. void stack_trace_print(void)
  36. {
  37. long i;
  38. int size;
  39. pr_emerg(" Depth Size Location (%d entries)\n"
  40. " ----- ---- --------\n",
  41. stack_trace_max.nr_entries);
  42. for (i = 0; i < stack_trace_max.nr_entries; i++) {
  43. if (stack_dump_trace[i] == ULONG_MAX)
  44. break;
  45. if (i+1 == stack_trace_max.nr_entries ||
  46. stack_dump_trace[i+1] == ULONG_MAX)
  47. size = stack_trace_index[i];
  48. else
  49. size = stack_trace_index[i] - stack_trace_index[i+1];
  50. pr_emerg("%3ld) %8d %5d %pS\n", i, stack_trace_index[i],
  51. size, (void *)stack_dump_trace[i]);
  52. }
  53. }
  54. /*
  55. * When arch-specific code overides this function, the following
  56. * data should be filled up, assuming stack_trace_max_lock is held to
  57. * prevent concurrent updates.
  58. * stack_trace_index[]
  59. * stack_trace_max
  60. * stack_trace_max_size
  61. */
  62. void __weak
  63. check_stack(unsigned long ip, unsigned long *stack)
  64. {
  65. unsigned long this_size, flags; unsigned long *p, *top, *start;
  66. static int tracer_frame;
  67. int frame_size = ACCESS_ONCE(tracer_frame);
  68. int i, x;
  69. this_size = ((unsigned long)stack) & (THREAD_SIZE-1);
  70. this_size = THREAD_SIZE - this_size;
  71. /* Remove the frame of the tracer */
  72. this_size -= frame_size;
  73. if (this_size <= stack_trace_max_size)
  74. return;
  75. /* we do not handle interrupt stacks yet */
  76. if (!object_is_on_stack(stack))
  77. return;
  78. /* Can't do this from NMI context (can cause deadlocks) */
  79. if (in_nmi())
  80. return;
  81. local_irq_save(flags);
  82. arch_spin_lock(&stack_trace_max_lock);
  83. /*
  84. * RCU may not be watching, make it see us.
  85. * The stack trace code uses rcu_sched.
  86. */
  87. rcu_irq_enter();
  88. /* In case another CPU set the tracer_frame on us */
  89. if (unlikely(!frame_size))
  90. this_size -= tracer_frame;
  91. /* a race could have already updated it */
  92. if (this_size <= stack_trace_max_size)
  93. goto out;
  94. stack_trace_max_size = this_size;
  95. stack_trace_max.nr_entries = 0;
  96. stack_trace_max.skip = 3;
  97. save_stack_trace(&stack_trace_max);
  98. /* Skip over the overhead of the stack tracer itself */
  99. for (i = 0; i < stack_trace_max.nr_entries; i++) {
  100. if (stack_dump_trace[i] == ip)
  101. break;
  102. }
  103. /*
  104. * Now find where in the stack these are.
  105. */
  106. x = 0;
  107. start = stack;
  108. top = (unsigned long *)
  109. (((unsigned long)start & ~(THREAD_SIZE-1)) + THREAD_SIZE);
  110. /*
  111. * Loop through all the entries. One of the entries may
  112. * for some reason be missed on the stack, so we may
  113. * have to account for them. If they are all there, this
  114. * loop will only happen once. This code only takes place
  115. * on a new max, so it is far from a fast path.
  116. */
  117. while (i < stack_trace_max.nr_entries) {
  118. int found = 0;
  119. stack_trace_index[x] = this_size;
  120. p = start;
  121. for (; p < top && i < stack_trace_max.nr_entries; p++) {
  122. if (stack_dump_trace[i] == ULONG_MAX)
  123. break;
  124. if (*p == stack_dump_trace[i]) {
  125. stack_dump_trace[x] = stack_dump_trace[i++];
  126. this_size = stack_trace_index[x++] =
  127. (top - p) * sizeof(unsigned long);
  128. found = 1;
  129. /* Start the search from here */
  130. start = p + 1;
  131. /*
  132. * We do not want to show the overhead
  133. * of the stack tracer stack in the
  134. * max stack. If we haven't figured
  135. * out what that is, then figure it out
  136. * now.
  137. */
  138. if (unlikely(!tracer_frame)) {
  139. tracer_frame = (p - stack) *
  140. sizeof(unsigned long);
  141. stack_trace_max_size -= tracer_frame;
  142. }
  143. }
  144. }
  145. if (!found)
  146. i++;
  147. }
  148. stack_trace_max.nr_entries = x;
  149. for (; x < i; x++)
  150. stack_dump_trace[x] = ULONG_MAX;
  151. if (task_stack_end_corrupted(current)) {
  152. stack_trace_print();
  153. BUG();
  154. }
  155. out:
  156. rcu_irq_exit();
  157. arch_spin_unlock(&stack_trace_max_lock);
  158. local_irq_restore(flags);
  159. }
  160. static void
  161. stack_trace_call(unsigned long ip, unsigned long parent_ip,
  162. struct ftrace_ops *op, struct pt_regs *pt_regs)
  163. {
  164. unsigned long stack;
  165. int cpu;
  166. preempt_disable_notrace();
  167. cpu = raw_smp_processor_id();
  168. /* no atomic needed, we only modify this variable by this cpu */
  169. if (per_cpu(trace_active, cpu)++ != 0)
  170. goto out;
  171. ip += MCOUNT_INSN_SIZE;
  172. check_stack(ip, &stack);
  173. out:
  174. per_cpu(trace_active, cpu)--;
  175. /* prevent recursion in schedule */
  176. preempt_enable_notrace();
  177. }
  178. static struct ftrace_ops trace_ops __read_mostly =
  179. {
  180. .func = stack_trace_call,
  181. .flags = FTRACE_OPS_FL_RECURSION_SAFE,
  182. };
  183. static ssize_t
  184. stack_max_size_read(struct file *filp, char __user *ubuf,
  185. size_t count, loff_t *ppos)
  186. {
  187. unsigned long *ptr = filp->private_data;
  188. char buf[64];
  189. int r;
  190. r = snprintf(buf, sizeof(buf), "%ld\n", *ptr);
  191. if (r > sizeof(buf))
  192. r = sizeof(buf);
  193. return simple_read_from_buffer(ubuf, count, ppos, buf, r);
  194. }
  195. static ssize_t
  196. stack_max_size_write(struct file *filp, const char __user *ubuf,
  197. size_t count, loff_t *ppos)
  198. {
  199. long *ptr = filp->private_data;
  200. unsigned long val, flags;
  201. int ret;
  202. int cpu;
  203. ret = kstrtoul_from_user(ubuf, count, 10, &val);
  204. if (ret)
  205. return ret;
  206. local_irq_save(flags);
  207. /*
  208. * In case we trace inside arch_spin_lock() or after (NMI),
  209. * we will cause circular lock, so we also need to increase
  210. * the percpu trace_active here.
  211. */
  212. cpu = smp_processor_id();
  213. per_cpu(trace_active, cpu)++;
  214. arch_spin_lock(&stack_trace_max_lock);
  215. *ptr = val;
  216. arch_spin_unlock(&stack_trace_max_lock);
  217. per_cpu(trace_active, cpu)--;
  218. local_irq_restore(flags);
  219. return count;
  220. }
  221. static const struct file_operations stack_max_size_fops = {
  222. .open = tracing_open_generic,
  223. .read = stack_max_size_read,
  224. .write = stack_max_size_write,
  225. .llseek = default_llseek,
  226. };
  227. static void *
  228. __next(struct seq_file *m, loff_t *pos)
  229. {
  230. long n = *pos - 1;
  231. if (n > stack_trace_max.nr_entries || stack_dump_trace[n] == ULONG_MAX)
  232. return NULL;
  233. m->private = (void *)n;
  234. return &m->private;
  235. }
  236. static void *
  237. t_next(struct seq_file *m, void *v, loff_t *pos)
  238. {
  239. (*pos)++;
  240. return __next(m, pos);
  241. }
  242. static void *t_start(struct seq_file *m, loff_t *pos)
  243. {
  244. int cpu;
  245. local_irq_disable();
  246. cpu = smp_processor_id();
  247. per_cpu(trace_active, cpu)++;
  248. arch_spin_lock(&stack_trace_max_lock);
  249. if (*pos == 0)
  250. return SEQ_START_TOKEN;
  251. return __next(m, pos);
  252. }
  253. static void t_stop(struct seq_file *m, void *p)
  254. {
  255. int cpu;
  256. arch_spin_unlock(&stack_trace_max_lock);
  257. cpu = smp_processor_id();
  258. per_cpu(trace_active, cpu)--;
  259. local_irq_enable();
  260. }
  261. static void trace_lookup_stack(struct seq_file *m, long i)
  262. {
  263. unsigned long addr = stack_dump_trace[i];
  264. seq_printf(m, "%pS\n", (void *)addr);
  265. }
  266. static void print_disabled(struct seq_file *m)
  267. {
  268. seq_puts(m, "#\n"
  269. "# Stack tracer disabled\n"
  270. "#\n"
  271. "# To enable the stack tracer, either add 'stacktrace' to the\n"
  272. "# kernel command line\n"
  273. "# or 'echo 1 > /proc/sys/kernel/stack_tracer_enabled'\n"
  274. "#\n");
  275. }
  276. static int t_show(struct seq_file *m, void *v)
  277. {
  278. long i;
  279. int size;
  280. if (v == SEQ_START_TOKEN) {
  281. seq_printf(m, " Depth Size Location"
  282. " (%d entries)\n"
  283. " ----- ---- --------\n",
  284. stack_trace_max.nr_entries);
  285. if (!stack_tracer_enabled && !stack_trace_max_size)
  286. print_disabled(m);
  287. return 0;
  288. }
  289. i = *(long *)v;
  290. if (i >= stack_trace_max.nr_entries ||
  291. stack_dump_trace[i] == ULONG_MAX)
  292. return 0;
  293. if (i+1 == stack_trace_max.nr_entries ||
  294. stack_dump_trace[i+1] == ULONG_MAX)
  295. size = stack_trace_index[i];
  296. else
  297. size = stack_trace_index[i] - stack_trace_index[i+1];
  298. seq_printf(m, "%3ld) %8d %5d ", i, stack_trace_index[i], size);
  299. trace_lookup_stack(m, i);
  300. return 0;
  301. }
  302. static const struct seq_operations stack_trace_seq_ops = {
  303. .start = t_start,
  304. .next = t_next,
  305. .stop = t_stop,
  306. .show = t_show,
  307. };
  308. static int stack_trace_open(struct inode *inode, struct file *file)
  309. {
  310. return seq_open(file, &stack_trace_seq_ops);
  311. }
  312. static const struct file_operations stack_trace_fops = {
  313. .open = stack_trace_open,
  314. .read = seq_read,
  315. .llseek = seq_lseek,
  316. .release = seq_release,
  317. };
  318. static int
  319. stack_trace_filter_open(struct inode *inode, struct file *file)
  320. {
  321. return ftrace_regex_open(&trace_ops, FTRACE_ITER_FILTER,
  322. inode, file);
  323. }
  324. static const struct file_operations stack_trace_filter_fops = {
  325. .open = stack_trace_filter_open,
  326. .read = seq_read,
  327. .write = ftrace_filter_write,
  328. .llseek = tracing_lseek,
  329. .release = ftrace_regex_release,
  330. };
  331. int
  332. stack_trace_sysctl(struct ctl_table *table, int write,
  333. void __user *buffer, size_t *lenp,
  334. loff_t *ppos)
  335. {
  336. int ret;
  337. mutex_lock(&stack_sysctl_mutex);
  338. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  339. if (ret || !write ||
  340. (last_stack_tracer_enabled == !!stack_tracer_enabled))
  341. goto out;
  342. last_stack_tracer_enabled = !!stack_tracer_enabled;
  343. if (stack_tracer_enabled)
  344. register_ftrace_function(&trace_ops);
  345. else
  346. unregister_ftrace_function(&trace_ops);
  347. out:
  348. mutex_unlock(&stack_sysctl_mutex);
  349. return ret;
  350. }
  351. static char stack_trace_filter_buf[COMMAND_LINE_SIZE+1] __initdata;
  352. static __init int enable_stacktrace(char *str)
  353. {
  354. if (strncmp(str, "_filter=", 8) == 0)
  355. strncpy(stack_trace_filter_buf, str+8, COMMAND_LINE_SIZE);
  356. stack_tracer_enabled = 1;
  357. last_stack_tracer_enabled = 1;
  358. return 1;
  359. }
  360. __setup("stacktrace", enable_stacktrace);
  361. static __init int stack_trace_init(void)
  362. {
  363. struct dentry *d_tracer;
  364. d_tracer = tracing_init_dentry();
  365. if (IS_ERR(d_tracer))
  366. return 0;
  367. trace_create_file("stack_max_size", 0644, d_tracer,
  368. &stack_trace_max_size, &stack_max_size_fops);
  369. trace_create_file("stack_trace", 0444, d_tracer,
  370. NULL, &stack_trace_fops);
  371. trace_create_file("stack_trace_filter", 0444, d_tracer,
  372. NULL, &stack_trace_filter_fops);
  373. if (stack_trace_filter_buf[0])
  374. ftrace_set_early_filter(&trace_ops, stack_trace_filter_buf, 1);
  375. if (stack_tracer_enabled)
  376. register_ftrace_function(&trace_ops);
  377. return 0;
  378. }
  379. device_initcall(stack_trace_init);