2008-08-28 11:31:01 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <linux/stacktrace.h>
|
|
|
|
#include <linux/kallsyms.h>
|
|
|
|
#include <linux/seq_file.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/uaccess.h>
|
|
|
|
#include <linux/debugfs.h>
|
|
|
|
#include <linux/ftrace.h>
|
|
|
|
#include <linux/module.h>
|
2008-12-17 12:06:40 +08:00
|
|
|
#include <linux/sysctl.h>
|
2008-08-28 11:31:01 +08:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/fs.h>
|
2011-12-20 11:01:00 +08:00
|
|
|
|
|
|
|
#include <asm/setup.h>
|
|
|
|
|
2008-08-28 11:31:01 +08:00
|
|
|
#include "trace.h"
|
|
|
|
|
|
|
|
#define STACK_TRACE_ENTRIES 500
|
|
|
|
|
2013-03-14 09:25:35 +08:00
|
|
|
#ifdef CC_USING_FENTRY
|
2013-03-14 11:34:22 +08:00
|
|
|
# define fentry 1
|
2013-03-14 09:25:35 +08:00
|
|
|
#else
|
2013-03-14 11:34:22 +08:00
|
|
|
# define fentry 0
|
2013-03-14 09:25:35 +08:00
|
|
|
#endif
|
|
|
|
|
2008-08-30 04:51:43 +08:00
|
|
|
static unsigned long stack_dump_trace[STACK_TRACE_ENTRIES+1] =
|
|
|
|
{ [0 ... (STACK_TRACE_ENTRIES)] = ULONG_MAX };
|
|
|
|
static unsigned stack_dump_index[STACK_TRACE_ENTRIES];
|
|
|
|
|
2013-03-14 11:34:22 +08:00
|
|
|
/*
|
|
|
|
* Reserve one entry for the passed in ip. This will allow
|
|
|
|
* us to remove most or all of the stack size overhead
|
|
|
|
* added by the stack tracer itself.
|
|
|
|
*/
|
2008-08-28 11:31:01 +08:00
|
|
|
static struct stack_trace max_stack_trace = {
|
2013-03-14 11:34:22 +08:00
|
|
|
.max_entries = STACK_TRACE_ENTRIES - 1,
|
|
|
|
.entries = &stack_dump_trace[1],
|
2008-08-28 11:31:01 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned long max_stack_size;
|
2009-12-03 02:49:50 +08:00
|
|
|
static arch_spinlock_t max_stack_lock =
|
2009-12-03 19:38:57 +08:00
|
|
|
(arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
|
2008-08-28 11:31:01 +08:00
|
|
|
|
|
|
|
static DEFINE_PER_CPU(int, trace_active);
|
2008-12-17 12:06:40 +08:00
|
|
|
static DEFINE_MUTEX(stack_sysctl_mutex);
|
|
|
|
|
|
|
|
int stack_tracer_enabled;
|
|
|
|
static int last_stack_tracer_enabled;
|
2008-08-28 11:31:01 +08:00
|
|
|
|
2014-06-02 12:33:12 +08:00
|
|
|
static inline void print_max_stack(void)
|
|
|
|
{
|
|
|
|
long i;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
pr_emerg(" Depth Size Location (%d entries)\n"
|
|
|
|
" ----- ---- --------\n",
|
|
|
|
max_stack_trace.nr_entries - 1);
|
|
|
|
|
|
|
|
for (i = 0; i < max_stack_trace.nr_entries; i++) {
|
|
|
|
if (stack_dump_trace[i] == ULONG_MAX)
|
|
|
|
break;
|
|
|
|
if (i+1 == max_stack_trace.nr_entries ||
|
|
|
|
stack_dump_trace[i+1] == ULONG_MAX)
|
|
|
|
size = stack_dump_index[i];
|
|
|
|
else
|
|
|
|
size = stack_dump_index[i] - stack_dump_index[i+1];
|
|
|
|
|
|
|
|
pr_emerg("%3ld) %8d %5d %pS\n", i, stack_dump_index[i],
|
|
|
|
size, (void *)stack_dump_trace[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-14 08:43:57 +08:00
|
|
|
static inline void
|
2013-03-14 09:25:35 +08:00
|
|
|
check_stack(unsigned long ip, unsigned long *stack)
|
2008-08-28 11:31:01 +08:00
|
|
|
{
|
2014-06-02 12:33:12 +08:00
|
|
|
unsigned long this_size, flags; unsigned long *p, *top, *start;
|
2013-03-14 11:34:22 +08:00
|
|
|
static int tracer_frame;
|
|
|
|
int frame_size = ACCESS_ONCE(tracer_frame);
|
2008-08-30 04:51:43 +08:00
|
|
|
int i;
|
2008-08-28 11:31:01 +08:00
|
|
|
|
2013-03-14 08:43:57 +08:00
|
|
|
this_size = ((unsigned long)stack) & (THREAD_SIZE-1);
|
2008-08-28 11:31:01 +08:00
|
|
|
this_size = THREAD_SIZE - this_size;
|
2013-03-14 11:34:22 +08:00
|
|
|
/* Remove the frame of the tracer */
|
|
|
|
this_size -= frame_size;
|
2008-08-28 11:31:01 +08:00
|
|
|
|
|
|
|
if (this_size <= max_stack_size)
|
|
|
|
return;
|
|
|
|
|
2008-10-07 09:24:18 +08:00
|
|
|
/* we do not handle interrupt stacks yet */
|
2013-03-14 08:43:57 +08:00
|
|
|
if (!object_is_on_stack(stack))
|
2008-10-07 09:24:18 +08:00
|
|
|
return;
|
|
|
|
|
2008-12-03 04:34:05 +08:00
|
|
|
local_irq_save(flags);
|
2009-12-03 03:01:25 +08:00
|
|
|
arch_spin_lock(&max_stack_lock);
|
2008-08-28 11:31:01 +08:00
|
|
|
|
2013-03-14 11:34:22 +08:00
|
|
|
/* In case another CPU set the tracer_frame on us */
|
|
|
|
if (unlikely(!frame_size))
|
|
|
|
this_size -= tracer_frame;
|
|
|
|
|
2008-08-28 11:31:01 +08:00
|
|
|
/* a race could have already updated it */
|
|
|
|
if (this_size <= max_stack_size)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
max_stack_size = this_size;
|
|
|
|
|
2014-04-20 23:10:43 +08:00
|
|
|
max_stack_trace.nr_entries = 0;
|
|
|
|
|
|
|
|
if (using_ftrace_ops_list_func())
|
|
|
|
max_stack_trace.skip = 4;
|
|
|
|
else
|
|
|
|
max_stack_trace.skip = 3;
|
2008-08-28 11:31:01 +08:00
|
|
|
|
|
|
|
save_stack_trace(&max_stack_trace);
|
|
|
|
|
2013-03-14 09:25:35 +08:00
|
|
|
/*
|
2013-03-14 11:34:22 +08:00
|
|
|
* Add the passed in ip from the function tracer.
|
|
|
|
* Searching for this on the stack will skip over
|
|
|
|
* most of the overhead from the stack tracer itself.
|
2013-03-14 09:25:35 +08:00
|
|
|
*/
|
2013-03-14 11:34:22 +08:00
|
|
|
stack_dump_trace[0] = ip;
|
|
|
|
max_stack_trace.nr_entries++;
|
2013-03-14 09:25:35 +08:00
|
|
|
|
2008-08-30 04:51:43 +08:00
|
|
|
/*
|
|
|
|
* Now find where in the stack these are.
|
|
|
|
*/
|
|
|
|
i = 0;
|
2013-03-14 08:43:57 +08:00
|
|
|
start = stack;
|
2008-08-30 04:51:43 +08:00
|
|
|
top = (unsigned long *)
|
|
|
|
(((unsigned long)start & ~(THREAD_SIZE-1)) + THREAD_SIZE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Loop through all the entries. One of the entries may
|
|
|
|
* for some reason be missed on the stack, so we may
|
|
|
|
* have to account for them. If they are all there, this
|
|
|
|
* loop will only happen once. This code only takes place
|
|
|
|
* on a new max, so it is far from a fast path.
|
|
|
|
*/
|
|
|
|
while (i < max_stack_trace.nr_entries) {
|
2008-12-04 00:04:50 +08:00
|
|
|
int found = 0;
|
2008-08-30 04:51:43 +08:00
|
|
|
|
|
|
|
stack_dump_index[i] = this_size;
|
|
|
|
p = start;
|
|
|
|
|
|
|
|
for (; p < top && i < max_stack_trace.nr_entries; p++) {
|
|
|
|
if (*p == stack_dump_trace[i]) {
|
|
|
|
this_size = stack_dump_index[i++] =
|
|
|
|
(top - p) * sizeof(unsigned long);
|
2008-12-04 00:04:50 +08:00
|
|
|
found = 1;
|
2008-08-30 04:51:43 +08:00
|
|
|
/* Start the search from here */
|
|
|
|
start = p + 1;
|
2013-03-14 11:34:22 +08:00
|
|
|
/*
|
|
|
|
* We do not want to show the overhead
|
|
|
|
* of the stack tracer stack in the
|
|
|
|
* max stack. If we haven't figured
|
|
|
|
* out what that is, then figure it out
|
|
|
|
* now.
|
|
|
|
*/
|
|
|
|
if (unlikely(!tracer_frame) && i == 1) {
|
|
|
|
tracer_frame = (p - stack) *
|
|
|
|
sizeof(unsigned long);
|
|
|
|
max_stack_size -= tracer_frame;
|
|
|
|
}
|
2008-08-30 04:51:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-04 00:04:50 +08:00
|
|
|
if (!found)
|
|
|
|
i++;
|
2008-08-30 04:51:43 +08:00
|
|
|
}
|
|
|
|
|
2014-09-12 21:16:18 +08:00
|
|
|
if (task_stack_end_corrupted(current)) {
|
2014-06-02 12:33:12 +08:00
|
|
|
print_max_stack();
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
|
2008-08-28 11:31:01 +08:00
|
|
|
out:
|
2009-12-03 03:01:25 +08:00
|
|
|
arch_spin_unlock(&max_stack_lock);
|
2008-12-03 04:34:05 +08:00
|
|
|
local_irq_restore(flags);
|
2008-08-28 11:31:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-08-10 00:50:46 +08:00
|
|
|
stack_trace_call(unsigned long ip, unsigned long parent_ip,
|
|
|
|
struct ftrace_ops *op, struct pt_regs *pt_regs)
|
2008-08-28 11:31:01 +08:00
|
|
|
{
|
2013-03-14 08:43:57 +08:00
|
|
|
unsigned long stack;
|
2010-06-03 21:36:50 +08:00
|
|
|
int cpu;
|
2008-08-28 11:31:01 +08:00
|
|
|
|
2010-06-03 21:36:50 +08:00
|
|
|
preempt_disable_notrace();
|
2008-08-28 11:31:01 +08:00
|
|
|
|
|
|
|
cpu = raw_smp_processor_id();
|
|
|
|
/* no atomic needed, we only modify this variable by this cpu */
|
|
|
|
if (per_cpu(trace_active, cpu)++ != 0)
|
|
|
|
goto out;
|
|
|
|
|
2013-03-14 11:34:22 +08:00
|
|
|
/*
|
|
|
|
* When fentry is used, the traced function does not get
|
|
|
|
* its stack frame set up, and we lose the parent.
|
|
|
|
* The ip is pretty useless because the function tracer
|
|
|
|
* was called before that function set up its stack frame.
|
|
|
|
* In this case, we use the parent ip.
|
|
|
|
*
|
|
|
|
* By adding the return address of either the parent ip
|
|
|
|
* or the current ip we can disregard most of the stack usage
|
|
|
|
* caused by the stack tracer itself.
|
|
|
|
*
|
|
|
|
* The function tracer always reports the address of where the
|
|
|
|
* mcount call was, but the stack will hold the return address.
|
|
|
|
*/
|
|
|
|
if (fentry)
|
|
|
|
ip = parent_ip;
|
|
|
|
else
|
|
|
|
ip += MCOUNT_INSN_SIZE;
|
|
|
|
|
|
|
|
check_stack(ip, &stack);
|
2008-08-28 11:31:01 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
per_cpu(trace_active, cpu)--;
|
|
|
|
/* prevent recursion in schedule */
|
2010-06-03 21:36:50 +08:00
|
|
|
preempt_enable_notrace();
|
2008-08-28 11:31:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct ftrace_ops trace_ops __read_mostly =
|
|
|
|
{
|
|
|
|
.func = stack_trace_call,
|
2012-07-20 23:04:44 +08:00
|
|
|
.flags = FTRACE_OPS_FL_RECURSION_SAFE,
|
2008-08-28 11:31:01 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
stack_max_size_read(struct file *filp, char __user *ubuf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
unsigned long *ptr = filp->private_data;
|
|
|
|
char buf[64];
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = snprintf(buf, sizeof(buf), "%ld\n", *ptr);
|
|
|
|
if (r > sizeof(buf))
|
|
|
|
r = sizeof(buf);
|
|
|
|
return simple_read_from_buffer(ubuf, count, ppos, buf, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
stack_max_size_write(struct file *filp, const char __user *ubuf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
long *ptr = filp->private_data;
|
|
|
|
unsigned long val, flags;
|
|
|
|
int ret;
|
2010-02-02 15:32:09 +08:00
|
|
|
int cpu;
|
2008-08-28 11:31:01 +08:00
|
|
|
|
2011-06-08 03:58:27 +08:00
|
|
|
ret = kstrtoul_from_user(ubuf, count, 10, &val);
|
|
|
|
if (ret)
|
2008-08-28 11:31:01 +08:00
|
|
|
return ret;
|
|
|
|
|
2008-12-03 04:34:05 +08:00
|
|
|
local_irq_save(flags);
|
2010-02-02 15:32:09 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* In case we trace inside arch_spin_lock() or after (NMI),
|
|
|
|
* we will cause circular lock, so we also need to increase
|
|
|
|
* the percpu trace_active here.
|
|
|
|
*/
|
|
|
|
cpu = smp_processor_id();
|
|
|
|
per_cpu(trace_active, cpu)++;
|
|
|
|
|
2009-12-03 03:01:25 +08:00
|
|
|
arch_spin_lock(&max_stack_lock);
|
2008-08-28 11:31:01 +08:00
|
|
|
*ptr = val;
|
2009-12-03 03:01:25 +08:00
|
|
|
arch_spin_unlock(&max_stack_lock);
|
2010-02-02 15:32:09 +08:00
|
|
|
|
|
|
|
per_cpu(trace_active, cpu)--;
|
2008-12-03 04:34:05 +08:00
|
|
|
local_irq_restore(flags);
|
2008-08-28 11:31:01 +08:00
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2008-12-17 12:06:40 +08:00
|
|
|
static const struct file_operations stack_max_size_fops = {
|
2008-08-28 11:31:01 +08:00
|
|
|
.open = tracing_open_generic,
|
|
|
|
.read = stack_max_size_read,
|
|
|
|
.write = stack_max_size_write,
|
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-16 00:52:59 +08:00
|
|
|
.llseek = default_llseek,
|
2008-08-28 11:31:01 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static void *
|
2009-08-17 16:53:37 +08:00
|
|
|
__next(struct seq_file *m, loff_t *pos)
|
2008-08-28 11:31:01 +08:00
|
|
|
{
|
2009-08-17 16:53:37 +08:00
|
|
|
long n = *pos - 1;
|
2008-08-28 11:31:01 +08:00
|
|
|
|
2009-08-17 16:53:37 +08:00
|
|
|
if (n >= max_stack_trace.nr_entries || stack_dump_trace[n] == ULONG_MAX)
|
2008-08-28 11:31:01 +08:00
|
|
|
return NULL;
|
|
|
|
|
2009-08-17 16:53:37 +08:00
|
|
|
m->private = (void *)n;
|
2008-08-30 04:51:43 +08:00
|
|
|
return &m->private;
|
2008-08-28 11:31:01 +08:00
|
|
|
}
|
|
|
|
|
2009-08-17 16:53:37 +08:00
|
|
|
static void *
|
|
|
|
t_next(struct seq_file *m, void *v, loff_t *pos)
|
2008-08-28 11:31:01 +08:00
|
|
|
{
|
2009-08-17 16:53:37 +08:00
|
|
|
(*pos)++;
|
|
|
|
return __next(m, pos);
|
|
|
|
}
|
2008-08-28 11:31:01 +08:00
|
|
|
|
2009-08-17 16:53:37 +08:00
|
|
|
static void *t_start(struct seq_file *m, loff_t *pos)
|
|
|
|
{
|
2010-02-02 15:32:09 +08:00
|
|
|
int cpu;
|
|
|
|
|
2008-08-28 11:31:01 +08:00
|
|
|
local_irq_disable();
|
2010-02-02 15:32:09 +08:00
|
|
|
|
|
|
|
cpu = smp_processor_id();
|
|
|
|
per_cpu(trace_active, cpu)++;
|
|
|
|
|
2009-12-03 03:01:25 +08:00
|
|
|
arch_spin_lock(&max_stack_lock);
|
2008-08-28 11:31:01 +08:00
|
|
|
|
function tracing: fix wrong position computing of stack_trace
Impact: make output of stack_trace complete if buffer overruns
When read buffer overruns, the output of stack_trace isn't complete.
When printing records with seq_printf in t_show, if the read buffer
has overruned by the current record, then this record won't be
printed to user space through read buffer, it will just be dropped in
this printing.
When next printing, t_start should return the "*pos"th record, which
is the one dropped by previous printing, but it just returns
(m->private + *pos)th record.
Here we use a more sane method to implement seq_operations which can
be found in kernel code. Thus we needn't initialize m->private.
About testing, it's not easy to overrun read buffer, but we can use
seq_printf to print more padding bytes in t_show, then it's easy to
check whether or not records are lost.
This commit has been tested on both condition of overrun and non
overrun.
Signed-off-by: Liming Wang <liming.wang@windriver.com>
Acked-by: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-11-21 11:00:18 +08:00
|
|
|
if (*pos == 0)
|
|
|
|
return SEQ_START_TOKEN;
|
|
|
|
|
2009-08-17 16:53:37 +08:00
|
|
|
return __next(m, pos);
|
2008-08-28 11:31:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void t_stop(struct seq_file *m, void *p)
|
|
|
|
{
|
2010-02-02 15:32:09 +08:00
|
|
|
int cpu;
|
|
|
|
|
2009-12-03 03:01:25 +08:00
|
|
|
arch_spin_unlock(&max_stack_lock);
|
2010-02-02 15:32:09 +08:00
|
|
|
|
|
|
|
cpu = smp_processor_id();
|
|
|
|
per_cpu(trace_active, cpu)--;
|
|
|
|
|
2008-08-28 11:31:01 +08:00
|
|
|
local_irq_enable();
|
|
|
|
}
|
|
|
|
|
2008-08-30 04:51:43 +08:00
|
|
|
static int trace_lookup_stack(struct seq_file *m, long i)
|
2008-08-28 11:31:01 +08:00
|
|
|
{
|
2008-08-30 04:51:43 +08:00
|
|
|
unsigned long addr = stack_dump_trace[i];
|
2008-08-28 11:31:01 +08:00
|
|
|
|
2010-08-25 09:32:38 +08:00
|
|
|
return seq_printf(m, "%pS\n", (void *)addr);
|
2008-08-28 11:31:01 +08:00
|
|
|
}
|
|
|
|
|
2009-03-13 07:42:29 +08:00
|
|
|
static void print_disabled(struct seq_file *m)
|
|
|
|
{
|
|
|
|
seq_puts(m, "#\n"
|
|
|
|
"# Stack tracer disabled\n"
|
|
|
|
"#\n"
|
|
|
|
"# To enable the stack tracer, either add 'stacktrace' to the\n"
|
|
|
|
"# kernel command line\n"
|
|
|
|
"# or 'echo 1 > /proc/sys/kernel/stack_tracer_enabled'\n"
|
|
|
|
"#\n");
|
|
|
|
}
|
|
|
|
|
2008-08-28 11:31:01 +08:00
|
|
|
static int t_show(struct seq_file *m, void *v)
|
|
|
|
{
|
function tracing: fix wrong position computing of stack_trace
Impact: make output of stack_trace complete if buffer overruns
When read buffer overruns, the output of stack_trace isn't complete.
When printing records with seq_printf in t_show, if the read buffer
has overruned by the current record, then this record won't be
printed to user space through read buffer, it will just be dropped in
this printing.
When next printing, t_start should return the "*pos"th record, which
is the one dropped by previous printing, but it just returns
(m->private + *pos)th record.
Here we use a more sane method to implement seq_operations which can
be found in kernel code. Thus we needn't initialize m->private.
About testing, it's not easy to overrun read buffer, but we can use
seq_printf to print more padding bytes in t_show, then it's easy to
check whether or not records are lost.
This commit has been tested on both condition of overrun and non
overrun.
Signed-off-by: Liming Wang <liming.wang@windriver.com>
Acked-by: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-11-21 11:00:18 +08:00
|
|
|
long i;
|
2008-08-30 04:51:43 +08:00
|
|
|
int size;
|
|
|
|
|
function tracing: fix wrong position computing of stack_trace
Impact: make output of stack_trace complete if buffer overruns
When read buffer overruns, the output of stack_trace isn't complete.
When printing records with seq_printf in t_show, if the read buffer
has overruned by the current record, then this record won't be
printed to user space through read buffer, it will just be dropped in
this printing.
When next printing, t_start should return the "*pos"th record, which
is the one dropped by previous printing, but it just returns
(m->private + *pos)th record.
Here we use a more sane method to implement seq_operations which can
be found in kernel code. Thus we needn't initialize m->private.
About testing, it's not easy to overrun read buffer, but we can use
seq_printf to print more padding bytes in t_show, then it's easy to
check whether or not records are lost.
This commit has been tested on both condition of overrun and non
overrun.
Signed-off-by: Liming Wang <liming.wang@windriver.com>
Acked-by: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-11-21 11:00:18 +08:00
|
|
|
if (v == SEQ_START_TOKEN) {
|
2009-03-13 12:00:58 +08:00
|
|
|
seq_printf(m, " Depth Size Location"
|
2008-08-30 04:51:43 +08:00
|
|
|
" (%d entries)\n"
|
2009-03-13 12:00:58 +08:00
|
|
|
" ----- ---- --------\n",
|
2009-06-03 16:01:28 +08:00
|
|
|
max_stack_trace.nr_entries - 1);
|
2009-03-13 07:42:29 +08:00
|
|
|
|
|
|
|
if (!stack_tracer_enabled && !max_stack_size)
|
|
|
|
print_disabled(m);
|
|
|
|
|
2008-08-30 04:51:43 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2008-08-28 11:31:01 +08:00
|
|
|
|
function tracing: fix wrong position computing of stack_trace
Impact: make output of stack_trace complete if buffer overruns
When read buffer overruns, the output of stack_trace isn't complete.
When printing records with seq_printf in t_show, if the read buffer
has overruned by the current record, then this record won't be
printed to user space through read buffer, it will just be dropped in
this printing.
When next printing, t_start should return the "*pos"th record, which
is the one dropped by previous printing, but it just returns
(m->private + *pos)th record.
Here we use a more sane method to implement seq_operations which can
be found in kernel code. Thus we needn't initialize m->private.
About testing, it's not easy to overrun read buffer, but we can use
seq_printf to print more padding bytes in t_show, then it's easy to
check whether or not records are lost.
This commit has been tested on both condition of overrun and non
overrun.
Signed-off-by: Liming Wang <liming.wang@windriver.com>
Acked-by: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-11-21 11:00:18 +08:00
|
|
|
i = *(long *)v;
|
|
|
|
|
2008-08-30 04:51:43 +08:00
|
|
|
if (i >= max_stack_trace.nr_entries ||
|
|
|
|
stack_dump_trace[i] == ULONG_MAX)
|
2008-08-28 11:31:01 +08:00
|
|
|
return 0;
|
|
|
|
|
2008-08-30 04:51:43 +08:00
|
|
|
if (i+1 == max_stack_trace.nr_entries ||
|
|
|
|
stack_dump_trace[i+1] == ULONG_MAX)
|
|
|
|
size = stack_dump_index[i];
|
|
|
|
else
|
|
|
|
size = stack_dump_index[i] - stack_dump_index[i+1];
|
|
|
|
|
|
|
|
seq_printf(m, "%3ld) %8d %5d ", i, stack_dump_index[i], size);
|
|
|
|
|
|
|
|
trace_lookup_stack(m, i);
|
2008-08-28 11:31:01 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-17 12:06:40 +08:00
|
|
|
static const struct seq_operations stack_trace_seq_ops = {
|
2008-08-28 11:31:01 +08:00
|
|
|
.start = t_start,
|
|
|
|
.next = t_next,
|
|
|
|
.stop = t_stop,
|
|
|
|
.show = t_show,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int stack_trace_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
2009-07-23 11:28:40 +08:00
|
|
|
return seq_open(file, &stack_trace_seq_ops);
|
2008-08-28 11:31:01 +08:00
|
|
|
}
|
|
|
|
|
2008-12-17 12:06:40 +08:00
|
|
|
static const struct file_operations stack_trace_fops = {
|
2008-08-28 11:31:01 +08:00
|
|
|
.open = stack_trace_open,
|
|
|
|
.read = seq_read,
|
|
|
|
.llseek = seq_lseek,
|
2009-07-23 11:28:40 +08:00
|
|
|
.release = seq_release,
|
2008-08-28 11:31:01 +08:00
|
|
|
};
|
|
|
|
|
2011-12-20 03:44:09 +08:00
|
|
|
static int
|
|
|
|
stack_trace_filter_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
return ftrace_regex_open(&trace_ops, FTRACE_ITER_FILTER,
|
|
|
|
inode, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations stack_trace_filter_fops = {
|
|
|
|
.open = stack_trace_filter_open,
|
|
|
|
.read = seq_read,
|
|
|
|
.write = ftrace_filter_write,
|
2013-12-22 06:39:40 +08:00
|
|
|
.llseek = tracing_lseek,
|
2011-12-20 03:44:09 +08:00
|
|
|
.release = ftrace_regex_release,
|
|
|
|
};
|
|
|
|
|
2008-12-17 12:06:40 +08:00
|
|
|
int
|
|
|
|
stack_trace_sysctl(struct ctl_table *table, int write,
|
2009-09-24 06:57:19 +08:00
|
|
|
void __user *buffer, size_t *lenp,
|
2008-12-17 12:06:40 +08:00
|
|
|
loff_t *ppos)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
mutex_lock(&stack_sysctl_mutex);
|
|
|
|
|
2009-09-24 06:57:19 +08:00
|
|
|
ret = proc_dointvec(table, write, buffer, lenp, ppos);
|
2008-12-17 12:06:40 +08:00
|
|
|
|
|
|
|
if (ret || !write ||
|
2009-06-26 16:55:51 +08:00
|
|
|
(last_stack_tracer_enabled == !!stack_tracer_enabled))
|
2008-12-17 12:06:40 +08:00
|
|
|
goto out;
|
|
|
|
|
2009-06-26 16:55:51 +08:00
|
|
|
last_stack_tracer_enabled = !!stack_tracer_enabled;
|
2008-12-17 12:06:40 +08:00
|
|
|
|
|
|
|
if (stack_tracer_enabled)
|
|
|
|
register_ftrace_function(&trace_ops);
|
|
|
|
else
|
|
|
|
unregister_ftrace_function(&trace_ops);
|
|
|
|
|
|
|
|
out:
|
|
|
|
mutex_unlock(&stack_sysctl_mutex);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-12-20 11:01:00 +08:00
|
|
|
static char stack_trace_filter_buf[COMMAND_LINE_SIZE+1] __initdata;
|
|
|
|
|
2008-12-17 12:06:40 +08:00
|
|
|
static __init int enable_stacktrace(char *str)
|
|
|
|
{
|
2011-12-20 11:01:00 +08:00
|
|
|
if (strncmp(str, "_filter=", 8) == 0)
|
|
|
|
strncpy(stack_trace_filter_buf, str+8, COMMAND_LINE_SIZE);
|
|
|
|
|
2008-12-17 22:43:00 +08:00
|
|
|
stack_tracer_enabled = 1;
|
|
|
|
last_stack_tracer_enabled = 1;
|
2008-12-17 12:06:40 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
__setup("stacktrace", enable_stacktrace);
|
|
|
|
|
2008-08-28 11:31:01 +08:00
|
|
|
static __init int stack_trace_init(void)
|
|
|
|
{
|
|
|
|
struct dentry *d_tracer;
|
|
|
|
|
|
|
|
d_tracer = tracing_init_dentry();
|
2013-04-10 08:18:12 +08:00
|
|
|
if (!d_tracer)
|
|
|
|
return 0;
|
2008-08-28 11:31:01 +08:00
|
|
|
|
2009-03-27 07:25:38 +08:00
|
|
|
trace_create_file("stack_max_size", 0644, d_tracer,
|
|
|
|
&max_stack_size, &stack_max_size_fops);
|
2008-08-28 11:31:01 +08:00
|
|
|
|
2009-03-27 07:25:38 +08:00
|
|
|
trace_create_file("stack_trace", 0444, d_tracer,
|
|
|
|
NULL, &stack_trace_fops);
|
2008-08-28 11:31:01 +08:00
|
|
|
|
2011-12-20 03:44:09 +08:00
|
|
|
trace_create_file("stack_trace_filter", 0444, d_tracer,
|
|
|
|
NULL, &stack_trace_filter_fops);
|
|
|
|
|
2011-12-20 11:01:00 +08:00
|
|
|
if (stack_trace_filter_buf[0])
|
|
|
|
ftrace_set_early_filter(&trace_ops, stack_trace_filter_buf, 1);
|
|
|
|
|
2008-12-17 22:43:00 +08:00
|
|
|
if (stack_tracer_enabled)
|
2008-12-17 12:06:40 +08:00
|
|
|
register_ftrace_function(&trace_ops);
|
2008-08-28 11:31:01 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
device_initcall(stack_trace_init);
|