Skip to content

Commit 591dffd

Browse files
committed
ftrace: Allow for function tracing instance to filter functions
Create a "set_ftrace_filter" and "set_ftrace_notrace" files in the instance directories to let users filter of functions to trace for the given instance. Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
1 parent e3b3e2e commit 591dffd

5 files changed

Lines changed: 96 additions & 14 deletions

File tree

‎include/linux/ftrace.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ typedef void (*ftrace_func_t)(unsigned long ip, unsigned long parent_ip,
9292
* STUB - The ftrace_ops is just a place holder.
9393
* INITIALIZED - The ftrace_ops has already been initialized (first use time
9494
* register_ftrace_function() is called, it will initialized the ops)
95+
* DELETED - The ops are being deleted, do not let them be registered again.
9596
*/
9697
enum {
9798
FTRACE_OPS_FL_ENABLED = 1 << 0,
@@ -103,6 +104,7 @@ enum {
103104
FTRACE_OPS_FL_RECURSION_SAFE = 1 << 6,
104105
FTRACE_OPS_FL_STUB = 1 << 7,
105106
FTRACE_OPS_FL_INITIALIZED = 1 << 8,
107+
FTRACE_OPS_FL_DELETED = 1 << 9,
106108
};
107109

108110
/*

‎kernel/trace/ftrace.c‎

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,9 @@ static int remove_ftrace_list_ops(struct ftrace_ops **list,
436436

437437
static int __register_ftrace_function(struct ftrace_ops *ops)
438438
{
439+
if (ops->flags & FTRACE_OPS_FL_DELETED)
440+
return -EINVAL;
441+
439442
if (FTRACE_WARN_ON(ops == &global_ops))
440443
return -EINVAL;
441444

@@ -4112,6 +4115,36 @@ static const struct file_operations ftrace_graph_notrace_fops = {
41124115
};
41134116
#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
41144117

4118+
void ftrace_create_filter_files(struct ftrace_ops *ops,
4119+
struct dentry *parent)
4120+
{
4121+
4122+
trace_create_file("set_ftrace_filter", 0644, parent,
4123+
ops, &ftrace_filter_fops);
4124+
4125+
trace_create_file("set_ftrace_notrace", 0644, parent,
4126+
ops, &ftrace_notrace_fops);
4127+
}
4128+
4129+
/*
4130+
* The name "destroy_filter_files" is really a misnomer. Although
4131+
* in the future, it may actualy delete the files, but this is
4132+
* really intended to make sure the ops passed in are disabled
4133+
* and that when this function returns, the caller is free to
4134+
* free the ops.
4135+
*
4136+
* The "destroy" name is only to match the "create" name that this
4137+
* should be paired with.
4138+
*/
4139+
void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4140+
{
4141+
mutex_lock(&ftrace_lock);
4142+
if (ops->flags & FTRACE_OPS_FL_ENABLED)
4143+
ftrace_shutdown(ops, 0);
4144+
ops->flags |= FTRACE_OPS_FL_DELETED;
4145+
mutex_unlock(&ftrace_lock);
4146+
}
4147+
41154148
static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
41164149
{
41174150

@@ -4121,11 +4154,7 @@ static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
41214154
trace_create_file("enabled_functions", 0444,
41224155
d_tracer, NULL, &ftrace_enabled_fops);
41234156

4124-
trace_create_file("set_ftrace_filter", 0644, d_tracer,
4125-
&global_ops, &ftrace_filter_fops);
4126-
4127-
trace_create_file("set_ftrace_notrace", 0644, d_tracer,
4128-
&global_ops, &ftrace_notrace_fops);
4157+
ftrace_create_filter_files(&global_ops, d_tracer);
41294158

41304159
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
41314160
trace_create_file("set_graph_function", 0444, d_tracer,

‎kernel/trace/trace.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6161,6 +6161,7 @@ static int instance_delete(const char *name)
61616161

61626162
tracing_set_nop(tr);
61636163
event_trace_del_tracer(tr);
6164+
ftrace_destroy_function_files(tr);
61646165
debugfs_remove_recursive(tr->dir);
61656166
free_percpu(tr->trace_buffer.data);
61666167
ring_buffer_free(tr->trace_buffer.buffer);
@@ -6291,6 +6292,9 @@ init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
62916292
trace_create_file("tracing_on", 0644, d_tracer,
62926293
tr, &rb_simple_fops);
62936294

6295+
if (ftrace_create_function_files(tr, d_tracer))
6296+
WARN(1, "Could not allocate function filter files");
6297+
62946298
#ifdef CONFIG_TRACER_SNAPSHOT
62956299
trace_create_file("snapshot", 0644, d_tracer,
62966300
tr, &snapshot_fops);

‎kernel/trace/trace.h‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,13 +819,36 @@ static inline int ftrace_trace_task(struct task_struct *task)
819819
return test_tsk_trace_trace(task);
820820
}
821821
extern int ftrace_is_dead(void);
822+
int ftrace_create_function_files(struct trace_array *tr,
823+
struct dentry *parent);
824+
void ftrace_destroy_function_files(struct trace_array *tr);
822825
#else
823826
static inline int ftrace_trace_task(struct task_struct *task)
824827
{
825828
return 1;
826829
}
827830
static inline int ftrace_is_dead(void) { return 0; }
828-
#endif
831+
static inline int
832+
ftrace_create_function_files(struct trace_array *tr,
833+
struct dentry *parent)
834+
{
835+
return 0;
836+
}
837+
static inline void ftrace_destroy_function_files(struct trace_array *tr) { }
838+
#endif /* CONFIG_FUNCTION_TRACER */
839+
840+
#if defined(CONFIG_FUNCTION_TRACER) && defined(CONFIG_DYNAMIC_FTRACE)
841+
void ftrace_create_filter_files(struct ftrace_ops *ops,
842+
struct dentry *parent);
843+
void ftrace_destroy_filter_files(struct ftrace_ops *ops);
844+
#else
845+
/*
846+
* The ops parameter passed in is usually undefined.
847+
* This must be a macro.
848+
*/
849+
#define ftrace_create_filter_files(ops, parent) do { } while (0)
850+
#define ftrace_destroy_filter_files(ops) do { } while (0)
851+
#endif /* CONFIG_FUNCTION_TRACER && CONFIG_DYNAMIC_FTRACE */
829852

830853
int ftrace_event_is_function(struct ftrace_event_call *call);
831854

‎kernel/trace/trace_functions.c‎

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,34 @@ static int allocate_ftrace_ops(struct trace_array *tr)
5252
return 0;
5353
}
5454

55+
56+
int ftrace_create_function_files(struct trace_array *tr,
57+
struct dentry *parent)
58+
{
59+
int ret;
60+
61+
/* The top level array uses the "global_ops". */
62+
if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL)) {
63+
ret = allocate_ftrace_ops(tr);
64+
if (ret)
65+
return ret;
66+
}
67+
68+
ftrace_create_filter_files(tr->ops, parent);
69+
70+
return 0;
71+
}
72+
73+
void ftrace_destroy_function_files(struct trace_array *tr)
74+
{
75+
ftrace_destroy_filter_files(tr->ops);
76+
kfree(tr->ops);
77+
tr->ops = NULL;
78+
}
79+
5580
static int function_trace_init(struct trace_array *tr)
5681
{
5782
struct ftrace_ops *ops;
58-
int ret;
5983

6084
if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
6185
/* There's only one global tr */
@@ -69,10 +93,13 @@ static int function_trace_init(struct trace_array *tr)
6993
else
7094
ops = &trace_ops;
7195
tr->ops = ops;
72-
} else {
73-
ret = allocate_ftrace_ops(tr);
74-
if (ret)
75-
return ret;
96+
} else if (!tr->ops) {
97+
/*
98+
* Instance trace_arrays get their ops allocated
99+
* at instance creation. Unless it failed
100+
* the allocation.
101+
*/
102+
return -ENOMEM;
76103
}
77104

78105
tr->trace_buffer.cpu = get_cpu();
@@ -87,9 +114,6 @@ static void function_trace_reset(struct trace_array *tr)
87114
{
88115
tracing_stop_function_trace(tr);
89116
tracing_stop_cmdline_record();
90-
if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL))
91-
kfree(tr->ops);
92-
tr->ops = NULL;
93117
}
94118

95119
static void function_trace_start(struct trace_array *tr)

0 commit comments

Comments
 (0)