tracehook.h 7.2 KB

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