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>
32 #include <sys/ioctl.h>
41 #include <sys/ptrace.h>
42 #include <machine/reg.h>
47 /* Nonzero if we are debugging an attached outside process
48 rather than an inferior. */
50 static int attach_flag
;
54 /* Record terminal status separately for debugger and inferior. */
56 static struct sgttyb sg_inferior
;
57 static struct tchars tc_inferior
;
58 static struct ltchars ltc_inferior
;
59 static int lmode_inferior
;
60 static int tflags_inferior
;
61 static int pgrp_inferior
;
63 static struct sgttyb sg_ours
;
64 static struct tchars tc_ours
;
65 static struct ltchars ltc_ours
;
66 static int lmode_ours
;
67 static int tflags_ours
;
70 /* Copy of inferior_io_terminal when inferior was last started. */
71 static char *inferior_thisrun_terminal
;
73 static void terminal_ours_1 ();
75 /* Nonzero if our terminal settings are in effect.
76 Zero if the inferior's settings are in effect. */
77 static int terminal_is_ours
;
79 /* Initialize the terminal settings we record for the inferior,
80 before we actually run the inferior. */
83 terminal_init_inferior ()
88 sg_inferior
= sg_ours
;
89 tc_inferior
= tc_ours
;
90 ltc_inferior
= ltc_ours
;
91 lmode_inferior
= lmode_ours
;
92 tflags_inferior
= tflags_ours
;
93 pgrp_inferior
= inferior_pid
;
98 /* Put the inferior's terminal settings into effect.
99 This is preparation for starting or resuming the inferior. */
104 if (remote_debugging
)
107 if (terminal_is_ours
) /* && inferior_thisrun_terminal == 0) */
109 fcntl (0, F_SETFL
, tflags_inferior
);
110 fcntl (0, F_SETFL
, tflags_inferior
);
111 ioctl (0, TIOCSETN
, &sg_inferior
);
112 ioctl (0, TIOCSETC
, &tc_inferior
);
113 ioctl (0, TIOCSLTC
, <c_inferior
);
114 ioctl (0, TIOCLSET
, &lmode_inferior
);
115 ioctl (0, TIOCSPGRP
, &pgrp_inferior
);
117 terminal_is_ours
= 0;
120 /* Put some of our terminal settings into effect,
121 enough to get proper results from our output,
122 but do not change into or out of RAW mode
123 so that no input is discarded.
125 After doing this, either terminal_ours or terminal_inferior
126 should be called to get back to a normal state of affairs. */
129 terminal_ours_for_output ()
131 if (remote_debugging
)
137 /* Put our terminal settings into effect.
138 First record the inferior's terminal settings
139 so they can be restored properly later. */
144 if (remote_debugging
)
151 terminal_ours_1 (output_only
)
154 /* Ignore this signal since it will happen when we try to set the pgrp. */
157 if (!terminal_is_ours
) /* && inferior_thisrun_terminal == 0) */
159 terminal_is_ours
= 1;
161 osigttou
= signal (SIGTTOU
, SIG_IGN
);
163 ioctl (0, TIOCGPGRP
, &pgrp_inferior
);
164 ioctl (0, TIOCSPGRP
, &pgrp_ours
);
166 signal (SIGTTOU
, osigttou
);
168 tflags_inferior
= fcntl (0, F_GETFL
, 0);
169 ioctl (0, TIOCGETP
, &sg_inferior
);
170 ioctl (0, TIOCGETC
, &tc_inferior
);
171 ioctl (0, TIOCGLTC
, <c_inferior
);
172 ioctl (0, TIOCLGET
, &lmode_inferior
);
175 sg_ours
.sg_flags
&= ~RAW
& ~CBREAK
;
177 sg_ours
.sg_flags
|= (RAW
| CBREAK
) & sg_inferior
.sg_flags
;
179 fcntl (0, F_SETFL
, tflags_ours
);
180 fcntl (0, F_SETFL
, tflags_ours
);
181 ioctl (0, TIOCSETN
, &sg_ours
);
182 ioctl (0, TIOCSETC
, &tc_ours
);
183 ioctl (0, TIOCSLTC
, <c_ours
);
184 ioctl (0, TIOCLSET
, &lmode_ours
);
185 sg_ours
.sg_flags
&= ~RAW
& ~CBREAK
;
189 term_status_command ()
193 if (remote_debugging
)
195 printf ("No terminal status when remote debugging.\n");
199 printf ("Inferior's terminal status (currently saved by GDB):\n");
200 printf ("fcntl flags = 0x%x, lmode = 0x%x,\nsgttyb.sg_flags = 0x%x, owner pid = %d.\n",
201 tflags_inferior
, lmode_inferior
,
202 sg_inferior
.sg_flags
, pgrp_inferior
);
204 for (i
= 0; i
< sizeof (struct tchars
); i
++)
205 printf ("0x%x ", ((char *)&tc_inferior
)[i
]);
207 printf ("ltchars: ");
208 for (i
= 0; i
< sizeof (struct ltchars
); i
++)
209 printf ("0x%x ", ((char *)<c_inferior
)[i
]);
221 /* I think it is better not to do this. Then C-z on the GDB terminal
222 will still stop the program, while C-z on the data terminal
225 /* Disconnect the child process from our controlling terminal. */
226 tty
= open("/dev/tty", O_RDWR
);
229 ioctl(tty
, TIOCNOTTY
, 0);
233 /* Now open the specified new terminal. */
235 tty
= open(ttyname
, O_RDWR
);
245 /* Start an inferior process and returns its pid.
246 ALLARGS is a vector of program-name and args.
247 ENV is the environment vector to pass. */
250 create_inferior (allargs
, env
)
256 extern char *sys_errlist
[];
259 /* exec is said to fail if the executable is open. */
264 perror_with_name ("vfork");
268 /* Run inferior in a separate process group. */
269 setpgrp (getpid (), getpid ());
271 inferior_thisrun_terminal
= inferior_io_terminal
;
272 if (inferior_io_terminal
!= 0)
273 new_tty (inferior_io_terminal
);
275 /* Not needed on Sun, at least, and loses there
276 because it clobbers the superior. */
277 /*??? signal (SIGQUIT, SIG_DFL);
278 signal (SIGINT, SIG_DFL); */
281 execle ("/bin/sh", "sh", "-c", allargs
, 0, env
);
283 fprintf (stderr
, "Cannot exec /bin/sh: %s.\n",
284 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
291 /* Kill the inferior process. Make us have no inferior. */
296 if (remote_debugging
)
298 if (inferior_pid
== 0)
299 error ("The program is not being run.");
300 if (!query ("Kill the inferior process? "))
301 error ("Not confirmed.");
307 if (remote_debugging
)
309 if (inferior_pid
== 0)
311 ptrace (8, inferior_pid
, 0, 0);
320 mark_breakpoints_out ();
322 if (have_core_file_p ())
323 set_current_frame (read_register (FP_REGNUM
));
326 /* Resume execution of the inferior process.
327 If STEP is nonzero, single-step it.
328 If SIGNAL is nonzero, give it that signal. */
331 resume (step
, signal
)
336 if (remote_debugging
)
337 remote_resume (step
, signal
);
340 ptrace (step
? 9 : 7, inferior_pid
, 1, signal
);
342 perror_with_name ("ptrace");
346 #ifdef NEW_SUN_PTRACE
348 /* Start debugging the process whose number is PID. */
354 ptrace (PTRACE_ATTACH
, pid
, 0, 0);
356 perror_with_name ("ptrace");
361 /* Stop debugging the process whose number is PID
362 and continue it with signal number SIGNAL.
363 SIGNAL = 0 means just continue it. */
370 ptrace (PTRACE_DETACH
, inferior_pid
, 1, signal
);
372 perror_with_name ("ptrace");
377 #ifdef NEW_SUN_PTRACE
380 fetch_inferior_registers ()
382 struct regs inferior_registers
;
383 struct fp_status inferior_fp_registers
;
384 extern char registers
[];
386 if (remote_debugging
)
387 remote_fetch_registers (registers
);
390 ptrace (PTRACE_GETREGS
, inferior_pid
, &inferior_registers
);
391 ptrace (PTRACE_GETFPREGS
, inferior_pid
, &inferior_fp_registers
);
393 bcopy (&inferior_registers
, registers
, 16 * 4);
394 bcopy (&inferior_fp_registers
, ®isters
[REGISTER_BYTE (FP0_REGNUM
)],
395 sizeof inferior_fp_registers
.fps_regs
);
396 *(int *)®isters
[REGISTER_BYTE (PS_REGNUM
)] = inferior_registers
.r_ps
;
397 *(int *)®isters
[REGISTER_BYTE (PC_REGNUM
)] = inferior_registers
.r_pc
;
398 bcopy (&inferior_fp_registers
.fps_control
,
399 ®isters
[REGISTER_BYTE (FPC_REGNUM
)],
400 sizeof inferior_fp_registers
- sizeof inferior_fp_registers
.fps_regs
);
404 /* Store our register values back into the inferior.
405 If REGNO is -1, do this for all registers.
406 Otherwise, REGNO specifies which register (so we can save time). */
408 store_inferior_registers (regno
)
411 struct regs inferior_registers
;
412 struct fp_status inferior_fp_registers
;
413 extern char registers
[];
415 if (remote_debugging
)
416 remote_store_registers (registers
);
419 bcopy (registers
, &inferior_registers
, 16 * 4);
420 bcopy (®isters
[REGISTER_BYTE (FP0_REGNUM
)], &inferior_fp_registers
,
421 sizeof inferior_fp_registers
.fps_regs
);
422 inferior_registers
.r_ps
= *(int *)®isters
[REGISTER_BYTE (PS_REGNUM
)];
423 inferior_registers
.r_pc
= *(int *)®isters
[REGISTER_BYTE (PC_REGNUM
)];
424 bcopy (®isters
[REGISTER_BYTE (FPC_REGNUM
)],
425 &inferior_fp_registers
.fps_control
,
426 sizeof inferior_fp_registers
- sizeof inferior_fp_registers
.fps_regs
);
428 ptrace (PTRACE_SETREGS
, inferior_pid
, &inferior_registers
);
429 ptrace (PTRACE_SETFPREGS
, inferior_pid
, &inferior_fp_registers
);
436 fetch_inferior_registers ()
439 register unsigned int regaddr
;
440 char buf
[MAX_REGISTER_RAW_SIZE
];
444 unsigned int offset
= 0;
447 unsigned int offset
= (char *) &u
.u_ar0
- (char *) &u
;
448 offset
= ptrace (3, inferior_pid
, offset
, 0) - KERNEL_U_ADDR
;
451 for (regno
= 0; regno
< NUM_REGS
; regno
++)
453 regaddr
= register_addr (regno
, offset
);
454 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
456 *(int *) &buf
[i
] = ptrace (3, inferior_pid
, regaddr
, 0);
457 regaddr
+= sizeof (int);
459 supply_register (regno
, buf
);
463 /* Store our register values back into the inferior.
464 If REGNO is -1, do this for all registers.
465 Otherwise, REGNO specifies which register (so we can save time). */
467 store_inferior_registers (regno
)
470 register unsigned int regaddr
;
474 unsigned int offset
= 0;
477 unsigned int offset
= (char *) &u
.u_ar0
- (char *) &u
;
478 offset
= ptrace (3, inferior_pid
, offset
, 0) - KERNEL_U_ADDR
;
483 regaddr
= register_addr (regno
, offset
);
485 ptrace (6, inferior_pid
, regaddr
, read_register (regno
));
488 sprintf (buf
, "writing register number %d", regno
);
489 perror_with_name (buf
);
492 else for (regno
= 0; regno
< NUM_REGS
; regno
++)
494 regaddr
= register_addr (regno
, offset
);
496 ptrace (6, inferior_pid
, regaddr
, read_register (regno
));
499 sprintf (buf
, "writing register number %d", regno
);
500 perror_with_name (buf
);
505 #endif /* not NEW_SUN_PTRACE */
507 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
508 in the NEW_SUN_PTRACE case.
509 It ought to be straightforward. But it appears that writing did
510 not write the data that I specified. I cannot understand where
511 it got the data that it actually did write. */
513 /* Copy LEN bytes from inferior's memory starting at MEMADDR
514 to debugger memory starting at MYADDR. */
516 read_inferior_memory (memaddr
, myaddr
, len
)
522 /* Round starting address down to longword boundary. */
523 register CORE_ADDR addr
= memaddr
& - sizeof (int);
524 /* Round ending address up; get number of longwords that makes. */
526 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
527 /* Allocate buffer of that many longwords. */
528 register int *buffer
= (int *) alloca (count
* sizeof (int));
530 /* Read all the longwords */
531 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
533 if (remote_debugging
)
534 buffer
[i
] = remote_fetch_word (addr
);
536 buffer
[i
] = ptrace (1, inferior_pid
, addr
, 0);
539 /* Copy appropriate bytes out of the buffer. */
540 bcopy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
543 /* Copy LEN bytes of data from debugger memory at MYADDR
544 to inferior's memory at MEMADDR.
545 On failure (cannot write the inferior)
546 returns the value of errno. */
549 write_inferior_memory (memaddr
, myaddr
, len
)
555 /* Round starting address down to longword boundary. */
556 register CORE_ADDR addr
= memaddr
& - sizeof (int);
557 /* Round ending address up; get number of longwords that makes. */
559 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
560 /* Allocate buffer of that many longwords. */
561 register int *buffer
= (int *) alloca (count
* sizeof (int));
564 /* Fill start and end extra bytes of buffer with existing memory data. */
566 if (remote_debugging
)
567 buffer
[0] = remote_fetch_word (addr
);
569 buffer
[0] = ptrace (1, inferior_pid
, addr
, 0);
573 if (remote_debugging
)
575 = remote_fetch_word (addr
+ (count
- 1) * sizeof (int));
578 = ptrace (1, inferior_pid
,
579 addr
+ (count
- 1) * sizeof (int), 0);
582 /* Copy data to be written over corresponding part of buffer */
584 bcopy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
586 /* Write the entire buffer. */
588 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
591 if (remote_debugging
)
592 remote_store_word (addr
, buffer
[i
]);
594 ptrace (4, inferior_pid
, addr
, buffer
[i
]);
603 try_writing_regs_command ()
609 if (inferior_pid
== 0)
610 error ("There is no inferior process now.");
612 for (i
= 0; ; i
+= 2)
616 value
= ptrace (3, inferior_pid
, i
, 0);
617 ptrace (6, inferior_pid
, i
, value
);
620 printf (" Succeeded with address 0x%x; value 0x%x (%d).\n",
623 else if ((i
& 0377) == 0)
624 printf (" Failed at 0x%x.\n", i
);
631 add_com ("term-status", class_obscure
, term_status_command
,
632 "Print info on inferior's saved terminal status.");
634 add_com ("try-writing-regs", class_obscure
, try_writing_regs_command
,
635 "Try writing all locations in inferior's system block.\n\
636 Report which ones can be written.");
638 add_com ("kill", class_run
, kill_command
,
639 "Kill execution of program being debugged.");
643 ioctl (0, TIOCGETP
, &sg_ours
);
644 ioctl (0, TIOCGETC
, &tc_ours
);
645 ioctl (0, TIOCGLTC
, <c_ours
);
646 ioctl (0, TIOCLGET
, &lmode_ours
);
647 fcntl (0, F_GETFL
, tflags_ours
);
648 ioctl (0, TIOCGPGRP
, &pgrp_ours
);
650 terminal_is_ours
= 1;