* Copyright 2005-2008, Ingo Weinhold, ingo_weinhold@gmx.de.
* Copyright 2013, Rene Gollent, rene@gollent.com.
* Distributed under the terms of the MIT License.
*/
#include "debug_utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string>
#include <string.h>
#include <debugger.h>
#include <libroot_private.h>
#include <syscalls.h>
#include <syscall_load_image.h>
extern const char* __progname;
static const char* kCommandName = __progname;
static status_t
find_program(const char* programName, std::string& resolvedPath)
{
if (*programName == '/' || strchr(programName, '/')) {
resolvedPath = programName;
return B_OK;
}
const char* paths = getenv("PATH");
if (!paths)
return B_ENTRY_NOT_FOUND;
do {
const char* pathEnd = strchr(paths, ':');
int pathLen = (pathEnd ? pathEnd - paths : strlen(paths));
if (pathLen > 0) {
std::string path(paths, pathLen);
path += "/";
path += programName;
struct stat st;
if (stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) {
resolvedPath = path;
return B_OK;
}
}
paths = (pathEnd ? pathEnd + 1 : NULL);
} while (paths);
return B_ENTRY_NOT_FOUND;
}
thread_id
load_program(const char* const* args, int32 argCount, bool traceLoading)
{
const char** mutableArgs = new const char*[argCount];
for (int i = 0; i < argCount; i++)
mutableArgs[i] = args[i];
std::string programPath;
status_t error = find_program(args[0], programPath);
if (error != B_OK) {
delete[] mutableArgs;
return error;
}
mutableArgs[0] = programPath.c_str();
int32 envCount = 0;
while (environ[envCount] != NULL)
envCount++;
char** flatArgs = NULL;
size_t flatArgsSize;
error = __flatten_process_args(mutableArgs, argCount, environ, &envCount,
mutableArgs[0], &flatArgs, &flatArgsSize);
thread_id thread;
if (error == B_OK) {
thread = _kern_load_image(flatArgs, flatArgsSize, argCount, envCount,
B_NORMAL_PRIORITY, (traceLoading ? 0 : B_WAIT_TILL_LOADED), -1, 0);
free(flatArgs);
} else
thread = error;
delete[] mutableArgs;
return thread;
}
status_t
set_team_debugging_flags(port_id nubPort, int32 flags)
{
debug_nub_set_team_flags message;
message.flags = flags;
status_t error = B_OK;
do {
error = write_port(nubPort, B_DEBUG_MESSAGE_SET_TEAM_FLAGS,
&message, sizeof(message));
} while (error == B_INTERRUPTED);
if (error != B_OK) {
fprintf(stderr, "%s: Failed to set team debug flags: %s\n",
kCommandName, strerror(error));
}
return error;
}
status_t
set_thread_debugging_flags(port_id nubPort, thread_id thread, int32 flags)
{
debug_nub_set_thread_flags message;
message.thread = thread;
message.flags = flags;
status_t error = B_OK;
do {
error = write_port(nubPort, B_DEBUG_MESSAGE_SET_THREAD_FLAGS,
&message, sizeof(message));
} while (error == B_INTERRUPTED);
if (error != B_OK) {
fprintf(stderr, "%s: Failed to set thread debug flags: %s\n",
kCommandName, strerror(error));
}
return error;
}
status_t
continue_thread(port_id nubPort, thread_id thread)
{
debug_nub_continue_thread message;
message.thread = thread;
message.handle_event = B_THREAD_DEBUG_HANDLE_EVENT;
message.single_step = false;
status_t error = B_OK;
do {
error = write_port(nubPort, B_DEBUG_MESSAGE_CONTINUE_THREAD,
&message, sizeof(message));
} while (error == B_INTERRUPTED);
if (error != B_OK) {
fprintf(stderr, "%s: Failed to run thread %" B_PRId32 ": %s\n",
kCommandName, thread, strerror(error));
}
return error;
}