2008-08-28 05:31:01 +02: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 05:06:40 +01:00
|
|
|
#include <linux/sysctl.h>
|
2008-08-28 05:31:01 +02:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/fs.h>
|
2011-12-20 04:01:00 +01:00
|
|
|
|
|
|
|
#include <asm/setup.h>
|
|
|
|
|
2008-08-28 05:31:01 +02:00
|
|
|
#include "trace.h"
|
|
|
|
|
|
|
|
#define STACK_TRACE_ENTRIES 500
|
|
|
|
|
2008-08-29 22:51:43 +02: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];
|
|
|
|
|
2008-08-28 05:31:01 +02:00
|
|
|
static struct stack_trace max_stack_trace = {
|
|
|
|
.max_entries = STACK_TRACE_ENTRIES,
|
|
|
|
.entries = stack_dump_trace,
|
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned long max_stack_size;
|
2009-12-02 19:49:50 +01:00
|
|
|
static arch_spinlock_t max_stack_lock =
|
2009-12-03 12:38:57 +01:00
|
|
|
(arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
|
2008-08-28 05:31:01 +02:00
|
|
|
|
|
|
|
static int stack_trace_disabled __read_mostly;
|
|
|
|
static DEFINE_PER_CPU(int, trace_active);
|
2008-12-17 05:06:40 +01:00
|
|
|
static DEFINE_MUTEX(stack_sysctl_mutex);
|
|
|
|
|
|
|
|
int stack_tracer_enabled;
|
|
|
|
static int last_stack_tracer_enabled;
|
2008-08-28 05:31:01 +02:00
|
|
|
|
|
|
|
static inline void check_stack(void)
|
|
|
|
{
|
2008-08-29 22:51:43 +02:00
|
|
|
unsigned long this_size, flags;
|
|
|
|
unsigned long *p, *top, *start;
|
|
|
|
int i;
|
2008-08-28 05:31:01 +02:00
|
|
|
|
|
|
|
this_size = ((unsigned long)&this_size) & (THREAD_SIZE-1);
|
|
|
|
this_size = THREAD_SIZE - this_size;
|
|
|
|
|
|
|
|
if (this_size <= max_stack_size)
|
|
|
|
return;
|
|
|
|
|
2008-10-07 03:24:18 +02:00
|
|
|
/* we do not handle interrupt stacks yet */
|
|
|
|
if (!object_is_on_stack(&this_size))
|
|
|
|
return;
|
|
|
|
|
2008-12-02 21:34:05 +01:00
|
|
|
local_irq_save(flags);
|
2009-12-02 20:01:25 +01:00
|
|
|
arch_spin_lock(&max_stack_lock);
|
2008-08-28 05:31:01 +02:00
|
|
|
|
|
|
|
/* a race could have already updated it */
|
|
|
|
if (this_size <= max_stack_size)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
max_stack_size = this_size;
|
|
|
|
|
|
|
|
max_stack_trace.nr_entries = 0;
|
2008-08-29 22:51:43 +02:00
|
|
|
max_stack_trace.skip = 3;
|
2008-08-28 05:31:01 +02:00
|
|
|
|
|
|
|
save_stack_trace(&max_stack_trace);
|
|
|
|
|
2008-08-29 22:51:43 +02:00
|
|
|
/*
|
|
|
|
* Now find where in the stack these are.
|
|
|
|
*/
|
|
|
|
i = 0;
|
|
|
|
start = &this_size;
|
|
|
|
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-03 17:04:50 +01:00
|
|
|
int found = 0;
|
2008-08-29 22:51:43 +02: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-03 17:04:50 +01:00
|
|
|
found = 1;
|
2008-08-29 22:51:43 +02:00
|
|
|
/* Start the search from here */
|
|
|
|
start = p + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-03 17:04:50 +01:00
|
|
|
if (!found)
|
|
|
|
i++;
|
2008-08-29 22:51:43 +02:00
|
|
|
}
|
|
|
|
|
2008-08-28 05:31:01 +02:00
|
|
|
out:
|
2009-12-02 20:01:25 +01:00
|
|
|
arch_spin_unlock(&max_stack_lock);
|
2008-12-02 21:34:05 +01:00
|
|
|
local_irq_restore(flags);
|
2008-08-28 05:31:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
stack_trace_call(unsigned long ip, unsigned long parent_ip)
|
|
|
|
{
|
2010-06-03 15:36:50 +02:00
|
|
|
int cpu;
|
2008-08-28 05:31:01 +02:00
|
|
|
|
|
|
|
if (unlikely(!ftrace_enabled || stack_trace_disabled))
|
|
|
|
return;
|
|
|
|
|
2010-06-03 15:36:50 +02:00
|
|
|
preempt_disable_notrace();
|
2008-08-28 05:31:01 +02: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;
|
|
|
|
|
|
|
|
check_stack();
|
|
|
|
|
|
|
|
out:
|
|
|
|
per_cpu(trace_active, cpu)--;
|
|
|
|
/* prevent recursion in schedule */
|
2010-06-03 15:36:50 +02:00
|
|
|
preempt_enable_notrace();
|
2008-08-28 05:31:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct ftrace_ops trace_ops __read_mostly =
|
|
|
|
{
|
|
|
|
.func = stack_trace_call,
|
|
|
|
};
|
|
|
|
|
|
|
|
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 08:32:09 +01:00
|
|
|
int cpu;
|
2008-08-28 05:31:01 +02:00
|
|
|
|
2011-06-07 21:58:27 +02:00
|
|
|
ret = kstrtoul_from_user(ubuf, count, 10, &val);
|
|
|
|
if (ret)
|
2008-08-28 05:31:01 +02:00
|
|
|
return ret;
|
|
|
|
|
2008-12-02 21:34:05 +01:00
|
|
|
local_irq_save(flags);
|
2010-02-02 08:32:09 +01: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-02 20:01:25 +01:00
|
|
|
arch_spin_lock(&max_stack_lock);
|
2008-08-28 05:31:01 +02:00
|
|
|
*ptr = val;
|
2009-12-02 20:01:25 +01:00
|
|
|
arch_spin_unlock(&max_stack_lock);
|
2010-02-02 08:32:09 +01:00
|
|
|
|
|
|
|
per_cpu(trace_active, cpu)--;
|
2008-12-02 21:34:05 +01:00
|
|
|
local_irq_restore(flags);
|
2008-08-28 05:31:01 +02:00
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2008-12-17 05:06:40 +01:00
|
|
|
static const struct file_operations stack_max_size_fops = {
|
2008-08-28 05:31:01 +02: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-15 18:52:59 +02:00
|
|
|
.llseek = default_llseek,
|
2008-08-28 05:31:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void *
|
2009-08-17 10:53:37 +02:00
|
|
|
__next(struct seq_file *m, loff_t *pos)
|
2008-08-28 05:31:01 +02:00
|
|
|
{
|
2009-08-17 10:53:37 +02:00
|
|
|
long n = *pos - 1;
|
2008-08-28 05:31:01 +02:00
|
|
|
|
2009-08-17 10:53:37 +02:00
|
|
|
if (n >= max_stack_trace.nr_entries || stack_dump_trace[n] == ULONG_MAX)
|
2008-08-28 05:31:01 +02:00
|
|
|
return NULL;
|
|
|
|
|
2009-08-17 10:53:37 +02:00
|
|
|
m->private = (void *)n;
|
2008-08-29 22:51:43 +02:00
|
|
|
return &m->private;
|
2008-08-28 05:31:01 +02:00
|
|
|
}
|
|
|
|
|
2009-08-17 10:53:37 +02:00
|
|
|
static void *
|
|
|
|
t_next(struct seq_file *m, void *v, loff_t *pos)
|
2008-08-28 05:31:01 +02:00
|
|
|
{
|
2009-08-17 10:53:37 +02:00
|
|
|
(*pos)++;
|
|
|
|
return __next(m, pos);
|
|
|
|
}
|
2008-08-28 05:31:01 +02:00
|
|
|
|
2009-08-17 10:53:37 +02:00
|
|
|
static void *t_start(struct seq_file *m, loff_t *pos)
|
|
|
|
{
|
2010-02-02 08:32:09 +01:00
|
|
|
int cpu;
|
|
|
|
|
2008-08-28 05:31:01 +02:00
|
|
|
local_irq_disable();
|
2010-02-02 08:32:09 +01:00
|
|
|
|
|
|
|
cpu = smp_processor_id();
|
|
|
|
per_cpu(trace_active, cpu)++;
|
|
|
|
|
2009-12-02 20:01:25 +01:00
|
|
|
arch_spin_lock(&max_stack_lock);
|
2008-08-28 05:31:01 +02: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 04:00:18 +01:00
|
|
|
if (*pos == 0)
|
|
|
|
return SEQ_START_TOKEN;
|
|
|
|
|
2009-08-17 10:53:37 +02:00
|
|
|
return __next(m, pos);
|
2008-08-28 05:31:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void t_stop(struct seq_file *m, void *p)
|
|
|
|
{
|
2010-02-02 08:32:09 +01:00
|
|
|
int cpu;
|
|
|
|
|
2009-12-02 20:01:25 +01:00
|
|
|
arch_spin_unlock(&max_stack_lock);
|
2010-02-02 08:32:09 +01:00
|
|
|
|
|
|
|
cpu = smp_processor_id();
|
|
|
|
per_cpu(trace_active, cpu)--;
|
|
|
|
|
2008-08-28 05:31:01 +02:00
|
|
|
local_irq_enable();
|
|
|
|
}
|
|
|
|
|
2008-08-29 22:51:43 +02:00
|
|
|
static int trace_lookup_stack(struct seq_file *m, long i)
|
2008-08-28 05:31:01 +02:00
|
|
|
{
|
2008-08-29 22:51:43 +02:00
|
|
|
unsigned long addr = stack_dump_trace[i];
|
2008-08-28 05:31:01 +02:00
|
|
|
|
2010-08-25 03:32:38 +02:00
|
|
|
return seq_printf(m, "%pS\n", (void *)addr);
|
2008-08-28 05:31:01 +02:00
|
|
|
}
|
|
|
|
|
2009-03-13 00:42:29 +01: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 05:31:01 +02: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 04:00:18 +01:00
|
|
|
long i;
|
2008-08-29 22:51:43 +02: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 04:00:18 +01:00
|
|
|
if (v == SEQ_START_TOKEN) {
|
2009-03-13 05:00:58 +01:00
|
|
|
seq_printf(m, " Depth Size Location"
|
2008-08-29 22:51:43 +02:00
|
|
|
" (%d entries)\n"
|
2009-03-13 05:00:58 +01:00
|
|
|
" ----- ---- --------\n",
|
2009-06-03 10:01:28 +02:00
|
|
|
max_stack_trace.nr_entries - 1);
|
2009-03-13 00:42:29 +01:00
|
|
|
|
|
|
|
if (!stack_tracer_enabled && !max_stack_size)
|
|
|
|
print_disabled(m);
|
|
|
|
|
2008-08-29 22:51:43 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2008-08-28 05:31:01 +02: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 04:00:18 +01:00
|
|
|
i = *(long *)v;
|
|
|
|
|
2008-08-29 22:51:43 +02:00
|
|
|
if (i >= max_stack_trace.nr_entries ||
|
|
|
|
stack_dump_trace[i] == ULONG_MAX)
|
2008-08-28 05:31:01 +02:00
|
|
|
return 0;
|
|
|
|
|
2008-08-29 22:51:43 +02: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 05:31:01 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-17 05:06:40 +01:00
|
|
|
static const struct seq_operations stack_trace_seq_ops = {
|
2008-08-28 05:31:01 +02: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 05:28:40 +02:00
|
|
|
return seq_open(file, &stack_trace_seq_ops);
|
2008-08-28 05:31:01 +02:00
|
|
|
}
|
|
|
|
|
2008-12-17 05:06:40 +01:00
|
|
|
static const struct file_operations stack_trace_fops = {
|
2008-08-28 05:31:01 +02:00
|
|
|
.open = stack_trace_open,
|
|
|
|
.read = seq_read,
|
|
|
|
.llseek = seq_lseek,
|
2009-07-23 05:28:40 +02:00
|
|
|
.release = seq_release,
|
2008-08-28 05:31:01 +02:00
|
|
|
};
|
|
|
|
|
2011-12-19 20:44:09 +01: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,
|
|
|
|
.llseek = ftrace_regex_lseek,
|
|
|
|
.release = ftrace_regex_release,
|
|
|
|
};
|
|
|
|
|
2008-12-17 05:06:40 +01:00
|
|
|
int
|
|
|
|
stack_trace_sysctl(struct ctl_table *table, int write,
|
2009-09-24 00:57:19 +02:00
|
|
|
void __user *buffer, size_t *lenp,
|
2008-12-17 05:06:40 +01:00
|
|
|
loff_t *ppos)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
mutex_lock(&stack_sysctl_mutex);
|
|
|
|
|
2009-09-24 00:57:19 +02:00
|
|
|
ret = proc_dointvec(table, write, buffer, lenp, ppos);
|
2008-12-17 05:06:40 +01:00
|
|
|
|
|
|
|
if (ret || !write ||
|
2009-06-26 10:55:51 +02:00
|
|
|
(last_stack_tracer_enabled == !!stack_tracer_enabled))
|
2008-12-17 05:06:40 +01:00
|
|
|
goto out;
|
|
|
|
|
2009-06-26 10:55:51 +02:00
|
|
|
last_stack_tracer_enabled = !!stack_tracer_enabled;
|
2008-12-17 05:06:40 +01: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 04:01:00 +01:00
|
|
|
static char stack_trace_filter_buf[COMMAND_LINE_SIZE+1] __initdata;
|
|
|
|
|
2008-12-17 05:06:40 +01:00
|
|
|
static __init int enable_stacktrace(char *str)
|
|
|
|
{
|
2011-12-20 04:01:00 +01:00
|
|
|
if (strncmp(str, "_filter=", 8) == 0)
|
|
|
|
strncpy(stack_trace_filter_buf, str+8, COMMAND_LINE_SIZE);
|
|
|
|
|
2008-12-17 15:43:00 +01:00
|
|
|
stack_tracer_enabled = 1;
|
|
|
|
last_stack_tracer_enabled = 1;
|
2008-12-17 05:06:40 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
__setup("stacktrace", enable_stacktrace);
|
|
|
|
|
2008-08-28 05:31:01 +02:00
|
|
|
static __init int stack_trace_init(void)
|
|
|
|
{
|
|
|
|
struct dentry *d_tracer;
|
|
|
|
|
|
|
|
d_tracer = tracing_init_dentry();
|
|
|
|
|
2009-03-27 00:25:38 +01:00
|
|
|
trace_create_file("stack_max_size", 0644, d_tracer,
|
|
|
|
&max_stack_size, &stack_max_size_fops);
|
2008-08-28 05:31:01 +02:00
|
|
|
|
2009-03-27 00:25:38 +01:00
|
|
|
trace_create_file("stack_trace", 0444, d_tracer,
|
|
|
|
NULL, &stack_trace_fops);
|
2008-08-28 05:31:01 +02:00
|
|
|
|
2011-12-19 20:44:09 +01:00
|
|
|
trace_create_file("stack_trace_filter", 0444, d_tracer,
|
|
|
|
NULL, &stack_trace_filter_fops);
|
|
|
|
|
2011-12-20 04:01:00 +01:00
|
|
|
if (stack_trace_filter_buf[0])
|
|
|
|
ftrace_set_early_filter(&trace_ops, stack_trace_filter_buf, 1);
|
|
|
|
|
2008-12-17 15:43:00 +01:00
|
|
|
if (stack_tracer_enabled)
|
2008-12-17 05:06:40 +01:00
|
|
|
register_ftrace_function(&trace_ops);
|
2008-08-28 05:31:01 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
device_initcall(stack_trace_init);
|