OSDN Git Service

*** empty log message ***
[pf3gnuchains/sourceware.git] / gdb / hppa-hpux-nat.c
1 /* Native-dependent code for PA-RISC HP-UX.
2
3    Copyright (C) 2004, 2005, 2007, 2008, 2009, 2010, 2011
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 3 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, see <http://www.gnu.org/licenses/>.  */
20
21 #include "defs.h"
22 #include "inferior.h"
23 #include "regcache.h"
24 #include "target.h"
25
26 #include "gdb_assert.h"
27 #include <sys/ptrace.h>
28 #include <sys/utsname.h>
29 #include <machine/save_state.h>
30
31 #ifdef HAVE_TTRACE
32 #include <sys/ttrace.h>
33 #endif
34
35 #include "hppa-tdep.h"
36 #include "solib-som.h"
37 #include "inf-ptrace.h"
38 #include "inf-ttrace.h"
39
40 /* Return the offset of register REGNUM within `struct save_state'.
41    The offset returns depends on the flags in the "flags" register and
42    the register size (32-bit or 64-bit).  These are taken from
43    REGCACHE.  */
44
45 LONGEST
46 hppa_hpux_save_state_offset (struct regcache *regcache, int regnum)
47 {
48   LONGEST offset;
49
50   if (regnum == HPPA_FLAGS_REGNUM)
51     return ssoff (ss_flags);
52
53   if (HPPA_R0_REGNUM < regnum && regnum < HPPA_FP0_REGNUM)
54     {
55       struct gdbarch *arch = get_regcache_arch (regcache);
56       size_t size = register_size (arch, HPPA_R1_REGNUM);
57       ULONGEST flags;
58
59       gdb_assert (size == 4 || size == 8);
60
61       regcache_cooked_read_unsigned (regcache, HPPA_FLAGS_REGNUM, &flags);
62       if (flags & SS_WIDEREGS)
63         offset = ssoff (ss_wide) + (8 - size) + (regnum - HPPA_R0_REGNUM) * 8;
64       else
65         offset = ssoff (ss_narrow) + (regnum - HPPA_R1_REGNUM) * 4;
66     }
67   else
68     {
69       struct gdbarch *arch = get_regcache_arch (regcache);
70       size_t size = register_size (arch, HPPA_FP0_REGNUM);
71
72       gdb_assert (size == 4 || size == 8);
73       gdb_assert (regnum >= HPPA_FP0_REGNUM);
74       offset = ssoff(ss_fpblock) + (regnum - HPPA_FP0_REGNUM) * size;
75     }
76
77   gdb_assert (offset < sizeof (save_state_t));
78   return offset;
79 }
80
81 /* Just in case a future version of PA-RISC HP-UX won't have ptrace(2)
82    at all.  */
83 #ifndef PTRACE_TYPE_RET
84 #define PTRACE_TYPE_RET void
85 #endif
86
87 static void
88 hppa_hpux_fetch_register (struct regcache *regcache, int regnum)
89 {
90   struct gdbarch *gdbarch = get_regcache_arch (regcache);
91   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
92   CORE_ADDR addr;
93   size_t size;
94   PTRACE_TYPE_RET *buf;
95   pid_t pid;
96   int i;
97
98   pid = ptid_get_pid (inferior_ptid);
99
100   /* This isn't really an address, but ptrace thinks of it as one.  */
101   addr = hppa_hpux_save_state_offset (regcache, regnum);
102   size = register_size (gdbarch, regnum);
103
104   gdb_assert (size == 4 || size == 8);
105   buf = alloca (size);
106
107 #ifdef HAVE_TTRACE
108   {
109     lwpid_t lwp = ptid_get_lwp (inferior_ptid);
110
111     if (ttrace (TT_LWP_RUREGS, pid, lwp, addr, size, (uintptr_t)buf) == -1)
112       error (_("Couldn't read register %s (#%d): %s"),
113              gdbarch_register_name (gdbarch, regnum),
114              regnum, safe_strerror (errno));
115   }
116 #else
117   {
118     int i;
119
120     /* Read the register contents from the inferior a chuck at the time.  */
121     for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
122       {
123         errno = 0;
124         buf[i] = ptrace (PT_RUREGS, pid, (PTRACE_TYPE_ARG3) addr, 0, 0);
125         if (errno != 0)
126           error (_("Couldn't read register %s (#%d): %s"),
127                  gdbarch_register_name (gdbarch, regnum),
128                  regnum, safe_strerror (errno));
129
130         addr += sizeof (PTRACE_TYPE_RET);
131       }
132   }
133 #endif
134
135   /* Take care with the "flags" register.  It's stored as an `int' in
136      `struct save_state', even for 64-bit code.  */
137   if (regnum == HPPA_FLAGS_REGNUM && size == 8)
138     {
139       ULONGEST flags;
140       flags = extract_unsigned_integer ((gdb_byte *)buf, 4, byte_order);
141       store_unsigned_integer ((gdb_byte *)buf, 8, byte_order, flags);
142     }
143
144   regcache_raw_supply (regcache, regnum, buf);
145 }
146
147 static void
148 hppa_hpux_fetch_inferior_registers (struct target_ops *ops,
149                                     struct regcache *regcache, int regnum)
150 {
151   if (regnum == -1)
152     for (regnum = 0;
153          regnum < gdbarch_num_regs (get_regcache_arch (regcache));
154          regnum++)
155       hppa_hpux_fetch_register (regcache, regnum);
156   else
157     hppa_hpux_fetch_register (regcache, regnum);
158 }
159
160 /* Store register REGNUM into the inferior.  */
161
162 static void
163 hppa_hpux_store_register (struct regcache *regcache, int regnum)
164 {
165   struct gdbarch *gdbarch = get_regcache_arch (regcache);
166   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
167   CORE_ADDR addr;
168   size_t size;
169   PTRACE_TYPE_RET *buf;
170   pid_t pid;
171
172   pid = ptid_get_pid (inferior_ptid);
173
174   /* This isn't really an address, but ptrace thinks of it as one.  */
175   addr = hppa_hpux_save_state_offset (regcache, regnum);
176   size = register_size (gdbarch, regnum);
177
178   gdb_assert (size == 4 || size == 8);
179   buf = alloca (size);
180
181   regcache_raw_collect (regcache, regnum, buf);
182
183   /* Take care with the "flags" register.  It's stored as an `int' in
184      `struct save_state', even for 64-bit code.  */
185   if (regnum == HPPA_FLAGS_REGNUM && size == 8)
186     {
187       ULONGEST flags;
188       flags = extract_unsigned_integer ((gdb_byte *)buf, 8, byte_order);
189       store_unsigned_integer ((gdb_byte *)buf, 4, byte_order, flags);
190       size = 4;
191     }
192
193 #ifdef HAVE_TTRACE
194   {
195     lwpid_t lwp = ptid_get_lwp (inferior_ptid);
196
197     if (ttrace (TT_LWP_WUREGS, pid, lwp, addr, size, (uintptr_t)buf) == -1)
198       error (_("Couldn't write register %s (#%d): %s"),
199              gdbarch_register_name (gdbarch, regnum),
200              regnum, safe_strerror (errno));
201   }
202 #else
203   {
204     int i;
205
206     /* Write the register contents into the inferior a chunk at the time.  */
207     for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
208       {
209         errno = 0;
210         ptrace (PT_WUREGS, pid, (PTRACE_TYPE_ARG3) addr, buf[i], 0);
211         if (errno != 0)
212           error (_("Couldn't write register %s (#%d): %s"),
213                  gdbarch_register_name (gdbarch, regnum),
214                  regnum, safe_strerror (errno));
215
216         addr += sizeof (PTRACE_TYPE_RET);
217       }
218   }
219 #endif
220 }
221
222 /* Store register REGNUM back into the inferior.  If REGNUM is -1, do
223    this for all registers (including the floating point registers).  */
224
225 static void
226 hppa_hpux_store_inferior_registers (struct target_ops *ops,
227                                     struct regcache *regcache, int regnum)
228 {
229   if (regnum == -1)
230     for (regnum = 0;
231          regnum < gdbarch_num_regs (get_regcache_arch (regcache));
232          regnum++)
233       hppa_hpux_store_register (regcache, regnum);
234   else
235     hppa_hpux_store_register (regcache, regnum);
236 }
237
238 /* Set hpux_major_release variable to the value retrieved from a call to
239    uname function.  */
240
241 static void
242 set_hpux_major_release (void)
243 {
244   struct utsname x;
245   char *p;
246
247   uname (&x);
248   p = strchr (x.release, '.');
249   if (p)
250     hpux_major_release = atoi (p + 1);
251 }
252
253 \f
254
255 /* Prevent warning from -Wmissing-prototypes.  */
256 void _initialize_hppa_hpux_nat (void);
257
258 void
259 _initialize_hppa_hpux_nat (void)
260 {
261   struct target_ops *t;
262
263   set_hpux_major_release ();
264
265 #ifdef HAVE_TTRACE
266   t = inf_ttrace_target ();
267 #else
268   t = inf_ptrace_target ();
269 #endif
270
271   t->to_fetch_registers = hppa_hpux_fetch_inferior_registers;
272   t->to_store_registers = hppa_hpux_store_inferior_registers;
273
274   add_target (t);
275 }