home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / gdb / inftarg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-12  |  9.0 KB  |  362 lines

  1. /* Target-vector operations for controlling Unix child processes, for GDB.
  2.    Copyright 1990, 1991, 1992, 1993, 1994, 1995
  3.    Free Software Foundation, Inc.
  4.    Contributed by Cygnus Support.
  5.  
  6. This file is part of GDB.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include "defs.h"
  23. #include "frame.h"  /* required by inferior.h */
  24. #include "inferior.h"
  25. #include "target.h"
  26. #include "wait.h"
  27. #include "gdbcore.h"
  28. #include "command.h"
  29. #include <signal.h>
  30. #include <sys/types.h>
  31. #include <fcntl.h>
  32.  
  33. static void
  34. child_prepare_to_store PARAMS ((void));
  35.  
  36. #ifndef CHILD_WAIT
  37. static int child_wait PARAMS ((int, struct target_waitstatus *));
  38. #endif /* CHILD_WAIT */
  39.  
  40. static void child_open PARAMS ((char *, int));
  41.  
  42. static void
  43. child_files_info PARAMS ((struct target_ops *));
  44.  
  45. static void
  46. child_detach PARAMS ((char *, int));
  47.  
  48. static void
  49. child_attach PARAMS ((char *, int));
  50.  
  51. static void
  52. ptrace_me PARAMS ((void));
  53.  
  54. static void
  55. ptrace_him PARAMS ((int));
  56.  
  57. static void child_create_inferior PARAMS ((char *, char *, char **));
  58.  
  59. static void
  60. child_mourn_inferior PARAMS ((void));
  61.  
  62. static int
  63. child_can_run PARAMS ((void));
  64.  
  65. extern char **environ;
  66.  
  67. /* Forward declaration */
  68. extern struct target_ops child_ops;
  69.  
  70. #ifndef CHILD_WAIT
  71.  
  72. /* Wait for child to do something.  Return pid of child, or -1 in case
  73.    of error; store status through argument pointer OURSTATUS.  */
  74.  
  75. static int
  76. child_wait (pid, ourstatus)
  77.      int pid;
  78.      struct target_waitstatus *ourstatus;
  79. {
  80.   int save_errno;
  81.   int status;
  82.  
  83.   do {
  84.     set_sigint_trap();    /* Causes SIGINT to be passed on to the
  85.                attached process. */
  86.     set_sigio_trap ();
  87.  
  88.     pid = proc_wait (inferior_pid, &status);
  89.     save_errno = errno;
  90.  
  91.     clear_sigio_trap ();
  92.  
  93.     clear_sigint_trap();
  94.  
  95.     if (pid == -1)
  96.       {
  97.     if (save_errno == EINTR)
  98.       continue;
  99.     fprintf_unfiltered (gdb_stderr, "Child process unexpectedly missing: %s.\n",
  100.          safe_strerror (save_errno));
  101.     /* Claim it exited with unknown signal.  */
  102.     ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
  103.     ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
  104.         return -1;
  105.       }
  106.   } while (pid != inferior_pid); /* Some other child died or stopped */
  107.   store_waitstatus (ourstatus, status);
  108.   return pid;
  109. }
  110. #endif /* CHILD_WAIT */
  111.  
  112. /* Attach to process PID, then initialize for debugging it.  */
  113.  
  114. static void
  115. child_attach (args, from_tty)
  116.      char *args;
  117.      int from_tty;
  118. {
  119.   if (!args)
  120.     error_no_arg ("process-id to attach");
  121.  
  122. #ifndef ATTACH_DETACH
  123.   error ("Can't attach to a process on this machine.");
  124. #else
  125.   {
  126.     char *exec_file;
  127.     int pid;
  128.  
  129.     pid = atoi (args);
  130.  
  131.     if (pid == getpid())        /* Trying to masturbate? */
  132.       error ("I refuse to debug myself!");
  133.  
  134.     if (from_tty)
  135.       {
  136.     exec_file = (char *) get_exec_file (0);
  137.  
  138.     if (exec_file)
  139.       printf_unfiltered ("Attaching to program `%s', %s\n", exec_file,
  140.           target_pid_to_str (pid));
  141.     else
  142.       printf_unfiltered ("Attaching to %s\n", target_pid_to_str (pid));
  143.  
  144.     gdb_flush (gdb_stdout);
  145.       }
  146.  
  147.     attach (pid);
  148.     inferior_pid = pid;
  149.     push_target (&child_ops);
  150.   }
  151. #endif  /* ATTACH_DETACH */
  152. }
  153.  
  154.  
  155. /* Take a program previously attached to and detaches it.
  156.    The program resumes execution and will no longer stop
  157.    on signals, etc.  We'd better not have left any breakpoints
  158.    in the program or it'll die when it hits one.  For this
  159.    to work, it may be necessary for the process to have been
  160.    previously attached.  It *might* work if the program was
  161.    started via the normal ptrace (PTRACE_TRACEME).  */
  162.  
  163. static void
  164. child_detach (args, from_tty)
  165.      char *args;
  166.      int from_tty;
  167. {
  168. #ifdef ATTACH_DETACH
  169.   {
  170.     int siggnal = 0;
  171.  
  172.     if (from_tty)
  173.       {
  174.     char *exec_file = get_exec_file (0);
  175.     if (exec_file == 0)
  176.       exec_file = "";
  177.     printf_unfiltered ("Detaching from program: %s %s\n", exec_file,
  178.         target_pid_to_str (inferior_pid));
  179.     gdb_flush (gdb_stdout);
  180.       }
  181.     if (args)
  182.       siggnal = atoi (args);
  183.  
  184.     detach (siggnal);
  185.     inferior_pid = 0;
  186.     unpush_target (&child_ops);
  187.   }
  188. #else
  189.   error ("This version of Unix does not support detaching a process.");
  190. #endif
  191. }
  192.  
  193. /* Get ready to modify the registers array.  On machines which store
  194.    individual registers, this doesn't need to do anything.  On machines
  195.    which store all the registers in one fell swoop, this makes sure
  196.    that registers contains all the registers from the program being
  197.    debugged.  */
  198.  
  199. static void
  200. child_prepare_to_store ()
  201. {
  202. #ifdef CHILD_PREPARE_TO_STORE
  203.   CHILD_PREPARE_TO_STORE ();
  204. #endif
  205. }
  206.  
  207. /* Print status information about what we're accessing.  */
  208.  
  209. static void
  210. child_files_info (ignore)
  211.      struct target_ops *ignore;
  212. {
  213.   printf_unfiltered ("\tUsing the running image of %s %s.\n",
  214.       attach_flag? "attached": "child", target_pid_to_str (inferior_pid));
  215. }
  216.  
  217. /* ARGSUSED */
  218. static void
  219. child_open (arg, from_tty)
  220.      char *arg;
  221.      int from_tty;
  222. {
  223.   error ("Use the \"run\" command to start a Unix child process.");
  224. }
  225.  
  226. /* Stub function which causes the inferior that runs it, to be ptrace-able
  227.    by its parent process.  */
  228.  
  229. static void
  230. ptrace_me ()
  231. {
  232.   /* "Trace me, Dr. Memory!" */
  233.   call_ptrace (0, 0, (PTRACE_ARG3_TYPE) 0, 0);
  234. }
  235.  
  236. /* Stub function which causes the GDB that runs it, to start ptrace-ing
  237.    the child process.  */
  238.  
  239. static void
  240. ptrace_him (pid)
  241.      int pid;
  242. {
  243.   push_target (&child_ops);
  244.  
  245. #ifdef START_INFERIOR_TRAPS_EXPECTED
  246.   startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
  247. #else
  248.   /* One trap to exec the shell, one to exec the program being debugged.  */
  249.   startup_inferior (2);
  250. #endif
  251. }
  252.  
  253. /* Start an inferior Unix child process and sets inferior_pid to its pid.
  254.    EXEC_FILE is the file to run.
  255.    ALLARGS is a string containing the arguments to the program.
  256.    ENV is the environment vector to pass.  Errors reported with error().  */
  257.  
  258. static void
  259. child_create_inferior (exec_file, allargs, env)
  260.      char *exec_file;
  261.      char *allargs;
  262.      char **env;
  263. {
  264.   fork_inferior (exec_file, allargs, env, ptrace_me, ptrace_him, NULL);
  265.   /* We are at the first instruction we care about.  */
  266.   /* Pedal to the metal... */
  267.   proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
  268. }
  269.  
  270. static void
  271. child_mourn_inferior ()
  272. {
  273.   unpush_target (&child_ops);
  274.   proc_remove_foreign (inferior_pid);
  275.   generic_mourn_inferior ();
  276. }
  277.  
  278. static int
  279. child_can_run ()
  280. {
  281.   return(1);
  282. }
  283.  
  284. /* Send a SIGINT to the process group.  This acts just like the user typed a
  285.    ^C on the controlling terminal.
  286.  
  287.    XXX - This may not be correct for all systems.  Some may want to use
  288.    killpg() instead of kill (-pgrp). */
  289.  
  290. void
  291. child_stop ()
  292. {
  293.   extern pid_t inferior_process_group;
  294.  
  295.   kill (-inferior_process_group, SIGINT);
  296. }
  297.  
  298. struct target_ops child_ops = {
  299.   "child",            /* to_shortname */
  300.   "Unix child process",        /* to_longname */
  301.   "Unix child process (started by the \"run\" command).",    /* to_doc */
  302.   child_open,            /* to_open */
  303.   0,                /* to_close */
  304.   child_attach,            /* to_attach */
  305.   child_detach,         /* to_detach */
  306.   child_resume,            /* to_resume */
  307.   child_wait,            /* to_wait */
  308.   fetch_inferior_registers,    /* to_fetch_registers */
  309.   store_inferior_registers,    /* to_store_registers */
  310.   child_prepare_to_store,    /* to_prepare_to_store */
  311.   child_xfer_memory,        /* to_xfer_memory */
  312.   child_files_info,        /* to_files_info */
  313.   memory_insert_breakpoint,    /* to_insert_breakpoint */
  314.   memory_remove_breakpoint,    /* to_remove_breakpoint */
  315.   terminal_init_inferior,    /* to_terminal_init */
  316.   terminal_inferior,         /* to_terminal_inferior */
  317.   terminal_ours_for_output,    /* to_terminal_ours_for_output */
  318.   terminal_ours,        /* to_terminal_ours */
  319.   child_terminal_info,        /* to_terminal_info */
  320.   kill_inferior,        /* to_kill */
  321.   0,                /* to_load */
  322.   0,                /* to_lookup_symbol */
  323.   child_create_inferior,    /* to_create_inferior */
  324.   child_mourn_inferior,        /* to_mourn_inferior */
  325.   child_can_run,        /* to_can_run */
  326.   0,                 /* to_notice_signals */
  327.   child_stop,            /* to_stop */
  328.   process_stratum,        /* to_stratum */
  329.   0,                /* to_next */
  330.   1,                /* to_has_all_memory */
  331.   1,                /* to_has_memory */
  332.   1,                /* to_has_stack */
  333.   1,                /* to_has_registers */
  334.   1,                /* to_has_execution */
  335.   0,                /* to_sections */
  336.   0,                /* to_sections_end */
  337.   OPS_MAGIC            /* to_magic */
  338. };
  339.  
  340. void
  341. _initialize_inftarg ()
  342. {
  343. #ifdef HAVE_OPTIONAL_PROC_FS
  344.   char procname[32];
  345.   int fd;
  346.  
  347.   /* If we have an optional /proc filesystem (e.g. under OSF/1),
  348.      don't add ptrace support if we can access the running GDB via /proc.  */
  349. #ifndef PROC_NAME_FMT
  350. #define PROC_NAME_FMT "/proc/%05d"
  351. #endif
  352.   sprintf (procname, PROC_NAME_FMT, getpid ());
  353.   if ((fd = open (procname, O_RDONLY)) >= 0)
  354.     {
  355.       close (fd);
  356.       return;
  357.     }
  358. #endif
  359.  
  360.   add_target (&child_ops);
  361. }
  362.