* gdbserver/low-hppabsd.c (read_inferior_memory): Add explicit
[binutils-gdb.git] / gdb / gdbserver / low-nbsd.c
1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright 1986, 1987, 1993, 2000, 2001 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, Boston, MA 02111-1307, USA. */
19
20 #include "server.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*********************/
30 static char my_registers[REGISTER_BYTES];
31 char *registers = my_registers;
32 /***************End MY defs*********************/
33
34 #include <sys/ptrace.h>
35 #include <machine/reg.h>
36
37 #define RF(dst, src) \
38 memcpy(&registers[REGISTER_BYTE(dst)], &src, sizeof(src))
39
40 #define RS(src, dst) \
41 memcpy(&dst, &registers[REGISTER_BYTE(src)], sizeof(dst))
42
43 #ifdef __i386__
44 struct env387
45 {
46 unsigned short control;
47 unsigned short r0;
48 unsigned short status;
49 unsigned short r1;
50 unsigned short tag;
51 unsigned short r2;
52 unsigned long eip;
53 unsigned short code_seg;
54 unsigned short opcode;
55 unsigned long operand;
56 unsigned short operand_seg;
57 unsigned short r3;
58 unsigned char regs[8][10];
59 };
60
61 /* i386_register_raw_size[i] is the number of bytes of storage in the
62 actual machine representation for register i. */
63 int i386_register_raw_size[MAX_NUM_REGS] = {
64 4, 4, 4, 4,
65 4, 4, 4, 4,
66 4, 4, 4, 4,
67 4, 4, 4, 4,
68 10, 10, 10, 10,
69 10, 10, 10, 10,
70 4, 4, 4, 4,
71 4, 4, 4, 4,
72 16, 16, 16, 16,
73 16, 16, 16, 16,
74 4
75 };
76
77 int i386_register_byte[MAX_NUM_REGS];
78
79 static void
80 initialize_arch (void)
81 {
82 /* Initialize the table saying where each register starts in the
83 register file. */
84 {
85 int i, offset;
86
87 offset = 0;
88 for (i = 0; i < MAX_NUM_REGS; i++)
89 {
90 i386_register_byte[i] = offset;
91 offset += i386_register_raw_size[i];
92 }
93 }
94 }
95 #endif /* !__i386__ */
96
97 #ifdef __m68k__
98 static void
99 initialize_arch (void)
100 {
101 }
102 #endif /* !__m68k__ */
103
104 #ifdef __ns32k__
105 static void
106 initialize_arch (void)
107 {
108 }
109 #endif /* !__ns32k__ */
110
111 #ifdef __powerpc__
112 #include "ppc-tdep.h"
113
114 static void
115 initialize_arch (void)
116 {
117 }
118 #endif /* !__powerpc__ */
119
120
121 /* Start an inferior process and returns its pid.
122 ALLARGS is a vector of program-name and args. */
123
124 int
125 create_inferior (char *program, char **allargs)
126 {
127 int pid;
128
129 pid = fork ();
130 if (pid < 0)
131 perror_with_name ("fork");
132
133 if (pid == 0)
134 {
135 ptrace (PT_TRACE_ME, 0, 0, 0);
136
137 execv (program, allargs);
138
139 fprintf (stderr, "Cannot exec %s: %s.\n", program,
140 errno < sys_nerr ? sys_errlist[errno] : "unknown error");
141 fflush (stderr);
142 _exit (0177);
143 }
144
145 return pid;
146 }
147
148 /* Kill the inferior process. Make us have no inferior. */
149
150 void
151 kill_inferior (void)
152 {
153 if (inferior_pid == 0)
154 return;
155 ptrace (PT_KILL, inferior_pid, 0, 0);
156 wait (0);
157 /*************inferior_died ();****VK**************/
158 }
159
160 /* Return nonzero if the given thread is still alive. */
161 int
162 mythread_alive (int pid)
163 {
164 return 1;
165 }
166
167 /* Wait for process, returns status */
168
169 unsigned char
170 mywait (char *status)
171 {
172 int pid;
173 int w;
174
175 pid = wait (&w);
176 if (pid != inferior_pid)
177 perror_with_name ("wait");
178
179 if (WIFEXITED (w))
180 {
181 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
182 *status = 'W';
183 return ((unsigned char) WEXITSTATUS (w));
184 }
185 else if (!WIFSTOPPED (w))
186 {
187 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
188 *status = 'X';
189 return ((unsigned char) WTERMSIG (w));
190 }
191
192 fetch_inferior_registers (0);
193
194 *status = 'T';
195 return ((unsigned char) WSTOPSIG (w));
196 }
197
198 /* Resume execution of the inferior process.
199 If STEP is nonzero, single-step it.
200 If SIGNAL is nonzero, give it that signal. */
201
202 void
203 myresume (int step, int signal)
204 {
205 errno = 0;
206 ptrace (step ? PT_STEP : PT_CONTINUE, inferior_pid,
207 (PTRACE_ARG3_TYPE) 1, signal);
208 if (errno)
209 perror_with_name ("ptrace");
210 }
211
212
213 #ifdef __i386__
214 /* Fetch one or more registers from the inferior. REGNO == -1 to get
215 them all. We actually fetch more than requested, when convenient,
216 marking them as valid so we won't fetch them again. */
217
218 void
219 fetch_inferior_registers (int ignored)
220 {
221 struct reg inferior_registers;
222 struct env387 inferior_fp_registers;
223
224 ptrace (PT_GETREGS, inferior_pid,
225 (PTRACE_ARG3_TYPE) &inferior_registers, 0);
226 ptrace (PT_GETFPREGS, inferior_pid,
227 (PTRACE_ARG3_TYPE) &inferior_fp_registers, 0);
228
229 RF ( 0, inferior_registers.r_eax);
230 RF ( 1, inferior_registers.r_ecx);
231 RF ( 2, inferior_registers.r_edx);
232 RF ( 3, inferior_registers.r_ebx);
233 RF ( 4, inferior_registers.r_esp);
234 RF ( 5, inferior_registers.r_ebp);
235 RF ( 6, inferior_registers.r_esi);
236 RF ( 7, inferior_registers.r_edi);
237 RF ( 8, inferior_registers.r_eip);
238 RF ( 9, inferior_registers.r_eflags);
239 RF (10, inferior_registers.r_cs);
240 RF (11, inferior_registers.r_ss);
241 RF (12, inferior_registers.r_ds);
242 RF (13, inferior_registers.r_es);
243 RF (14, inferior_registers.r_fs);
244 RF (15, inferior_registers.r_gs);
245
246 RF (FP0_REGNUM, inferior_fp_registers.regs[0]);
247 RF (FP0_REGNUM + 1, inferior_fp_registers.regs[1]);
248 RF (FP0_REGNUM + 2, inferior_fp_registers.regs[2]);
249 RF (FP0_REGNUM + 3, inferior_fp_registers.regs[3]);
250 RF (FP0_REGNUM + 4, inferior_fp_registers.regs[4]);
251 RF (FP0_REGNUM + 5, inferior_fp_registers.regs[5]);
252 RF (FP0_REGNUM + 6, inferior_fp_registers.regs[6]);
253 RF (FP0_REGNUM + 7, inferior_fp_registers.regs[7]);
254
255 RF (FCTRL_REGNUM, inferior_fp_registers.control);
256 RF (FSTAT_REGNUM, inferior_fp_registers.status);
257 RF (FTAG_REGNUM, inferior_fp_registers.tag);
258 RF (FCS_REGNUM, inferior_fp_registers.code_seg);
259 RF (FCOFF_REGNUM, inferior_fp_registers.eip);
260 RF (FDS_REGNUM, inferior_fp_registers.operand_seg);
261 RF (FDOFF_REGNUM, inferior_fp_registers.operand);
262 RF (FOP_REGNUM, inferior_fp_registers.opcode);
263 }
264
265 /* Store our register values back into the inferior.
266 If REGNO is -1, do this for all registers.
267 Otherwise, REGNO specifies which register (so we can save time). */
268
269 void
270 store_inferior_registers (int ignored)
271 {
272 struct reg inferior_registers;
273 struct env387 inferior_fp_registers;
274
275 RS ( 0, inferior_registers.r_eax);
276 RS ( 1, inferior_registers.r_ecx);
277 RS ( 2, inferior_registers.r_edx);
278 RS ( 3, inferior_registers.r_ebx);
279 RS ( 4, inferior_registers.r_esp);
280 RS ( 5, inferior_registers.r_ebp);
281 RS ( 6, inferior_registers.r_esi);
282 RS ( 7, inferior_registers.r_edi);
283 RS ( 8, inferior_registers.r_eip);
284 RS ( 9, inferior_registers.r_eflags);
285 RS (10, inferior_registers.r_cs);
286 RS (11, inferior_registers.r_ss);
287 RS (12, inferior_registers.r_ds);
288 RS (13, inferior_registers.r_es);
289 RS (14, inferior_registers.r_fs);
290 RS (15, inferior_registers.r_gs);
291
292 RS (FP0_REGNUM, inferior_fp_registers.regs[0]);
293 RS (FP0_REGNUM + 1, inferior_fp_registers.regs[1]);
294 RS (FP0_REGNUM + 2, inferior_fp_registers.regs[2]);
295 RS (FP0_REGNUM + 3, inferior_fp_registers.regs[3]);
296 RS (FP0_REGNUM + 4, inferior_fp_registers.regs[4]);
297 RS (FP0_REGNUM + 5, inferior_fp_registers.regs[5]);
298 RS (FP0_REGNUM + 6, inferior_fp_registers.regs[6]);
299 RS (FP0_REGNUM + 7, inferior_fp_registers.regs[7]);
300
301 RS (FCTRL_REGNUM, inferior_fp_registers.control);
302 RS (FSTAT_REGNUM, inferior_fp_registers.status);
303 RS (FTAG_REGNUM, inferior_fp_registers.tag);
304 RS (FCS_REGNUM, inferior_fp_registers.code_seg);
305 RS (FCOFF_REGNUM, inferior_fp_registers.eip);
306 RS (FDS_REGNUM, inferior_fp_registers.operand_seg);
307 RS (FDOFF_REGNUM, inferior_fp_registers.operand);
308 RS (FOP_REGNUM, inferior_fp_registers.opcode);
309
310 ptrace (PT_SETREGS, inferior_pid,
311 (PTRACE_ARG3_TYPE) &inferior_registers, 0);
312 ptrace (PT_SETFPREGS, inferior_pid,
313 (PTRACE_ARG3_TYPE) &inferior_fp_registers, 0);
314 }
315 #endif /* !__i386__ */
316
317 #ifdef __m68k__
318 /* Fetch one or more registers from the inferior. REGNO == -1 to get
319 them all. We actually fetch more than requested, when convenient,
320 marking them as valid so we won't fetch them again. */
321
322 void
323 fetch_inferior_registers (int regno)
324 {
325 struct reg inferior_registers;
326 struct fpreg inferior_fp_registers;
327
328 ptrace (PT_GETREGS, inferior_pid,
329 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
330 memcpy (&registers[REGISTER_BYTE (0)], &inferior_registers,
331 sizeof (inferior_registers));
332
333 ptrace (PT_GETFPREGS, inferior_pid,
334 (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0);
335 memcpy (&registers[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers,
336 sizeof (inferior_fp_registers));
337 }
338
339 /* Store our register values back into the inferior.
340 If REGNO is -1, do this for all registers.
341 Otherwise, REGNO specifies which register (so we can save time). */
342
343 void
344 store_inferior_registers (int regno)
345 {
346 struct reg inferior_registers;
347 struct fpreg inferior_fp_registers;
348
349 memcpy (&inferior_registers, &registers[REGISTER_BYTE (0)],
350 sizeof (inferior_registers));
351 ptrace (PT_SETREGS, inferior_pid,
352 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
353
354 memcpy (&inferior_fp_registers, &registers[REGISTER_BYTE (FP0_REGNUM)],
355 sizeof (inferior_fp_registers));
356 ptrace (PT_SETFPREGS, inferior_pid,
357 (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0);
358 }
359 #endif /* !__m68k__ */
360
361
362 #ifdef __ns32k__
363 /* Fetch one or more registers from the inferior. REGNO == -1 to get
364 them all. We actually fetch more than requested, when convenient,
365 marking them as valid so we won't fetch them again. */
366
367 void
368 fetch_inferior_registers (int regno)
369 {
370 struct reg inferior_registers;
371 struct fpreg inferior_fpregisters;
372
373 ptrace (PT_GETREGS, inferior_pid,
374 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
375 ptrace (PT_GETFPREGS, inferior_pid,
376 (PTRACE_ARG3_TYPE) & inferior_fpregisters, 0);
377
378 RF (R0_REGNUM + 0, inferior_registers.r_r0);
379 RF (R0_REGNUM + 1, inferior_registers.r_r1);
380 RF (R0_REGNUM + 2, inferior_registers.r_r2);
381 RF (R0_REGNUM + 3, inferior_registers.r_r3);
382 RF (R0_REGNUM + 4, inferior_registers.r_r4);
383 RF (R0_REGNUM + 5, inferior_registers.r_r5);
384 RF (R0_REGNUM + 6, inferior_registers.r_r6);
385 RF (R0_REGNUM + 7, inferior_registers.r_r7);
386
387 RF (SP_REGNUM, inferior_registers.r_sp);
388 RF (FP_REGNUM, inferior_registers.r_fp);
389 RF (PC_REGNUM, inferior_registers.r_pc);
390 RF (PS_REGNUM, inferior_registers.r_psr);
391
392 RF (FPS_REGNUM, inferior_fpregisters.r_fsr);
393 RF (FP0_REGNUM + 0, inferior_fpregisters.r_freg[0]);
394 RF (FP0_REGNUM + 2, inferior_fpregisters.r_freg[2]);
395 RF (FP0_REGNUM + 4, inferior_fpregisters.r_freg[4]);
396 RF (FP0_REGNUM + 6, inferior_fpregisters.r_freg[6]);
397 RF (LP0_REGNUM + 1, inferior_fpregisters.r_freg[1]);
398 RF (LP0_REGNUM + 3, inferior_fpregisters.r_freg[3]);
399 RF (LP0_REGNUM + 5, inferior_fpregisters.r_freg[5]);
400 RF (LP0_REGNUM + 7, inferior_fpregisters.r_freg[7]);
401 }
402
403 /* Store our register values back into the inferior.
404 If REGNO is -1, do this for all registers.
405 Otherwise, REGNO specifies which register (so we can save time). */
406
407 void
408 store_inferior_registers (int regno)
409 {
410 struct reg inferior_registers;
411 struct fpreg inferior_fpregisters;
412
413 RS (R0_REGNUM + 0, inferior_registers.r_r0);
414 RS (R0_REGNUM + 1, inferior_registers.r_r1);
415 RS (R0_REGNUM + 2, inferior_registers.r_r2);
416 RS (R0_REGNUM + 3, inferior_registers.r_r3);
417 RS (R0_REGNUM + 4, inferior_registers.r_r4);
418 RS (R0_REGNUM + 5, inferior_registers.r_r5);
419 RS (R0_REGNUM + 6, inferior_registers.r_r6);
420 RS (R0_REGNUM + 7, inferior_registers.r_r7);
421
422 RS (SP_REGNUM, inferior_registers.r_sp);
423 RS (FP_REGNUM, inferior_registers.r_fp);
424 RS (PC_REGNUM, inferior_registers.r_pc);
425 RS (PS_REGNUM, inferior_registers.r_psr);
426
427 RS (FPS_REGNUM, inferior_fpregisters.r_fsr);
428 RS (FP0_REGNUM + 0, inferior_fpregisters.r_freg[0]);
429 RS (FP0_REGNUM + 2, inferior_fpregisters.r_freg[2]);
430 RS (FP0_REGNUM + 4, inferior_fpregisters.r_freg[4]);
431 RS (FP0_REGNUM + 6, inferior_fpregisters.r_freg[6]);
432 RS (LP0_REGNUM + 1, inferior_fpregisters.r_freg[1]);
433 RS (LP0_REGNUM + 3, inferior_fpregisters.r_freg[3]);
434 RS (LP0_REGNUM + 5, inferior_fpregisters.r_freg[5]);
435 RS (LP0_REGNUM + 7, inferior_fpregisters.r_freg[7]);
436
437 ptrace (PT_SETREGS, inferior_pid,
438 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
439 ptrace (PT_SETFPREGS, inferior_pid,
440 (PTRACE_ARG3_TYPE) & inferior_fpregisters, 0);
441
442 }
443 #endif /* !__ns32k__ */
444
445 #ifdef __powerpc__
446 /* Fetch one or more registers from the inferior. REGNO == -1 to get
447 them all. We actually fetch more than requested, when convenient,
448 marking them as valid so we won't fetch them again. */
449
450 void
451 fetch_inferior_registers (int regno)
452 {
453 struct reg inferior_registers;
454 #ifdef PT_GETFPREGS
455 struct fpreg inferior_fp_registers;
456 #endif
457 int i;
458
459 ptrace (PT_GETREGS, inferior_pid,
460 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
461 for (i = 0; i < 32; i++)
462 RF (i, inferior_registers.fixreg[i]);
463 RF (PPC_LR_REGNUM, inferior_registers.lr);
464 RF (PPC_CR_REGNUM, inferior_registers.cr);
465 RF (PPC_XER_REGNUM, inferior_registers.xer);
466 RF (PPC_CTR_REGNUM, inferior_registers.ctr);
467 RF (PC_REGNUM, inferior_registers.pc);
468
469 #ifdef PT_GETFPREGS
470 ptrace (PT_GETFPREGS, inferior_pid,
471 (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0);
472 for (i = 0; i < 32; i++)
473 RF (FP0_REGNUM + i, inferior_fp_registers.r_regs[i]);
474 #endif
475 }
476
477 /* Store our register values back into the inferior.
478 If REGNO is -1, do this for all registers.
479 Otherwise, REGNO specifies which register (so we can save time). */
480
481 void
482 store_inferior_registers (int regno)
483 {
484 struct reg inferior_registers;
485 #ifdef PT_SETFPREGS
486 struct fpreg inferior_fp_registers;
487 #endif
488 int i;
489
490 for (i = 0; i < 32; i++)
491 RS (i, inferior_registers.fixreg[i]);
492 RS (PPC_LR_REGNUM, inferior_registers.lr);
493 RS (PPC_CR_REGNUM, inferior_registers.cr);
494 RS (PPC_XER_REGNUM, inferior_registers.xer);
495 RS (PPC_CTR_REGNUM, inferior_registers.ctr);
496 RS (PC_REGNUM, inferior_registers.pc);
497 ptrace (PT_SETREGS, inferior_pid,
498 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
499
500 #ifdef PT_SETFPREGS
501 for (i = 0; i < 32; i++)
502 RS (FP0_REGNUM + i, inferior_fp_registers.r_regs[i]);
503 ptrace (PT_SETFPREGS, inferior_pid,
504 (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0);
505 #endif
506 }
507 #endif /* !__powerpc__ */
508
509 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
510 in the NEW_SUN_PTRACE case.
511 It ought to be straightforward. But it appears that writing did
512 not write the data that I specified. I cannot understand where
513 it got the data that it actually did write. */
514
515 /* Copy LEN bytes from inferior's memory starting at MEMADDR
516 to debugger memory starting at MYADDR. */
517
518 void
519 read_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len)
520 {
521 register int i;
522 /* Round starting address down to longword boundary. */
523 register CORE_ADDR addr = memaddr & -sizeof (int);
524 /* Round ending address up; get number of longwords that makes. */
525 register int count
526 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
527 /* Allocate buffer of that many longwords. */
528 register int *buffer = (int *) alloca (count * sizeof (int));
529
530 /* Read all the longwords */
531 for (i = 0; i < count; i++, addr += sizeof (int))
532 {
533 buffer[i] = ptrace (PT_READ_D, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
534 }
535
536 /* Copy appropriate bytes out of the buffer. */
537 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
538 }
539
540 /* Copy LEN bytes of data from debugger memory at MYADDR
541 to inferior's memory at MEMADDR.
542 On failure (cannot write the inferior)
543 returns the value of errno. */
544
545 int
546 write_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len)
547 {
548 register int i;
549 /* Round starting address down to longword boundary. */
550 register CORE_ADDR addr = memaddr & -sizeof (int);
551 /* Round ending address up; get number of longwords that makes. */
552 register int count
553 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
554 /* Allocate buffer of that many longwords. */
555 register int *buffer = (int *) alloca (count * sizeof (int));
556 extern int errno;
557
558 /* Fill start and end extra bytes of buffer with existing memory data. */
559
560 buffer[0] = ptrace (PT_READ_D, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
561
562 if (count > 1)
563 {
564 buffer[count - 1]
565 = ptrace (PT_READ_D, inferior_pid,
566 (PTRACE_ARG3_TYPE) addr + (count - 1) * sizeof (int), 0);
567 }
568
569 /* Copy data to be written over corresponding part of buffer */
570
571 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
572
573 /* Write the entire buffer. */
574
575 for (i = 0; i < count; i++, addr += sizeof (int))
576 {
577 errno = 0;
578 ptrace (PT_WRITE_D, inferior_pid, (PTRACE_ARG3_TYPE) addr, buffer[i]);
579 if (errno)
580 return errno;
581 }
582
583 return 0;
584 }
585 \f
586 void
587 initialize_low (void)
588 {
589 initialize_arch ();
590 }