trace_stack.c 11 KB

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