Page MenuHomeFreeBSD

D29955.id88058.diff
No OneTemporary

D29955.id88058.diff

diff --git a/lib/libc/sys/ptrace.2 b/lib/libc/sys/ptrace.2
--- a/lib/libc/sys/ptrace.2
+++ b/lib/libc/sys/ptrace.2
@@ -2,7 +2,7 @@
.\" $NetBSD: ptrace.2,v 1.2 1995/02/27 12:35:37 cgd Exp $
.\"
.\" This file is in the public domain.
-.Dd July 15, 2019
+.Dd April 10, 2021
.Dt PTRACE 2
.Os
.Sh NAME
@@ -807,6 +807,22 @@
The
.Fa data
argument is ignored.
+.It Dv PT_COREDUMP
+This request creates a coredump for the stopped program.
+.Fa addr
+argument specifies a pointer to a
+.Vt "struct ptrace_coredump" ,
+which is defined as follows:
+.Bd -literal
+struct ptrace_coredump {
+ TODO;
+};
+.Ed
+.Pp
+The size of
+.Vt "struct ptrace_coredump"
+must be passed in
+.Fa data .
.El
.Sh ARM MACHINE-SPECIFIC REQUESTS
.Bl -tag -width "Dv PT_SETVFPREGS"
diff --git a/sys/compat/freebsd32/freebsd32.h b/sys/compat/freebsd32/freebsd32.h
--- a/sys/compat/freebsd32/freebsd32.h
+++ b/sys/compat/freebsd32/freebsd32.h
@@ -429,4 +429,14 @@
int32_t stbcnt;
};
+struct ptrace_coredump32 {
+ int pc_fd;
+ uint32_t pc_limit1, pc_limit2;
+ uint32_t pc_flags;
+ uint32_t pc_res1[8];
+ uint32_t pc_res2[16];
+ uint32_t pc_res3[8];
+};
+
+
#endif /* !_COMPAT_FREEBSD32_FREEBSD32_H_ */
diff --git a/sys/compat/freebsd32/freebsd32_misc.c b/sys/compat/freebsd32/freebsd32_misc.c
--- a/sys/compat/freebsd32/freebsd32_misc.c
+++ b/sys/compat/freebsd32/freebsd32_misc.c
@@ -932,6 +932,7 @@
struct ptrace_io_desc piod;
struct ptrace_lwpinfo pl;
struct ptrace_vm_entry pve;
+ struct ptrace_coredump pc;
struct dbreg32 dbreg;
struct fpreg32 fpreg;
struct reg32 reg;
@@ -943,6 +944,7 @@
struct ptrace_io_desc32 piod;
struct ptrace_lwpinfo32 pl;
struct ptrace_vm_entry32 pve;
+ struct ptrace_coredump32 pc;
uint32_t args[nitems(td->td_sa.args)];
struct ptrace_sc_ret32 psr;
} r32;
@@ -1021,6 +1023,15 @@
CP(r32.pve, r.pve, pve_fsid);
PTRIN_CP(r32.pve, r.pve, pve_path);
break;
+ case PT_COREDUMP:
+ if (uap->data != sizeof(r32.pc))
+ error = EINVAL;
+ else
+ error = copyin(uap->addr, &r32.pc, uap->data);
+ CP(r32.pc, r.pc, pc_fd);
+ r.pc.pc_limit = PAIR32TO64(off_t, r32.pc.pc_limit);
+ CP(r32.pc, r.pc, pc_flags);
+ break;
default:
addr = uap->addr;
break;
diff --git a/sys/kern/imgact_elf.c b/sys/kern/imgact_elf.c
--- a/sys/kern/imgact_elf.c
+++ b/sys/kern/imgact_elf.c
@@ -1705,7 +1705,7 @@
}
/* Create a compression stream if necessary. */
- if (compress_user_cores != 0) {
+ if (compress_user_cores != 0 && !(flags & SVC_NOCOMPRESS)) {
params.comp = compressor_init(core_compressed_write,
compress_user_cores, CORE_BUF_SIZE,
compress_user_cores_level, &params);
diff --git a/sys/kern/kern_sig.c b/sys/kern/kern_sig.c
--- a/sys/kern/kern_sig.c
+++ b/sys/kern/kern_sig.c
@@ -2521,6 +2521,41 @@
thread_unlock(td);
}
+static void
+ptrace_coredump(struct thread *td)
+{
+ struct proc *p;
+ struct thr_coredump_req *tcq;
+ void *rl_cookie;
+
+ p = td->td_proc;
+ PROC_LOCK_ASSERT(p, MA_OWNED);
+ if ((td->td_dbgflags & TDB_COREDUMPRQ) == 0)
+ return;
+ KASSERT((p->p_flag & P_STOPPED_TRACE) != 0, ("not stopped"));
+
+ tcq = td->td_coredump;
+ KASSERT(tcq != NULL, ("td_coredump is NULL"));
+
+ if (p->p_sysent->sv_coredump == NULL) {
+ tcq->tc_error = ENOSYS;
+ goto wake;
+ }
+
+ PROC_UNLOCK(p);
+ rl_cookie = vn_rangelock_wlock(tcq->tc_vp, 0, OFF_MAX);
+
+ tcq->tc_error = p->p_sysent->sv_coredump(td, tcq->tc_vp,
+ tcq->tc_limit, SVC_NOCOMPRESS);
+
+ vn_rangelock_unlock(tcq->tc_vp, rl_cookie);
+ PROC_LOCK(p);
+wake:
+ td->td_dbgflags &= ~TDB_COREDUMPRQ;
+ td->td_coredump = NULL;
+ wakeup(p);
+}
+
static int
sig_suspend_threads(struct thread *td, struct proc *p, int sending)
{
@@ -2648,7 +2683,15 @@
td->td_dbgflags &= ~TDB_STOPATFORK;
}
stopme:
+ td->td_dbgflags |= TDB_SSWITCH;
thread_suspend_switch(td, p);
+ td->td_dbgflags &= ~TDB_SSWITCH;
+ if ((td->td_dbgflags & TDB_COREDUMPRQ) != 0) {
+ PROC_SUNLOCK(p);
+ ptrace_coredump(td);
+ PROC_SLOCK(p);
+ goto stopme;
+ }
if (p->p_xthread == td)
p->p_xthread = NULL;
if (!(p->p_flag & P_TRACED))
diff --git a/sys/kern/kern_thread.c b/sys/kern/kern_thread.c
--- a/sys/kern/kern_thread.c
+++ b/sys/kern/kern_thread.c
@@ -1514,6 +1514,23 @@
return (setrunnable(td, 0));
}
+int
+thread_run_flash(struct thread *td)
+{
+ struct proc *p;
+
+ p = td->td_proc;
+ PROC_LOCK_ASSERT(p, MA_OWNED);
+
+ thread_lock(td);
+ KASSERT(TD_IS_SUSPENDED(td), ("Thread not suspended"));
+ TD_CLR_SUSPENDED(td);
+ PROC_SLOCK(p);
+ p->p_suspcount--;
+ PROC_SUNLOCK(p);
+ return (setrunnable(td, 0));
+}
+
/*
* Allow all threads blocked by single threading to continue running.
*/
diff --git a/sys/kern/sys_process.c b/sys/kern/sys_process.c
--- a/sys/kern/sys_process.c
+++ b/sys/kern/sys_process.c
@@ -51,6 +51,8 @@
#include <sys/sx.h>
#include <sys/malloc.h>
#include <sys/signalvar.h>
+#include <sys/caprights.h>
+#include <sys/filedesc.h>
#include <machine/reg.h>
@@ -469,6 +471,7 @@
struct ptrace_io_desc piod;
struct ptrace_lwpinfo pl;
struct ptrace_vm_entry pve;
+ struct ptrace_coredump pc;
struct dbreg dbreg;
struct fpreg fpreg;
struct reg reg;
@@ -519,6 +522,12 @@
case PT_VM_ENTRY:
error = copyin(uap->addr, &r.pve, sizeof(r.pve));
break;
+ case PT_COREDUMP:
+ if (uap->data != sizeof(r.pc))
+ error = EINVAL;
+ else
+ error = copyin(uap->addr, &r.pc, uap->data);
+ break;
default:
addr = uap->addr;
break;
@@ -601,6 +610,46 @@
p->p_ptevents = PTRACE_DEFAULT;
}
+static int
+proc_can_ptrace(struct thread *td, struct proc *p)
+{
+ PROC_LOCK_ASSERT(p, MA_OWNED);
+
+ if ((p->p_flag & P_WEXIT) != 0)
+ return (ESRCH);
+
+ /* not being traced... */
+ if ((p->p_flag & P_TRACED) == 0)
+ return (EPERM);
+
+ /* not being traced by YOU */
+ if (p->p_pptr != td->td_proc)
+ return (EBUSY);
+
+ /* not currently stopped */
+ if ((p->p_flag & P_STOPPED_TRACE) == 0 ||
+ p->p_suspcount != p->p_numthreads ||
+ (p->p_flag & P_WAITED) == 0)
+ return (EBUSY);
+
+ return (0);
+}
+
+static struct thread *
+ptrace_sel_coredump_thread(struct proc *p)
+{
+ struct thread *td2;
+
+ PROC_LOCK_ASSERT(p, MA_OWNED);
+ MPASS((p->p_flag & P_STOPPED_TRACE) != 0);
+
+ FOREACH_THREAD_IN_PROC(p, td2) {
+ if ((td2->td_dbgflags & TDB_SSWITCH) != 0)
+ return (td2);
+ }
+ return (NULL);
+}
+
int
kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data)
{
@@ -611,6 +660,9 @@
struct ptrace_io_desc *piod = NULL;
struct ptrace_lwpinfo *pl;
struct ptrace_sc_ret *psr;
+ struct file *fp;
+ struct ptrace_coredump *pc;
+ struct thr_coredump_req *tcq;
int error, num, tmp;
int proctree_locked = 0;
lwpid_t tid = 0, *buf;
@@ -757,31 +809,34 @@
/* FALLTHROUGH */
default:
- /* not being traced... */
- if ((p->p_flag & P_TRACED) == 0) {
- error = EPERM;
- goto fail;
- }
-
- /* not being traced by YOU */
- if (p->p_pptr != td->td_proc) {
- error = EBUSY;
+ /*
+ * Check for ptrace eligibility before waiting for
+ * holds to drain.
+ */
+ error = proc_can_ptrace(td, p);
+ if (error != 0)
goto fail;
- }
- /* not currently stopped */
- if ((p->p_flag & P_STOPPED_TRACE) == 0 ||
- p->p_suspcount != p->p_numthreads ||
- (p->p_flag & P_WAITED) == 0) {
- error = EBUSY;
- goto fail;
+ /*
+ * Block parallel ptrace requests. Most important, do
+ * not allow other thread in debugger to continue the
+ * debuggee until coredump finished.
+ */
+ while (p->p_lock > 0) {
+ msleep(&p->p_lock, &p->p_mtx, PPAUSE, "", 0);
+ error = proc_can_ptrace(td, p);
+ if (error != 0)
+ goto fail;
}
- /* OK */
+ /* Ok */
break;
}
- /* Keep this process around until we finish this request. */
+ /*
+ * Keep this process around and block parallel ptrace()
+ * request until we finish this request.
+ */
_PHOLD(p);
/*
@@ -1052,6 +1107,9 @@
break;
}
+ if (error != 0)
+ break;
+
sx_xunlock(&proctree_lock);
proctree_locked = 0;
@@ -1299,6 +1357,61 @@
PROC_LOCK(p);
break;
+ case PT_COREDUMP:
+ pc = addr;
+ CTR2(KTR_PTRACE, "PT_COREDUMP: pid %d, fd %d",
+ p->p_pid, pc->pc_fd);
+
+ if ((pc->pc_flags & ~(PC_COMPRESS)) != 0) {
+ error = EINVAL;
+ break;
+ }
+ PROC_UNLOCK(p);
+
+ tcq = malloc(sizeof(*tcq), M_TEMP, M_WAITOK | M_ZERO);
+ fp = NULL;
+ error = getvnode(td, pc->pc_fd, &cap_write_rights, &fp);
+ if (error != 0 || fp->f_vnode->v_type != VREG) {
+ error = EINVAL;
+ goto coredump_cleanup;
+ }
+
+ PROC_LOCK(p);
+ error = proc_can_ptrace(td, p);
+ if (error != 0)
+ goto coredump_cleanup_locked;
+
+ td2 = ptrace_sel_coredump_thread(p);
+ if (td2 == NULL) {
+ error = EBUSY;
+ goto coredump_cleanup_locked;
+ }
+ KASSERT((td2->td_dbgflags & TDB_COREDUMPRQ) == 0,
+ ("proc %d tid %d req coredump", p->p_pid, td2->td_tid));
+
+ tcq->tc_vp = fp->f_vnode;
+ tcq->tc_limit = pc->pc_limit == 0 ? OFF_MAX : pc->pc_limit;
+ if ((pc->pc_flags & PC_COMPRESS) == 0)
+ tcq->tc_flags |= SVC_NOCOMPRESS;
+ td2->td_coredump = tcq;
+ td2->td_dbgflags |= TDB_COREDUMPRQ;
+ if (thread_run_flash(td2)) {
+ PROC_UNLOCK(p);
+ kick_proc0();
+ PROC_LOCK(p);
+ }
+ while ((td2->td_dbgflags & TDB_COREDUMPRQ) != 0)
+ msleep(p, &p->p_mtx, PPAUSE, "crdmp", 0);
+ error = tcq->tc_error;
+coredump_cleanup_locked:
+ PROC_UNLOCK(p);
+coredump_cleanup:
+ if (fp != NULL)
+ fdrop(fp, td);
+ free(tcq, M_TEMP);
+ PROC_LOCK(p);
+ break;
+
default:
#ifdef __HAVE_PTRACE_MACHDEP
if (req >= PT_FIRSTMACH) {
@@ -1311,7 +1424,6 @@
error = EINVAL;
break;
}
-
out:
/* Drop our hold on this process now that the request has completed. */
_PRELE(p);
diff --git a/sys/sys/proc.h b/sys/sys/proc.h
--- a/sys/sys/proc.h
+++ b/sys/sys/proc.h
@@ -377,6 +377,7 @@
int td_oncpu; /* (t) Which cpu we are on. */
void *td_lkpi_task; /* LinuxKPI task struct pointer */
int td_pmcpend;
+ void *td_coredump; /* (c) coredump request. */
#ifdef EPOCH_TRACE
SLIST_HEAD(, epoch_tracker) td_epochs;
#endif
@@ -485,6 +486,8 @@
#define TDB_VFORK 0x00000800 /* vfork indicator for ptrace() */
#define TDB_FSTP 0x00001000 /* The thread is PT_ATTACH leader */
#define TDB_STEP 0x00002000 /* (x86) PSL_T set for PT_STEP */
+#define TDB_SSWITCH 0x00004000
+#define TDB_COREDUMPRQ 0x00008000 /* Coredump request */
/*
* "Private" flags kept in td_pflags:
@@ -1191,6 +1194,7 @@
void childproc_stopped(struct proc *child, int reason);
void childproc_continued(struct proc *child);
void childproc_exited(struct proc *child);
+int thread_run_flash(struct thread *td);
int thread_suspend_check(int how);
bool thread_suspend_check_needed(void);
void thread_suspend_switch(struct thread *, struct proc *p);
diff --git a/sys/sys/ptrace.h b/sys/sys/ptrace.h
--- a/sys/sys/ptrace.h
+++ b/sys/sys/ptrace.h
@@ -74,6 +74,8 @@
#define PT_GET_SC_ARGS 27 /* fetch syscall args */
#define PT_GET_SC_RET 28 /* fetch syscall results */
+#define PT_COREDUMP 29 /* create a coredump */
+
#define PT_GETREGS 33 /* get general-purpose registers */
#define PT_SETREGS 34 /* set general-purpose registers */
#define PT_GETFPREGS 35 /* get floating-point registers */
@@ -176,8 +178,29 @@
char *pve_path; /* Path name of object. */
};
+/* Argument structure for PT_COREDUMP */
+struct ptrace_coredump {
+ int pc_fd; /* File descriptor to write dump to. */
+ off_t pc_limit; /* Maximum size of the coredump,
+ 0 for no limit. */
+ uint32_t pc_flags; /* Flags (currently reserved). */
+ void *pc_res1[8];
+ uint64_t pc_res2[8];
+ uint32_t pc_res3[8];
+};
+
+/* Flags for PT_COREDUMP pc_flags */
+#define PC_COMPRESS 0x00000001 /* Allow compression */
+
#ifdef _KERNEL
+struct thr_coredump_req {
+ struct vnode *tc_vp; /* vnode to write coredump to. */
+ off_t tc_limit; /* max coredump file size. */
+ int tc_flags; /* user flags */
+ int tc_error; /* request result */
+};
+
int ptrace_set_pc(struct thread *_td, unsigned long _addr);
int ptrace_single_step(struct thread *_td);
int ptrace_clear_single_step(struct thread *_td);
diff --git a/sys/sys/sysent.h b/sys/sys/sysent.h
--- a/sys/sys/sysent.h
+++ b/sys/sys/sysent.h
@@ -171,6 +171,9 @@
#define SV_ABI_CLOUDABI 17
#define SV_ABI_UNDEF 255
+/* sv_coredump flags */
+#define SVC_NOCOMPRESS 0x00000001 /* disable compression. */
+
#ifdef _KERNEL
extern struct sysentvec aout_sysvec;
extern struct sysent sysent[];

File Metadata

Mime Type
text/plain
Expires
Thu, Mar 12, 9:46 PM (12 h, 51 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
29597081
Default Alt Text
D29955.id88058.diff (12 KB)

Event Timeline