- We now build up the argument list in a BStringList and map argv to those.

The previous solution had various issues where argv pointed to stack-based
  objects that had potentially been destroyed/overwritten already. Fixes
  #8173.

- Modify _SetupGDBArguments() to return an error code and check for it
  accordingly.
This commit is contained in:
Rene Gollent
2011-12-06 19:56:04 -05:00
parent cee4855acb
commit c0954dc661
+51 -34
View File
@@ -1,4 +1,5 @@
/* /*
* Copyright 2011, Rene Gollent, [email protected].
* Copyright 2005-2009, Ingo Weinhold, [email protected]. * Copyright 2005-2009, Ingo Weinhold, [email protected].
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
@@ -27,6 +28,7 @@
#include <RegistrarDefs.h> #include <RegistrarDefs.h>
#include <RosterPrivate.h> #include <RosterPrivate.h>
#include <Server.h> #include <Server.h>
#include <StringList.h>
#include <util/DoublyLinkedList.h> #include <util/DoublyLinkedList.h>
@@ -115,8 +117,7 @@ private:
status_t _PopMessage(DebugMessage *&message); status_t _PopMessage(DebugMessage *&message);
thread_id _EnterDebugger(); thread_id _EnterDebugger();
void _SetupGDBArguments(const char **argv, int &argc, char *teamString, status_t _SetupGDBArguments(BStringList &arguments, bool usingConsoled);
size_t teamStringSize, bool usingConsoled);
void _KillTeam(); void _KillTeam();
bool _HandleMessage(DebugMessage *message); bool _HandleMessage(DebugMessage *message);
@@ -435,12 +436,12 @@ TeamDebugHandler::_PopMessage(DebugMessage *&message)
} }
void status_t
TeamDebugHandler::_SetupGDBArguments(const char **argv, int &argc, TeamDebugHandler::_SetupGDBArguments(BStringList &arguments, bool usingConsoled)
char *teamString, size_t teamStringSize, bool usingConsoled)
{ {
// prepare the argument vector // prepare the argument vector
snprintf(teamString, teamStringSize, "--pid=%ld", fTeam); BString teamString;
teamString.SetToFormat("--pid=%ld", fTeam);
status_t error; status_t error;
BPath terminalPath; BPath terminalPath;
@@ -449,37 +450,39 @@ TeamDebugHandler::_SetupGDBArguments(const char **argv, int &argc,
if (error != B_OK) { if (error != B_OK) {
debug_printf("debug_server: can't find system-bin directory: %s\n", debug_printf("debug_server: can't find system-bin directory: %s\n",
strerror(error)); strerror(error));
return; return error;
} }
error = terminalPath.Append("consoled"); error = terminalPath.Append("consoled");
if (error != B_OK) { if (error != B_OK) {
debug_printf("debug_server: can't append to system-bin path: %s\n", debug_printf("debug_server: can't append to system-bin path: %s\n",
strerror(error)); strerror(error));
return; return error;
} }
} else { } else {
error = find_directory(B_SYSTEM_APPS_DIRECTORY, &terminalPath); error = find_directory(B_SYSTEM_APPS_DIRECTORY, &terminalPath);
if (error != B_OK) { if (error != B_OK) {
debug_printf("debug_server: can't find system-apps directory: %s\n", debug_printf("debug_server: can't find system-apps directory: %s\n",
strerror(error)); strerror(error));
return; return error;
} }
error = terminalPath.Append("Terminal"); error = terminalPath.Append("Terminal");
if (error != B_OK) { if (error != B_OK) {
debug_printf("debug_server: can't append to system-apps path: %s\n", debug_printf("debug_server: can't append to system-apps path: %s\n",
strerror(error)); strerror(error));
return; return error;
} }
} }
argv[argc++] = terminalPath.Path(); arguments.MakeEmpty();
if (!arguments.Add(terminalPath.Path()))
return B_NO_MEMORY;
if (!usingConsoled) { if (!usingConsoled) {
char windowTitle[64]; BString windowTitle;
snprintf(windowTitle, sizeof(windowTitle), "Debug of Team %ld: %s", windowTitle.SetToFormat("Debug of Team %ld: %s", fTeam,
fTeam, _LastPathComponent(fExecutablePath)); _LastPathComponent(fExecutablePath));
argv[argc++] = "-t"; if (!arguments.Add("-t") || !arguments.Add(windowTitle))
argv[argc++] = windowTitle; return B_NO_MEMORY;
} }
BPath gdbPath; BPath gdbPath;
@@ -487,20 +490,21 @@ TeamDebugHandler::_SetupGDBArguments(const char **argv, int &argc,
if (error != B_OK) { if (error != B_OK) {
debug_printf("debug_server: can't find system-bin directory: %s\n", debug_printf("debug_server: can't find system-bin directory: %s\n",
strerror(error)); strerror(error));
return; return error;
} }
error = gdbPath.Append("gdb"); error = gdbPath.Append("gdb");
if (error != B_OK) { if (error != B_OK) {
debug_printf("debug_server: can't append to system-bin path: %s\n", debug_printf("debug_server: can't append to system-bin path: %s\n",
strerror(error)); strerror(error));
return; return error;
} }
if (!arguments.Add(gdbPath.Path()) || !arguments.Add(teamString))
return B_NO_MEMORY;
argv[argc++] = gdbPath.Path(); if (strlen(fExecutablePath) > 0 && !arguments.Add(fExecutablePath))
argv[argc++] = teamString; return B_NO_MEMORY;
if (strlen(fExecutablePath) > 0)
argv[argc++] = fExecutablePath; return B_OK;
argv[argc] = NULL;
} }
@@ -522,14 +526,19 @@ TeamDebugHandler::_EnterDebugger()
return error; return error;
} }
BStringList arguments;
const char *argv[16]; const char *argv[16];
int argc = 0; int argc = 0;
char teamString[32];
bool debugInConsoled = _IsGUIServer() || !_AreGUIServersAlive(); bool debugInConsoled = _IsGUIServer() || !_AreGUIServersAlive();
#ifdef HANDOVER_USE_GDB #ifdef HANDOVER_USE_GDB
_SetupGDBArguments(argv, argc, teamString, sizeof(teamString), error = _SetupGDBArguments(arguments, debugInConsoled);
debugInConsoled); if (error != B_OK) {
debug_printf("debug_server: Failed to set up gdb arguments: %s\n",
strerror(error));
return error;
}
// start the terminal // start the terminal
TRACE(("debug_server: TeamDebugHandler::_EnterDebugger(): starting " TRACE(("debug_server: TeamDebugHandler::_EnterDebugger(): starting "
@@ -537,12 +546,14 @@ TeamDebugHandler::_EnterDebugger()
#elif defined(HANDOVER_USE_DEBUGGER) #elif defined(HANDOVER_USE_DEBUGGER)
if (debugInConsoled) { if (debugInConsoled) {
_SetupGDBArguments(argv, argc, teamString, sizeof(teamString), error = _SetupGDBArguments(arguments, debugInConsoled);
debugInConsoled); if (error != B_OK) {
debug_printf("debug_server: Failed to set up gdb arguments: %s\n",
strerror(error));
return error;
}
} else { } else {
// prepare the argument vector // prepare the argument vector
snprintf(teamString, sizeof(teamString), "%ld", fTeam);
BPath debuggerPath; BPath debuggerPath;
error = find_directory(B_SYSTEM_APPS_DIRECTORY, &debuggerPath); error = find_directory(B_SYSTEM_APPS_DIRECTORY, &debuggerPath);
if (error != B_OK) { if (error != B_OK) {
@@ -556,11 +567,13 @@ TeamDebugHandler::_EnterDebugger()
strerror(error)); strerror(error));
return error; return error;
} }
if (!arguments.Add(debuggerPath.Path()))
return B_NO_MEMORY;
argv[argc++] = debuggerPath.Path(); BString debuggerParam;
argv[argc++] = "--team"; debuggerParam.SetToFormat("%ld", fTeam);
argv[argc++] = teamString; if (!arguments.Add("--team") || !arguments.Add(debuggerParam))
argv[argc] = NULL; return B_NO_MEMORY;
// start the debugger // start the debugger
TRACE(("debug_server: TeamDebugHandler::_EnterDebugger(): starting " TRACE(("debug_server: TeamDebugHandler::_EnterDebugger(): starting "
@@ -568,6 +581,10 @@ TeamDebugHandler::_EnterDebugger()
} }
#endif #endif
for (int32 i = 0; i < arguments.CountStrings(); i++)
argv[argc++] = arguments.StringAt(i).String();
argv[argc] = NULL;
thread_id thread = load_image(argc, argv, (const char**)environ); thread_id thread = load_image(argc, argv, (const char**)environ);
if (thread < 0) { if (thread < 0) {
debug_printf("debug_server: Failed to start debugger: %s\n", debug_printf("debug_server: Failed to start debugger: %s\n",