Roland McGrath | 88ac292 | 2008-07-25 19:45:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Tracing hooks |
| 3 | * |
Roland McGrath | ae6d2ed | 2009-09-23 15:56:53 -0700 | [diff] [blame] | 4 | * Copyright (C) 2008-2009 Red Hat, Inc. All rights reserved. |
Roland McGrath | 88ac292 | 2008-07-25 19:45:43 -0700 | [diff] [blame] | 5 | * |
| 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 McGrath | 6341c39 | 2008-07-25 19:45:44 -0700 | [diff] [blame] | 51 | #include <linux/security.h> |
| 52 | struct linux_binprm; |
| 53 | |
Roland McGrath | 283d755 | 2008-07-25 19:45:52 -0700 | [diff] [blame] | 54 | /* |
| 55 | * ptrace report for syscall entry and exit looks identical. |
| 56 | */ |
| 57 | static inline void ptrace_report_syscall(struct pt_regs *regs) |
| 58 | { |
Tejun Heo | d21142e | 2011-06-17 16:50:34 +0200 | [diff] [blame] | 59 | int ptrace = current->ptrace; |
Roland McGrath | 283d755 | 2008-07-25 19:45:52 -0700 | [diff] [blame] | 60 | |
| 61 | if (!(ptrace & PT_PTRACED)) |
| 62 | return; |
| 63 | |
| 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 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * tracehook_report_syscall_entry - task is about to attempt a system call |
| 79 | * @regs: user register state of current task |
| 80 | * |
| 81 | * This will be called if %TIF_SYSCALL_TRACE has been set, when the |
| 82 | * current task has just entered the kernel for a system call. |
| 83 | * Full user register state is available here. Changing the values |
| 84 | * in @regs can affect the system call number and arguments to be tried. |
| 85 | * It is safe to block here, preventing the system call from beginning. |
| 86 | * |
| 87 | * Returns zero normally, or nonzero if the calling arch code should abort |
| 88 | * the system call. That must prevent normal entry so no system call is |
| 89 | * made. If @task ever returns to user mode after this, its register state |
| 90 | * is unspecified, but should be something harmless like an %ENOSYS error |
Roland McGrath | 828c365 | 2008-07-25 19:45:57 -0700 | [diff] [blame] | 91 | * return. It should preserve enough information so that syscall_rollback() |
| 92 | * can work (see asm-generic/syscall.h). |
Roland McGrath | 283d755 | 2008-07-25 19:45:52 -0700 | [diff] [blame] | 93 | * |
| 94 | * Called without locks, just after entering kernel mode. |
| 95 | */ |
| 96 | static inline __must_check int tracehook_report_syscall_entry( |
| 97 | struct pt_regs *regs) |
| 98 | { |
| 99 | ptrace_report_syscall(regs); |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * tracehook_report_syscall_exit - task has just finished a system call |
| 105 | * @regs: user register state of current task |
| 106 | * @step: nonzero if simulating single-step or block-step |
| 107 | * |
| 108 | * This will be called if %TIF_SYSCALL_TRACE has been set, when the |
| 109 | * current task has just finished an attempted system call. Full |
| 110 | * user register state is available here. It is safe to block here, |
| 111 | * preventing signals from being processed. |
| 112 | * |
| 113 | * If @step is nonzero, this report is also in lieu of the normal |
| 114 | * trap that would follow the system call instruction because |
| 115 | * user_enable_block_step() or user_enable_single_step() was used. |
| 116 | * In this case, %TIF_SYSCALL_TRACE might not be set. |
| 117 | * |
| 118 | * Called without locks, just before checking for pending signals. |
| 119 | */ |
| 120 | static inline void tracehook_report_syscall_exit(struct pt_regs *regs, int step) |
| 121 | { |
Oleg Nesterov | 2f0edac | 2009-12-15 16:47:19 -0800 | [diff] [blame] | 122 | if (step) { |
| 123 | siginfo_t info; |
| 124 | user_single_step_siginfo(current, regs, &info); |
| 125 | force_sig_info(SIGTRAP, &info, current); |
| 126 | return; |
| 127 | } |
| 128 | |
Roland McGrath | 283d755 | 2008-07-25 19:45:52 -0700 | [diff] [blame] | 129 | ptrace_report_syscall(regs); |
| 130 | } |
| 131 | |
Roland McGrath | fa8e26c | 2008-07-25 19:45:50 -0700 | [diff] [blame] | 132 | /** |
Roland McGrath | c45aea2 | 2008-07-25 19:45:50 -0700 | [diff] [blame] | 133 | * tracehook_signal_handler - signal handler setup is complete |
| 134 | * @sig: number of signal being delivered |
| 135 | * @info: siginfo_t of signal being delivered |
| 136 | * @ka: sigaction setting that chose the handler |
| 137 | * @regs: user register state |
| 138 | * @stepping: nonzero if debugger single-step or block-step in use |
| 139 | * |
| 140 | * Called by the arch code after a signal handler has been set up. |
| 141 | * Register and stack state reflects the user handler about to run. |
| 142 | * Signal mask changes have already been made. |
| 143 | * |
| 144 | * Called without locks, shortly before returning to user mode |
| 145 | * (or handling more signals). |
| 146 | */ |
| 147 | static inline void tracehook_signal_handler(int sig, siginfo_t *info, |
| 148 | const struct k_sigaction *ka, |
| 149 | struct pt_regs *regs, int stepping) |
| 150 | { |
| 151 | if (stepping) |
| 152 | ptrace_notify(SIGTRAP); |
| 153 | } |
| 154 | |
Roland McGrath | 115a326 | 2008-08-04 13:56:01 -0700 | [diff] [blame] | 155 | #define DEATH_REAP -1 |
| 156 | #define DEATH_DELAYED_GROUP_LEADER -2 |
| 157 | |
Roland McGrath | 2b2a1ff | 2008-07-25 19:45:54 -0700 | [diff] [blame] | 158 | /** |
| 159 | * tracehook_notify_death - task is dead, ready to notify parent |
| 160 | * @task: @current task now exiting |
| 161 | * @death_cookie: value to pass to tracehook_report_death() |
| 162 | * @group_dead: nonzero if this was the last thread in the group to die |
| 163 | * |
Roland McGrath | 5c7edcd | 2008-07-31 02:04:09 -0700 | [diff] [blame] | 164 | * A return value >= 0 means call do_notify_parent() with that signal |
| 165 | * number. Negative return value can be %DEATH_REAP to self-reap right |
| 166 | * now, or %DEATH_DELAYED_GROUP_LEADER to a zombie without notifying our |
| 167 | * parent. Note that a return value of 0 means a do_notify_parent() call |
| 168 | * that sends no signal, but still wakes up a parent blocked in wait*(). |
Roland McGrath | 2b2a1ff | 2008-07-25 19:45:54 -0700 | [diff] [blame] | 169 | * |
| 170 | * Called with write_lock_irq(&tasklist_lock) held. |
| 171 | */ |
| 172 | static inline int tracehook_notify_death(struct task_struct *task, |
| 173 | void **death_cookie, int group_dead) |
| 174 | { |
Oleg Nesterov | bb24c67 | 2009-04-02 16:58:20 -0700 | [diff] [blame] | 175 | if (task_detached(task)) |
Roland McGrath | 5c7edcd | 2008-07-31 02:04:09 -0700 | [diff] [blame] | 176 | return task->ptrace ? SIGCHLD : DEATH_REAP; |
Roland McGrath | 2b2a1ff | 2008-07-25 19:45:54 -0700 | [diff] [blame] | 177 | |
| 178 | /* |
| 179 | * If something other than our normal parent is ptracing us, then |
| 180 | * send it a SIGCHLD instead of honoring exit_signal. exit_signal |
| 181 | * only has special meaning to our real parent. |
| 182 | */ |
| 183 | if (thread_group_empty(task) && !ptrace_reparented(task)) |
| 184 | return task->exit_signal; |
| 185 | |
Roland McGrath | 5c7edcd | 2008-07-31 02:04:09 -0700 | [diff] [blame] | 186 | return task->ptrace ? SIGCHLD : DEATH_DELAYED_GROUP_LEADER; |
Roland McGrath | 2b2a1ff | 2008-07-25 19:45:54 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Roland McGrath | 64b1208 | 2008-07-25 19:45:56 -0700 | [diff] [blame] | 189 | #ifdef TIF_NOTIFY_RESUME |
| 190 | /** |
| 191 | * set_notify_resume - cause tracehook_notify_resume() to be called |
| 192 | * @task: task that will call tracehook_notify_resume() |
| 193 | * |
| 194 | * Calling this arranges that @task will call tracehook_notify_resume() |
| 195 | * before returning to user mode. If it's already running in user mode, |
| 196 | * it will enter the kernel and call tracehook_notify_resume() soon. |
| 197 | * If it's blocked, it will not be woken. |
| 198 | */ |
| 199 | static inline void set_notify_resume(struct task_struct *task) |
| 200 | { |
| 201 | if (!test_and_set_tsk_thread_flag(task, TIF_NOTIFY_RESUME)) |
| 202 | kick_process(task); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * tracehook_notify_resume - report when about to return to user mode |
| 207 | * @regs: user-mode registers of @current task |
| 208 | * |
| 209 | * This is called when %TIF_NOTIFY_RESUME has been set. Now we are |
| 210 | * about to return to user mode, and the user state in @regs can be |
| 211 | * inspected or adjusted. The caller in arch code has cleared |
| 212 | * %TIF_NOTIFY_RESUME before the call. If the flag gets set again |
| 213 | * asynchronously, this will be called again before we return to |
| 214 | * user mode. |
| 215 | * |
| 216 | * Called without locks. |
| 217 | */ |
| 218 | static inline void tracehook_notify_resume(struct pt_regs *regs) |
| 219 | { |
| 220 | } |
| 221 | #endif /* TIF_NOTIFY_RESUME */ |
| 222 | |
Roland McGrath | 88ac292 | 2008-07-25 19:45:43 -0700 | [diff] [blame] | 223 | #endif /* <linux/tracehook.h> */ |