b88352ccda715a5dd852da1a9f7785d96ef192d0
[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 #include <sys/user.h>
31 #include <signal.h>
32 #include <sys/ioctl.h>
33 #include <sgtty.h>
34 #include <fcntl.h>
35
36 #ifdef UMAX_PTRACE
37 #include <a.out.h>
38 #endif
39
40 #ifdef NEW_SUN_PTRACE
41 #include <sys/ptrace.h>
42 #include <machine/reg.h>
43 #endif
44
45 extern int errno;
46
47 /* Nonzero if we are debugging an attached outside process
48 rather than an inferior. */
49
50 static int attach_flag;
51
52 START_FILE
53 \f
54 /* Record terminal status separately for debugger and inferior. */
55
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;
62
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;
68 static int pgrp_ours;
69
70 /* Copy of inferior_io_terminal when inferior was last started. */
71 static char *inferior_thisrun_terminal;
72
73 static void terminal_ours_1 ();
74
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;
78
79 /* Initialize the terminal settings we record for the inferior,
80 before we actually run the inferior. */
81
82 void
83 terminal_init_inferior ()
84 {
85 if (remote_debugging)
86 return;
87
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;
94
95 terminal_is_ours = 1;
96 }
97
98 /* Put the inferior's terminal settings into effect.
99 This is preparation for starting or resuming the inferior. */
100
101 void
102 terminal_inferior ()
103 {
104 if (remote_debugging)
105 return;
106
107 if (terminal_is_ours) /* && inferior_thisrun_terminal == 0) */
108 {
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, &ltc_inferior);
114 ioctl (0, TIOCLSET, &lmode_inferior);
115 ioctl (0, TIOCSPGRP, &pgrp_inferior);
116 }
117 terminal_is_ours = 0;
118 }
119
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.
124
125 After doing this, either terminal_ours or terminal_inferior
126 should be called to get back to a normal state of affairs. */
127
128 void
129 terminal_ours_for_output ()
130 {
131 if (remote_debugging)
132 return;
133
134 terminal_ours_1 (1);
135 }
136
137 /* Put our terminal settings into effect.
138 First record the inferior's terminal settings
139 so they can be restored properly later. */
140
141 void
142 terminal_ours ()
143 {
144 if (remote_debugging)
145 return;
146
147 terminal_ours_1 (0);
148 }
149
150 static void
151 terminal_ours_1 (output_only)
152 int output_only;
153 {
154 /* Ignore this signal since it will happen when we try to set the pgrp. */
155 int (*osigttou) ();
156
157 if (!terminal_is_ours) /* && inferior_thisrun_terminal == 0) */
158 {
159 terminal_is_ours = 1;
160
161 osigttou = signal (SIGTTOU, SIG_IGN);
162
163 ioctl (0, TIOCGPGRP, &pgrp_inferior);
164 ioctl (0, TIOCSPGRP, &pgrp_ours);
165
166 signal (SIGTTOU, osigttou);
167
168 tflags_inferior = fcntl (0, F_GETFL, 0);
169 ioctl (0, TIOCGETP, &sg_inferior);
170 ioctl (0, TIOCGETC, &tc_inferior);
171 ioctl (0, TIOCGLTC, &ltc_inferior);
172 ioctl (0, TIOCLGET, &lmode_inferior);
173 }
174
175 sg_ours.sg_flags &= ~RAW & ~CBREAK;
176 if (output_only)
177 sg_ours.sg_flags |= (RAW | CBREAK) & sg_inferior.sg_flags;
178
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, &ltc_ours);
184 ioctl (0, TIOCLSET, &lmode_ours);
185 sg_ours.sg_flags &= ~RAW & ~CBREAK;
186 }
187
188 static void
189 term_status_command ()
190 {
191 register int i;
192
193 if (remote_debugging)
194 {
195 printf ("No terminal status when remote debugging.\n");
196 return;
197 }
198
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);
203 printf ("tchars: ");
204 for (i = 0; i < sizeof (struct tchars); i++)
205 printf ("0x%x ", ((char *)&tc_inferior)[i]);
206 printf ("\n");
207 printf ("ltchars: ");
208 for (i = 0; i < sizeof (struct ltchars); i++)
209 printf ("0x%x ", ((char *)&ltc_inferior)[i]);
210 printf ("\n");
211 }
212 \f
213 static void
214 new_tty (ttyname)
215 char *ttyname;
216 {
217 register int tty;
218 register int fd;
219
220 #if 0
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
223 will be input. */
224
225 /* Disconnect the child process from our controlling terminal. */
226 tty = open("/dev/tty", O_RDWR);
227 if (tty > 0)
228 {
229 ioctl(tty, TIOCNOTTY, 0);
230 close(tty);
231 }
232 #endif
233 /* Now open the specified new terminal. */
234
235 tty = open(ttyname, O_RDWR);
236 if (tty == -1)
237 _exit(1);
238
239 dup2(tty, 0);
240 dup2(tty, 1);
241 dup2(tty, 2);
242 close(tty);
243 }
244 \f
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. */
248
249 int
250 create_inferior (allargs, env)
251 char **allargs;
252 char **env;
253 {
254 int pid;
255 extern int sys_nerr;
256 extern char *sys_errlist[];
257 extern int errno;
258
259 /* exec is said to fail if the executable is open. */
260 close_exec_file ();
261
262 pid = vfork ();
263 if (pid < 0)
264 perror_with_name ("vfork");
265
266 if (pid == 0)
267 {
268 /* Run inferior in a separate process group. */
269 setpgrp (getpid (), getpid ());
270
271 inferior_thisrun_terminal = inferior_io_terminal;
272 if (inferior_io_terminal != 0)
273 new_tty (inferior_io_terminal);
274
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); */
279
280 ptrace (0);
281 execle ("/bin/sh", "sh", "-c", allargs, 0, env);
282
283 fprintf (stderr, "Cannot exec /bin/sh: %s.\n",
284 errno < sys_nerr ? sys_errlist[errno] : "unknown error");
285 fflush (stderr);
286 _exit (0177);
287 }
288 return pid;
289 }
290
291 /* Kill the inferior process. Make us have no inferior. */
292
293 static void
294 kill_command ()
295 {
296 if (remote_debugging)
297 return;
298 if (inferior_pid == 0)
299 error ("The program is not being run.");
300 if (!query ("Kill the inferior process? "))
301 error ("Not confirmed.");
302 kill_inferior ();
303 }
304
305 kill_inferior ()
306 {
307 if (remote_debugging)
308 return;
309 if (inferior_pid == 0)
310 return;
311 ptrace (8, inferior_pid, 0, 0);
312 wait (0);
313 inferior_died ();
314 }
315
316 inferior_died ()
317 {
318 inferior_pid = 0;
319 attach_flag = 0;
320 mark_breakpoints_out ();
321 reopen_exec_file ();
322 if (have_core_file_p ())
323 set_current_frame (read_register (FP_REGNUM));
324 }
325
326 /* Resume execution of the inferior process.
327 If STEP is nonzero, single-step it.
328 If SIGNAL is nonzero, give it that signal. */
329
330 void
331 resume (step, signal)
332 int step;
333 int signal;
334 {
335 errno = 0;
336 if (remote_debugging)
337 remote_resume (step, signal);
338 else
339 {
340 ptrace (step ? 9 : 7, inferior_pid, 1, signal);
341 if (errno)
342 perror_with_name ("ptrace");
343 }
344 }
345 \f
346 #ifdef NEW_SUN_PTRACE
347
348 /* Start debugging the process whose number is PID. */
349
350 attach (pid)
351 int pid;
352 {
353 errno = 0;
354 ptrace (PTRACE_ATTACH, pid, 0, 0);
355 if (errno)
356 perror_with_name ("ptrace");
357 attach_flag = 1;
358 return pid;
359 }
360
361 /* Stop debugging the process whose number is PID
362 and continue it with signal number SIGNAL.
363 SIGNAL = 0 means just continue it. */
364
365 void
366 detach (signal)
367 int signal;
368 {
369 errno = 0;
370 ptrace (PTRACE_DETACH, inferior_pid, 1, signal);
371 if (errno)
372 perror_with_name ("ptrace");
373 attach_flag = 0;
374 }
375 #endif
376 \f
377 #ifdef NEW_SUN_PTRACE
378
379 void
380 fetch_inferior_registers ()
381 {
382 struct regs inferior_registers;
383 struct fp_status inferior_fp_registers;
384 extern char registers[];
385
386 if (remote_debugging)
387 remote_fetch_registers (registers);
388 else
389 {
390 ptrace (PTRACE_GETREGS, inferior_pid, &inferior_registers);
391 ptrace (PTRACE_GETFPREGS, inferior_pid, &inferior_fp_registers);
392
393 bcopy (&inferior_registers, registers, 16 * 4);
394 bcopy (&inferior_fp_registers, &registers[REGISTER_BYTE (FP0_REGNUM)],
395 sizeof inferior_fp_registers.fps_regs);
396 *(int *)&registers[REGISTER_BYTE (PS_REGNUM)] = inferior_registers.r_ps;
397 *(int *)&registers[REGISTER_BYTE (PC_REGNUM)] = inferior_registers.r_pc;
398 bcopy (&inferior_fp_registers.fps_control,
399 &registers[REGISTER_BYTE (FPC_REGNUM)],
400 sizeof inferior_fp_registers - sizeof inferior_fp_registers.fps_regs);
401 }
402 }
403
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). */
407
408 store_inferior_registers (regno)
409 int regno;
410 {
411 struct regs inferior_registers;
412 struct fp_status inferior_fp_registers;
413 extern char registers[];
414
415 if (remote_debugging)
416 remote_store_registers (registers);
417 else
418 {
419 bcopy (registers, &inferior_registers, 16 * 4);
420 bcopy (&registers[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers,
421 sizeof inferior_fp_registers.fps_regs);
422 inferior_registers.r_ps = *(int *)&registers[REGISTER_BYTE (PS_REGNUM)];
423 inferior_registers.r_pc = *(int *)&registers[REGISTER_BYTE (PC_REGNUM)];
424 bcopy (&registers[REGISTER_BYTE (FPC_REGNUM)],
425 &inferior_fp_registers.fps_control,
426 sizeof inferior_fp_registers - sizeof inferior_fp_registers.fps_regs);
427
428 ptrace (PTRACE_SETREGS, inferior_pid, &inferior_registers);
429 ptrace (PTRACE_SETFPREGS, inferior_pid, &inferior_fp_registers);
430 }
431 }
432
433 #else
434
435 void
436 fetch_inferior_registers ()
437 {
438 register int regno;
439 register unsigned int regaddr;
440 char buf[MAX_REGISTER_RAW_SIZE];
441 register int i;
442
443 #ifdef UMAX_PTRACE
444 unsigned int offset = 0;
445 #else
446 struct user u;
447 unsigned int offset = (char *) &u.u_ar0 - (char *) &u;
448 offset = ptrace (3, inferior_pid, offset, 0) - KERNEL_U_ADDR;
449 #endif
450
451 for (regno = 0; regno < NUM_REGS; regno++)
452 {
453 regaddr = register_addr (regno, offset);
454 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
455 {
456 *(int *) &buf[i] = ptrace (3, inferior_pid, regaddr, 0);
457 regaddr += sizeof (int);
458 }
459 supply_register (regno, buf);
460 }
461 }
462
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). */
466
467 store_inferior_registers (regno)
468 int regno;
469 {
470 register unsigned int regaddr;
471 char buf[80];
472
473 #ifdef UMAX_PTRACE
474 unsigned int offset = 0;
475 #else
476 struct user u;
477 unsigned int offset = (char *) &u.u_ar0 - (char *) &u;
478 offset = ptrace (3, inferior_pid, offset, 0) - KERNEL_U_ADDR;
479 #endif
480
481 if (regno >= 0)
482 {
483 regaddr = register_addr (regno, offset);
484 errno = 0;
485 ptrace (6, inferior_pid, regaddr, read_register (regno));
486 if (errno != 0)
487 {
488 sprintf (buf, "writing register number %d", regno);
489 perror_with_name (buf);
490 }
491 }
492 else for (regno = 0; regno < NUM_REGS; regno++)
493 {
494 regaddr = register_addr (regno, offset);
495 errno = 0;
496 ptrace (6, inferior_pid, regaddr, read_register (regno));
497 if (errno != 0)
498 {
499 sprintf (buf, "writing register number %d", regno);
500 perror_with_name (buf);
501 }
502 }
503 }
504
505 #endif /* not NEW_SUN_PTRACE */
506 \f
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. */
512
513 /* Copy LEN bytes from inferior's memory starting at MEMADDR
514 to debugger memory starting at MYADDR. */
515
516 read_inferior_memory (memaddr, myaddr, len)
517 CORE_ADDR memaddr;
518 char *myaddr;
519 int len;
520 {
521 register int i;
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. */
525 register int count
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));
529
530 /* Read all the longwords */
531 for (i = 0; i < count; i++, addr += sizeof (int))
532 {
533 if (remote_debugging)
534 buffer[i] = remote_fetch_word (addr);
535 else
536 buffer[i] = ptrace (1, inferior_pid, addr, 0);
537 }
538
539 /* Copy appropriate bytes out of the buffer. */
540 bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
541 }
542
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. */
547
548 int
549 write_inferior_memory (memaddr, myaddr, len)
550 CORE_ADDR memaddr;
551 char *myaddr;
552 int len;
553 {
554 register int i;
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. */
558 register int count
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));
562 extern int errno;
563
564 /* Fill start and end extra bytes of buffer with existing memory data. */
565
566 if (remote_debugging)
567 buffer[0] = remote_fetch_word (addr);
568 else
569 buffer[0] = ptrace (1, inferior_pid, addr, 0);
570
571 if (count > 1)
572 {
573 if (remote_debugging)
574 buffer[count - 1]
575 = remote_fetch_word (addr + (count - 1) * sizeof (int));
576 else
577 buffer[count - 1]
578 = ptrace (1, inferior_pid,
579 addr + (count - 1) * sizeof (int), 0);
580 }
581
582 /* Copy data to be written over corresponding part of buffer */
583
584 bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
585
586 /* Write the entire buffer. */
587
588 for (i = 0; i < count; i++, addr += sizeof (int))
589 {
590 errno = 0;
591 if (remote_debugging)
592 remote_store_word (addr, buffer[i]);
593 else
594 ptrace (4, inferior_pid, addr, buffer[i]);
595 if (errno)
596 return errno;
597 }
598
599 return 0;
600 }
601 \f
602 static void
603 try_writing_regs_command ()
604 {
605 register int i;
606 register int value;
607 extern int errno;
608
609 if (inferior_pid == 0)
610 error ("There is no inferior process now.");
611
612 for (i = 0; ; i += 2)
613 {
614 QUIT;
615 errno = 0;
616 value = ptrace (3, inferior_pid, i, 0);
617 ptrace (6, inferior_pid, i, value);
618 if (errno == 0)
619 {
620 printf (" Succeeded with address 0x%x; value 0x%x (%d).\n",
621 i, value, value);
622 }
623 else if ((i & 0377) == 0)
624 printf (" Failed at 0x%x.\n", i);
625 }
626 }
627 \f
628 static
629 initialize ()
630 {
631 add_com ("term-status", class_obscure, term_status_command,
632 "Print info on inferior's saved terminal status.");
633
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.");
637
638 add_com ("kill", class_run, kill_command,
639 "Kill execution of program being debugged.");
640
641 inferior_pid = 0;
642
643 ioctl (0, TIOCGETP, &sg_ours);
644 ioctl (0, TIOCGETC, &tc_ours);
645 ioctl (0, TIOCGLTC, &ltc_ours);
646 ioctl (0, TIOCLGET, &lmode_ours);
647 fcntl (0, F_GETFL, tflags_ours);
648 ioctl (0, TIOCGPGRP, &pgrp_ours);
649
650 terminal_is_ours = 1;
651 }
652
653 END_FILE
This page took 0.043115 seconds and 4 git commands to generate.