| 1 | /* Subroutines for handling an "inferior" (child) process as a target |
| 2 | for debugging, in GDB. |
| 3 | Copyright 1990, 1991 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 "ieee-float.h" /* Required by REGISTER_CONVERT_TO_XXX */ |
| 29 | |
| 30 | static void |
| 31 | child_prepare_to_store PARAMS ((void)); |
| 32 | |
| 33 | static int |
| 34 | child_wait PARAMS ((int *)); |
| 35 | |
| 36 | static void |
| 37 | child_open PARAMS ((char *, int)); |
| 38 | |
| 39 | static void |
| 40 | child_files_info PARAMS ((struct target_ops *)); |
| 41 | |
| 42 | static void |
| 43 | child_detach PARAMS ((char *, int)); |
| 44 | |
| 45 | /* Forward declaration */ |
| 46 | extern struct target_ops child_ops; |
| 47 | |
| 48 | /* Wait for child to do something. Return pid of child, or -1 in case |
| 49 | of error; store status through argument pointer STATUS. */ |
| 50 | |
| 51 | static int |
| 52 | child_wait (status) |
| 53 | int *status; |
| 54 | { |
| 55 | int pid; |
| 56 | |
| 57 | do { |
| 58 | #ifdef USE_PROC_FS |
| 59 | pid = proc_wait (status); |
| 60 | #else |
| 61 | pid = wait (status); |
| 62 | #endif |
| 63 | if (pid == -1) /* No more children to wait for */ |
| 64 | { |
| 65 | fprintf (stderr, "Child process unexpectedly missing.\n"); |
| 66 | *status = 42; /* Claim it exited with signal 42 */ |
| 67 | return -1; |
| 68 | } |
| 69 | } while (pid != inferior_pid); /* Some other child died or stopped */ |
| 70 | return pid; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | /* |
| 75 | * child_detach() |
| 76 | * takes a program previously attached to and detaches it. |
| 77 | * The program resumes execution and will no longer stop |
| 78 | * on signals, etc. We better not have left any breakpoints |
| 79 | * in the program or it'll die when it hits one. For this |
| 80 | * to work, it may be necessary for the process to have been |
| 81 | * previously attached. It *might* work if the program was |
| 82 | * started via the normal ptrace (PTRACE_TRACEME). |
| 83 | */ |
| 84 | |
| 85 | static void |
| 86 | child_detach (args, from_tty) |
| 87 | char *args; |
| 88 | int from_tty; |
| 89 | { |
| 90 | int siggnal = 0; |
| 91 | |
| 92 | #ifdef ATTACH_DETACH |
| 93 | if (from_tty) |
| 94 | { |
| 95 | char *exec_file = get_exec_file (0); |
| 96 | if (exec_file == 0) |
| 97 | exec_file = ""; |
| 98 | printf ("Detaching program: %s pid %d\n", |
| 99 | exec_file, inferior_pid); |
| 100 | fflush (stdout); |
| 101 | } |
| 102 | if (args) |
| 103 | siggnal = atoi (args); |
| 104 | |
| 105 | detach (siggnal); |
| 106 | inferior_pid = 0; |
| 107 | unpush_target (&child_ops); /* Pop out of handling an inferior */ |
| 108 | #else |
| 109 | error ("This version of Unix does not support detaching a process."); |
| 110 | #endif |
| 111 | } |
| 112 | |
| 113 | /* Get ready to modify the registers array. On machines which store |
| 114 | individual registers, this doesn't need to do anything. On machines |
| 115 | which store all the registers in one fell swoop, this makes sure |
| 116 | that registers contains all the registers from the program being |
| 117 | debugged. */ |
| 118 | |
| 119 | static void |
| 120 | child_prepare_to_store () |
| 121 | { |
| 122 | #ifdef CHILD_PREPARE_TO_STORE |
| 123 | CHILD_PREPARE_TO_STORE (); |
| 124 | #endif |
| 125 | } |
| 126 | |
| 127 | /* Print status information about what we're accessing. */ |
| 128 | |
| 129 | static void |
| 130 | child_files_info (ignore) |
| 131 | struct target_ops *ignore; |
| 132 | { |
| 133 | printf ("\tUsing the running image of %s process %d.\n", |
| 134 | attach_flag? "attached": "child", inferior_pid); |
| 135 | } |
| 136 | |
| 137 | /* ARGSUSED */ |
| 138 | static void |
| 139 | child_open (arg, from_tty) |
| 140 | char *arg; |
| 141 | int from_tty; |
| 142 | { |
| 143 | error ("Use the \"run\" command to start a Unix child process."); |
| 144 | } |
| 145 | |
| 146 | struct target_ops child_ops = { |
| 147 | "child", /* to_shortname */ |
| 148 | "Unix child process", /* to_longname */ |
| 149 | "Unix child process (started by the \"run\" command).", /* to_doc */ |
| 150 | child_open, /* to_open */ |
| 151 | 0, /* to_close */ |
| 152 | child_attach, /* to_attach */ |
| 153 | child_detach, /* to_detach */ |
| 154 | child_resume, /* to_resume */ |
| 155 | child_wait, /* to_wait */ |
| 156 | fetch_inferior_registers, /* to_fetch_registers */ |
| 157 | store_inferior_registers, /* to_store_registers */ |
| 158 | child_prepare_to_store, /* to_prepare_to_store */ |
| 159 | child_xfer_memory, /* to_xfer_memory */ |
| 160 | child_files_info, /* to_files_info */ |
| 161 | memory_insert_breakpoint, /* to_insert_breakpoint */ |
| 162 | memory_remove_breakpoint, /* to_remove_breakpoint */ |
| 163 | terminal_init_inferior, /* to_terminal_init */ |
| 164 | terminal_inferior, /* to_terminal_inferior */ |
| 165 | terminal_ours_for_output, /* to_terminal_ours_for_output */ |
| 166 | terminal_ours, /* to_terminal_ours */ |
| 167 | child_terminal_info, /* to_terminal_info */ |
| 168 | kill_inferior, /* to_kill */ |
| 169 | 0, /* to_load */ |
| 170 | 0, /* to_lookup_symbol */ |
| 171 | child_create_inferior, /* to_create_inferior */ |
| 172 | child_mourn_inferior, /* to_mourn_inferior */ |
| 173 | process_stratum, /* to_stratum */ |
| 174 | 0, /* to_next */ |
| 175 | 1, /* to_has_all_memory */ |
| 176 | 1, /* to_has_memory */ |
| 177 | 1, /* to_has_stack */ |
| 178 | 1, /* to_has_registers */ |
| 179 | 1, /* to_has_execution */ |
| 180 | 0, /* sections */ |
| 181 | 0, /* sections_end */ |
| 182 | OPS_MAGIC /* to_magic */ |
| 183 | }; |
| 184 | |
| 185 | void |
| 186 | _initialize_inftarg () |
| 187 | { |
| 188 | add_target (&child_ops); |
| 189 | } |