hrev43431 adds 1 changeset to branch 'master' old head: cee4855acbc8402e0ac54f390ec1d4335e32439d new head: c0954dc66186425eefeece8ce6a2c3b2720c6752 ---------------------------------------------------------------------------- c0954dc: - 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. [ Rene Gollent <anevilyak@xxxxxxxxx> ] ---------------------------------------------------------------------------- Revision: hrev43431 Commit: c0954dc66186425eefeece8ce6a2c3b2720c6752 URL: http://cgit.haiku-os.org/haiku/commit/?id=c0954dc Author: Rene Gollent <anevilyak@xxxxxxxxx> Date: Wed Dec 7 00:56:04 2011 UTC Ticket: https://dev.haiku-os.org/ticket/8173 ---------------------------------------------------------------------------- 1 files changed, 51 insertions(+), 34 deletions(-) src/servers/debug/DebugServer.cpp | 85 ++++++++++++++++++++------------- ---------------------------------------------------------------------------- diff --git a/src/servers/debug/DebugServer.cpp b/src/servers/debug/DebugServer.cpp index 24d75e2..c8a131e 100644 --- a/src/servers/debug/DebugServer.cpp +++ b/src/servers/debug/DebugServer.cpp @@ -1,4 +1,5 @@ /* + * Copyright 2011, Rene Gollent, rene@xxxxxxxxxxxx * Copyright 2005-2009, Ingo Weinhold, bonefish@xxxxxxxxxxxxx * Distributed under the terms of the MIT License. */ @@ -27,6 +28,7 @@ #include <RegistrarDefs.h> #include <RosterPrivate.h> #include <Server.h> +#include <StringList.h> #include <util/DoublyLinkedList.h> @@ -115,8 +117,7 @@ private: status_t _PopMessage(DebugMessage *&message); thread_id _EnterDebugger(); - void _SetupGDBArguments(const char **argv, int &argc, char *teamString, - size_t teamStringSize, bool usingConsoled); + status_t _SetupGDBArguments(BStringList &arguments, bool usingConsoled); void _KillTeam(); bool _HandleMessage(DebugMessage *message); @@ -435,12 +436,12 @@ TeamDebugHandler::_PopMessage(DebugMessage *&message) } -void -TeamDebugHandler::_SetupGDBArguments(const char **argv, int &argc, - char *teamString, size_t teamStringSize, bool usingConsoled) +status_t +TeamDebugHandler::_SetupGDBArguments(BStringList &arguments, bool usingConsoled) { // prepare the argument vector - snprintf(teamString, teamStringSize, "--pid=%ld", fTeam); + BString teamString; + teamString.SetToFormat("--pid=%ld", fTeam); status_t error; BPath terminalPath; @@ -449,37 +450,39 @@ TeamDebugHandler::_SetupGDBArguments(const char **argv, int &argc, if (error != B_OK) { debug_printf("debug_server: can't find system-bin directory: %s\n", strerror(error)); - return; + return error; } error = terminalPath.Append("consoled"); if (error != B_OK) { debug_printf("debug_server: can't append to system-bin path: %s\n", strerror(error)); - return; + return error; } } else { error = find_directory(B_SYSTEM_APPS_DIRECTORY, &terminalPath); if (error != B_OK) { debug_printf("debug_server: can't find system-apps directory: %s\n", strerror(error)); - return; + return error; } error = terminalPath.Append("Terminal"); if (error != B_OK) { debug_printf("debug_server: can't append to system-apps path: %s\n", strerror(error)); - return; + return error; } } - argv[argc++] = terminalPath.Path(); + arguments.MakeEmpty(); + if (!arguments.Add(terminalPath.Path())) + return B_NO_MEMORY; if (!usingConsoled) { - char windowTitle[64]; - snprintf(windowTitle, sizeof(windowTitle), "Debug of Team %ld: %s", - fTeam, _LastPathComponent(fExecutablePath)); - argv[argc++] = "-t"; - argv[argc++] = windowTitle; + BString windowTitle; + windowTitle.SetToFormat("Debug of Team %ld: %s", fTeam, + _LastPathComponent(fExecutablePath)); + if (!arguments.Add("-t") || !arguments.Add(windowTitle)) + return B_NO_MEMORY; } BPath gdbPath; @@ -487,20 +490,21 @@ TeamDebugHandler::_SetupGDBArguments(const char **argv, int &argc, if (error != B_OK) { debug_printf("debug_server: can't find system-bin directory: %s\n", strerror(error)); - return; + return error; } error = gdbPath.Append("gdb"); if (error != B_OK) { debug_printf("debug_server: can't append to system-bin path: %s\n", strerror(error)); - return; + return error; } + if (!arguments.Add(gdbPath.Path()) || !arguments.Add(teamString)) + return B_NO_MEMORY; - argv[argc++] = gdbPath.Path(); - argv[argc++] = teamString; - if (strlen(fExecutablePath) > 0) - argv[argc++] = fExecutablePath; - argv[argc] = NULL; + if (strlen(fExecutablePath) > 0 && !arguments.Add(fExecutablePath)) + return B_NO_MEMORY; + + return B_OK; } @@ -522,14 +526,19 @@ TeamDebugHandler::_EnterDebugger() return error; } + BStringList arguments; const char *argv[16]; int argc = 0; - char teamString[32]; + bool debugInConsoled = _IsGUIServer() || !_AreGUIServersAlive(); #ifdef HANDOVER_USE_GDB - _SetupGDBArguments(argv, argc, teamString, sizeof(teamString), - debugInConsoled); + error = _SetupGDBArguments(arguments, debugInConsoled); + if (error != B_OK) { + debug_printf("debug_server: Failed to set up gdb arguments: %s\n", + strerror(error)); + return error; + } // start the terminal TRACE(("debug_server: TeamDebugHandler::_EnterDebugger(): starting " @@ -537,12 +546,14 @@ TeamDebugHandler::_EnterDebugger() #elif defined(HANDOVER_USE_DEBUGGER) if (debugInConsoled) { - _SetupGDBArguments(argv, argc, teamString, sizeof(teamString), - debugInConsoled); + error = _SetupGDBArguments(arguments, debugInConsoled); + if (error != B_OK) { + debug_printf("debug_server: Failed to set up gdb arguments: %s\n", + strerror(error)); + return error; + } } else { // prepare the argument vector - snprintf(teamString, sizeof(teamString), "%ld", fTeam); - BPath debuggerPath; error = find_directory(B_SYSTEM_APPS_DIRECTORY, &debuggerPath); if (error != B_OK) { @@ -556,11 +567,13 @@ TeamDebugHandler::_EnterDebugger() strerror(error)); return error; } + if (!arguments.Add(debuggerPath.Path())) + return B_NO_MEMORY; - argv[argc++] = debuggerPath.Path(); - argv[argc++] = "--team"; - argv[argc++] = teamString; - argv[argc] = NULL; + BString debuggerParam; + debuggerParam.SetToFormat("%ld", fTeam); + if (!arguments.Add("--team") || !arguments.Add(debuggerParam)) + return B_NO_MEMORY; // start the debugger TRACE(("debug_server: TeamDebugHandler::_EnterDebugger(): starting " @@ -568,6 +581,10 @@ TeamDebugHandler::_EnterDebugger() } #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); if (thread < 0) { debug_printf("debug_server: Failed to start debugger: %s\n",