diff options
author | Todd T. Fries <todd@cvs.openbsd.org> | 2002-06-09 02:44:14 +0000 |
---|---|---|
committer | Todd T. Fries <todd@cvs.openbsd.org> | 2002-06-09 02:44:14 +0000 |
commit | 644356b24e821469532d008a5aaaae1e59c20c5c (patch) | |
tree | 1b7704988ebedfce982e54cf0c0c704567dd9011 | |
parent | c645793bc2c9536cd259f4bf722d057e4a2f423a (diff) |
provide '-p <pid>' (process attach); thanks to
Jean-Francois Brousseau <krapht@secureops.com>
-rw-r--r-- | usr.bin/pmdb/pmdb.c | 29 | ||||
-rw-r--r-- | usr.bin/pmdb/process.c | 43 |
2 files changed, 50 insertions, 22 deletions
diff --git a/usr.bin/pmdb/pmdb.c b/usr.bin/pmdb/pmdb.c index 43a8ae790f2..99701a6e0af 100644 --- a/usr.bin/pmdb/pmdb.c +++ b/usr.bin/pmdb/pmdb.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pmdb.c,v 1.7 2002/06/05 18:02:27 fgsch Exp $ */ +/* $OpenBSD: pmdb.c,v 1.8 2002/06/09 02:44:13 todd Exp $ */ /* * Copyright (c) 2002 Artur Grabowski <art@openbsd.org> * All rights reserved. @@ -81,7 +81,7 @@ usage() { extern char *__progname; - fprintf(stderr, "Usage: %s [-c core] <program> args\n", __progname); + fprintf(stderr, "Usage: %s [-c core] [-p pid] <program> args\n", __progname); exit(1); } @@ -92,20 +92,27 @@ main(int argc, char **argv) int i, c; int status; void *cm; - char *pmenv, *core; + char *pmenv, *core, *perr; int level; + pid_t pid; if (argc < 2) { usage(); } core = NULL; + pid = 0; - while ((c = getopt(argc, argv, "c:")) != -1) { + while((c = getopt(argc, argv, "c:p:")) != -1) { switch(c) { case 'c': core = optarg; break; + case 'p': + pid = (pid_t) strtol(optarg, &perr, 10); + if(*perr != '\0') + errx(1, "invalid PID"); + break; case '?': default: usage(); @@ -126,7 +133,7 @@ main(int argc, char **argv) asprintf(&pmenv, "%d", level); setenv("IN_PMDB", pmenv, 1); - ps.ps_pid = 0; + ps.ps_pid = pid; ps.ps_state = NONE; ps.ps_argc = argc; ps.ps_argv = argv; @@ -308,10 +315,16 @@ cmd_quit(int argc, char **argv, void *arg) { struct pstate *ps = arg; - ps->ps_flags |= PSF_KILL; + if ((ps->ps_flags & PSF_ATCH)) { + if ((ps->ps_flags & PSF_ATCH) && + ptrace(PT_DETACH, ps->ps_pid, NULL, 0) < 0) + err(1, "ptrace(PT_DETACH)"); + } else { + ps->ps_flags |= PSF_KILL; - if (process_kill(ps)) - return 1; + if (process_kill(ps)) + return 1; + } ps->ps_state = TERMINATED; return 1; diff --git a/usr.bin/pmdb/process.c b/usr.bin/pmdb/process.c index 1e7f1d694ab..86ee73dd1a0 100644 --- a/usr.bin/pmdb/process.c +++ b/usr.bin/pmdb/process.c @@ -1,4 +1,4 @@ -/* $OpenBSD: process.c,v 1.3 2002/03/19 07:26:58 fgsch Exp $ */ +/* $OpenBSD: process.c,v 1.4 2002/06/09 02:44:13 todd Exp $ */ /* * Copyright (c) 2002 Artur Grabowski <art@openbsd.org> * All rights reserved. @@ -27,6 +27,8 @@ #include <sys/types.h> #include <sys/ptrace.h> #include <sys/wait.h> +#include <sys/stat.h> + #include <err.h> #include <errno.h> #include <signal.h> @@ -53,18 +55,32 @@ process_load(struct pstate *ps) return (0); } - switch (ps->ps_pid = fork()) { - case 0: - if (ptrace(PT_TRACE_ME, getpid(), NULL, 0) != 0) - err(1, "ptrace(PT_TRACE_ME)"); - execvp(*ps->ps_argv, ps->ps_argv); - err(1, "exec"); - /* NOTREACHED */ - case -1: - err(1, "fork"); - /* NOTREACHED */ - default: - break; + if (stat(ps->ps_argv[0], &(ps->exec_stat)) < 0) + err(1, "stat()"); + + if (ps->ps_pid != 0) { + /* attach to an already running process */ + if (ptrace(PT_ATTACH, ps->ps_pid, (caddr_t) 0, 0) < 0) + err(1, "failed to ptrace process"); + ps->ps_state = STOPPED; + ps->ps_flags |= PSF_ATCH; + } + else { + switch (ps->ps_pid = fork()) { + case 0: + if (ptrace(PT_TRACE_ME, getpid(), NULL, 0) != 0) + err(1, "ptrace(PT_TRACE_ME)"); + execvp(*ps->ps_argv, ps->ps_argv); + err(1, "exec"); + /* NOTREACHED */ + case -1: + err(1, "fork"); + /* NOTREACHED */ + default: + break; + } + + ps->ps_state = LOADED; } if ((ps->ps_flags & PSF_SYMBOLS) == 0) { @@ -75,7 +91,6 @@ process_load(struct pstate *ps) if (wait(&status) == 0) err(1, "wait"); - ps->ps_state = LOADED; return 0; } |