home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / gdb-4.9 / gdb / inflow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-12  |  15.6 KB  |  612 lines

  1. /* Low level interface to ptrace, for GDB when running under Unix.
  2.    Copyright 1986, 1987, 1989, 1991, 1992 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.  
  20. #include "defs.h"
  21. #include "frame.h"
  22. #include "inferior.h"
  23. #include "command.h"
  24. #include "signals.h"
  25. #include "terminal.h"
  26. #include "target.h"
  27.  
  28. #ifdef USG
  29. #include <sys/types.h>
  30. #endif
  31.  
  32. /* Some USG-esque systems (some of which are BSD-esque enough so that USG
  33.    is not defined) want this header, and it won't do any harm.  */
  34. #include <fcntl.h>
  35.  
  36. #include <sys/param.h>
  37. #include <signal.h>
  38.  
  39. static void
  40. kill_command PARAMS ((char *, int));
  41.  
  42. static void
  43. terminal_ours_1 PARAMS ((int));
  44.  
  45. /* Nonzero if we are debugging an attached outside process
  46.    rather than an inferior.  */
  47.  
  48. int attach_flag;
  49.  
  50.  
  51. /* Record terminal status separately for debugger and inferior.  */
  52.  
  53. /* Does GDB have a terminal (on stdin)?  */
  54. int gdb_has_a_terminal;
  55. #if !defined(__GO32__)
  56. static TERMINAL sg_inferior;
  57. static TERMINAL sg_ours;
  58. #endif
  59. static int tflags_inferior;
  60. static int tflags_ours;
  61.  
  62. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  63. static struct tchars tc_inferior;
  64. static struct tchars tc_ours;
  65. #endif
  66.  
  67. #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
  68. static struct ltchars ltc_inferior;
  69. static struct ltchars ltc_ours;
  70. #endif
  71.  
  72. #ifdef TIOCLGET
  73. /* The line discipline flags.  Note that even when gdb_has_a_terminal
  74.    is true, the ioctl's to get and set the line discipline flags may
  75.    fail.  An example is running gdb under "script" using the streams
  76.    based interface under SVR4.  So we keep track of whether or not
  77.    the flags we get are valid by setting the *_valid flag, and don't
  78.    try to reset them unless they are valid. */
  79. static int lmode_inferior;
  80. static int lmode_inferior_valid;
  81. static int lmode_ours;
  82. static int lmode_ours_valid;
  83. #endif
  84.  
  85. #ifdef TIOCGPGRP
  86. # ifdef SHORT_PGRP
  87. static short pgrp_inferior;
  88. static short pgrp_ours;
  89. # else /* not def SHORT_PGRP */
  90. static int pgrp_inferior;
  91. static int pgrp_ours;
  92. # endif /* not def SHORT_PGRP */
  93. #else /* not def TIOCGPGRP */
  94. static void (*sigint_ours) ();
  95. static void (*sigquit_ours) ();
  96. #endif /* TIOCGPGRP */
  97.  
  98. /* The name of the tty (from the `tty' command) that we gave to the inferior
  99.    when it was last started.  */
  100.  
  101. static char *inferior_thisrun_terminal;
  102.  
  103. /* Nonzero if our terminal settings are in effect.
  104.    Zero if the inferior's settings are in effect.  */
  105.  
  106. static int terminal_is_ours;
  107.  
  108. /* Macro for printing errors from ioctl operations */
  109.  
  110. #define    OOPSY(what)    \
  111.   if (result == -1)    \
  112.     fprintf(stderr, "[%s failed in terminal_inferior: %s]\n", \
  113.         what, strerror (errno))
  114.  
  115. static void terminal_ours_1 ();
  116.  
  117. /* Initialize the terminal settings we record for the inferior,
  118.    before we actually run the inferior.  */
  119.  
  120. void
  121. terminal_init_inferior ()
  122. {
  123. #if !defined(__GO32__)
  124.   sg_inferior = sg_ours;
  125.   tflags_inferior = tflags_ours;
  126.  
  127. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  128.   tc_inferior = tc_ours;
  129. #endif
  130.  
  131. #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
  132.   ltc_inferior = ltc_ours;
  133. #endif
  134.  
  135. #ifdef TIOCLGET
  136.   lmode_inferior = lmode_ours;
  137.   lmode_inferior_valid = lmode_ours_valid;
  138. #endif
  139.  
  140. #ifdef TIOCGPGRP
  141.   pgrp_inferior = inferior_pid;
  142. #endif /* TIOCGPGRP */
  143. #endif
  144.   terminal_is_ours = 1;
  145. }
  146.  
  147. /* Put the inferior's terminal settings into effect.
  148.    This is preparation for starting or resuming the inferior.  */
  149.  
  150. void
  151. terminal_inferior ()
  152. {
  153. #if !defined(__GO32__)
  154.   int result;
  155.  
  156.   if (gdb_has_a_terminal && terminal_is_ours && inferior_thisrun_terminal == 0)
  157.     {
  158.       result = fcntl (0, F_SETFL, tflags_inferior);
  159.       result = fcntl (0, F_SETFL, tflags_inferior);
  160.       OOPSY ("fcntl F_SETFL");
  161.       result = ioctl (0, TIOCSETN, &sg_inferior);
  162.       OOPSY ("ioctl TIOCSETN");
  163.  
  164. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  165.       result = ioctl (0, TIOCSETC, &tc_inferior);
  166.       OOPSY ("ioctl TIOCSETC");
  167. #endif
  168. #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
  169.       result = ioctl (0, TIOCSLTC, <c_inferior);
  170.       OOPSY ("ioctl TIOCSLTC");
  171. #endif
  172. #ifdef TIOCLSET
  173.       if (lmode_inferior_valid)
  174.     {
  175.       result = ioctl (0, TIOCLSET, &lmode_inferior);
  176.       OOPSY ("ioctl TIOCLSET");
  177.     }
  178. #endif
  179.  
  180. #ifdef TIOCGPGRP
  181.       result = ioctl (0, TIOCSPGRP, &pgrp_inferior);
  182.       /* If we attached to the process, we might or might not be sharing
  183.      a terminal.  Avoid printing error msg if we are unable to set our
  184.      terminal's process group to his process group ID.  */
  185.       if (!attach_flag) {
  186.     OOPSY ("ioctl TIOCSPGRP");
  187.       }
  188. #else
  189.       sigint_ours = (void (*) ()) signal (SIGINT, SIG_IGN);
  190.       sigquit_ours = (void (*) ()) signal (SIGQUIT, SIG_IGN);
  191. #endif /* TIOCGPGRP */
  192.     }
  193. #endif
  194.   terminal_is_ours = 0;
  195. }
  196.  
  197. /* Put some of our terminal settings into effect,
  198.    enough to get proper results from our output,
  199.    but do not change into or out of RAW mode
  200.    so that no input is discarded.
  201.  
  202.    After doing this, either terminal_ours or terminal_inferior
  203.    should be called to get back to a normal state of affairs.  */
  204.  
  205. void
  206. terminal_ours_for_output ()
  207. {
  208.   terminal_ours_1 (1);
  209. }
  210.  
  211. /* Put our terminal settings into effect.
  212.    First record the inferior's terminal settings
  213.    so they can be restored properly later.  */
  214.  
  215. void
  216. terminal_ours ()
  217. {
  218.   terminal_ours_1 (0);
  219. }
  220.  
  221. static void
  222. terminal_ours_1 (output_only)
  223.      int output_only;
  224. {
  225. #if !defined(__GO32__)
  226.   int result;
  227. #ifdef TIOCGPGRP
  228.   /* Ignore this signal since it will happen when we try to set the pgrp.  */
  229.   void (*osigttou) ();
  230. #endif /* TIOCGPGRP */
  231.  
  232.   /* Checking inferior_thisrun_terminal is necessary so that
  233.      if GDB is running in the background, it won't block trying
  234.      to do the ioctl()'s below.  Checking gdb_has_a_terminal
  235.      avoids attempting all the ioctl's when running in batch.  */
  236.   if (inferior_thisrun_terminal != 0 || gdb_has_a_terminal == 0)
  237.     return;
  238.  
  239.   if (!terminal_is_ours)
  240.     {
  241.       terminal_is_ours = 1;
  242.  
  243. #ifdef TIOCGPGRP
  244.       osigttou = (void (*) ()) signal (SIGTTOU, SIG_IGN);
  245.  
  246.       result = ioctl (0, TIOCGPGRP, &pgrp_inferior);
  247.       result = ioctl (0, TIOCSPGRP, &pgrp_ours);
  248.  
  249.       signal (SIGTTOU, osigttou);
  250. #else
  251.       signal (SIGINT, sigint_ours);
  252.       signal (SIGQUIT, sigquit_ours);
  253. #endif /* TIOCGPGRP */
  254.  
  255.       tflags_inferior = fcntl (0, F_GETFL, 0);
  256.       result = ioctl (0, TIOCGETP, &sg_inferior);
  257.  
  258. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  259.       result = ioctl (0, TIOCGETC, &tc_inferior);
  260. #endif
  261. #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
  262.       result = ioctl (0, TIOCGLTC, <c_inferior);
  263. #endif
  264. #ifdef TIOCLGET
  265.       result = ioctl (0, TIOCLGET, &lmode_inferior);
  266.       lmode_inferior_valid = (result == 0);
  267. #endif
  268.     }
  269.  
  270. #ifdef HAVE_TERMIO
  271.   sg_ours.c_lflag |= ICANON;
  272.   if (output_only && !(sg_inferior.c_lflag & ICANON))
  273.     sg_ours.c_lflag &= ~ICANON;
  274. #else /* not HAVE_TERMIO */
  275.   sg_ours.sg_flags &= ~RAW & ~CBREAK;
  276.   if (output_only)
  277.     sg_ours.sg_flags |= (RAW | CBREAK) & sg_inferior.sg_flags;
  278. #endif /* not HAVE_TERMIO */
  279.  
  280.   result = fcntl (0, F_SETFL, tflags_ours);
  281.   result = fcntl (0, F_SETFL, tflags_ours);
  282.   result = ioctl (0, TIOCSETN, &sg_ours);
  283.  
  284. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  285.   result = ioctl (0, TIOCSETC, &tc_ours);
  286. #endif
  287. #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
  288.   result = ioctl (0, TIOCSLTC, <c_ours);
  289. #endif
  290. #ifdef TIOCLGET
  291.   if (lmode_ours_valid)
  292.     {
  293.       result = ioctl (0, TIOCLSET, &lmode_ours);
  294.     }
  295. #endif
  296.  
  297. #ifdef HAVE_TERMIO
  298.   sg_ours.c_lflag |= ICANON;
  299. #else /* not HAVE_TERMIO */
  300.   sg_ours.sg_flags &= ~RAW & ~CBREAK;
  301. #endif /* not HAVE_TERMIO */
  302.  
  303.   result = result;    /* lint */
  304. #endif
  305. }
  306.  
  307. /* ARGSUSED */
  308. void
  309. term_info (arg, from_tty)
  310.      char *arg;
  311.      int from_tty;
  312. {
  313.   target_terminal_info (arg, from_tty);
  314. }
  315.  
  316. /* ARGSUSED */
  317. void
  318. child_terminal_info (args, from_tty)
  319.      char *args;
  320.      int from_tty;
  321. {
  322.   register int i;
  323.  
  324.   if (!gdb_has_a_terminal) {
  325.     printf_filtered ("This GDB does not control a terminal.\n");
  326.     return;
  327.   }
  328. #if !defined(__GO32__)
  329.   printf_filtered ("Inferior's terminal status (currently saved by GDB):\n");
  330.  
  331. #ifdef TIOCGPGRP
  332.   printf_filtered ("owner pgrp = %d, fcntl flags = 0x%x.\n",
  333.       pgrp_inferior, tflags_inferior);
  334. #endif /* TIOCGPGRP */
  335.  
  336. #ifdef HAVE_TERMIO
  337.  
  338.   printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
  339.       sg_inferior.c_iflag, sg_inferior.c_oflag);
  340.   printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
  341.       sg_inferior.c_cflag, sg_inferior.c_lflag, sg_inferior.c_line);
  342.   printf_filtered ("c_cc: ");
  343.   for (i = 0; (i < NCC); i += 1)
  344.     printf_filtered ("0x%x ", sg_inferior.c_cc[i]);
  345.   printf_filtered ("\n");
  346.  
  347. #else /* not HAVE_TERMIO */
  348.  
  349.   printf_filtered ("sgttyb.sg_flags = 0x%x.\n", sg_inferior.sg_flags);
  350.  
  351. #endif /* not HAVE_TERMIO */
  352.  
  353. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  354.   printf_filtered ("tchars: ");
  355.   for (i = 0; i < (int)sizeof (struct tchars); i++)
  356.     printf_filtered ("0x%x ", ((unsigned char *)&tc_inferior)[i]);
  357.   printf_filtered ("\n");
  358. #endif
  359.  
  360. #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
  361.   printf_filtered ("ltchars: ");
  362.   for (i = 0; i < (int)sizeof (struct ltchars); i++)
  363.     printf_filtered ("0x%x ", ((unsigned char *)<c_inferior)[i]);
  364.   printf_filtered ("\n");
  365. #endif
  366.   
  367. #ifdef TIOCLGET
  368.   printf_filtered ("lmode:  0x%x\n", lmode_inferior);
  369. #endif
  370. #else
  371.   printf_filtered("This is a DOS machine; there is no terminal state\n");
  372. #endif
  373.  
  374. }
  375.  
  376. /* NEW_TTY_PREFORK is called before forking a new child process,
  377.    so we can record the state of ttys in the child to be formed.
  378.    TTYNAME is null if we are to share the terminal with gdb;
  379.    or points to a string containing the name of the desired tty.
  380.  
  381.    NEW_TTY is called in new child processes under Unix, which will
  382.    become debugger target processes.  This actually switches to
  383.    the terminal specified in the NEW_TTY_PREFORK call.  */
  384.  
  385. void
  386. new_tty_prefork (ttyname)
  387.      char *ttyname;
  388. {
  389.   /* Save the name for later, for determining whether we and the child
  390.      are sharing a tty.  */
  391.   inferior_thisrun_terminal = ttyname;
  392. }
  393.  
  394. void
  395. new_tty ()
  396. {
  397.   register int tty;
  398.   void (*osigttou) ();
  399.  
  400.   if (inferior_thisrun_terminal == 0)
  401.     return;
  402. #if !defined(__GO32__)
  403. #ifdef TIOCNOTTY
  404.   /* Disconnect the child process from our controlling terminal.  On some
  405.      systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
  406.      ignore SIGTTOU. */
  407.   tty = open("/dev/tty", O_RDWR);
  408.   if (tty > 0)
  409.     {
  410.       osigttou = (void (*)()) signal(SIGTTOU, SIG_IGN);
  411.       ioctl(tty, TIOCNOTTY, 0);
  412.       close(tty);
  413.       signal(SIGTTOU, osigttou);
  414.     }
  415. #endif
  416.  
  417.   /* Now open the specified new terminal.  */
  418.  
  419. #ifdef USE_O_NOCTTY
  420.   tty = open(inferior_thisrun_terminal, O_RDWR | O_NOCTTY);
  421. #else
  422.   tty = open(inferior_thisrun_terminal, O_RDWR);
  423. #endif
  424.   if (tty == -1)
  425.     {
  426.       print_sys_errmsg (inferior_thisrun_terminal, errno);
  427.       _exit(1);
  428.     }
  429.  
  430.   /* Avoid use of dup2; doesn't exist on all systems.  */
  431.   if (tty != 0)
  432.     { close (0); dup (tty); }
  433.   if (tty != 1)
  434.     { close (1); dup (tty); }
  435.   if (tty != 2)
  436.     { close (2); dup (tty); }
  437.   if (tty > 2)
  438.     close(tty);
  439. #endif /* !go32 */
  440. }
  441.  
  442. /* Kill the inferior process.  Make us have no inferior.  */
  443.  
  444. /* ARGSUSED */
  445. static void
  446. kill_command (arg, from_tty)
  447.      char *arg;
  448.      int from_tty;
  449. {
  450.   /* Shouldn't this be target_has_execution?  FIXME.  */
  451.   if (inferior_pid == 0)
  452.     error ("The program is not being run.");
  453.   if (!query ("Kill the program being debugged? "))
  454.     error ("Not confirmed.");
  455.   target_kill ();
  456.  
  457.   /* Killing off the inferior can leave us with a core file.  If so,
  458.      print the state we are left in.  */
  459.   if (target_has_stack) {
  460.     printf_filtered ("In %s,\n", current_target->to_longname);
  461.     if (selected_frame == NULL)
  462.       fputs_filtered ("No selected stack frame.\n", stdout);
  463.     else
  464.       print_stack_frame (selected_frame, selected_frame_level, 1);
  465.   }
  466. }
  467.  
  468. /* The inferior process has died.  Long live the inferior!  */
  469.  
  470. void
  471. generic_mourn_inferior ()
  472. {
  473.   inferior_pid = 0;
  474.   attach_flag = 0;
  475.   mark_breakpoints_out ();
  476.   registers_changed ();
  477.  
  478. #ifdef CLEAR_DEFERRED_STORES
  479.   /* Delete any pending stores to the inferior... */
  480.   CLEAR_DEFERRED_STORES;
  481. #endif
  482.  
  483.   reopen_exec_file ();
  484.   if (target_has_stack) {
  485.     set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  486.                       read_pc ()));
  487.     select_frame (get_current_frame (), 0);
  488.   } else {
  489.     set_current_frame (0);
  490.     select_frame ((FRAME) 0, -1);
  491.   }
  492.   /* It is confusing to the user for ignore counts to stick around
  493.      from previous runs of the inferior.  So clear them.  */
  494.   breakpoint_clear_ignore_counts ();
  495. }
  496.  
  497. /* Call set_sigint_trap when you need to pass a signal on to an attached
  498.    process when handling SIGINT */
  499.  
  500. static void
  501. pass_signal()
  502. {
  503.   kill (inferior_pid, SIGINT);
  504. }
  505.  
  506. static void (*osig)();
  507.  
  508. void
  509. set_sigint_trap()
  510. {
  511.   osig = (void (*) ()) signal (SIGINT, pass_signal);
  512. }
  513.  
  514. void
  515. clear_sigint_trap()
  516. {
  517.   signal (SIGINT, osig);
  518. }
  519.  
  520.  
  521. #if 0 
  522. /* This function is just for testing, and on some systems (Sony NewsOS
  523.    3.2) <sys/user.h> also includes <sys/time.h> which leads to errors
  524.    (since on this system at least sys/time.h is not protected against
  525.    multiple inclusion).  */
  526. /* ARGSUSED */
  527. static void
  528. try_writing_regs_command (arg, from_tty)
  529.      char *arg;
  530.      int from_tty;
  531. {
  532.   register int i;
  533.   register int value;
  534.  
  535.   if (inferior_pid == 0)
  536.     error ("There is no inferior process now.");
  537.  
  538.   /* A Sun 3/50 or 3/60 (at least) running SunOS 4.0.3 will have a
  539.      kernel panic if we try to write past the end of the user area.
  540.      Presumably Sun will fix this bug (it has been reported), but it
  541.      is tacky to crash the system, so at least on SunOS4 we need to
  542.      stop writing when we hit the end of the user area.  */
  543.   for (i = 0; i < sizeof (struct user); i += 2)
  544.     {
  545.       QUIT;
  546.       errno = 0;
  547.       value = call_ptrace (3, inferior_pid, (PTRACE_ARG3_TYPE) i, 0);
  548.       call_ptrace (6, inferior_pid, (PTRACE_ARG3_TYPE) i, value);
  549.       if (errno == 0)
  550.     {
  551.       printf (" Succeeded with address 0x%x; value 0x%x (%d).\n",
  552.           i, value, value);
  553.     }
  554.       else if ((i & 0377) == 0)
  555.     printf (" Failed at 0x%x.\n", i);
  556.     }
  557. }
  558. #endif
  559.  
  560. void
  561. _initialize_inflow ()
  562. {
  563.   int result;
  564.  
  565.   add_info ("terminal", term_info,
  566.        "Print inferior's saved terminal status.");
  567.  
  568. #if 0
  569.   add_com ("try-writing-regs", class_obscure, try_writing_regs_command,
  570.        "Try writing all locations in inferior's system block.\n\
  571. Report which ones can be written.");
  572. #endif
  573.  
  574.   add_com ("kill", class_run, kill_command,
  575.        "Kill execution of program being debugged.");
  576.  
  577.   inferior_pid = 0;
  578.  
  579.   /* Get all the current tty settings (including whether we have a tty at
  580.      all!).  */
  581.  
  582. #if !defined(__GO32__)
  583.   tflags_ours = fcntl (0, F_GETFL, 0);
  584.  
  585.   result = ioctl (0, TIOCGETP, &sg_ours);
  586.   if (result == 0) {
  587.     gdb_has_a_terminal = 1;
  588.     /* Get the rest of the tty settings, then... */
  589. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  590.     ioctl (0, TIOCGETC, &tc_ours);
  591. #endif
  592. #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
  593.     ioctl (0, TIOCGLTC, <c_ours);
  594. #endif
  595. #ifdef TIOCLGET
  596.     result = ioctl (0, TIOCLGET, &lmode_ours);
  597.     lmode_ours_valid = (result == 0);
  598. #endif
  599. #ifdef TIOCGPGRP
  600.     ioctl (0, TIOCGPGRP, &pgrp_ours);
  601. #endif /* TIOCGPGRP */
  602.   } else {
  603.     gdb_has_a_terminal = 0;
  604.   }
  605. #else
  606.   gdb_has_a_terminal = 0;
  607. #endif
  608.  
  609.  
  610.   terminal_is_ours = 1;
  611. }
  612.