Skip to content

Commit

Permalink
shell: Move process creation/attachment in main module instead
Browse files Browse the repository at this point in the history
  • Loading branch information
dd86k committed Dec 1, 2024
1 parent ba6aa5e commit 6fd8e2d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 46 deletions.
9 changes: 9 additions & 0 deletions debugger/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,14 @@ int main(int argc, const(char)** argv) {
return EXIT_FAILURE;
}

// Start or attach to process if specified
if (argc > 0 && argv && shell_spawn(*argv, argc > 1 ? argv + 1 : null)) {
logerror("Could not spawn process: %s", adbg_error_message());
return 1;
} else if (opt_pid && shell_attach(opt_pid)) {
logerror("Could not attach to process: %s", adbg_error_message());
return 1;
}

return shell_start(argc, getoptleftovers());
}
83 changes: 37 additions & 46 deletions debugger/shell.d
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,6 @@ void logwrite(const(char) *pre, int color, const(char) *fmt, va_list args) {
}

int shell_start(int argc, const(char)** argv) {
// Start or attach to process if specified
if (argc > 0 && argv && shell_spawn(*argv, argc > 1 ? argv + 1 : null)) {
logerror("Could not spawn process: %s", adbg_error_message());
return 1;
} else if (opt_pid && shell_attach(opt_pid)) {
logerror("Could not attach to process: %s", adbg_error_message());
return 1;
}

Lcommand:
fputs("(adbg) ", stdout);
fflush(stdout);
Expand Down Expand Up @@ -188,6 +179,43 @@ int shell_execv(int argc, const(char) **argv) {
return command.entry(argc, argv);
}

int shell_spawn(const(char) *exec, const(char) **argv) {
// Save for restart
last_spawn_exec = exec;
last_spawn_argv = argv;

// Spawn process
process = adbg_debugger_spawn(exec,
AdbgSpawnOpt.argv, argv,
0);
if (process == null)
return ShellError.alicedbg;

printf("Process '%s' created", exec);
if (argv && *argv) {
printf(" with arguments:");
for (int i; argv[i]; ++i)
printf(" '%s'", argv[i]);
}
putchar('\n');

return shell_setup();
}

int shell_attach(int pid) {
// Save for restart
opt_pid = pid;

// Attach to process
process = adbg_debugger_attach(pid, 0);
if (process == null)
return ShellError.alicedbg;

loginfo("Debugger attached.");

return shell_setup();
}

private:
__gshared:

Expand Down Expand Up @@ -537,43 +565,6 @@ immutable(command2_t)* shell_findcommand(const(char) *ucommand) {
return null;
}

int shell_spawn(const(char) *exec, const(char) **argv) {
// Save for restart
last_spawn_exec = exec;
last_spawn_argv = argv;

// Spawn process
process = adbg_debugger_spawn(exec,
AdbgSpawnOpt.argv, argv,
0);
if (process == null)
return ShellError.alicedbg;

printf("Process '%s' created", exec);
if (argv && *argv) {
printf(" with arguments:");
for (int i; argv[i]; ++i)
printf(" '%s'", argv[i]);
}
putchar('\n');

return shell_setup();
}

int shell_attach(int pid) {
// Save for restart
opt_pid = pid;

// Attach to process
process = adbg_debugger_attach(pid, 0);
if (process == null)
return ShellError.alicedbg;

loginfo("Debugger attached.");

return shell_setup();
}

// After
int shell_setup() {
if (adbg_debugger_on(process, AdbgEvent.exception, &shell_event_exception) ||
Expand Down

0 comments on commit 6fd8e2d

Please sign in to comment.