Commit | Line | Data |
---|---|---|
e20520b8 SG |
1 | /* Low level interface to ptrace, for the remote server for GDB. |
2 | Copyright (C) 1986, 1987, 1993 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., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
41e170e2 | 20 | #include "server.h" |
e20520b8 SG |
21 | #include "frame.h" |
22 | #include "inferior.h" | |
e20520b8 SG |
23 | |
24 | #include <stdio.h> | |
25 | #include <sys/param.h> | |
26 | #include <sys/dir.h> | |
e20520b8 SG |
27 | #define LYNXOS |
28 | #include <sys/mem.h> | |
29 | #include <sys/signal.h> | |
30 | #include <sys/file.h> | |
31 | #include <sys/kernel.h> | |
b292fca8 SS |
32 | #ifndef __LYNXOS |
33 | #define __LYNXOS | |
34 | #endif | |
e20520b8 SG |
35 | #include <sys/itimer.h> |
36 | #include <sys/time.h> | |
37 | #include <sys/resource.h> | |
38 | #include <sys/proc.h> | |
39 | #include <signal.h> | |
40 | #include <sys/ioctl.h> | |
41 | #include <sgtty.h> | |
42 | #include <fcntl.h> | |
4c0b3e57 | 43 | #include <sys/wait.h> |
b292fca8 | 44 | #include <sys/fpp.h> |
e20520b8 | 45 | |
e20520b8 SG |
46 | char registers[REGISTER_BYTES]; |
47 | ||
e20520b8 | 48 | #include <sys/ptrace.h> |
e20520b8 SG |
49 | |
50 | /* Start an inferior process and returns its pid. | |
db691e4b | 51 | ALLARGS is a vector of program-name and args. */ |
e20520b8 SG |
52 | |
53 | int | |
f450d101 SG |
54 | create_inferior (program, allargs) |
55 | char *program; | |
e20520b8 | 56 | char **allargs; |
e20520b8 SG |
57 | { |
58 | int pid; | |
e20520b8 SG |
59 | |
60 | pid = fork (); | |
61 | if (pid < 0) | |
62 | perror_with_name ("fork"); | |
63 | ||
64 | if (pid == 0) | |
65 | { | |
db691e4b SG |
66 | int pgrp; |
67 | ||
68 | /* Switch child to it's own process group so that signals won't | |
69 | directly affect gdbserver. */ | |
70 | ||
71 | pgrp = getpid(); | |
72 | setpgrp(0, pgrp); | |
73 | ioctl (0, TIOCSPGRP, &pgrp); | |
74 | ||
e20520b8 SG |
75 | ptrace (PTRACE_TRACEME); |
76 | ||
f450d101 | 77 | execv (program, allargs); |
e20520b8 | 78 | |
db691e4b SG |
79 | fprintf (stderr, "GDBserver (process %d): Cannot exec %s: %s.\n", |
80 | getpid(), program, | |
e20520b8 SG |
81 | errno < sys_nerr ? sys_errlist[errno] : "unknown error"); |
82 | fflush (stderr); | |
83 | _exit (0177); | |
84 | } | |
85 | ||
86 | return pid; | |
87 | } | |
88 | ||
89 | /* Kill the inferior process. Make us have no inferior. */ | |
90 | ||
91 | void | |
92 | kill_inferior () | |
93 | { | |
94 | if (inferior_pid == 0) | |
95 | return; | |
41e170e2 | 96 | ptrace (PTRACE_KILL, inferior_pid, 0, 0); |
e20520b8 | 97 | wait (0); |
41e170e2 SG |
98 | |
99 | inferior_pid = 0; | |
e20520b8 SG |
100 | } |
101 | ||
102 | /* Wait for process, returns status */ | |
103 | ||
104 | unsigned char | |
105 | mywait (status) | |
106 | char *status; | |
107 | { | |
108 | int pid; | |
109 | union wait w; | |
110 | ||
41e170e2 SG |
111 | enable_async_io(); |
112 | ||
e20520b8 | 113 | pid = wait (&w); |
41e170e2 SG |
114 | |
115 | disable_async_io(); | |
116 | ||
e20520b8 SG |
117 | if (pid != PIDGET(inferior_pid)) |
118 | perror_with_name ("wait"); | |
119 | ||
120 | inferior_pid = BUILDPID (inferior_pid, w.w_tid); | |
121 | ||
122 | if (WIFEXITED (w)) | |
123 | { | |
41e170e2 SG |
124 | fprintf (stderr, "\nChild exited with status %d\n", WEXITSTATUS (w)); |
125 | fprintf (stderr, "GDBserver exiting\n"); | |
126 | exit (0); | |
e20520b8 SG |
127 | } |
128 | else if (!WIFSTOPPED (w)) | |
129 | { | |
130 | fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w)); | |
131 | *status = 'T'; | |
132 | return ((unsigned char) WTERMSIG (w)); | |
133 | } | |
134 | ||
135 | fetch_inferior_registers (0); | |
136 | ||
137 | *status = 'S'; | |
138 | return ((unsigned char) WSTOPSIG (w)); | |
139 | } | |
140 | ||
141 | /* Resume execution of the inferior process. | |
142 | If STEP is nonzero, single-step it. | |
143 | If SIGNAL is nonzero, give it that signal. */ | |
144 | ||
145 | void | |
146 | myresume (step, signal) | |
147 | int step; | |
148 | int signal; | |
149 | { | |
150 | errno = 0; | |
151 | ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, inferior_pid, 1, signal); | |
152 | if (errno) | |
153 | perror_with_name ("ptrace"); | |
154 | } | |
155 | ||
156 | #undef offsetof | |
157 | #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER) | |
158 | ||
9a13e99e SG |
159 | /* Mapping between GDB register #s and offsets into econtext. Must be |
160 | consistent with REGISTER_NAMES macro in various tmXXX.h files. */ | |
e20520b8 | 161 | |
9a13e99e | 162 | #define X(ENTRY)(offsetof(struct econtext, ENTRY)) |
e20520b8 | 163 | |
9a13e99e SG |
164 | #ifdef I386 |
165 | /* Mappings from tm-i386v.h */ | |
e20520b8 | 166 | |
9a13e99e SG |
167 | static int regmap[] = |
168 | { | |
e20520b8 SG |
169 | X(eax), |
170 | X(ecx), | |
171 | X(edx), | |
172 | X(ebx), | |
9a13e99e SG |
173 | X(esp), /* sp */ |
174 | X(ebp), /* fp */ | |
e20520b8 SG |
175 | X(esi), |
176 | X(edi), | |
9a13e99e | 177 | X(eip), /* pc */ |
e20520b8 SG |
178 | X(flags), /* ps */ |
179 | X(cs), | |
180 | X(ss), | |
181 | X(ds), | |
182 | X(es), | |
183 | X(ecode), /* Lynx doesn't give us either fs or gs, so */ | |
9a13e99e | 184 | X(fault), /* we just substitute these two in the hopes |
e20520b8 | 185 | that they are useful. */ |
9a13e99e SG |
186 | }; |
187 | #endif | |
188 | ||
189 | #ifdef M68K | |
190 | /* Mappings from tm-m68k.h */ | |
191 | ||
192 | static int regmap[] = | |
193 | { | |
194 | X(regs[0]), /* d0 */ | |
195 | X(regs[1]), /* d1 */ | |
196 | X(regs[2]), /* d2 */ | |
197 | X(regs[3]), /* d3 */ | |
198 | X(regs[4]), /* d4 */ | |
199 | X(regs[5]), /* d5 */ | |
200 | X(regs[6]), /* d6 */ | |
201 | X(regs[7]), /* d7 */ | |
202 | X(regs[8]), /* a0 */ | |
203 | X(regs[9]), /* a1 */ | |
204 | X(regs[10]), /* a2 */ | |
205 | X(regs[11]), /* a3 */ | |
206 | X(regs[12]), /* a4 */ | |
207 | X(regs[13]), /* a5 */ | |
208 | X(regs[14]), /* fp */ | |
209 | 0, /* sp */ | |
210 | X(status), /* ps */ | |
211 | X(pc), | |
212 | ||
213 | X(fregs[0*3]), /* fp0 */ | |
214 | X(fregs[1*3]), /* fp1 */ | |
215 | X(fregs[2*3]), /* fp2 */ | |
216 | X(fregs[3*3]), /* fp3 */ | |
217 | X(fregs[4*3]), /* fp4 */ | |
218 | X(fregs[5*3]), /* fp5 */ | |
219 | X(fregs[6*3]), /* fp6 */ | |
220 | X(fregs[7*3]), /* fp7 */ | |
221 | ||
222 | X(fcregs[0]), /* fpcontrol */ | |
223 | X(fcregs[1]), /* fpstatus */ | |
224 | X(fcregs[2]), /* fpiaddr */ | |
225 | X(ssw), /* fpcode */ | |
226 | X(fault), /* fpflags */ | |
227 | }; | |
228 | #endif | |
229 | ||
b292fca8 SS |
230 | #ifdef SPARC |
231 | /* Mappings from tm-sparc.h */ | |
232 | ||
233 | #define FX(ENTRY)(offsetof(struct fcontext, ENTRY)) | |
234 | ||
235 | static int regmap[] = | |
236 | { | |
237 | -1, /* g0 */ | |
238 | X(g1), | |
239 | X(g2), | |
240 | X(g3), | |
241 | X(g4), | |
242 | -1, /* g5->g7 aren't saved by Lynx */ | |
243 | -1, | |
244 | -1, | |
245 | ||
246 | X(o[0]), | |
247 | X(o[1]), | |
248 | X(o[2]), | |
249 | X(o[3]), | |
250 | X(o[4]), | |
251 | X(o[5]), | |
252 | X(o[6]), /* sp */ | |
253 | X(o[7]), /* ra */ | |
254 | ||
255 | -1,-1,-1,-1,-1,-1,-1,-1, /* l0 -> l7 */ | |
256 | ||
257 | -1,-1,-1,-1,-1,-1,-1,-1, /* i0 -> i7 */ | |
258 | ||
259 | FX(f.fregs[0]), /* f0 */ | |
260 | FX(f.fregs[1]), | |
261 | FX(f.fregs[2]), | |
262 | FX(f.fregs[3]), | |
263 | FX(f.fregs[4]), | |
264 | FX(f.fregs[5]), | |
265 | FX(f.fregs[6]), | |
266 | FX(f.fregs[7]), | |
267 | FX(f.fregs[8]), | |
268 | FX(f.fregs[9]), | |
269 | FX(f.fregs[10]), | |
270 | FX(f.fregs[11]), | |
271 | FX(f.fregs[12]), | |
272 | FX(f.fregs[13]), | |
273 | FX(f.fregs[14]), | |
274 | FX(f.fregs[15]), | |
275 | FX(f.fregs[16]), | |
276 | FX(f.fregs[17]), | |
277 | FX(f.fregs[18]), | |
278 | FX(f.fregs[19]), | |
279 | FX(f.fregs[20]), | |
280 | FX(f.fregs[21]), | |
281 | FX(f.fregs[22]), | |
282 | FX(f.fregs[23]), | |
283 | FX(f.fregs[24]), | |
284 | FX(f.fregs[25]), | |
285 | FX(f.fregs[26]), | |
286 | FX(f.fregs[27]), | |
287 | FX(f.fregs[28]), | |
288 | FX(f.fregs[29]), | |
289 | FX(f.fregs[30]), | |
290 | FX(f.fregs[31]), | |
291 | ||
292 | X(y), | |
293 | X(psr), | |
294 | X(wim), | |
295 | X(tbr), | |
296 | X(pc), | |
297 | X(npc), | |
298 | FX(fsr), /* fpsr */ | |
299 | -1, /* cpsr */ | |
300 | }; | |
301 | #endif | |
302 | ||
303 | #ifdef SPARC | |
304 | ||
305 | /* This routine handles some oddball cases for Sparc registers and LynxOS. | |
306 | In partucular, it causes refs to G0, g5->7, and all fp regs to return zero. | |
307 | It also handles knows where to find the I & L regs on the stack. */ | |
308 | ||
309 | void | |
310 | fetch_inferior_registers (regno) | |
311 | int regno; | |
312 | { | |
313 | #if 0 | |
314 | int whatregs = 0; | |
315 | ||
316 | #define WHATREGS_FLOAT 1 | |
317 | #define WHATREGS_GEN 2 | |
318 | #define WHATREGS_STACK 4 | |
319 | ||
320 | if (regno == -1) | |
321 | whatregs = WHATREGS_FLOAT | WHATREGS_GEN | WHATREGS_STACK; | |
322 | else if (regno >= L0_REGNUM && regno <= I7_REGNUM) | |
323 | whatregs = WHATREGS_STACK; | |
324 | else if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32) | |
325 | whatregs = WHATREGS_FLOAT; | |
326 | else | |
327 | whatregs = WHATREGS_GEN; | |
328 | ||
329 | if (whatregs & WHATREGS_GEN) | |
330 | { | |
331 | struct econtext ec; /* general regs */ | |
332 | char buf[MAX_REGISTER_RAW_SIZE]; | |
333 | int retval; | |
334 | int i; | |
335 | ||
336 | errno = 0; | |
337 | retval = ptrace (PTRACE_GETREGS, inferior_pid, (PTRACE_ARG3_TYPE) &ec, | |
338 | 0); | |
339 | if (errno) | |
340 | perror_with_name ("Sparc fetch_inferior_registers(ptrace)"); | |
341 | ||
342 | memset (buf, 0, REGISTER_RAW_SIZE (G0_REGNUM)); | |
343 | supply_register (G0_REGNUM, buf); | |
344 | supply_register (TBR_REGNUM, (char *)&ec.tbr); | |
345 | ||
346 | memcpy (®isters[REGISTER_BYTE (G1_REGNUM)], &ec.g1, | |
347 | 4 * REGISTER_RAW_SIZE (G1_REGNUM)); | |
348 | for (i = G1_REGNUM; i <= G1_REGNUM + 3; i++) | |
349 | register_valid[i] = 1; | |
350 | ||
351 | supply_register (PS_REGNUM, (char *)&ec.psr); | |
352 | supply_register (Y_REGNUM, (char *)&ec.y); | |
353 | supply_register (PC_REGNUM, (char *)&ec.pc); | |
354 | supply_register (NPC_REGNUM, (char *)&ec.npc); | |
355 | supply_register (WIM_REGNUM, (char *)&ec.wim); | |
356 | ||
357 | memcpy (®isters[REGISTER_BYTE (O0_REGNUM)], ec.o, | |
358 | 8 * REGISTER_RAW_SIZE (O0_REGNUM)); | |
359 | for (i = O0_REGNUM; i <= O0_REGNUM + 7; i++) | |
360 | register_valid[i] = 1; | |
361 | } | |
362 | ||
363 | if (whatregs & WHATREGS_STACK) | |
364 | { | |
365 | CORE_ADDR sp; | |
366 | int i; | |
367 | ||
368 | sp = read_register (SP_REGNUM); | |
369 | ||
370 | target_xfer_memory (sp + FRAME_SAVED_I0, | |
371 | ®isters[REGISTER_BYTE(I0_REGNUM)], | |
372 | 8 * REGISTER_RAW_SIZE (I0_REGNUM), 0); | |
373 | for (i = I0_REGNUM; i <= I7_REGNUM; i++) | |
374 | register_valid[i] = 1; | |
375 | ||
376 | target_xfer_memory (sp + FRAME_SAVED_L0, | |
377 | ®isters[REGISTER_BYTE(L0_REGNUM)], | |
378 | 8 * REGISTER_RAW_SIZE (L0_REGNUM), 0); | |
379 | for (i = L0_REGNUM; i <= L0_REGNUM + 7; i++) | |
380 | register_valid[i] = 1; | |
381 | } | |
382 | ||
383 | if (whatregs & WHATREGS_FLOAT) | |
384 | { | |
385 | struct fcontext fc; /* fp regs */ | |
386 | int retval; | |
387 | int i; | |
388 | ||
389 | errno = 0; | |
390 | retval = ptrace (PTRACE_GETFPREGS, inferior_pid, (PTRACE_ARG3_TYPE) &fc, | |
391 | 0); | |
392 | if (errno) | |
393 | perror_with_name ("Sparc fetch_inferior_registers(ptrace)"); | |
394 | ||
395 | memcpy (®isters[REGISTER_BYTE (FP0_REGNUM)], fc.f.fregs, | |
396 | 32 * REGISTER_RAW_SIZE (FP0_REGNUM)); | |
397 | for (i = FP0_REGNUM; i <= FP0_REGNUM + 31; i++) | |
398 | register_valid[i] = 1; | |
399 | ||
400 | supply_register (FPS_REGNUM, (char *)&fc.fsr); | |
401 | } | |
402 | #endif | |
403 | } | |
404 | ||
405 | /* This routine handles storing of the I & L regs for the Sparc. The trick | |
406 | here is that they actually live on the stack. The really tricky part is | |
407 | that when changing the stack pointer, the I & L regs must be written to | |
408 | where the new SP points, otherwise the regs will be incorrect when the | |
409 | process is started up again. We assume that the I & L regs are valid at | |
410 | this point. */ | |
411 | ||
412 | void | |
413 | store_inferior_registers (regno) | |
414 | int regno; | |
415 | { | |
416 | #if 0 | |
417 | int whatregs = 0; | |
418 | ||
419 | if (regno == -1) | |
420 | whatregs = WHATREGS_FLOAT | WHATREGS_GEN | WHATREGS_STACK; | |
421 | else if (regno >= L0_REGNUM && regno <= I7_REGNUM) | |
422 | whatregs = WHATREGS_STACK; | |
423 | else if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32) | |
424 | whatregs = WHATREGS_FLOAT; | |
425 | else if (regno == SP_REGNUM) | |
426 | whatregs = WHATREGS_STACK | WHATREGS_GEN; | |
427 | else | |
428 | whatregs = WHATREGS_GEN; | |
429 | ||
430 | if (whatregs & WHATREGS_GEN) | |
431 | { | |
432 | struct econtext ec; /* general regs */ | |
433 | int retval; | |
434 | ||
435 | ec.tbr = read_register (TBR_REGNUM); | |
436 | memcpy (&ec.g1, ®isters[REGISTER_BYTE (G1_REGNUM)], | |
437 | 4 * REGISTER_RAW_SIZE (G1_REGNUM)); | |
438 | ||
439 | ec.psr = read_register (PS_REGNUM); | |
440 | ec.y = read_register (Y_REGNUM); | |
441 | ec.pc = read_register (PC_REGNUM); | |
442 | ec.npc = read_register (NPC_REGNUM); | |
443 | ec.wim = read_register (WIM_REGNUM); | |
444 | ||
445 | memcpy (ec.o, ®isters[REGISTER_BYTE (O0_REGNUM)], | |
446 | 8 * REGISTER_RAW_SIZE (O0_REGNUM)); | |
447 | ||
448 | errno = 0; | |
449 | retval = ptrace (PTRACE_SETREGS, inferior_pid, (PTRACE_ARG3_TYPE) &ec, | |
450 | 0); | |
451 | if (errno) | |
452 | perror_with_name ("Sparc fetch_inferior_registers(ptrace)"); | |
453 | } | |
454 | ||
455 | if (whatregs & WHATREGS_STACK) | |
456 | { | |
457 | int regoffset; | |
458 | CORE_ADDR sp; | |
459 | ||
460 | sp = read_register (SP_REGNUM); | |
461 | ||
462 | if (regno == -1 || regno == SP_REGNUM) | |
463 | { | |
464 | if (!register_valid[L0_REGNUM+5]) | |
465 | abort(); | |
466 | target_xfer_memory (sp + FRAME_SAVED_I0, | |
467 | ®isters[REGISTER_BYTE (I0_REGNUM)], | |
468 | 8 * REGISTER_RAW_SIZE (I0_REGNUM), 1); | |
469 | ||
470 | target_xfer_memory (sp + FRAME_SAVED_L0, | |
471 | ®isters[REGISTER_BYTE (L0_REGNUM)], | |
472 | 8 * REGISTER_RAW_SIZE (L0_REGNUM), 1); | |
473 | } | |
474 | else if (regno >= L0_REGNUM && regno <= I7_REGNUM) | |
475 | { | |
476 | if (!register_valid[regno]) | |
477 | abort(); | |
478 | if (regno >= L0_REGNUM && regno <= L0_REGNUM + 7) | |
479 | regoffset = REGISTER_BYTE (regno) - REGISTER_BYTE (L0_REGNUM) | |
480 | + FRAME_SAVED_L0; | |
481 | else | |
482 | regoffset = REGISTER_BYTE (regno) - REGISTER_BYTE (I0_REGNUM) | |
483 | + FRAME_SAVED_I0; | |
484 | target_xfer_memory (sp + regoffset, ®isters[REGISTER_BYTE (regno)], | |
485 | REGISTER_RAW_SIZE (regno), 1); | |
486 | } | |
487 | } | |
488 | ||
489 | if (whatregs & WHATREGS_FLOAT) | |
490 | { | |
491 | struct fcontext fc; /* fp regs */ | |
492 | int retval; | |
493 | ||
494 | /* We read fcontext first so that we can get good values for fq_t... */ | |
495 | errno = 0; | |
496 | retval = ptrace (PTRACE_GETFPREGS, inferior_pid, (PTRACE_ARG3_TYPE) &fc, | |
497 | 0); | |
498 | if (errno) | |
499 | perror_with_name ("Sparc fetch_inferior_registers(ptrace)"); | |
500 | ||
501 | memcpy (fc.f.fregs, ®isters[REGISTER_BYTE (FP0_REGNUM)], | |
502 | 32 * REGISTER_RAW_SIZE (FP0_REGNUM)); | |
503 | ||
504 | fc.fsr = read_register (FPS_REGNUM); | |
505 | ||
506 | errno = 0; | |
507 | retval = ptrace (PTRACE_SETFPREGS, inferior_pid, (PTRACE_ARG3_TYPE) &fc, | |
508 | 0); | |
509 | if (errno) | |
510 | perror_with_name ("Sparc fetch_inferior_registers(ptrace)"); | |
511 | } | |
512 | #endif | |
513 | } | |
514 | #endif /* SPARC */ | |
515 | ||
516 | #ifndef SPARC | |
517 | ||
9a13e99e SG |
518 | /* Return the offset relative to the start of the per-thread data to the |
519 | saved context block. */ | |
520 | ||
521 | static unsigned long | |
522 | lynx_registers_addr() | |
523 | { | |
524 | CORE_ADDR stblock; | |
525 | int ecpoff = offsetof(st_t, ecp); | |
526 | CORE_ADDR ecp; | |
527 | ||
528 | errno = 0; | |
529 | stblock = (CORE_ADDR) ptrace (PTRACE_THREADUSER, inferior_pid, | |
530 | (PTRACE_ARG3_TYPE)0, 0); | |
531 | if (errno) | |
532 | perror_with_name ("PTRACE_THREADUSER"); | |
533 | ||
534 | ecp = (CORE_ADDR) ptrace (PTRACE_PEEKTHREAD, inferior_pid, | |
535 | (PTRACE_ARG3_TYPE)ecpoff, 0); | |
536 | if (errno) | |
537 | perror_with_name ("lynx_registers_addr(PTRACE_PEEKTHREAD)"); | |
538 | ||
539 | return ecp - stblock; | |
540 | } | |
e20520b8 SG |
541 | |
542 | /* Fetch one or more registers from the inferior. REGNO == -1 to get | |
543 | them all. We actually fetch more than requested, when convenient, | |
544 | marking them as valid so we won't fetch them again. */ | |
545 | ||
546 | void | |
547 | fetch_inferior_registers (ignored) | |
548 | int ignored; | |
549 | { | |
550 | int regno; | |
551 | unsigned long reg; | |
9a13e99e | 552 | unsigned long ecp; |
e20520b8 SG |
553 | |
554 | ecp = lynx_registers_addr(); | |
555 | ||
556 | for (regno = 0; regno < NUM_REGS; regno++) | |
557 | { | |
9a13e99e SG |
558 | int ptrace_fun = PTRACE_PEEKTHREAD; |
559 | ||
560 | #ifdef PTRACE_PEEKUSP | |
561 | ptrace_fun = regno == SP_REGNUM ? PTRACE_PEEKUSP : PTRACE_PEEKTHREAD; | |
562 | #endif | |
563 | ||
e20520b8 | 564 | errno = 0; |
9a13e99e SG |
565 | reg = ptrace (ptrace_fun, inferior_pid, |
566 | (PTRACE_ARG3_TYPE) (ecp + regmap[regno]), 0); | |
e20520b8 SG |
567 | if (errno) |
568 | perror_with_name ("fetch_inferior_registers(PTRACE_PEEKTHREAD)"); | |
569 | ||
570 | *(unsigned long *)®isters[REGISTER_BYTE (regno)] = reg; | |
571 | } | |
572 | } | |
573 | ||
574 | /* Store our register values back into the inferior. | |
575 | If REGNO is -1, do this for all registers. | |
576 | Otherwise, REGNO specifies which register (so we can save time). */ | |
577 | ||
578 | void | |
579 | store_inferior_registers (ignored) | |
580 | int ignored; | |
581 | { | |
582 | int regno; | |
583 | unsigned long reg; | |
9a13e99e | 584 | unsigned long ecp; |
e20520b8 SG |
585 | |
586 | ecp = lynx_registers_addr(); | |
587 | ||
588 | for (regno = 0; regno < NUM_REGS; regno++) | |
589 | { | |
9a13e99e SG |
590 | int ptrace_fun = PTRACE_POKEUSER; |
591 | ||
592 | #ifdef PTRACE_POKEUSP | |
593 | ptrace_fun = regno == SP_REGNUM ? PTRACE_POKEUSP : PTRACE_POKEUSER; | |
594 | #endif | |
595 | ||
e20520b8 SG |
596 | reg = *(unsigned long *)®isters[REGISTER_BYTE (regno)]; |
597 | ||
598 | errno = 0; | |
9a13e99e SG |
599 | ptrace (ptrace_fun, inferior_pid, |
600 | (PTRACE_ARG3_TYPE) (ecp + regmap[regno]), reg); | |
e20520b8 SG |
601 | if (errno) |
602 | perror_with_name ("PTRACE_POKEUSER"); | |
603 | } | |
604 | } | |
605 | ||
b292fca8 SS |
606 | #endif /* ! SPARC */ |
607 | ||
e20520b8 SG |
608 | /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory |
609 | in the NEW_SUN_PTRACE case. | |
610 | It ought to be straightforward. But it appears that writing did | |
611 | not write the data that I specified. I cannot understand where | |
612 | it got the data that it actually did write. */ | |
613 | ||
614 | /* Copy LEN bytes from inferior's memory starting at MEMADDR | |
615 | to debugger memory starting at MYADDR. */ | |
616 | ||
41e170e2 | 617 | void |
e20520b8 SG |
618 | read_inferior_memory (memaddr, myaddr, len) |
619 | CORE_ADDR memaddr; | |
620 | char *myaddr; | |
621 | int len; | |
622 | { | |
623 | register int i; | |
624 | /* Round starting address down to longword boundary. */ | |
625 | register CORE_ADDR addr = memaddr & -sizeof (int); | |
626 | /* Round ending address up; get number of longwords that makes. */ | |
627 | register int count | |
628 | = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); | |
629 | /* Allocate buffer of that many longwords. */ | |
630 | register int *buffer = (int *) alloca (count * sizeof (int)); | |
631 | ||
632 | /* Read all the longwords */ | |
633 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
634 | { | |
41e170e2 | 635 | buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid, addr, 0); |
e20520b8 SG |
636 | } |
637 | ||
638 | /* Copy appropriate bytes out of the buffer. */ | |
a6e0dae9 | 639 | memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len); |
e20520b8 SG |
640 | } |
641 | ||
642 | /* Copy LEN bytes of data from debugger memory at MYADDR | |
643 | to inferior's memory at MEMADDR. | |
644 | On failure (cannot write the inferior) | |
645 | returns the value of errno. */ | |
646 | ||
647 | int | |
648 | write_inferior_memory (memaddr, myaddr, len) | |
649 | CORE_ADDR memaddr; | |
650 | char *myaddr; | |
651 | int len; | |
652 | { | |
653 | register int i; | |
654 | /* Round starting address down to longword boundary. */ | |
655 | register CORE_ADDR addr = memaddr & -sizeof (int); | |
656 | /* Round ending address up; get number of longwords that makes. */ | |
657 | register int count | |
658 | = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); | |
659 | /* Allocate buffer of that many longwords. */ | |
660 | register int *buffer = (int *) alloca (count * sizeof (int)); | |
661 | extern int errno; | |
662 | ||
663 | /* Fill start and end extra bytes of buffer with existing memory data. */ | |
664 | ||
41e170e2 | 665 | buffer[0] = ptrace (PTRACE_PEEKTEXT, inferior_pid, addr, 0); |
e20520b8 SG |
666 | |
667 | if (count > 1) | |
668 | { | |
669 | buffer[count - 1] | |
41e170e2 | 670 | = ptrace (PTRACE_PEEKTEXT, inferior_pid, |
e20520b8 SG |
671 | addr + (count - 1) * sizeof (int), 0); |
672 | } | |
673 | ||
674 | /* Copy data to be written over corresponding part of buffer */ | |
675 | ||
a6e0dae9 | 676 | memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len); |
e20520b8 SG |
677 | |
678 | /* Write the entire buffer. */ | |
679 | ||
680 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
681 | { | |
db691e4b SG |
682 | while (1) |
683 | { | |
684 | errno = 0; | |
685 | ptrace (PTRACE_POKETEXT, inferior_pid, addr, buffer[i]); | |
686 | if (errno) | |
687 | { | |
688 | fprintf(stderr, "ptrace (PTRACE_POKETEXT): errno=%d, inferior_pid=0x%x, addr=0x%x, buffer[i] = 0x%x\n", errno, inferior_pid, addr, buffer[i]); | |
689 | fprintf(stderr, "Sleeping for 1 second\n"); | |
690 | sleep(1); | |
691 | } | |
692 | else | |
693 | break; | |
694 | } | |
e20520b8 SG |
695 | } |
696 | ||
697 | return 0; | |
698 | } |