Constify strings in tracepoint.c, lookup_cmd and the completers.
[platform/upstream/binutils.git] / gdb / i386gnu-nat.c
1 /* Low level interface to i386 running the GNU Hurd.
2
3    Copyright (C) 1992-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 "floatformat.h"
23 #include "regcache.h"
24
25 #include "gdb_assert.h"
26 #include <errno.h>
27 #include <stdio.h>
28 #include "gdb_string.h"
29
30 #include <mach.h>
31 #include <mach_error.h>
32 #include <mach/message.h>
33 #include <mach/exception.h>
34
35 #include "i386-tdep.h"
36
37 #include "gnu-nat.h"
38 #include "i387-tdep.h"
39
40 #ifdef HAVE_SYS_PROCFS_H
41 # include <sys/procfs.h>
42 # include "gregset.h"
43 #endif
44
45 /* Offset to the thread_state_t location where REG is stored.  */
46 #define REG_OFFSET(reg) offsetof (struct i386_thread_state, reg)
47
48 /* At REG_OFFSET[N] is the offset to the thread_state_t location where
49    the GDB register N is stored.  */
50 static int reg_offset[] =
51 {
52   REG_OFFSET (eax), REG_OFFSET (ecx), REG_OFFSET (edx), REG_OFFSET (ebx),
53   REG_OFFSET (uesp), REG_OFFSET (ebp), REG_OFFSET (esi), REG_OFFSET (edi),
54   REG_OFFSET (eip), REG_OFFSET (efl), REG_OFFSET (cs), REG_OFFSET (ss),
55   REG_OFFSET (ds), REG_OFFSET (es), REG_OFFSET (fs), REG_OFFSET (gs)
56 };
57
58 #define REG_ADDR(state, regnum) ((char *)(state) + reg_offset[regnum])
59 #define CREG_ADDR(state, regnum) ((const char *)(state) + reg_offset[regnum])
60
61 \f
62 /* Get the whole floating-point state of THREAD and record the values
63    of the corresponding (pseudo) registers.  */
64
65 static void
66 fetch_fpregs (struct regcache *regcache, struct proc *thread)
67 {
68   mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT;
69   struct i386_float_state state;
70   error_t err;
71
72   err = thread_get_state (thread->port, i386_FLOAT_STATE,
73                           (thread_state_t) &state, &count);
74   if (err)
75     {
76       warning (_("Couldn't fetch floating-point state from %s"),
77                proc_string (thread));
78       return;
79     }
80
81   if (!state.initialized)
82     {
83       /* The floating-point state isn't initialized.  */
84       i387_supply_fsave (regcache, -1, NULL);
85     }
86   else
87     {
88       /* Supply the floating-point registers.  */
89       i387_supply_fsave (regcache, -1, state.hw_state);
90     }
91 }
92
93 #ifdef HAVE_SYS_PROCFS_H
94 /* These two calls are used by the core-regset.c code for
95    reading ELF core files.  */
96 void
97 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregs)
98 {
99   int i;
100   for (i = 0; i < I386_NUM_GREGS; i++)
101     regcache_raw_supply (regcache, i, CREG_ADDR (gregs, i));
102 }
103
104 void
105 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregs)
106 {
107   i387_supply_fsave (regcache, -1, fpregs);
108 }
109 #endif
110
111 /* Fetch register REGNO, or all regs if REGNO is -1.  */
112 static void
113 gnu_fetch_registers (struct target_ops *ops,
114                      struct regcache *regcache, int regno)
115 {
116   struct proc *thread;
117
118   /* Make sure we know about new threads.  */
119   inf_update_procs (gnu_current_inf);
120
121   thread = inf_tid_to_thread (gnu_current_inf,
122                               ptid_get_tid (inferior_ptid));
123   if (!thread)
124     error (_("Can't fetch registers from thread %s: No such thread"),
125            target_pid_to_str (inferior_ptid));
126
127   if (regno < I386_NUM_GREGS || regno == -1)
128     {
129       thread_state_t state;
130
131       /* This does the dirty work for us.  */
132       state = proc_get_state (thread, 0);
133       if (!state)
134         {
135           warning (_("Couldn't fetch registers from %s"),
136                    proc_string (thread));
137           return;
138         }
139
140       if (regno == -1)
141         {
142           int i;
143
144           proc_debug (thread, "fetching all register");
145
146           for (i = 0; i < I386_NUM_GREGS; i++)
147             regcache_raw_supply (regcache, i, REG_ADDR (state, i));
148           thread->fetched_regs = ~0;
149         }
150       else
151         {
152           proc_debug (thread, "fetching register %s",
153                       gdbarch_register_name (get_regcache_arch (regcache),
154                                              regno));
155
156           regcache_raw_supply (regcache, regno,
157                                REG_ADDR (state, regno));
158           thread->fetched_regs |= (1 << regno);
159         }
160     }
161
162   if (regno >= I386_NUM_GREGS || regno == -1)
163     {
164       proc_debug (thread, "fetching floating-point registers");
165
166       fetch_fpregs (regcache, thread);
167     }
168 }
169 \f
170
171 /* Store the whole floating-point state into THREAD using information
172    from the corresponding (pseudo) registers.  */
173 static void
174 store_fpregs (const struct regcache *regcache, struct proc *thread, int regno)
175 {
176   mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT;
177   struct i386_float_state state;
178   error_t err;
179
180   err = thread_get_state (thread->port, i386_FLOAT_STATE,
181                           (thread_state_t) &state, &count);
182   if (err)
183     {
184       warning (_("Couldn't fetch floating-point state from %s"),
185                proc_string (thread));
186       return;
187     }
188
189   /* FIXME: kettenis/2001-07-15: Is this right?  Should we somehow
190      take into account DEPRECATED_REGISTER_VALID like the old code did?  */
191   i387_collect_fsave (regcache, regno, state.hw_state);
192
193   err = thread_set_state (thread->port, i386_FLOAT_STATE,
194                           (thread_state_t) &state, i386_FLOAT_STATE_COUNT);
195   if (err)
196     {
197       warning (_("Couldn't store floating-point state into %s"),
198                proc_string (thread));
199       return;
200     }
201 }
202
203 /* Store at least register REGNO, or all regs if REGNO == -1.  */
204 static void
205 gnu_store_registers (struct target_ops *ops,
206                      struct regcache *regcache, int regno)
207 {
208   struct proc *thread;
209   struct gdbarch *gdbarch = get_regcache_arch (regcache);
210
211   /* Make sure we know about new threads.  */
212   inf_update_procs (gnu_current_inf);
213
214   thread = inf_tid_to_thread (gnu_current_inf,
215                               ptid_get_tid (inferior_ptid));
216   if (!thread)
217     error (_("Couldn't store registers into thread %s: No such thread"),
218            target_pid_to_str (inferior_ptid));
219
220   if (regno < I386_NUM_GREGS || regno == -1)
221     {
222       thread_state_t state;
223       thread_state_data_t old_state;
224       int was_aborted = thread->aborted;
225       int was_valid = thread->state_valid;
226       int trace;
227
228       if (!was_aborted && was_valid)
229         memcpy (&old_state, &thread->state, sizeof (old_state));
230
231       state = proc_get_state (thread, 1);
232       if (!state)
233         {
234           warning (_("Couldn't store registers into %s"),
235                    proc_string (thread));
236           return;
237         }
238
239       /* Save the T bit.  We might try to restore the %eflags register
240          below, but changing the T bit would seriously confuse GDB.  */
241       trace = ((struct i386_thread_state *)state)->efl & 0x100;
242
243       if (!was_aborted && was_valid)
244         /* See which registers have changed after aborting the thread.  */
245         {
246           int check_regno;
247
248           for (check_regno = 0; check_regno < I386_NUM_GREGS; check_regno++)
249             if ((thread->fetched_regs & (1 << check_regno))
250                 && memcpy (REG_ADDR (&old_state, check_regno),
251                            REG_ADDR (state, check_regno),
252                            register_size (gdbarch, check_regno)))
253               /* Register CHECK_REGNO has changed!  Ack!  */
254               {
255                 warning (_("Register %s changed after the thread was aborted"),
256                          gdbarch_register_name (gdbarch, check_regno));
257                 if (regno >= 0 && regno != check_regno)
258                   /* Update GDB's copy of the register.  */
259                   regcache_raw_supply (regcache, check_regno,
260                                        REG_ADDR (state, check_regno));
261                 else
262                   warning (_("... also writing this register!  "
263                              "Suspicious..."));
264               }
265         }
266
267       if (regno == -1)
268         {
269           int i;
270
271           proc_debug (thread, "storing all registers");
272
273           for (i = 0; i < I386_NUM_GREGS; i++)
274             if (REG_VALID == regcache_register_status (regcache, i))
275               regcache_raw_collect (regcache, i, REG_ADDR (state, i));
276         }
277       else
278         {
279           proc_debug (thread, "storing register %s",
280                       gdbarch_register_name (gdbarch, regno));
281
282           gdb_assert (REG_VALID == regcache_register_status (regcache, regno));
283           regcache_raw_collect (regcache, regno, REG_ADDR (state, regno));
284         }
285
286       /* Restore the T bit.  */
287       ((struct i386_thread_state *)state)->efl &= ~0x100;
288       ((struct i386_thread_state *)state)->efl |= trace;
289     }
290
291   if (regno >= I386_NUM_GREGS || regno == -1)
292     {
293       proc_debug (thread, "storing floating-point registers");
294
295       store_fpregs (regcache, thread, regno);
296     }
297 }
298
299 /* Provide a prototype to silence -Wmissing-prototypes.  */
300 extern initialize_file_ftype _initialize_i386gnu_nat;
301
302 void
303 _initialize_i386gnu_nat (void)
304 {
305   struct target_ops *t;
306
307   /* Fill in the generic GNU/Hurd methods.  */
308   t = gnu_target ();
309
310   t->to_fetch_registers = gnu_fetch_registers;
311   t->to_store_registers = gnu_store_registers;
312
313   /* Register the target.  */
314   add_target (t);
315 }