1 /* Target-vector operations for controlling Unix child processes, for GDB. 2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 int 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 static int 66 proc_wait PARAMS ((int, int*)); 67 68 static void 69 child_stop PARAMS ((void)); 70 71 #ifndef CHILD_THREAD_ALIVE 72 static int child_thread_alive PARAMS ((int)); 73 #endif 74 75 extern char **environ; 76 77 /* Forward declaration */ 78 extern struct target_ops child_ops; 79 80 int child_suppress_run = 0; /* Non-zero if inftarg should pretend not to 81 be a runnable target. Used by targets 82 that can sit atop inftarg, such as HPUX 83 thread support. */ 84 static int 85 proc_wait (pid, status) 86 int pid; 87 int *status; 88 { 89 #ifndef __GO32__ 90 return wait (status); 91 #endif 92 } 93 94 #ifndef CHILD_WAIT 95 96 /* Wait for child to do something. Return pid of child, or -1 in case 97 of error; store status through argument pointer OURSTATUS. */ 98 99 static int 100 child_wait (pid, ourstatus) 101 int pid; 102 struct target_waitstatus *ourstatus; 103 { 104 int save_errno; 105 int status; 106 107 do { 108 set_sigint_trap(); /* Causes SIGINT to be passed on to the 109 attached process. */ 110 set_sigio_trap (); 111 112 pid = proc_wait (inferior_pid, &status); 113 save_errno = errno; 114 115 clear_sigio_trap (); 116 117 clear_sigint_trap(); 118 119 if (pid == -1) 120 { 121 if (save_errno == EINTR) 122 continue; 123 fprintf_unfiltered (gdb_stderr, "Child process unexpectedly missing: %s.\n", 124 safe_strerror (save_errno)); 125 /* Claim it exited with unknown signal. */ 126 ourstatus->kind = TARGET_WAITKIND_SIGNALLED; 127 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN; 128 return -1; 129 } 130 } while (pid != inferior_pid); /* Some other child died or stopped */ 131 store_waitstatus (ourstatus, status); 132 return pid; 133 } 134 #endif /* CHILD_WAIT */ 135 136 #ifndef CHILD_THREAD_ALIVE 137 138 /* Check to see if the given thread is alive. 139 140 FIXME: Is kill() ever the right way to do this? I doubt it, but 141 for now we're going to try and be compatable with the old thread 142 code. */ 143 static int 144 child_thread_alive (pid) 145 int pid; 146 { 147 return (kill (pid, 0) != -1); 148 } 149 150 #endif 151 152 /* Attach to process PID, then initialize for debugging it. */ 153 154 static void 155 child_attach (args, from_tty) 156 char *args; 157 int from_tty; 158 { 159 if (!args) 160 error_no_arg ("process-id to attach"); 161 162 #ifndef ATTACH_DETACH 163 error ("Can't attach to a process on this machine."); 164 #else 165 { 166 char *exec_file; 167 int pid; 168 169 pid = atoi (args); 170 171 if (pid == getpid()) /* Trying to masturbate? */ 172 error ("I refuse to debug myself!"); 173 174 if (from_tty) 175 { 176 exec_file = (char *) get_exec_file (0); 177 178 if (exec_file) 179 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file, 180 target_pid_to_str (pid)); 181 else 182 printf_unfiltered ("Attaching to %s\n", target_pid_to_str (pid)); 183 184 gdb_flush (gdb_stdout); 185 } 186 187 attach (pid); 188 inferior_pid = pid; 189 push_target (&child_ops); 190 } 191 #endif /* ATTACH_DETACH */ 192 } 193 194 195 /* Take a program previously attached to and detaches it. 196 The program resumes execution and will no longer stop 197 on signals, etc. We'd better not have left any breakpoints 198 in the program or it'll die when it hits one. For this 199 to work, it may be necessary for the process to have been 200 previously attached. It *might* work if the program was 201 started via the normal ptrace (PTRACE_TRACEME). */ 202 203 static void 204 child_detach (args, from_tty) 205 char *args; 206 int from_tty; 207 { 208 #ifdef ATTACH_DETACH 209 { 210 int siggnal = 0; 211 212 if (from_tty) 213 { 214 char *exec_file = get_exec_file (0); 215 if (exec_file == 0) 216 exec_file = ""; 217 printf_unfiltered ("Detaching from program: %s %s\n", exec_file, 218 target_pid_to_str (inferior_pid)); 219 gdb_flush (gdb_stdout); 220 } 221 if (args) 222 siggnal = atoi (args); 223 224 detach (siggnal); 225 inferior_pid = 0; 226 unpush_target (&child_ops); 227 } 228 #else 229 error ("This version of Unix does not support detaching a process."); 230 #endif 231 } 232 233 /* Get ready to modify the registers array. On machines which store 234 individual registers, this doesn't need to do anything. On machines 235 which store all the registers in one fell swoop, this makes sure 236 that registers contains all the registers from the program being 237 debugged. */ 238 239 static void 240 child_prepare_to_store () 241 { 242 #ifdef CHILD_PREPARE_TO_STORE 243 CHILD_PREPARE_TO_STORE (); 244 #endif 245 } 246 247 /* Print status information about what we're accessing. */ 248 249 static void 250 child_files_info (ignore) 251 struct target_ops *ignore; 252 { 253 printf_unfiltered ("\tUsing the running image of %s %s.\n", 254 attach_flag? "attached": "child", target_pid_to_str (inferior_pid)); 255 } 256 257 /* ARGSUSED */ 258 static void 259 child_open (arg, from_tty) 260 char *arg; 261 int from_tty; 262 { 263 error ("Use the \"run\" command to start a Unix child process."); 264 } 265 266 /* Stub function which causes the inferior that runs it, to be ptrace-able 267 by its parent process. */ 268 269 static void 270 ptrace_me () 271 { 272 /* "Trace me, Dr. Memory!" */ 273 call_ptrace (0, 0, (PTRACE_ARG3_TYPE) 0, 0); 274 } 275 276 /* Stub function which causes the GDB that runs it, to start ptrace-ing 277 the child process. */ 278 279 static int 280 ptrace_him (pid) 281 int pid; 282 { 283 push_target (&child_ops); 284 285 #ifdef START_INFERIOR_TRAPS_EXPECTED 286 startup_inferior (START_INFERIOR_TRAPS_EXPECTED); 287 #else 288 /* One trap to exec the shell, one to exec the program being debugged. */ 289 startup_inferior (2); 290 #endif 291 292 return pid; 293 } 294 295 /* Start an inferior Unix child process and sets inferior_pid to its pid. 296 EXEC_FILE is the file to run. 297 ALLARGS is a string containing the arguments to the program. 298 ENV is the environment vector to pass. Errors reported with error(). */ 299 300 static void 301 child_create_inferior (exec_file, allargs, env) 302 char *exec_file; 303 char *allargs; 304 char **env; 305 { 306 fork_inferior (exec_file, allargs, env, ptrace_me, ptrace_him, NULL); 307 /* We are at the first instruction we care about. */ 308 /* Pedal to the metal... */ 309 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0); 310 } 311 312 static void 313 child_mourn_inferior () 314 { 315 /* FIXME: Should be in a header file */ 316 extern void proc_remove_foreign PARAMS ((int)); 317 318 unpush_target (&child_ops); 319 proc_remove_foreign (inferior_pid); 320 generic_mourn_inferior (); 321 } 322 323 static int 324 child_can_run () 325 { 326 /* This variable is controlled by modules that sit atop inftarg that may layer 327 their own process structure atop that provided here. hpux-thread.c does 328 this because of the Hpux user-mode level thread model. */ 329 330 return !child_suppress_run; 331 } 332 333 /* Send a SIGINT to the process group. This acts just like the user typed a 334 ^C on the controlling terminal. 335 336 XXX - This may not be correct for all systems. Some may want to use 337 killpg() instead of kill (-pgrp). */ 338 339 static void 340 child_stop () 341 { 342 extern pid_t inferior_process_group; 343 344 kill (-inferior_process_group, SIGINT); 345 } 346 347 struct target_ops child_ops = { 348 "child", /* to_shortname */ 349 "Unix child process", /* to_longname */ 350 "Unix child process (started by the \"run\" command).", /* to_doc */ 351 child_open, /* to_open */ 352 0, /* to_close */ 353 child_attach, /* to_attach */ 354 child_detach, /* to_detach */ 355 child_resume, /* to_resume */ 356 child_wait, /* to_wait */ 357 fetch_inferior_registers, /* to_fetch_registers */ 358 store_inferior_registers, /* to_store_registers */ 359 child_prepare_to_store, /* to_prepare_to_store */ 360 child_xfer_memory, /* to_xfer_memory */ 361 child_files_info, /* to_files_info */ 362 memory_insert_breakpoint, /* to_insert_breakpoint */ 363 memory_remove_breakpoint, /* to_remove_breakpoint */ 364 terminal_init_inferior, /* to_terminal_init */ 365 terminal_inferior, /* to_terminal_inferior */ 366 terminal_ours_for_output, /* to_terminal_ours_for_output */ 367 terminal_ours, /* to_terminal_ours */ 368 child_terminal_info, /* to_terminal_info */ 369 kill_inferior, /* to_kill */ 370 0, /* to_load */ 371 0, /* to_lookup_symbol */ 372 child_create_inferior, /* to_create_inferior */ 373 child_mourn_inferior, /* to_mourn_inferior */ 374 child_can_run, /* to_can_run */ 375 0, /* to_notice_signals */ 376 child_thread_alive, /* to_thread_alive */ 377 child_stop, /* to_stop */ 378 process_stratum, /* to_stratum */ 379 0, /* to_next */ 380 1, /* to_has_all_memory */ 381 1, /* to_has_memory */ 382 1, /* to_has_stack */ 383 1, /* to_has_registers */ 384 1, /* to_has_execution */ 385 0, /* to_sections */ 386 0, /* to_sections_end */ 387 OPS_MAGIC /* to_magic */ 388 }; 389 390 void 391 _initialize_inftarg () 392 { 393 #ifdef HAVE_OPTIONAL_PROC_FS 394 char procname[32]; 395 int fd; 396 397 /* If we have an optional /proc filesystem (e.g. under OSF/1), 398 don't add ptrace support if we can access the running GDB via /proc. */ 399 #ifndef PROC_NAME_FMT 400 #define PROC_NAME_FMT "/proc/%05d" 401 #endif 402 sprintf (procname, PROC_NAME_FMT, getpid ()); 403 if ((fd = open (procname, O_RDONLY)) >= 0) 404 { 405 close (fd); 406 return; 407 } 408 #endif 409 410 add_target (&child_ops); 411 } 412