tracehook.h 8.8 KB

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