Skip to content

Commit 1e10486

Browse files
namhyungrostedt
authored andcommitted
ftrace: Add 'function-fork' trace option
The function-fork option is same as event-fork that it tracks task fork/exit and set the pid filter properly. This can be useful if user wants to trace selected tasks including their children only. Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Namhyung Kim <[email protected]> Signed-off-by: Steven Rostedt (VMware) <[email protected]>
1 parent b980b11 commit 1e10486

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

kernel/trace/ftrace.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5600,6 +5600,43 @@ ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
56005600
trace_ignore_this_task(pid_list, next));
56015601
}
56025602

5603+
static void
5604+
ftrace_pid_follow_sched_process_fork(void *data,
5605+
struct task_struct *self,
5606+
struct task_struct *task)
5607+
{
5608+
struct trace_pid_list *pid_list;
5609+
struct trace_array *tr = data;
5610+
5611+
pid_list = rcu_dereference_sched(tr->function_pids);
5612+
trace_filter_add_remove_task(pid_list, self, task);
5613+
}
5614+
5615+
static void
5616+
ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
5617+
{
5618+
struct trace_pid_list *pid_list;
5619+
struct trace_array *tr = data;
5620+
5621+
pid_list = rcu_dereference_sched(tr->function_pids);
5622+
trace_filter_add_remove_task(pid_list, NULL, task);
5623+
}
5624+
5625+
void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
5626+
{
5627+
if (enable) {
5628+
register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
5629+
tr);
5630+
register_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
5631+
tr);
5632+
} else {
5633+
unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
5634+
tr);
5635+
unregister_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
5636+
tr);
5637+
}
5638+
}
5639+
56035640
static void clear_ftrace_pids(struct trace_array *tr)
56045641
{
56055642
struct trace_pid_list *pid_list;

kernel/trace/trace.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ unsigned long long ns2usecs(u64 nsec)
257257

258258
/* trace_flags that are default zero for instances */
259259
#define ZEROED_TRACE_FLAGS \
260-
TRACE_ITER_EVENT_FORK
260+
(TRACE_ITER_EVENT_FORK | TRACE_ITER_FUNC_FORK)
261261

262262
/*
263263
* The global_trace is the descriptor that holds the top-level tracing
@@ -4205,6 +4205,9 @@ int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
42054205
if (mask == TRACE_ITER_EVENT_FORK)
42064206
trace_event_follow_fork(tr, enabled);
42074207

4208+
if (mask == TRACE_ITER_FUNC_FORK)
4209+
ftrace_pid_follow_fork(tr, enabled);
4210+
42084211
if (mask == TRACE_ITER_OVERWRITE) {
42094212
ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
42104213
#ifdef CONFIG_TRACER_MAX_TRACE

kernel/trace/trace.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,7 @@ void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer);
897897
void ftrace_init_tracefs_toplevel(struct trace_array *tr,
898898
struct dentry *d_tracer);
899899
int init_function_trace(void);
900+
void ftrace_pid_follow_fork(struct trace_array *tr, bool enable);
900901
#else
901902
static inline int ftrace_trace_task(struct trace_array *tr)
902903
{
@@ -916,6 +917,7 @@ static inline void ftrace_reset_array_ops(struct trace_array *tr) { }
916917
static inline void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d) { }
917918
static inline void ftrace_init_tracefs_toplevel(struct trace_array *tr, struct dentry *d) { }
918919
static inline int init_function_trace(void) { return 0; }
920+
static inline void ftrace_pid_follow_fork(struct trace_array *tr, bool enable) { }
919921
/* ftace_func_t type is not defined, use macro instead of static inline */
920922
#define ftrace_init_array_ops(tr, func) do { } while (0)
921923
#endif /* CONFIG_FUNCTION_TRACER */
@@ -989,11 +991,13 @@ extern int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
989991

990992
#ifdef CONFIG_FUNCTION_TRACER
991993
# define FUNCTION_FLAGS \
992-
C(FUNCTION, "function-trace"),
994+
C(FUNCTION, "function-trace"), \
995+
C(FUNC_FORK, "function-fork"),
993996
# define FUNCTION_DEFAULT_FLAGS TRACE_ITER_FUNCTION
994997
#else
995998
# define FUNCTION_FLAGS
996999
# define FUNCTION_DEFAULT_FLAGS 0UL
1000+
# define TRACE_ITER_FUNC_FORK 0UL
9971001
#endif
9981002

9991003
#ifdef CONFIG_STACKTRACE

0 commit comments

Comments
 (0)