* gdbserver/remote-utils.c (remote_open): Set gdbserver as "owner"
[deliverable/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 enable_async_io ();
176 pid = waitpid (inferior_pid, &w, 0);
177 disable_async_io ();
178 if (pid != inferior_pid)
179 perror_with_name ("wait");
180
181 if (WIFEXITED (w))
182 {
183 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
184 *status = 'W';
185 return ((unsigned char) WEXITSTATUS (w));
186 }
187 else if (!WIFSTOPPED (w))
188 {
189 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
190 *status = 'X';
191 return ((unsigned char) WTERMSIG (w));
192 }
193
194 fetch_inferior_registers (0);
195
196 *status = 'T';
197 return ((unsigned char) WSTOPSIG (w));
198 }
199
200 /* Resume execution of the inferior process.
201 If STEP is nonzero, single-step it.
202 If SIGNAL is nonzero, give it that signal. */
203
204 void
205 myresume (int step, int signal)
206 {
207 errno = 0;
208 ptrace (step ? PT_STEP : PT_CONTINUE, inferior_pid,
209 (PTRACE_ARG3_TYPE) 1, signal);
210 if (errno)
211 perror_with_name ("ptrace");
212 }
213
214
215 #ifdef __i386__
216 /* Fetch one or more registers from the inferior. REGNO == -1 to get
217 them all. We actually fetch more than requested, when convenient,
218 marking them as valid so we won't fetch them again. */
219
220 void
221 fetch_inferior_registers (int ignored)
222 {
223 struct reg inferior_registers;
224 struct env387 inferior_fp_registers;
225
226 ptrace (PT_GETREGS, inferior_pid,
227 (PTRACE_ARG3_TYPE) &inferior_registers, 0);
228 ptrace (PT_GETFPREGS, inferior_pid,
229 (PTRACE_ARG3_TYPE) &inferior_fp_registers, 0);
230
231 RF ( 0, inferior_registers.r_eax);
232 RF ( 1, inferior_registers.r_ecx);
233 RF ( 2, inferior_registers.r_edx);
234 RF ( 3, inferior_registers.r_ebx);
235 RF ( 4, inferior_registers.r_esp);
236 RF ( 5, inferior_registers.r_ebp);
237 RF ( 6, inferior_registers.r_esi);
238 RF ( 7, inferior_registers.r_edi);
239 RF ( 8, inferior_registers.r_eip);
240 RF ( 9, inferior_registers.r_eflags);
241 RF (10, inferior_registers.r_cs);
242 RF (11, inferior_registers.r_ss);
243 RF (12, inferior_registers.r_ds);
244 RF (13, inferior_registers.r_es);
245 RF (14, inferior_registers.r_fs);
246 RF (15, inferior_registers.r_gs);
247
248 RF (FP0_REGNUM, inferior_fp_registers.regs[0]);
249 RF (FP0_REGNUM + 1, inferior_fp_registers.regs[1]);
250 RF (FP0_REGNUM + 2, inferior_fp_registers.regs[2]);
251 RF (FP0_REGNUM + 3, inferior_fp_registers.regs[3]);
252 RF (FP0_REGNUM + 4, inferior_fp_registers.regs[4]);
253 RF (FP0_REGNUM + 5, inferior_fp_registers.regs[5]);
254 RF (FP0_REGNUM + 6, inferior_fp_registers.regs[6]);
255 RF (FP0_REGNUM + 7, inferior_fp_registers.regs[7]);
256
257 RF (FCTRL_REGNUM, inferior_fp_registers.control);
258 RF (FSTAT_REGNUM, inferior_fp_registers.status);
259 RF (FTAG_REGNUM, inferior_fp_registers.tag);
260 RF (FCS_REGNUM, inferior_fp_registers.code_seg);
261 RF (FCOFF_REGNUM, inferior_fp_registers.eip);
262 RF (FDS_REGNUM, inferior_fp_registers.operand_seg);
263 RF (FDOFF_REGNUM, inferior_fp_registers.operand);
264 RF (FOP_REGNUM, inferior_fp_registers.opcode);
265 }
266
267 /* Store our register values back into the inferior.
268 If REGNO is -1, do this for all registers.
269 Otherwise, REGNO specifies which register (so we can save time). */
270
271 void
272 store_inferior_registers (int ignored)
273 {
274 struct reg inferior_registers;
275 struct env387 inferior_fp_registers;
276
277 RS ( 0, inferior_registers.r_eax);
278 RS ( 1, inferior_registers.r_ecx);
279 RS ( 2, inferior_registers.r_edx);
280 RS ( 3, inferior_registers.r_ebx);
281 RS ( 4, inferior_registers.r_esp);
282 RS ( 5, inferior_registers.r_ebp);
283 RS ( 6, inferior_registers.r_esi);
284 RS ( 7, inferior_registers.r_edi);
285 RS ( 8, inferior_registers.r_eip);
286 RS ( 9, inferior_registers.r_eflags);
287 RS (10, inferior_registers.r_cs);
288 RS (11, inferior_registers.r_ss);
289 RS (12, inferior_registers.r_ds);
290 RS (13, inferior_registers.r_es);
291 RS (14, inferior_registers.r_fs);
292 RS (15, inferior_registers.r_gs);
293
294 RS (FP0_REGNUM, inferior_fp_registers.regs[0]);
295 RS (FP0_REGNUM + 1, inferior_fp_registers.regs[1]);
296 RS (FP0_REGNUM + 2, inferior_fp_registers.regs[2]);
297 RS (FP0_REGNUM + 3, inferior_fp_registers.regs[3]);
298 RS (FP0_REGNUM + 4, inferior_fp_registers.regs[4]);
299 RS (FP0_REGNUM + 5, inferior_fp_registers.regs[5]);
300 RS (FP0_REGNUM + 6, inferior_fp_registers.regs[6]);
301 RS (FP0_REGNUM + 7, inferior_fp_registers.regs[7]);
302
303 RS (FCTRL_REGNUM, inferior_fp_registers.control);
304 RS (FSTAT_REGNUM, inferior_fp_registers.status);
305 RS (FTAG_REGNUM, inferior_fp_registers.tag);
306 RS (FCS_REGNUM, inferior_fp_registers.code_seg);
307 RS (FCOFF_REGNUM, inferior_fp_registers.eip);
308 RS (FDS_REGNUM, inferior_fp_registers.operand_seg);
309 RS (FDOFF_REGNUM, inferior_fp_registers.operand);
310 RS (FOP_REGNUM, inferior_fp_registers.opcode);
311
312 ptrace (PT_SETREGS, inferior_pid,
313 (PTRACE_ARG3_TYPE) &inferior_registers, 0);
314 ptrace (PT_SETFPREGS, inferior_pid,
315 (PTRACE_ARG3_TYPE) &inferior_fp_registers, 0);
316 }
317 #endif /* !__i386__ */
318
319 #ifdef __m68k__
320 /* Fetch one or more registers from the inferior. REGNO == -1 to get
321 them all. We actually fetch more than requested, when convenient,
322 marking them as valid so we won't fetch them again. */
323
324 void
325 fetch_inferior_registers (int regno)
326 {
327 struct reg inferior_registers;
328 struct fpreg inferior_fp_registers;
329
330 ptrace (PT_GETREGS, inferior_pid,
331 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
332 memcpy (&registers[REGISTER_BYTE (0)], &inferior_registers,
333 sizeof (inferior_registers));
334
335 ptrace (PT_GETFPREGS, inferior_pid,
336 (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0);
337 memcpy (&registers[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers,
338 sizeof (inferior_fp_registers));
339 }
340
341 /* Store our register values back into the inferior.
342 If REGNO is -1, do this for all registers.
343 Otherwise, REGNO specifies which register (so we can save time). */
344
345 void
346 store_inferior_registers (int regno)
347 {
348 struct reg inferior_registers;
349 struct fpreg inferior_fp_registers;
350
351 memcpy (&inferior_registers, &registers[REGISTER_BYTE (0)],
352 sizeof (inferior_registers));
353 ptrace (PT_SETREGS, inferior_pid,
354 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
355
356 memcpy (&inferior_fp_registers, &registers[REGISTER_BYTE (FP0_REGNUM)],
357 sizeof (inferior_fp_registers));
358 ptrace (PT_SETFPREGS, inferior_pid,
359 (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0);
360 }
361 #endif /* !__m68k__ */
362
363
364 #ifdef __ns32k__
365 /* Fetch one or more registers from the inferior. REGNO == -1 to get
366 them all. We actually fetch more than requested, when convenient,
367 marking them as valid so we won't fetch them again. */
368
369 void
370 fetch_inferior_registers (int regno)
371 {
372 struct reg inferior_registers;
373 struct fpreg inferior_fpregisters;
374
375 ptrace (PT_GETREGS, inferior_pid,
376 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
377 ptrace (PT_GETFPREGS, inferior_pid,
378 (PTRACE_ARG3_TYPE) & inferior_fpregisters, 0);
379
380 RF (R0_REGNUM + 0, inferior_registers.r_r0);
381 RF (R0_REGNUM + 1, inferior_registers.r_r1);
382 RF (R0_REGNUM + 2, inferior_registers.r_r2);
383 RF (R0_REGNUM + 3, inferior_registers.r_r3);
384 RF (R0_REGNUM + 4, inferior_registers.r_r4);
385 RF (R0_REGNUM + 5, inferior_registers.r_r5);
386 RF (R0_REGNUM + 6, inferior_registers.r_r6);
387 RF (R0_REGNUM + 7, inferior_registers.r_r7);
388
389 RF (SP_REGNUM, inferior_registers.r_sp);
390 RF (FP_REGNUM, inferior_registers.r_fp);
391 RF (PC_REGNUM, inferior_registers.r_pc);
392 RF (PS_REGNUM, inferior_registers.r_psr);
393
394 RF (FPS_REGNUM, inferior_fpregisters.r_fsr);
395 RF (FP0_REGNUM + 0, inferior_fpregisters.r_freg[0]);
396 RF (FP0_REGNUM + 2, inferior_fpregisters.r_freg[2]);
397 RF (FP0_REGNUM + 4, inferior_fpregisters.r_freg[4]);
398 RF (FP0_REGNUM + 6, inferior_fpregisters.r_freg[6]);
399 RF (LP0_REGNUM + 1, inferior_fpregisters.r_freg[1]);
400 RF (LP0_REGNUM + 3, inferior_fpregisters.r_freg[3]);
401 RF (LP0_REGNUM + 5, inferior_fpregisters.r_freg[5]);
402 RF (LP0_REGNUM + 7, inferior_fpregisters.r_freg[7]);
403 }
404
405 /* Store our register values back into the inferior.
406 If REGNO is -1, do this for all registers.
407 Otherwise, REGNO specifies which register (so we can save time). */
408
409 void
410 store_inferior_registers (int regno)
411 {
412 struct reg inferior_registers;
413 struct fpreg inferior_fpregisters;
414
415 RS (R0_REGNUM + 0, inferior_registers.r_r0);
416 RS (R0_REGNUM + 1, inferior_registers.r_r1);
417 RS (R0_REGNUM + 2, inferior_registers.r_r2);
418 RS (R0_REGNUM + 3, inferior_registers.r_r3);
419 RS (R0_REGNUM + 4, inferior_registers.r_r4);
420 RS (R0_REGNUM + 5, inferior_registers.r_r5);
421 RS (R0_REGNUM + 6, inferior_registers.r_r6);
422 RS (R0_REGNUM + 7, inferior_registers.r_r7);
423
424 RS (SP_REGNUM, inferior_registers.r_sp);
425 RS (FP_REGNUM, inferior_registers.r_fp);
426 RS (PC_REGNUM, inferior_registers.r_pc);
427 RS (PS_REGNUM, inferior_registers.r_psr);
428
429 RS (FPS_REGNUM, inferior_fpregisters.r_fsr);
430 RS (FP0_REGNUM + 0, inferior_fpregisters.r_freg[0]);
431 RS (FP0_REGNUM + 2, inferior_fpregisters.r_freg[2]);
432 RS (FP0_REGNUM + 4, inferior_fpregisters.r_freg[4]);
433 RS (FP0_REGNUM + 6, inferior_fpregisters.r_freg[6]);
434 RS (LP0_REGNUM + 1, inferior_fpregisters.r_freg[1]);
435 RS (LP0_REGNUM + 3, inferior_fpregisters.r_freg[3]);
436 RS (LP0_REGNUM + 5, inferior_fpregisters.r_freg[5]);
437 RS (LP0_REGNUM + 7, inferior_fpregisters.r_freg[7]);
438
439 ptrace (PT_SETREGS, inferior_pid,
440 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
441 ptrace (PT_SETFPREGS, inferior_pid,
442 (PTRACE_ARG3_TYPE) & inferior_fpregisters, 0);
443
444 }
445 #endif /* !__ns32k__ */
446
447 #ifdef __powerpc__
448 /* Fetch one or more registers from the inferior. REGNO == -1 to get
449 them all. We actually fetch more than requested, when convenient,
450 marking them as valid so we won't fetch them again. */
451
452 void
453 fetch_inferior_registers (int regno)
454 {
455 struct reg inferior_registers;
456 #ifdef PT_GETFPREGS
457 struct fpreg inferior_fp_registers;
458 #endif
459 int i;
460
461 ptrace (PT_GETREGS, inferior_pid,
462 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
463 for (i = 0; i < 32; i++)
464 RF (i, inferior_registers.fixreg[i]);
465 RF (PPC_LR_REGNUM, inferior_registers.lr);
466 RF (PPC_CR_REGNUM, inferior_registers.cr);
467 RF (PPC_XER_REGNUM, inferior_registers.xer);
468 RF (PPC_CTR_REGNUM, inferior_registers.ctr);
469 RF (PC_REGNUM, inferior_registers.pc);
470
471 #ifdef PT_GETFPREGS
472 ptrace (PT_GETFPREGS, inferior_pid,
473 (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0);
474 for (i = 0; i < 32; i++)
475 RF (FP0_REGNUM + i, inferior_fp_registers.r_regs[i]);
476 #endif
477 }
478
479 /* Store our register values back into the inferior.
480 If REGNO is -1, do this for all registers.
481 Otherwise, REGNO specifies which register (so we can save time). */
482
483 void
484 store_inferior_registers (int regno)
485 {
486 struct reg inferior_registers;
487 #ifdef PT_SETFPREGS
488 struct fpreg inferior_fp_registers;
489 #endif
490 int i;
491
492 for (i = 0; i < 32; i++)
493 RS (i, inferior_registers.fixreg[i]);
494 RS (PPC_LR_REGNUM, inferior_registers.lr);
495 RS (PPC_CR_REGNUM, inferior_registers.cr);
496 RS (PPC_XER_REGNUM, inferior_registers.xer);
497 RS (PPC_CTR_REGNUM, inferior_registers.ctr);
498 RS (PC_REGNUM, inferior_registers.pc);
499 ptrace (PT_SETREGS, inferior_pid,
500 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
501
502 #ifdef PT_SETFPREGS
503 for (i = 0; i < 32; i++)
504 RS (FP0_REGNUM + i, inferior_fp_registers.r_regs[i]);
505 ptrace (PT_SETFPREGS, inferior_pid,
506 (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0);
507 #endif
508 }
509 #endif /* !__powerpc__ */
510
511 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
512 in the NEW_SUN_PTRACE case.
513 It ought to be straightforward. But it appears that writing did
514 not write the data that I specified. I cannot understand where
515 it got the data that it actually did write. */
516
517 /* Copy LEN bytes from inferior's memory starting at MEMADDR
518 to debugger memory starting at MYADDR. */
519
520 void
521 read_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len)
522 {
523 register int i;
524 /* Round starting address down to longword boundary. */
525 register CORE_ADDR addr = memaddr & -sizeof (int);
526 /* Round ending address up; get number of longwords that makes. */
527 register int count
528 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
529 /* Allocate buffer of that many longwords. */
530 register int *buffer = (int *) alloca (count * sizeof (int));
531
532 /* Read all the longwords */
533 for (i = 0; i < count; i++, addr += sizeof (int))
534 {
535 buffer[i] = ptrace (PT_READ_D, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
536 }
537
538 /* Copy appropriate bytes out of the buffer. */
539 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
540 }
541
542 /* Copy LEN bytes of data from debugger memory at MYADDR
543 to inferior's memory at MEMADDR.
544 On failure (cannot write the inferior)
545 returns the value of errno. */
546
547 int
548 write_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len)
549 {
550 register int i;
551 /* Round starting address down to longword boundary. */
552 register CORE_ADDR addr = memaddr & -sizeof (int);
553 /* Round ending address up; get number of longwords that makes. */
554 register int count
555 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
556 /* Allocate buffer of that many longwords. */
557 register int *buffer = (int *) alloca (count * sizeof (int));
558 extern int errno;
559
560 /* Fill start and end extra bytes of buffer with existing memory data. */
561
562 buffer[0] = ptrace (PT_READ_D, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
563
564 if (count > 1)
565 {
566 buffer[count - 1]
567 = ptrace (PT_READ_D, inferior_pid,
568 (PTRACE_ARG3_TYPE) addr + (count - 1) * sizeof (int), 0);
569 }
570
571 /* Copy data to be written over corresponding part of buffer */
572
573 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
574
575 /* Write the entire buffer. */
576
577 for (i = 0; i < count; i++, addr += sizeof (int))
578 {
579 errno = 0;
580 ptrace (PT_WRITE_D, inferior_pid, (PTRACE_ARG3_TYPE) addr, buffer[i]);
581 if (errno)
582 return errno;
583 }
584
585 return 0;
586 }
587 \f
588 void
589 initialize_low (void)
590 {
591 initialize_arch ();
592 }
This page took 0.1665 seconds and 4 git commands to generate.