2005-04-28 Andrew Cagney <cagney@gnu.org>
[deliverable/binutils-gdb.git] / gdb / hppa-hpux-nat.c
1 /* Native-dependent code for PA-RISC HP-UX.
2
3 Copyright 2004 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 2 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, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "defs.h"
23 #include "inferior.h"
24 #include "regcache.h"
25 #include "target.h"
26
27 #include "gdb_assert.h"
28 #include <sys/ptrace.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 "inf-ptrace.h"
37 #include "inf-ttrace.h"
38
39 /* Non-zero if we should pretend not to be a runnable target. */
40 int child_suppress_run = 0;
41
42 /* Return the offset of register REGNUM within `struct save_state'.
43 The offset returns depends on the flags in the "flags" register and
44 the register size (32-bit or 64-bit). These are taken from
45 REGCACHE. */
46
47 LONGEST
48 hppa_hpux_save_state_offset (struct regcache *regcache, int regnum)
49 {
50 LONGEST offset;
51
52 if (regnum == HPPA_FLAGS_REGNUM)
53 return ssoff (ss_flags);
54
55 if (HPPA_R0_REGNUM < regnum && regnum < HPPA_FP0_REGNUM)
56 {
57 struct gdbarch *arch = get_regcache_arch (regcache);
58 size_t size = register_size (arch, HPPA_R1_REGNUM);
59 ULONGEST flags;
60
61 gdb_assert (size == 4 || size == 8);
62
63 regcache_cooked_read_unsigned (regcache, HPPA_FLAGS_REGNUM, &flags);
64 if (flags & SS_WIDEREGS)
65 offset = ssoff (ss_wide) + (8 - size) + (regnum - HPPA_R0_REGNUM) * 8;
66 else
67 offset = ssoff (ss_narrow) + (regnum - HPPA_R1_REGNUM) * 4;
68 }
69 else
70 {
71 struct gdbarch *arch = get_regcache_arch (regcache);
72 size_t size = register_size (arch, HPPA_FP0_REGNUM);
73
74 gdb_assert (size == 4 || size == 8);
75 gdb_assert (regnum >= HPPA_FP0_REGNUM);
76 offset = ssoff(ss_fpblock) + (regnum - HPPA_FP0_REGNUM) * size;
77 }
78
79 gdb_assert (offset < sizeof (save_state_t));
80 return offset;
81 }
82
83 /* Just in case a future version of PA-RISC HP-UX won't have ptrace(2)
84 at all. */
85 #ifndef PTRACE_TYPE_RET
86 #define PTRACE_TYPE_RET void
87 #endif
88
89 static void
90 hppa_hpux_fetch_register (int regnum)
91 {
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(current_regcache, regnum);
102 size = register_size (current_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 REGISTER_NAME (regnum), regnum, safe_strerror (errno));
114 }
115 #else
116 {
117 int i;
118
119 /* Read the register contents from the inferior a chuck at the time. */
120 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
121 {
122 errno = 0;
123 buf[i] = ptrace (PT_RUREGS, pid, (PTRACE_TYPE_ARG3) addr, 0, 0);
124 if (errno != 0)
125 error (_("Couldn't read register %s (#%d): %s"),
126 REGISTER_NAME (regnum), regnum, safe_strerror (errno));
127
128 addr += sizeof (PTRACE_TYPE_RET);
129 }
130 }
131 #endif
132
133 /* Take care with the "flags" register. It's stored as an `int' in
134 `struct save_state', even for 64-bit code. */
135 if (regnum == HPPA_FLAGS_REGNUM && size == 8)
136 {
137 ULONGEST flags = extract_unsigned_integer (buf, 4);
138 store_unsigned_integer (buf, 8, flags);
139 }
140
141 regcache_raw_supply (current_regcache, regnum, buf);
142 }
143
144 static void
145 hppa_hpux_fetch_inferior_registers (int regnum)
146 {
147 if (regnum == -1)
148 for (regnum = 0; regnum < NUM_REGS; regnum++)
149 hppa_hpux_fetch_register (regnum);
150 else
151 hppa_hpux_fetch_register (regnum);
152 }
153
154 /* Store register REGNUM into the inferior. */
155
156 static void
157 hppa_hpux_store_register (int regnum)
158 {
159 CORE_ADDR addr;
160 size_t size;
161 PTRACE_TYPE_RET *buf;
162 pid_t pid;
163
164 pid = ptid_get_pid (inferior_ptid);
165
166 /* This isn't really an address, but ptrace thinks of it as one. */
167 addr = hppa_hpux_save_state_offset(current_regcache, regnum);
168 size = register_size (current_gdbarch, regnum);
169
170 gdb_assert (size == 4 || size == 8);
171 buf = alloca (size);
172
173 regcache_raw_collect (current_regcache, regnum, buf);
174
175 /* Take care with the "flags" register. It's stored as an `int' in
176 `struct save_state', even for 64-bit code. */
177 if (regnum == HPPA_FLAGS_REGNUM && size == 8)
178 {
179 ULONGEST flags = extract_unsigned_integer (buf, 8);
180 store_unsigned_integer (buf, 4, flags);
181 size = 4;
182 }
183
184 #ifdef HAVE_TTRACE
185 {
186 lwpid_t lwp = ptid_get_lwp (inferior_ptid);
187
188 if (ttrace (TT_LWP_WUREGS, pid, lwp, addr, size, (uintptr_t)buf) == -1)
189 error (_("Couldn't write register %s (#%d): %s"),
190 REGISTER_NAME (regnum), regnum, safe_strerror (errno));
191 }
192 #else
193 {
194 int i;
195
196 /* Write the register contents into the inferior a chunk at the time. */
197 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
198 {
199 errno = 0;
200 ptrace (PT_WUREGS, pid, (PTRACE_TYPE_ARG3) addr, buf[i], 0);
201 if (errno != 0)
202 error (_("Couldn't write register %s (#%d): %s"),
203 REGISTER_NAME (regnum), regnum, safe_strerror (errno));
204
205 addr += sizeof (PTRACE_TYPE_RET);
206 }
207 }
208 #endif
209 }
210
211 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
212 this for all registers (including the floating point registers). */
213
214 static void
215 hppa_hpux_store_inferior_registers (int regnum)
216 {
217 if (regnum == -1)
218 for (regnum = 0; regnum < NUM_REGS; regnum++)
219 hppa_hpux_store_register (regnum);
220 else
221 hppa_hpux_store_register (regnum);
222 }
223
224 static int
225 hppa_hpux_child_can_run (void)
226 {
227 /* This variable is controlled by modules that layer their own
228 process structure atop that provided here. The code in
229 hpux-thread.c does this to support the HP-UX user-mode DCE
230 threads. */
231 return !child_suppress_run;
232 }
233 \f
234
235 /* Prevent warning from -Wmissing-prototypes. */
236 void _initialize_hppa_hpux_nat (void);
237
238 void
239 _initialize_hppa_hpux_nat (void)
240 {
241 struct target_ops *t;
242
243 #ifdef HAVE_TTRACE
244 t = inf_ttrace_target ();
245 #else
246 t = inf_ptrace_target ();
247 #endif
248
249 t->to_fetch_registers = hppa_hpux_fetch_inferior_registers;
250 t->to_store_registers = hppa_hpux_store_inferior_registers;
251 t->to_can_run = hppa_hpux_child_can_run;
252
253 add_target (t);
254 }
This page took 0.033794 seconds and 4 git commands to generate.