blob: 09d678433fc02ad787774775afb5101c9898a7a3 [file] [log] [blame]
Thomas Gleixner2522fe42019-05-28 09:57:20 -07001/* SPDX-License-Identifier: GPL-2.0-only */
Roland McGrath88ac2922008-07-25 19:45:43 -07002/*
3 * Tracing hooks
4 *
Roland McGrathae6d2ed2009-09-23 15:56:53 -07005 * Copyright (C) 2008-2009 Red Hat, Inc. All rights reserved.
Roland McGrath88ac2922008-07-25 19:45:43 -07006 *
Roland McGrath88ac2922008-07-25 19:45:43 -07007 * This file defines hook entry points called by core code where
8 * user tracing/debugging support might need to do something. These
9 * entry points are called tracehook_*(). Each hook declared below
10 * has a detailed kerneldoc comment giving the context (locking et
11 * al) from which it is called, and the meaning of its return value.
12 *
13 * Each function here typically has only one call site, so it is ok
14 * to have some nontrivial tracehook_*() inlines. In all cases, the
15 * fast path when no tracing is enabled should be very short.
16 *
17 * The purpose of this file and the tracehook_* layer is to consolidate
18 * the interface that the kernel core and arch code uses to enable any
19 * user debugging or tracing facility (such as ptrace). The interfaces
20 * here are carefully documented so that maintainers of core and arch
21 * code do not need to think about the implementation details of the
22 * tracing facilities. Likewise, maintainers of the tracing code do not
23 * need to understand all the calling core or arch code in detail, just
24 * documented circumstances of each call, such as locking conditions.
25 *
26 * If the calling core code changes so that locking is different, then
27 * it is ok to change the interface documented here. The maintainer of
28 * core code changing should notify the maintainers of the tracing code
29 * that they need to work out the change.
30 *
31 * Some tracehook_*() inlines take arguments that the current tracing
32 * implementations might not necessarily use. These function signatures
33 * are chosen to pass in all the information that is on hand in the
34 * caller and might conceivably be relevant to a tracer, so that the
35 * core code won't have to be updated when tracing adds more features.
36 * If a call site changes so that some of those parameters are no longer
37 * already on hand without extra work, then the tracehook_* interface
38 * can change so there is no make-work burden on the core code. The
39 * maintainer of core code changing should notify the maintainers of the
40 * tracing code that they need to work out the change.
41 */
42
43#ifndef _LINUX_TRACEHOOK_H
44#define _LINUX_TRACEHOOK_H 1
45
46#include <linux/sched.h>
47#include <linux/ptrace.h>
Roland McGrath6341c392008-07-25 19:45:44 -070048#include <linux/security.h>
Oleg Nesterove73f8952012-05-11 10:59:07 +100049#include <linux/task_work.h>
Tejun Heob23afb92015-11-05 18:46:11 -080050#include <linux/memcontrol.h>
Josef Bacikd09d8df2018-07-03 11:14:55 -040051#include <linux/blk-cgroup.h>
Roland McGrath6341c392008-07-25 19:45:44 -070052struct linux_binprm;
53
Roland McGrath283d7552008-07-25 19:45:52 -070054/*
55 * ptrace report for syscall entry and exit looks identical.
56 */
Oleg Nesterov15cab952012-03-23 15:02:39 -070057static inline int ptrace_report_syscall(struct pt_regs *regs)
Roland McGrath283d7552008-07-25 19:45:52 -070058{
Tejun Heod21142e2011-06-17 16:50:34 +020059 int ptrace = current->ptrace;
Roland McGrath283d7552008-07-25 19:45:52 -070060
61 if (!(ptrace & PT_PTRACED))
Oleg Nesterov15cab952012-03-23 15:02:39 -070062 return 0;
Roland McGrath283d7552008-07-25 19:45:52 -070063
64 ptrace_notify(SIGTRAP | ((ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
65
66 /*
67 * this isn't the same as continuing with a signal, but it will do
68 * for normal use. strace only continues with a signal if the
69 * stopping signal is not SIGTRAP. -brl
70 */
71 if (current->exit_code) {
72 send_sig(current->exit_code, current, 1);
73 current->exit_code = 0;
74 }
Oleg Nesterov15cab952012-03-23 15:02:39 -070075
76 return fatal_signal_pending(current);
Roland McGrath283d7552008-07-25 19:45:52 -070077}
78
79/**
80 * tracehook_report_syscall_entry - task is about to attempt a system call
81 * @regs: user register state of current task
82 *
Elvira Khabirovad19f9132018-11-10 04:22:09 +010083 * This will be called if %TIF_SYSCALL_TRACE or %TIF_SYSCALL_EMU have been set,
84 * when the current task has just entered the kernel for a system call.
Roland McGrath283d7552008-07-25 19:45:52 -070085 * Full user register state is available here. Changing the values
86 * in @regs can affect the system call number and arguments to be tried.
87 * It is safe to block here, preventing the system call from beginning.
88 *
89 * Returns zero normally, or nonzero if the calling arch code should abort
90 * the system call. That must prevent normal entry so no system call is
91 * made. If @task ever returns to user mode after this, its register state
92 * is unspecified, but should be something harmless like an %ENOSYS error
Roland McGrath828c3652008-07-25 19:45:57 -070093 * return. It should preserve enough information so that syscall_rollback()
94 * can work (see asm-generic/syscall.h).
Roland McGrath283d7552008-07-25 19:45:52 -070095 *
96 * Called without locks, just after entering kernel mode.
97 */
98static inline __must_check int tracehook_report_syscall_entry(
99 struct pt_regs *regs)
100{
Oleg Nesterov15cab952012-03-23 15:02:39 -0700101 return ptrace_report_syscall(regs);
Roland McGrath283d7552008-07-25 19:45:52 -0700102}
103
104/**
105 * tracehook_report_syscall_exit - task has just finished a system call
106 * @regs: user register state of current task
107 * @step: nonzero if simulating single-step or block-step
108 *
109 * This will be called if %TIF_SYSCALL_TRACE has been set, when the
110 * current task has just finished an attempted system call. Full
111 * user register state is available here. It is safe to block here,
112 * preventing signals from being processed.
113 *
114 * If @step is nonzero, this report is also in lieu of the normal
115 * trap that would follow the system call instruction because
116 * user_enable_block_step() or user_enable_single_step() was used.
117 * In this case, %TIF_SYSCALL_TRACE might not be set.
118 *
119 * Called without locks, just before checking for pending signals.
120 */
121static inline void tracehook_report_syscall_exit(struct pt_regs *regs, int step)
122{
Eric W. Biedermanefc463a2018-04-16 14:18:26 -0500123 if (step)
124 user_single_step_report(regs);
125 else
126 ptrace_report_syscall(regs);
Roland McGrath283d7552008-07-25 19:45:52 -0700127}
128
Roland McGrathfa8e26c2008-07-25 19:45:50 -0700129/**
Roland McGrathc45aea22008-07-25 19:45:50 -0700130 * tracehook_signal_handler - signal handler setup is complete
Roland McGrathc45aea22008-07-25 19:45:50 -0700131 * @stepping: nonzero if debugger single-step or block-step in use
132 *
133 * Called by the arch code after a signal handler has been set up.
134 * Register and stack state reflects the user handler about to run.
135 * Signal mask changes have already been made.
136 *
137 * Called without locks, shortly before returning to user mode
138 * (or handling more signals).
139 */
Richard Weinbergerdf5601f2013-10-07 15:37:19 +0200140static inline void tracehook_signal_handler(int stepping)
Roland McGrathc45aea22008-07-25 19:45:50 -0700141{
142 if (stepping)
143 ptrace_notify(SIGTRAP);
144}
145
Roland McGrath64b1208d2008-07-25 19:45:56 -0700146/**
147 * set_notify_resume - cause tracehook_notify_resume() to be called
148 * @task: task that will call tracehook_notify_resume()
149 *
150 * Calling this arranges that @task will call tracehook_notify_resume()
151 * before returning to user mode. If it's already running in user mode,
152 * it will enter the kernel and call tracehook_notify_resume() soon.
153 * If it's blocked, it will not be woken.
154 */
155static inline void set_notify_resume(struct task_struct *task)
156{
Oleg Nesterove73f8952012-05-11 10:59:07 +1000157#ifdef TIF_NOTIFY_RESUME
Roland McGrath64b1208d2008-07-25 19:45:56 -0700158 if (!test_and_set_tsk_thread_flag(task, TIF_NOTIFY_RESUME))
159 kick_process(task);
Oleg Nesterove73f8952012-05-11 10:59:07 +1000160#endif
Roland McGrath64b1208d2008-07-25 19:45:56 -0700161}
162
163/**
164 * tracehook_notify_resume - report when about to return to user mode
165 * @regs: user-mode registers of @current task
166 *
167 * This is called when %TIF_NOTIFY_RESUME has been set. Now we are
168 * about to return to user mode, and the user state in @regs can be
169 * inspected or adjusted. The caller in arch code has cleared
170 * %TIF_NOTIFY_RESUME before the call. If the flag gets set again
171 * asynchronously, this will be called again before we return to
172 * user mode.
173 *
174 * Called without locks.
175 */
176static inline void tracehook_notify_resume(struct pt_regs *regs)
177{
Oleg Nesterove73f8952012-05-11 10:59:07 +1000178 /*
179 * The caller just cleared TIF_NOTIFY_RESUME. This barrier
180 * pairs with task_work_add()->set_notify_resume() after
181 * hlist_add_head(task->task_works);
182 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100183 smp_mb__after_atomic();
Al Viro158e1642012-06-27 09:24:13 +0400184 if (unlikely(current->task_works))
Oleg Nesterove73f8952012-05-11 10:59:07 +1000185 task_work_run();
Tejun Heob23afb92015-11-05 18:46:11 -0800186
187 mem_cgroup_handle_over_high();
Josef Bacikd09d8df2018-07-03 11:14:55 -0400188 blkcg_maybe_throttle_current();
Roland McGrath64b1208d2008-07-25 19:45:56 -0700189}
Roland McGrath64b1208d2008-07-25 19:45:56 -0700190
Roland McGrath88ac2922008-07-25 19:45:43 -0700191#endif /* <linux/tracehook.h> */