* target.h (struct regcache): Add forward declaration.
[deliverable/binutils-gdb.git] / gdb / hppa-hpux-nat.c
CommitLineData
eeb8076c
MK
1/* Native-dependent code for PA-RISC HP-UX.
2
6aba47ca 3 Copyright (C) 2004, 2005, 2007 Free Software Foundation, Inc.
eeb8076c
MK
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
197e01b6
EZ
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
eeb8076c
MK
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
eee22bf8
MK
31#ifdef HAVE_TTRACE
32#include <sys/ttrace.h>
33#endif
34
eeb8076c
MK
35#include "hppa-tdep.h"
36#include "inf-ptrace.h"
eee22bf8
MK
37#include "inf-ttrace.h"
38
eeca586f
MK
39/* Non-zero if we should pretend not to be a runnable target. */
40int child_suppress_run = 0;
eeb8076c 41
eeca586f
MK
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. */
3f6306ec 46
eeca586f
MK
47LONGEST
48hppa_hpux_save_state_offset (struct regcache *regcache, int regnum)
eeb8076c 49{
eeca586f 50 LONGEST offset;
eeb8076c 51
eeca586f
MK
52 if (regnum == HPPA_FLAGS_REGNUM)
53 return ssoff (ss_flags);
eeb8076c 54
eeca586f
MK
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;
eeb8076c
MK
81}
82
eee22bf8
MK
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
eeb8076c 89static void
56be3814 90hppa_hpux_fetch_register (struct regcache *regcache, int regnum)
eeb8076c
MK
91{
92 CORE_ADDR addr;
93 size_t size;
94 PTRACE_TYPE_RET *buf;
95 pid_t pid;
96 int i;
97
eee22bf8 98 pid = ptid_get_pid (inferior_ptid);
eeb8076c 99
eeca586f 100 /* This isn't really an address, but ptrace thinks of it as one. */
56be3814 101 addr = hppa_hpux_save_state_offset (regcache, regnum);
eeb8076c
MK
102 size = register_size (current_gdbarch, regnum);
103
eee22bf8 104 gdb_assert (size == 4 || size == 8);
eeb8076c
MK
105 buf = alloca (size);
106
eee22bf8
MK
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)
8a3fe4f8 112 error (_("Couldn't read register %s (#%d): %s"),
eee22bf8
MK
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)
8a3fe4f8 125 error (_("Couldn't read register %s (#%d): %s"),
eee22bf8
MK
126 REGISTER_NAME (regnum), regnum, safe_strerror (errno));
127
128 addr += sizeof (PTRACE_TYPE_RET);
129 }
130 }
131#endif
eeb8076c 132
eeca586f
MK
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 {
0d559b99
MK
137 ULONGEST flags = extract_unsigned_integer ((gdb_byte *)buf, 4);
138 store_unsigned_integer ((gdb_byte *)buf, 8, flags);
eeca586f
MK
139 }
140
56be3814 141 regcache_raw_supply (regcache, regnum, buf);
eeb8076c
MK
142}
143
144static void
56be3814 145hppa_hpux_fetch_inferior_registers (struct regcache *regcache, int regnum)
eeb8076c
MK
146{
147 if (regnum == -1)
148 for (regnum = 0; regnum < NUM_REGS; regnum++)
56be3814 149 hppa_hpux_fetch_register (regcache, regnum);
eeb8076c 150 else
56be3814 151 hppa_hpux_fetch_register (regcache, regnum);
eeb8076c
MK
152}
153
154/* Store register REGNUM into the inferior. */
155
156static void
56be3814 157hppa_hpux_store_register (struct regcache *regcache, int regnum)
eeb8076c
MK
158{
159 CORE_ADDR addr;
160 size_t size;
161 PTRACE_TYPE_RET *buf;
162 pid_t pid;
eeb8076c 163
eee22bf8 164 pid = ptid_get_pid (inferior_ptid);
eeb8076c 165
eeca586f 166 /* This isn't really an address, but ptrace thinks of it as one. */
56be3814 167 addr = hppa_hpux_save_state_offset (regcache, regnum);
eeb8076c
MK
168 size = register_size (current_gdbarch, regnum);
169
eee22bf8 170 gdb_assert (size == 4 || size == 8);
eeb8076c
MK
171 buf = alloca (size);
172
56be3814 173 regcache_raw_collect (regcache, regnum, buf);
eeb8076c 174
eeca586f
MK
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 {
0d559b99
MK
179 ULONGEST flags = extract_unsigned_integer ((gdb_byte *)buf, 8);
180 store_unsigned_integer ((gdb_byte *)buf, 4, flags);
eeca586f
MK
181 size = 4;
182 }
183
eee22bf8
MK
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)
8a3fe4f8 189 error (_("Couldn't write register %s (#%d): %s"),
eee22bf8
MK
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)
8a3fe4f8 202 error (_("Couldn't write register %s (#%d): %s"),
eee22bf8
MK
203 REGISTER_NAME (regnum), regnum, safe_strerror (errno));
204
205 addr += sizeof (PTRACE_TYPE_RET);
206 }
207 }
208#endif
eeb8076c
MK
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
214static void
56be3814 215hppa_hpux_store_inferior_registers (struct regcache *regcache, int regnum)
eeb8076c
MK
216{
217 if (regnum == -1)
218 for (regnum = 0; regnum < NUM_REGS; regnum++)
56be3814 219 hppa_hpux_store_register (regcache, regnum);
eeb8076c 220 else
56be3814 221 hppa_hpux_store_register (regcache, regnum);
eeb8076c 222}
3f6306ec
DA
223
224static int
225hppa_hpux_child_can_run (void)
226{
eeca586f 227 /* This variable is controlled by modules that layer their own
0d9b270c
MK
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. */
3f6306ec
DA
231 return !child_suppress_run;
232}
eeb8076c
MK
233\f
234
235/* Prevent warning from -Wmissing-prototypes. */
236void _initialize_hppa_hpux_nat (void);
237
238void
239_initialize_hppa_hpux_nat (void)
240{
241 struct target_ops *t;
242
eee22bf8
MK
243#ifdef HAVE_TTRACE
244 t = inf_ttrace_target ();
245#else
eeb8076c 246 t = inf_ptrace_target ();
eee22bf8
MK
247#endif
248
eeb8076c
MK
249 t->to_fetch_registers = hppa_hpux_fetch_inferior_registers;
250 t->to_store_registers = hppa_hpux_store_inferior_registers;
3f6306ec 251 t->to_can_run = hppa_hpux_child_can_run;
eee22bf8 252
eeb8076c
MK
253 add_target (t);
254}
This page took 0.24413 seconds and 4 git commands to generate.