1 /*	$OpenBSD: i386.c,v 1.6 2004/02/17 00:52:22 jfb Exp $	*/
2 /*
3  * Copyright (c) 2002 Federico Schwindt <fgsch@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/param.h>
28 #include <sys/ptrace.h>
29 #include <machine/reg.h>
30 #include <machine/frame.h>
31 #include <string.h>
32 #include "pmdb.h"
33 
34 /*
35  * No frame for x86?
36  */
37 struct frame {
38 	u_int32_t fp;
39 	u_int32_t pc;
40 };
41 
42 static const char *md_reg_names[] = {
43 	"%eax", "%ecx", "%edx", "%ebx", "%esp", "%ebp", "%esi", "%edi", "%eip",
44 	"%eflags", "%cs", "%ss", "%ds", "%es", "%fs", "%gs"
45 };
46 
47 struct md_def md_def = { md_reg_names, 16, 8 };
48 
49 void
md_def_init(void)50 md_def_init(void)
51 {
52 	/* no need to do anything */
53 }
54 
55 int
md_getframe(struct pstate * ps,int frame,struct md_frame * fram)56 md_getframe(struct pstate *ps, int frame, struct md_frame *fram)
57 {
58 	struct frame fr;
59 	struct reg r;
60 	int count;
61 
62 	if (process_getregs(ps, &r) != 0)
63 		return (-1);
64 
65 	fr.fp = r.r_ebp;
66 	fr.pc = r.r_eip;
67 	for (count = 0; count < frame; count++) {
68 		if (process_read(ps, (off_t)fr.fp, &fr, sizeof(fr)) < 0)
69 			return (-1);
70 
71 		if (fr.pc < 0x1000)
72 			return (-1);
73 	}
74 
75 	fram->pc = (reg)fr.pc;
76 	fram->fp = (reg)fr.fp;
77 
78 	return (0);
79 }
80 
81 int
md_getregs(struct pstate * ps,reg * regs)82 md_getregs(struct pstate *ps, reg *regs)
83 {
84 	struct reg r;
85 
86 	if (process_getregs(ps, &r) != 0)
87 		return (-1);
88 
89 	memcpy(regs, &r, sizeof(r));
90 
91 	return (0);
92 }
93