static void inf_ptrace_resume (ptid_t ptid, int step, enum target_signal signal) { pid_t pid = ptid_get_pid (ptid); int request = PT_CONTINUE; if (pid == -1) /* Resume all threads. Traditionally ptrace() only supports single-threaded processes, so simply resume the inferior. */ pid = ptid_get_pid (inferior_ptid); if (step) { /* If this system does not support PT_STEP, a higher level function will have called single_step() to transmute the step request into a continue request (by setting breakpoints on all possible successor instructions), so we don't have to worry about that here. */ request = PT_STEP; } /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from where it was. If GDB wanted it to start some other way, we have already written a new program counter value to the child. */ errno = 0; ptrace (request, pid, (PTRACE_TYPE_ARG3)1, target_signal_to_host (signal)); if (errno != 0) perror_with_name (("ptrace")); }
void child_resume (ptid_t ptid, int step, enum target_signal signal) { int request = PT_CONTINUE; int pid = PIDGET (ptid); if (pid == -1) /* Resume all threads. */ /* I think this only gets used in the non-threaded case, where "resume all threads" and "resume inferior_ptid" are the same. */ pid = PIDGET (inferior_ptid); if (step) { /* If this system does not support PT_STEP, a higher level function will have called single_step() to transmute the step request into a continue request (by setting breakpoints on all possible successor instructions), so we don't have to worry about that here. */ gdb_assert (!SOFTWARE_SINGLE_STEP_P ()); request = PT_STEP; } /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from where it was. If GDB wanted it to start some other way, we have already written a new PC value to the child. */ errno = 0; ptrace (request, pid, (PTRACE_TYPE_ARG3)1, target_signal_to_host (signal)); if (errno != 0) perror_with_name (("ptrace")); }
void child_resume (ptid_t ptid, int step, enum target_signal signal) { int func; int pid = PIDGET (ptid); errno = 0; /* If pid == -1, then we want to step/continue all threads, else we only want to step/continue a single thread. */ if (pid == -1) { pid = PIDGET (inferior_ptid); func = step ? PTRACE_SINGLESTEP : PTRACE_CONT; } else func = step ? PTRACE_SINGLESTEP_ONE : PTRACE_CONT_ONE; /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where it was. (If GDB wanted it to start some other way, we have already written a new PC value to the child.) If this system does not support PT_STEP, a higher level function will have called single_step() to transmute the step request into a continue request (by setting breakpoints on all possible successor instructions), so we don't have to worry about that here. */ ptrace (func, pid, (PTRACE_ARG3_TYPE) 1, target_signal_to_host (signal)); if (errno) perror_with_name ("ptrace"); }
static void i386_linux_resume (ptid_t ptid, int step, enum target_signal signal) { int pid = PIDGET (ptid); int request = PTRACE_CONT; if (pid == -1) /* Resume all threads. */ /* I think this only gets used in the non-threaded case, where "resume all threads" and "resume inferior_ptid" are the same. */ pid = PIDGET (inferior_ptid); if (step) { CORE_ADDR pc = read_pc_pid (pid_to_ptid (pid)); gdb_byte buf[LINUX_SYSCALL_LEN]; request = PTRACE_SINGLESTEP; /* Returning from a signal trampoline is done by calling a special system call (sigreturn or rt_sigreturn, see i386-linux-tdep.c for more information). This system call restores the registers that were saved when the signal was raised, including %eflags. That means that single-stepping won't work. Instead, we'll have to modify the signal context that's about to be restored, and set the trace flag there. */ /* First check if PC is at a system call. */ if (deprecated_read_memory_nobpt (pc, buf, LINUX_SYSCALL_LEN) == 0 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0) { int syscall = read_register_pid (LINUX_SYSCALL_REGNUM, pid_to_ptid (pid)); /* Then check the system call number. */ if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn) { CORE_ADDR sp = read_register (I386_ESP_REGNUM); CORE_ADDR addr = sp; unsigned long int eflags; if (syscall == SYS_rt_sigreturn) addr = read_memory_integer (sp + 8, 4) + 20; /* Set the trace flag in the context that's about to be restored. */ addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET; read_memory (addr, (gdb_byte *) &eflags, 4); eflags |= 0x0100; write_memory (addr, (gdb_byte *) &eflags, 4); } } } if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1) perror_with_name (("ptrace")); }
static void i386fbsd_resume (struct target_ops *ops, ptid_t ptid, int step, enum target_signal signal) { pid_t pid = ptid_get_pid (ptid); int request = PT_STEP; if (pid == -1) /* Resume all threads. This only gets used in the non-threaded case, where "resume all threads" and "resume inferior_ptid" are the same. */ pid = ptid_get_pid (inferior_ptid); if (!step) { struct regcache *regcache = get_current_regcache (); ULONGEST eflags; /* Workaround for a bug in FreeBSD. Make sure that the trace flag is off when doing a continue. There is a code path through the kernel which leaves the flag set when it should have been cleared. If a process has a signal pending (such as SIGALRM) and we do a PT_STEP, the process never really has a chance to run because the kernel needs to notify the debugger that a signal is being sent. Therefore, the process never goes through the kernel's trap() function which would normally clear it. */ regcache_cooked_read_unsigned (regcache, I386_EFLAGS_REGNUM, &eflags); if (eflags & 0x0100) regcache_cooked_write_unsigned (regcache, I386_EFLAGS_REGNUM, eflags & ~0x0100); request = PT_CONTINUE; } /* An addres of (caddr_t) 1 tells ptrace to continue from where it was. (If GDB wanted it to start some other way, we have already written a new PC value to the child.) */ if (ptrace (request, pid, (caddr_t) 1, target_signal_to_host (signal)) == -1) perror_with_name (("ptrace")); }
int main (int argc, char *argv[]) { char ch, status, *own_buf, mem_buf[2000]; int i = 0; unsigned char signal; unsigned int len; CORE_ADDR mem_addr; int bad_attach; int pid; char *arg_end; if (setjmp (toplevel)) { fprintf (stderr, "Exiting\n"); exit (1); } bad_attach = 0; pid = 0; attached = 0; if (argc >= 3 && strcmp (argv[2], "--attach") == 0) { if (argc == 4 && argv[3] != '\0' && (pid = strtoul (argv[3], &arg_end, 10)) != 0 && *arg_end == '\0') { ; } else bad_attach = 1; } if (argc < 3 || bad_attach) gdbserver_usage(); initialize_low (); own_buf = malloc (PBUFSIZ); if (pid == 0) { /* Wait till we are at first instruction in program. */ signal = start_inferior (&argv[2], &status); /* We are now stopped at the first instruction of the target process */ } else { switch (attach_inferior (pid, &status, &signal)) { case -1: error ("Attaching not supported on this target"); break; default: attached = 1; break; } } while (1) { remote_open (argv[1]); restart: setjmp (toplevel); while (getpkt (own_buf) > 0) { unsigned char sig; i = 0; ch = own_buf[i++]; switch (ch) { case 'q': handle_query (own_buf); break; case 'd': remote_debug = !remote_debug; break; case 'D': fprintf (stderr, "Detaching from inferior\n"); detach_inferior (); write_ok (own_buf); putpkt (own_buf); remote_close (); /* If we are attached, then we can exit. Otherwise, we need to hang around doing nothing, until the child is gone. */ if (!attached) { int status, ret; do { ret = waitpid (signal_pid, &status, 0); if (WIFEXITED (status) || WIFSIGNALED (status)) break; } while (ret != -1 || errno != ECHILD); } exit (0); case '!': if (attached == 0) { extended_protocol = 1; prepare_resume_reply (own_buf, status, signal); } else { /* We can not use the extended protocol if we are attached, because we can not restart the running program. So return unrecognized. */ own_buf[0] = '\0'; } break; case '?': prepare_resume_reply (own_buf, status, signal); break; case 'H': switch (own_buf[1]) { case 'g': general_thread = strtol (&own_buf[2], NULL, 16); write_ok (own_buf); set_desired_inferior (1); break; case 'c': cont_thread = strtol (&own_buf[2], NULL, 16); write_ok (own_buf); break; case 's': step_thread = strtol (&own_buf[2], NULL, 16); write_ok (own_buf); break; default: /* Silently ignore it so that gdb can extend the protocol without compatibility headaches. */ own_buf[0] = '\0'; break; } break; case 'g': set_desired_inferior (1); registers_to_string (own_buf); break; case 'G': set_desired_inferior (1); registers_from_string (&own_buf[1]); write_ok (own_buf); break; case 'm': decode_m_packet (&own_buf[1], &mem_addr, &len); if (read_inferior_memory (mem_addr, mem_buf, len) == 0) convert_int_to_ascii (mem_buf, own_buf, len); else write_enn (own_buf); break; case 'M': decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf); if (write_inferior_memory (mem_addr, mem_buf, len) == 0) write_ok (own_buf); else write_enn (own_buf); break; case 'C': convert_ascii_to_int (own_buf + 1, &sig, 1); if (target_signal_to_host_p (sig)) signal = target_signal_to_host (sig); else signal = 0; set_desired_inferior (0); myresume (0, signal); signal = mywait (&status, 1); prepare_resume_reply (own_buf, status, signal); break; case 'S': convert_ascii_to_int (own_buf + 1, &sig, 1); if (target_signal_to_host_p (sig)) signal = target_signal_to_host (sig); else signal = 0; set_desired_inferior (0); myresume (1, signal); signal = mywait (&status, 1); prepare_resume_reply (own_buf, status, signal); break; case 'c': set_desired_inferior (0); myresume (0, 0); signal = mywait (&status, 1); prepare_resume_reply (own_buf, status, signal); break; case 's': set_desired_inferior (0); myresume (1, 0); signal = mywait (&status, 1); prepare_resume_reply (own_buf, status, signal); break; case 'k': fprintf (stderr, "Killing inferior\n"); kill_inferior (); /* When using the extended protocol, we start up a new debugging session. The traditional protocol will exit instead. */ if (extended_protocol) { write_ok (own_buf); fprintf (stderr, "GDBserver restarting\n"); /* Wait till we are at 1st instruction in prog. */ signal = start_inferior (&argv[2], &status); goto restart; break; } else { exit (0); break; } case 'T': if (mythread_alive (strtol (&own_buf[1], NULL, 16))) write_ok (own_buf); else write_enn (own_buf); break; case 'R': /* Restarting the inferior is only supported in the extended protocol. */ if (extended_protocol) { kill_inferior (); write_ok (own_buf); fprintf (stderr, "GDBserver restarting\n"); /* Wait till we are at 1st instruction in prog. */ signal = start_inferior (&argv[2], &status); goto restart; break; } else { /* It is a request we don't understand. Respond with an empty packet so that gdb knows that we don't support this request. */ own_buf[0] = '\0'; break; } case 'v': /* Extended (long) request. */ handle_v_requests (own_buf, &status, &signal); break; default: /* It is a request we don't understand. Respond with an empty packet so that gdb knows that we don't support this request. */ own_buf[0] = '\0'; break; } putpkt (own_buf); if (status == 'W') fprintf (stderr, "\nChild exited with status %d\n", signal); if (status == 'X') fprintf (stderr, "\nChild terminated with signal = 0x%x\n", signal); if (status == 'W' || status == 'X') { if (extended_protocol) { fprintf (stderr, "Killing inferior\n"); kill_inferior (); write_ok (own_buf); fprintf (stderr, "GDBserver restarting\n"); /* Wait till we are at 1st instruction in prog. */ signal = start_inferior (&argv[2], &status); goto restart; break; } else { fprintf (stderr, "GDBserver exiting\n"); exit (0); } } } /* We come here when getpkt fails. For the extended remote protocol we exit (and this is the only way we gracefully exit!). For the traditional remote protocol close the connection, and re-open it at the top of the loop. */ if (extended_protocol) { remote_close (); exit (0); } else { fprintf (stderr, "Remote side has terminated connection. " "GDBserver will reopen the connection.\n"); remote_close (); } } }
/* Parse vCont packets. */ void handle_v_cont (char *own_buf, char *status, unsigned char *signal) { char *p, *q; int n = 0, i = 0; struct thread_resume *resume_info, default_action; /* Count the number of semicolons in the packet. There should be one for every action. */ p = &own_buf[5]; while (p) { n++; p++; p = strchr (p, ';'); } /* Allocate room for one extra action, for the default remain-stopped behavior; if no default action is in the list, we'll need the extra slot. */ resume_info = malloc ((n + 1) * sizeof (resume_info[0])); default_action.thread = -1; default_action.leave_stopped = 1; default_action.step = 0; default_action.sig = 0; p = &own_buf[5]; i = 0; while (*p) { p++; resume_info[i].leave_stopped = 0; if (p[0] == 's' || p[0] == 'S') resume_info[i].step = 1; else if (p[0] == 'c' || p[0] == 'C') resume_info[i].step = 0; else goto err; if (p[0] == 'S' || p[0] == 'C') { int sig; sig = strtol (p + 1, &q, 16); if (p == q) goto err; p = q; if (!target_signal_to_host_p (sig)) goto err; resume_info[i].sig = target_signal_to_host (sig); } else { resume_info[i].sig = 0; p = p + 1; } if (p[0] == 0) { resume_info[i].thread = -1; default_action = resume_info[i]; /* Note: we don't increment i here, we'll overwrite this entry the next time through. */ } else if (p[0] == ':') { resume_info[i].thread = strtol (p + 1, &q, 16); if (p == q) goto err; p = q; if (p[0] != ';' && p[0] != 0) goto err; i++; } } resume_info[i] = default_action; /* Still used in occasional places in the backend. */ if (n == 1 && resume_info[0].thread != -1) cont_thread = resume_info[0].thread; else cont_thread = -1; set_desired_inferior (0); (*the_target->resume) (resume_info); free (resume_info); *signal = mywait (status, 1); prepare_resume_reply (own_buf, *status, *signal); return; err: /* No other way to report an error... */ strcpy (own_buf, ""); free (resume_info); return; }
static ptid_t gdbsim_wait (ptid_t ptid, struct target_waitstatus *status) { static RETSIGTYPE (*prev_sigint) (); int sigrc = 0; enum sim_stop reason = sim_running; if (sr_get_debug ()) printf_filtered ("gdbsim_wait\n"); #if defined (HAVE_SIGACTION) && defined (SA_RESTART) { struct sigaction sa, osa; sa.sa_handler = gdbsim_cntrl_c; sigemptyset (&sa.sa_mask); sa.sa_flags = 0; sigaction (SIGINT, &sa, &osa); prev_sigint = osa.sa_handler; } #else prev_sigint = signal (SIGINT, gdbsim_cntrl_c); #endif sim_resume (gdbsim_desc, resume_step, target_signal_to_host (resume_siggnal)); signal (SIGINT, prev_sigint); resume_step = 0; sim_stop_reason (gdbsim_desc, &reason, &sigrc); switch (reason) { case sim_exited: status->kind = TARGET_WAITKIND_EXITED; status->value.integer = sigrc; break; case sim_stopped: switch (sigrc) { case SIGABRT: quit (); break; case SIGINT: case SIGTRAP: default: status->kind = TARGET_WAITKIND_STOPPED; /* The signal in sigrc is a host signal. That probably should be fixed. */ status->value.sig = target_signal_from_host (sigrc); break; } break; case sim_signalled: status->kind = TARGET_WAITKIND_SIGNALLED; /* The signal in sigrc is a host signal. That probably should be fixed. */ status->value.sig = target_signal_from_host (sigrc); break; case sim_running: case sim_polling: /* FIXME: Is this correct? */ break; } return inferior_ptid; }
void require_notification_of_events (int pid) { #if defined(PT_SET_EVENT_MASK) int pt_status; ptrace_event_t ptrace_events; int nsigs; int signum; /* Instruct the kernel as to the set of events we wish to be informed of. (This support does not exist before HPUX 10.0. We'll assume if PT_SET_EVENT_MASK has not been defined by <sys/ptrace.h>, then we're being built on pre-10.0.) */ memset (&ptrace_events, 0, sizeof (ptrace_events)); /* Note: By default, all signals are visible to us. If we wish the kernel to keep certain signals hidden from us, we do it by calling sigdelset (ptrace_events.pe_signals, signal) for each such signal here, before doing PT_SET_EVENT_MASK. */ /* RM: The above comment is no longer true. We start with ignoring all signals, and then add the ones we are interested in. We could do it the other way: start by looking at all signals and then deleting the ones that we aren't interested in, except that multiple gdb signals may be mapped to the same host signal (eg. TARGET_SIGNAL_IO and TARGET_SIGNAL_POLL both get mapped to signal 22 on HPUX 10.20) We want to be notified if we are interested in either signal. */ sigfillset (&ptrace_events.pe_signals); /* RM: Let's not bother with signals we don't care about */ nsigs = (int) TARGET_SIGNAL_LAST; for (signum = nsigs; signum > 0; signum--) { if ((signal_stop_state (signum)) || (signal_print_state (signum)) || (!signal_pass_state (signum))) { if (target_signal_to_host_p (signum)) sigdelset (&ptrace_events.pe_signals, target_signal_to_host (signum)); } } ptrace_events.pe_set_event = 0; ptrace_events.pe_set_event |= PTRACE_SIGNAL; ptrace_events.pe_set_event |= PTRACE_EXEC; ptrace_events.pe_set_event |= PTRACE_FORK; ptrace_events.pe_set_event |= PTRACE_VFORK; /* ??rehrauer: Add this one when we're prepared to catch it... ptrace_events.pe_set_event |= PTRACE_EXIT; */ errno = 0; pt_status = call_ptrace (PT_SET_EVENT_MASK, pid, (PTRACE_ARG3_TYPE) & ptrace_events, sizeof (ptrace_events)); if (errno) perror_with_name ("ptrace"); if (pt_status < 0) return; #endif }
int default_target_signal_to_host (struct gdbarch *gdbarch, enum target_signal ts) { return target_signal_to_host (ts); }
static void i386_linux_resume (struct target_ops *ops, ptid_t ptid, int step, enum target_signal signal) { int pid = PIDGET (ptid); int request; if (catch_syscall_enabled () > 0) request = PTRACE_SYSCALL; else request = PTRACE_CONT; if (step) { struct regcache *regcache = get_thread_regcache (pid_to_ptid (pid)); struct gdbarch *gdbarch = get_regcache_arch (regcache); enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); ULONGEST pc; gdb_byte buf[LINUX_SYSCALL_LEN]; request = PTRACE_SINGLESTEP; regcache_cooked_read_unsigned (regcache, gdbarch_pc_regnum (gdbarch), &pc); /* Returning from a signal trampoline is done by calling a special system call (sigreturn or rt_sigreturn, see i386-linux-tdep.c for more information). This system call restores the registers that were saved when the signal was raised, including %eflags. That means that single-stepping won't work. Instead, we'll have to modify the signal context that's about to be restored, and set the trace flag there. */ /* First check if PC is at a system call. */ if (target_read_memory (pc, buf, LINUX_SYSCALL_LEN) == 0 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0) { ULONGEST syscall; regcache_cooked_read_unsigned (regcache, LINUX_SYSCALL_REGNUM, &syscall); /* Then check the system call number. */ if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn) { ULONGEST sp, addr; unsigned long int eflags; regcache_cooked_read_unsigned (regcache, I386_ESP_REGNUM, &sp); if (syscall == SYS_rt_sigreturn) addr = read_memory_unsigned_integer (sp + 8, 4, byte_order) + 20; else addr = sp; /* Set the trace flag in the context that's about to be restored. */ addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET; read_memory (addr, (gdb_byte *) &eflags, 4); eflags |= 0x0100; write_memory (addr, (gdb_byte *) &eflags, 4); } } } if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1) perror_with_name (("ptrace")); }
void server_main (void) { static char status; static int zignal; char ch; int i = 0; unsigned int len; CORE_ADDR mem_addr; zignal = valgrind_wait (&status); if (VG_MINIMAL_SETJMP(toplevel)) { dlog(0, "error caused VG_MINIMAL_LONGJMP to server_main\n"); } while (1) { unsigned char sig; int packet_len; int new_packet_len = -1; if (resume_reply_packet_needed) { /* Send the resume reply to reply to last GDB resume request. */ resume_reply_packet_needed = False; prepare_resume_reply (own_buf, status, zignal); putpkt (own_buf); } /* If we our status is terminal (exit or fatal signal) get out as quickly as we can. We won't be able to handle any request anymore. */ if (status == 'W' || status == 'X') { return; } packet_len = getpkt (own_buf); if (packet_len <= 0) break; i = 0; ch = own_buf[i++]; switch (ch) { case 'Q': handle_set (own_buf, &new_packet_len); break; case 'q': handle_query (own_buf, &new_packet_len); break; case 'd': /* set/unset debugging is done through valgrind debug level. */ own_buf[0] = '\0'; break; case 'D': reset_valgrind_sink("gdb detaching from process"); /* When detaching or kill the process, gdb expects to get an packet OK back. Any other output will make gdb believes detach did not work. */ write_ok (own_buf); putpkt (own_buf); remote_finish (reset_after_error); remote_open (VG_(clo_vgdb_prefix)); myresume (0, 0); resume_reply_packet_needed = False; return; case '!': /* We can not use the extended protocol with valgrind, because we can not restart the running program. So return unrecognized. */ own_buf[0] = '\0'; break; case '?': prepare_resume_reply (own_buf, status, zignal); break; case 'H': if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') { unsigned long gdb_id, thread_id; gdb_id = strtoul (&own_buf[2], NULL, 16); thread_id = gdb_id_to_thread_id (gdb_id); if (thread_id == 0) { write_enn (own_buf); break; } if (own_buf[1] == 'g') { general_thread = thread_id; set_desired_inferior (1); } else if (own_buf[1] == 'c') { cont_thread = thread_id; } else if (own_buf[1] == 's') { step_thread = thread_id; } write_ok (own_buf); } else { /* Silently ignore it so that gdb can extend the protocol without compatibility headaches. */ own_buf[0] = '\0'; } break; case 'g': set_desired_inferior (1); registers_to_string (own_buf); break; case 'G': set_desired_inferior (1); registers_from_string (&own_buf[1]); write_ok (own_buf); break; case 'P': { int regno; char *regbytes; Bool mod; ThreadState *tst; regno = strtol(&own_buf[1], NULL, 16); regbytes = strchr(&own_buf[0], '=') + 1; set_desired_inferior (1); tst = (ThreadState *) inferior_target_data (current_inferior); /* Only accept changing registers in "runnable state3. In fact, it would be ok to change most of the registers except a few "sensitive" registers such as the PC, SP, BP. We assume we do not need to very specific here, and that we can just refuse all of these. */ if (tst->status == VgTs_Runnable || tst->status == VgTs_Yielding) { supply_register_from_string (regno, regbytes, &mod); write_ok (own_buf); } else { /* at least from gdb 6.6 onwards, an E. error reply is shown to the user. So, we do an error msg which both is accepted by gdb as an error msg and is readable by the user. */ VG_(sprintf) (own_buf, "E.\n" "ERROR changing register %s regno %d\n" "gdb commands changing registers (pc, sp, ...) (e.g. 'jump',\n" "set pc, calling from gdb a function in the debugged process, ...)\n" "can only be accepted if the thread is VgTs_Runnable or VgTs_Yielding state\n" "Thread status is %s\n", find_register_by_number (regno)->name, regno, VG_(name_of_ThreadStatus)(tst->status)); if (VG_(clo_verbosity) > 1) VG_(umsg) ("%s\n", own_buf); } break; } case 'm': decode_m_packet (&own_buf[1], &mem_addr, &len); if (valgrind_read_memory (mem_addr, mem_buf, len) == 0) convert_int_to_ascii (mem_buf, own_buf, len); else write_enn (own_buf); break; case 'M': decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf); if (valgrind_write_memory (mem_addr, mem_buf, len) == 0) write_ok (own_buf); else write_enn (own_buf); break; case 'X': if (decode_X_packet (&own_buf[1], packet_len - 1, &mem_addr, &len, mem_buf) < 0 || valgrind_write_memory (mem_addr, mem_buf, len) != 0) write_enn (own_buf); else write_ok (own_buf); break; case 'C': convert_ascii_to_int (own_buf + 1, &sig, 1); if (target_signal_to_host_p (sig)) zignal = target_signal_to_host (sig); else zignal = 0; set_desired_inferior (0); myresume (0, zignal); return; // return control to valgrind case 'S': convert_ascii_to_int (own_buf + 1, &sig, 1); if (target_signal_to_host_p (sig)) zignal = target_signal_to_host (sig); else zignal = 0; set_desired_inferior (0); myresume (1, zignal); return; // return control to valgrind case 'c': set_desired_inferior (0); myresume (0, 0); return; // return control to valgrind case 's': set_desired_inferior (0); myresume (1, 0); return; // return control to valgrind case 'Z': { char *lenptr; char *dataptr; CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); int zlen = strtol (lenptr + 1, &dataptr, 16); char type = own_buf[1]; if (type < '0' || type > '4') { /* Watchpoint command type unrecognized. */ own_buf[0] = '\0'; } else { int res; res = valgrind_insert_watchpoint (type, addr, zlen); if (res == 0) write_ok (own_buf); else if (res == 1) /* Unsupported. */ own_buf[0] = '\0'; else write_enn (own_buf); } break; } case 'z': { char *lenptr; char *dataptr; CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); int zlen = strtol (lenptr + 1, &dataptr, 16); char type = own_buf[1]; if (type < '0' || type > '4') { /* Watchpoint command type unrecognized. */ own_buf[0] = '\0'; } else { int res; res = valgrind_remove_watchpoint (type, addr, zlen); if (res == 0) write_ok (own_buf); else if (res == 1) /* Unsupported. */ own_buf[0] = '\0'; else write_enn (own_buf); } break; } case 'k': kill_request("Gdb request to kill this process\n"); break; case 'T': { unsigned long gdb_id, thread_id; gdb_id = strtoul (&own_buf[1], NULL, 16); thread_id = gdb_id_to_thread_id (gdb_id); if (thread_id == 0) { write_enn (own_buf); break; } if (valgrind_thread_alive (thread_id)) write_ok (own_buf); else write_enn (own_buf); break; } case 'R': /* Restarting the inferior is only supported in the extended protocol. => It is a request we don't understand. Respond with an empty packet so that gdb knows that we don't support this request. */ own_buf[0] = '\0'; break; case 'v': /* Extended (long) request. */ handle_v_requests (own_buf, &status, &zignal); break; default: /* It is a request we don't understand. Respond with an empty packet so that gdb knows that we don't support this request. */ own_buf[0] = '\0'; break; } if (new_packet_len != -1) putpkt_binary (own_buf, new_packet_len); else putpkt (own_buf); if (status == 'W') VG_(umsg) ("\nChild exited with status %d\n", zignal); if (status == 'X') VG_(umsg) ("\nChild terminated with signal = 0x%x (%s)\n", target_signal_to_host (zignal), target_signal_to_name (zignal)); if (status == 'W' || status == 'X') { VG_(umsg) ("Process exiting\n"); VG_(exit) (0); } } /* We come here when getpkt fails => close the connection, and re-open. Then return control to valgrind. We return the control to valgrind as we assume that the connection was closed due to vgdb having finished to execute a command. */ if (VG_(clo_verbosity) > 1) VG_(umsg) ("Remote side has terminated connection. " "GDBserver will reopen the connection.\n"); remote_finish (reset_after_error); remote_open (VG_(clo_vgdb_prefix)); myresume (0, 0); resume_reply_packet_needed = False; return; }
int main (int argc, char *argv[]) { char ch, status, *own_buf; unsigned char *mem_buf; int i = 0; int signal; unsigned int len; CORE_ADDR mem_addr; int bad_attach; int pid; char *arg_end; my_stdout = stdout; my_stderr = stderr; myname = argv[0]; if (argc >= 2 && strcmp (argv[1], "--version") == 0) { gdbserver_version (); exit (0); } if (argc >= 2 && strcmp (argv[1], "--help") == 0) { gdbserver_usage (); exit (0); } if (setjmp (toplevel)) { warning ("Exiting"); exit (1); } bad_attach = 0; pid = 0; attached = 0; if (argc >= 3 && strcmp (argv[2], "--attach") == 0) { if (argc == 4 && argv[3][0] != '\0' && (pid = strtoul (argv[3], &arg_end, 10)) != 0 && *arg_end == '\0') { ; } else bad_attach = 1; } if (argc < 3 || bad_attach) { gdbserver_usage (); exit (1); } if (strcmp (argv[1], "pipe") == 0) { my_stdout = my_stderr = stderr; } initialize_low (); own_buf = malloc (PBUFSIZ + 1); mem_buf = malloc (PBUFSIZ); if (pid == 0) { /* Wait till we are at first instruction in program. */ signal = start_inferior (&argv[2], &status); /* We are now (hopefully) stopped at the first instruction of the target process. This assumes that the target process was successfully created. */ /* Don't report shared library events on the initial connection, even if some libraries are preloaded. */ dlls_changed = 0; } else { switch (attach_inferior (pid, &status, &signal)) { case -1: error ("Attaching not supported on this target"); break; default: attached = 1; break; } } if (setjmp (toplevel)) { warning ("Killing inferior"); kill_inferior (); exit (1); } if (status == 'W' || status == 'X') { warning ("No inferior, GDBserver exiting."); exit (1); } while (1) { remote_open (argv[1]); restart: if (setjmp (toplevel)) { if (remote_debug) printf_filtered ("gdbserver: error returned to main loop\n"); write_enn (own_buf); putpkt (own_buf); } while (1) { unsigned char sig; int packet_len; int new_packet_len = -1; packet_len = getpkt (own_buf, PBUFSIZ); if (packet_len <= 0) break; i = 0; ch = own_buf[i++]; switch (ch) { case 'q': handle_query (own_buf, packet_len, &new_packet_len); break; case 'Q': handle_general_set (own_buf); break; case 'D': warning ("Detaching from inferior"); if (detach_inferior () != 0) { write_enn (own_buf); putpkt (own_buf); } else { write_ok (own_buf); putpkt (own_buf); remote_close (); /* If we are attached, then we can exit. Otherwise, we need to hang around doing nothing, until the child is gone. */ if (!attached) join_inferior (); exit (0); } case '!': if (attached == 0) { extended_protocol = 1; prepare_resume_reply (own_buf, status, signal); } else { /* We can not use the extended protocol if we are attached, because we can not restart the running program. So return unrecognized. */ own_buf[0] = '\0'; } break; case '?': prepare_resume_reply (own_buf, status, signal); break; case 'H': if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') { unsigned long gdb_id, thread_id; gdb_id = strtoul (&own_buf[2], NULL, 16); thread_id = gdb_id_to_thread_id (gdb_id); if (thread_id == 0) { write_enn (own_buf); break; } if (own_buf[1] == 'g') { general_thread = thread_id; set_desired_inferior (1); } else if (own_buf[1] == 'c') cont_thread = thread_id; else if (own_buf[1] == 's') step_thread = thread_id; write_ok (own_buf); } else { /* Silently ignore it so that gdb can extend the protocol without compatibility headaches. */ own_buf[0] = '\0'; } break; case 'g': set_desired_inferior (1); registers_to_string (own_buf); break; case 'G': set_desired_inferior (1); registers_from_string (&own_buf[1]); write_ok (own_buf); break; case 'm': decode_m_packet (&own_buf[1], &mem_addr, &len); if (read_inferior_memory (mem_addr, mem_buf, len) == 0) convert_int_to_ascii (mem_buf, own_buf, len); else write_enn (own_buf); break; case 'M': decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf); if (write_inferior_memory (mem_addr, mem_buf, len) == 0) write_ok (own_buf); else write_enn (own_buf); break; case 'X': if (decode_X_packet (&own_buf[1], packet_len - 1, &mem_addr, &len, mem_buf) < 0 || write_inferior_memory (mem_addr, mem_buf, len) != 0) write_enn (own_buf); else write_ok (own_buf); break; case 'C': convert_ascii_to_int (own_buf + 1, &sig, 1); if (target_signal_to_host_p (sig)) signal = target_signal_to_host (sig); else signal = 0; set_desired_inferior (0); myresume (0, signal); signal = mywait (&status, 1); prepare_resume_reply (own_buf, status, signal); break; case 'S': convert_ascii_to_int (own_buf + 1, &sig, 1); if (target_signal_to_host_p (sig)) signal = target_signal_to_host (sig); else signal = 0; set_desired_inferior (0); myresume (1, signal); signal = mywait (&status, 1); prepare_resume_reply (own_buf, status, signal); break; case 'c': set_desired_inferior (0); myresume (0, 0); signal = mywait (&status, 1); prepare_resume_reply (own_buf, status, signal); break; case 's': set_desired_inferior (0); myresume (1, 0); signal = mywait (&status, 1); prepare_resume_reply (own_buf, status, signal); break; case 'z': case 'Z': { char *lenptr; char *dataptr; CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); int len = strtol (lenptr + 1, &dataptr, 16); char type = own_buf[1]; if (the_target->insert_watchpoint == NULL || the_target->remove_watchpoint == NULL || (type < '0' || type > '4')) { /* No watchpoint support or not a watchpoint command; unrecognized either way. */ own_buf[0] = '\0'; } else { int res; if (ch == 'z') res = (*the_target->remove_watchpoint) (type, addr, len); else res = (*the_target->insert_watchpoint) (type, addr, len); if (res == 0) write_ok (own_buf); else if (res == 1) /* Unsupported. */ own_buf[0] = '\0'; else write_enn (own_buf); } break; } case 'k': warning ("Killing inferior"); kill_inferior (); /* When using the extended protocol, we start up a new debugging session. The traditional protocol will exit instead. */ if (extended_protocol) { write_ok (own_buf); warning ("GDBserver restarting"); /* Wait till we are at 1st instruction in prog. */ signal = start_inferior (&argv[2], &status); goto restart; break; } else { exit (0); break; } case 'T': { unsigned long gdb_id, thread_id; gdb_id = strtoul (&own_buf[1], NULL, 16); thread_id = gdb_id_to_thread_id (gdb_id); if (thread_id == 0) { write_enn (own_buf); break; } if (mythread_alive (thread_id)) write_ok (own_buf); else write_enn (own_buf); } break; case 'R': /* Restarting the inferior is only supported in the extended protocol. */ if (extended_protocol) { kill_inferior (); write_ok (own_buf); warning ("GDBserver restarting"); /* Wait till we are at 1st instruction in prog. */ signal = start_inferior (&argv[2], &status); goto restart; break; } else { /* It is a request we don't understand. Respond with an empty packet so that gdb knows that we don't support this request. */ own_buf[0] = '\0'; break; } case 'v': /* Extended (long) request. */ handle_v_requests (own_buf, &status, &signal); break; default: /* It is a request we don't understand. Respond with an empty packet so that gdb knows that we don't support this request. */ own_buf[0] = '\0'; break; } if (new_packet_len != -1) putpkt_binary (own_buf, new_packet_len); else putpkt (own_buf); if (status == 'W') warning ("\nChild exited with status %d", signal); if (status == 'X') warning ("\nChild terminated with signal = 0x%x (%s)", target_signal_to_host (signal), target_signal_to_name (signal)); if (status == 'W' || status == 'X') { if (extended_protocol) { warning ("Killing inferior"); kill_inferior (); write_ok (own_buf); warning ("GDBserver restarting"); /* Wait till we are at 1st instruction in prog. */ signal = start_inferior (&argv[2], &status); goto restart; break; } else { warning ("GDBserver exiting"); exit (0); } } } /* We come here when getpkt fails. For the extended remote protocol we exit (and this is the only way we gracefully exit!). For the traditional remote protocol close the connection, and re-open it at the top of the loop. */ if (extended_protocol) { remote_close (); exit (0); } else { warning ("Remote side has terminated connection. " "GDBserver will reopen the connection."); remote_close (); } } }
int main (int argc, char *argv[]) { char ch, status, *own_buf; unsigned char *mem_buf; int i = 0; int signal; unsigned int len; CORE_ADDR mem_addr; int bad_attach; int pid; char *arg_end, *port; char **next_arg = &argv[1]; int multi_mode = 0; int attach = 0; int was_running; while (*next_arg != NULL && **next_arg == '-') { if (strcmp (*next_arg, "--version") == 0) { gdbserver_version (); exit (0); } else if (strcmp (*next_arg, "--help") == 0) { gdbserver_usage (stdout); exit (0); } else if (strcmp (*next_arg, "--attach") == 0) attach = 1; else if (strcmp (*next_arg, "--multi") == 0) multi_mode = 1; else if (strcmp (*next_arg, "--wrapper") == 0) { next_arg++; wrapper_argv = next_arg; while (*next_arg != NULL && strcmp (*next_arg, "--") != 0) next_arg++; if (next_arg == wrapper_argv || *next_arg == NULL) { gdbserver_usage (stderr); exit (1); } /* Consume the "--". */ *next_arg = NULL; } else if (strcmp (*next_arg, "--debug") == 0) debug_threads = 1; else if (strcmp (*next_arg, "--disable-packet") == 0) { gdbserver_show_disableable (stdout); exit (0); } else if (strncmp (*next_arg, "--disable-packet=", sizeof ("--disable-packet=") - 1) == 0) { char *packets, *tok; packets = *next_arg += sizeof ("--disable-packet=") - 1; for (tok = strtok (packets, ","); tok != NULL; tok = strtok (NULL, ",")) { if (strcmp ("vCont", tok) == 0) disable_packet_vCont = 1; else if (strcmp ("Tthread", tok) == 0) disable_packet_Tthread = 1; else if (strcmp ("qC", tok) == 0) disable_packet_qC = 1; else if (strcmp ("qfThreadInfo", tok) == 0) disable_packet_qfThreadInfo = 1; else if (strcmp ("threads", tok) == 0) { disable_packet_vCont = 1; disable_packet_Tthread = 1; disable_packet_qC = 1; disable_packet_qfThreadInfo = 1; } else { fprintf (stderr, "Don't know how to disable \"%s\".\n\n", tok); gdbserver_show_disableable (stderr); exit (1); } } } else { fprintf (stderr, "Unknown argument: %s\n", *next_arg); exit (1); } next_arg++; continue; } if (setjmp (toplevel)) { fprintf (stderr, "Exiting\n"); exit (1); } port = *next_arg; next_arg++; if (port == NULL || (!attach && !multi_mode && *next_arg == NULL)) { gdbserver_usage (stderr); exit (1); } bad_attach = 0; pid = 0; /* --attach used to come after PORT, so allow it there for compatibility. */ if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0) { attach = 1; next_arg++; } if (attach && (*next_arg == NULL || (*next_arg)[0] == '\0' || (pid = strtoul (*next_arg, &arg_end, 0)) == 0 || *arg_end != '\0' || next_arg[1] != NULL)) bad_attach = 1; if (bad_attach) { gdbserver_usage (stderr); exit (1); } initialize_async_io (); initialize_low (); own_buf = malloc (PBUFSIZ + 1); mem_buf = malloc (PBUFSIZ); if (pid == 0 && *next_arg != NULL) { int i, n; n = argc - (next_arg - argv); program_argv = malloc (sizeof (char *) * (n + 1)); for (i = 0; i < n; i++) program_argv[i] = strdup (next_arg[i]); program_argv[i] = NULL; /* Wait till we are at first instruction in program. */ signal = start_inferior (program_argv, &status); /* We are now (hopefully) stopped at the first instruction of the target process. This assumes that the target process was successfully created. */ } else if (pid != 0) { if (attach_inferior (pid, &status, &signal) == -1) error ("Attaching not supported on this target"); /* Otherwise succeeded. */ } else { status = 'W'; signal = 0; } /* Don't report shared library events on the initial connection, even if some libraries are preloaded. Avoids the "stopped by shared library event" notice on gdb side. */ dlls_changed = 0; if (setjmp (toplevel)) { fprintf (stderr, "Killing inferior\n"); kill_inferior (); exit (1); } if (status == 'W' || status == 'X') was_running = 0; else was_running = 1; if (!was_running && !multi_mode) { fprintf (stderr, "No program to debug. GDBserver exiting.\n"); exit (1); } while (1) { noack_mode = 0; remote_open (port); restart: if (setjmp (toplevel) != 0) { /* An error occurred. */ if (response_needed) { write_enn (own_buf); putpkt (own_buf); } } disable_async_io (); while (!exit_requested) { unsigned char sig; int packet_len; int new_packet_len = -1; response_needed = 0; packet_len = getpkt (own_buf); if (packet_len <= 0) break; response_needed = 1; i = 0; ch = own_buf[i++]; switch (ch) { case 'q': handle_query (own_buf, packet_len, &new_packet_len); break; case 'Q': handle_general_set (own_buf); break; case 'D': require_running (own_buf); fprintf (stderr, "Detaching from inferior\n"); if (detach_inferior () != 0) write_enn (own_buf); else { write_ok (own_buf); if (extended_protocol) { /* Treat this like a normal program exit. */ signal = 0; status = 'W'; } else { putpkt (own_buf); remote_close (); /* If we are attached, then we can exit. Otherwise, we need to hang around doing nothing, until the child is gone. */ if (!attached) join_inferior (); exit (0); } } break; case '!': extended_protocol = 1; write_ok (own_buf); break; case '?': prepare_resume_reply (own_buf, status, signal); break; case 'H': if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') { unsigned long gdb_id, thread_id; require_running (own_buf); gdb_id = strtoul (&own_buf[2], NULL, 16); if (gdb_id == 0 || gdb_id == -1) thread_id = gdb_id; else { thread_id = gdb_id_to_thread_id (gdb_id); if (thread_id == 0) { write_enn (own_buf); break; } } if (own_buf[1] == 'g') { general_thread = thread_id; set_desired_inferior (1); } else if (own_buf[1] == 'c') cont_thread = thread_id; else if (own_buf[1] == 's') step_thread = thread_id; write_ok (own_buf); } else { /* Silently ignore it so that gdb can extend the protocol without compatibility headaches. */ own_buf[0] = '\0'; } break; case 'g': require_running (own_buf); set_desired_inferior (1); registers_to_string (own_buf); break; case 'G': require_running (own_buf); set_desired_inferior (1); registers_from_string (&own_buf[1]); write_ok (own_buf); break; case 'm': require_running (own_buf); decode_m_packet (&own_buf[1], &mem_addr, &len); if (read_inferior_memory (mem_addr, mem_buf, len) == 0) convert_int_to_ascii (mem_buf, own_buf, len); else write_enn (own_buf); break; case 'M': require_running (own_buf); decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf); if (write_inferior_memory (mem_addr, mem_buf, len) == 0) write_ok (own_buf); else write_enn (own_buf); break; case 'X': require_running (own_buf); if (decode_X_packet (&own_buf[1], packet_len - 1, &mem_addr, &len, mem_buf) < 0 || write_inferior_memory (mem_addr, mem_buf, len) != 0) write_enn (own_buf); else write_ok (own_buf); break; case 'C': require_running (own_buf); convert_ascii_to_int (own_buf + 1, &sig, 1); if (target_signal_to_host_p (sig)) signal = target_signal_to_host (sig); else signal = 0; myresume (own_buf, 0, &signal, &status); break; case 'S': require_running (own_buf); convert_ascii_to_int (own_buf + 1, &sig, 1); if (target_signal_to_host_p (sig)) signal = target_signal_to_host (sig); else signal = 0; myresume (own_buf, 1, &signal, &status); break; case 'c': require_running (own_buf); signal = 0; myresume (own_buf, 0, &signal, &status); break; case 's': require_running (own_buf); signal = 0; myresume (own_buf, 1, &signal, &status); break; case 'Z': { char *lenptr; char *dataptr; CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); int len = strtol (lenptr + 1, &dataptr, 16); char type = own_buf[1]; if (the_target->insert_watchpoint == NULL || (type < '2' || type > '4')) { /* No watchpoint support or not a watchpoint command; unrecognized either way. */ own_buf[0] = '\0'; } else { int res; require_running (own_buf); res = (*the_target->insert_watchpoint) (type, addr, len); if (res == 0) write_ok (own_buf); else if (res == 1) /* Unsupported. */ own_buf[0] = '\0'; else write_enn (own_buf); } break; } case 'z': { char *lenptr; char *dataptr; CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); int len = strtol (lenptr + 1, &dataptr, 16); char type = own_buf[1]; if (the_target->remove_watchpoint == NULL || (type < '2' || type > '4')) { /* No watchpoint support or not a watchpoint command; unrecognized either way. */ own_buf[0] = '\0'; } else { int res; require_running (own_buf); res = (*the_target->remove_watchpoint) (type, addr, len); if (res == 0) write_ok (own_buf); else if (res == 1) /* Unsupported. */ own_buf[0] = '\0'; else write_enn (own_buf); } break; } case 'k': response_needed = 0; if (!target_running ()) /* The packet we received doesn't make sense - but we can't reply to it, either. */ goto restart; fprintf (stderr, "Killing inferior\n"); kill_inferior (); /* When using the extended protocol, we wait with no program running. The traditional protocol will exit instead. */ if (extended_protocol) { status = 'X'; signal = TARGET_SIGNAL_KILL; was_running = 0; goto restart; } else { exit (0); break; } case 'T': { unsigned long gdb_id, thread_id; require_running (own_buf); gdb_id = strtoul (&own_buf[1], NULL, 16); thread_id = gdb_id_to_thread_id (gdb_id); if (thread_id == 0) { write_enn (own_buf); break; } if (mythread_alive (thread_id)) write_ok (own_buf); else write_enn (own_buf); } break; case 'R': response_needed = 0; /* Restarting the inferior is only supported in the extended protocol. */ if (extended_protocol) { if (target_running ()) kill_inferior (); fprintf (stderr, "GDBserver restarting\n"); /* Wait till we are at 1st instruction in prog. */ if (program_argv != NULL) signal = start_inferior (program_argv, &status); else { status = 'X'; signal = TARGET_SIGNAL_KILL; } goto restart; } else { /* It is a request we don't understand. Respond with an empty packet so that gdb knows that we don't support this request. */ own_buf[0] = '\0'; break; } case 'v': /* Extended (long) request. */ handle_v_requests (own_buf, &status, &signal, packet_len, &new_packet_len); break; default: /* It is a request we don't understand. Respond with an empty packet so that gdb knows that we don't support this request. */ own_buf[0] = '\0'; break; } if (new_packet_len != -1) putpkt_binary (own_buf, new_packet_len); else putpkt (own_buf); response_needed = 0; if (was_running && (status == 'W' || status == 'X')) { was_running = 0; if (status == 'W') fprintf (stderr, "\nChild exited with status %d\n", signal); if (status == 'X') fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n", target_signal_to_host (signal), target_signal_to_name (signal)); if (extended_protocol) goto restart; else { fprintf (stderr, "GDBserver exiting\n"); exit (0); } } if (status != 'W' && status != 'X') was_running = 1; } /* If an exit was requested (using the "monitor exit" command), terminate now. The only other way to get here is for getpkt to fail; close the connection and reopen it at the top of the loop. */ if (exit_requested) { remote_close (); if (attached && target_running ()) detach_inferior (); else if (target_running ()) kill_inferior (); exit (0); } else { fprintf (stderr, "Remote side has terminated connection. " "GDBserver will reopen the connection.\n"); remote_close (); } } }
void gdbserver_main (void) { CORE_ADDR mem_addr; char *own_buf; unsigned char *mem_buf; int i = 0; unsigned int len; own_buf = malloc (PBUFSIZ + 1); mem_buf = malloc (PBUFSIZ); while (1) { remote_open (port); restart: #if 0 if (setjmp (toplevel) != 0) { /* An error occurred. */ if (response_needed) { write_enn (own_buf); putpkt (own_buf); } } #endif disable_async_io (); while (!exit_requested) { unsigned char sig; int packet_len; int new_packet_len = -1; response_needed = 0; packet_len = getpkt (own_buf); if (packet_len <= 0) break; response_needed = 1; i = 0; ch = own_buf[i++]; switch (ch) { case 'q': handle_query (own_buf, packet_len, &new_packet_len); break; case 'Q': handle_general_set (own_buf); break; case 'D': require_running (own_buf); fprintf (stderr, "Detaching from inferior\n"); if (detach_inferior () != 0) write_enn (own_buf); else { write_ok (own_buf); if (extended_protocol) { /* Treat this like a normal program exit. */ signal = 0; status = 'W'; } else { putpkt (own_buf); remote_close (); /* If we are attached, then we can exit. Otherwise, we need to hang around doing nothing, until the child is gone. */ if (!attached) join_inferior (); exit (0); } } break; case '!': extended_protocol = 1; write_ok (own_buf); break; case '?': prepare_resume_reply (own_buf, status, signal); break; case 'H': if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') { unsigned long gdb_id, thread_id; require_running (own_buf); gdb_id = strtoul (&own_buf[2], NULL, 16); if (gdb_id == 0 || gdb_id == -1) thread_id = gdb_id; else { thread_id = gdb_id_to_thread_id (gdb_id); if (thread_id == 0) { write_enn (own_buf); break; } } if (own_buf[1] == 'g') { general_thread = thread_id; set_desired_inferior (1); } else if (own_buf[1] == 'c') cont_thread = thread_id; else if (own_buf[1] == 's') step_thread = thread_id; write_ok (own_buf); } else { /* Silently ignore it so that gdb can extend the protocol without compatibility headaches. */ own_buf[0] = '\0'; } break; case 'g': require_running (own_buf); set_desired_inferior (1); registers_to_string (own_buf); break; case 'G': require_running (own_buf); set_desired_inferior (1); registers_from_string (&own_buf[1]); write_ok (own_buf); break; case 'm': require_running (own_buf); decode_m_packet (&own_buf[1], &mem_addr, &len); if (read_inferior_memory (mem_addr, mem_buf, len) == 0) convert_int_to_ascii (mem_buf, own_buf, len); else write_enn (own_buf); break; case 'M': require_running (own_buf); decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf); if (write_inferior_memory (mem_addr, mem_buf, len) == 0) write_ok (own_buf); else write_enn (own_buf); break; case 'X': require_running (own_buf); if (decode_X_packet (&own_buf[1], packet_len - 1, &mem_addr, &len, mem_buf) < 0 || write_inferior_memory (mem_addr, mem_buf, len) != 0) write_enn (own_buf); else write_ok (own_buf); break; case 'C': require_running (own_buf); convert_ascii_to_int (own_buf + 1, &sig, 1); if (target_signal_to_host_p (sig)) signal = target_signal_to_host (sig); else signal = 0; myresume (own_buf, 0, &signal, &status); break; case 'S': require_running (own_buf); convert_ascii_to_int (own_buf + 1, &sig, 1); if (target_signal_to_host_p (sig)) signal = target_signal_to_host (sig); else signal = 0; myresume (own_buf, 1, &signal, &status); break; case 'c': require_running (own_buf); signal = 0; myresume (own_buf, 0, &signal, &status); break; case 's': require_running (own_buf); signal = 0; myresume (own_buf, 1, &signal, &status); break; case 'Z': { char *lenptr; char *dataptr; CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); int len = strtol (lenptr + 1, &dataptr, 16); char type = own_buf[1]; if (the_target->insert_watchpoint == NULL || (type < '2' || type > '4')) { /* No watchpoint support or not a watchpoint command; unrecognized either way. */ own_buf[0] = '\0'; } else { int res; require_running (own_buf); res = (*the_target->insert_watchpoint) (type, addr, len); if (res == 0) write_ok (own_buf); else if (res == 1) /* Unsupported. */ own_buf[0] = '\0'; else write_enn (own_buf); } break; } case 'z': { char *lenptr; char *dataptr; CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); int len = strtol (lenptr + 1, &dataptr, 16); char type = own_buf[1]; if (the_target->remove_watchpoint == NULL || (type < '2' || type > '4')) { /* No watchpoint support or not a watchpoint command; unrecognized either way. */ own_buf[0] = '\0'; } else { int res; require_running (own_buf); res = (*the_target->remove_watchpoint) (type, addr, len); if (res == 0) write_ok (own_buf); else if (res == 1) /* Unsupported. */ own_buf[0] = '\0'; else write_enn (own_buf); } break; } case 'k': response_needed = 0; if (!target_running ()) /* The packet we received doesn't make sense - but we can't reply to it, either. */ goto restart; fprintf (stderr, "Killing inferior\n"); kill_inferior (); /* When using the extended protocol, we wait with no program running. The traditional protocol will exit instead. */ if (extended_protocol) { status = 'X'; signal = TARGET_SIGNAL_KILL; was_running = 0; goto restart; } else { exit (0); break; } case 'T': { unsigned long gdb_id, thread_id; require_running (own_buf); gdb_id = strtoul (&own_buf[1], NULL, 16); thread_id = gdb_id_to_thread_id (gdb_id); if (thread_id == 0) { write_enn (own_buf); break; } if (mythread_alive (thread_id)) write_ok (own_buf); else write_enn (own_buf); } break; case 'R': response_needed = 0; /* Restarting the inferior is only supported in the extended protocol. */ if (extended_protocol) { if (target_running ()) kill_inferior (); fprintf (stderr, "GDBserver restarting\n"); /* Wait till we are at 1st instruction in prog. */ if (program_argv != NULL) signal = start_inferior (program_argv, &status); else { status = 'X'; signal = TARGET_SIGNAL_KILL; } goto restart; } else { /* It is a request we don't understand. Respond with an empty packet so that gdb knows that we don't support this request. */ own_buf[0] = '\0'; break; } case 'v': /* Extended (long) request. */ handle_v_requests (own_buf, &status, &signal, packet_len, &new_packet_len); break; default: /* It is a request we don't understand. Respond with an empty packet so that gdb knows that we don't support this request. */ own_buf[0] = '\0'; break; } if (new_packet_len != -1) putpkt_binary (own_buf, new_packet_len); else putpkt (own_buf); response_needed = 0; if (was_running && (status == 'W' || status == 'X')) { was_running = 0; if (status == 'W') fprintf (stderr, "\nChild exited with status %d\n", signal); if (status == 'X') fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n", target_signal_to_host (signal), target_signal_to_name (signal)); if (extended_protocol) goto restart; else { fprintf (stderr, "GDBserver exiting\n"); exit (0); } } if (status != 'W' && status != 'X') was_running = 1; } /* If an exit was requested (using the "monitor exit" command), terminate now. The only other way to get here is for getpkt to fail; close the connection and reopen it at the top of the loop. */ if (exit_requested) { remote_close (); if (attached && target_running ()) detach_inferior (); else if (target_running ()) kill_inferior (); exit (0); } else { fprintf (stderr, "Remote side has terminated connection. " "GDBserver will reopen the connection.\n"); remote_close (); } } }
int main (int argc, char *argv[]) { char ch, status, *own_buf; unsigned char *mem_buf; int i = 0; int signal; unsigned int len; CORE_ADDR mem_addr; int bad_attach; int pid; char *arg_end; if (argc >= 2 && strcmp (argv[1], "--version") == 0) { gdbserver_version (); exit (0); } if (argc >= 2 && strcmp (argv[1], "--help") == 0) { gdbserver_usage (); exit (0); } if (setjmp (toplevel)) { fprintf (stderr, "Exiting\n"); exit (1); } bad_attach = 0; pid = 0; attached = 0; if (argc >= 3 && strcmp (argv[2], "--attach") == 0) { if (argc == 4 && argv[3] != '\0' && (pid = strtoul (argv[3], &arg_end, 10)) != 0 && *arg_end == '\0') { ; } else bad_attach = 1; } if (argc < 3 || bad_attach) { gdbserver_usage (); exit (1); } initialize_low (); own_buf = malloc (PBUFSIZ); mem_buf = malloc (PBUFSIZ); if (pid == 0) { /* Wait till we are at first instruction in program. */ signal = start_inferior (&argv[2], &status); /* start_inferior() returns an integer, but the wait * function returns an unsigned char. in the case of * of an error, the wait returns -1 which means 255. */ if (status == 'W' || status == 'X') { fprintf (stderr, "Aborting server; child exited with %i\n", signal); exit (signal); } /* We are now stopped at the first instruction of the target process */ } else { switch (attach_inferior (pid, &status, &signal)) { case -1: error ("Attaching not supported on this target"); break; default: attached = 1; break; } } while (1) { remote_open (argv[1]); restart: setjmp (toplevel); while (1) { unsigned char sig; int packet_len; int new_packet_len = -1; packet_len = getpkt (own_buf); if (packet_len <= 0) break; i = 0; ch = own_buf[i++]; switch (ch) { case 'q': handle_query (own_buf, &new_packet_len); break; case 'd': remote_debug = !remote_debug; break; #ifndef USE_WIN32API /* Skip "detach" support on mingw32, since we don't have waitpid. */ case 'D': fprintf (stderr, "Detaching from inferior\n"); detach_inferior (); write_ok (own_buf); putpkt (own_buf); remote_close (); /* If we are attached, then we can exit. Otherwise, we need to hang around doing nothing, until the child is gone. */ if (!attached) { int status, ret; do { ret = waitpid (signal_pid, &status, 0); if (WIFEXITED (status) || WIFSIGNALED (status)) break; } while (ret != -1 || errno != ECHILD); } exit (0); #endif case '!': if (attached == 0) { extended_protocol = 1; prepare_resume_reply (own_buf, status, signal); } else { /* We can not use the extended protocol if we are attached, because we can not restart the running program. So return unrecognized. */ own_buf[0] = '\0'; } break; case '?': prepare_resume_reply (own_buf, status, signal); break; case 'H': if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') { unsigned long gdb_id, thread_id; gdb_id = strtoul (&own_buf[2], NULL, 16); thread_id = gdb_id_to_thread_id (gdb_id); if (thread_id == 0) { write_enn (own_buf); break; } if (own_buf[1] == 'g') { general_thread = thread_id; set_desired_inferior (1); } else if (own_buf[1] == 'c') cont_thread = thread_id; else if (own_buf[1] == 's') step_thread = thread_id; write_ok (own_buf); } else { /* Silently ignore it so that gdb can extend the protocol without compatibility headaches. */ own_buf[0] = '\0'; } break; case 'g': set_desired_inferior (1); registers_to_string (own_buf); break; case 'G': set_desired_inferior (1); registers_from_string (&own_buf[1]); write_ok (own_buf); break; case 'm': decode_m_packet (&own_buf[1], &mem_addr, &len); if (read_inferior_memory (mem_addr, mem_buf, len) == 0) convert_int_to_ascii (mem_buf, own_buf, len); else write_enn (own_buf); break; case 'M': decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf); if (write_inferior_memory (mem_addr, mem_buf, len) == 0) write_ok (own_buf); else write_enn (own_buf); break; case 'X': if (decode_X_packet (&own_buf[1], packet_len - 1, &mem_addr, &len, mem_buf) < 0 || write_inferior_memory (mem_addr, mem_buf, len) != 0) write_enn (own_buf); else write_ok (own_buf); break; case 'C': convert_ascii_to_int (own_buf + 1, &sig, 1); if (target_signal_to_host_p (sig)) signal = target_signal_to_host (sig); else signal = 0; set_desired_inferior (0); myresume (0, signal); signal = mywait (&status, 1); prepare_resume_reply (own_buf, status, signal); break; case 'S': convert_ascii_to_int (own_buf + 1, &sig, 1); if (target_signal_to_host_p (sig)) signal = target_signal_to_host (sig); else signal = 0; set_desired_inferior (0); myresume (1, signal); signal = mywait (&status, 1); prepare_resume_reply (own_buf, status, signal); break; case 'c': set_desired_inferior (0); myresume (0, 0); signal = mywait (&status, 1); prepare_resume_reply (own_buf, status, signal); break; case 's': set_desired_inferior (0); myresume (1, 0); signal = mywait (&status, 1); prepare_resume_reply (own_buf, status, signal); break; case 'Z': { char *lenptr; char *dataptr; CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); int len = strtol (lenptr + 1, &dataptr, 16); char type = own_buf[1]; if (the_target->insert_watchpoint == NULL || (type < '2' || type > '4')) { /* No watchpoint support or not a watchpoint command; unrecognized either way. */ own_buf[0] = '\0'; } else { int res; res = (*the_target->insert_watchpoint) (type, addr, len); if (res == 0) write_ok (own_buf); else if (res == 1) /* Unsupported. */ own_buf[0] = '\0'; else write_enn (own_buf); } break; } case 'z': { char *lenptr; char *dataptr; CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); int len = strtol (lenptr + 1, &dataptr, 16); char type = own_buf[1]; if (the_target->remove_watchpoint == NULL || (type < '2' || type > '4')) { /* No watchpoint support or not a watchpoint command; unrecognized either way. */ own_buf[0] = '\0'; } else { int res; res = (*the_target->remove_watchpoint) (type, addr, len); if (res == 0) write_ok (own_buf); else if (res == 1) /* Unsupported. */ own_buf[0] = '\0'; else write_enn (own_buf); } break; } case 'k': fprintf (stderr, "Killing inferior\n"); kill_inferior (); /* When using the extended protocol, we start up a new debugging session. The traditional protocol will exit instead. */ if (extended_protocol) { write_ok (own_buf); fprintf (stderr, "GDBserver restarting\n"); /* Wait till we are at 1st instruction in prog. */ signal = start_inferior (&argv[2], &status); goto restart; break; } else { exit (0); break; } case 'T': { unsigned long gdb_id, thread_id; gdb_id = strtoul (&own_buf[1], NULL, 16); thread_id = gdb_id_to_thread_id (gdb_id); if (thread_id == 0) { write_enn (own_buf); break; } if (mythread_alive (thread_id)) write_ok (own_buf); else write_enn (own_buf); } break; case 'R': /* Restarting the inferior is only supported in the extended protocol. */ if (extended_protocol) { kill_inferior (); write_ok (own_buf); fprintf (stderr, "GDBserver restarting\n"); /* Wait till we are at 1st instruction in prog. */ signal = start_inferior (&argv[2], &status); goto restart; break; } else { /* It is a request we don't understand. Respond with an empty packet so that gdb knows that we don't support this request. */ own_buf[0] = '\0'; break; } case 'v': /* Extended (long) request. */ handle_v_requests (own_buf, &status, &signal); break; default: /* It is a request we don't understand. Respond with an empty packet so that gdb knows that we don't support this request. */ own_buf[0] = '\0'; break; } if (new_packet_len != -1) putpkt_binary (own_buf, new_packet_len); else putpkt (own_buf); if (status == 'W') fprintf (stderr, "\nChild exited with status %d\n", signal); if (status == 'X') fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n", target_signal_to_host (signal), target_signal_to_name (signal)); if (status == 'W' || status == 'X') { if (extended_protocol) { fprintf (stderr, "Killing inferior\n"); kill_inferior (); write_ok (own_buf); fprintf (stderr, "GDBserver restarting\n"); /* Wait till we are at 1st instruction in prog. */ signal = start_inferior (&argv[2], &status); goto restart; break; } else { fprintf (stderr, "GDBserver exiting\n"); exit (0); } } } /* We come here when getpkt fails. For the extended remote protocol we exit (and this is the only way we gracefully exit!). For the traditional remote protocol close the connection, and re-open it at the top of the loop. */ if (extended_protocol) { remote_close (); exit (0); } else { fprintf (stderr, "Remote side has terminated connection. " "GDBserver will reopen the connection.\n"); remote_close (); } } }
static void fbsd_thread_resume (ptid_t ptid, int step, enum target_signal signo) { td_thrhandle_t th; td_thrinfo_t ti; ptid_t work_ptid; int resume_all, ret; long lwp, thvalid = 0; if (!fbsd_thread_active) { child_ops.to_resume (ptid, step, signo); return; } if (GET_PID(ptid) != -1 && step != 0) { resume_all = 0; work_ptid = ptid; } else { resume_all = 1; work_ptid = inferior_ptid; } lwp = GET_LWP (work_ptid); if (lwp == 0) { /* check user thread */ ret = td_ta_map_id2thr_p (thread_agent, GET_THREAD(work_ptid), &th); if (ret) error (thread_db_err_str (ret)); /* For M:N thread, we need to tell UTS to set/unset single step flag at context switch time, the flag will be written into thread mailbox. This becauses some architecture may not have machine single step flag in ucontext, so we put the flag in mailbox, when the thread switches back, kse_switchin restores the single step state. */ ret = td_thr_sstep_p (&th, step); if (ret) error (thread_db_err_str (ret)); ret = td_thr_get_info_p (&th, &ti); if (ret) error (thread_db_err_str (ret)); thvalid = 1; lwp = ti.ti_lid; } if (lwp) { int req = step ? PT_SETSTEP : PT_CLEARSTEP; if (ptrace (req, (pid_t) lwp, (caddr_t) 1, target_signal_to_host(signo))) perror_with_name ("PT_SETSTEP/PT_CLEARSTEP"); } if (!ptid_equal (last_single_step_thread, null_ptid)) { ret = td_ta_thr_iter_p (thread_agent, resume_thread_callback, NULL, TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY, TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS); if (ret != TD_OK) error ("resume error: %s", thread_db_err_str (ret)); } if (!resume_all) { ret = td_ta_thr_iter_p (thread_agent, suspend_thread_callback, NULL, TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY, TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS); if (ret != TD_OK) error ("suspend error: %s", thread_db_err_str (ret)); last_single_step_thread = work_ptid; } else last_single_step_thread = null_ptid; if (thvalid) { ret = td_thr_dbresume_p (&th); if (ret != TD_OK) error ("resume error: %s", thread_db_err_str (ret)); } else { /* it is not necessary, put it here for completness */ ret = ptrace(PT_RESUME, lwp, 0, 0); } /* now continue the process, suspended thread wont run */ if (ptrace (PT_CONTINUE, proc_handle.pid , (caddr_t)1, target_signal_to_host(signo))) perror_with_name ("PT_CONTINUE"); }
/* An inferior Unix process CHILD_PID has been created by a call to fork() (or variants like vfork). It is presently stopped, and waiting to be resumed. clone_and_follow_inferior will fork the debugger, and that clone will "follow" (attach to) CHILD_PID. The original copy of the debugger will not touch CHILD_PID again. Also, the original debugger will set FOLLOWED_CHILD FALSE, while the clone will set it TRUE. */ void clone_and_follow_inferior (int child_pid, int *followed_child) { int debugger_pid; int status; char pid_spelling[100]; /* Arbitrary but sufficient length. */ /* This semaphore is used to coordinate the two debuggers' handoff of CHILD_PID. The original debugger will detach from CHILD_PID, and then the clone debugger will attach to it. (It must be done this way because on some targets, only one process at a time can trace another. Thus, the original debugger must relinquish its tracing rights before the clone can pick them up.) */ #define SEM_TALK (1) #define SEM_LISTEN (0) int handoff_semaphore[2]; /* Original "talks" to [1], clone "listens" to [0] */ int talk_value = 99; int listen_value; /* Set debug_fork then attach to the child while it sleeps, to debug. */ static int debug_fork = 0; /* It is generally good practice to flush any possible pending stdio output prior to doing a fork, to avoid the possibility of both the parent and child flushing the same data after the fork. */ gdb_flush (gdb_stdout); gdb_flush (gdb_stderr); /* Open the semaphore pipes. */ status = pipe (handoff_semaphore); if (status < 0) error ("error getting pipe for handoff semaphore"); /* Clone the debugger. Note that the apparent call to vfork() below *might* actually be a call to fork() due to the fact that autoconf will ``#define vfork fork'' on certain platforms. */ if (debug_fork) debugger_pid = fork (); else debugger_pid = vfork (); if (debugger_pid < 0) perror_with_name ("fork"); /* Are we the original debugger? If so, we must relinquish all claims to CHILD_PID. */ if (debugger_pid != 0) { char signal_spelling[100]; /* Arbitrary but sufficient length */ /* Detach from CHILD_PID. Deliver a "stop" signal when we do, though, so that it remains stopped until the clone debugger can attach to it. */ detach_breakpoints (child_pid); sprintf (signal_spelling, "%d", target_signal_to_host (TARGET_SIGNAL_STOP)); target_require_detach (child_pid, signal_spelling, 1); /* Notify the clone debugger that it should attach to CHILD_PID. */ write (handoff_semaphore[SEM_TALK], &talk_value, sizeof (talk_value)); *followed_child = 0; } /* We're the child. */ else { if (debug_fork) sleep (debug_fork); /* The child (i.e., the cloned debugger) must now attach to CHILD_PID. inferior_ptid is presently set to the parent process of the fork, while CHILD_PID should be the child process of the fork. Wait until the original debugger relinquishes control of CHILD_PID, though. */ read (handoff_semaphore[SEM_LISTEN], &listen_value, sizeof (listen_value)); /* Note that we DON'T want to actually detach from inferior_ptid, because that would allow it to run free. The original debugger wants to retain control of the process. So, we just reset inferior_ptid to CHILD_PID, and then ensure that all breakpoints are really set in CHILD_PID. */ target_mourn_inferior (); /* Ask the tty subsystem to switch to the one we specified earlier (or to share the current terminal, if none was specified). */ new_tty (); dont_repeat (); sprintf (pid_spelling, "%d", child_pid); target_require_attach (pid_spelling, 1); /* Perform any necessary cleanup, after attachment. (This form of attaching can behave differently on some targets than the standard method, where a process formerly not under debugger control was suddenly attached to..) */ target_post_follow_inferior_by_clone (); *followed_child = 1; } /* Discard the handoff sempahore. */ (void) close (handoff_semaphore[SEM_LISTEN]); (void) close (handoff_semaphore[SEM_TALK]); }