diff --git a/src/platform/commandline.c b/src/platform/commandline.c index 772778b68..eecf19250 100644 --- a/src/platform/commandline.c +++ b/src/platform/commandline.c @@ -1,5 +1,13 @@ #include "commandline.h" +#ifdef USE_CLI_DEBUGGER +#include "debugger/cli-debugger.h" +#endif + +#ifdef USE_GDB_STUB +#include "debugger/gdb-stub.h" +#endif + #include #include @@ -51,6 +59,40 @@ int parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv) { return 1; } +struct ARMDebugger* createDebugger(struct StartupOptions* opts) { + union DebugUnion { + struct ARMDebugger d; +#ifdef USE_CLI_DEBUGGER + struct CLIDebugger cli; +#endif +#ifdef USE_GDB_STUB + struct GDBStub gdb; +#endif + }; + + union DebugUnion* debugger = malloc(sizeof(union DebugUnion)); + + switch (opts->debuggerType) { +#ifdef USE_CLI_DEBUGGER + case DEBUGGER_CLI: + CLIDebuggerCreate(&debugger->cli); + break; +#endif +#ifdef USE_GDB_STUB + case DEBUGGER_GDB: + GDBStubCreate(&debugger->gdb); + break; +#endif + case DEBUGGER_NONE: + case DEBUGGER_MAX: + free(debugger); + return 0; + break; + } + + return &debugger->d; +} + void usage(const char* arg0) { printf("%s: bad arguments\n", arg0); } diff --git a/src/platform/commandline.h b/src/platform/commandline.h index 2b6c1b711..d14246935 100644 --- a/src/platform/commandline.h +++ b/src/platform/commandline.h @@ -31,6 +31,7 @@ struct StartupOptions { }; int parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv); +struct ARMDebugger* createDebugger(struct StartupOptions* opts); void usage(const char* arg0); #endif diff --git a/src/platform/sdl/gl-main.c b/src/platform/sdl/gl-main.c index 14e4672ab..eacb9ee40 100644 --- a/src/platform/sdl/gl-main.c +++ b/src/platform/sdl/gl-main.c @@ -77,18 +77,7 @@ int main(int argc, char** argv) { return 1; } - union { - struct ARMDebugger d; -#ifdef USE_CLI_DEBUGGER - struct CLIDebugger cli; -#endif -#ifdef USE_GDB_STUB - struct GDBStub gdb; -#endif - } debugger; - struct GBAThread context = { - .debugger = &debugger.d, .renderer = &renderer.d.d, .startCallback = _GBASDLStart, .cleanCallback = _GBASDLClean, @@ -97,22 +86,7 @@ int main(int argc, char** argv) { .userData = &renderer }; - switch (opts.debuggerType) { -#ifdef USE_CLI_DEBUGGER - case DEBUGGER_CLI: - CLIDebuggerCreate(&debugger.cli); - break; -#endif -#ifdef USE_GDB_STUB - case DEBUGGER_GDB: - GDBStubCreate(&debugger.gdb); - break; -#endif - case DEBUGGER_NONE: - case DEBUGGER_MAX: - context.debugger = 0; - break; - } + context.debugger = createDebugger(&opts); GBAMapOptionsToContext(&opts, &context); @@ -125,6 +99,7 @@ int main(int argc, char** argv) { if (opts.biosFd >= 0) { close(opts.biosFd); } + free(context.debugger); _GBASDLDeinit(&renderer);