1 /* Low level interface to ptrace, for GDB when running under Unix.
2 Copyright (C) 1986, 1987 Free Software Foundation, Inc.
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.
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.
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!
22 #include "initialize.h"
28 #include <sys/param.h>
34 #include <sys/ioctl.h>
39 #include <sys/ptrace.h>
40 #define PTRACE_ATTACH PT_ATTACH
41 #define PTRACE_DETACH PT_FREEPROC
45 #include <sys/ptrace.h>
46 #include <machine/reg.h>
50 #include <sys/ptrace.h>
58 #define TIOCGETP TCGETA
60 #define TIOCSETN TCSETA
62 #define TIOCSETP TCSETAF
63 #define TERMINAL struct termio
66 #define TERMINAL struct sgttyb
71 /* Nonzero if we are debugging an attached outside process
72 rather than an inferior. */
74 static int attach_flag;
78 /* Record terminal status separately for debugger and inferior. */
80 static TERMINAL sg_inferior;
81 static TERMINAL sg_ours;
82 static int tflags_inferior;
83 static int tflags_ours;
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;
95 static int pgrp_inferior;
98 static int (*sigint_ours) ();
99 static int (*sigquit_ours) ();
100 #endif /* TIOCGPGRP */
102 /* Copy of inferior_io_terminal when inferior was last started. */
103 static char *inferior_thisrun_terminal;
105 static void terminal_ours_1 ();
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;
111 /* Initialize the terminal settings we record for the inferior,
112 before we actually run the inferior. */
115 terminal_init_inferior ()
117 if (remote_debugging)
120 sg_inferior = sg_ours;
121 tflags_inferior = tflags_ours;
124 tc_inferior = tc_ours;
125 ltc_inferior = ltc_ours;
126 lmode_inferior = lmode_ours;
127 #endif /* TIOCGLTC */
130 pgrp_inferior = inferior_pid;
131 #endif /* TIOCGPGRP */
133 terminal_is_ours = 1;
136 /* Put the inferior's terminal settings into effect.
137 This is preparation for starting or resuming the inferior. */
142 if (remote_debugging)
145 if (terminal_is_ours) /* && inferior_thisrun_terminal == 0) */
147 fcntl (0, F_SETFL, tflags_inferior);
148 fcntl (0, F_SETFL, tflags_inferior);
149 ioctl (0, TIOCSETN, &sg_inferior);
152 ioctl (0, TIOCSETC, &tc_inferior);
153 ioctl (0, TIOCSLTC, <c_inferior);
154 ioctl (0, TIOCLSET, &lmode_inferior);
155 #endif /* TIOCGLTC */
158 ioctl (0, TIOCSPGRP, &pgrp_inferior);
160 sigint_ours = (signal (SIGINT, SIG_IGN));
161 sigquit_ours = (signal (SIGQUIT, SIG_IGN));
162 #endif /* TIOCGPGRP */
164 terminal_is_ours = 0;
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.
172 After doing this, either terminal_ours or terminal_inferior
173 should be called to get back to a normal state of affairs. */
176 terminal_ours_for_output ()
178 if (remote_debugging)
184 /* Put our terminal settings into effect.
185 First record the inferior's terminal settings
186 so they can be restored properly later. */
191 if (remote_debugging)
198 terminal_ours_1 (output_only)
202 /* Ignore this signal since it will happen when we try to set the pgrp. */
204 #endif /* TIOCGPGRP */
206 if (!terminal_is_ours) /* && inferior_thisrun_terminal == 0) */
208 terminal_is_ours = 1;
211 osigttou = signal (SIGTTOU, SIG_IGN);
213 ioctl (0, TIOCGPGRP, &pgrp_inferior);
214 ioctl (0, TIOCSPGRP, &pgrp_ours);
216 signal (SIGTTOU, osigttou);
218 signal (SIGINT, sigint_ours);
219 signal (SIGQUIT, sigquit_ours);
220 #endif /* TIOCGPGRP */
222 tflags_inferior = fcntl (0, F_GETFL, 0);
223 ioctl (0, TIOCGETP, &sg_inferior);
226 ioctl (0, TIOCGETC, &tc_inferior);
227 ioctl (0, TIOCGLTC, <c_inferior);
228 ioctl (0, TIOCLGET, &lmode_inferior);
229 #endif /* TIOCGLTC */
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;
239 sg_ours.sg_flags |= (RAW | CBREAK) & sg_inferior.sg_flags;
240 #endif /* not HAVE_TERMIO */
242 fcntl (0, F_SETFL, tflags_ours);
243 fcntl (0, F_SETFL, tflags_ours);
244 ioctl (0, TIOCSETN, &sg_ours);
247 ioctl (0, TIOCSETC, &tc_ours);
248 ioctl (0, TIOCSLTC, <c_ours);
249 ioctl (0, TIOCLSET, &lmode_ours);
250 #endif /* TIOCGLTC */
254 sg_ours.c_lflag |= ICANON;
255 #else /* not HAVE_TERMIO */
256 sg_ours.sg_flags &= ~RAW & ~CBREAK;
257 #endif /* not HAVE_TERMIO */
261 term_status_command ()
265 if (remote_debugging)
267 printf ("No terminal status when remote debugging.\n");
271 printf ("Inferior's terminal status (currently saved by GDB):\n");
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);
280 for (i = 0; (i < NCC); i += 1)
281 printf ("0x%x ", sg_inferior.c_cc[i]);
284 #else /* not HAVE_TERMIO */
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);
290 for (i = 0; i < sizeof (struct tchars); i++)
291 printf ("0x%x ", ((char *)&tc_inferior)[i]);
293 printf ("ltchars: ");
294 for (i = 0; i < sizeof (struct ltchars); i++)
295 printf ("0x%x ", ((char *)<c_inferior)[i]);
297 #endif /* not HAVE_TERMIO */
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
312 /* Disconnect the child process from our controlling terminal. */
313 tty = open("/dev/tty", O_RDWR);
316 ioctl(tty, TIOCNOTTY, 0);
320 /* Now open the specified new terminal. */
322 tty = open(ttyname, O_RDWR);
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. */
337 #define SHELL_FILE "/bin/sh"
341 create_inferior (allargs, env)
348 extern char *sys_errlist[];
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);
358 shell_command = allargs;
361 /* exec is said to fail if the executable is open. */
366 perror_with_name ("vfork");
373 /* Run inferior in a separate process group. */
374 setpgrp (getpid (), getpid ());
375 #endif /* TIOCGPGRP */
377 inferior_thisrun_terminal = inferior_io_terminal;
378 if (inferior_io_terminal != 0)
379 new_tty (inferior_io_terminal);
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); */
390 args[2] = shell_command;
393 execve (SHELL_FILE, args, env);
395 fprintf (stderr, "Cannot exec %s: %s.\n", SHELL_FILE,
396 errno < sys_nerr ? sys_errlist[errno] : "unknown error");
403 /* Kill the inferior process. Make us have no inferior. */
408 if (remote_debugging)
410 if (inferior_pid == 0)
411 error ("The program is not being run.");
412 if (!query ("Kill the inferior process? "))
413 error ("Not confirmed.");
419 if (remote_debugging)
421 if (inferior_pid == 0)
423 ptrace (8, inferior_pid, 0, 0);
428 /* This is used when GDB is exiting. It gives less chance of error.*/
430 kill_inferior_fast ()
432 if (remote_debugging)
434 if (inferior_pid == 0)
436 ptrace (8, inferior_pid, 0, 0);
444 mark_breakpoints_out ();
446 if (have_core_file_p ())
447 set_current_frame (read_register (FP_REGNUM));
450 /* Resume execution of the inferior process.
451 If STEP is nonzero, single-step it.
452 If SIGNAL is nonzero, give it that signal. */
455 resume (step, signal)
460 if (remote_debugging)
461 remote_resume (step, signal);
464 ptrace (step ? 9 : 7, inferior_pid, 1, signal);
466 perror_with_name ("ptrace");
472 /* Start debugging the process whose number is PID. */
478 ptrace (PTRACE_ATTACH, pid, 0, 0);
480 perror_with_name ("ptrace");
485 /* Stop debugging the process whose number is PID
486 and continue it with signal number SIGNAL.
487 SIGNAL = 0 means just continue it. */
494 ptrace (PTRACE_DETACH, inferior_pid, 1, signal);
496 perror_with_name ("ptrace");
499 #endif /* ATTACH_DETACH */
501 #ifdef NEW_SUN_PTRACE
504 fetch_inferior_registers ()
506 struct regs inferior_registers;
507 struct fp_status inferior_fp_registers;
508 extern char registers[];
510 if (remote_debugging)
511 remote_fetch_registers (registers);
514 ptrace (PTRACE_GETREGS, inferior_pid, &inferior_registers);
515 ptrace (PTRACE_GETFPREGS, inferior_pid, &inferior_fp_registers);
517 bcopy (&inferior_registers, registers, 16 * 4);
518 bcopy (&inferior_fp_registers, ®isters[REGISTER_BYTE (FP0_REGNUM)],
519 sizeof inferior_fp_registers.fps_regs);
520 *(int *)®isters[REGISTER_BYTE (PS_REGNUM)] = inferior_registers.r_ps;
521 *(int *)®isters[REGISTER_BYTE (PC_REGNUM)] = inferior_registers.r_pc;
522 bcopy (&inferior_fp_registers.fps_control,
523 ®isters[REGISTER_BYTE (FPC_REGNUM)],
524 sizeof inferior_fp_registers - sizeof inferior_fp_registers.fps_regs);
528 /* Store our register values back into the inferior.
529 If REGNO is -1, do this for all registers.
530 Otherwise, REGNO specifies which register (so we can save time). */
532 store_inferior_registers (regno)
535 struct regs inferior_registers;
536 struct fp_status inferior_fp_registers;
537 extern char registers[];
539 if (remote_debugging)
540 remote_store_registers (registers);
543 bcopy (registers, &inferior_registers, 16 * 4);
544 bcopy (®isters[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers,
545 sizeof inferior_fp_registers.fps_regs);
546 inferior_registers.r_ps = *(int *)®isters[REGISTER_BYTE (PS_REGNUM)];
547 inferior_registers.r_pc = *(int *)®isters[REGISTER_BYTE (PC_REGNUM)];
548 bcopy (®isters[REGISTER_BYTE (FPC_REGNUM)],
549 &inferior_fp_registers.fps_control,
550 sizeof inferior_fp_registers - sizeof inferior_fp_registers.fps_regs);
552 ptrace (PTRACE_SETREGS, inferior_pid, &inferior_registers);
553 ptrace (PTRACE_SETFPREGS, inferior_pid, &inferior_fp_registers);
560 #define FP_REGISTER_ADDR_DIFF(u, regno) \
561 (((char *) (FP_REGISTER_ADDR (u, regno))) - ((char *) &(u)))
563 #define INFERIOR_AR0(u) \
565 (PT_RUAREA, inferior_pid, ((char *) &u.u_ar0 - (char *) &u), 0)) \
569 fetch_inferior_register (regno, regaddr)
571 register unsigned int regaddr;
573 #ifndef HPUX_VERSION_5
574 if (regno == PS_REGNUM)
576 union { int i; short s[2]; } ps_val;
579 ps_val.i = (ptrace (PT_RUAREA, inferior_pid, regaddr, 0));
580 regval = ps_val.s[0];
581 supply_register (regno, ®val);
584 #endif /* not HPUX_VERSION_5 */
586 char buf[MAX_REGISTER_RAW_SIZE];
589 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
591 *(int *) &buf[i] = ptrace (PT_RUAREA, inferior_pid, regaddr, 0);
592 regaddr += sizeof (int);
594 supply_register (regno, buf);
600 store_inferior_register_1 (regno, regaddr, value)
602 unsigned int regaddr;
606 ptrace (PT_WUAREA, inferior_pid, regaddr, value);
608 /* HP-UX randomly sets errno to non-zero for regno == 25.
609 However, the value is correctly written, so ignore errno. */
614 sprintf (string_buf, "writing register number %d", regno);
615 perror_with_name (string_buf);
622 store_inferior_register (regno, regaddr)
624 register unsigned int regaddr;
626 #ifndef HPUX_VERSION_5
627 if (regno == PS_REGNUM)
629 union { int i; short s[2]; } ps_val;
631 ps_val.i = (ptrace (PT_RUAREA, inferior_pid, regaddr, 0));
632 ps_val.s[0] = (read_register (regno));
633 store_inferior_register_1 (regno, regaddr, ps_val.i);
636 #endif /* not HPUX_VERSION_5 */
638 char buf[MAX_REGISTER_RAW_SIZE];
640 extern char registers[];
642 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
644 store_inferior_register_1
646 (*(int *) ®isters[(REGISTER_BYTE (regno)) + i]));
647 regaddr += sizeof (int);
654 fetch_inferior_registers ()
658 register unsigned int ar0_offset;
660 ar0_offset = (INFERIOR_AR0 (u));
661 for (regno = 0; (regno < FP0_REGNUM); regno++)
662 fetch_inferior_register (regno, (REGISTER_ADDR (ar0_offset, regno)));
663 for (; (regno < NUM_REGS); regno++)
664 fetch_inferior_register (regno, (FP_REGISTER_ADDR_DIFF (u, regno)));
667 /* Store our register values back into the inferior.
668 If REGNO is -1, do this for all registers.
669 Otherwise, REGNO specifies which register (so we can save time). */
671 store_inferior_registers (regno)
675 register unsigned int ar0_offset;
677 if (regno >= FP0_REGNUM)
679 store_inferior_register (regno, (FP_REGISTER_ADDR_DIFF (u, regno)));
683 ar0_offset = (INFERIOR_AR0 (u));
686 store_inferior_register (regno, (REGISTER_ADDR (ar0_offset, regno)));
690 for (regno = 0; (regno < FP0_REGNUM); regno++)
691 store_inferior_register (regno, (REGISTER_ADDR (ar0_offset, regno)));
692 for (; (regno < NUM_REGS); regno++)
693 store_inferior_register (regno, (FP_REGISTER_ADDR_DIFF (u, regno)));
697 #else /* not HP9K320 */
700 fetch_inferior_registers ()
703 register unsigned int regaddr;
704 char buf[MAX_REGISTER_RAW_SIZE];
708 unsigned int offset = 0;
711 unsigned int offset = (char *) &u.u_ar0 - (char *) &u;
712 offset = ptrace (3, inferior_pid, offset, 0) - KERNEL_U_ADDR;
715 for (regno = 0; regno < NUM_REGS; regno++)
717 regaddr = register_addr (regno, offset);
718 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
720 *(int *) &buf[i] = ptrace (3, inferior_pid, regaddr, 0);
721 regaddr += sizeof (int);
723 supply_register (regno, buf);
727 /* Store our register values back into the inferior.
728 If REGNO is -1, do this for all registers.
729 Otherwise, REGNO specifies which register (so we can save time). */
731 store_inferior_registers (regno)
734 register unsigned int regaddr;
736 extern char registers[];
740 unsigned int offset = 0;
743 unsigned int offset = (char *) &u.u_ar0 - (char *) &u;
744 offset = ptrace (3, inferior_pid, offset, 0) - KERNEL_U_ADDR;
749 regaddr = register_addr (regno, offset);
750 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
753 ptrace (6, inferior_pid, regaddr,
754 *(int *) ®isters[REGISTER_BYTE (regno) + i]);
757 sprintf (buf, "writing register number %d(%d)", regno, i);
758 perror_with_name (buf);
760 regaddr += sizeof(int);
763 else for (regno = 0; regno < NUM_REGS; regno++)
765 regaddr = register_addr (regno, offset);
766 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
769 ptrace (6, inferior_pid, regaddr,
770 *(int *) ®isters[REGISTER_BYTE (regno) + i]);
773 sprintf (buf, "writing register number %d(%d)", regno, i);
774 perror_with_name (buf);
776 regaddr += sizeof(int);
781 #endif /* not HP9K320 */
782 #endif /* not NEW_SUN_PTRACE */
784 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
785 in the NEW_SUN_PTRACE case.
786 It ought to be straightforward. But it appears that writing did
787 not write the data that I specified. I cannot understand where
788 it got the data that it actually did write. */
790 /* Copy LEN bytes from inferior's memory starting at MEMADDR
791 to debugger memory starting at MYADDR. */
793 read_inferior_memory (memaddr, myaddr, len)
799 /* Round starting address down to longword boundary. */
800 register CORE_ADDR addr = memaddr & - sizeof (int);
801 /* Round ending address up; get number of longwords that makes. */
803 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
804 /* Allocate buffer of that many longwords. */
805 register int *buffer = (int *) alloca (count * sizeof (int));
807 /* Read all the longwords */
808 for (i = 0; i < count; i++, addr += sizeof (int))
810 if (remote_debugging)
811 buffer[i] = remote_fetch_word (addr);
813 buffer[i] = ptrace (1, inferior_pid, addr, 0);
816 /* Copy appropriate bytes out of the buffer. */
817 bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
820 /* Copy LEN bytes of data from debugger memory at MYADDR
821 to inferior's memory at MEMADDR.
822 On failure (cannot write the inferior)
823 returns the value of errno. */
826 write_inferior_memory (memaddr, myaddr, len)
832 /* Round starting address down to longword boundary. */
833 register CORE_ADDR addr = memaddr & - sizeof (int);
834 /* Round ending address up; get number of longwords that makes. */
836 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
837 /* Allocate buffer of that many longwords. */
838 register int *buffer = (int *) alloca (count * sizeof (int));
841 /* Fill start and end extra bytes of buffer with existing memory data. */
843 if (remote_debugging)
844 buffer[0] = remote_fetch_word (addr);
846 buffer[0] = ptrace (1, inferior_pid, addr, 0);
850 if (remote_debugging)
852 = remote_fetch_word (addr + (count - 1) * sizeof (int));
855 = ptrace (1, inferior_pid,
856 addr + (count - 1) * sizeof (int), 0);
859 /* Copy data to be written over corresponding part of buffer */
861 bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
863 /* Write the entire buffer. */
865 for (i = 0; i < count; i++, addr += sizeof (int))
868 if (remote_debugging)
869 remote_store_word (addr, buffer[i]);
871 ptrace (4, inferior_pid, addr, buffer[i]);
880 try_writing_regs_command ()
886 if (inferior_pid == 0)
887 error ("There is no inferior process now.");
889 for (i = 0; ; i += 2)
893 value = ptrace (3, inferior_pid, i, 0);
894 ptrace (6, inferior_pid, i, value);
897 printf (" Succeeded with address 0x%x; value 0x%x (%d).\n",
900 else if ((i & 0377) == 0)
901 printf (" Failed at 0x%x.\n", i);
908 add_com ("term-status", class_obscure, term_status_command,
909 "Print info on inferior's saved terminal status.");
911 add_com ("try-writing-regs", class_obscure, try_writing_regs_command,
912 "Try writing all locations in inferior's system block.\n\
913 Report which ones can be written.");
915 add_com ("kill", class_run, kill_command,
916 "Kill execution of program being debugged.");
920 ioctl (0, TIOCGETP, &sg_ours);
921 fcntl (0, F_GETFL, tflags_ours);
924 ioctl (0, TIOCGETC, &tc_ours);
925 ioctl (0, TIOCGLTC, <c_ours);
926 ioctl (0, TIOCLGET, &lmode_ours);
927 #endif /* TIOCGLTC */
930 ioctl (0, TIOCGPGRP, &pgrp_ours);
931 #endif /* TIOCGPGRP */
933 terminal_is_ours = 1;