Sparc version of remote-inflow.c. Hasn't been made portable yet.
[binutils-gdb.git] / gdb / gdbserver / remote-inflow-sparc.c
1 /* Low level interface to ptrace, for GDB when running under Unix.
2 Copyright (C) 1986, 1987 Free Software Foundation, Inc.
3 */
4
5 #include "defs.h"
6 #include "wait.h"
7 #include "frame.h"
8 #include "inferior.h"
9 /***************************
10 #include "initialize.h"
11 ****************************/
12
13 #include <stdio.h>
14 #include <sys/param.h>
15 #include <sys/dir.h>
16 #include <sys/user.h>
17 #include <signal.h>
18 #include <sys/ioctl.h>
19 #include <sgtty.h>
20 #include <fcntl.h>
21
22 /***************Begin MY defs*********************/
23 int quit_flag = 0;
24 char registers[REGISTER_BYTES];
25
26 /* Index within `registers' of the first byte of the space for
27 register N. */
28
29
30 char buf2[MAX_REGISTER_RAW_SIZE];
31 /***************End MY defs*********************/
32
33 #include <sys/ptrace.h>
34 #include <machine/reg.h>
35
36 extern char **environ;
37 extern int errno;
38 extern int inferior_pid;
39 void error (), quit (), perror_with_name ();
40 int query ();
41 void supply_register (), write_register ();
42 CORE_ADDR read_register ();
43
44 /* Nonzero if we are debugging an attached outside process
45 rather than an inferior. */
46
47
48 /* Start an inferior process and returns its pid.
49 ALLARGS is a vector of program-name and args.
50 ENV is the environment vector to pass. */
51
52 int
53 create_inferior (allargs, env)
54 char **allargs;
55 char **env;
56 {
57 int pid;
58 extern int sys_nerr;
59 extern char *sys_errlist[];
60 extern int errno;
61 char status;
62 char execbuf[1024];
63
64 /* exec is said to fail if the executable is open. */
65 /****************close_exec_file ();*****************/
66
67 sprintf (execbuf, "exec %s", allargs);
68
69 pid = vfork ();
70 if (pid < 0)
71 perror_with_name ("vfork");
72
73 if (pid == 0)
74 {
75 /* Run inferior in a separate process group. */
76 setpgrp (getpid (), getpid ());
77
78 errno = 0;
79 ptrace (PTRACE_TRACEME);
80
81 execle ("/bin/sh", "sh", "-c", execbuf, 0, env);
82
83 fprintf (stderr, "Cannot exec /bin/sh: %s.\n",
84 errno < sys_nerr ? sys_errlist[errno] : "unknown error");
85 fflush (stderr);
86 _exit (0177);
87 }
88
89 return pid;
90 }
91
92 /* Kill the inferior process. Make us have no inferior. */
93
94 void
95 kill_inferior ()
96 {
97 if (inferior_pid == 0)
98 return;
99 ptrace (8, inferior_pid, 0, 0);
100 wait (0);
101 /*************inferior_died ();****VK**************/
102 }
103
104 /* Resume execution of the inferior process.
105 If STEP is nonzero, single-step it.
106 If SIGNAL is nonzero, give it that signal. */
107
108 unsigned char
109 myresume (step, signal, status)
110 int step;
111 int signal;
112 char *status;
113 {
114 int pid;
115 WAITTYPE w;
116
117 errno = 0;
118 ptrace (step ? 9 : 7, inferior_pid, 1, signal);
119 if (errno)
120 perror_with_name ("ptrace");
121 pid = wait (&w);
122 if (pid != inferior_pid)
123 perror_with_name ("wait");
124
125 fetch_inferior_registers (0);
126
127 if (WIFEXITED (w))
128 {
129 printf ("\nChild exited with retcode = %x \n", WEXITSTATUS (w));
130 *status = 'E';
131 return ((unsigned char) WEXITSTATUS (w));
132 }
133 else if (!WIFSTOPPED (w))
134 {
135 printf ("\nChild terminated with signal = %x \n", WTERMSIG (w));
136 *status = 'T';
137 return ((unsigned char) WTERMSIG (w));
138 }
139 else
140 {
141 printf ("\nChild stopped with signal = %x \n", WSTOPSIG (w));
142 *status = 'S';
143 return ((unsigned char) WSTOPSIG (w));
144 }
145 }
146
147 #define INT_REGS 1
148 #define STACK_REGS 2
149 #define FP_REGS 4
150
151 /* Fetch one or more registers from the inferior. REGNO == -1 to get
152 them all. We actually fetch more than requested, when convenient,
153 marking them as valid so we won't fetch them again. */
154
155 void
156 fetch_inferior_registers (ignored)
157 int ignored;
158 {
159 struct regs inferior_registers;
160 struct fp_status inferior_fp_registers;
161 int i;
162
163 /* Global and Out regs are fetched directly, as well as the control
164 registers. If we're getting one of the in or local regs,
165 and the stack pointer has not yet been fetched,
166 we have to do that first, since they're found in memory relative
167 to the stack pointer. */
168
169 if (ptrace (PTRACE_GETREGS, inferior_pid,
170 (PTRACE_ARG3_TYPE) &inferior_registers, 0))
171 perror("ptrace_getregs");
172
173 registers[REGISTER_BYTE (0)] = 0;
174 memcpy (&registers[REGISTER_BYTE (1)], &inferior_registers.r_g1,
175 15 * REGISTER_RAW_SIZE (G0_REGNUM));
176 *(int *)&registers[REGISTER_BYTE (PS_REGNUM)] = inferior_registers.r_ps;
177 *(int *)&registers[REGISTER_BYTE (PC_REGNUM)] = inferior_registers.r_pc;
178 *(int *)&registers[REGISTER_BYTE (NPC_REGNUM)] = inferior_registers.r_npc;
179 *(int *)&registers[REGISTER_BYTE (Y_REGNUM)] = inferior_registers.r_y;
180
181 /* Floating point registers */
182
183 if (ptrace (PTRACE_GETFPREGS, inferior_pid,
184 (PTRACE_ARG3_TYPE) &inferior_fp_registers,
185 0))
186 perror("ptrace_getfpregs");
187 memcpy (&registers[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers,
188 sizeof inferior_fp_registers.fpu_fr);
189
190 /* These regs are saved on the stack by the kernel. Only read them
191 all (16 ptrace calls!) if we really need them. */
192
193 read_inferior_memory (*(CORE_ADDR*)&registers[REGISTER_BYTE (SP_REGNUM)],
194 &registers[REGISTER_BYTE (L0_REGNUM)],
195 16*REGISTER_RAW_SIZE (L0_REGNUM));
196 }
197
198 /* Store our register values back into the inferior.
199 If REGNO is -1, do this for all registers.
200 Otherwise, REGNO specifies which register (so we can save time). */
201
202 void
203 store_inferior_registers (ignored)
204 int ignored;
205 {
206 struct regs inferior_registers;
207 struct fp_status inferior_fp_registers;
208 CORE_ADDR sp = *(CORE_ADDR *)&registers[REGISTER_BYTE (SP_REGNUM)];
209
210 write_inferior_memory (sp, &registers[REGISTER_BYTE (L0_REGNUM)],
211 16*REGISTER_RAW_SIZE (L0_REGNUM));
212
213 memcpy (&inferior_registers.r_g1, &registers[REGISTER_BYTE (G1_REGNUM)],
214 15 * REGISTER_RAW_SIZE (G1_REGNUM));
215
216 inferior_registers.r_ps =
217 *(int *)&registers[REGISTER_BYTE (PS_REGNUM)];
218 inferior_registers.r_pc =
219 *(int *)&registers[REGISTER_BYTE (PC_REGNUM)];
220 inferior_registers.r_npc =
221 *(int *)&registers[REGISTER_BYTE (NPC_REGNUM)];
222 inferior_registers.r_y =
223 *(int *)&registers[REGISTER_BYTE (Y_REGNUM)];
224
225 if (ptrace (PTRACE_SETREGS, inferior_pid,
226 (PTRACE_ARG3_TYPE) &inferior_registers, 0))
227 perror("ptrace_setregs");
228
229 memcpy (&inferior_fp_registers, &registers[REGISTER_BYTE (FP0_REGNUM)],
230 sizeof inferior_fp_registers.fpu_fr);
231
232 if (ptrace (PTRACE_SETFPREGS, inferior_pid,
233 (PTRACE_ARG3_TYPE) &inferior_fp_registers, 0))
234 perror("ptrace_setfpregs");
235 }
236
237 #if 0
238 void
239 fetch_inferior_registers ()
240 {
241 struct regs inferior_registers;
242 struct fp_status inferior_fp_registers;
243 extern char registers[];
244
245 ptrace (PTRACE_GETREGS, inferior_pid, &inferior_registers);
246 if (errno)
247 perror_with_name ("ptrace");
248 /**********debugging begin **********/
249 print_some_registers (&inferior_registers);
250 /**********debugging end **********/
251 ptrace (PTRACE_GETFPREGS, inferior_pid, &inferior_fp_registers);
252 if (errno)
253 perror_with_name ("ptrace");
254
255 bcopy (&inferior_registers, registers, 16 * 4);
256 bcopy (&inferior_fp_registers, &registers[REGISTER_BYTE (FP0_REGNUM)],
257 sizeof inferior_fp_registers.fpu_regs);
258 *(int *) &registers[REGISTER_BYTE (PS_REGNUM)] = inferior_registers.r_ps;
259 *(int *) &registers[REGISTER_BYTE (PC_REGNUM)] = inferior_registers.r_pc;
260 bcopy (&inferior_fp_registers.fpu_flags,
261 &registers[REGISTER_BYTE (FPC_REGNUM)],
262 sizeof inferior_fp_registers - sizeof inferior_fp_registers.fpu_regs);
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 store_inferior_registers (regno)
270 int regno;
271 {
272 struct regs inferior_registers;
273 struct fp_status inferior_fp_registers;
274 extern char registers[];
275
276 bcopy (registers, &inferior_registers, 16 * 4);
277 bcopy (&registers[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers,
278 sizeof inferior_fp_registers.fps_regs);
279 inferior_registers.r_ps = *(int *) &registers[REGISTER_BYTE (PS_REGNUM)];
280 inferior_registers.r_pc = *(int *) &registers[REGISTER_BYTE (PC_REGNUM)];
281 bcopy (&registers[REGISTER_BYTE (FPC_REGNUM)],
282 &inferior_fp_registers.fps_control,
283 sizeof inferior_fp_registers - sizeof inferior_fp_registers.fps_regs);
284
285 ptrace (PTRACE_SETREGS, inferior_pid, &inferior_registers);
286 if (errno)
287 perror_with_name ("ptrace");
288 ptrace (PTRACE_SETFPREGS, inferior_pid, &inferior_fp_registers);
289 if (errno)
290 perror_with_name ("ptrace");
291 }
292 #endif /* 0 */
293
294 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
295 in the NEW_SUN_PTRACE case.
296 It ought to be straightforward. But it appears that writing did
297 not write the data that I specified. I cannot understand where
298 it got the data that it actually did write. */
299
300 /* Copy LEN bytes from inferior's memory starting at MEMADDR
301 to debugger memory starting at MYADDR. */
302
303 read_inferior_memory (memaddr, myaddr, len)
304 CORE_ADDR memaddr;
305 char *myaddr;
306 int len;
307 {
308 register int i;
309 /* Round starting address down to longword boundary. */
310 register CORE_ADDR addr = memaddr & -sizeof (int);
311 /* Round ending address up; get number of longwords that makes. */
312 register int count
313 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
314 /* Allocate buffer of that many longwords. */
315 register int *buffer = (int *) alloca (count * sizeof (int));
316
317 /* Read all the longwords */
318 for (i = 0; i < count; i++, addr += sizeof (int))
319 {
320 buffer[i] = ptrace (1, inferior_pid, addr, 0);
321 }
322
323 /* Copy appropriate bytes out of the buffer. */
324 bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
325 }
326
327 /* Copy LEN bytes of data from debugger memory at MYADDR
328 to inferior's memory at MEMADDR.
329 On failure (cannot write the inferior)
330 returns the value of errno. */
331
332 int
333 write_inferior_memory (memaddr, myaddr, len)
334 CORE_ADDR memaddr;
335 char *myaddr;
336 int len;
337 {
338 register int i;
339 /* Round starting address down to longword boundary. */
340 register CORE_ADDR addr = memaddr & -sizeof (int);
341 /* Round ending address up; get number of longwords that makes. */
342 register int count
343 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
344 /* Allocate buffer of that many longwords. */
345 register int *buffer = (int *) alloca (count * sizeof (int));
346 extern int errno;
347
348 /* Fill start and end extra bytes of buffer with existing memory data. */
349
350 buffer[0] = ptrace (1, inferior_pid, addr, 0);
351
352 if (count > 1)
353 {
354 buffer[count - 1]
355 = ptrace (1, inferior_pid,
356 addr + (count - 1) * sizeof (int), 0);
357 }
358
359 /* Copy data to be written over corresponding part of buffer */
360
361 bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
362
363 /* Write the entire buffer. */
364
365 for (i = 0; i < count; i++, addr += sizeof (int))
366 {
367 errno = 0;
368 ptrace (4, inferior_pid, addr, buffer[i]);
369 if (errno)
370 return errno;
371 }
372
373 return 0;
374 }
375 \f
376 void
377 try_writing_regs_command ()
378 {
379 register int i;
380 register int val;
381
382 if (inferior_pid == 0)
383 error ("There is no inferior process now.");
384
385 fetch_inferior_registers (0);
386 for (i = 0; i < 18; i++)
387 {
388 QUIT;
389 errno = 0;
390 val = read_register (i);
391 write_register (i, val);
392 if (errno == 0)
393 {
394 printf (" Succeeded with register %d; value 0x%x (%d).\n",
395 i, val, val);
396 }
397 else
398 printf (" Failed with register %d.\n", i);
399 }
400 }
401
402 void
403 initialize ()
404 {
405
406 inferior_pid = 0;
407
408
409 }
410
411
412 /* Return the contents of register REGNO,
413 regarding it as an integer. */
414
415 CORE_ADDR
416 read_register (regno)
417 int regno;
418 {
419 /* This loses when REGISTER_RAW_SIZE (regno) != sizeof (int) */
420 return *(int *) &registers[REGISTER_BYTE (regno)];
421 }
422
423 /* Store VALUE in the register number REGNO, regarded as an integer. */
424
425 void
426 write_register (regno, val)
427 int regno, val;
428 {
429 /* This loses when REGISTER_RAW_SIZE (regno) != sizeof (int) */
430 *(int *) &registers[REGISTER_BYTE (regno)] = val;
431
432 if (have_inferior_p ())
433 store_inferior_registers (regno);
434 }
435
436
437 int
438 have_inferior_p ()
439 {
440 return inferior_pid != 0;
441 }
442
443 print_some_registers (regs)
444 int regs[];
445 {
446 register int i;
447 for (i = 0; i < 18; i++)
448 {
449 printf ("reg[%d] = %x\n", i, regs[i]);
450 }
451 }