* Makefile.in (symfile.o): Add gdb-stabs.h to dependencies list.
[deliverable/binutils-gdb.git] / gdb / gdbserver / low-nbsd.c
CommitLineData
d6e9fb05
JK
1/* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1986, 1987, 1993, 2000 Free Software Foundation, Inc.
3
4This file is part of GDB.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20#include "defs.h"
21#include <sys/types.h>
22#include <sys/wait.h>
23#include "frame.h"
24#include "inferior.h"
25
26#include <stdio.h>
27#include <errno.h>
28
29/***************Begin MY defs*********************/
30int quit_flag = 0;
31static char my_registers[REGISTER_BYTES];
32char *registers = my_registers;
33
34/* Index within `registers' of the first byte of the space for
35 register N. */
36
37char buf2[MAX_REGISTER_RAW_SIZE];
38/***************End MY defs*********************/
39
40#include <sys/ptrace.h>
41#include <machine/reg.h>
42
43extern int sys_nerr;
44// extern char **sys_errlist;
45extern char **environ;
46extern int inferior_pid;
47void quit (), perror_with_name ();
48
9cebe72f
C
49#define RF(dst, src) \
50 memcpy(&registers[REGISTER_BYTE(dst)], &src, sizeof(src))
51
52#define RS(src, dst) \
53 memcpy(&dst, &registers[REGISTER_BYTE(src)], sizeof(dst))
54
55#ifdef __i386__
56struct env387
57 {
58 unsigned short control;
59 unsigned short r0;
60 unsigned short status;
61 unsigned short r1;
62 unsigned short tag;
63 unsigned short r2;
64 unsigned long eip;
65 unsigned short code_seg;
66 unsigned short opcode;
67 unsigned long operand;
68 unsigned short operand_seg;
69 unsigned short r3;
70 unsigned char regs[8][10];
71 };
72
d6e9fb05
JK
73/* i386_register_raw_size[i] is the number of bytes of storage in the
74 actual machine representation for register i. */
75int i386_register_raw_size[MAX_NUM_REGS] = {
76 4, 4, 4, 4,
77 4, 4, 4, 4,
78 4, 4, 4, 4,
79 4, 4, 4, 4,
80 10, 10, 10, 10,
81 10, 10, 10, 10,
82 4, 4, 4, 4,
83 4, 4, 4, 4,
84 16, 16, 16, 16,
85 16, 16, 16, 16,
86 4
87};
88
89int i386_register_byte[MAX_NUM_REGS];
90
91static void
fba45db2 92initialize_arch (void)
d6e9fb05
JK
93{
94 /* Initialize the table saying where each register starts in the
95 register file. */
96 {
97 int i, offset;
98
99 offset = 0;
100 for (i = 0; i < MAX_NUM_REGS; i++)
101 {
102 i386_register_byte[i] = offset;
103 offset += i386_register_raw_size[i];
104 }
105 }
106}
9cebe72f 107#endif /* !__i386__ */
d6e9fb05 108
9cebe72f 109#ifdef __powerpc__
22c72081
C
110#include "ppc-tdep.h"
111
9cebe72f 112static void
fba45db2 113initialize_arch (void)
9cebe72f
C
114{
115}
116#endif /* !__powerpc__ */
d6e9fb05
JK
117
118
119/* Start an inferior process and returns its pid.
120 ALLARGS is a vector of program-name and args.
121 ENV is the environment vector to pass. */
122
123int
fba45db2 124create_inferior (char *program, char **allargs)
d6e9fb05
JK
125{
126 int pid;
127
128 pid = fork ();
129 if (pid < 0)
130 perror_with_name ("fork");
131
132 if (pid == 0)
133 {
134 ptrace (PT_TRACE_ME, 0, 0, 0);
135
136 execv (program, allargs);
137
138 fprintf (stderr, "Cannot exec %s: %s.\n", program,
139 errno < sys_nerr ? sys_errlist[errno] : "unknown error");
140 fflush (stderr);
141 _exit (0177);
142 }
143
144 return pid;
145}
146
147/* Kill the inferior process. Make us have no inferior. */
148
149void
fba45db2 150kill_inferior (void)
d6e9fb05
JK
151{
152 if (inferior_pid == 0)
153 return;
154 ptrace (PT_KILL, inferior_pid, 0, 0);
155 wait (0);
156 /*************inferior_died ();****VK**************/
157}
158
159/* Return nonzero if the given thread is still alive. */
160int
fba45db2 161mythread_alive (int pid)
d6e9fb05
JK
162{
163 return 1;
164}
165
166/* Wait for process, returns status */
167
168unsigned char
fba45db2 169mywait (char *status)
d6e9fb05
JK
170{
171 int pid;
172 int w;
173
174 pid = wait (&w);
175 if (pid != inferior_pid)
176 perror_with_name ("wait");
177
178 if (WIFEXITED (w))
179 {
180 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
181 *status = 'W';
182 return ((unsigned char) WEXITSTATUS (w));
183 }
184 else if (!WIFSTOPPED (w))
185 {
186 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
187 *status = 'X';
188 return ((unsigned char) WTERMSIG (w));
189 }
190
191 fetch_inferior_registers (0);
192
193 *status = 'T';
194 return ((unsigned char) WSTOPSIG (w));
195}
196
197/* Resume execution of the inferior process.
198 If STEP is nonzero, single-step it.
199 If SIGNAL is nonzero, give it that signal. */
200
201void
fba45db2 202myresume (int step, int signal)
d6e9fb05
JK
203{
204 errno = 0;
205 ptrace (step ? PT_STEP : PT_CONTINUE, inferior_pid,
206 (PTRACE_ARG3_TYPE) 1, signal);
207 if (errno)
208 perror_with_name ("ptrace");
209}
210
9cebe72f
C
211
212#ifdef __i386__
d6e9fb05
JK
213/* Fetch one or more registers from the inferior. REGNO == -1 to get
214 them all. We actually fetch more than requested, when convenient,
215 marking them as valid so we won't fetch them again. */
216
217void
fba45db2 218fetch_inferior_registers (int ignored)
d6e9fb05
JK
219{
220 struct reg inferior_registers;
9cebe72f 221 struct env387 inferior_fp_registers;
d6e9fb05
JK
222
223 ptrace (PT_GETREGS, inferior_pid,
224 (PTRACE_ARG3_TYPE) &inferior_registers, 0);
d6e9fb05
JK
225 ptrace (PT_GETFPREGS, inferior_pid,
226 (PTRACE_ARG3_TYPE) &inferior_fp_registers, 0);
9cebe72f
C
227
228 RF ( 0, inferior_registers.r_eax);
229 RF ( 1, inferior_registers.r_ecx);
230 RF ( 2, inferior_registers.r_edx);
231 RF ( 3, inferior_registers.r_ebx);
232 RF ( 4, inferior_registers.r_esp);
233 RF ( 5, inferior_registers.r_ebp);
234 RF ( 6, inferior_registers.r_esi);
235 RF ( 7, inferior_registers.r_edi);
236 RF ( 8, inferior_registers.r_eip);
237 RF ( 9, inferior_registers.r_eflags);
238 RF (10, inferior_registers.r_cs);
239 RF (11, inferior_registers.r_ss);
240 RF (12, inferior_registers.r_ds);
241 RF (13, inferior_registers.r_es);
242 RF (14, inferior_registers.r_fs);
243 RF (15, inferior_registers.r_gs);
244
0c9f8a69
C
245 RF (FP0_REGNUM, inferior_fp_registers.regs[0]);
246 RF (FP0_REGNUM + 1, inferior_fp_registers.regs[1]);
247 RF (FP0_REGNUM + 2, inferior_fp_registers.regs[2]);
248 RF (FP0_REGNUM + 3, inferior_fp_registers.regs[3]);
249 RF (FP0_REGNUM + 4, inferior_fp_registers.regs[4]);
250 RF (FP0_REGNUM + 5, inferior_fp_registers.regs[5]);
251 RF (FP0_REGNUM + 6, inferior_fp_registers.regs[6]);
252 RF (FP0_REGNUM + 7, inferior_fp_registers.regs[7]);
9cebe72f 253
0c9f8a69
C
254 RF (FCTRL_REGNUM, inferior_fp_registers.control);
255 RF (FSTAT_REGNUM, inferior_fp_registers.status);
256 RF (FTAG_REGNUM, inferior_fp_registers.tag);
257 RF (FCS_REGNUM, inferior_fp_registers.code_seg);
258 RF (FCOFF_REGNUM, inferior_fp_registers.eip);
259 RF (FDS_REGNUM, inferior_fp_registers.operand_seg);
260 RF (FDOFF_REGNUM, inferior_fp_registers.operand);
261 RF (FOP_REGNUM, inferior_fp_registers.opcode);
d6e9fb05
JK
262}
263
264/* Store our register values back into the inferior.
265 If REGNO is -1, do this for all registers.
266 Otherwise, REGNO specifies which register (so we can save time). */
267
268void
fba45db2 269store_inferior_registers (int ignored)
d6e9fb05
JK
270{
271 struct reg inferior_registers;
9cebe72f
C
272 struct env387 inferior_fp_registers;
273
274 RS ( 0, inferior_registers.r_eax);
275 RS ( 1, inferior_registers.r_ecx);
276 RS ( 2, inferior_registers.r_edx);
277 RS ( 3, inferior_registers.r_ebx);
278 RS ( 4, inferior_registers.r_esp);
279 RS ( 5, inferior_registers.r_ebp);
280 RS ( 6, inferior_registers.r_esi);
281 RS ( 7, inferior_registers.r_edi);
282 RS ( 8, inferior_registers.r_eip);
283 RS ( 9, inferior_registers.r_eflags);
284 RS (10, inferior_registers.r_cs);
285 RS (11, inferior_registers.r_ss);
286 RS (12, inferior_registers.r_ds);
287 RS (13, inferior_registers.r_es);
288 RS (14, inferior_registers.r_fs);
289 RS (15, inferior_registers.r_gs);
290
0c9f8a69
C
291 RS (FP0_REGNUM, inferior_fp_registers.regs[0]);
292 RS (FP0_REGNUM + 1, inferior_fp_registers.regs[1]);
293 RS (FP0_REGNUM + 2, inferior_fp_registers.regs[2]);
294 RS (FP0_REGNUM + 3, inferior_fp_registers.regs[3]);
295 RS (FP0_REGNUM + 4, inferior_fp_registers.regs[4]);
296 RS (FP0_REGNUM + 5, inferior_fp_registers.regs[5]);
297 RS (FP0_REGNUM + 6, inferior_fp_registers.regs[6]);
298 RS (FP0_REGNUM + 7, inferior_fp_registers.regs[7]);
9cebe72f 299
0c9f8a69
C
300 RS (FCTRL_REGNUM, inferior_fp_registers.control);
301 RS (FSTAT_REGNUM, inferior_fp_registers.status);
302 RS (FTAG_REGNUM, inferior_fp_registers.tag);
303 RS (FCS_REGNUM, inferior_fp_registers.code_seg);
304 RS (FCOFF_REGNUM, inferior_fp_registers.eip);
305 RS (FDS_REGNUM, inferior_fp_registers.operand_seg);
306 RS (FDOFF_REGNUM, inferior_fp_registers.operand);
307 RS (FOP_REGNUM, inferior_fp_registers.opcode);
d6e9fb05 308
d6e9fb05
JK
309 ptrace (PT_SETREGS, inferior_pid,
310 (PTRACE_ARG3_TYPE) &inferior_registers, 0);
d6e9fb05
JK
311 ptrace (PT_SETFPREGS, inferior_pid,
312 (PTRACE_ARG3_TYPE) &inferior_fp_registers, 0);
d6e9fb05 313}
9cebe72f
C
314#endif /* !__i386__ */
315
316#ifdef __powerpc__
317/* Fetch one or more registers from the inferior. REGNO == -1 to get
318 them all. We actually fetch more than requested, when convenient,
319 marking them as valid so we won't fetch them again. */
320
321void
fba45db2 322fetch_inferior_registers (int regno)
9cebe72f
C
323{
324 struct reg inferior_registers;
22c72081 325#ifdef PT_GETFPREGS
9cebe72f 326 struct fpreg inferior_fp_registers;
22c72081 327#endif
9cebe72f
C
328 int i;
329
330 ptrace (PT_GETREGS, inferior_pid,
331 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
9cebe72f
C
332 for (i = 0; i < 32; i++)
333 RF (i, inferior_registers.fixreg[i]);
22c72081
C
334 RF (PPC_LR_REGNUM, inferior_registers.lr);
335 RF (PPC_CR_REGNUM, inferior_registers.cr);
336 RF (PPC_XER_REGNUM, inferior_registers.xer);
337 RF (PPC_CTR_REGNUM, inferior_registers.ctr);
9cebe72f
C
338 RF (PC_REGNUM, inferior_registers.pc);
339
22c72081
C
340#ifdef PT_GETFPREGS
341 ptrace (PT_GETFPREGS, inferior_pid,
342 (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0);
9cebe72f
C
343 for (i = 0; i < 32; i++)
344 RF (FP0_REGNUM + i, inferior_fp_registers.r_regs[i]);
22c72081 345#endif
9cebe72f
C
346}
347
348/* Store our register values back into the inferior.
349 If REGNO is -1, do this for all registers.
350 Otherwise, REGNO specifies which register (so we can save time). */
351
352void
fba45db2 353store_inferior_registers (int regno)
9cebe72f
C
354{
355 struct reg inferior_registers;
22c72081 356#ifdef PT_SETFPREGS
9cebe72f 357 struct fpreg inferior_fp_registers;
22c72081 358#endif
9cebe72f
C
359 int i;
360
361 for (i = 0; i < 32; i++)
362 RS (i, inferior_registers.fixreg[i]);
22c72081
C
363 RS (PPC_LR_REGNUM, inferior_registers.lr);
364 RS (PPC_CR_REGNUM, inferior_registers.cr);
365 RS (PPC_XER_REGNUM, inferior_registers.xer);
366 RS (PPC_CTR_REGNUM, inferior_registers.ctr);
9cebe72f 367 RS (PC_REGNUM, inferior_registers.pc);
22c72081
C
368 ptrace (PT_SETREGS, inferior_pid,
369 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
9cebe72f 370
22c72081 371#ifdef PT_SETFPREGS
9cebe72f
C
372 for (i = 0; i < 32; i++)
373 RS (FP0_REGNUM + i, inferior_fp_registers.r_regs[i]);
9cebe72f
C
374 ptrace (PT_SETFPREGS, inferior_pid,
375 (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0);
22c72081 376#endif
9cebe72f
C
377}
378#endif /* !__powerpc__ */
d6e9fb05
JK
379
380/* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
381 in the NEW_SUN_PTRACE case.
382 It ought to be straightforward. But it appears that writing did
383 not write the data that I specified. I cannot understand where
384 it got the data that it actually did write. */
385
386/* Copy LEN bytes from inferior's memory starting at MEMADDR
387 to debugger memory starting at MYADDR. */
388
fba45db2 389read_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len)
d6e9fb05
JK
390{
391 register int i;
392 /* Round starting address down to longword boundary. */
393 register CORE_ADDR addr = memaddr & -sizeof (int);
394 /* Round ending address up; get number of longwords that makes. */
395 register int count
396 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
397 /* Allocate buffer of that many longwords. */
398 register int *buffer = (int *) alloca (count * sizeof (int));
399
400 /* Read all the longwords */
401 for (i = 0; i < count; i++, addr += sizeof (int))
402 {
403 buffer[i] = ptrace (PT_READ_D, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
404 }
405
406 /* Copy appropriate bytes out of the buffer. */
407 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
408}
409
410/* Copy LEN bytes of data from debugger memory at MYADDR
411 to inferior's memory at MEMADDR.
412 On failure (cannot write the inferior)
413 returns the value of errno. */
414
415int
fba45db2 416write_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len)
d6e9fb05
JK
417{
418 register int i;
419 /* Round starting address down to longword boundary. */
420 register CORE_ADDR addr = memaddr & -sizeof (int);
421 /* Round ending address up; get number of longwords that makes. */
422 register int count
423 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
424 /* Allocate buffer of that many longwords. */
425 register int *buffer = (int *) alloca (count * sizeof (int));
426 extern int errno;
427
428 /* Fill start and end extra bytes of buffer with existing memory data. */
429
430 buffer[0] = ptrace (PT_READ_D, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
431
432 if (count > 1)
433 {
434 buffer[count - 1]
435 = ptrace (PT_READ_D, inferior_pid,
436 (PTRACE_ARG3_TYPE) addr + (count - 1) * sizeof (int), 0);
437 }
438
439 /* Copy data to be written over corresponding part of buffer */
440
441 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
442
443 /* Write the entire buffer. */
444
445 for (i = 0; i < count; i++, addr += sizeof (int))
446 {
447 errno = 0;
448 ptrace (PT_WRITE_D, inferior_pid, (PTRACE_ARG3_TYPE) addr, buffer[i]);
449 if (errno)
450 return errno;
451 }
452
453 return 0;
454}
455\f
456void
fba45db2 457initialize_low (void)
d6e9fb05
JK
458{
459 initialize_arch ();
460}
This page took 0.07801 seconds and 4 git commands to generate.