Skip to content

Commit a871bd3

Browse files
jibaronfweisbec
authored andcommitted
tracing: Add syscall tracepoints
add two tracepoints in syscall exit and entry path, conditioned on TIF_SYSCALL_FTRACE. Supports the syscall trace event code. Signed-off-by: Jason Baron <jbaron@redhat.com> Cc: Lai Jiangshan <laijs@cn.fujitsu.com> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> Cc: Jiaying Zhang <jiayingz@google.com> Cc: Martin Bligh <mbligh@google.com> Cc: Li Zefan <lizf@cn.fujitsu.com> Cc: Masami Hiramatsu <mhiramat@redhat.com> Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com>
1 parent 63fbdab commit a871bd3

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

arch/x86/kernel/ptrace.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737

3838
#include <trace/syscall.h>
3939

40+
DEFINE_TRACE(syscall_enter);
41+
DEFINE_TRACE(syscall_exit);
42+
4043
#include "tls.h"
4144

4245
enum x86_regset {
@@ -1498,7 +1501,7 @@ asmregparm long syscall_trace_enter(struct pt_regs *regs)
14981501
ret = -1L;
14991502

15001503
if (unlikely(test_thread_flag(TIF_SYSCALL_FTRACE)))
1501-
ftrace_syscall_enter(regs);
1504+
trace_syscall_enter(regs, regs->orig_ax);
15021505

15031506
if (unlikely(current->audit_context)) {
15041507
if (IS_IA32)
@@ -1524,7 +1527,7 @@ asmregparm void syscall_trace_leave(struct pt_regs *regs)
15241527
audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
15251528

15261529
if (unlikely(test_thread_flag(TIF_SYSCALL_FTRACE)))
1527-
ftrace_syscall_exit(regs);
1530+
trace_syscall_exit(regs, regs->ax);
15281531

15291532
if (test_thread_flag(TIF_SYSCALL_TRACE))
15301533
tracehook_report_syscall_exit(regs, 0);

include/trace/syscall.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
11
#ifndef _TRACE_SYSCALL_H
22
#define _TRACE_SYSCALL_H
33

4+
#include <linux/tracepoint.h>
5+
46
#include <asm/ptrace.h>
57

8+
9+
extern void syscall_regfunc(void);
10+
extern void syscall_unregfunc(void);
11+
12+
DECLARE_TRACE_WITH_CALLBACK(syscall_enter,
13+
TP_PROTO(struct pt_regs *regs, long id),
14+
TP_ARGS(regs, id),
15+
syscall_regfunc,
16+
syscall_unregfunc
17+
);
18+
19+
DECLARE_TRACE_WITH_CALLBACK(syscall_exit,
20+
TP_PROTO(struct pt_regs *regs, long ret),
21+
TP_ARGS(regs, ret),
22+
syscall_regfunc,
23+
syscall_unregfunc
24+
);
25+
626
/*
727
* A syscall entry in the ftrace syscalls array.
828
*

kernel/tracepoint.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <linux/tracepoint.h>
2525
#include <linux/err.h>
2626
#include <linux/slab.h>
27+
#include <linux/sched.h>
2728

2829
extern struct tracepoint __start___tracepoints[];
2930
extern struct tracepoint __stop___tracepoints[];
@@ -577,3 +578,40 @@ static int init_tracepoints(void)
577578
__initcall(init_tracepoints);
578579

579580
#endif /* CONFIG_MODULES */
581+
582+
static DEFINE_MUTEX(regfunc_mutex);
583+
static int sys_tracepoint_refcount;
584+
585+
void syscall_regfunc(void)
586+
{
587+
unsigned long flags;
588+
struct task_struct *g, *t;
589+
590+
mutex_lock(&regfunc_mutex);
591+
if (!sys_tracepoint_refcount) {
592+
read_lock_irqsave(&tasklist_lock, flags);
593+
do_each_thread(g, t) {
594+
set_tsk_thread_flag(t, TIF_SYSCALL_FTRACE);
595+
} while_each_thread(g, t);
596+
read_unlock_irqrestore(&tasklist_lock, flags);
597+
}
598+
sys_tracepoint_refcount++;
599+
mutex_unlock(&regfunc_mutex);
600+
}
601+
602+
void syscall_unregfunc(void)
603+
{
604+
unsigned long flags;
605+
struct task_struct *g, *t;
606+
607+
mutex_lock(&regfunc_mutex);
608+
sys_tracepoint_refcount--;
609+
if (!sys_tracepoint_refcount) {
610+
read_lock_irqsave(&tasklist_lock, flags);
611+
do_each_thread(g, t) {
612+
clear_tsk_thread_flag(t, TIF_SYSCALL_FTRACE);
613+
} while_each_thread(g, t);
614+
read_unlock_irqrestore(&tasklist_lock, flags);
615+
}
616+
mutex_unlock(&regfunc_mutex);
617+
}

0 commit comments

Comments
 (0)