PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/trace/trace_stack.c

https://gitlab.com/LiquidSmooth-Devices/android_kernel_htc_msm8974
C | 380 lines | 280 code | 96 blank | 4 comment | 37 complexity | d81a675105e584e2bb1629ef15d4951e MD5 | raw file
Possible License(s): GPL-2.0
  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. static unsigned long stack_dump_trace[STACK_TRACE_ENTRIES+1] =
  20. { [0 ... (STACK_TRACE_ENTRIES)] = ULONG_MAX };
  21. static unsigned stack_dump_index[STACK_TRACE_ENTRIES];
  22. static struct stack_trace max_stack_trace = {
  23. .max_entries = STACK_TRACE_ENTRIES,
  24. .entries = stack_dump_trace,
  25. };
  26. static unsigned long max_stack_size;
  27. static arch_spinlock_t max_stack_lock =
  28. (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  29. static int stack_trace_disabled __read_mostly;
  30. static DEFINE_PER_CPU(int, trace_active);
  31. static DEFINE_MUTEX(stack_sysctl_mutex);
  32. int stack_tracer_enabled;
  33. static int last_stack_tracer_enabled;
  34. static inline void check_stack(void)
  35. {
  36. unsigned long this_size, flags;
  37. unsigned long *p, *top, *start;
  38. int i;
  39. this_size = ((unsigned long)&this_size) & (THREAD_SIZE-1);
  40. this_size = THREAD_SIZE - this_size;
  41. if (this_size <= max_stack_size)
  42. return;
  43. if (!object_is_on_stack(&this_size))
  44. return;
  45. local_irq_save(flags);
  46. arch_spin_lock(&max_stack_lock);
  47. if (this_size <= max_stack_size)
  48. goto out;
  49. max_stack_size = this_size;
  50. max_stack_trace.nr_entries = 0;
  51. max_stack_trace.skip = 3;
  52. save_stack_trace(&max_stack_trace);
  53. i = 0;
  54. start = &this_size;
  55. top = (unsigned long *)
  56. (((unsigned long)start & ~(THREAD_SIZE-1)) + THREAD_SIZE);
  57. while (i < max_stack_trace.nr_entries) {
  58. int found = 0;
  59. stack_dump_index[i] = this_size;
  60. p = start;
  61. for (; p < top && i < max_stack_trace.nr_entries; p++) {
  62. if (*p == stack_dump_trace[i]) {
  63. this_size = stack_dump_index[i++] =
  64. (top - p) * sizeof(unsigned long);
  65. found = 1;
  66. start = p + 1;
  67. }
  68. }
  69. if (!found)
  70. i++;
  71. }
  72. out:
  73. arch_spin_unlock(&max_stack_lock);
  74. local_irq_restore(flags);
  75. }
  76. static void
  77. stack_trace_call(unsigned long ip, unsigned long parent_ip)
  78. {
  79. int cpu;
  80. if (unlikely(!ftrace_enabled || stack_trace_disabled))
  81. return;
  82. preempt_disable_notrace();
  83. cpu = raw_smp_processor_id();
  84. if (per_cpu(trace_active, cpu)++ != 0)
  85. goto out;
  86. check_stack();
  87. out:
  88. per_cpu(trace_active, cpu)--;
  89. preempt_enable_notrace();
  90. }
  91. static struct ftrace_ops trace_ops __read_mostly =
  92. {
  93. .func = stack_trace_call,
  94. };
  95. static ssize_t
  96. stack_max_size_read(struct file *filp, char __user *ubuf,
  97. size_t count, loff_t *ppos)
  98. {
  99. unsigned long *ptr = filp->private_data;
  100. char buf[64];
  101. int r;
  102. r = snprintf(buf, sizeof(buf), "%ld\n", *ptr);
  103. if (r > sizeof(buf))
  104. r = sizeof(buf);
  105. return simple_read_from_buffer(ubuf, count, ppos, buf, r);
  106. }
  107. static ssize_t
  108. stack_max_size_write(struct file *filp, const char __user *ubuf,
  109. size_t count, loff_t *ppos)
  110. {
  111. long *ptr = filp->private_data;
  112. unsigned long val, flags;
  113. int ret;
  114. int cpu;
  115. ret = kstrtoul_from_user(ubuf, count, 10, &val);
  116. if (ret)
  117. return ret;
  118. local_irq_save(flags);
  119. cpu = smp_processor_id();
  120. per_cpu(trace_active, cpu)++;
  121. arch_spin_lock(&max_stack_lock);
  122. *ptr = val;
  123. arch_spin_unlock(&max_stack_lock);
  124. per_cpu(trace_active, cpu)--;
  125. local_irq_restore(flags);
  126. return count;
  127. }
  128. static const struct file_operations stack_max_size_fops = {
  129. .open = tracing_open_generic,
  130. .read = stack_max_size_read,
  131. .write = stack_max_size_write,
  132. .llseek = default_llseek,
  133. };
  134. static void *
  135. __next(struct seq_file *m, loff_t *pos)
  136. {
  137. long n = *pos - 1;
  138. if (n >= max_stack_trace.nr_entries || stack_dump_trace[n] == ULONG_MAX)
  139. return NULL;
  140. m->private = (void *)n;
  141. return &m->private;
  142. }
  143. static void *
  144. t_next(struct seq_file *m, void *v, loff_t *pos)
  145. {
  146. (*pos)++;
  147. return __next(m, pos);
  148. }
  149. static void *t_start(struct seq_file *m, loff_t *pos)
  150. {
  151. int cpu;
  152. local_irq_disable();
  153. cpu = smp_processor_id();
  154. per_cpu(trace_active, cpu)++;
  155. arch_spin_lock(&max_stack_lock);
  156. if (*pos == 0)
  157. return SEQ_START_TOKEN;
  158. return __next(m, pos);
  159. }
  160. static void t_stop(struct seq_file *m, void *p)
  161. {
  162. int cpu;
  163. arch_spin_unlock(&max_stack_lock);
  164. cpu = smp_processor_id();
  165. per_cpu(trace_active, cpu)--;
  166. local_irq_enable();
  167. }
  168. static int trace_lookup_stack(struct seq_file *m, long i)
  169. {
  170. unsigned long addr = stack_dump_trace[i];
  171. return seq_printf(m, "%pS\n", (void *)addr);
  172. }
  173. static void print_disabled(struct seq_file *m)
  174. {
  175. seq_puts(m, "#\n"
  176. "# Stack tracer disabled\n"
  177. "#\n"
  178. "# To enable the stack tracer, either add 'stacktrace' to the\n"
  179. "# kernel command line\n"
  180. "# or 'echo 1 > /proc/sys/kernel/stack_tracer_enabled'\n"
  181. "#\n");
  182. }
  183. static int t_show(struct seq_file *m, void *v)
  184. {
  185. long i;
  186. int size;
  187. if (v == SEQ_START_TOKEN) {
  188. seq_printf(m, " Depth Size Location"
  189. " (%d entries)\n"
  190. " ----- ---- --------\n",
  191. max_stack_trace.nr_entries - 1);
  192. if (!stack_tracer_enabled && !max_stack_size)
  193. print_disabled(m);
  194. return 0;
  195. }
  196. i = *(long *)v;
  197. if (i >= max_stack_trace.nr_entries ||
  198. stack_dump_trace[i] == ULONG_MAX)
  199. return 0;
  200. if (i+1 == max_stack_trace.nr_entries ||
  201. stack_dump_trace[i+1] == ULONG_MAX)
  202. size = stack_dump_index[i];
  203. else
  204. size = stack_dump_index[i] - stack_dump_index[i+1];
  205. seq_printf(m, "%3ld) %8d %5d ", i, stack_dump_index[i], size);
  206. trace_lookup_stack(m, i);
  207. return 0;
  208. }
  209. static const struct seq_operations stack_trace_seq_ops = {
  210. .start = t_start,
  211. .next = t_next,
  212. .stop = t_stop,
  213. .show = t_show,
  214. };
  215. static int stack_trace_open(struct inode *inode, struct file *file)
  216. {
  217. return seq_open(file, &stack_trace_seq_ops);
  218. }
  219. static const struct file_operations stack_trace_fops = {
  220. .open = stack_trace_open,
  221. .read = seq_read,
  222. .llseek = seq_lseek,
  223. .release = seq_release,
  224. };
  225. static int
  226. stack_trace_filter_open(struct inode *inode, struct file *file)
  227. {
  228. return ftrace_regex_open(&trace_ops, FTRACE_ITER_FILTER,
  229. inode, file);
  230. }
  231. static const struct file_operations stack_trace_filter_fops = {
  232. .open = stack_trace_filter_open,
  233. .read = seq_read,
  234. .write = ftrace_filter_write,
  235. .llseek = ftrace_regex_lseek,
  236. .release = ftrace_regex_release,
  237. };
  238. int
  239. stack_trace_sysctl(struct ctl_table *table, int write,
  240. void __user *buffer, size_t *lenp,
  241. loff_t *ppos)
  242. {
  243. int ret;
  244. mutex_lock(&stack_sysctl_mutex);
  245. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  246. if (ret || !write ||
  247. (last_stack_tracer_enabled == !!stack_tracer_enabled))
  248. goto out;
  249. last_stack_tracer_enabled = !!stack_tracer_enabled;
  250. if (stack_tracer_enabled)
  251. register_ftrace_function(&trace_ops);
  252. else
  253. unregister_ftrace_function(&trace_ops);
  254. out:
  255. mutex_unlock(&stack_sysctl_mutex);
  256. return ret;
  257. }
  258. static char stack_trace_filter_buf[COMMAND_LINE_SIZE+1] __initdata;
  259. static __init int enable_stacktrace(char *str)
  260. {
  261. if (strncmp(str, "_filter=", 8) == 0)
  262. strncpy(stack_trace_filter_buf, str+8, COMMAND_LINE_SIZE);
  263. stack_tracer_enabled = 1;
  264. last_stack_tracer_enabled = 1;
  265. return 1;
  266. }
  267. __setup("stacktrace", enable_stacktrace);
  268. static __init int stack_trace_init(void)
  269. {
  270. struct dentry *d_tracer;
  271. d_tracer = tracing_init_dentry();
  272. trace_create_file("stack_max_size", 0644, d_tracer,
  273. &max_stack_size, &stack_max_size_fops);
  274. trace_create_file("stack_trace", 0444, d_tracer,
  275. NULL, &stack_trace_fops);
  276. trace_create_file("stack_trace_filter", 0444, d_tracer,
  277. NULL, &stack_trace_filter_fops);
  278. if (stack_trace_filter_buf[0])
  279. ftrace_set_early_filter(&trace_ops, stack_trace_filter_buf, 1);
  280. if (stack_tracer_enabled)
  281. register_ftrace_function(&trace_ops);
  282. return 0;
  283. }
  284. device_initcall(stack_trace_init);