tracehook.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #include <linux/task_work.h>
  51. struct linux_binprm;
  52. /*
  53. * ptrace report for syscall entry and exit looks identical.
  54. */
  55. static inline int ptrace_report_syscall(struct pt_regs *regs)
  56. {
  57. int ptrace = current->ptrace;
  58. if (!(ptrace & PT_PTRACED))
  59. return 0;
  60. ptrace_notify(SIGTRAP | ((ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
  61. /*
  62. * this isn't the same as continuing with a signal, but it will do
  63. * for normal use. strace only continues with a signal if the
  64. * stopping signal is not SIGTRAP. -brl
  65. */
  66. if (current->exit_code) {
  67. send_sig(current->exit_code, current, 1);
  68. current->exit_code = 0;
  69. }
  70. return fatal_signal_pending(current);
  71. }
  72. /**
  73. * tracehook_report_syscall_entry - task is about to attempt a system call
  74. * @regs: user register state of current task
  75. *
  76. * This will be called if %TIF_SYSCALL_TRACE has been set, when the
  77. * current task has just entered the kernel for a system call.
  78. * Full user register state is available here. Changing the values
  79. * in @regs can affect the system call number and arguments to be tried.
  80. * It is safe to block here, preventing the system call from beginning.
  81. *
  82. * Returns zero normally, or nonzero if the calling arch code should abort
  83. * the system call. That must prevent normal entry so no system call is
  84. * made. If @task ever returns to user mode after this, its register state
  85. * is unspecified, but should be something harmless like an %ENOSYS error
  86. * return. It should preserve enough information so that syscall_rollback()
  87. * can work (see asm-generic/syscall.h).
  88. *
  89. * Called without locks, just after entering kernel mode.
  90. */
  91. static inline __must_check int tracehook_report_syscall_entry(
  92. struct pt_regs *regs)
  93. {
  94. return ptrace_report_syscall(regs);
  95. }
  96. /**
  97. * tracehook_report_syscall_exit - task has just finished a system call
  98. * @regs: user register state of current task
  99. * @step: nonzero if simulating single-step or block-step
  100. *
  101. * This will be called if %TIF_SYSCALL_TRACE has been set, when the
  102. * current task has just finished an attempted system call. Full
  103. * user register state is available here. It is safe to block here,
  104. * preventing signals from being processed.
  105. *
  106. * If @step is nonzero, this report is also in lieu of the normal
  107. * trap that would follow the system call instruction because
  108. * user_enable_block_step() or user_enable_single_step() was used.
  109. * In this case, %TIF_SYSCALL_TRACE might not be set.
  110. *
  111. * Called without locks, just before checking for pending signals.
  112. */
  113. static inline void tracehook_report_syscall_exit(struct pt_regs *regs, int step)
  114. {
  115. if (step) {
  116. siginfo_t info;
  117. user_single_step_siginfo(current, regs, &info);
  118. force_sig_info(SIGTRAP, &info, current);
  119. return;
  120. }
  121. ptrace_report_syscall(regs);
  122. }
  123. /**
  124. * tracehook_signal_handler - signal handler setup is complete
  125. * @sig: number of signal being delivered
  126. * @info: siginfo_t of signal being delivered
  127. * @ka: sigaction setting that chose the handler
  128. * @regs: user register state
  129. * @stepping: nonzero if debugger single-step or block-step in use
  130. *
  131. * Called by the arch code after a signal handler has been set up.
  132. * Register and stack state reflects the user handler about to run.
  133. * Signal mask changes have already been made.
  134. *
  135. * Called without locks, shortly before returning to user mode
  136. * (or handling more signals).
  137. */
  138. static inline void tracehook_signal_handler(int sig, siginfo_t *info,
  139. const struct k_sigaction *ka,
  140. struct pt_regs *regs, int stepping)
  141. {
  142. if (stepping)
  143. ptrace_notify(SIGTRAP);
  144. }
  145. /**
  146. * set_notify_resume - cause tracehook_notify_resume() to be called
  147. * @task: task that will call tracehook_notify_resume()
  148. *
  149. * Calling this arranges that @task will call tracehook_notify_resume()
  150. * before returning to user mode. If it's already running in user mode,
  151. * it will enter the kernel and call tracehook_notify_resume() soon.
  152. * If it's blocked, it will not be woken.
  153. */
  154. static inline void set_notify_resume(struct task_struct *task)
  155. {
  156. #ifdef TIF_NOTIFY_RESUME
  157. if (!test_and_set_tsk_thread_flag(task, TIF_NOTIFY_RESUME))
  158. kick_process(task);
  159. #endif
  160. }
  161. /**
  162. * tracehook_notify_resume - report when about to return to user mode
  163. * @regs: user-mode registers of @current task
  164. *
  165. * This is called when %TIF_NOTIFY_RESUME has been set. Now we are
  166. * about to return to user mode, and the user state in @regs can be
  167. * inspected or adjusted. The caller in arch code has cleared
  168. * %TIF_NOTIFY_RESUME before the call. If the flag gets set again
  169. * asynchronously, this will be called again before we return to
  170. * user mode.
  171. *
  172. * Called without locks.
  173. */
  174. static inline void tracehook_notify_resume(struct pt_regs *regs)
  175. {
  176. if (current->replacement_session_keyring)
  177. key_replace_session_keyring();
  178. /*
  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. */
  183. smp_mb__after_clear_bit();
  184. if (unlikely(!hlist_empty(&current->task_works)))
  185. task_work_run();
  186. }
  187. #endif /* <linux/tracehook.h> */