Merge branch 'vendor/LIBARCHIVE'
[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.6 2008/06/05 18:06:30 swildner 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(struct regcache *regcache, int regno)
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                 regcache_raw_supply(regcache, regno, NULL);
60                 return;
61         }
62
63         /*
64          * kt->pcb == 0 is a marker for "non-dumping kernel thread".
65          */
66         if (kt->pcb == 0) {
67                 uintptr_t regs[5];
68                 uintptr_t addr;
69                 uintptr_t sp;
70
71                 addr = kt->kaddr + offsetof(struct thread, td_sp);
72                 kvm_read(kvm, addr, &sp, sizeof(sp));
73                 /*
74                  * Stack is:
75                  * -2 ret
76                  * -1 popfl
77                  * 0 popl %edi
78                  * 1 popl %esi
79                  * 2 popl %ebx
80                  * 3 popl %ebp
81                  * 4 ret
82                  */
83                 if (kvm_read(kvm, sp + 2 * sizeof(regs[0]), regs, sizeof(regs)) != sizeof(regs)) {
84                         warnx("kvm_read: %s", kvm_geterr(kvm));
85                         memset(regs, 0, sizeof(regs));
86                 }
87                 regcache_raw_supply(regcache, I386_EDI_REGNUM, &regs[0]);
88                 regcache_raw_supply(regcache, I386_ESI_REGNUM, &regs[1]);
89                 regcache_raw_supply(regcache, I386_EBX_REGNUM, &regs[2]);
90                 regcache_raw_supply(regcache, I386_EBP_REGNUM, &regs[3]);
91                 regcache_raw_supply(regcache, I386_EIP_REGNUM, &regs[4]);
92                 sp += 7 * sizeof(regs[0]);
93                 regcache_raw_supply(regcache, I386_ESP_REGNUM, &sp);
94                 return;
95         }
96
97         if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) {
98                 warnx("kvm_read: %s", kvm_geterr(kvm));
99                 memset(&pcb, 0, sizeof(pcb));
100         }
101         regcache_raw_supply(regcache, I386_EBX_REGNUM, (char *)&pcb.pcb_ebx);
102         regcache_raw_supply(regcache, I386_ESP_REGNUM, (char *)&pcb.pcb_esp);
103         regcache_raw_supply(regcache, I386_EBP_REGNUM, (char *)&pcb.pcb_ebp);
104         regcache_raw_supply(regcache, I386_ESI_REGNUM, (char *)&pcb.pcb_esi);
105         regcache_raw_supply(regcache, I386_EDI_REGNUM, (char *)&pcb.pcb_edi);
106         regcache_raw_supply(regcache, I386_EIP_REGNUM, (char *)&pcb.pcb_eip);
107 }
108
109 void
110 kgdb_trgt_store_registers(struct regcache *regcache, int regno __unused)
111 {
112         fprintf_unfiltered(gdb_stderr, "XXX: %s\n", __func__);
113 }
114
115 struct kgdb_frame_cache {
116         int             intrframe;
117         CORE_ADDR       pc;
118         CORE_ADDR       sp;
119 };
120
121 static int kgdb_trgt_frame_offset[15] = {
122         offsetof(struct trapframe, tf_eax),
123         offsetof(struct trapframe, tf_ecx),
124         offsetof(struct trapframe, tf_edx),
125         offsetof(struct trapframe, tf_ebx),
126         offsetof(struct trapframe, tf_esp),
127         offsetof(struct trapframe, tf_ebp),
128         offsetof(struct trapframe, tf_esi),
129         offsetof(struct trapframe, tf_edi),
130         offsetof(struct trapframe, tf_eip),
131         offsetof(struct trapframe, tf_eflags),
132         offsetof(struct trapframe, tf_cs),
133         offsetof(struct trapframe, tf_ss),
134         offsetof(struct trapframe, tf_ds),
135         offsetof(struct trapframe, tf_es),
136         offsetof(struct trapframe, tf_fs)
137 };
138
139 static struct kgdb_frame_cache *
140 kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache)
141 {
142         char buf[MAX_REGISTER_SIZE];
143         struct kgdb_frame_cache *cache;
144         char *pname;
145
146         cache = *this_cache;
147         if (cache == NULL) {
148                 cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache);
149                 *this_cache = cache;
150                 cache->pc = get_frame_address_in_block(next_frame);
151                 find_pc_partial_function(cache->pc, &pname, NULL, NULL);
152
153                 /*
154                  * Handle weird trapframe cases:
155                  *
156                  * 4 for pushed esp (IN the function!)
157                  * + 8 for cpl/intno (IN the function, only intrs)
158                  */
159                 cache->intrframe = 4;
160                 if (pname[0] == 'X')
161                         cache->intrframe += offsetof(struct intrframe, if_esp) -
162                                             offsetof(struct trapframe, tf_esp);
163
164                 frame_unwind_register(next_frame, I386_ESP_REGNUM, buf);
165                 cache->sp = extract_unsigned_integer(buf,
166                     register_size(current_gdbarch, I386_ESP_REGNUM));
167         }
168         return (cache);
169 }
170
171 static void
172 kgdb_trgt_trapframe_this_id(struct frame_info *next_frame, void **this_cache,
173     struct frame_id *this_id)
174 {
175         struct kgdb_frame_cache *cache;
176
177         cache = kgdb_trgt_frame_cache(next_frame, this_cache);
178         *this_id = frame_id_build(cache->sp, cache->pc);
179 }
180
181 static void
182 kgdb_trgt_trapframe_prev_register(struct frame_info *next_frame,
183     void **this_cache, int regnum, int *optimizedp, enum lval_type *lvalp,
184     CORE_ADDR *addrp, int *realnump, gdb_byte *valuep)
185 {
186         char dummy_valuep[MAX_REGISTER_SIZE];
187         struct kgdb_frame_cache *cache;
188         int ofs, regsz;
189
190         regsz = register_size(current_gdbarch, regnum);
191
192         if (valuep == NULL)
193                 valuep = dummy_valuep;
194         memset(valuep, 0, regsz);
195         *optimizedp = 0;
196         *addrp = 0;
197         *lvalp = not_lval;
198         *realnump = -1;
199
200         if (regnum < I386_EAX_REGNUM || regnum > I386_FS_REGNUM)
201                 return;
202
203         cache = kgdb_trgt_frame_cache(next_frame, this_cache);
204
205         ofs = kgdb_trgt_frame_offset[regnum];
206         *addrp = cache->sp + ofs + cache->intrframe;
207
208         /*
209          * If we are in the kernel, we don't have esp stored in the
210          * trapframe, but we can calculate it simply by subtracting
211          * the size of the frame.
212          */
213         if (regnum == I386_ESP_REGNUM) {
214                 char buf[4];
215
216                 frame_unwind_register(next_frame, I386_CS_REGNUM, buf);
217                 if (extract_unsigned_integer(buf, 4) != SEL_UPL) {
218                         store_unsigned_integer(valuep, regsz, *addrp);
219                         return;
220                 }
221                 /* FALLTHROUGH */
222         }
223         *lvalp = lval_memory;
224         target_read_memory(*addrp, valuep, regsz);
225 }
226
227 static int
228 kgdb_trgt_trapframe_sniffer(const struct frame_unwind *self,
229                             struct frame_info *next_frame,
230                             void **this_prologue_cache)
231 {
232         char *pname;
233         CORE_ADDR pc;
234
235         pc = frame_unwind_address_in_block(next_frame, NORMAL_FRAME);
236         pname = NULL;
237         find_pc_partial_function(pc, &pname, NULL, NULL);
238         if (pname == NULL)
239                 return (0);
240         if (strcmp(pname, "calltrap") == 0 ||
241             strcmp(pname, "dblfault_handler") == 0 ||
242             (pname[0] == 'X' && pname[1] != '_'))
243                 return 1;
244         /* printf("%s: %llx =%s\n", __func__, pc, pname); */
245         return 0;
246 }
247
248 const struct frame_unwind kgdb_trgt_trapframe_unwind = {
249         NORMAL_FRAME,
250         &kgdb_trgt_trapframe_this_id,
251         &kgdb_trgt_trapframe_prev_register,
252         .sniffer = kgdb_trgt_trapframe_sniffer
253 };