b5199dca775bc50761d662780e7fc0fe8701abbf
[binutils-gdb.git] / gdb / gdbserver / low-nbsd.c
1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1986, 1987, 1993, 2000 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 "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*********************/
30 int quit_flag = 0;
31 static char my_registers[REGISTER_BYTES];
32 char *registers = my_registers;
33
34 /* Index within `registers' of the first byte of the space for
35 register N. */
36
37 char buf2[MAX_REGISTER_RAW_SIZE];
38 /***************End MY defs*********************/
39
40 #include <sys/ptrace.h>
41 #include <machine/reg.h>
42
43 extern int sys_nerr;
44 // extern char **sys_errlist;
45 extern char **environ;
46 extern int inferior_pid;
47 void quit (), perror_with_name ();
48
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__
56 struct 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
73 /* i386_register_raw_size[i] is the number of bytes of storage in the
74 actual machine representation for register i. */
75 int 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
89 int i386_register_byte[MAX_NUM_REGS];
90
91 static void
92 initialize_arch (void)
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 }
107 #endif /* !__i386__ */
108
109 #ifdef __powerpc__
110 #include "ppc-tdep.h"
111
112 static void
113 initialize_arch (void)
114 {
115 }
116 #endif /* !__powerpc__ */
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
123 int
124 create_inferior (char *program, char **allargs)
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
149 void
150 kill_inferior (void)
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. */
160 int
161 mythread_alive (int pid)
162 {
163 return 1;
164 }
165
166 /* Wait for process, returns status */
167
168 unsigned char
169 mywait (char *status)
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
201 void
202 myresume (int step, int signal)
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
211
212 #ifdef __i386__
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
217 void
218 fetch_inferior_registers (int ignored)
219 {
220 struct reg inferior_registers;
221 struct env387 inferior_fp_registers;
222
223 ptrace (PT_GETREGS, inferior_pid,
224 (PTRACE_ARG3_TYPE) &inferior_registers, 0);
225 ptrace (PT_GETFPREGS, inferior_pid,
226 (PTRACE_ARG3_TYPE) &inferior_fp_registers, 0);
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
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]);
253
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);
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
268 void
269 store_inferior_registers (int ignored)
270 {
271 struct reg inferior_registers;
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
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]);
299
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);
308
309 ptrace (PT_SETREGS, inferior_pid,
310 (PTRACE_ARG3_TYPE) &inferior_registers, 0);
311 ptrace (PT_SETFPREGS, inferior_pid,
312 (PTRACE_ARG3_TYPE) &inferior_fp_registers, 0);
313 }
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
321 void
322 fetch_inferior_registers (int regno)
323 {
324 struct reg inferior_registers;
325 #ifdef PT_GETFPREGS
326 struct fpreg inferior_fp_registers;
327 #endif
328 int i;
329
330 ptrace (PT_GETREGS, inferior_pid,
331 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
332 for (i = 0; i < 32; i++)
333 RF (i, inferior_registers.fixreg[i]);
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);
338 RF (PC_REGNUM, inferior_registers.pc);
339
340 #ifdef PT_GETFPREGS
341 ptrace (PT_GETFPREGS, inferior_pid,
342 (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0);
343 for (i = 0; i < 32; i++)
344 RF (FP0_REGNUM + i, inferior_fp_registers.r_regs[i]);
345 #endif
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
352 void
353 store_inferior_registers (int regno)
354 {
355 struct reg inferior_registers;
356 #ifdef PT_SETFPREGS
357 struct fpreg inferior_fp_registers;
358 #endif
359 int i;
360
361 for (i = 0; i < 32; i++)
362 RS (i, inferior_registers.fixreg[i]);
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);
367 RS (PC_REGNUM, inferior_registers.pc);
368 ptrace (PT_SETREGS, inferior_pid,
369 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
370
371 #ifdef PT_SETFPREGS
372 for (i = 0; i < 32; i++)
373 RS (FP0_REGNUM + i, inferior_fp_registers.r_regs[i]);
374 ptrace (PT_SETFPREGS, inferior_pid,
375 (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0);
376 #endif
377 }
378 #endif /* !__powerpc__ */
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
389 read_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len)
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
415 int
416 write_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len)
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
456 void
457 initialize_low (void)
458 {
459 initialize_arch ();
460 }