int main() { struct GDBState *s; int fd = gdbserver_open(1234); if (fd == -1) { return -1; } s = (struct GDBState*)malloc(sizeof(struct GDBState)); s->fd = fd; gdbserver_accept(s); gdbserver_main(s); printf("GDB stub had been done.\n"); close(fd); free(s); return 0; }
int main (int argc, char *argv[]) { int bad_attach; int pid; char *arg_end; char **next_arg = &argv[1]; int attach = 0; 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 (); 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, "--debug") == 0) debug_threads = 1; else { fprintf (stderr, "Unknown argument: %s\n", *next_arg); exit (1); } next_arg++; continue; } #if 0 if (setjmp (toplevel)) { fprintf (stderr, "Exiting\n"); exit (1); } #endif port = *next_arg; next_arg++; if (port == NULL || (!attach && !multi_mode && *next_arg == NULL)) { gdbserver_usage (); 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 (); exit (1); } /* initialize_async_io (); initialize_low (); */ 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 0 if (setjmp (toplevel)) { fprintf (stderr, "Killing inferior\n"); kill_inferior (); exit (1); } #endif 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); } gdbserver_main(); }