static int inf_ptrace_follow_fork (struct target_ops *ops, int follow_child, int detach_fork) { pid_t pid, fpid; ptrace_state_t pe; pid = ptid_get_pid (inferior_ptid); if (ptrace (PT_GET_PROCESS_STATE, pid, (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1) perror_with_name (("ptrace")); gdb_assert (pe.pe_report_event == PTRACE_FORK); fpid = pe.pe_other_pid; if (follow_child) { struct inferior *parent_inf, *child_inf; struct thread_info *tp; parent_inf = find_inferior_pid (pid); /* Add the child. */ child_inf = add_inferior (fpid); child_inf->attach_flag = parent_inf->attach_flag; copy_terminal_info (child_inf, parent_inf); child_inf->pspace = parent_inf->pspace; child_inf->aspace = parent_inf->aspace; /* Before detaching from the parent, remove all breakpoints from it. */ remove_breakpoints (); if (ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, 0) == -1) perror_with_name (("ptrace")); /* Switch inferior_ptid out of the parent's way. */ inferior_ptid = pid_to_ptid (fpid); /* Delete the parent. */ detach_inferior (pid); add_thread_silent (inferior_ptid); } else { /* Breakpoints have already been detached from the child by infrun.c. */ if (ptrace (PT_DETACH, fpid, (PTRACE_TYPE_ARG3)1, 0) == -1) perror_with_name (("ptrace")); } return 0; }
static void inf_ptrace_attach (struct target_ops *ops, char *args, int from_tty) { char *exec_file; pid_t pid; char *dummy; struct inferior *inf; if (!args) error_no_arg (_("process-id to attach")); dummy = args; pid = strtol (args, &dummy, 0); /* Some targets don't set errno on errors, grrr! */ if (pid == 0 && args == dummy) error (_("Illegal process-id: %s."), args); if (pid == getpid ()) /* Trying to m********e? */ error (_("I refuse to debug myself!")); if (from_tty) { exec_file = get_exec_file (0); if (exec_file) printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file, target_pid_to_str (pid_to_ptid (pid))); else printf_unfiltered (_("Attaching to %s\n"), target_pid_to_str (pid_to_ptid (pid))); gdb_flush (gdb_stdout); } #ifdef PT_ATTACH errno = 0; ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0); if (errno != 0) perror_with_name (("ptrace")); #else error (_("This system does not support attaching to a process")); #endif inferior_ptid = pid_to_ptid (pid); inf = add_inferior (pid); inf->attach_flag = 1; /* Always add a main thread. If some target extends the ptrace target, it should decorate the ptid later with more info. */ add_thread_silent (inferior_ptid); push_target(ops); }
static void kgdb_trgt_open(char *filename, int from_tty) { struct cleanup *old_chain; struct kthr *kt; struct inferior *inf8; struct program_space *pspace; kvm_t *nkvm; char *temp; int first_inferior = 1; target_preopen (from_tty); if (!filename) error ("No vmcore file specified."); if (!exec_bfd) error ("Can't open a vmcore without a kernel"); filename = tilde_expand (filename); if (filename[0] != '/') { temp = concat (current_directory, "/", filename, NULL); xfree(filename); filename = temp; } old_chain = make_cleanup (xfree, filename); nkvm = kvm_openfiles(bfd_get_filename(exec_bfd), filename, NULL, write_files ? O_RDWR : O_RDONLY, kvm_err); if (nkvm == NULL) error ("Failed to open vmcore: %s", kvm_err); /* Don't free the filename now and close any previous vmcore. */ discard_cleanups(old_chain); unpush_target(&kgdb_trgt_ops); kvm = nkvm; vmcore = filename; old_chain = make_cleanup(kgdb_core_cleanup, NULL); push_target (&kgdb_trgt_ops); discard_cleanups (old_chain); kgdb_dmesg(); init_thread_list(); kt = kgdb_thr_init(); while (kt != NULL) { if (!in_inferior_list(kt->pid)) { if (first_inferior) { first_inferior = 0; inf8 = current_inferior(); inf8->pid = kt->pid; inferior_appeared (inf8, kt->pid); pspace = current_program_space; pspace->ebfd = 0; pspace->ebfd_mtime = 0; } else { inf8 = add_inferior(kt->pid); pspace = add_program_space(new_address_space()); pspace->symfile_object_file = symfile_objfile; pspace->objfiles = object_files; } inf8->pspace = pspace; inf8->aspace = pspace->aspace; } add_thread(ptid_build(kt->pid, 0, kt->tid)); kt = kgdb_thr_next(kt); } if (curkthr != 0) inferior_ptid = ptid_build(curkthr->pid, 0, curkthr->tid); frame_unwind_prepend_unwinder(get_frame_arch(get_current_frame()), &kgdb_trgt_trapframe_unwind); /* XXX: fetch registers? */ kld_init(); reinit_frame_cache(); select_frame (get_current_frame()); print_stack_frame(get_selected_frame(NULL), frame_relative_level(get_selected_frame(NULL)), 1); }