PTRACE(2)

πŸ¦‹ NAME πŸ¦‹

ptrace β€” process tracing and debugging. Yes bestie, this is PTRACE(2), straight out of the System Calls Manual. Buckle up. πŸ’…


πŸŽ€ LIBRARY πŸŽ€

Standard C Library (libc, -lc). The classics.


✨ SYNOPSIS ✨

#include <sys/types.h>
#include <sys/ptrace.h>

int
ptrace(int request, pid_t pid, caddr_t addr, int data);

πŸ’– DESCRIPTION πŸ’–

okay so. The ptrace() system call is the tracing-and-debugging facility. It lets one process (we call her the tracing process) control another one (the traced process). Main character energy, honestly.

The tracing process has to attach first, and then fire off a whole series of ptrace() system calls to control how the process executes, plus peek at its memory and register state. For the entire duration of the tracing session, the traced process gets β€œre-parented” β€” its parent process ID (and all the behavior that comes with it) gets changed over to the tracing process. And yes, one tracing process is absolutely allowed to attach to more than one other process at the same time. Multi-tasking queen. πŸ‘‘

When the tracing process finishes its work, it must detach the traced process.

⚠️ If a tracing process exits without first detaching all the processes it attached to, those processes will be killed. No cap. Not a vibe. Detach your children, bestie.

Most of the time the traced process just runs normally, but the second it receives a signal (see sigaction(2)), it stops. The tracing process is expected to notice that via wait(2) or by getting a SIGCHLD signal, then examine the state of the stopped process and either terminate it or continue it, whichever makes sense. 🌸

That signal could be a totally normal process signal β€” generated by the traced process's own behavior, or by someone using the kill(2) system call. Or alternatively it could be generated by the tracing facility itself, because of attaching, because the tracing process stepped it, or because of some event in the traced process. The tracing process can choose to intercept the signal and use it to observe process behavior (like SIGTRAP, iconic), or just forward it along to the process if that's the move. The ptrace() system call is the mechanism by which literally all of this happens. 🫢


A traced process may report additional signal stops corresponding to events in the traced process. These extras get reported as SIGTRAP or SIGSTOP signals. The tracing process can use the PT_LWPINFO request to figure out which events are associated with a SIGTRAP or SIGSTOP signal.

πŸ’‘ Note that multiple events may be associated with a single signal. For example, events indicated by the PL_FLAG_BORN, PL_FLAG_FORKED, and PL_FLAG_EXEC flags are also reported as a system call exit event (PL_FLAG_SCX).

The signal stop for a new child process enabled via PTRACE_FORK will report a SIGSTOP signal. All other additional signal stops use SIGTRAP.


Every traced process has a tracing event mask. An event in the traced process only reports a signal stop if the corresponding flag is set in the tracing event mask. The current set of tracing event flags includes:

  • PTRACE_EXEC β€” report a stop for a successful invocation of execve(2). This event is indicated by the PL_FLAG_EXEC flag in the pl_flags member of struct ptrace_lwpinfo.
  • PTRACE_SCE β€” report a stop on each system call entry. This event is indicated by the PL_FLAG_SCE flag in the pl_flags member of struct ptrace_lwpinfo.
  • PTRACE_SCX β€” report a stop on each system call exit. This event is indicated by the PL_FLAG_SCX flag in the pl_flags member of struct ptrace_lwpinfo.
  • PTRACE_SYSCALL β€” report stops for both system call entry and exit. Best of both worlds, slay. ✨
  • PTRACE_FORK β€” this event flag controls tracing for new child processes of a traced process.

When this event flag is enabled, new child processes will enable tracing and stop before executing their first instruction. The new child process will include the PL_FLAG_CHILD flag in the pl_flags member of struct ptrace_lwpinfo. The traced process will report a stop that includes the PL_FLAG_FORKED flag. The process ID of the new child process will also be present in the pl_child_pid member of struct ptrace_lwpinfo. If the new child process was created via vfork(2), the traced process's stop will also include the PL_FLAG_VFORKED flag. Note that new child processes will be attached with the default tracing event mask β€” they do not inherit the event mask of the traced process.

When this event flag is not enabled, new child processes will execute without tracing enabled.

  • PTRACE_LWP β€” this event flag controls tracing of LWP (kernel thread) creation and destruction. When this event is enabled, new LWPs will stop and report an event with PL_FLAG_BORN set before executing their first instruction, and exiting LWPs will stop and report an event with PL_FLAG_EXITED set before completing their termination.

Note that new processes do not report an event for the creation of their initial thread, and exiting processes do not report an event for the termination of the last thread. πŸ™ˆ

  • PTRACE_VFORK β€” report a stop event when a parent process resumes after a vfork(2).

When a thread in the traced process creates a new child process via vfork(2), the stop that reports PL_FLAG_FORKED and PL_FLAG_SCX occurs just after the child process is created, but before the thread waits for the child process to stop sharing process memory. If a debugger is not tracing the new child process, it must ensure that no breakpoints are enabled in the shared process memory before detaching from the new child process. Which means: no breakpoints are enabled in the parent process either. Careful, bestie. 😭

The PTRACE_VFORK flag enables a new stop that indicates when the new child process stops sharing the process memory of the parent process. A debugger can reinsert breakpoints in the parent process and resume it in response to this event. This event is indicated by setting the PL_FLAG_VFORK_DONE flag.

The default tracing event mask when attaching to a process via PT_ATTACH, PT_TRACE_ME, or PTRACE_FORK includes only PTRACE_EXEC events. All other event flags are disabled. Periodt.


The request argument specifies what operation is being performed. The meaning of the rest of the arguments depends on the operation, but β€” except for one special case noted below β€” all ptrace() calls are made by the tracing process, and the pid argument specifies the process ID of the traced process or a corresponding thread ID. πŸ’œ

The request argument can be:

  • PT_TRACE_ME β€” this request is the only one used by the traced process; it declares that the process expects to be traced by its parent. All the other arguments are ignored. (If the parent process doesn't expect to trace the child, it's gonna be, like, rather confused by the results. Once the traced process stops, it cannot be made to continue except via ptrace().) When a process has used this request and calls execve(2) or any of the routines built on it (such as execv(3)), it will stop before executing the first instruction of the new image. Also, any setuid or setgid bits on the executable being executed will be ignored. If the child was created by the vfork(2) system call or an rfork(2) call with the RFMEM flag specified, the debugging events are reported to the parent only after the execve(2) is executed.
  • PT_READ_I, PT_READ_D β€” these requests read a single int of data from the traced process's address space. Traditionally, ptrace() has allowed for machines with distinct address spaces for instruction and data, which is why there are two requests: conceptually, PT_READ_I reads from the instruction space and PT_READ_D reads from the data space. In the current FreeBSD implementation, these two requests are completely identical. The addr argument specifies the address (in the traced process's virtual address space) at which the read is to be done. This address does not have to meet any alignment constraints. The value read is returned as the return value from ptrace(). πŸ“
  • PT_WRITE_I, PT_WRITE_D β€” these requests parallel PT_READ_I and PT_READ_D, except that they write rather than read. The data argument supplies the value to be written.
  • PT_IO β€” this request allows reading and writing arbitrary amounts of data in the traced process's address space. The addr argument specifies a pointer to a struct ptrace_io_desc, which is defined as follows:
struct ptrace_io_desc {
        int     piod_op;        /* I/O operation */
        void    *piod_offs;     /* child offset */
        void    *piod_addr;     /* parent offset */
        size_t  piod_len;       /* request length */
};

/*
 * Operations in piod_op.
 */
#define PIOD_READ_D     1       /* Read from D space */
#define PIOD_WRITE_D    2       /* Write to D space */
#define PIOD_READ_I     3       /* Read from I space */
#define PIOD_WRITE_I    4       /* Write to I space */

The data argument is ignored. The actual number of bytes read or written is stored in piod_len upon return. πŸ’«

πŸ’– PT_CONTINUE β€” the "go bestie, keep going" request

Your traced process keeps executing, periodt. The addr argument is an address that says where execution should resume β€” literally a whole new value for the program counter, main character energy. Or you pass (caddr_t)1 to say "nah, just pick up exactly where it left off, we're not doing anything fancy." The data argument hands over a signal number to be delivered to the traced process as it resumes, or 0 if you're not sending any signal at all. ✨

πŸ¦‹ PT_STEP β€” one instruction at a time, babes

The traced process gets single stepped, one (1) instruction, and that's it. The addr argument should be passed (caddr_t)1. The data argument provides a signal number to be delivered to the traced process as it resumes execution, or 0 if no signal is to be sent. Tiny steps, huge slay. πŸ’…

πŸ’€ PT_KILL β€” the dramatic exit

The traced process terminates, exactly as if PT_CONTINUE had been used with SIGKILL given as the signal to be delivered. It's giving "we're so done here." Not a soft launch, not a gentle goodbye β€” it's SIGKILL, iykyk. 😭

πŸŽ€ PT_ATTACH β€” walking up to a stranger and saying "you're mine now"

This request lets a process gain control of an otherwise unrelated process and begin tracing it. Zero cooperation required from the to-be-traced process β€” it doesn't even have to know, which is lowkey unhinged but that's the assignment. πŸ™ˆ

In this case, pid specifies the process ID of the to-be-traced process, and the other two arguments are just ignored, like they're not even there.

Okay but there are rules, bestie. This request requires that the target process must have the same real UID as the tracing process, and that it must not be executing a setuid or setgid executable. (If the tracing process is running as root, these restrictions do not apply β€” root stays iconic and unbothered. πŸ‘‘)

The tracing process will see the newly-traced process stop, and may then control it as if it had been traced all along. Instant adoption. 🫢

🌸 PT_DETACH β€” the "you're free now, go live your life" request

This request is like PT_CONTINUE, except that it does not allow specifying an alternate place to continue execution. And after it succeeds, the traced process is no longer traced and just continues execution normally. No strings, no lingering situationship. πŸ¦‹

⭐ PT_GETREGS β€” reading the machine registers

This request reads the traced process's machine registers into the β€œstruct reg” (defined in <machine/reg.h>) pointed to by addr. Get you a struct that can hold all that. πŸ’–

🌈 PT_SETREGS β€” writing them back

This request is the converse of PT_GETREGS; it loads the traced process's machine registers from the β€œstruct reg” (defined in <machine/reg.h>) pointed to by addr. Symmetry, we love to see it. ✨

πŸ’œ PT_GETFPREGS β€” floating-point registers, but make it read

This request reads the traced process's floating-point registers into the β€œstruct fpreg” (defined in <machine/reg.h>) pointed to by addr.

🧚 PT_SETFPREGS β€” floating-point registers, but make it write

This request is the converse of PT_GETFPREGS; it loads the traced process's floating-point registers from the β€œstruct fpreg” (defined in <machine/reg.h>) pointed to by addr.

πŸ”₯ PT_GETDBREGS β€” debug registers, incoming

This request reads the traced process's debug registers into the β€œstruct dbreg” (defined in <machine/reg.h>) pointed to by addr.

πŸ“ PT_SETDBREGS β€” debug registers, outgoing

This request is the converse of PT_GETDBREGS; it loads the traced process's debug registers from the β€œstruct dbreg” (defined in <machine/reg.h>) pointed to by addr.

πŸ‘‘ PT_LWPINFO β€” the "who exactly stopped me" request

This request can be used to obtain information about the kernel thread, also known as a lightweight process, that caused the traced process to stop. The addr argument specifies a pointer to a struct ptrace_lwpinfo, which is defined as follows:

struct ptrace_lwpinfo {
        lwpid_t pl_lwpid;
        int     pl_event;
        int     pl_flags;
        sigset_t pl_sigmask;
        sigset_t pl_siglist;
        siginfo_t pl_siginfo;
        char    pl_tdname[MAXCOMLEN + 1];
        pid_t   pl_child_pid;
        u_int   pl_syscall_code;
        u_int   pl_syscall_narg;
};

The data argument is to be set to the size of the structure known to the caller. That way the structure can grow later without affecting older programs β€” future-proofing, we stan. 🌟

okay so the fields in struct ptrace_lwpinfo are giving major lore, and here's exactly what each one means πŸ’–

  • pl_lwpid β€” the LWP id of the thread
  • pl_event β€” the event that caused the stop. The currently defined events are:
    • PL_EVENT_NONE β€” no reason given, she's just mysterious like that
    • PL_EVENT_SIGNAL β€” the thread stopped because of the pending signal
  • pl_flags β€” flags that spill the extra tea about the stop you're looking at. The currently defined ones are:
    • PL_FLAG_SCE β€” the thread stopped on a system call entry, right after the kernel got entered. The debugger is allowed to peek at the syscall arguments chilling in memory and registers, per the ABI of the current process, and even modify them if it needs to. main character debugger behavior fr
    • PL_FLAG_SCX β€” the thread is stopped right before the syscall returns to usermode. The debugger can inspect the system call return values sitting in the ABI-defined registers and/or memory.
    • PL_FLAG_EXEC β€” when PL_FLAG_SCX is set, this flag can be set as well, to let you know the program being executed by the debuggee process got changed because a system call from the execve(2) family ran successfully. glow-up via exec, iconic
    • PL_FLAG_SI β€” tells you that the pl_siginfo member of struct ptrace_lwpinfo is holding valid information
    • PL_FLAG_FORKED β€” the process is returning from a call to fork(2) that created a new child process. The process identifier of that new process is available in the pl_child_pid member of struct ptrace_lwpinfo. baby's first process 🍼
    • PL_FLAG_CHILD β€” set for the first event reported from a new child, which gets automatically attached when PTRACE_FORK is enabled
    • PL_FLAG_BORN β€” set for the first event reported from a new LWP when PTRACE_LWP is enabled. It gets reported right along with PL_FLAG_SCX.
    • PL_FLAG_EXITED β€” set for the last event reported by an exiting LWP when PTRACE_LWP is enabled. Heads up though: this event is not reported when the last LWP in a process exits. The termination of that final thread comes through as a normal process exit event instead. no grand finale for the last one 😭
    • PL_FLAG_VFORKED β€” the thread is returning from a call to vfork(2) that created a new child process. This one is set in addition to PL_FLAG_FORKED.
    • PL_FLAG_VFORK_DONE β€” the thread has resumed after a child process created via vfork(2) stopped sharing its address space with the traced process.
  • pl_sigmask β€” the current signal mask of the LWP
  • pl_siglist β€” the current pending set of signals for the LWP. Watch out bestie: signals that get delivered to the process will not appear on an LWP siglist until the thread is actually selected for delivery. patience is a virtue πŸ’œ
  • pl_siginfo β€” the siginfo that comes with the pending signal. Only valid for a PL_EVENT_SIGNAL stop when PL_FLAG_SI is set in pl_flags.
  • pl_tdname β€” the name of the thread
  • pl_child_pid β€” the process identifier of the new child process. Only valid for a PL_EVENT_SIGNAL stop when PL_FLAG_FORKED is set in pl_flags.
  • pl_syscall_code β€” the ABI-specific identifier of the current system call. Note that for indirect system calls this field reports the indirected system call. Only valid when PL_FLAG_SCE or PL_FLAG_SCX is set in pl_flags.
  • pl_syscall_narg β€” the number of arguments passed to the current system call, not counting the system call identifier. Note that for indirect system calls this field reports the arguments passed to the indirected system call. Only valid when PL_FLAG_SCE or PL_FLAG_SCX is set in pl_flags.

and now, the requests themselves β€” the whole lineup, no skips ✨

  • PT_GETNUMLWPS β€” this request returns the number of kernel threads associated with the traced process.
  • PT_GETLWPLIST β€” this one grabs the current thread list for you. Pass a pointer to an array of type lwpid_t in addr, with the array size specified by data. The return value from ptrace() is the count of array entries that got filled in.
  • PT_SETSTEP β€” turns on single stepping for the specified process. Stepping gets automatically disabled when a single step trap is caught.
  • PT_CLEARSTEP β€” turns off single stepping for the specified process.
  • PT_SUSPEND β€” suspends the specified thread. nap time πŸ¦‹
  • PT_RESUME β€” resumes the specified thread. wake up queen πŸ‘‘
  • PT_TO_SCE β€” sets the PTRACE_SCE event flag so you trace all future system call entries, then continues the process. The addr and data arguments are used the same as for PT_CONTINUE.
  • PT_TO_SCX β€” sets the PTRACE_SCX event flag so you trace all future system call exits, then continues the process. The addr and data arguments are used the same as for PT_CONTINUE.
  • PT_SYSCALL β€” sets the PTRACE_SYSCALL event flag so you trace all future system call entries and exits, then continues the process. The addr and data arguments are used the same as for PT_CONTINUE.
  • PT_GET_SC_ARGS β€” for a thread stopped in either the PL_FLAG_SCE or PL_FLAG_SCX state (so, on entry or exit to a syscall), this request fetches the syscall arguments.

The arguments get copied out into the buffer pointed to by the addr pointer, sequentially. Each syscall argument is stored as the machine word. The kernel copies out as many arguments as the syscall accepts β€” see the pl_syscall_narg member of the struct ptrace_lwpinfo β€” but no more than data bytes in total get copied.

  • PT_GET_SC_RET β€” fetches the system call return values on exit from a syscall. This request is only valid for threads stopped in a syscall exit (the PL_FLAG_SCX state). The addr argument specifies a pointer to a struct ptrace_sc_ret, which is defined like this:
struct ptrace_sc_ret {
        register_t      sr_retval[2];
        int             sr_error;
};

The data argument gets set to the size of the structure.

If the system call completed successfully, sr_error is set to zero and the return values of the system call are saved in sr_retval. If the system call failed to execute, the sr_error field is set to a positive errno(2) value. If the system call completed in an unusual fashion, sr_error is set to a negative value:

  • ERESTART β€” bestie, the syscall is gonna get restarted. like it said "hold on, be right back" πŸ’…
  • EJUSTRETURN β€” the syscall finished successfully but did not set a return value (think setcontext(2) and sigreturn(2), the iconics that just vibe on through).
  • PT_FOLLOW_FORK β€” this one controls tracing for new child processes of a traced process, no cap. If data is non-zero, PTRACE_FORK gets set in the traced process's event tracing mask. If data is zero, PTRACE_FORK gets cleared from the traced process's event tracing mask. set it, clear it, it's giving toggle energy ✨
  • PT_LWP_EVENTS β€” same vibe but for LWP (lightweight process) creation and destruction. If data is non-zero, PTRACE_LWP is set in the traced process's event tracing mask. If data is zero, PTRACE_LWP is cleared from the traced process's event tracing mask. πŸ¦‹
  • PT_GET_EVENT_MASK β€” reads the traced process's event tracing mask into the integer that addr points at. the size of that integer has to be passed in data. don't forget it, she's mandatory πŸ™ˆ
  • PT_SET_EVENT_MASK β€” sets the traced process's event tracing mask from the integer that addr points at. the size of the integer goes in data. same deal πŸ’–
  • PT_VM_TIMESTAMP β€” returns the generation number (aka the timestamp) of the traced process's memory map, handed back as the return value from ptrace(). it's the low-cost way for the tracing process to figure out if the VM map changed since the last time it made this request. cheap and effective, main character behavior πŸ‘‘
  • PT_VM_ENTRY β€” this request iterates over the entries of the VM map of the traced process. the addr argument is a pointer to a struct ptrace_vm_entry, which is defined as follows:
struct ptrace_vm_entry {
        int             pve_entry;
        int             pve_timestamp;
        u_long          pve_start;
        u_long          pve_end;
        u_long          pve_offset;
        u_int           pve_prot;
        u_int           pve_pathlen;
        long            pve_fileid;
        uint32_t        pve_fsid;
        char            *pve_path;
};

The first entry comes back when you set pve_entry to zero. Subsequent entries come back when you leave pve_entry unmodified from the value that previous requests returned β€” so like, don't touch it, she remembers where she was 🫢

The pve_timestamp field is there to detect changes to the VM map while you're iterating over the entries. The tracing process can then take appropriate action, such as restarting. By setting pve_pathlen to a non-zero value on entry, the pathname of the backing object is returned in the buffer pointed to by pve_path, provided the entry is backed by a vnode. The pve_pathlen field is updated with the actual length of the pathname (including the terminating null character). The pve_offset field is the offset within the backing object at which the range starts. The range is located in the VM space at pve_start and extends up to pve_end (inclusive).

The data argument is ignored. literally ignored. not even looked at 😭


🌈 ARM MACHINE-SPECIFIC REQUESTS 🌈

  • PT_GETVFPREGS β€” return the thread's VFP machine state in the buffer pointed to by addr.

The data argument is ignored.

  • PT_SETVFPREGS β€” set the thread's VFP machine state from the buffer pointed to by addr.

The data argument is ignored.


πŸ’œ x86 MACHINE-SPECIFIC REQUESTS πŸ’œ

  • PT_GETXMMREGS β€” copy the XMM FPU state into the buffer pointed to by the argument addr. The buffer has the same layout as the 32-bit save buffer for the machine instruction FXSAVE.

This request is only valid for i386 programs, both on native 32-bit systems and on amd64 kernels. For 64-bit amd64 programs, the XMM state is reported as part of the FPU state returned by the PT_GETFPREGS request.

The data argument is ignored.

  • PT_SETXMMREGS β€” load the XMM FPU state for the thread from the buffer pointed to by the argument addr. The buffer has the same layout as the 32-bit load buffer for the machine instruction FXRSTOR.

As with PT_GETXMMREGS, this request is only valid for i386 programs.

The data argument is ignored.

  • PT_GETXSTATE_INFO β€” report which XSAVE FPU extensions are supported by the CPU and allowed in userspace programs. The addr argument must point to a variable of type struct ptrace_xstate_info, which contains the information on the request return. struct ptrace_xstate_info is defined as follows:
struct ptrace_xstate_info {
        uint64_t        xsave_mask;
        uint32_t        xsave_len;
};

The xsave_mask field is a bitmask of the currently enabled extensions. The meaning of the bits is defined in the Intel and AMD processor documentation. The xsave_len field reports the length of the XSAVE area for storing the hardware state for currently enabled extensions in the format defined by the x86 XSAVE machine instruction.

The data argument value must be equal to the size of the struct ptrace_xstate_info. no wiggle room, she's strict πŸ’…

  • PT_GETXSTATE β€” return the content of the XSAVE area for the thread. The addr argument points to the buffer where the content is copied, and the data argument specifies the size of the buffer. The kernel copies out as much content as allowed by the buffer size. The buffer layout is specified by the layout of the save area for the XSAVE machine instruction.
  • PT_SETXSTATE β€” load the XSAVE state for the thread from the buffer specified by the addr pointer. The buffer size is passed in the data argument. The buffer must be at least as large as the struct savefpu (defined in x86/fpu.h) to allow the complete x87 FPU and XMM state load. It must not be larger than the XSAVE state length, as reported by the xsave_len field from the struct ptrace_xstate_info of the PT_GETXSTATE_INFO request. Layout of the buffer is identical to the layout of the load area for the XRSTOR machine instruction.
not me stressing the size bounds but fr fr, too small and too big are both a no πŸ™ˆ
  • PT_GETFSBASE β€” return the value of the base used when doing segmented memory addressing using the %fs segment register. The addr argument points to an unsigned long variable where the base value is stored.

The data argument is ignored.

  • PT_GETGSBASE β€” like the PT_GETFSBASE request, but returns the base for the %gs segment register.
  • PT_SETFSBASE β€” set the base for the %fs segment register to the value pointed to by the addr argument. addr must point to the unsigned long variable containing the new base.

The data argument is ignored.

  • PT_SETGSBASE β€” like the PT_SETFSBASE request, but sets the base for the %gs segment register.

πŸŽ€ PowerPC MACHINE-SPECIFIC REQUESTS πŸŽ€

  • PT_GETVRREGS β€” return the thread's ALTIVEC machine state in the buffer pointed to by addr.

The data argument is ignored.

  • PT_SETVRREGS β€” set the thread's ALTIVEC machine state from the buffer pointed to by addr.

The data argument is ignored.

  • PT_GETVSRREGS β€” return doubleword 1 of the thread's VSX registers VSR0-VSR31 in the buffer pointed to by addr.

The data argument is ignored.

  • PT_SETVSRREGS β€” set doubleword 1 of the thread's VSX registers VSR0-VSR31 from the buffer pointed to by addr.

The data argument is ignored.

Additionally, other machine-specific requests can exist. so like, stay curious bestie πŸ¦‹


✨ RETURN VALUES ✨

Most requests return 0 on success and -1 on error. But β€” and this is the plot twist β€” some requests can cause ptrace() to return -1 as a non-error value. Among them are PT_READ_I and PT_READ_D, which return the value read from the process memory on success. 😭

To disambiguate, set errno to 0 before the call and check it afterwards. it's the classic "was that a real error or just the vibe" check πŸ’…

The current ptrace() implementation always sets errno to 0 before calling into the kernel, both for historic reasons and for consistency with other operating systems. It is recommended to assign zero to errno explicitly for forward compatibility. yes, explicitly. do it anyway, future-proof yourself 🫢

πŸ’€ ERRORS πŸ’€

bestie. listen. ptrace() can flop, and here's exactly how it flops. every single one of these is real and you will meet them all eventually. The ptrace() system call may fail if:

  • [ESRCH] β€” the process said "who?" and meant it:
    • No process having the specified process ID exists. ghosted fr fr πŸ‘»
  • [EINVAL] β€” you gave it something it simply cannot work with:
    • A process attempted to use PT_ATTACH on itself. not the self-attach 😭 bestie you cannot trace yourself, that's giving main character syndrome
    • The request argument was not one of the legal requests. made-up request? jail. πŸš”
    • The signal number (in data) to PT_CONTINUE was neither 0 nor a legal signal number.
    • PT_GETREGS, PT_SETREGS, PT_GETFPREGS, PT_SETFPREGS, PT_GETDBREGS, or PT_SETDBREGS was attempted on a process with no valid register set. (This is normally true only of system processes.) system processes stay unregistered, it's their whole vibe
    • PT_VM_ENTRY was given an invalid value for pve_entry. This can also be caused by changes to the VM map of the process. the map moved while you were looking at it, iconic chaos
    • The size (in data) provided to PT_LWPINFO was less than or equal to zero, or larger than the ptrace_lwpinfo structure known to the kernel.
    • The size (in data) provided to the x86-specific PT_GETXSTATE_INFO request was not equal to the size of the struct ptrace_xstate_info. exact match only, she's picky πŸ’…
    • The size (in data) provided to the x86-specific PT_SETXSTATE request was less than the size of the x87 plus the XMM save area.
    • The size (in data) provided to the x86-specific PT_SETXSTATE request was larger than returned in the xsave_len member of the struct ptrace_xstate_info from the PT_GETXSTATE_INFO request.
    • The base value, provided to the amd64-specific requests PT_SETFSBASE or PT_SETGSBASE, pointed outside of the valid user address space. This error will not occur in 32-bit programs. 32-bit girlies, this one's not for you 🫢
  • [EBUSY] β€” she's booked, occupied, not available, do not perceive her:
    • PT_ATTACH was attempted on a process that was already being traced. somebody already got there first, sorry babe
    • A request attempted to manipulate a process that was being traced by some process other than the one making the request.
    • A request (other than PT_ATTACH) specified a process that was not stopped. it has to be STOPPED. paused. frozen. periodt.
  • [EPERM] β€” permission? never heard of her:
    • A request (other than PT_ATTACH) attempted to manipulate a process that was not being traced at all. there's no trace to speak of, bestie, it's giving nothing
    • An attempt was made to use PT_ATTACH on a process in violation of the requirements listed under PT_ATTACH above. scroll back up, the rules are right there πŸ”₯
  • [ENOENT] β€” you asked for one more and there isn't one:
    • PT_VM_ENTRY previously returned the last entry of the memory map. No more entries exist. you ate the whole map. it's empty. go home. 🍽️
  • [ENAMETOOLONG] β€” the name did not fit in the lil outfit you gave it:
    • PT_VM_ENTRY cannot return the pathname of the backing object because the buffer is not big enough. pve_pathlen holds the minimum buffer size required on return. she's literally telling you the size you need, take the hint 😭

πŸ‘€ SEE ALSO πŸ‘€

the besties you should also be reading, no cap:


πŸ¦‹ HISTORY πŸ¦‹

The ptrace() function appeared in Version 6 AT&T UNIX. she's been doing this since the actual 1970s, absolute legend, the blueprint, the original icon πŸ‘‘


okay that's the whole page bestie, you made it, I'm so proud of you πŸ«ΆπŸ’– this whole saga lives in the Debian ptrace(2) manual, dated July 15, 2019 β€” go forth and trace responsibly, mwah πŸ’…βœ¨πŸŒΈ

πŸ“¦ where this page comes from