FreeBSD Bugzilla – Attachment 223712 Details for
Bug 254661
Debug registers seem to be inherited in forked processes
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
fbsd-fork-dbreg.c
fbsd-fork-dbreg.c (text/plain), 2.49 KB, created by
Michał Górny
on 2021-03-30 15:31:03 UTC
(
hide
)
Description:
fbsd-fork-dbreg.c
Filename:
MIME Type:
Creator:
Michał Górny
Created:
2021-03-30 15:31:03 UTC
Size:
2.49 KB
patch
obsolete
>#include <sys/types.h> >#include <sys/ptrace.h> >#include <sys/wait.h> > >#include <assert.h> >#include <signal.h> >#include <spawn.h> >#include <stdio.h> >#include <stdlib.h> >#include <string.h> >#include <unistd.h> > >#include <cpuid.h> > >volatile int g_val = 0; > >int main() { > int ret; > pid_t pid = fork(); > assert(pid != -1); > > if (pid == 0) { > /* child -- debugged program */ > /* request tracing */ > ret = ptrace(PT_TRACE_ME, 0, NULL, 0); > assert(ret != -1); > ret = raise(SIGSTOP); > assert(ret != -1); > > ret = fork(); > assert(ret != -1); > _exit(0); > } > > /* parent -- the debugger */ > pid_t waited; > > /* 1. SIGSTOP for the process */ > waited = waitpid(pid, &ret, 0); > assert(waited == pid); > assert(WIFSTOPPED(ret)); > assert(WSTOPSIG(ret) == SIGSTOP); > > /* set the event mask */ > int event_mask = PTRACE_FORK; > ret = ptrace(PT_SET_EVENT_MASK, waited, > (void*)&event_mask, > sizeof(event_mask)); > assert(ret == 0); > > /* set dbregs */ > struct dbreg dbregs = {0}; > dbregs.dr[0] = (uintptr_t) &g_val; > dbregs.dr[7] = 0x13; > > printf("dr0 = %p\n", (void*)dbregs.dr[0]); > printf("dr7 = %08lx\n", dbregs.dr[7]); > > ret = ptrace(PT_SETDBREGS, waited, (void*)&dbregs, 0); > assert(ret == 0); > > ret = ptrace(PT_CONTINUE, waited, (void*)1, 0); > assert(ret == 0); > > /* 2. SIGTRAP for fork */ > waited = waitpid(pid, &ret, 0); > assert(waited == pid); > assert(WIFSTOPPED(ret)); > assert(WSTOPSIG(ret) == SIGTRAP); > > struct ptrace_lwpinfo info; > ret = ptrace(PT_LWPINFO, waited, (void*)&info, sizeof(info)); > assert(ret == 0); > assert(info.pl_flags & PL_FLAG_FORKED); > > ret = ptrace(PT_CONTINUE, waited, (void*)1, 0); > assert(ret == 0); > > /* 3. SIGCHLD for the child */ > waited = waitpid(info.pl_child_pid, &ret, 0); > assert(waited != pid); > assert(WIFSTOPPED(ret)); > assert(WSTOPSIG(ret) == SIGSTOP); > > /* grab dbregs */ > ret = ptrace(PT_GETDBREGS, waited, (void*)&dbregs, 0); > assert(ret == 0); > > printf("dr0 = %p\n", (void*)dbregs.dr[0]); > printf("dr7 = %08lx\n", dbregs.dr[7]); > > ret = ptrace(PT_CONTINUE, waited, (void*)1, 0); > assert(ret == 0); > > /* 4. exit for both */ > waited = waitpid(pid, &ret, 0); > assert(waited == pid); > assert(WIFEXITED(ret)); > assert(WEXITSTATUS(ret) == 0); > > waited = waitpid(-1, &ret, 0); > assert(waited != pid); > assert(WIFEXITED(ret)); > assert(WEXITSTATUS(ret) == 0); > > return 0; >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 254661
: 223712