]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/low-hppabsd.c
import gdb-1999-07-07 post reformat
[thirdparty/binutils-gdb.git] / gdb / gdbserver / low-hppabsd.c
1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1995 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include "defs.h"
22 #include <sys/wait.h>
23 #include "frame.h"
24 #include "inferior.h"
25
26 #include <stdio.h>
27 #include <sys/param.h>
28 #include <sys/dir.h>
29 #include <sys/user.h>
30 #include <signal.h>
31 #include <sys/ioctl.h>
32 #include <sgtty.h>
33 #include <fcntl.h>
34
35 /***************Begin MY defs*********************/
36 int quit_flag = 0;
37 char registers[REGISTER_BYTES];
38
39 /* Index within `registers' of the first byte of the space for
40 register N. */
41
42
43 char buf2[MAX_REGISTER_RAW_SIZE];
44 /***************End MY defs*********************/
45
46 #include <sys/ptrace.h>
47 #include <machine/reg.h>
48
49 extern char **environ;
50 extern int errno;
51 extern int inferior_pid;
52 void quit (), perror_with_name ();
53 int query ();
54
55 /* Start an inferior process and returns its pid.
56 ALLARGS is a vector of program-name and args.
57 ENV is the environment vector to pass. */
58
59 int
60 create_inferior (program, allargs)
61 char *program;
62 char **allargs;
63 {
64 int pid;
65
66 pid = fork ();
67 if (pid < 0)
68 perror_with_name ("fork");
69
70 if (pid == 0)
71 {
72 ptrace (PT_TRACE_ME, 0, 0, 0, 0);
73
74 execv (program, allargs);
75
76 fprintf (stderr, "Cannot exec %s: %s.\n", program,
77 errno < sys_nerr ? sys_errlist[errno] : "unknown error");
78 fflush (stderr);
79 _exit (0177);
80 }
81
82 return pid;
83 }
84
85 /* Kill the inferior process. Make us have no inferior. */
86
87 void
88 kill_inferior ()
89 {
90 if (inferior_pid == 0)
91 return;
92 ptrace (8, inferior_pid, 0, 0, 0);
93 wait (0);
94 /*************inferior_died ();****VK**************/
95 }
96
97 /* Return nonzero if the given thread is still alive. */
98 int
99 mythread_alive (pid)
100 int pid;
101 {
102 return 1;
103 }
104
105 /* Wait for process, returns status */
106
107 unsigned char
108 mywait (status)
109 char *status;
110 {
111 int pid;
112 union wait w;
113
114 pid = wait (&w);
115 if (pid != inferior_pid)
116 perror_with_name ("wait");
117
118 if (WIFEXITED (w))
119 {
120 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
121 *status = 'W';
122 return ((unsigned char) WEXITSTATUS (w));
123 }
124 else if (!WIFSTOPPED (w))
125 {
126 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
127 *status = 'X';
128 return ((unsigned char) WTERMSIG (w));
129 }
130
131 fetch_inferior_registers (0);
132
133 *status = 'T';
134 return ((unsigned char) WSTOPSIG (w));
135 }
136
137 /* Resume execution of the inferior process.
138 If STEP is nonzero, single-step it.
139 If SIGNAL is nonzero, give it that signal. */
140
141 void
142 myresume (step, signal)
143 int step;
144 int signal;
145 {
146 errno = 0;
147 ptrace (step ? PT_STEP : PT_CONTINUE, inferior_pid, 1, signal, 0);
148 if (errno)
149 perror_with_name ("ptrace");
150 }
151
152
153 #if !defined (offsetof)
154 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
155 #endif
156
157 /* U_REGS_OFFSET is the offset of the registers within the u area. */
158 #if !defined (U_REGS_OFFSET)
159 #define U_REGS_OFFSET \
160 ptrace (PT_READ_U, inferior_pid, \
161 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
162 - KERNEL_U_ADDR
163 #endif
164
165 CORE_ADDR
166 register_addr (regno, blockend)
167 int regno;
168 CORE_ADDR blockend;
169 {
170 CORE_ADDR addr;
171
172 if (regno < 0 || regno >= ARCH_NUM_REGS)
173 error ("Invalid register number %d.", regno);
174
175 REGISTER_U_ADDR (addr, blockend, regno);
176
177 return addr;
178 }
179
180 /* Fetch one register. */
181
182 static void
183 fetch_register (regno)
184 int regno;
185 {
186 register unsigned int regaddr;
187 char buf[MAX_REGISTER_RAW_SIZE];
188 register int i;
189
190 /* Offset of registers within the u area. */
191 unsigned int offset;
192
193 offset = U_REGS_OFFSET;
194
195 regaddr = register_addr (regno, offset);
196 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
197 {
198 errno = 0;
199 *(int *) &registers[regno * 4 + i] = ptrace (PT_RUREGS, inferior_pid,
200 (PTRACE_ARG3_TYPE) regaddr, 0, 0);
201 regaddr += sizeof (int);
202 if (errno != 0)
203 {
204 /* Warning, not error, in case we are attached; sometimes the
205 kernel doesn't let us at the registers. */
206 char *err = strerror (errno);
207 char *msg = alloca (strlen (err) + 128);
208 sprintf (msg, "reading register %d: %s", regno, err);
209 error (msg);
210 goto error_exit;
211 }
212 }
213 error_exit:;
214 }
215
216 /* Fetch all registers, or just one, from the child process. */
217
218 void
219 fetch_inferior_registers (regno)
220 int regno;
221 {
222 if (regno == -1 || regno == 0)
223 for (regno = 0; regno < NUM_REGS; regno++)
224 fetch_register (regno);
225 else
226 fetch_register (regno);
227 }
228
229 /* Store our register values back into the inferior.
230 If REGNO is -1, do this for all registers.
231 Otherwise, REGNO specifies which register (so we can save time). */
232
233 void
234 store_inferior_registers (regno)
235 int regno;
236 {
237 register unsigned int regaddr;
238 char buf[80];
239 extern char registers[];
240 register int i;
241 unsigned int offset = U_REGS_OFFSET;
242 int scratch;
243
244 if (regno >= 0)
245 {
246 if (CANNOT_STORE_REGISTER (regno))
247 return;
248 regaddr = register_addr (regno, offset);
249 errno = 0;
250 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM)
251 {
252 scratch = *(int *) &registers[REGISTER_BYTE (regno)] | 0x3;
253 ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
254 scratch, 0);
255 if (errno != 0)
256 {
257 /* Error, even if attached. Failing to write these two
258 registers is pretty serious. */
259 sprintf (buf, "writing register number %d", regno);
260 perror_with_name (buf);
261 }
262 }
263 else
264 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
265 {
266 errno = 0;
267 ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
268 *(int *) &registers[REGISTER_BYTE (regno) + i], 0);
269 if (errno != 0)
270 {
271 /* Warning, not error, in case we are attached; sometimes the
272 kernel doesn't let us at the registers. */
273 char *err = strerror (errno);
274 char *msg = alloca (strlen (err) + 128);
275 sprintf (msg, "writing register %d: %s",
276 regno, err);
277 error (msg);
278 return;
279 }
280 regaddr += sizeof (int);
281 }
282 }
283 else
284 for (regno = 0; regno < NUM_REGS; regno++)
285 store_inferior_registers (regno);
286 }
287
288 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
289 in the NEW_SUN_PTRACE case.
290 It ought to be straightforward. But it appears that writing did
291 not write the data that I specified. I cannot understand where
292 it got the data that it actually did write. */
293
294 /* Copy LEN bytes from inferior's memory starting at MEMADDR
295 to debugger memory starting at MYADDR. */
296
297 read_inferior_memory (memaddr, myaddr, len)
298 CORE_ADDR memaddr;
299 char *myaddr;
300 int len;
301 {
302 register int i;
303 /* Round starting address down to longword boundary. */
304 register CORE_ADDR addr = memaddr & -sizeof (int);
305 /* Round ending address up; get number of longwords that makes. */
306 register int count
307 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
308 /* Allocate buffer of that many longwords. */
309 register int *buffer = (int *) alloca (count * sizeof (int));
310
311 /* Read all the longwords */
312 for (i = 0; i < count; i++, addr += sizeof (int))
313 {
314 buffer[i] = ptrace (1, inferior_pid, addr, 0, 0);
315 }
316
317 /* Copy appropriate bytes out of the buffer. */
318 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
319 }
320
321 /* Copy LEN bytes of data from debugger memory at MYADDR
322 to inferior's memory at MEMADDR.
323 On failure (cannot write the inferior)
324 returns the value of errno. */
325
326 int
327 write_inferior_memory (memaddr, myaddr, len)
328 CORE_ADDR memaddr;
329 char *myaddr;
330 int len;
331 {
332 register int i;
333 /* Round starting address down to longword boundary. */
334 register CORE_ADDR addr = memaddr & -sizeof (int);
335 /* Round ending address up; get number of longwords that makes. */
336 register int count
337 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
338 /* Allocate buffer of that many longwords. */
339 register int *buffer = (int *) alloca (count * sizeof (int));
340 extern int errno;
341
342 /* Fill start and end extra bytes of buffer with existing memory data. */
343
344 buffer[0] = ptrace (1, inferior_pid, addr, 0, 0);
345
346 if (count > 1)
347 {
348 buffer[count - 1]
349 = ptrace (1, inferior_pid,
350 addr + (count - 1) * sizeof (int), 0, 0);
351 }
352
353 /* Copy data to be written over corresponding part of buffer */
354
355 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
356
357 /* Write the entire buffer. */
358
359 for (i = 0; i < count; i++, addr += sizeof (int))
360 {
361 errno = 0;
362 ptrace (4, inferior_pid, addr, buffer[i], 0);
363 if (errno)
364 return errno;
365 }
366
367 return 0;
368 }
369 \f
370 void
371 initialize ()
372 {
373 inferior_pid = 0;
374 }
375
376 int
377 have_inferior_p ()
378 {
379 return inferior_pid != 0;
380 }