Annotation of src/usr.bin/pmdb/pmdb.c, Revision 1.20
1.20 ! steven 1: /* $OpenBSD: pmdb.c,v 1.19 2003/08/18 17:55:57 jfb Exp $ */
1.1 art 2: /*
3: * Copyright (c) 2002 Artur Grabowski <art@openbsd.org>
4: * All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: *
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. The name of the author may not be used to endorse or promote products
13: * derived from this software without specific prior written permission.
14: *
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
26:
27: #include <sys/types.h>
28: #include <sys/ptrace.h>
29: #include <sys/wait.h>
30:
31: #include <stdlib.h>
32: #include <stdio.h>
33: #include <unistd.h>
34: #include <signal.h>
35: #include <err.h>
36: #include <errno.h>
37: #include <string.h>
1.19 jfb 38: #include <paths.h>
1.1 art 39:
40: #include "pmdb.h"
41: #include "symbol.h"
42: #include "clit.h"
43: #include "break.h"
1.7 fgsch 44: #include "core.h"
1.1 art 45:
46: static int cmd_show_registers(int, char **, void *);
47: static int cmd_show_backtrace(int, char **, void *);
1.14 mickey 48: static int cmd_examine(int, char **, void *);
1.1 art 49: static int cmd_quit(int, char **, void *);
50:
51: struct clit cmds[] = {
52: /* debugging info commands. */
53: { "regs", "show registers", 0, 0, cmd_show_registers, (void *)-1 },
54: { "trace", "show backtrace", 0, 0, cmd_show_backtrace, (void *)-1 },
1.14 mickey 55: { "x", "examine memory", 1, 16, cmd_examine, (void *)-1 },
1.1 art 56:
57: /* Process handling commands. */
58: { "run", "run process", 0, 0, cmd_process_run, (void *)-1 },
59: { "continue", "continue process", 0, 0, cmd_process_cont, (void *)-1 },
60: { "kill", "kill process", 0, 0, cmd_process_kill, (void *)-1 },
1.12 art 61: { "setenv", "set env variables", 2, 2, cmd_process_setenv, (void *)-1 },
1.1 art 62:
63: /* signal handling commands. */
64: { "signal", "ignore signal", 2, 2, cmd_signal_ignore, (void *)-1 },
65: { "sigstate", "show signal state", 0, 0, cmd_signal_show, (void *)-1 },
66:
67: /* breakpoints */
68: { "break", "set breakpoint", 1, 1, cmd_bkpt_add, (void *)-1 },
69: { "step", "single step one insn", 0, 0, cmd_sstep, (void *)-1 },
1.3 art 70:
71: /* symbols */
72: { "sym_load", "load symbol table", 2, 2, cmd_sym_load, (void *)-1 },
1.1 art 73:
74: /* misc commands. */
75: { "help", "print help", 0, 1, cmd_help, NULL },
76: { "quit", "quit", 0, 0, cmd_quit, (void *)-1 },
77: { "exit", "quit", 0, 0, cmd_quit, (void *)-1 },
78: };
79:
1.6 fgsch 80: #define NCMDS sizeof(cmds)/sizeof(cmds[0])
81:
1.7 fgsch 82: void
1.17 deraadt 83: usage(void)
1.7 fgsch 84: {
85: extern char *__progname;
86:
1.9 fgsch 87: fprintf(stderr, "Usage: %s [-c core] [-p pid] <program> args\n",
88: __progname);
1.7 fgsch 89: exit(1);
90: }
91:
1.1 art 92: int
93: main(int argc, char **argv)
94: {
95: struct pstate ps;
1.7 fgsch 96: int i, c;
1.1 art 97: int status;
98: void *cm;
1.19 jfb 99: char *pmenv, *core, *perr, execpath[MAXPATHLEN];
1.1 art 100: int level;
1.8 todd 101: pid_t pid;
1.1 art 102:
1.7 fgsch 103: core = NULL;
1.8 todd 104: pid = 0;
1.7 fgsch 105:
1.9 fgsch 106: while ((c = getopt(argc, argv, "c:p:")) != -1) {
1.7 fgsch 107: switch(c) {
108: case 'c':
109: core = optarg;
110: break;
1.8 todd 111: case 'p':
112: pid = (pid_t) strtol(optarg, &perr, 10);
1.9 fgsch 113: if (*perr != '\0')
1.8 todd 114: errx(1, "invalid PID");
115: break;
1.7 fgsch 116: case '?':
117: default:
118: usage();
119: /* NOTREACHED */
120: }
121: }
122: argc -= optind;
123: argv += optind;
1.11 fgsch 124:
125: if (argc == 0)
126: usage();
1.7 fgsch 127:
1.1 art 128: if ((pmenv = getenv("IN_PMDB")) != NULL) {
129: level = atoi(pmenv);
130: level++;
131: } else
132: level = 0;
133:
134: if (level > 0)
135: asprintf(&prompt_add, "(%d)", level);
136: asprintf(&pmenv, "%d", level);
137: setenv("IN_PMDB", pmenv, 1);
1.15 pvalchev 138: if (pmenv)
139: free(pmenv);
1.1 art 140:
1.19 jfb 141: if (getexecpath(argv[0], execpath, sizeof(execpath)) == -1) {
142: err(1, "cannot find `%s'", argv[0]);
143: }
144: argv[0] = execpath;
145:
146: memset(&ps, 0, sizeof(ps));
147: process_setargv(&ps, argc, argv);
148:
1.8 todd 149: ps.ps_pid = pid;
1.1 art 150: ps.ps_state = NONE;
151: ps.ps_flags = 0;
152: ps.ps_signum = 0;
153: ps.ps_npc = 1;
154: TAILQ_INIT(&ps.ps_bkpts);
155: TAILQ_INIT(&ps.ps_sstep_cbs);
156:
157: signal(SIGINT, SIG_IGN);
158:
1.6 fgsch 159: for (i = 0; i < NCMDS; i++)
1.1 art 160: if (cmds[i].arg == (void *)-1)
161: cmds[i].arg = &ps;
162:
163: md_def_init();
164: init_sigstate(&ps);
165:
1.10 art 166: if ((core != NULL) && (read_core(core, &ps) < 0))
167: warnx("failed to load core file");
1.1 art 168:
1.10 art 169: if (process_load(&ps) < 0)
170: errx(1, "failed to load process");
1.7 fgsch 171:
1.6 fgsch 172: cm = cmdinit(cmds, NCMDS);
1.1 art 173: while (ps.ps_state != TERMINATED) {
174: int signum;
175: int stopped;
176: int cont;
177:
178: if (ps.ps_state == STOPPED) {
179: sym_update(&ps);
180: }
181:
182: if (ps.ps_state != RUNNING && cmdloop(cm) == 0) {
183: cmd_quit(0, NULL, &ps);
184: }
185:
186: if (ps.ps_state == TERMINATED)
187: break;
188:
189: if (wait(&status) == 0)
190: err(1, "wait");
191: if (WIFEXITED(status)) {
192: if ((ps.ps_flags & PSF_KILL) == 0) {
193: ps.ps_state = NONE;
194: } else {
195: ps.ps_state = TERMINATED;
196: }
197: fprintf(stderr, "process exited with status %d\n",
198: WEXITSTATUS(status));
199: continue;
200: }
201: if (WIFSIGNALED(status)) {
202: signum = WTERMSIG(status);
203: stopped = 0;
204: } else {
205: signum = WSTOPSIG(status);
206: stopped = 1;
207: }
208: cont = 0;
209: if (stopped)
210: cont = bkpt_check(&ps);
211: process_signal(&ps, signum, stopped, cont);
212: }
213:
214: cmdend(cm);
215:
216: sym_destroy(&ps);
217:
218: return (0);
219: }
220:
221:
222: static int
223: cmd_show_registers(int argc, char **argv, void *arg)
224: {
225: struct pstate *ps = arg;
226: char buf[256];
227: int i;
228: reg *rg;
229:
230: if (ps->ps_state != STOPPED) {
1.7 fgsch 231: if (ps->ps_flags & PSF_CORE) {
232: /* dump registers from core */
1.13 mickey 233: core_printregs(ps);
1.7 fgsch 234: return (0);
235: }
1.1 art 236: fprintf(stderr, "process not stopped\n");
1.7 fgsch 237: return (0);
1.1 art 238: }
239:
240: rg = alloca(sizeof(*rg) * md_def.nregs);
241:
242: if (md_getregs(ps, rg))
243: err(1, "can't get registers");
244: for (i = 0; i < md_def.nregs; i++)
245: printf("%s:\t0x%.*lx\t%s\n", md_def.md_reg_names[i],
246: (int)(sizeof(reg) * 2), (long)rg[i],
247: sym_print(ps, rg[i], buf, sizeof(buf)));
1.7 fgsch 248: return (0);
1.1 art 249: }
250:
251: static int
252: cmd_show_backtrace(int argc, char **argv, void *arg)
253: {
254: struct pstate *ps = arg;
255: int i;
256:
1.10 art 257: if (ps->ps_state != STOPPED && !(ps->ps_flags & PSF_CORE)) {
1.1 art 258: fprintf(stderr, "process not stopped\n");
1.7 fgsch 259: return (0);
1.1 art 260: }
261:
262: /* no more than 100 frames */
263: for (i = 0; i < 100; i++) {
264: struct md_frame mfr;
265: char namebuf[1024], *name;
266: reg offs;
267: int j;
268:
269: mfr.nargs = -1;
270:
271: if (md_getframe(ps, i, &mfr))
272: break;
273:
274: name = sym_name_and_offset(ps, mfr.pc, namebuf,
275: sizeof(namebuf), &offs);
276: if (name == NULL) {
277: snprintf(namebuf, sizeof(namebuf), "0x%lx", mfr.pc);
278: name = namebuf;
1.5 drahn 279: offs = 0;
1.1 art 280: }
281:
282: printf("%s(", name);
283: for (j = 0; j < mfr.nargs; j++) {
284: printf("0x%lx", mfr.args[j]);
285: if (j < mfr.nargs - 1)
286: printf(", ");
287: }
1.5 drahn 288: if (offs == 0) {
1.6 fgsch 289: printf(")\n");
1.5 drahn 290: } else {
291: printf(")+0x%lx\n", offs);
292: }
1.1 art 293: }
1.7 fgsch 294:
295: return (0);
1.1 art 296: }
297:
298: static int
299: cmd_quit(int argc, char **argv, void *arg)
300: {
301: struct pstate *ps = arg;
302:
1.8 todd 303: if ((ps->ps_flags & PSF_ATCH)) {
304: if ((ps->ps_flags & PSF_ATCH) &&
305: ptrace(PT_DETACH, ps->ps_pid, NULL, 0) < 0)
306: err(1, "ptrace(PT_DETACH)");
307: } else {
308: ps->ps_flags |= PSF_KILL;
1.1 art 309:
1.8 todd 310: if (process_kill(ps))
1.9 fgsch 311: return (1);
1.8 todd 312: }
1.1 art 313:
314: ps->ps_state = TERMINATED;
1.9 fgsch 315: return (1);
1.14 mickey 316: }
317:
318: static int
319: cmd_examine(int argc, char **argv, void *arg)
320: {
321: struct pstate *ps = arg;
322: char buf[256];
323: reg addr, val;
324: int i;
325:
326: for (i = 1; argv[i]; i++) {
327:
328: addr = strtoul(argv[i], NULL, 0);
329: if (!addr) { /* assume it's a symbol */
330: if (sym_lookup(ps, argv[i], &addr)) {
331: warn( "Can't find: %s", argv[i]);
332: return (0);
333: }
334: }
335:
1.18 mickey 336: if (process_read(ps, addr, &val, sizeof(val)) < 0) {
1.14 mickey 337: warn("Can't read process contents at 0x%lx", addr);
338: return (0);
339: }
340:
341: printf("%s:\t%s\n", argv[i],
342: sym_print(ps, val, buf, sizeof(buf)));
343: }
344:
345: return (0);
1.1 art 346: }
347:
348: /*
349: * Perform command completion.
350: * Pretty simple. if there are spaces in "buf", the last string is a symbol
351: * otherwise it's a command.
352: */
353: int
354: cmd_complt(char *buf, size_t buflen)
355: {
356: struct clit *match;
357: char *start;
358: int command;
359: int i, j, len;
360: int onlymatch;
361:
362: command = (strchr(buf, ' ') == NULL);
363:
364: if (!command) {
365: /* XXX - can't handle symbols yet. */
1.9 fgsch 366: return (-1);
1.1 art 367: }
368:
369: start = buf;
370: len = strlen(buf);
371:
372: match = NULL;
373: for (i = 0; i < sizeof(cmds) / sizeof(cmds[i]); i++) {
374: if (strncmp(start, cmds[i].cmd, len) == 0) {
375: struct clit *cmdp;
376:
377: cmdp = &cmds[i];
378: if (match == NULL) {
379: onlymatch = 1;
380: match = cmdp;
381: strlcpy(buf, match->cmd, buflen);
382: continue;
383: }
384: onlymatch = 0;
385: for (j = len; j < buflen; j++) {
386: if (buf[j] != cmdp->cmd[j]) {
387: buf[j] = '\0';
388: break;
389: }
390: if (cmdp->cmd[j] == '\0')
391: break;
392: }
393: }
394: }
395:
396: /*
397: * Be nice. If there could be arguments for this command and it's
398: * the only match append a space.
399: */
400: if (match && onlymatch /*&& match->maxargc > 0*/)
401: strlcat(buf, " ", buflen);
402:
403: return (match && onlymatch) ? 0 : -1;
404: }
405:
406: /*
1.7 fgsch 407: * The "standard" wrapper
1.1 art 408: */
409: void *
410: emalloc(size_t sz)
411: {
412: void *ret;
413: if ((ret = malloc(sz)) == NULL)
414: err(1, "malloc");
415: return (ret);
1.19 jfb 416: }
417:
418:
419: /*
420: * Find the first valid path to the executable whose name is <ename>.
421: * The resulting path is stored in <dst>, up to <dlen> - 1 bytes, and is
422: * NUL-terminated. If <dst> is too small, the result will be truncated to
423: * fit, but getexecpath() will return -1.
424: * Returns 0 on success, -1 on failure.
425: */
426:
427: int
428: getexecpath(const char *ename, char *dst, size_t dlen)
429: {
430: char *envp, *pathenv, *pp, *pfp;
431: struct stat pstat;
432:
433: if (stat(ename, &pstat) == 0) {
434: if (strlcpy(dst, ename, dlen) >= dlen)
435: return (-1);
436: return (0);
437: }
438:
439: if (strchr(ename, '/') != NULL) {
440: /* don't bother looking in PATH */
441: return (-1);
442: }
443:
444: envp = getenv("PATH");
445: if (envp == NULL)
446: envp = _PATH_DEFPATH;
447:
448: pathenv = strdup(envp);
449: if (pathenv == NULL) {
450: warn("failed to allocate PATH buffer");
451: return (-1);
452: }
453:
454: for (pp = pathenv; (pfp = strsep(&pp, ":")) != NULL; ) {
455: /* skip cwd, was already tested */
456: if (*pfp == '\0')
457: continue;
458:
459: if (snprintf(dst, dlen, "%s/%s", pfp, ename) >= (int)dlen)
460: continue;
461:
462: if (stat(dst, &pstat) != -1) {
463: free(pathenv);
464: return (0);
465: }
466: }
467:
468: free(pathenv);
469: return (-1);
1.1 art 470: }