gdb-2.8.1
[deliverable/binutils-gdb.git] / gdb / inflow.c
1 /* Low level interface to ptrace, for GDB when running under Unix.
2 Copyright (C) 1986, 1987 Free Software Foundation, Inc.
3
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY. No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
9
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License. A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities. It
14 should be in a file named COPYING. Among other things, the copyright
15 notice and this notice must be preserved on all copies.
16
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther. Help stamp out software hoarding!
19 */
20
21 #include "defs.h"
22 #include "initialize.h"
23 #include "param.h"
24 #include "frame.h"
25 #include "inferior.h"
26
27 #include <stdio.h>
28 #include <sys/param.h>
29 #include <sys/dir.h>
30 #ifndef UMAX_PTRACE
31 #include <sys/user.h>
32 #endif
33 #include <signal.h>
34 #include <sys/ioctl.h>
35 #include <fcntl.h>
36
37 #ifdef UMAX_PTRACE
38 #include <a.out.h>
39 #include <sys/ptrace.h>
40 #define PTRACE_ATTACH PT_ATTACH
41 #define PTRACE_DETACH PT_FREEPROC
42 #endif
43
44 #ifdef NEW_SUN_PTRACE
45 #include <sys/ptrace.h>
46 #include <machine/reg.h>
47 #endif
48
49 #ifdef HP9K320
50 #include <sys/ptrace.h>
51 #include <sys/reg.h>
52 #include <sys/trap.h>
53 #endif
54
55 #ifdef HAVE_TERMIO
56 #include <termio.h>
57 #undef TIOCGETP
58 #define TIOCGETP TCGETA
59 #undef TIOCSETN
60 #define TIOCSETN TCSETA
61 #undef TIOCSETP
62 #define TIOCSETP TCSETAF
63 #define TERMINAL struct termio
64 #else
65 #include <sgtty.h>
66 #define TERMINAL struct sgttyb
67 #endif
68
69 extern int errno;
70
71 /* Nonzero if we are debugging an attached outside process
72 rather than an inferior. */
73
74 static int attach_flag;
75
76 START_FILE
77 \f
78 /* Record terminal status separately for debugger and inferior. */
79
80 static TERMINAL sg_inferior;
81 static TERMINAL sg_ours;
82 static int tflags_inferior;
83 static int tflags_ours;
84
85 #ifdef TIOCGLTC
86 static struct tchars tc_inferior;
87 static struct tchars tc_ours;
88 static struct ltchars ltc_inferior;
89 static struct ltchars ltc_ours;
90 static int lmode_inferior;
91 static int lmode_ours;
92 #endif /* TIOCGLTC */
93
94 #ifdef TIOCGPGRP
95 static int pgrp_inferior;
96 static int pgrp_ours;
97 #else
98 static int (*sigint_ours) ();
99 static int (*sigquit_ours) ();
100 #endif /* TIOCGPGRP */
101
102 /* Copy of inferior_io_terminal when inferior was last started. */
103 static char *inferior_thisrun_terminal;
104
105 static void terminal_ours_1 ();
106
107 /* Nonzero if our terminal settings are in effect.
108 Zero if the inferior's settings are in effect. */
109 static int terminal_is_ours;
110
111 /* Initialize the terminal settings we record for the inferior,
112 before we actually run the inferior. */
113
114 void
115 terminal_init_inferior ()
116 {
117 if (remote_debugging)
118 return;
119
120 sg_inferior = sg_ours;
121 tflags_inferior = tflags_ours;
122
123 #ifdef TIOCGLTC
124 tc_inferior = tc_ours;
125 ltc_inferior = ltc_ours;
126 lmode_inferior = lmode_ours;
127 #endif /* TIOCGLTC */
128
129 #ifdef TIOCGPGRP
130 pgrp_inferior = inferior_pid;
131 #endif /* TIOCGPGRP */
132
133 terminal_is_ours = 1;
134 }
135
136 /* Put the inferior's terminal settings into effect.
137 This is preparation for starting or resuming the inferior. */
138
139 void
140 terminal_inferior ()
141 {
142 if (remote_debugging)
143 return;
144
145 if (terminal_is_ours) /* && inferior_thisrun_terminal == 0) */
146 {
147 fcntl (0, F_SETFL, tflags_inferior);
148 fcntl (0, F_SETFL, tflags_inferior);
149 ioctl (0, TIOCSETN, &sg_inferior);
150
151 #ifdef TIOCGLTC
152 ioctl (0, TIOCSETC, &tc_inferior);
153 ioctl (0, TIOCSLTC, &ltc_inferior);
154 ioctl (0, TIOCLSET, &lmode_inferior);
155 #endif /* TIOCGLTC */
156
157 #ifdef TIOCGPGRP
158 ioctl (0, TIOCSPGRP, &pgrp_inferior);
159 #else
160 sigint_ours = (signal (SIGINT, SIG_IGN));
161 sigquit_ours = (signal (SIGQUIT, SIG_IGN));
162 #endif /* TIOCGPGRP */
163 }
164 terminal_is_ours = 0;
165 }
166
167 /* Put some of our terminal settings into effect,
168 enough to get proper results from our output,
169 but do not change into or out of RAW mode
170 so that no input is discarded.
171
172 After doing this, either terminal_ours or terminal_inferior
173 should be called to get back to a normal state of affairs. */
174
175 void
176 terminal_ours_for_output ()
177 {
178 if (remote_debugging)
179 return;
180
181 terminal_ours_1 (1);
182 }
183
184 /* Put our terminal settings into effect.
185 First record the inferior's terminal settings
186 so they can be restored properly later. */
187
188 void
189 terminal_ours ()
190 {
191 if (remote_debugging)
192 return;
193
194 terminal_ours_1 (0);
195 }
196
197 static void
198 terminal_ours_1 (output_only)
199 int output_only;
200 {
201 #ifdef TIOCGPGRP
202 /* Ignore this signal since it will happen when we try to set the pgrp. */
203 int (*osigttou) ();
204 #endif /* TIOCGPGRP */
205
206 if (!terminal_is_ours) /* && inferior_thisrun_terminal == 0) */
207 {
208 terminal_is_ours = 1;
209
210 #ifdef TIOCGPGRP
211 osigttou = signal (SIGTTOU, SIG_IGN);
212
213 ioctl (0, TIOCGPGRP, &pgrp_inferior);
214 ioctl (0, TIOCSPGRP, &pgrp_ours);
215
216 signal (SIGTTOU, osigttou);
217 #else
218 signal (SIGINT, sigint_ours);
219 signal (SIGQUIT, sigquit_ours);
220 #endif /* TIOCGPGRP */
221
222 tflags_inferior = fcntl (0, F_GETFL, 0);
223 ioctl (0, TIOCGETP, &sg_inferior);
224
225 #ifdef TIOCGLTC
226 ioctl (0, TIOCGETC, &tc_inferior);
227 ioctl (0, TIOCGLTC, &ltc_inferior);
228 ioctl (0, TIOCLGET, &lmode_inferior);
229 #endif /* TIOCGLTC */
230 }
231
232 #ifdef HAVE_TERMIO
233 sg_ours.c_lflag |= ICANON;
234 if (output_only && !(sg_inferior.c_lflag & ICANON))
235 sg_ours.c_lflag &= ~ICANON;
236 #else /* not HAVE_TERMIO */
237 sg_ours.sg_flags &= ~RAW & ~CBREAK;
238 if (output_only)
239 sg_ours.sg_flags |= (RAW | CBREAK) & sg_inferior.sg_flags;
240 #endif /* not HAVE_TERMIO */
241
242 fcntl (0, F_SETFL, tflags_ours);
243 fcntl (0, F_SETFL, tflags_ours);
244 ioctl (0, TIOCSETN, &sg_ours);
245
246 #ifdef TIOCGLTC
247 ioctl (0, TIOCSETC, &tc_ours);
248 ioctl (0, TIOCSLTC, &ltc_ours);
249 ioctl (0, TIOCLSET, &lmode_ours);
250 #endif /* TIOCGLTC */
251
252
253 #ifdef HAVE_TERMIO
254 sg_ours.c_lflag |= ICANON;
255 #else /* not HAVE_TERMIO */
256 sg_ours.sg_flags &= ~RAW & ~CBREAK;
257 #endif /* not HAVE_TERMIO */
258 }
259
260 static void
261 term_status_command ()
262 {
263 register int i;
264
265 if (remote_debugging)
266 {
267 printf ("No terminal status when remote debugging.\n");
268 return;
269 }
270
271 printf ("Inferior's terminal status (currently saved by GDB):\n");
272
273 #ifdef HAVE_TERMIO
274
275 printf ("fcntl flags = 0x%x, c_iflag = 0x%x, c_oflag = 0x%x,\n",
276 tflags_inferior, sg_inferior.c_iflag, sg_inferior.c_oflag);
277 printf ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
278 sg_inferior.c_cflag, sg_inferior.c_lflag, sg_inferior.c_line);
279 printf ("c_cc: ");
280 for (i = 0; (i < NCC); i += 1)
281 printf ("0x%x ", sg_inferior.c_cc[i]);
282 printf ("\n");
283
284 #else /* not HAVE_TERMIO */
285
286 printf ("fcntl flags = 0x%x, lmode = 0x%x,\nsgttyb.sg_flags = 0x%x, owner pid = %d.\n",
287 tflags_inferior, lmode_inferior,
288 sg_inferior.sg_flags, pgrp_inferior);
289 printf ("tchars: ");
290 for (i = 0; i < sizeof (struct tchars); i++)
291 printf ("0x%x ", ((char *)&tc_inferior)[i]);
292 printf ("\n");
293 printf ("ltchars: ");
294 for (i = 0; i < sizeof (struct ltchars); i++)
295 printf ("0x%x ", ((char *)&ltc_inferior)[i]);
296
297 #endif /* not HAVE_TERMIO */
298 }
299 \f
300 static void
301 new_tty (ttyname)
302 char *ttyname;
303 {
304 register int tty;
305 register int fd;
306
307 #if 0
308 /* I think it is better not to do this. Then C-z on the GDB terminal
309 will still stop the program, while C-z on the data terminal
310 will be input. */
311
312 /* Disconnect the child process from our controlling terminal. */
313 tty = open("/dev/tty", O_RDWR);
314 if (tty > 0)
315 {
316 ioctl(tty, TIOCNOTTY, 0);
317 close(tty);
318 }
319 #endif
320 /* Now open the specified new terminal. */
321
322 tty = open(ttyname, O_RDWR);
323 if (tty == -1)
324 _exit(1);
325
326 dup2(tty, 0);
327 dup2(tty, 1);
328 dup2(tty, 2);
329 close(tty);
330 }
331 \f
332 /* Start an inferior process and returns its pid.
333 ALLARGS is a string containing shell command to run the program.
334 ENV is the environment vector to pass. */
335
336 #ifndef SHELL_FILE
337 #define SHELL_FILE "/bin/sh"
338 #endif
339
340 int
341 create_inferior (allargs, env)
342 char *allargs;
343 char **env;
344 {
345 int pid;
346 char *shell_command;
347 extern int sys_nerr;
348 extern char *sys_errlist[];
349 extern int errno;
350
351 /* If desired, concat something onto the front of ALLARGS.
352 SHELL_COMMAND is the result. */
353 #ifdef SHELL_COMMAND_CONCAT
354 shell_command = (char *) alloca (strlen (SHELL_COMMAND_CONCAT) + strlen (allargs) + 1);
355 strcpy (shell_command, SHELL_COMMAND_CONCAT);
356 strcat (shell_command, allargs);
357 #else
358 shell_command = allargs;
359 #endif
360
361 /* exec is said to fail if the executable is open. */
362 close_exec_file ();
363
364 pid = vfork ();
365 if (pid < 0)
366 perror_with_name ("vfork");
367
368 if (pid == 0)
369 {
370 char *args[4];
371
372 #ifdef TIOCGPGRP
373 /* Run inferior in a separate process group. */
374 setpgrp (getpid (), getpid ());
375 #endif /* TIOCGPGRP */
376
377 inferior_thisrun_terminal = inferior_io_terminal;
378 if (inferior_io_terminal != 0)
379 new_tty (inferior_io_terminal);
380
381 /* Not needed on Sun, at least, and loses there
382 because it clobbers the superior. */
383 /*??? signal (SIGQUIT, SIG_DFL);
384 signal (SIGINT, SIG_DFL); */
385
386 ptrace (0);
387
388 args[0] = "sh";
389 args[1] = "-c";
390 args[2] = shell_command;
391 args[3] = 0;
392
393 execve (SHELL_FILE, args, env);
394
395 fprintf (stderr, "Cannot exec %s: %s.\n", SHELL_FILE,
396 errno < sys_nerr ? sys_errlist[errno] : "unknown error");
397 fflush (stderr);
398 _exit (0177);
399 }
400 return pid;
401 }
402
403 /* Kill the inferior process. Make us have no inferior. */
404
405 static void
406 kill_command ()
407 {
408 if (remote_debugging)
409 return;
410 if (inferior_pid == 0)
411 error ("The program is not being run.");
412 if (!query ("Kill the inferior process? "))
413 error ("Not confirmed.");
414 kill_inferior ();
415 }
416
417 kill_inferior ()
418 {
419 if (remote_debugging)
420 return;
421 if (inferior_pid == 0)
422 return;
423 ptrace (8, inferior_pid, 0, 0);
424 wait (0);
425 inferior_died ();
426 }
427
428 /* This is used when GDB is exiting. It gives less chance of error.*/
429
430 kill_inferior_fast ()
431 {
432 if (remote_debugging)
433 return;
434 if (inferior_pid == 0)
435 return;
436 ptrace (8, inferior_pid, 0, 0);
437 wait (0);
438 }
439
440 inferior_died ()
441 {
442 inferior_pid = 0;
443 attach_flag = 0;
444 mark_breakpoints_out ();
445 reopen_exec_file ();
446 if (have_core_file_p ())
447 set_current_frame (read_register (FP_REGNUM));
448 }
449
450 /* Resume execution of the inferior process.
451 If STEP is nonzero, single-step it.
452 If SIGNAL is nonzero, give it that signal. */
453
454 void
455 resume (step, signal)
456 int step;
457 int signal;
458 {
459 errno = 0;
460 if (remote_debugging)
461 remote_resume (step, signal);
462 else
463 {
464 #ifdef NO_SINGLE_STEP
465 if (step)
466 {
467 single_step (signal);
468 }
469 else ptrace (7, inferior_pid, 1, signal);
470 #else
471 ptrace (step ? 9 : 7, inferior_pid, 1, signal);
472 #endif
473 if (errno)
474 perror_with_name ("ptrace");
475 }
476 }
477 \f
478 #ifdef ATTACH_DETACH
479
480 /* Start debugging the process whose number is PID. */
481
482 attach (pid)
483 int pid;
484 {
485 errno = 0;
486 ptrace (PTRACE_ATTACH, pid, 0, 0);
487 if (errno)
488 perror_with_name ("ptrace");
489 attach_flag = 1;
490 return pid;
491 }
492
493 /* Stop debugging the process whose number is PID
494 and continue it with signal number SIGNAL.
495 SIGNAL = 0 means just continue it. */
496
497 void
498 detach (signal)
499 int signal;
500 {
501 errno = 0;
502 ptrace (PTRACE_DETACH, inferior_pid, 1, signal);
503 if (errno)
504 perror_with_name ("ptrace");
505 attach_flag = 0;
506 }
507 #endif /* ATTACH_DETACH */
508 \f
509 #ifdef NEW_SUN_PTRACE
510
511 void
512 fetch_inferior_registers ()
513 {
514 struct regs inferior_registers;
515 struct fp_status inferior_fp_registers;
516 extern char registers[];
517
518 if (remote_debugging)
519 remote_fetch_registers (registers);
520 else
521 {
522 ptrace (PTRACE_GETREGS, inferior_pid, &inferior_registers);
523 ptrace (PTRACE_GETFPREGS, inferior_pid, &inferior_fp_registers);
524
525 #if defined(sun2) || defined(sun3)
526 bcopy (&inferior_registers, registers, 16 * 4);
527 bcopy (&inferior_fp_registers, &registers[REGISTER_BYTE (FP0_REGNUM)],
528 sizeof inferior_fp_registers.fps_regs);
529 *(int *)&registers[REGISTER_BYTE (PS_REGNUM)] = inferior_registers.r_ps;
530 *(int *)&registers[REGISTER_BYTE (PC_REGNUM)] = inferior_registers.r_pc;
531 bcopy (&inferior_fp_registers.fps_control,
532 &registers[REGISTER_BYTE (FPC_REGNUM)],
533 sizeof inferior_fp_registers - sizeof inferior_fp_registers.fps_regs);
534 #endif
535 #if defined(sun4)
536 registers[REGISTER_BYTE (0)] = 0;
537 bcopy (&inferior_registers.r_g1, &registers[REGISTER_BYTE (1)], 15 * 4);
538 bcopy (&inferior_fp_registers, &registers[REGISTER_BYTE (FP0_REGNUM)],
539 sizeof inferior_fp_registers.fpu_fr);
540 *(int *)&registers[REGISTER_BYTE (PS_REGNUM)] = inferior_registers.r_ps;
541 *(int *)&registers[REGISTER_BYTE (PC_REGNUM)] = inferior_registers.r_pc;
542 *(int *)&registers[REGISTER_BYTE (NPC_REGNUM)] = inferior_registers.r_npc;
543 *(int *)&registers[REGISTER_BYTE (Y_REGNUM)] = inferior_registers.r_y;
544 /* *(int *)&registers[REGISTER_BYTE (RP_REGNUM)] =
545 inferior_registers.r_o7 + 8;
546 bcopy (&inferior_fp_registers.Fpu_fsr,
547 &registers[REGISTER_BYTE (FPS_REGNUM)],
548 sizeof (FPU_FSR_TYPE)); */
549 read_inferior_memory (inferior_registers.r_sp,
550 &registers[REGISTER_BYTE (16)],
551 16*4);
552 #endif
553 }
554 }
555
556 /* Store our register values back into the inferior.
557 If REGNO is -1, do this for all registers.
558 Otherwise, REGNO specifies which register (so we can save time). */
559
560 store_inferior_registers (regno)
561 int regno;
562 {
563 struct regs inferior_registers;
564 struct fp_status inferior_fp_registers;
565 extern char registers[];
566
567 if (remote_debugging)
568 remote_store_registers (registers);
569 else
570 {
571 int in_regs = 1, in_fpregs = 1, in_fparegs, in_cpregs = 1;
572
573 #if defined(sun2) || defined(sun3)
574 if (in_regs)
575 {
576 bcopy (registers, &inferior_registers, 16 * 4);
577 inferior_registers.r_ps = *(int *)&registers[REGISTER_BYTE (PS_REGNUM)];
578 inferior_registers.r_pc = *(int *)&registers[REGISTER_BYTE (PC_REGNUM)];
579 }
580 if (in_fpregs)
581 {
582 bcopy (&registers[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers,
583 sizeof inferior_fp_registers.fps_regs);
584 bcopy (&registers[REGISTER_BYTE (FPC_REGNUM)],
585 &inferior_fp_registers.fps_control,
586 sizeof inferior_fp_registers - sizeof inferior_fp_registers.fps_regs);
587 }
588 if (in_regs)
589 ptrace (PTRACE_SETREGS, inferior_pid, &inferior_registers);
590 if (in_fpregs)
591 ptrace (PTRACE_SETFPREGS, inferior_pid, &inferior_fp_registers);
592 #endif
593 #if defined(sun4)
594 if (regno >= 0)
595 if (FP0_REGNUM <= regno && regno <= FP0_REGNUM + 32)
596 in_regs = 0;
597 else
598 in_fpregs = 0;
599
600 if (in_regs)
601 {
602 bcopy (&registers[REGISTER_BYTE (1)], &inferior_registers.r_g1, 15 * 4);
603 inferior_registers.r_ps = *(int *)&registers[REGISTER_BYTE (PS_REGNUM)];
604 inferior_registers.r_pc = *(int *)&registers[REGISTER_BYTE (PC_REGNUM)];
605 inferior_registers.r_npc = *(int *)&registers[REGISTER_BYTE (NPC_REGNUM)];
606 inferior_registers.r_y = *(int *)&registers[REGISTER_BYTE (Y_REGNUM)];
607 write_inferior_memory (*(int *)&registers[REGISTER_BYTE (SP_REGNUM)],
608 &registers[REGISTER_BYTE (16)],
609 16*4);
610 }
611 if (in_fpregs)
612 {
613 bcopy (&registers[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers,
614 sizeof inferior_fp_registers.fpu_fr);
615 /* bcopy (&registers[REGISTER_BYTE (FPS_REGNUM)],
616 &inferior_fp_registers.Fpu_fsr,
617 sizeof (FPU_FSR_TYPE));
618 ****/
619 }
620
621 if (in_regs)
622 ptrace (PTRACE_SETREGS, inferior_pid, &inferior_registers);
623 if (in_fpregs)
624 ptrace (PTRACE_SETFPREGS, inferior_pid, &inferior_fp_registers);
625 #endif
626 }
627 }
628
629 #else
630 #ifdef HP9K320
631
632 #define FP_REGISTER_ADDR_DIFF(u, regno) \
633 (((char *) (FP_REGISTER_ADDR (u, regno))) - ((char *) &(u)))
634
635 #define INFERIOR_AR0(u) \
636 ((ptrace \
637 (PT_RUAREA, inferior_pid, ((char *) &u.u_ar0 - (char *) &u), 0)) \
638 - KERNEL_U_ADDR)
639
640 static void
641 fetch_inferior_register (regno, regaddr)
642 register int regno;
643 register unsigned int regaddr;
644 {
645 #ifndef HPUX_VERSION_5
646 if (regno == PS_REGNUM)
647 {
648 union { int i; short s[2]; } ps_val;
649 int regval;
650
651 ps_val.i = (ptrace (PT_RUAREA, inferior_pid, regaddr, 0));
652 regval = ps_val.s[0];
653 supply_register (regno, &regval);
654 }
655 else
656 #endif /* not HPUX_VERSION_5 */
657 {
658 char buf[MAX_REGISTER_RAW_SIZE];
659 register int i;
660
661 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
662 {
663 *(int *) &buf[i] = ptrace (PT_RUAREA, inferior_pid, regaddr, 0);
664 regaddr += sizeof (int);
665 }
666 supply_register (regno, buf);
667 }
668 return;
669 }
670
671 static void
672 store_inferior_register_1 (regno, regaddr, value)
673 int regno;
674 unsigned int regaddr;
675 int value;
676 {
677 errno = 0;
678 ptrace (PT_WUAREA, inferior_pid, regaddr, value);
679 #if 0
680 /* HP-UX randomly sets errno to non-zero for regno == 25.
681 However, the value is correctly written, so ignore errno. */
682 if (errno != 0)
683 {
684 char string_buf[64];
685
686 sprintf (string_buf, "writing register number %d", regno);
687 perror_with_name (string_buf);
688 }
689 #endif
690 return;
691 }
692
693 static void
694 store_inferior_register (regno, regaddr)
695 register int regno;
696 register unsigned int regaddr;
697 {
698 #ifndef HPUX_VERSION_5
699 if (regno == PS_REGNUM)
700 {
701 union { int i; short s[2]; } ps_val;
702
703 ps_val.i = (ptrace (PT_RUAREA, inferior_pid, regaddr, 0));
704 ps_val.s[0] = (read_register (regno));
705 store_inferior_register_1 (regno, regaddr, ps_val.i);
706 }
707 else
708 #endif /* not HPUX_VERSION_5 */
709 {
710 char buf[MAX_REGISTER_RAW_SIZE];
711 register int i;
712 extern char registers[];
713
714 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
715 {
716 store_inferior_register_1
717 (regno, regaddr,
718 (*(int *) &registers[(REGISTER_BYTE (regno)) + i]));
719 regaddr += sizeof (int);
720 }
721 }
722 return;
723 }
724
725 void
726 fetch_inferior_registers ()
727 {
728 struct user u;
729 register int regno;
730 register unsigned int ar0_offset;
731
732 ar0_offset = (INFERIOR_AR0 (u));
733 for (regno = 0; (regno < FP0_REGNUM); regno++)
734 fetch_inferior_register (regno, (REGISTER_ADDR (ar0_offset, regno)));
735 for (; (regno < NUM_REGS); regno++)
736 fetch_inferior_register (regno, (FP_REGISTER_ADDR_DIFF (u, regno)));
737 }
738
739 /* Store our register values back into the inferior.
740 If REGNO is -1, do this for all registers.
741 Otherwise, REGNO specifies which register (so we can save time). */
742
743 store_inferior_registers (regno)
744 register int regno;
745 {
746 struct user u;
747 register unsigned int ar0_offset;
748
749 if (regno >= FP0_REGNUM)
750 {
751 store_inferior_register (regno, (FP_REGISTER_ADDR_DIFF (u, regno)));
752 return;
753 }
754
755 ar0_offset = (INFERIOR_AR0 (u));
756 if (regno >= 0)
757 {
758 store_inferior_register (regno, (REGISTER_ADDR (ar0_offset, regno)));
759 return;
760 }
761
762 for (regno = 0; (regno < FP0_REGNUM); regno++)
763 store_inferior_register (regno, (REGISTER_ADDR (ar0_offset, regno)));
764 for (; (regno < NUM_REGS); regno++)
765 store_inferior_register (regno, (FP_REGISTER_ADDR_DIFF (u, regno)));
766 return;
767 }
768
769 #else /* not HP9K320 */
770
771 void
772 fetch_inferior_registers ()
773 {
774 register int regno;
775 register unsigned int regaddr;
776 char buf[MAX_REGISTER_RAW_SIZE];
777 register int i;
778
779 #ifdef UMAX_PTRACE
780 unsigned int offset = 0;
781 #else
782 struct user u;
783 unsigned int offset = (char *) &u.u_ar0 - (char *) &u;
784 offset = ptrace (3, inferior_pid, offset, 0) - KERNEL_U_ADDR;
785 #endif
786
787 for (regno = 0; regno < NUM_REGS; regno++)
788 {
789 regaddr = register_addr (regno, offset);
790 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
791 {
792 *(int *) &buf[i] = ptrace (3, inferior_pid, regaddr, 0);
793 regaddr += sizeof (int);
794 }
795 supply_register (regno, buf);
796 }
797 }
798
799 /* Store our register values back into the inferior.
800 If REGNO is -1, do this for all registers.
801 Otherwise, REGNO specifies which register (so we can save time). */
802
803 store_inferior_registers (regno)
804 int regno;
805 {
806 register unsigned int regaddr;
807 char buf[80];
808 extern char registers[];
809 int i;
810
811 #ifdef UMAX_PTRACE
812 unsigned int offset = 0;
813 #else
814 struct user u;
815 unsigned int offset = (char *) &u.u_ar0 - (char *) &u;
816 offset = ptrace (3, inferior_pid, offset, 0) - KERNEL_U_ADDR;
817 #endif
818
819 if (regno >= 0)
820 {
821 regaddr = register_addr (regno, offset);
822 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
823 {
824 errno = 0;
825 ptrace (6, inferior_pid, regaddr,
826 *(int *) &registers[REGISTER_BYTE (regno) + i]);
827 if (errno != 0)
828 {
829 sprintf (buf, "writing register number %d(%d)", regno, i);
830 perror_with_name (buf);
831 }
832 regaddr += sizeof(int);
833 }
834 }
835 else for (regno = 0; regno < NUM_REGS; regno++)
836 {
837 regaddr = register_addr (regno, offset);
838 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
839 {
840 errno = 0;
841 ptrace (6, inferior_pid, regaddr,
842 *(int *) &registers[REGISTER_BYTE (regno) + i]);
843 if (errno != 0)
844 {
845 sprintf (buf, "writing register number %d(%d)", regno, i);
846 perror_with_name (buf);
847 }
848 regaddr += sizeof(int);
849 }
850 }
851 }
852
853 #endif /* not HP9K320 */
854 #endif /* not NEW_SUN_PTRACE */
855 \f
856 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
857 in the NEW_SUN_PTRACE case.
858 It ought to be straightforward. But it appears that writing did
859 not write the data that I specified. I cannot understand where
860 it got the data that it actually did write. */
861
862 /* Copy LEN bytes from inferior's memory starting at MEMADDR
863 to debugger memory starting at MYADDR. */
864
865 read_inferior_memory (memaddr, myaddr, len)
866 CORE_ADDR memaddr;
867 char *myaddr;
868 int len;
869 {
870 register int i;
871 /* Round starting address down to longword boundary. */
872 register CORE_ADDR addr = memaddr & - sizeof (int);
873 /* Round ending address up; get number of longwords that makes. */
874 register int count
875 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
876 /* Allocate buffer of that many longwords. */
877 register int *buffer = (int *) alloca (count * sizeof (int));
878
879 /* Read all the longwords */
880 for (i = 0; i < count; i++, addr += sizeof (int))
881 {
882 if (remote_debugging)
883 buffer[i] = remote_fetch_word (addr);
884 else
885 buffer[i] = ptrace (1, inferior_pid, addr, 0);
886 }
887
888 /* Copy appropriate bytes out of the buffer. */
889 bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
890 }
891
892 /* Copy LEN bytes of data from debugger memory at MYADDR
893 to inferior's memory at MEMADDR.
894 On failure (cannot write the inferior)
895 returns the value of errno. */
896
897 int
898 write_inferior_memory (memaddr, myaddr, len)
899 CORE_ADDR memaddr;
900 char *myaddr;
901 int len;
902 {
903 register int i;
904 /* Round starting address down to longword boundary. */
905 register CORE_ADDR addr = memaddr & - sizeof (int);
906 /* Round ending address up; get number of longwords that makes. */
907 register int count
908 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
909 /* Allocate buffer of that many longwords. */
910 register int *buffer = (int *) alloca (count * sizeof (int));
911 extern int errno;
912
913 /* Fill start and end extra bytes of buffer with existing memory data. */
914
915 if (remote_debugging)
916 buffer[0] = remote_fetch_word (addr);
917 else
918 buffer[0] = ptrace (1, inferior_pid, addr, 0);
919
920 if (count > 1)
921 {
922 if (remote_debugging)
923 buffer[count - 1]
924 = remote_fetch_word (addr + (count - 1) * sizeof (int));
925 else
926 buffer[count - 1]
927 = ptrace (1, inferior_pid,
928 addr + (count - 1) * sizeof (int), 0);
929 }
930
931 /* Copy data to be written over corresponding part of buffer */
932
933 bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
934
935 /* Write the entire buffer. */
936
937 for (i = 0; i < count; i++, addr += sizeof (int))
938 {
939 errno = 0;
940 if (remote_debugging)
941 remote_store_word (addr, buffer[i]);
942 else
943 ptrace (4, inferior_pid, addr, buffer[i]);
944 if (errno)
945 return errno;
946 }
947
948 return 0;
949 }
950 \f
951 static void
952 try_writing_regs_command ()
953 {
954 register int i;
955 register int value;
956 extern int errno;
957
958 if (inferior_pid == 0)
959 error ("There is no inferior process now.");
960
961 for (i = 0; ; i += 2)
962 {
963 QUIT;
964 errno = 0;
965 value = ptrace (3, inferior_pid, i, 0);
966 ptrace (6, inferior_pid, i, value);
967 if (errno == 0)
968 {
969 printf (" Succeeded with address 0x%x; value 0x%x (%d).\n",
970 i, value, value);
971 }
972 else if ((i & 0377) == 0)
973 printf (" Failed at 0x%x.\n", i);
974 }
975 }
976 \f
977 static
978 initialize ()
979 {
980 add_com ("term-status", class_obscure, term_status_command,
981 "Print info on inferior's saved terminal status.");
982
983 add_com ("try-writing-regs", class_obscure, try_writing_regs_command,
984 "Try writing all locations in inferior's system block.\n\
985 Report which ones can be written.");
986
987 add_com ("kill", class_run, kill_command,
988 "Kill execution of program being debugged.");
989
990 inferior_pid = 0;
991
992 ioctl (0, TIOCGETP, &sg_ours);
993 fcntl (0, F_GETFL, tflags_ours);
994
995 #ifdef TIOCGLTC
996 ioctl (0, TIOCGETC, &tc_ours);
997 ioctl (0, TIOCGLTC, &ltc_ours);
998 ioctl (0, TIOCLGET, &lmode_ours);
999 #endif /* TIOCGLTC */
1000
1001 #ifdef TIOCGPGRP
1002 ioctl (0, TIOCGPGRP, &pgrp_ours);
1003 #endif /* TIOCGPGRP */
1004
1005 terminal_is_ours = 1;
1006 }
1007
1008 END_FILE
This page took 0.074718 seconds and 5 git commands to generate.