* linux-nat.c (linux_register_u_offset): Remove.
[external/binutils.git] / gdb / mips-linux-nat.c
1 /* Native-dependent code for GNU/Linux on MIPS processors.
2
3    Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
4    Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor,
21    Boston, MA 02110-1301, USA.  */
22
23 #include "defs.h"
24 #include "inferior.h"
25 #include "mips-tdep.h"
26 #include "target.h"
27 #include "linux-nat.h"
28 #include "mips-linux-tdep.h"
29 #include "gdbcore.h"
30
31 #include "gdb_proc_service.h"
32
33 #include <sys/ptrace.h>
34
35 #ifndef PTRACE_GET_THREAD_AREA
36 #define PTRACE_GET_THREAD_AREA 25
37 #endif
38
39 /* Assume that we have PTRACE_GETREGS et al. support.  If we do not,
40    we'll clear this and use PTRACE_PEEKUSER instead.  */
41 static int have_ptrace_regsets = 1;
42
43 /* Saved function pointers to fetch and store a single register using
44    PTRACE_PEEKUSER and PTRACE_POKEUSER.  */
45
46 void (*super_fetch_registers) (int);
47 void (*super_store_registers) (int);
48
49 /* Pseudo registers can not be read.  ptrace does not provide a way to
50    read (or set) MIPS_PS_REGNUM, and there's no point in reading or
51    setting MIPS_ZERO_REGNUM.  We also can not set BADVADDR, CAUSE, or
52    FCRIR via ptrace().  */
53
54 int
55 mips_linux_cannot_fetch_register (int regno)
56 {
57   if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
58     return 0;
59   else if (regno >= mips_regnum (current_gdbarch)->fp0
60            && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
61     return 0;
62   else if (regno == mips_regnum (current_gdbarch)->lo
63            || regno == mips_regnum (current_gdbarch)->hi
64            || regno == mips_regnum (current_gdbarch)->badvaddr
65            || regno == mips_regnum (current_gdbarch)->cause
66            || regno == mips_regnum (current_gdbarch)->pc
67            || regno == mips_regnum (current_gdbarch)->fp_control_status
68            || regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
69     return 0;
70   else
71     return 1;
72 }
73
74 int
75 mips_linux_cannot_store_register (int regno)
76 {
77   if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
78     return 0;
79   else if (regno >= FP0_REGNUM && regno <= FP0_REGNUM + 32)
80     return 0;
81   else if (regno == mips_regnum (current_gdbarch)->lo
82            || regno == mips_regnum (current_gdbarch)->hi
83            || regno == mips_regnum (current_gdbarch)->pc
84            || regno == mips_regnum (current_gdbarch)->fp_control_status)
85     return 0;
86   else
87     return 1;
88 }
89
90 /* Fetch the thread-local storage pointer for libthread_db.  */
91
92 ps_err_e
93 ps_get_thread_area (const struct ps_prochandle *ph,
94                     lwpid_t lwpid, int idx, void **base)
95 {
96   if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
97     return PS_ERR;
98
99   /* IDX is the bias from the thread pointer to the beginning of the
100      thread descriptor.  It has to be subtracted due to implementation
101      quirks in libthread_db.  */
102   *base = (void *) ((char *)*base - idx);
103
104   return PS_OK;
105 }
106
107 /* Fetch REGNO (or all registers if REGNO == -1) from the target
108    using PTRACE_GETREGS et al.  */
109
110 static void
111 mips64_linux_regsets_fetch_registers (int regno)
112 {
113   int is_fp;
114   int tid;
115
116   if (regno >= mips_regnum (current_gdbarch)->fp0
117       && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
118     is_fp = 1;
119   else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
120     is_fp = 1;
121   else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
122     is_fp = 1;
123   else
124     is_fp = 0;
125
126   tid = ptid_get_lwp (inferior_ptid);
127   if (tid == 0)
128     tid = ptid_get_pid (inferior_ptid);
129
130   if (regno == -1 || !is_fp)
131     {
132       mips64_elf_gregset_t regs;
133
134       if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
135         {
136           if (errno == EIO)
137             {
138               have_ptrace_regsets = 0;
139               return;
140             }
141           perror_with_name (_("Couldn't get registers"));
142         }
143
144       mips64_supply_gregset (&regs);
145     }
146
147   if (regno == -1 || is_fp)
148     {
149       mips64_elf_fpregset_t fp_regs;
150
151       if (ptrace (PTRACE_GETFPREGS, tid, 0L,
152                   (PTRACE_TYPE_ARG3) &fp_regs) == -1)
153         {
154           if (errno == EIO)
155             {
156               have_ptrace_regsets = 0;
157               return;
158             }
159           perror_with_name (_("Couldn't get FP registers"));
160         }
161
162       mips64_supply_fpregset (&fp_regs);
163     }
164 }
165
166 /* Store REGNO (or all registers if REGNO == -1) to the target
167    using PTRACE_SETREGS et al.  */
168
169 static void
170 mips64_linux_regsets_store_registers (int regno)
171 {
172   int is_fp;
173   int tid;
174
175   if (regno >= mips_regnum (current_gdbarch)->fp0
176       && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
177     is_fp = 1;
178   else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
179     is_fp = 1;
180   else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
181     is_fp = 1;
182   else
183     is_fp = 0;
184
185   tid = ptid_get_lwp (inferior_ptid);
186   if (tid == 0)
187     tid = ptid_get_pid (inferior_ptid);
188
189   if (regno == -1 || !is_fp)
190     {
191       mips64_elf_gregset_t regs;
192
193       if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
194         perror_with_name (_("Couldn't get registers"));
195
196       mips64_fill_gregset (&regs, regno);
197
198       if (ptrace (PTRACE_SETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
199         perror_with_name (_("Couldn't set registers"));
200     }
201
202   if (regno == -1 || is_fp)
203     {
204       mips64_elf_fpregset_t fp_regs;
205
206       if (ptrace (PTRACE_GETFPREGS, tid, 0L,
207                   (PTRACE_TYPE_ARG3) &fp_regs) == -1)
208         perror_with_name (_("Couldn't get FP registers"));
209
210       mips64_fill_fpregset (&fp_regs, regno);
211
212       if (ptrace (PTRACE_SETFPREGS, tid, 0L,
213                   (PTRACE_TYPE_ARG3) &fp_regs) == -1)
214         perror_with_name (_("Couldn't set FP registers"));
215     }
216 }
217
218 /* Fetch REGNO (or all registers if REGNO == -1) from the target
219    using any working method.  */
220
221 static void
222 mips64_linux_fetch_registers (int regnum)
223 {
224   /* Unless we already know that PTRACE_GETREGS does not work, try it.  */
225   if (have_ptrace_regsets)
226     mips64_linux_regsets_fetch_registers (regnum);
227
228   /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
229      back to PTRACE_PEEKUSER.  */
230   if (!have_ptrace_regsets)
231     super_fetch_registers (regnum);
232 }
233
234 /* Store REGNO (or all registers if REGNO == -1) to the target
235    using any working method.  */
236
237 static void
238 mips64_linux_store_registers (int regnum)
239 {
240   /* Unless we already know that PTRACE_GETREGS does not work, try it.  */
241   if (have_ptrace_regsets)
242     mips64_linux_regsets_store_registers (regnum);
243
244   /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
245      back to PTRACE_PEEKUSER.  */
246   if (!have_ptrace_regsets)
247     super_store_registers (regnum);
248 }
249
250 /* Return the address in the core dump or inferior of register
251    REGNO.  */
252
253 static CORE_ADDR
254 mips_linux_register_u_offset (int regno)
255 {
256   /* FIXME drow/2005-09-04: The hardcoded use of register_addr should go
257      away.  This requires disentangling the various definitions of it
258      (particularly alpha-nat.c's).  */
259   return register_addr (regno, 0);
260 }
261
262 void _initialize_mips_linux_nat (void);
263
264 void
265 _initialize_mips_linux_nat (void)
266 {
267   struct target_ops *t = linux_trad_target (mips_linux_register_u_offset);
268
269   super_fetch_registers = t->to_fetch_registers;
270   super_store_registers = t->to_store_registers;
271
272   t->to_fetch_registers = mips64_linux_fetch_registers;
273   t->to_store_registers = mips64_linux_store_registers;
274
275   linux_nat_add_target (t);
276 }