Switch to building a libgdb and both gdb and kgdb.
[dragonfly.git] / gnu / usr.bin / gdb / kgdb / trgt_i386.c
1 /*
2  * Copyright (c) 2004 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD: src/gnu/usr.bin/gdb/kgdb/trgt_i386.c,v 1.5 2005/09/11 05:36:30 marcel Exp $
27  * $DragonFly: src/gnu/usr.bin/gdb/kgdb/trgt_i386.c,v 1.1 2006/03/07 15:48:11 corecode Exp $
28  */
29
30 #include <sys/cdefs.h>
31
32 #include <sys/types.h>
33 #include <machine/thread.h>
34 #include <sys/thread.h>
35 #include <machine/pcb.h>
36 #include <machine/frame.h>
37 #include <err.h>
38 #include <kvm.h>
39 #include <string.h>
40
41 #include <defs.h>
42 #include <target.h>
43 #include <gdbthread.h>
44 #include <inferior.h>
45 #include <regcache.h>
46 #include <frame-unwind.h>
47 #include <i386-tdep.h>
48
49 #include "kgdb.h"
50
51 void
52 kgdb_trgt_fetch_registers(int regno __unused)
53 {
54         struct kthr *kt;
55         struct pcb pcb;
56
57         kt = kgdb_thr_lookup_tid(ptid_get_tid(inferior_ptid));
58         if (kt == NULL)
59                 return;
60
61         /*
62          * XXX Detect pure thread
63          * XXX handle pure thread dumping
64          */
65         if (kt->paddr == 0) {
66                 uintptr_t regs[5];
67                 uintptr_t addr;
68                 uintptr_t sp;
69
70                 addr = kt->kaddr + offsetof(struct thread, td_sp);
71                 kvm_read(kvm, addr, &sp, sizeof(sp));
72                 /*
73                  * Stack is:
74                  * -2 ret
75                  * -1 popfl
76                  * 0 popl %edi
77                  * 1 popl %esi
78                  * 2 popl %ebx
79                  * 3 popl %ebp
80                  * 4 ret
81                  */
82                 if (kvm_read(kvm, sp + 2 * sizeof(regs[0]), regs, sizeof(regs)) != sizeof(regs)) {
83                         warnx("kvm_read: %s", kvm_geterr(kvm));
84                         memset(regs, 0, sizeof(regs));
85                 }
86                 supply_register(I386_EDI_REGNUM, &regs[0]);
87                 supply_register(I386_ESI_REGNUM, &regs[1]);
88                 supply_register(I386_EBX_REGNUM, &regs[2]);
89                 supply_register(I386_EBP_REGNUM, &regs[3]);
90                 supply_register(I386_EIP_REGNUM, &regs[4]);
91                 return;
92         }
93
94         if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) {
95                 warnx("kvm_read: %s", kvm_geterr(kvm));
96                 memset(&pcb, 0, sizeof(pcb));
97         }
98         supply_register(I386_EBX_REGNUM, (char *)&pcb.pcb_ebx);
99         supply_register(I386_ESP_REGNUM, (char *)&pcb.pcb_esp);
100         supply_register(I386_EBP_REGNUM, (char *)&pcb.pcb_ebp);
101         supply_register(I386_ESI_REGNUM, (char *)&pcb.pcb_esi);
102         supply_register(I386_EDI_REGNUM, (char *)&pcb.pcb_edi);
103         supply_register(I386_EIP_REGNUM, (char *)&pcb.pcb_eip);
104 }
105
106 void
107 kgdb_trgt_store_registers(int regno __unused)
108 {
109         fprintf_unfiltered(gdb_stderr, "XXX: %s\n", __func__);
110 }
111
112 struct kgdb_frame_cache {
113         int             intrframe;
114         CORE_ADDR       pc;
115         CORE_ADDR       sp;
116 };
117
118 static int kgdb_trgt_frame_offset[15] = {
119         offsetof(struct trapframe, tf_eax),
120         offsetof(struct trapframe, tf_ecx),
121         offsetof(struct trapframe, tf_edx),
122         offsetof(struct trapframe, tf_ebx),
123         offsetof(struct trapframe, tf_esp),
124         offsetof(struct trapframe, tf_ebp),
125         offsetof(struct trapframe, tf_esi),
126         offsetof(struct trapframe, tf_edi),
127         offsetof(struct trapframe, tf_eip),
128         offsetof(struct trapframe, tf_eflags),
129         offsetof(struct trapframe, tf_cs),
130         offsetof(struct trapframe, tf_ss),
131         offsetof(struct trapframe, tf_ds),
132         offsetof(struct trapframe, tf_es),
133         offsetof(struct trapframe, tf_fs)
134 };
135
136 static struct kgdb_frame_cache *
137 kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache)
138 {
139         char buf[MAX_REGISTER_SIZE];
140         struct kgdb_frame_cache *cache;
141         char *pname;
142
143         cache = *this_cache;
144         if (cache == NULL) {
145                 cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache);
146                 *this_cache = cache;
147                 cache->pc = frame_func_unwind(next_frame);
148                 find_pc_partial_function(cache->pc, &pname, NULL, NULL);
149                 cache->intrframe = (pname[0] == 'X') ? 1 : 0;
150                 frame_unwind_register(next_frame, SP_REGNUM, buf);
151                 cache->sp = extract_unsigned_integer(buf,
152                     register_size(current_gdbarch, SP_REGNUM));
153         }
154         return (cache);
155 }
156
157 static void
158 kgdb_trgt_trapframe_this_id(struct frame_info *next_frame, void **this_cache,
159     struct frame_id *this_id)
160 {
161         struct kgdb_frame_cache *cache;
162
163         cache = kgdb_trgt_frame_cache(next_frame, this_cache);
164         *this_id = frame_id_build(cache->sp, cache->pc);
165 }
166
167 static void
168 kgdb_trgt_trapframe_prev_register(struct frame_info *next_frame,
169     void **this_cache, int regnum, int *optimizedp, enum lval_type *lvalp,
170     CORE_ADDR *addrp, int *realnump, void *valuep)
171 {
172         char dummy_valuep[MAX_REGISTER_SIZE];
173         struct kgdb_frame_cache *cache;
174         int ofs, regsz;
175
176         regsz = register_size(current_gdbarch, regnum);
177
178         if (valuep == NULL)
179                 valuep = dummy_valuep;
180         memset(valuep, 0, regsz);
181         *optimizedp = 0;
182         *addrp = 0;
183         *lvalp = not_lval;
184         *realnump = -1;
185
186         if (regnum < I386_EAX_REGNUM || regnum > I386_FS_REGNUM)
187                 return;
188
189         cache = kgdb_trgt_frame_cache(next_frame, this_cache);
190
191         ofs = kgdb_trgt_frame_offset[regnum];
192         *addrp = cache->sp + ofs + (cache->intrframe ? 4 : 0);
193
194         /*
195          * If we are in the kernel, we don't have esp stored in the
196          * trapframe, but we can calculate it simply by subtracting
197          * the size of the frame.
198          */
199         if (regnum == I386_ESP_REGNUM) {
200                 char buf[4];
201
202                 frame_unwind_register(next_frame, I386_CS_REGNUM, buf);
203                 if (extract_unsigned_integer(buf, 4) != SEL_UPL) {
204                         store_unsigned_integer(valuep, regsz, *addrp);
205                         return;
206                 }
207                 /* FALLTHROUGH */
208         }
209         *lvalp = lval_memory;
210         target_read_memory(*addrp, valuep, regsz);
211 }
212
213 static int
214 kgdb_trgt_trapframe_sniffer(const struct frame_unwind *self,
215                             struct frame_info *next_frame,
216                             void **this_prologue_cache)
217 {
218         char *pname;
219         CORE_ADDR pc;
220
221         pc = frame_unwind_address_in_block(next_frame);
222         pname = NULL;
223         find_pc_partial_function(pc, &pname, NULL, NULL);
224         if (pname == NULL)
225                 return (NULL);
226         if (strcmp(pname, "calltrap") == 0 ||
227             strcmp(pname, "dblfault_handler") == 0 ||
228             (pname[0] == 'X' && pname[1] != '_'))
229                 return 1;
230         /* printf("%s: %llx =%s\n", __func__, pc, pname); */
231         return 0;
232 }
233
234 const struct frame_unwind kgdb_trgt_trapframe_unwind = {
235         UNKNOWN_FRAME,
236         &kgdb_trgt_trapframe_this_id,
237         &kgdb_trgt_trapframe_prev_register,
238         .sniffer = kgdb_trgt_trapframe_sniffer
239 };