blob: 26c152122a424dc337764357db5bb297a40074fd [file] [log] [blame]
Roland McGrath88ac2922008-07-25 19:45:43 -07001/*
2 * Tracing hooks
3 *
Roland McGrathae6d2ed2009-09-23 15:56:53 -07004 * Copyright (C) 2008-2009 Red Hat, Inc. All rights reserved.
Roland McGrath88ac2922008-07-25 19:45:43 -07005 *
6 * This copyrighted material is made available to anyone wishing to use,
7 * modify, copy, or redistribute it subject to the terms and conditions
8 * of the GNU General Public License v.2.
9 *
10 * This file defines hook entry points called by core code where
11 * user tracing/debugging support might need to do something. These
12 * entry points are called tracehook_*(). Each hook declared below
13 * has a detailed kerneldoc comment giving the context (locking et
14 * al) from which it is called, and the meaning of its return value.
15 *
16 * Each function here typically has only one call site, so it is ok
17 * to have some nontrivial tracehook_*() inlines. In all cases, the
18 * fast path when no tracing is enabled should be very short.
19 *
20 * The purpose of this file and the tracehook_* layer is to consolidate
21 * the interface that the kernel core and arch code uses to enable any
22 * user debugging or tracing facility (such as ptrace). The interfaces
23 * here are carefully documented so that maintainers of core and arch
24 * code do not need to think about the implementation details of the
25 * tracing facilities. Likewise, maintainers of the tracing code do not
26 * need to understand all the calling core or arch code in detail, just
27 * documented circumstances of each call, such as locking conditions.
28 *
29 * If the calling core code changes so that locking is different, then
30 * it is ok to change the interface documented here. The maintainer of
31 * core code changing should notify the maintainers of the tracing code
32 * that they need to work out the change.
33 *
34 * Some tracehook_*() inlines take arguments that the current tracing
35 * implementations might not necessarily use. These function signatures
36 * are chosen to pass in all the information that is on hand in the
37 * caller and might conceivably be relevant to a tracer, so that the
38 * core code won't have to be updated when tracing adds more features.
39 * If a call site changes so that some of those parameters are no longer
40 * already on hand without extra work, then the tracehook_* interface
41 * can change so there is no make-work burden on the core code. The
42 * maintainer of core code changing should notify the maintainers of the
43 * tracing code that they need to work out the change.
44 */
45
46#ifndef _LINUX_TRACEHOOK_H
47#define _LINUX_TRACEHOOK_H 1
48
49#include <linux/sched.h>
50#include <linux/ptrace.h>
Roland McGrath6341c392008-07-25 19:45:44 -070051#include <linux/security.h>
Oleg Nesterove73f8952012-05-11 10:59:07 +100052#include <linux/task_work.h>
Tejun Heob23afb92015-11-05 18:46:11 -080053#include <linux/memcontrol.h>
Roland McGrath6341c392008-07-25 19:45:44 -070054struct linux_binprm;
55
Roland McGrath283d7552008-07-25 19:45:52 -070056/*
57 * ptrace report for syscall entry and exit looks identical.
58 */
Oleg Nesterov15cab952012-03-23 15:02:39 -070059static inline int ptrace_report_syscall(struct pt_regs *regs)
Roland McGrath283d7552008-07-25 19:45:52 -070060{
Tejun Heod21142e2011-06-17 16:50:34 +020061 int ptrace = current->ptrace;
Roland McGrath283d7552008-07-25 19:45:52 -070062
63 if (!(ptrace & PT_PTRACED))
Oleg Nesterov15cab952012-03-23 15:02:39 -070064 return 0;
Roland McGrath283d7552008-07-25 19:45:52 -070065
66 ptrace_notify(SIGTRAP | ((ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
67
68 /*
69 * this isn't the same as continuing with a signal, but it will do
70 * for normal use. strace only continues with a signal if the
71 * stopping signal is not SIGTRAP. -brl
72 */
73 if (current->exit_code) {
74 send_sig(current->exit_code, current, 1);
75 current->exit_code = 0;
76 }
Oleg Nesterov15cab952012-03-23 15:02:39 -070077
78 return fatal_signal_pending(current);
Roland McGrath283d7552008-07-25 19:45:52 -070079}
80
81/**
82 * tracehook_report_syscall_entry - task is about to attempt a system call
83 * @regs: user register state of current task
84 *
85 * This will be called if %TIF_SYSCALL_TRACE has been set, when the
86 * current task has just entered the kernel for a system call.
87 * Full user register state is available here. Changing the values
88 * in @regs can affect the system call number and arguments to be tried.
89 * It is safe to block here, preventing the system call from beginning.
90 *
91 * Returns zero normally, or nonzero if the calling arch code should abort
92 * the system call. That must prevent normal entry so no system call is
93 * made. If @task ever returns to user mode after this, its register state
94 * is unspecified, but should be something harmless like an %ENOSYS error
Roland McGrath828c3652008-07-25 19:45:57 -070095 * return. It should preserve enough information so that syscall_rollback()
96 * can work (see asm-generic/syscall.h).
Roland McGrath283d7552008-07-25 19:45:52 -070097 *
98 * Called without locks, just after entering kernel mode.
99 */
100static inline __must_check int tracehook_report_syscall_entry(
101 struct pt_regs *regs)
102{
Oleg Nesterov15cab952012-03-23 15:02:39 -0700103 return ptrace_report_syscall(regs);
Roland McGrath283d7552008-07-25 19:45:52 -0700104}
105
106/**
107 * tracehook_report_syscall_exit - task has just finished a system call
108 * @regs: user register state of current task
109 * @step: nonzero if simulating single-step or block-step
110 *
111 * This will be called if %TIF_SYSCALL_TRACE has been set, when the
112 * current task has just finished an attempted system call. Full
113 * user register state is available here. It is safe to block here,
114 * preventing signals from being processed.
115 *
116 * If @step is nonzero, this report is also in lieu of the normal
117 * trap that would follow the system call instruction because
118 * user_enable_block_step() or user_enable_single_step() was used.
119 * In this case, %TIF_SYSCALL_TRACE might not be set.
120 *
121 * Called without locks, just before checking for pending signals.
122 */
123static inline void tracehook_report_syscall_exit(struct pt_regs *regs, int step)
124{
Oleg Nesterov2f0edac2009-12-15 16:47:19 -0800125 if (step) {
126 siginfo_t info;
127 user_single_step_siginfo(current, regs, &info);
128 force_sig_info(SIGTRAP, &info, current);
129 return;
130 }
131
Roland McGrath283d7552008-07-25 19:45:52 -0700132 ptrace_report_syscall(regs);
133}
134
Roland McGrathfa8e26c2008-07-25 19:45:50 -0700135/**
Roland McGrathc45aea22008-07-25 19:45:50 -0700136 * tracehook_signal_handler - signal handler setup is complete
Roland McGrathc45aea22008-07-25 19:45:50 -0700137 * @stepping: nonzero if debugger single-step or block-step in use
138 *
139 * Called by the arch code after a signal handler has been set up.
140 * Register and stack state reflects the user handler about to run.
141 * Signal mask changes have already been made.
142 *
143 * Called without locks, shortly before returning to user mode
144 * (or handling more signals).
145 */
Richard Weinbergerdf5601f2013-10-07 15:37:19 +0200146static inline void tracehook_signal_handler(int stepping)
Roland McGrathc45aea22008-07-25 19:45:50 -0700147{
148 if (stepping)
149 ptrace_notify(SIGTRAP);
150}
151
Roland McGrath64b1208d2008-07-25 19:45:56 -0700152/**
153 * set_notify_resume - cause tracehook_notify_resume() to be called
154 * @task: task that will call tracehook_notify_resume()
155 *
156 * Calling this arranges that @task will call tracehook_notify_resume()
157 * before returning to user mode. If it's already running in user mode,
158 * it will enter the kernel and call tracehook_notify_resume() soon.
159 * If it's blocked, it will not be woken.
160 */
161static inline void set_notify_resume(struct task_struct *task)
162{
Oleg Nesterove73f8952012-05-11 10:59:07 +1000163#ifdef TIF_NOTIFY_RESUME
Roland McGrath64b1208d2008-07-25 19:45:56 -0700164 if (!test_and_set_tsk_thread_flag(task, TIF_NOTIFY_RESUME))
165 kick_process(task);
Oleg Nesterove73f8952012-05-11 10:59:07 +1000166#endif
Roland McGrath64b1208d2008-07-25 19:45:56 -0700167}
168
169/**
170 * tracehook_notify_resume - report when about to return to user mode
171 * @regs: user-mode registers of @current task
172 *
173 * This is called when %TIF_NOTIFY_RESUME has been set. Now we are
174 * about to return to user mode, and the user state in @regs can be
175 * inspected or adjusted. The caller in arch code has cleared
176 * %TIF_NOTIFY_RESUME before the call. If the flag gets set again
177 * asynchronously, this will be called again before we return to
178 * user mode.
179 *
180 * Called without locks.
181 */
182static inline void tracehook_notify_resume(struct pt_regs *regs)
183{
Oleg Nesterove73f8952012-05-11 10:59:07 +1000184 /*
185 * The caller just cleared TIF_NOTIFY_RESUME. This barrier
186 * pairs with task_work_add()->set_notify_resume() after
187 * hlist_add_head(task->task_works);
188 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100189 smp_mb__after_atomic();
Al Viro158e1642012-06-27 09:24:13 +0400190 if (unlikely(current->task_works))
Oleg Nesterove73f8952012-05-11 10:59:07 +1000191 task_work_run();
Tejun Heob23afb92015-11-05 18:46:11 -0800192
193 mem_cgroup_handle_over_high();
Roland McGrath64b1208d2008-07-25 19:45:56 -0700194}
Roland McGrath64b1208d2008-07-25 19:45:56 -0700195
Roland McGrath88ac2922008-07-25 19:45:43 -0700196#endif /* <linux/tracehook.h> */