2 * utrace infrastructure interface for debugging user processes
4 * Copyright (C) 2006, 2007 Red Hat, Inc. All rights reserved.
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.
10 * Red Hat Author: Roland McGrath.
12 * This interface allows for notification of interesting events in a thread.
13 * It also mediates access to thread state such as registers.
14 * Multiple unrelated users can be associated with a single thread.
15 * We call each of these a tracing engine.
17 * A tracing engine starts by calling utrace_attach on the chosen thread,
18 * passing in a set of hooks (struct utrace_engine_ops), and some associated
19 * data. This produces a struct utrace_attached_engine, which is the handle
20 * used for all other operations. An attached engine has its ops vector,
21 * its data, and a flags word controlled by utrace_set_flags.
23 * Each engine's flags word contains two kinds of flags: events of
24 * interest, and action state flags.
26 * For each event flag that is set, that engine will get the
27 * appropriate ops->report_* callback when the event occurs. The
28 * struct utrace_engine_ops need not provide callbacks for an event
29 * unless the engine sets one of the associated event flags.
31 * Action state flags change the normal behavior of the thread.
32 * These bits are in UTRACE_ACTION_STATE_MASK; these can be OR'd into
33 * flags set with utrace_set_flags. Also, every callback that return
34 * an action value can reset these bits for the engine (see below).
36 * The bits UTRACE_ACTION_STATE_MASK of all attached engines are OR'd
37 * together, so each action is in force as long as any engine requests it.
38 * As long as some engine sets the UTRACE_ACTION_QUIESCE flag, the thread
39 * will block and not resume running user code. When the last engine
40 * clears its UTRACE_ACTION_QUIESCE flag, the thread will resume running.
43 #ifndef _LINUX_UTRACE_H
44 #define _LINUX_UTRACE_H 1
46 #include <linux/list.h>
47 #include <linux/rcupdate.h>
48 #include <linux/signal.h>
49 #include <linux/sched.h>
55 struct utrace_regset_view;
59 * Flags in task_struct.utrace_flags and utrace_attached_engine.flags.
60 * Low four bits are UTRACE_ACTION_STATE_MASK bits (below).
61 * Higher bits are events of interest.
64 #define UTRACE_FIRST_EVENT 4
65 #define UTRACE_EVENT_BITS (BITS_PER_LONG - UTRACE_FIRST_EVENT)
66 #define UTRACE_EVENT_MASK (-1UL &~ UTRACE_ACTION_STATE_MASK)
69 _UTRACE_EVENT_QUIESCE, /* Tracing requests stop. */
70 _UTRACE_EVENT_REAP, /* Zombie reaped, no more tracing possible. */
71 _UTRACE_EVENT_CLONE, /* Successful clone/fork/vfork just done. */
72 _UTRACE_EVENT_VFORK_DONE, /* vfork woke from waiting for child. */
73 _UTRACE_EVENT_EXEC, /* Successful execve just completed. */
74 _UTRACE_EVENT_EXIT, /* Thread exit in progress. */
75 _UTRACE_EVENT_DEATH, /* Thread has died. */
76 _UTRACE_EVENT_SYSCALL_ENTRY, /* User entered kernel for system call. */
77 _UTRACE_EVENT_SYSCALL_EXIT, /* Returning to user after system call. */
78 _UTRACE_EVENT_SIGNAL, /* Signal delivery will run a user handler. */
79 _UTRACE_EVENT_SIGNAL_IGN, /* No-op signal to be delivered. */
80 _UTRACE_EVENT_SIGNAL_STOP, /* Signal delivery will suspend. */
81 _UTRACE_EVENT_SIGNAL_TERM, /* Signal delivery will terminate. */
82 _UTRACE_EVENT_SIGNAL_CORE, /* Signal delivery will dump core. */
83 _UTRACE_EVENT_JCTL, /* Job control stop or continue completed. */
86 #define UTRACE_EVENT_BIT(type) (UTRACE_FIRST_EVENT + _UTRACE_EVENT_##type)
87 #define UTRACE_EVENT(type) (1UL << UTRACE_EVENT_BIT(type))
90 * All the kinds of signal events. These all use the report_signal callback.
92 #define UTRACE_EVENT_SIGNAL_ALL (UTRACE_EVENT(SIGNAL) \
93 | UTRACE_EVENT(SIGNAL_IGN) \
94 | UTRACE_EVENT(SIGNAL_STOP) \
95 | UTRACE_EVENT(SIGNAL_TERM) \
96 | UTRACE_EVENT(SIGNAL_CORE))
98 * Both kinds of syscall events; these call the report_syscall_entry and
99 * report_syscall_exit callbacks, respectively.
101 #define UTRACE_EVENT_SYSCALL \
102 (UTRACE_EVENT(SYSCALL_ENTRY) | UTRACE_EVENT(SYSCALL_EXIT))
106 * Action flags, in return value of callbacks.
108 * UTRACE_ACTION_RESUME (zero) is the return value to do nothing special.
109 * For each particular callback, some bits in UTRACE_ACTION_OP_MASK can
110 * be set in the return value to change the thread's behavior (see below).
112 * If UTRACE_ACTION_NEWSTATE is set, then the UTRACE_ACTION_STATE_MASK
113 * bits in the return value replace the engine's flags as in utrace_set_flags
114 * (but the event flags remained unchanged).
116 * If UTRACE_ACTION_HIDE is set, then the callbacks to other engines
117 * should be suppressed for this event. This is appropriate only when
118 * the event was artificially provoked by something this engine did,
119 * such as setting a breakpoint.
121 * If UTRACE_ACTION_DETACH is set, this engine is detached as by utrace_detach.
122 * The action bits in UTRACE_ACTION_OP_MASK work as normal, but the engine's
123 * UTRACE_ACTION_STATE_MASK bits will no longer affect the thread.
125 #define UTRACE_ACTION_RESUME 0x0000 /* Continue normally after event. */
126 #define UTRACE_ACTION_HIDE 0x0010 /* Hide event from other tracing. */
127 #define UTRACE_ACTION_DETACH 0x0020 /* Detach me, state flags ignored. */
128 #define UTRACE_ACTION_NEWSTATE 0x0040 /* Replace state bits. */
131 * These flags affect the state of the thread until they are changed via
132 * utrace_set_flags or by the next callback to the same engine that uses
133 * UTRACE_ACTION_NEWSTATE.
135 #define UTRACE_ACTION_QUIESCE 0x0001 /* Stay quiescent after callbacks. */
136 #define UTRACE_ACTION_SINGLESTEP 0x0002 /* Resume for one instruction. */
137 #define UTRACE_ACTION_BLOCKSTEP 0x0004 /* Resume until next branch. */
138 #define UTRACE_ACTION_NOREAP 0x0008 /* Inhibit parent SIGCHLD and wait. */
139 #define UTRACE_ACTION_STATE_MASK 0x000f /* Lasting state bits. */
141 /* These flags have meanings specific to the particular event report hook. */
142 #define UTRACE_ACTION_OP_MASK 0xff00
145 * Action flags in return value and argument of report_signal callback.
147 #define UTRACE_SIGNAL_DELIVER 0x0100 /* Deliver according to sigaction. */
148 #define UTRACE_SIGNAL_IGN 0x0200 /* Ignore the signal. */
149 #define UTRACE_SIGNAL_TERM 0x0300 /* Terminate the process. */
150 #define UTRACE_SIGNAL_CORE 0x0400 /* Terminate with core dump. */
151 #define UTRACE_SIGNAL_STOP 0x0500 /* Deliver as absolute stop. */
152 #define UTRACE_SIGNAL_TSTP 0x0600 /* Deliver as job control stop. */
153 #define UTRACE_SIGNAL_HOLD 0x1000 /* Flag, push signal back on queue. */
155 * This value is passed to a report_signal callback after a signal
156 * handler is entered while UTRACE_ACTION_SINGLESTEP is in force.
157 * For this callback, no signal will never actually be delivered regardless
158 * of the return value, and the other callback parameters are null.
160 #define UTRACE_SIGNAL_HANDLER 0x0700
162 /* Action flag in return value of report_jctl. */
163 #define UTRACE_JCTL_NOSIGCHLD 0x0100 /* Do not notify the parent. */
167 * Flags for utrace_attach. If UTRACE_ATTACH_CREATE is not specified,
168 * you only look up an existing engine already attached to the
169 * thread. If UTRACE_ATTACH_MATCH_* bits are set, only consider
170 * matching engines. If UTRACE_ATTACH_EXCLUSIVE is set, attempting to
171 * attach a second (matching) engine fails with -EEXIST.
173 #define UTRACE_ATTACH_CREATE 0x0010 /* Attach a new engine. */
174 #define UTRACE_ATTACH_EXCLUSIVE 0x0020 /* Refuse if existing match. */
175 #define UTRACE_ATTACH_MATCH_OPS 0x0001 /* Match engines on ops. */
176 #define UTRACE_ATTACH_MATCH_DATA 0x0002 /* Match engines on data. */
177 #define UTRACE_ATTACH_MATCH_MASK 0x000f
183 * Per-thread structure task_struct.utrace points to.
185 * The task itself never has to worry about this going away after
186 * some event is found set in task_struct.utrace_flags.
187 * Once created, this pointer is changed only when the task is quiescent
188 * (TASK_TRACED or TASK_STOPPED with the siglock held, or dead).
190 * For other parties, the pointer to this is protected by RCU and
191 * task_lock. Since call_rcu is never used while the thread is alive and
192 * using this struct utrace, we can overlay the RCU data structure used
193 * only for a dead struct with some local state used only for a live utrace
194 * on an active thread.
199 struct rcu_head dead;
201 struct task_struct *cloning;
202 struct utrace_signal *signal;
209 struct list_head engines;
215 * Per-engine per-thread structure.
217 * The task itself never has to worry about engines detaching while
218 * it's doing event callbacks. These structures are freed only when
219 * the task is quiescent. For other parties, the list is protected
220 * by RCU and utrace->lock.
222 struct utrace_attached_engine
224 struct list_head entry; /* Entry on thread's utrace.engines list. */
225 struct rcu_head rhead;
227 const struct utrace_engine_ops *ops;
234 struct utrace_engine_ops
237 * Event reporting hooks.
239 * Return values contain UTRACE_ACTION_* flag bits.
240 * The UTRACE_ACTION_OP_MASK bits are specific to each kind of event.
242 * All report_* hooks are called with no locks held, in a generally
243 * safe environment when we will be returning to user mode soon.
244 * It is fine to block for memory allocation and the like, but all
245 * hooks are *asynchronous* and must not block on external events.
246 * If you want the thread to block, request UTRACE_ACTION_QUIESCE in
247 * your hook; then later wake it up with utrace_set_flags.
252 * Event reported for parent, before child might run.
253 * The PF_STARTING flag prevents other engines from attaching
254 * before this one has its chance.
256 u32 (*report_clone)(struct utrace_attached_engine *engine,
257 struct task_struct *parent,
258 unsigned long clone_flags,
259 struct task_struct *child);
262 * Event reported for parent using CLONE_VFORK or vfork system call.
263 * The child has died or exec'd, so the vfork parent has unblocked
264 * and is about to return child_pid.
266 u32 (*report_vfork_done)(struct utrace_attached_engine *engine,
267 struct task_struct *parent, pid_t child_pid);
270 * Event reported after UTRACE_ACTION_QUIESCE is set, when the target
271 * thread is quiescent. Either it's the current thread, or it's in
272 * TASK_TRACED or TASK_STOPPED and will not resume running until the
273 * UTRACE_ACTION_QUIESCE flag is no longer asserted by any engine.
275 u32 (*report_quiesce)(struct utrace_attached_engine *engine,
276 struct task_struct *tsk);
279 * Thread dequeuing a signal to be delivered.
280 * The action and *return_ka values say what UTRACE_ACTION_RESUME
281 * will do (possibly already influenced by another tracing engine).
282 * An UTRACE_SIGNAL_* return value overrides the signal disposition.
283 * The *info data (including info->si_signo) can be changed at will.
284 * Changing *return_ka affects the sigaction that be used.
285 * The *orig_ka value is the one in force before other tracing
286 * engines intervened.
288 u32 (*report_signal)(struct utrace_attached_engine *engine,
289 struct task_struct *tsk,
290 struct pt_regs *regs,
291 u32 action, siginfo_t *info,
292 const struct k_sigaction *orig_ka,
293 struct k_sigaction *return_ka);
296 * Job control event completing, about to send SIGCHLD to parent
297 * with CLD_STOPPED or CLD_CONTINUED as given in type.
298 * UTRACE_JOBSTOP_NOSIGCHLD in the return value inhibits that.
300 u32 (*report_jctl)(struct utrace_attached_engine *engine,
301 struct task_struct *tsk,
305 * Thread has just completed an exec.
306 * The initial user register state is handy to be tweaked directly.
308 u32 (*report_exec)(struct utrace_attached_engine *engine,
309 struct task_struct *tsk,
310 const struct linux_binprm *bprm,
311 struct pt_regs *regs);
314 * Thread has entered the kernel to request a system call.
315 * The user register state is handy to be tweaked directly.
317 u32 (*report_syscall_entry)(struct utrace_attached_engine *engine,
318 struct task_struct *tsk,
319 struct pt_regs *regs);
322 * Thread is about to leave the kernel after a system call request.
323 * The user register state is handy to be tweaked directly.
325 u32 (*report_syscall_exit)(struct utrace_attached_engine *engine,
326 struct task_struct *tsk,
327 struct pt_regs *regs);
330 * Thread is exiting and cannot be prevented from doing so,
331 * but all its state is still live. The *code value will be
332 * the wait result seen by the parent, and can be changed by
333 * this engine or others. The orig_code value is the real
334 * status, not changed by any tracing engine.
336 u32 (*report_exit)(struct utrace_attached_engine *engine,
337 struct task_struct *tsk,
338 long orig_code, long *code);
341 * Thread is really dead now. If UTRACE_ACTION_NOREAP is in force,
342 * it remains an unreported zombie. Otherwise, it might be reaped
343 * by its parent, or self-reap immediately. Though the actual
344 * reaping may happen in parallel, a report_reap callback will
345 * always be ordered after a report_death callback.
347 * If UTRACE_ACTION_NOREAP is in force and this was a group_leader
348 * dying with other threads still in the group (delayed_group_leader),
349 * then there can be a second report_death callback later when the
350 * group_leader is no longer delayed. This second callback can be
351 * made from another thread's context, but it will always be
352 * serialized after the first report_death callback and before the
353 * report_reap callback. It's possible that delayed_group_leader will
354 * already be true by the time it can be checked inside the first
355 * report_death callback made at the time of death, but that a second
356 * callback will be made almost immediately thereafter.
358 u32 (*report_death)(struct utrace_attached_engine *engine,
359 struct task_struct *tsk);
362 * Called when someone reaps the dead task (parent, init, or self).
363 * No more callbacks are made after this one.
364 * The engine is always detached.
365 * There is nothing more a tracing engine can do about this thread.
367 void (*report_reap)(struct utrace_attached_engine *engine,
368 struct task_struct *tsk);
371 * Miscellaneous hooks. These are not associated with event reports.
372 * Any of these may be null if the engine has nothing to say.
373 * These hooks are called in more constrained environments and should
374 * not block or do very much.
378 * Return nonzero iff the caller task should be allowed to access
379 * the memory of the target task via /proc/PID/mem and so forth,
380 * by dint of this engine's attachment to the target.
382 int (*allow_access_process_vm)(struct utrace_attached_engine *engine,
383 struct task_struct *target,
384 struct task_struct *caller);
387 * Return LSM_UNSAFE_* bits that apply to the exec in progress
388 * due to tracing done by this engine. These bits indicate that
389 * someone is able to examine the process and so a set-UID or similar
390 * privilege escalation may not be safe to permit.
392 * Called with task_lock held.
394 int (*unsafe_exec)(struct utrace_attached_engine *engine,
395 struct task_struct *target);
398 * Return the task_struct for the task using ptrace on this one, or
399 * NULL. Always called with rcu_read_lock held to keep the
400 * returned struct alive.
402 * At exec time, this may be called with task_lock(target) still
403 * held from when unsafe_exec was just called. In that case it
404 * must give results consistent with those unsafe_exec results,
405 * i.e. non-NULL if any LSM_UNSAFE_PTRACE_* bits were set.
407 * The value is also used to display after "TracerPid:" in
408 * /proc/PID/status, where it is called with only rcu_read_lock held.
410 * If this engine returns NULL, another engine may supply the result.
412 struct task_struct *(*tracer_task)(struct utrace_attached_engine *,
413 struct task_struct *target);
418 *** These are the exported entry points for tracing engines to use.
422 * Attach a new tracing engine to a thread, or look up attached engines.
423 * See UTRACE_ATTACH_* flags, above. The caller must ensure that the
424 * target thread does not get freed, i.e. hold a ref or be its parent.
426 struct utrace_attached_engine *utrace_attach(struct task_struct *target,
428 const struct utrace_engine_ops *,
432 * Detach a tracing engine from a thread. After this, the engine
433 * data structure is no longer accessible, and the thread might be reaped.
434 * The thread will start running again if it was being kept quiescent
435 * and no longer has any attached engines asserting UTRACE_ACTION_QUIESCE.
437 * If the target thread is not already quiescent, then a callback to this
438 * engine might be in progress or about to start on another CPU. If it's
439 * quiescent when utrace_detach is called, then after successful return
440 * it's guaranteed that no more callbacks to the ops vector will be done.
441 * The only exception is SIGKILL (and exec by another thread in the group),
442 * which breaks quiescence and can cause asynchronous DEATH and/or REAP
443 * callbacks even when UTRACE_ACTION_QUIESCE is set. In that event,
444 * utrace_detach fails with -ESRCH or -EALREADY to indicate that the
445 * report_reap or report_death callbacks have begun or will run imminently.
447 int utrace_detach(struct task_struct *target,
448 struct utrace_attached_engine *engine);
451 * Change the flags for a tracing engine.
452 * This resets the event flags and the action state flags.
453 * If UTRACE_ACTION_QUIESCE and UTRACE_EVENT(QUIESCE) are set,
454 * this will cause a report_quiesce callback soon, maybe immediately.
455 * If UTRACE_ACTION_QUIESCE was set before and is no longer set by
456 * any engine, this will wake the thread up.
458 * This fails with -EALREADY and does nothing if you try to clear
459 * UTRACE_EVENT(DEATH) when the report_death callback may already have
460 * begun, if you try to clear UTRACE_EVENT(REAP) when the report_reap
461 * callback may already have begun, if you try to newly set
462 * UTRACE_ACTION_NOREAP when the target may already have sent its
463 * parent SIGCHLD, or if you try to newly set UTRACE_EVENT(DEATH),
464 * UTRACE_EVENT(QUIESCE), or UTRACE_ACTION_QUIESCE, when the target is
465 * already dead or dying. It can fail with -ESRCH when the target has
466 * already been detached (including forcible detach on reaping). If
467 * the target was quiescent before the call, then after a successful
468 * call, no event callbacks not requested in the new flags will be
469 * made, and a report_quiesce callback will always be made if
470 * requested. These rules provide for coherent synchronization based
471 * on quiescence, even when SIGKILL is breaking quiescence.
473 int utrace_set_flags(struct task_struct *target,
474 struct utrace_attached_engine *engine,
475 unsigned long flags);
478 * Cause a specified signal delivery in the target thread, which must be
479 * quiescent (or the current thread). The action has UTRACE_SIGNAL_* bits
480 * as returned from a report_signal callback. If ka is non-null, it gives
481 * the sigaction to follow for UTRACE_SIGNAL_DELIVER; otherwise, the
482 * installed sigaction at the time of delivery is used.
484 int utrace_inject_signal(struct task_struct *target,
485 struct utrace_attached_engine *engine,
486 u32 action, siginfo_t *info,
487 const struct k_sigaction *ka);
490 * Prepare to access thread's machine state, see <linux/tracehook.h>.
491 * The given thread must be quiescent (or the current thread).
492 * When this returns, the struct utrace_regset calls may be used to
493 * interrogate or change the thread's state. Do not cache the returned
494 * pointer when the thread can resume. You must call utrace_regset to
495 * ensure that context switching has completed and consistent state is
498 const struct utrace_regset *utrace_regset(struct task_struct *target,
499 struct utrace_attached_engine *,
500 const struct utrace_regset_view *,
505 * Hooks in <linux/tracehook.h> call these entry points to the utrace dispatch.
507 int utrace_quiescent(struct task_struct *, struct utrace_signal *);
508 void utrace_release_task(struct task_struct *);
509 int utrace_get_signal(struct task_struct *, struct pt_regs *,
510 siginfo_t *, struct k_sigaction *);
511 void utrace_report_clone(unsigned long clone_flags, struct task_struct *child);
512 void utrace_report_vfork_done(pid_t child_pid);
513 void utrace_report_exit(long *exit_code);
514 void utrace_report_death(struct task_struct *, struct utrace *);
515 void utrace_report_delayed_group_leader(struct task_struct *);
516 int utrace_report_jctl(int type);
517 void utrace_report_exec(struct linux_binprm *bprm, struct pt_regs *regs);
518 void utrace_report_syscall(struct pt_regs *regs, int is_exit);
519 struct task_struct *utrace_tracer_task(struct task_struct *);
520 int utrace_allow_access_process_vm(struct task_struct *);
521 int utrace_unsafe_exec(struct task_struct *);
522 void utrace_signal_handler_singlestep(struct task_struct *, struct pt_regs *);
525 * <linux/tracehook.h> uses these accessors to avoid #ifdef CONFIG_UTRACE.
527 static inline unsigned long tsk_utrace_flags(struct task_struct *tsk)
529 return tsk->utrace_flags;
531 static inline struct utrace *tsk_utrace_struct(struct task_struct *tsk)
535 static inline void utrace_init_task(struct task_struct *child)
537 child->utrace_flags = 0;
538 child->utrace = NULL;
541 #else /* !CONFIG_UTRACE */
543 static unsigned long tsk_utrace_flags(struct task_struct *tsk)
547 static struct utrace *tsk_utrace_struct(struct task_struct *tsk)
551 static inline void utrace_init_task(struct task_struct *child)
556 * The calls to these should all be in if (0) and optimized out entirely.
557 * We have stubs here only so tracehook.h doesn't need to #ifdef them
558 * to avoid external references in case of unoptimized compilation.
560 static inline int utrace_quiescent(struct task_struct *tsk, void *ignored)
565 static inline void utrace_release_task(struct task_struct *tsk)
569 static inline int utrace_get_signal(struct task_struct *tsk,
570 struct pt_regs *regs,
571 siginfo_t *info, struct k_sigaction *ka)
576 static inline void utrace_report_clone(unsigned long clone_flags,
577 struct task_struct *child)
581 static inline void utrace_report_vfork_done(pid_t child_pid)
585 static inline void utrace_report_exit(long *exit_code)
589 static inline void utrace_report_death(struct task_struct *tsk, void *ignored)
593 static inline void utrace_report_delayed_group_leader(struct task_struct *tsk)
597 static inline int utrace_report_jctl(int type)
602 static inline void utrace_report_exec(struct linux_binprm *bprm,
603 struct pt_regs *regs)
607 static inline void utrace_report_syscall(struct pt_regs *regs, int is_exit)
611 static inline struct task_struct *utrace_tracer_task(struct task_struct *tsk)
616 static inline int utrace_allow_access_process_vm(struct task_struct *tsk)
621 static inline int utrace_unsafe_exec(struct task_struct *tsk)
626 static inline void utrace_signal_handler_singlestep(struct task_struct *tsk,
627 struct pt_regs *regs)
632 #endif /* CONFIG_UTRACE */
634 #endif /* linux/utrace.h */