gdb: Reduce the recent changes to only the necessary bits in amd64dfly-nat.c.
[dragonfly.git] / contrib / gdb-7 / gdb / i386dfly-nat.c
1 /* Native-dependent code for DragonFly/i386.
2
3    Copyright (C) 2001-2013 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "inferior.h"
22 #include "regcache.h"
23 #include "target.h"
24 #include "gregset.h"
25
26 #include <sys/types.h>
27 #include <sys/procfs.h>
28 #include <sys/ptrace.h>
29 #include <sys/sysctl.h>
30
31 #include "fbsd-nat.h"
32 #include "i386-tdep.h"
33 #include "i386-nat.h"
34 #include "i386bsd-nat.h"
35
36 #ifdef DFLY_PCB_SUPPLY
37 /* Resume execution of the inferior process.  If STEP is nonzero,
38    single-step it.  If SIGNAL is nonzero, give it that signal.  */
39
40 static void
41 i386dfly_resume (struct target_ops *ops,
42                  ptid_t ptid, int step, enum gdb_signal signal)
43 {
44   pid_t pid = ptid_get_pid (ptid);
45   int request = PT_STEP;
46
47   if (pid == -1)
48     /* Resume all threads.  This only gets used in the non-threaded
49        case, where "resume all threads" and "resume inferior_ptid" are
50        the same.  */
51     pid = ptid_get_pid (inferior_ptid);
52
53   if (!step)
54     {
55       struct regcache *regcache = get_current_regcache ();
56       ULONGEST eflags;
57
58       /* Workaround for a bug in FreeBSD.  Make sure that the trace
59          flag is off when doing a continue.  There is a code path
60          through the kernel which leaves the flag set when it should
61          have been cleared.  If a process has a signal pending (such
62          as SIGALRM) and we do a PT_STEP, the process never really has
63          a chance to run because the kernel needs to notify the
64          debugger that a signal is being sent.  Therefore, the process
65          never goes through the kernel's trap() function which would
66          normally clear it.  */
67
68       regcache_cooked_read_unsigned (regcache, I386_EFLAGS_REGNUM,
69                                      &eflags);
70       if (eflags & 0x0100)
71         regcache_cooked_write_unsigned (regcache, I386_EFLAGS_REGNUM,
72                                         eflags & ~0x0100);
73
74       request = PT_CONTINUE;
75     }
76
77   /* An addres of (caddr_t) 1 tells ptrace to continue from where it
78      was.  (If GDB wanted it to start some other way, we have already
79      written a new PC value to the child.)  */
80   if (ptrace (request, pid, (caddr_t) 1,
81               gdb_signal_to_host (signal)) == -1)
82     perror_with_name (("ptrace"));
83 }
84 \f
85
86 /* Transfering the registers between GDB, inferiors and core files.  */
87
88 /* Fill GDB's register array with the general-purpose register values
89    in *GREGSETP.  */
90
91 void
92 supply_gregset (struct regcache *regcache, const gregset_t *gregsetp)
93 {
94   i386bsd_supply_gregset (regcache, gregsetp);
95 }
96
97 /* Fill register REGNUM (if it is a general-purpose register) in
98    *GREGSETPS with the value in GDB's register array.  If REGNUM is -1,
99    do this for all registers.  */
100
101 void
102 fill_gregset (const struct regcache *regcache, gdb_gregset_t *gregsetp, int regnum)
103 {
104   i386bsd_collect_gregset (regcache, gregsetp, regnum);
105 }
106
107 #include "i387-tdep.h"
108
109 /* Fill GDB's register array with the floating-point register values
110    in *FPREGSETP.  */
111
112 void
113 supply_fpregset (struct regcache *regcache, const fpregset_t *fpregsetp)
114 {
115   i387_supply_fsave (regcache, -1, fpregsetp);
116 }
117
118 /* Fill register REGNUM (if it is a floating-point register) in
119    *FPREGSETP with the value in GDB's register array.  If REGNUM is -1,
120    do this for all registers.  */
121
122 void
123 fill_fpregset (const struct regcache *regcache, gdb_fpregset_t *fpregsetp, int regnum)
124 {
125   i387_collect_fsave (regcache, regnum, fpregsetp);
126 }
127 \f
128
129 /* Support for debugging kernel virtual memory images.  */
130
131 #include <sys/types.h>
132 #include <machine/pcb.h>
133
134 #include "bsd-kvm.h"
135
136 static int
137 i386dfly_supply_pcb (struct regcache *regcache, struct pcb *pcb)
138 {
139   /* The following is true for FreeBSD 4.7:
140
141      The pcb contains %eip, %ebx, %esp, %ebp, %esi, %edi and %gs.
142      This accounts for all callee-saved registers specified by the
143      psABI and then some.  Here %esp contains the stack pointer at the
144      point just after the call to cpu_switch().  From this information
145      we reconstruct the register state as it would look when we just
146      returned from cpu_switch().  */
147
148   /* The stack pointer shouldn't be zero.  */
149   if (pcb->pcb_esp == 0)
150     return 0;
151
152   pcb->pcb_esp += 4;
153   regcache_raw_supply (regcache, I386_EDI_REGNUM, &pcb->pcb_edi);
154   regcache_raw_supply (regcache, I386_ESI_REGNUM, &pcb->pcb_esi);
155   regcache_raw_supply (regcache, I386_EBP_REGNUM, &pcb->pcb_ebp);
156   regcache_raw_supply (regcache, I386_ESP_REGNUM, &pcb->pcb_esp);
157   regcache_raw_supply (regcache, I386_EBX_REGNUM, &pcb->pcb_ebx);
158   regcache_raw_supply (regcache, I386_EIP_REGNUM, &pcb->pcb_eip);
159   regcache_raw_supply (regcache, I386_GS_REGNUM, &pcb->pcb_gs);
160
161   return 1;
162 }
163 #endif /* DFLY_PCB_SUPPLY */
164 \f
165
166 /* Prevent warning from -Wmissing-prototypes.  */
167 void _initialize_i386dfly_nat (void);
168
169 void
170 _initialize_i386dfly_nat (void)
171 {
172   struct target_ops *t;
173
174   /* Add some extra features to the common *BSD/i386 target.  */
175   t = i386bsd_target ();
176
177 #ifdef HAVE_PT_GETDBREGS
178
179   i386_use_watchpoints (t);
180
181   i386_dr_low.set_control = i386bsd_dr_set_control;
182   i386_dr_low.set_addr = i386bsd_dr_set_addr;
183   i386_dr_low.get_addr = i386bsd_dr_get_addr;
184   i386_dr_low.get_status = i386bsd_dr_get_status;
185   i386_dr_low.get_control = i386bsd_dr_get_control;
186   i386_set_debug_register_length (4);
187
188 #endif /* HAVE_PT_GETDBREGS */
189
190
191   t->to_pid_to_exec_file = fbsd_pid_to_exec_file;
192   t->to_find_memory_regions = fbsd_find_memory_regions;
193   t->to_make_corefile_notes = fbsd_make_corefile_notes;
194   add_target (t);
195
196 #ifdef DFLY_PCB_SUPPLY
197   /* Support debugging kernel virtual memory images.  */
198   bsd_kvm_add_target (i386dfly_supply_pcb);
199 #endif
200
201   /* DragonFly provides a kern.ps_strings sysctl that we can use to
202      locate the sigtramp.  That way we can still recognize a sigtramp
203      if its location is changed in a new kernel.  Of course this is
204      still based on the assumption that the sigtramp is placed
205      directly under the location where the program arguments and
206      environment can be found.  */
207 #ifdef KERN_PS_STRINGS
208   {
209     int mib[2];
210     u_long ps_strings;
211     size_t len;
212
213     mib[0] = CTL_KERN;
214     mib[1] = KERN_PS_STRINGS;
215     len = sizeof (ps_strings);
216     if (sysctl (mib, 2, &ps_strings, &len, NULL, 0) == 0)
217       {
218         i386dfly_sigtramp_start_addr = ps_strings - 128;
219         i386dfly_sigtramp_end_addr = ps_strings;
220       }
221   }
222 #endif
223 }