Move debugger creation off the stack

This commit is contained in:
Jeffrey Pfau 2014-04-20 04:19:03 -07:00
parent 89ccb41b03
commit 7c356ffd07
3 changed files with 45 additions and 27 deletions

View File

@ -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>
@ -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);
}

View File

@ -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

View File

@ -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);