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

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