Move debugger creation off the stack
Jeffrey Pfau jeffrey@endrift.com
Sun, 20 Apr 2014 04:19:03 -0700
3 files changed,
45 insertions(+),
27 deletions(-)
M
src/platform/commandline.c
→
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 <fcntl.h> #include <getopt.h>@@ -49,6 +57,40 @@ opts->fname = _defaultFilename;
} opts->fd = open(opts->fname, O_RDONLY); 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) {
M
src/platform/commandline.h
→
src/platform/commandline.h
@@ -31,6 +31,7 @@ int debugAtStart;
}; int parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv); +struct ARMDebugger* createDebugger(struct StartupOptions* opts); void usage(const char* arg0); #endif
M
src/platform/sdl/gl-main.c
→
src/platform/sdl/gl-main.c
@@ -77,18 +77,7 @@ if (!_GBASDLInit(&renderer)) {
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 @@ .sync.audioWait = 1,
.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 @@ close(opts.fd);
if (opts.biosFd >= 0) { close(opts.biosFd); } + free(context.debugger); _GBASDLDeinit(&renderer);