trace_stack.c 11 KB

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