Skip to content

Commit f38f1d2

Browse files
Steven RostedtIngo Molnar
authored andcommitted
trace: add a way to enable or disable the stack tracer
Impact: enhancement to stack tracer The stack tracer currently is either on when configured in or off when it is not. It can not be disabled when it is configured on. (besides disabling the function tracer that it uses) This patch adds a way to enable or disable the stack tracer at run time. It defaults off on bootup, but a kernel parameter 'stacktrace' has been added to enable it on bootup. A new sysctl has been added "kernel.stack_tracer_enabled" to let the user enable or disable the stack tracer at run time. Signed-off-by: Steven Rostedt <srostedt@redhat.com> Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent 4087449 commit f38f1d2

5 files changed

Lines changed: 79 additions & 8 deletions

File tree

‎Documentation/kernel-parameters.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ parameter is applicable:
8989
SPARC Sparc architecture is enabled.
9090
SWSUSP Software suspend (hibernation) is enabled.
9191
SUSPEND System suspend states are enabled.
92+
FTRACE Function tracing enabled.
9293
TS Appropriate touchscreen support is enabled.
9394
USB USB support is enabled.
9495
USBHID USB Human Interface Device support is enabled.
@@ -2173,6 +2174,9 @@ and is between 256 and 4096 characters. It is defined in the file
21732174
st= [HW,SCSI] SCSI tape parameters (buffers, etc.)
21742175
See Documentation/scsi/st.txt.
21752176

2177+
stacktrace [FTRACE]
2178+
Enabled the stack tracer on boot up.
2179+
21762180
sti= [PARISC,HW]
21772181
Format: <num>
21782182
Set the STI (builtin display/keyboard on the HP-PARISC

‎include/linux/ftrace.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ static inline void ftrace_stop(void) { }
8686
static inline void ftrace_start(void) { }
8787
#endif /* CONFIG_FUNCTION_TRACER */
8888

89+
#ifdef CONFIG_STACK_TRACER
90+
extern int stack_tracer_enabled;
91+
int
92+
stack_trace_sysctl(struct ctl_table *table, int write,
93+
struct file *file, void __user *buffer, size_t *lenp,
94+
loff_t *ppos);
95+
#endif
96+
8997
#ifdef CONFIG_DYNAMIC_FTRACE
9098
/* asm/ftrace.h must be defined for archs supporting dynamic ftrace */
9199
#include <asm/ftrace.h>

‎kernel/sysctl.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,16 @@ static struct ctl_table kern_table[] = {
487487
.proc_handler = &ftrace_enable_sysctl,
488488
},
489489
#endif
490+
#ifdef CONFIG_STACK_TRACER
491+
{
492+
.ctl_name = CTL_UNNUMBERED,
493+
.procname = "stack_tracer_enabled",
494+
.data = &stack_tracer_enabled,
495+
.maxlen = sizeof(int),
496+
.mode = 0644,
497+
.proc_handler = &stack_trace_sysctl,
498+
},
499+
#endif
490500
#ifdef CONFIG_TRACING
491501
{
492502
.ctl_name = CTL_UNNUMBERED,

‎kernel/trace/Kconfig‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,15 @@ config STACK_TRACER
244244

245245
This tracer works by hooking into every function call that the
246246
kernel executes, and keeping a maximum stack depth value and
247-
stack-trace saved. Because this logic has to execute in every
248-
kernel function, all the time, this option can slow down the
249-
kernel measurably and is generally intended for kernel
250-
developers only.
247+
stack-trace saved. If this is configured with DYNAMIC_FTRACE
248+
then it will not have any overhead while the stack tracer
249+
is disabled.
250+
251+
To enable the stack tracer on bootup, pass in 'stacktrace'
252+
on the kernel command line.
253+
254+
The stack tracer can also be enabled or disabled via the
255+
sysctl kernel.stack_tracer_enabled
251256

252257
Say N if unsure.
253258

‎kernel/trace/trace_stack.c‎

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/debugfs.h>
1111
#include <linux/ftrace.h>
1212
#include <linux/module.h>
13+
#include <linux/sysctl.h>
1314
#include <linux/init.h>
1415
#include <linux/fs.h>
1516
#include "trace.h"
@@ -31,6 +32,10 @@ static raw_spinlock_t max_stack_lock =
3132

3233
static int stack_trace_disabled __read_mostly;
3334
static DEFINE_PER_CPU(int, trace_active);
35+
static DEFINE_MUTEX(stack_sysctl_mutex);
36+
37+
int stack_tracer_enabled;
38+
static int last_stack_tracer_enabled;
3439

3540
static inline void check_stack(void)
3641
{
@@ -174,7 +179,7 @@ stack_max_size_write(struct file *filp, const char __user *ubuf,
174179
return count;
175180
}
176181

177-
static struct file_operations stack_max_size_fops = {
182+
static const struct file_operations stack_max_size_fops = {
178183
.open = tracing_open_generic,
179184
.read = stack_max_size_read,
180185
.write = stack_max_size_write,
@@ -272,7 +277,7 @@ static int t_show(struct seq_file *m, void *v)
272277
return 0;
273278
}
274279

275-
static struct seq_operations stack_trace_seq_ops = {
280+
static const struct seq_operations stack_trace_seq_ops = {
276281
.start = t_start,
277282
.next = t_next,
278283
.stop = t_stop,
@@ -288,12 +293,48 @@ static int stack_trace_open(struct inode *inode, struct file *file)
288293
return ret;
289294
}
290295

291-
static struct file_operations stack_trace_fops = {
296+
static const struct file_operations stack_trace_fops = {
292297
.open = stack_trace_open,
293298
.read = seq_read,
294299
.llseek = seq_lseek,
295300
};
296301

302+
int
303+
stack_trace_sysctl(struct ctl_table *table, int write,
304+
struct file *file, void __user *buffer, size_t *lenp,
305+
loff_t *ppos)
306+
{
307+
int ret;
308+
309+
mutex_lock(&stack_sysctl_mutex);
310+
311+
ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
312+
313+
if (ret || !write ||
314+
(last_stack_tracer_enabled == stack_tracer_enabled))
315+
goto out;
316+
317+
last_stack_tracer_enabled = stack_tracer_enabled;
318+
319+
if (stack_tracer_enabled)
320+
register_ftrace_function(&trace_ops);
321+
else
322+
unregister_ftrace_function(&trace_ops);
323+
324+
out:
325+
mutex_unlock(&stack_sysctl_mutex);
326+
return ret;
327+
}
328+
329+
static int start_stack_trace __initdata;
330+
331+
static __init int enable_stacktrace(char *str)
332+
{
333+
start_stack_trace = 1;
334+
return 1;
335+
}
336+
__setup("stacktrace", enable_stacktrace);
337+
297338
static __init int stack_trace_init(void)
298339
{
299340
struct dentry *d_tracer;
@@ -311,7 +352,10 @@ static __init int stack_trace_init(void)
311352
if (!entry)
312353
pr_warning("Could not create debugfs 'stack_trace' entry\n");
313354

314-
register_ftrace_function(&trace_ops);
355+
if (start_stack_trace) {
356+
register_ftrace_function(&trace_ops);
357+
stack_tracer_enabled = 1;
358+
}
315359

316360
return 0;
317361
}

0 commit comments

Comments
 (0)