Removing the debugger and dependencies. Needs rethinking.
This commit is contained in:
@@ -9,9 +9,6 @@
|
||||
|
||||
#include <xenia/cpu/processor.h>
|
||||
|
||||
#include <jansson.h>
|
||||
|
||||
#include <alloy/runtime/debugger.h>
|
||||
#include <xenia/emulator.h>
|
||||
#include <xenia/export_resolver.h>
|
||||
#include <xenia/cpu/xenon_memory.h>
|
||||
@@ -56,28 +53,11 @@ Processor::Processor(Emulator* emulator) :
|
||||
emulator_(emulator), export_resolver_(emulator->export_resolver()),
|
||||
runtime_(0), memory_(emulator->memory()),
|
||||
interrupt_thread_lock_(NULL), interrupt_thread_state_(NULL),
|
||||
interrupt_thread_block_(0),
|
||||
DebugTarget(emulator->debug_server()) {
|
||||
interrupt_thread_block_(0) {
|
||||
InitializeIfNeeded();
|
||||
|
||||
debug_client_states_lock_ = xe_mutex_alloc();
|
||||
|
||||
emulator_->debug_server()->AddTarget("cpu", this);
|
||||
}
|
||||
|
||||
Processor::~Processor() {
|
||||
emulator_->debug_server()->RemoveTarget("cpu");
|
||||
|
||||
xe_mutex_lock(debug_client_states_lock_);
|
||||
for (auto it = debug_client_states_.begin();
|
||||
it != debug_client_states_.end(); ++it) {
|
||||
DebugClientState* client_state = it->second;
|
||||
delete client_state;
|
||||
}
|
||||
debug_client_states_.clear();
|
||||
xe_mutex_unlock(debug_client_states_lock_);
|
||||
xe_mutex_free(debug_client_states_lock_);
|
||||
|
||||
if (interrupt_thread_block_) {
|
||||
memory_->HeapFree(interrupt_thread_block_, 2048);
|
||||
delete interrupt_thread_state_;
|
||||
@@ -105,31 +85,6 @@ int Processor::Setup() {
|
||||
return result;
|
||||
}
|
||||
|
||||
// Setup debugger events.
|
||||
auto debugger = runtime_->debugger();
|
||||
auto debug_server = emulator_->debug_server();
|
||||
debugger->breakpoint_hit.AddListener(
|
||||
[debug_server](BreakpointHitEvent& e) {
|
||||
const char* breakpoint_id = e.breakpoint()->id();
|
||||
if (!breakpoint_id) {
|
||||
// This is not a breakpoint we know about. Ignore.
|
||||
return;
|
||||
}
|
||||
|
||||
json_t* event_json = json_object();
|
||||
json_t* type_json = json_string("breakpoint");
|
||||
json_object_set_new(event_json, "type", type_json);
|
||||
|
||||
json_t* thread_id_json = json_integer(e.thread_state()->thread_id());
|
||||
json_object_set_new(event_json, "threadId", thread_id_json);
|
||||
json_t* breakpoint_id_json = json_string(breakpoint_id);
|
||||
json_object_set_new(event_json, "breakpointId", breakpoint_id_json);
|
||||
|
||||
debug_server->BroadcastEvent(event_json);
|
||||
|
||||
json_decref(event_json);
|
||||
});
|
||||
|
||||
interrupt_thread_lock_ = xe_mutex_alloc(10000);
|
||||
interrupt_thread_state_ = new XenonThreadState(
|
||||
runtime_, 0, 16 * 1024, 0);
|
||||
@@ -199,640 +154,3 @@ uint64_t Processor::ExecuteInterrupt(
|
||||
xe_mutex_unlock(interrupt_thread_lock_);
|
||||
return result;
|
||||
}
|
||||
|
||||
void Processor::OnDebugClientConnected(uint32_t client_id) {
|
||||
DebugClientState* client_state = new DebugClientState(runtime_);
|
||||
xe_mutex_lock(debug_client_states_lock_);
|
||||
debug_client_states_[client_id] = client_state;
|
||||
xe_mutex_unlock(debug_client_states_lock_);
|
||||
}
|
||||
|
||||
void Processor::OnDebugClientDisconnected(uint32_t client_id) {
|
||||
DebugClientState* client_state = debug_client_states_[client_id];
|
||||
xe_mutex_lock(debug_client_states_lock_);
|
||||
debug_client_states_.erase(client_id);
|
||||
xe_mutex_unlock(debug_client_states_lock_);
|
||||
delete client_state;
|
||||
|
||||
// Whenever we support multiple clients we will need to respect pause
|
||||
// settings. For now, resume until running.
|
||||
runtime_->debugger()->ResumeAllThreads(true);
|
||||
}
|
||||
|
||||
json_t* json_object_set_string_new(
|
||||
json_t* object, const char* key, const char* value) {
|
||||
json_t* value_json = json_string(value);
|
||||
json_object_set_new(object, key, value_json);
|
||||
return value_json;
|
||||
}
|
||||
|
||||
json_t* json_object_set_string_format_new(
|
||||
json_t* object, const char* key, const char* format, ...) {
|
||||
char buffer[1024];
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
xevsnprintfa(buffer, XECOUNT(buffer), format, args);
|
||||
va_end(args);
|
||||
json_t* value_json = json_string(buffer);
|
||||
json_object_set_new(object, key, value_json);
|
||||
return value_json;
|
||||
}
|
||||
|
||||
json_t* json_object_set_integer_new(
|
||||
json_t* object, const char* key, json_int_t value) {
|
||||
json_t* value_json = json_integer(value);
|
||||
json_object_set_new(object, key, value_json);
|
||||
return value_json;
|
||||
}
|
||||
|
||||
json_t* Processor::OnDebugRequest(
|
||||
uint32_t client_id, const char* command, json_t* request,
|
||||
bool& succeeded) {
|
||||
xe_mutex_lock(debug_client_states_lock_);
|
||||
DebugClientState* client_state = debug_client_states_[client_id];
|
||||
xe_mutex_unlock(debug_client_states_lock_);
|
||||
XEASSERTNOTNULL(client_state);
|
||||
|
||||
succeeded = true;
|
||||
if (xestrcmpa(command, "get_module_list") == 0) {
|
||||
json_t* list = json_array();
|
||||
Runtime::ModuleList modules = runtime_->GetModules();
|
||||
for (auto it = modules.begin(); it != modules.end(); ++it) {
|
||||
XexModule* module = (XexModule*)(*it);
|
||||
json_t* module_json = json_object();
|
||||
json_object_set_string_new(module_json, "name", module->name());
|
||||
json_array_append_new(list, module_json);
|
||||
}
|
||||
return list;
|
||||
} else if (xestrcmpa(command, "get_module") == 0) {
|
||||
json_t* module_name_json = json_object_get(request, "module");
|
||||
if (!module_name_json || !json_is_string(module_name_json)) {
|
||||
succeeded = false;
|
||||
return json_string("Module name not specified");
|
||||
}
|
||||
const char* module_name = json_string_value(module_name_json);
|
||||
XexModule* module = (XexModule*)runtime_->GetModule(module_name);
|
||||
if (!module) {
|
||||
succeeded = false;
|
||||
return json_string("Module not found");
|
||||
}
|
||||
return DumpModule(module, succeeded);
|
||||
} else if (xestrcmpa(command, "get_function_list") == 0) {
|
||||
json_t* module_name_json = json_object_get(request, "module");
|
||||
if (!module_name_json || !json_is_string(module_name_json)) {
|
||||
succeeded = false;
|
||||
return json_string("Module name not specified");
|
||||
}
|
||||
const char* module_name = json_string_value(module_name_json);
|
||||
XexModule* module = (XexModule*)runtime_->GetModule(module_name);
|
||||
if (!module) {
|
||||
succeeded = false;
|
||||
return json_string("Module not found");
|
||||
}
|
||||
json_t* since_json = json_object_get(request, "since");
|
||||
if (since_json && !json_is_number(since_json)) {
|
||||
succeeded = false;
|
||||
return json_string("Version since is an invalid type");
|
||||
}
|
||||
size_t since = since_json ?
|
||||
(size_t)json_number_value(since_json) : 0;
|
||||
json_t* list = json_array();
|
||||
size_t version = 0;
|
||||
module->ForEachFunction(since, version, [&](FunctionInfo* info) {
|
||||
json_t* fn_json = json_object();
|
||||
const char* name = info->name();
|
||||
char name_buffer[32];
|
||||
if (!name) {
|
||||
xesnprintfa(name_buffer, XECOUNT(name_buffer), "sub_%.8X",
|
||||
info->address());
|
||||
name = name_buffer;
|
||||
}
|
||||
json_object_set_string_new(fn_json, "name", name);
|
||||
json_object_set_integer_new(fn_json, "address", info->address());
|
||||
json_object_set_integer_new(fn_json, "linkStatus", info->status());
|
||||
json_array_append_new(list, fn_json);
|
||||
});
|
||||
json_t* result = json_object();
|
||||
json_object_set_integer_new(result, "version", version);
|
||||
json_object_set_new(result, "list", list);
|
||||
return result;
|
||||
} else if (xestrcmpa(command, "get_function") == 0) {
|
||||
json_t* address_json = json_object_get(request, "address");
|
||||
if (!address_json || !json_is_number(address_json)) {
|
||||
succeeded = false;
|
||||
return json_string("Function address not specified");
|
||||
}
|
||||
uint64_t address = (uint64_t)json_number_value(address_json);
|
||||
return DumpFunction(address, succeeded);
|
||||
} else if (xestrcmpa(command, "get_thread_states") == 0) {
|
||||
json_t* result = json_object();
|
||||
runtime_->debugger()->ForEachThread([&](ThreadState* thread_state) {
|
||||
json_t* state_json = DumpThreadState((XenonThreadState*)thread_state);
|
||||
char threadIdString[32];
|
||||
xesnprintfa(
|
||||
threadIdString, XECOUNT(threadIdString),
|
||||
"%d", thread_state->thread_id());
|
||||
json_object_set_new(result, threadIdString, state_json);
|
||||
});
|
||||
return result;
|
||||
} else if (xestrcmpa(command, "add_breakpoints") == 0) {
|
||||
// breakpoints: [{}]
|
||||
json_t* breakpoints_json = json_object_get(request, "breakpoints");
|
||||
if (!breakpoints_json || !json_is_array(breakpoints_json)) {
|
||||
succeeded = false;
|
||||
return json_string("Breakpoints not specified");
|
||||
}
|
||||
if (!json_array_size(breakpoints_json)) {
|
||||
// No-op;
|
||||
return json_null();
|
||||
}
|
||||
for (size_t n = 0; n < json_array_size(breakpoints_json); n++) {
|
||||
json_t* breakpoint_json = json_array_get(breakpoints_json, n);
|
||||
if (!breakpoint_json || !json_is_object(breakpoint_json)) {
|
||||
succeeded = false;
|
||||
return json_string("Invalid breakpoint type");
|
||||
}
|
||||
|
||||
json_t* breakpoint_id_json = json_object_get(breakpoint_json, "id");
|
||||
json_t* type_json = json_object_get(breakpoint_json, "type");
|
||||
json_t* address_json = json_object_get(breakpoint_json, "address");
|
||||
if (!breakpoint_id_json || !json_is_string(breakpoint_id_json) ||
|
||||
!type_json || !json_is_string(type_json) ||
|
||||
!address_json || !json_is_number(address_json)) {
|
||||
succeeded = false;
|
||||
return json_string("Invalid breakpoint members");
|
||||
}
|
||||
const char* breakpoint_id = json_string_value(breakpoint_id_json);
|
||||
const char* type_str = json_string_value(type_json);
|
||||
uint64_t address = (uint64_t)json_number_value(address_json);
|
||||
Breakpoint::Type type;
|
||||
if (xestrcmpa(type_str, "temp") == 0) {
|
||||
type = Breakpoint::TEMP_TYPE;
|
||||
} else if (xestrcmpa(type_str, "code") == 0) {
|
||||
type = Breakpoint::CODE_TYPE;
|
||||
} else {
|
||||
succeeded = false;
|
||||
return json_string("Unknown breakpoint type");
|
||||
}
|
||||
|
||||
Breakpoint* breakpoint = new Breakpoint(
|
||||
type, address);
|
||||
breakpoint->set_id(breakpoint_id);
|
||||
if (client_state->AddBreakpoint(breakpoint_id, breakpoint)) {
|
||||
succeeded = false;
|
||||
return json_string("Error adding breakpoint");
|
||||
}
|
||||
}
|
||||
return json_null();
|
||||
} else if (xestrcmpa(command, "remove_breakpoints") == 0) {
|
||||
// breakpointIds: ['id']
|
||||
json_t* breakpoint_ids_json = json_object_get(request, "breakpointIds");
|
||||
if (!breakpoint_ids_json || !json_is_array(breakpoint_ids_json)) {
|
||||
succeeded = false;
|
||||
return json_string("Breakpoint IDs not specified");
|
||||
}
|
||||
if (!json_array_size(breakpoint_ids_json)) {
|
||||
// No-op;
|
||||
return json_null();
|
||||
}
|
||||
for (size_t n = 0; n < json_array_size(breakpoint_ids_json); n++) {
|
||||
json_t* breakpoint_id_json = json_array_get(breakpoint_ids_json, n);
|
||||
if (!breakpoint_id_json || !json_is_string(breakpoint_id_json)) {
|
||||
succeeded = false;
|
||||
return json_string("Invalid breakpoint ID type");
|
||||
}
|
||||
const char* breakpoint_id = json_string_value(breakpoint_id_json);
|
||||
if (client_state->RemoveBreakpoint(breakpoint_id)) {
|
||||
succeeded = false;
|
||||
return json_string("Unable to remove breakpoints");
|
||||
}
|
||||
}
|
||||
return json_null();
|
||||
} else if (xestrcmpa(command, "remove_all_breakpoints") == 0) {
|
||||
if (client_state->RemoveAllBreakpoints()) {
|
||||
succeeded = false;
|
||||
return json_string("Unable to remove breakpoints");
|
||||
}
|
||||
return json_null();
|
||||
} else if (xestrcmpa(command, "continue") == 0) {
|
||||
if (runtime_->debugger()->ResumeAllThreads()) {
|
||||
succeeded = false;
|
||||
return json_string("Unable to resume threads");
|
||||
}
|
||||
return json_null();
|
||||
} else if (xestrcmpa(command, "break") == 0) {
|
||||
if (runtime_->debugger()->SuspendAllThreads()) {
|
||||
succeeded = false;
|
||||
return json_string("Unable to suspend threads");
|
||||
}
|
||||
return json_null();
|
||||
} else if (xestrcmpa(command, "step") == 0) {
|
||||
// threadId
|
||||
return json_null();
|
||||
} else {
|
||||
succeeded = false;
|
||||
return json_string("Unknown command");
|
||||
}
|
||||
}
|
||||
|
||||
json_t* Processor::DumpModule(XexModule* module, bool& succeeded) {
|
||||
auto xex = module->xex();
|
||||
auto header = xe_xex2_get_header(xex);
|
||||
|
||||
auto export_resolver = runtime_->export_resolver();
|
||||
|
||||
json_t* module_json = json_object();
|
||||
|
||||
json_object_set_integer_new(
|
||||
module_json, "moduleFlags", header->module_flags);
|
||||
json_object_set_integer_new(
|
||||
module_json, "systemFlags", header->system_flags);
|
||||
json_object_set_integer_new(
|
||||
module_json, "exeAddress", header->exe_address);
|
||||
json_object_set_integer_new(
|
||||
module_json, "exeEntryPoint", header->exe_entry_point);
|
||||
json_object_set_integer_new(
|
||||
module_json, "exeStackSize", header->exe_stack_size);
|
||||
json_object_set_integer_new(
|
||||
module_json, "exeHeapSize", header->exe_heap_size);
|
||||
|
||||
json_t* exec_info_json = json_object();
|
||||
json_object_set_integer_new(
|
||||
exec_info_json, "mediaId", header->execution_info.media_id);
|
||||
json_object_set_string_format_new(
|
||||
exec_info_json, "version", "%d.%d.%d.%d",
|
||||
header->execution_info.version.major,
|
||||
header->execution_info.version.minor,
|
||||
header->execution_info.version.build,
|
||||
header->execution_info.version.qfe);
|
||||
json_object_set_string_format_new(
|
||||
exec_info_json, "baseVersion", "%d.%d.%d.%d",
|
||||
header->execution_info.base_version.major,
|
||||
header->execution_info.base_version.minor,
|
||||
header->execution_info.base_version.build,
|
||||
header->execution_info.base_version.qfe);
|
||||
json_object_set_integer_new(
|
||||
exec_info_json, "titleId", header->execution_info.title_id);
|
||||
json_object_set_integer_new(
|
||||
exec_info_json, "platform", header->execution_info.platform);
|
||||
json_object_set_integer_new(
|
||||
exec_info_json, "executableTable", header->execution_info.executable_table);
|
||||
json_object_set_integer_new(
|
||||
exec_info_json, "discNumber", header->execution_info.disc_number);
|
||||
json_object_set_integer_new(
|
||||
exec_info_json, "discCount", header->execution_info.disc_count);
|
||||
json_object_set_integer_new(
|
||||
exec_info_json, "savegameId", header->execution_info.savegame_id);
|
||||
json_object_set_new(module_json, "executionInfo", exec_info_json);
|
||||
|
||||
json_t* loader_info_json = json_object();
|
||||
json_object_set_integer_new(
|
||||
loader_info_json, "imageFlags", header->loader_info.image_flags);
|
||||
json_object_set_integer_new(
|
||||
loader_info_json, "gameRegions", header->loader_info.game_regions);
|
||||
json_object_set_integer_new(
|
||||
loader_info_json, "mediaFlags", header->loader_info.media_flags);
|
||||
json_object_set_new(module_json, "loaderInfo", loader_info_json);
|
||||
|
||||
json_t* tls_info_json = json_object();
|
||||
json_object_set_integer_new(
|
||||
tls_info_json, "slotCount", header->tls_info.slot_count);
|
||||
json_object_set_integer_new(
|
||||
tls_info_json, "dataSize", header->tls_info.data_size);
|
||||
json_object_set_integer_new(
|
||||
tls_info_json, "rawDataAddress", header->tls_info.raw_data_address);
|
||||
json_object_set_integer_new(
|
||||
tls_info_json, "rawDataSize", header->tls_info.raw_data_size);
|
||||
json_object_set_new(module_json, "tlsInfo", tls_info_json);
|
||||
|
||||
json_t* headers_json = json_array();
|
||||
for (size_t n = 0; n < header->header_count; n++) {
|
||||
auto opt_header = &header->headers[n];
|
||||
json_t* header_entry_json = json_object();
|
||||
json_object_set_integer_new(
|
||||
header_entry_json, "key", opt_header->key);
|
||||
json_object_set_integer_new(
|
||||
header_entry_json, "length", opt_header->length);
|
||||
json_object_set_integer_new(
|
||||
header_entry_json, "value", opt_header->value);
|
||||
json_array_append_new(headers_json, header_entry_json);
|
||||
}
|
||||
json_object_set_new(module_json, "headers", headers_json);
|
||||
|
||||
json_t* resource_infos_json = json_array();
|
||||
for (size_t n = 0; n < header->resource_info_count; n++) {
|
||||
auto& res = header->resource_infos[n];
|
||||
json_t* resource_info_json = json_object();
|
||||
json_object_set_string_new(
|
||||
resource_info_json, "name", res.name);
|
||||
json_object_set_integer_new(
|
||||
resource_info_json, "address", res.address);
|
||||
json_object_set_integer_new(
|
||||
resource_info_json, "size", res.size);
|
||||
json_array_append_new(resource_infos_json, resource_info_json);
|
||||
}
|
||||
json_object_set_new(module_json, "resourceInfos", resource_infos_json);
|
||||
|
||||
json_t* sections_json = json_array();
|
||||
for (size_t n = 0, i = 0; n < header->section_count; n++) {
|
||||
const xe_xex2_section_t* section = &header->sections[n];
|
||||
const char* type = "unknown";
|
||||
switch (section->info.type) {
|
||||
case XEX_SECTION_CODE:
|
||||
type = "code";
|
||||
break;
|
||||
case XEX_SECTION_DATA:
|
||||
type = "rwdata";
|
||||
break;
|
||||
case XEX_SECTION_READONLY_DATA:
|
||||
type = "rodata";
|
||||
break;
|
||||
}
|
||||
const size_t start_address =
|
||||
header->exe_address + (i * xe_xex2_section_length);
|
||||
const size_t end_address =
|
||||
start_address + (section->info.page_count * xe_xex2_section_length);
|
||||
json_t* section_entry_json = json_object();
|
||||
json_object_set_string_new(
|
||||
section_entry_json, "type", type);
|
||||
json_object_set_integer_new(
|
||||
section_entry_json, "pageCount", section->info.page_count);
|
||||
json_object_set_integer_new(
|
||||
section_entry_json, "startAddress", start_address);
|
||||
json_object_set_integer_new(
|
||||
section_entry_json, "endAddress", end_address);
|
||||
json_object_set_integer_new(
|
||||
section_entry_json, "totalLength",
|
||||
section->info.page_count * xe_xex2_section_length);
|
||||
json_array_append_new(sections_json, section_entry_json);
|
||||
i += section->info.page_count;
|
||||
}
|
||||
json_object_set_new(module_json, "sections", sections_json);
|
||||
|
||||
json_t* static_libraries_json = json_array();
|
||||
for (size_t n = 0; n < header->static_library_count; n++) {
|
||||
const xe_xex2_static_library_t *library = &header->static_libraries[n];
|
||||
json_t* static_library_entry_json = json_object();
|
||||
json_object_set_string_new(
|
||||
static_library_entry_json, "name", library->name);
|
||||
json_object_set_string_format_new(
|
||||
static_library_entry_json, "version", "%d.%d.%d.%d",
|
||||
library->major, library->minor, library->build, library->qfe);
|
||||
json_array_append_new(static_libraries_json, static_library_entry_json);
|
||||
}
|
||||
json_object_set_new(module_json, "staticLibraries", static_libraries_json);
|
||||
|
||||
json_t* library_imports_json = json_array();
|
||||
for (size_t n = 0; n < header->import_library_count; n++) {
|
||||
const xe_xex2_import_library_t* library = &header->import_libraries[n];
|
||||
xe_xex2_import_info_t* import_infos;
|
||||
size_t import_info_count;
|
||||
if (xe_xex2_get_import_infos(
|
||||
xex, library, &import_infos, &import_info_count)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
json_t* import_library_json = json_object();
|
||||
json_object_set_string_new(
|
||||
import_library_json, "name", library->name);
|
||||
json_object_set_string_format_new(
|
||||
import_library_json, "version", "%d.%d.%d.%d",
|
||||
library->version.major, library->version.minor,
|
||||
library->version.build, library->version.qfe);
|
||||
json_object_set_string_format_new(
|
||||
import_library_json, "minVersion", "%d.%d.%d.%d",
|
||||
library->min_version.major, library->min_version.minor,
|
||||
library->min_version.build, library->min_version.qfe);
|
||||
|
||||
json_t* imports_json = json_array();
|
||||
for (size_t m = 0; m < import_info_count; m++) {
|
||||
auto info = &import_infos[m];
|
||||
auto kernel_export = export_resolver->GetExportByOrdinal(
|
||||
library->name, info->ordinal);
|
||||
json_t* import_json = json_object();
|
||||
if (kernel_export) {
|
||||
json_object_set_string_new(
|
||||
import_json, "name", kernel_export->name);
|
||||
json_object_set_new(
|
||||
import_json, "implemented",
|
||||
kernel_export->is_implemented ? json_true() : json_false());
|
||||
} else {
|
||||
json_object_set_new(import_json, "name", json_null());
|
||||
json_object_set_new(import_json, "implemented", json_false());
|
||||
}
|
||||
json_object_set_integer_new(
|
||||
import_json, "ordinal", info->ordinal);
|
||||
json_object_set_integer_new(
|
||||
import_json, "valueAddress", info->value_address);
|
||||
if (kernel_export && kernel_export->type == KernelExport::Variable) {
|
||||
json_object_set_string_new(
|
||||
import_json, "type", "variable");
|
||||
} else if (kernel_export) {
|
||||
json_object_set_string_new(
|
||||
import_json, "type", "function");
|
||||
json_object_set_integer_new(
|
||||
import_json, "thunkAddress", info->thunk_address);
|
||||
} else {
|
||||
json_object_set_string_new(
|
||||
import_json, "type", "unknown");
|
||||
}
|
||||
json_array_append_new(imports_json, import_json);
|
||||
}
|
||||
json_object_set_new(import_library_json, "imports", imports_json);
|
||||
|
||||
json_array_append_new(library_imports_json, import_library_json);
|
||||
}
|
||||
json_object_set_new(module_json, "libraryImports", library_imports_json);
|
||||
|
||||
// TODO(benvanik): exports?
|
||||
|
||||
return module_json;
|
||||
}
|
||||
|
||||
json_t* Processor::DumpFunction(uint64_t address, bool& succeeded) {
|
||||
FunctionInfo* info;
|
||||
if (runtime_->LookupFunctionInfo(address, &info)) {
|
||||
succeeded = false;
|
||||
return json_string("Function not found");
|
||||
}
|
||||
|
||||
// Demand a new function with all debug info retained.
|
||||
// If we ever wanted absolute x64 addresses/etc we could
|
||||
// use the x64 from the function in the symbol table.
|
||||
Function* fn;
|
||||
if (runtime_->frontend()->DefineFunction(
|
||||
info, DEBUG_INFO_ALL_DISASM, &fn)) {
|
||||
succeeded = false;
|
||||
return json_string("Unable to resolve function");
|
||||
}
|
||||
DebugInfo* debug_info = fn->debug_info();
|
||||
if (!debug_info) {
|
||||
succeeded = false;
|
||||
return json_string("No debug info present for function");
|
||||
}
|
||||
|
||||
json_t* fn_json = json_object();
|
||||
const char* name = info->name();
|
||||
char name_buffer[32];
|
||||
if (!name) {
|
||||
xesnprintfa(name_buffer, XECOUNT(name_buffer), "sub_%.8X",
|
||||
info->address());
|
||||
name = name_buffer;
|
||||
}
|
||||
json_t* name_json = json_string(name);
|
||||
json_object_set_new(fn_json, "name", name_json);
|
||||
json_t* start_address_json = json_integer(info->address());
|
||||
json_object_set_new(fn_json, "startAddress", start_address_json);
|
||||
json_t* end_address_json = json_integer(info->end_address());
|
||||
json_object_set_new(fn_json, "endAddress", end_address_json);
|
||||
json_t* link_status_json = json_integer(info->status());
|
||||
json_object_set_new(fn_json, "linkStatus", link_status_json);
|
||||
|
||||
json_t* disasm_json = json_object();
|
||||
json_t* disasm_str_json;
|
||||
disasm_str_json = json_loads(debug_info->source_disasm(), 0, NULL);
|
||||
json_object_set_new(disasm_json, "source", disasm_str_json);
|
||||
disasm_str_json = json_string(debug_info->raw_hir_disasm());
|
||||
json_object_set_new(disasm_json, "rawHir", disasm_str_json);
|
||||
disasm_str_json = json_string(debug_info->hir_disasm());
|
||||
json_object_set_new(disasm_json, "hir", disasm_str_json);
|
||||
disasm_str_json = json_string(debug_info->machine_code_disasm());
|
||||
json_object_set_new(disasm_json, "machineCode", disasm_str_json);
|
||||
json_object_set_new(fn_json, "disasm", disasm_json);
|
||||
|
||||
delete fn;
|
||||
|
||||
succeeded = true;
|
||||
return fn_json;
|
||||
}
|
||||
|
||||
json_t* Processor::DumpThreadState(XenonThreadState* thread_state) {
|
||||
json_t* result = json_object();
|
||||
|
||||
json_object_set_integer_new(result, "id", thread_state->thread_id());
|
||||
json_object_set_string_new(result, "name", thread_state->name());
|
||||
json_object_set_integer_new(
|
||||
result, "stackAddress", thread_state->stack_address());
|
||||
json_object_set_integer_new(
|
||||
result, "stackSize", thread_state->stack_size());
|
||||
json_object_set_integer_new(
|
||||
result, "threadStateAddress", thread_state->thread_state_address());
|
||||
|
||||
char value[32];
|
||||
|
||||
json_t* context_json = json_object();
|
||||
auto context = thread_state->context();
|
||||
|
||||
json_object_set_new(
|
||||
context_json, "pc", json_integer(0));
|
||||
json_object_set_new(
|
||||
context_json, "lr", json_integer(context->lr));
|
||||
|
||||
json_object_set_new(
|
||||
context_json, "ctr", json_integer(context->ctr));
|
||||
xesnprintfa(value, XECOUNT(value), "%.16llX", context->ctr);
|
||||
json_object_set_new(
|
||||
context_json, "ctrh", json_string(value));
|
||||
xesnprintfa(value, XECOUNT(value), "%lld", context->ctr);
|
||||
json_object_set_new(
|
||||
context_json, "ctrs", json_string(value));
|
||||
|
||||
// xer
|
||||
// cr*
|
||||
// fpscr
|
||||
|
||||
json_t* r_json = json_array();
|
||||
for (size_t n = 0; n < 32; n++) {
|
||||
json_array_append_new(r_json, json_integer(context->r[n]));
|
||||
}
|
||||
json_object_set_new(context_json, "r", r_json);
|
||||
json_t* rh_json = json_array();
|
||||
for (size_t n = 0; n < 32; n++) {
|
||||
xesnprintfa(value, XECOUNT(value), "%.16llX", context->r[n]);
|
||||
json_array_append_new(rh_json, json_string(value));
|
||||
}
|
||||
json_object_set_new(context_json, "rh", rh_json);
|
||||
json_t* rs_json = json_array();
|
||||
for (size_t n = 0; n < 32; n++) {
|
||||
xesnprintfa(value, XECOUNT(value), "%lld", context->r[n]);
|
||||
json_array_append_new(rs_json, json_string(value));
|
||||
}
|
||||
json_object_set_new(context_json, "rs", rs_json);
|
||||
|
||||
json_t* f_json = json_array();
|
||||
for (size_t n = 0; n < 32; n++) {
|
||||
json_array_append_new(f_json, json_real(context->f[n]));
|
||||
}
|
||||
json_object_set_new(context_json, "f", f_json);
|
||||
json_t* fh_json = json_array();
|
||||
for (size_t n = 0; n < 32; n++) {
|
||||
union {
|
||||
double f;
|
||||
uint64_t i;
|
||||
} fi = { context->f[n] };
|
||||
xesnprintfa(value, XECOUNT(value), "%.16llX", fi.i);
|
||||
json_array_append_new(fh_json, json_string(value));
|
||||
}
|
||||
json_object_set_new(context_json, "fh", fh_json);
|
||||
|
||||
json_t* v_json = json_array();
|
||||
for (size_t n = 0; n < 128; n++) {
|
||||
auto& v = context->v[n];
|
||||
json_t* vec4_json = json_array();
|
||||
json_array_append_new(vec4_json, json_integer(v.ix));
|
||||
json_array_append_new(vec4_json, json_integer(v.iy));
|
||||
json_array_append_new(vec4_json, json_integer(v.iz));
|
||||
json_array_append_new(vec4_json, json_integer(v.iw));
|
||||
json_array_append_new(v_json, vec4_json);
|
||||
}
|
||||
json_object_set_new(context_json, "v", v_json);
|
||||
|
||||
json_object_set_new(result, "context", context_json);
|
||||
|
||||
// TODO(benvanik): callstack
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Processor::DebugClientState::DebugClientState(XenonRuntime* runtime) :
|
||||
runtime_(runtime) {
|
||||
breakpoints_lock_ = xe_mutex_alloc(10000);
|
||||
}
|
||||
|
||||
Processor::DebugClientState::~DebugClientState() {
|
||||
RemoveAllBreakpoints();
|
||||
xe_mutex_free(breakpoints_lock_);
|
||||
}
|
||||
|
||||
int Processor::DebugClientState::AddBreakpoint(
|
||||
const char* breakpoint_id, Breakpoint* breakpoint) {
|
||||
xe_mutex_lock(breakpoints_lock_);
|
||||
breakpoints_[breakpoint_id] = breakpoint;
|
||||
int result = runtime_->debugger()->AddBreakpoint(breakpoint);
|
||||
xe_mutex_unlock(breakpoints_lock_);
|
||||
return result;
|
||||
}
|
||||
|
||||
int Processor::DebugClientState::RemoveBreakpoint(const char* breakpoint_id) {
|
||||
xe_mutex_lock(breakpoints_lock_);
|
||||
Breakpoint* breakpoint = breakpoints_[breakpoint_id];
|
||||
if (breakpoint) {
|
||||
breakpoints_.erase(breakpoint_id);
|
||||
runtime_->debugger()->RemoveBreakpoint(breakpoint);
|
||||
delete breakpoint;
|
||||
}
|
||||
xe_mutex_unlock(breakpoints_lock_);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Processor::DebugClientState::RemoveAllBreakpoints() {
|
||||
xe_mutex_lock(breakpoints_lock_);
|
||||
for (auto it = breakpoints_.begin(); it != breakpoints_.end(); ++it) {
|
||||
Breakpoint* breakpoint = it->second;
|
||||
runtime_->debugger()->RemoveBreakpoint(breakpoint);
|
||||
delete breakpoint;
|
||||
}
|
||||
breakpoints_.clear();
|
||||
xe_mutex_unlock(breakpoints_lock_);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
#define XENIA_CPU_PROCESSOR_H_
|
||||
|
||||
#include <xenia/core.h>
|
||||
#include <xenia/debug/debug_target.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
@@ -28,7 +27,7 @@ namespace xe {
|
||||
namespace cpu {
|
||||
|
||||
|
||||
class Processor : public debug::DebugTarget {
|
||||
class Processor {
|
||||
public:
|
||||
Processor(Emulator* emulator);
|
||||
~Processor();
|
||||
@@ -48,17 +47,6 @@ public:
|
||||
uint64_t ExecuteInterrupt(
|
||||
uint32_t cpu, uint64_t address, uint64_t args[], size_t arg_count);
|
||||
|
||||
virtual void OnDebugClientConnected(uint32_t client_id);
|
||||
virtual void OnDebugClientDisconnected(uint32_t client_id);
|
||||
virtual json_t* OnDebugRequest(
|
||||
uint32_t client_id, const char* command, json_t* request,
|
||||
bool& succeeded);
|
||||
|
||||
private:
|
||||
json_t* DumpModule(XexModule* module, bool& succeeded);
|
||||
json_t* DumpFunction(uint64_t address, bool& succeeded);
|
||||
json_t* DumpThreadState(XenonThreadState* thread_state);
|
||||
|
||||
private:
|
||||
Emulator* emulator_;
|
||||
ExportResolver* export_resolver_;
|
||||
@@ -69,27 +57,6 @@ private:
|
||||
xe_mutex_t* interrupt_thread_lock_;
|
||||
XenonThreadState* interrupt_thread_state_;
|
||||
uint64_t interrupt_thread_block_;
|
||||
|
||||
class DebugClientState {
|
||||
public:
|
||||
DebugClientState(XenonRuntime* runtime);
|
||||
~DebugClientState();
|
||||
|
||||
int AddBreakpoint(const char* breakpoint_id,
|
||||
alloy::runtime::Breakpoint* breakpoint);
|
||||
int RemoveBreakpoint(const char* breakpoint_id);
|
||||
int RemoveAllBreakpoints();
|
||||
|
||||
private:
|
||||
XenonRuntime* runtime_;
|
||||
|
||||
xe_mutex_t* breakpoints_lock_;
|
||||
typedef std::unordered_map<std::string, alloy::runtime::Breakpoint*> BreakpointMap;
|
||||
BreakpointMap breakpoints_;
|
||||
};
|
||||
xe_mutex_t* debug_client_states_lock_;
|
||||
typedef std::unordered_map<uint32_t, DebugClientState*> DebugClientStateMap;
|
||||
DebugClientStateMap debug_client_states_;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include <xenia/debug/debug_client.h>
|
||||
|
||||
#include <xenia/debug/debug_server.h>
|
||||
|
||||
|
||||
using namespace xe;
|
||||
using namespace xe::debug;
|
||||
|
||||
|
||||
uint32_t DebugClient::next_client_id_ = 1;
|
||||
|
||||
|
||||
DebugClient::DebugClient(DebugServer* debug_server) :
|
||||
debug_server_(debug_server),
|
||||
readied_(false) {
|
||||
client_id_ = next_client_id_++;
|
||||
debug_server_->AddClient(this);
|
||||
}
|
||||
|
||||
DebugClient::~DebugClient() {
|
||||
debug_server_->RemoveClient(this);
|
||||
}
|
||||
|
||||
void DebugClient::MakeReady() {
|
||||
if (readied_) {
|
||||
return;
|
||||
}
|
||||
debug_server_->ReadyClient(this);
|
||||
readied_ = true;
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef XENIA_DEBUG_DEBUG_CLIENT_H_
|
||||
#define XENIA_DEBUG_DEBUG_CLIENT_H_
|
||||
|
||||
#include <xenia/common.h>
|
||||
#include <xenia/core.h>
|
||||
|
||||
|
||||
XEDECLARECLASS2(xe, debug, DebugServer);
|
||||
|
||||
struct json_t;
|
||||
|
||||
|
||||
namespace xe {
|
||||
namespace debug {
|
||||
|
||||
|
||||
class DebugClient {
|
||||
public:
|
||||
DebugClient(DebugServer* debug_server);
|
||||
virtual ~DebugClient();
|
||||
|
||||
uint32_t client_id() const { return client_id_; }
|
||||
|
||||
virtual int Setup() = 0;
|
||||
virtual void Close() = 0;
|
||||
|
||||
virtual void SendEvent(json_t* event_json) = 0;
|
||||
|
||||
protected:
|
||||
void MakeReady();
|
||||
|
||||
protected:
|
||||
static uint32_t next_client_id_;
|
||||
|
||||
DebugServer* debug_server_;
|
||||
uint32_t client_id_;
|
||||
bool readied_;
|
||||
};
|
||||
|
||||
|
||||
} // namespace debug
|
||||
} // namespace xe
|
||||
|
||||
|
||||
#endif // XENIA_DEBUG_DEBUG_CLIENT_H_
|
||||
@@ -1,186 +0,0 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include <xenia/debug/debug_server.h>
|
||||
|
||||
#include <gflags/gflags.h>
|
||||
|
||||
#include <xenia/emulator.h>
|
||||
#include <xenia/debug/debug_client.h>
|
||||
#include <xenia/debug/debug_target.h>
|
||||
#include <xenia/debug/protocol.h>
|
||||
#include <xenia/debug/protocols/gdb/gdb_protocol.h>
|
||||
#include <xenia/debug/protocols/ws/ws_protocol.h>
|
||||
|
||||
|
||||
using namespace xe;
|
||||
using namespace xe::debug;
|
||||
|
||||
|
||||
DEFINE_bool(wait_for_debugger, false,
|
||||
"Whether to wait for the debugger to attach before launching.");
|
||||
|
||||
|
||||
DebugServer::DebugServer(Emulator* emulator) :
|
||||
emulator_(emulator), lock_(0) {
|
||||
lock_ = xe_mutex_alloc(10000);
|
||||
|
||||
client_event_ = CreateEvent(NULL, FALSE, FALSE, NULL);
|
||||
|
||||
protocols_.push_back(
|
||||
new protocols::gdb::GDBProtocol(this));
|
||||
protocols_.push_back(
|
||||
new protocols::ws::WSProtocol(this));
|
||||
}
|
||||
|
||||
DebugServer::~DebugServer() {
|
||||
Shutdown();
|
||||
|
||||
CloseHandle(client_event_);
|
||||
|
||||
xe_free(lock_);
|
||||
lock_ = 0;
|
||||
}
|
||||
|
||||
bool DebugServer::has_clients() {
|
||||
xe_mutex_lock(lock_);
|
||||
bool has_clients = clients_.size() > 0;
|
||||
xe_mutex_unlock(lock_);
|
||||
return has_clients;
|
||||
}
|
||||
|
||||
int DebugServer::Startup() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DebugServer::BeforeEntry() {
|
||||
// HACK(benvanik): say we are ok even if we have no listener.
|
||||
if (!protocols_.size()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Start listeners.
|
||||
// This may launch threads and such.
|
||||
for (std::vector<Protocol*>::iterator it = protocols_.begin();
|
||||
it != protocols_.end(); ++it) {
|
||||
Protocol* protocol = *it;
|
||||
if (protocol->Setup()) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// If desired, wait until the first client connects.
|
||||
if (FLAGS_wait_for_debugger) {
|
||||
XELOGI("Waiting for debugger...");
|
||||
if (WaitForClient()) {
|
||||
return 1;
|
||||
}
|
||||
XELOGI("Debugger attached, continuing...");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void DebugServer::Shutdown() {
|
||||
xe_mutex_lock(lock_);
|
||||
|
||||
std::vector<DebugClient*> clients(clients_.begin(), clients_.end());
|
||||
clients_.clear();
|
||||
for (std::vector<DebugClient*>::iterator it = clients.begin();
|
||||
it != clients.end(); ++it) {
|
||||
delete *it;
|
||||
}
|
||||
|
||||
std::vector<Protocol*> protocols(protocols_.begin(), protocols_.end());
|
||||
protocols_.clear();
|
||||
for (std::vector<Protocol*>::iterator it = protocols.begin();
|
||||
it != protocols.end(); ++it) {
|
||||
delete *it;
|
||||
}
|
||||
|
||||
xe_mutex_unlock(lock_);
|
||||
}
|
||||
|
||||
void DebugServer::AddTarget(const char* name, DebugTarget* target) {
|
||||
xe_mutex_lock(lock_);
|
||||
targets_[name] = target;
|
||||
xe_mutex_unlock(lock_);
|
||||
}
|
||||
|
||||
void DebugServer::RemoveTarget(const char* name) {
|
||||
xe_mutex_lock(lock_);
|
||||
targets_[name] = NULL;
|
||||
xe_mutex_unlock(lock_);
|
||||
}
|
||||
|
||||
DebugTarget* DebugServer::GetTarget(const char* name) {
|
||||
xe_mutex_lock(lock_);
|
||||
DebugTarget* target = targets_[name];
|
||||
xe_mutex_unlock(lock_);
|
||||
return target;
|
||||
}
|
||||
|
||||
void DebugServer::BroadcastEvent(json_t* event_json) {
|
||||
// TODO(benvanik): avoid lock somehow?
|
||||
xe_mutex_lock(lock_);
|
||||
for (auto client : clients_) {
|
||||
client->SendEvent(event_json);
|
||||
}
|
||||
xe_mutex_unlock(lock_);
|
||||
}
|
||||
|
||||
int DebugServer::WaitForClient() {
|
||||
while (!has_clients()) {
|
||||
WaitForSingleObject(client_event_, INFINITE);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void DebugServer::AddClient(DebugClient* debug_client) {
|
||||
xe_mutex_lock(lock_);
|
||||
|
||||
// Only one debugger at a time right now. Kill any old one.
|
||||
while (clients_.size()) {
|
||||
DebugClient* old_client = clients_.back();
|
||||
clients_.pop_back();
|
||||
old_client->Close();
|
||||
}
|
||||
|
||||
clients_.push_back(debug_client);
|
||||
|
||||
// Notify targets.
|
||||
for (auto it = targets_.begin(); it != targets_.end(); ++it) {
|
||||
it->second->OnDebugClientConnected(debug_client->client_id());
|
||||
}
|
||||
|
||||
xe_mutex_unlock(lock_);
|
||||
}
|
||||
|
||||
void DebugServer::ReadyClient(DebugClient* debug_client) {
|
||||
SetEvent(client_event_);
|
||||
}
|
||||
|
||||
void DebugServer::RemoveClient(DebugClient* debug_client) {
|
||||
xe_mutex_lock(lock_);
|
||||
|
||||
// Notify targets.
|
||||
for (auto it = targets_.begin(); it != targets_.end(); ++it) {
|
||||
it->second->OnDebugClientDisconnected(debug_client->client_id());
|
||||
}
|
||||
|
||||
for (std::vector<DebugClient*>::iterator it = clients_.begin();
|
||||
it != clients_.end(); ++it) {
|
||||
if (*it == debug_client) {
|
||||
clients_.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
xe_mutex_unlock(lock_);
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef XENIA_DEBUG_DEBUG_SERVER_H_
|
||||
#define XENIA_DEBUG_DEBUG_SERVER_H_
|
||||
|
||||
#include <xenia/common.h>
|
||||
#include <xenia/core.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
|
||||
XEDECLARECLASS1(xe, Emulator);
|
||||
XEDECLARECLASS2(xe, debug, DebugClient);
|
||||
XEDECLARECLASS2(xe, debug, DebugTarget);
|
||||
XEDECLARECLASS2(xe, debug, Protocol);
|
||||
|
||||
struct json_t;
|
||||
|
||||
|
||||
namespace xe {
|
||||
namespace debug {
|
||||
|
||||
|
||||
class DebugServer {
|
||||
public:
|
||||
DebugServer(Emulator* emulator);
|
||||
virtual ~DebugServer();
|
||||
|
||||
Emulator* emulator() const { return emulator_; }
|
||||
|
||||
bool has_clients();
|
||||
|
||||
int Startup();
|
||||
int BeforeEntry();
|
||||
void Shutdown();
|
||||
|
||||
void AddTarget(const char* name, DebugTarget* target);
|
||||
void RemoveTarget(const char* name);
|
||||
DebugTarget* GetTarget(const char* name);
|
||||
|
||||
void BroadcastEvent(json_t* event_json);
|
||||
|
||||
int WaitForClient();
|
||||
|
||||
private:
|
||||
void AddClient(DebugClient* debug_client);
|
||||
void ReadyClient(DebugClient* debug_client);
|
||||
void RemoveClient(DebugClient* debug_client);
|
||||
|
||||
friend class DebugClient;
|
||||
|
||||
private:
|
||||
Emulator* emulator_;
|
||||
std::vector<Protocol*> protocols_;
|
||||
|
||||
xe_mutex_t* lock_;
|
||||
typedef std::unordered_map<std::string, DebugTarget*> TargetMap;
|
||||
TargetMap targets_;
|
||||
std::vector<DebugClient*> clients_;
|
||||
HANDLE client_event_;
|
||||
};
|
||||
|
||||
|
||||
} // namespace debug
|
||||
} // namespace xe
|
||||
|
||||
|
||||
#endif // XENIA_DEBUG_DEBUG_SERVER_H_
|
||||
@@ -1,48 +0,0 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef XENIA_DEBUG_DEBUG_TARGET_H_
|
||||
#define XENIA_DEBUG_DEBUG_TARGET_H_
|
||||
|
||||
#include <xenia/common.h>
|
||||
#include <xenia/core.h>
|
||||
#include <xenia/debug/debug_server.h>
|
||||
|
||||
|
||||
struct json_t;
|
||||
|
||||
|
||||
namespace xe {
|
||||
namespace debug {
|
||||
|
||||
|
||||
class DebugTarget {
|
||||
public:
|
||||
DebugTarget(DebugServer* debug_server) :
|
||||
debug_server_(debug_server) {}
|
||||
virtual ~DebugTarget() {}
|
||||
|
||||
DebugServer* debug_server() const { return debug_server_; }
|
||||
|
||||
virtual void OnDebugClientConnected(uint32_t client_id) {}
|
||||
virtual void OnDebugClientDisconnected(uint32_t client_id) {}
|
||||
virtual json_t* OnDebugRequest(
|
||||
uint32_t client_id, const char* command, json_t* request,
|
||||
bool& succeeded) = 0;
|
||||
|
||||
protected:
|
||||
DebugServer* debug_server_;
|
||||
};
|
||||
|
||||
|
||||
} // namespace debug
|
||||
} // namespace xe
|
||||
|
||||
|
||||
#endif // XENIA_DEBUG_DEBUG_TARGET_H_
|
||||
@@ -1,22 +0,0 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include <xenia/debug/protocol.h>
|
||||
|
||||
|
||||
using namespace xe;
|
||||
using namespace xe::debug;
|
||||
|
||||
|
||||
Protocol::Protocol(DebugServer* debug_server) :
|
||||
debug_server_(debug_server) {
|
||||
}
|
||||
|
||||
Protocol::~Protocol() {
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef XENIA_DEBUG_PROTOCOL_H_
|
||||
#define XENIA_DEBUG_PROTOCOL_H_
|
||||
|
||||
#include <xenia/common.h>
|
||||
#include <xenia/core.h>
|
||||
|
||||
|
||||
XEDECLARECLASS2(xe, debug, DebugServer);
|
||||
|
||||
|
||||
namespace xe {
|
||||
namespace debug {
|
||||
|
||||
|
||||
class Protocol {
|
||||
public:
|
||||
Protocol(DebugServer* debug_server);
|
||||
virtual ~Protocol();
|
||||
|
||||
virtual int Setup() = 0;
|
||||
|
||||
protected:
|
||||
DebugServer* debug_server_;
|
||||
};
|
||||
|
||||
|
||||
} // namespace debug
|
||||
} // namespace xe
|
||||
|
||||
|
||||
#endif // XENIA_DEBUG_PROTOCOL_H_
|
||||
@@ -1,456 +0,0 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include <xenia/debug/protocols/gdb/gdb_client.h>
|
||||
|
||||
#include <xenia/debug/debug_server.h>
|
||||
#include <xenia/debug/protocols/gdb/message.h>
|
||||
|
||||
|
||||
using namespace xe;
|
||||
using namespace xe::debug;
|
||||
using namespace xe::debug::protocols::gdb;
|
||||
|
||||
|
||||
GDBClient::GDBClient(DebugServer* debug_server, socket_t socket_id) :
|
||||
DebugClient(debug_server),
|
||||
thread_(NULL),
|
||||
socket_id_(socket_id),
|
||||
current_reader_(0), send_queue_stalled_(false) {
|
||||
mutex_ = xe_mutex_alloc(1000);
|
||||
|
||||
loop_ = xe_socket_loop_create(socket_id);
|
||||
}
|
||||
|
||||
GDBClient::~GDBClient() {
|
||||
xe_mutex_t* mutex = mutex_;
|
||||
xe_mutex_lock(mutex);
|
||||
|
||||
mutex_ = NULL;
|
||||
|
||||
delete current_reader_;
|
||||
for (auto it = message_reader_pool_.begin();
|
||||
it != message_reader_pool_.end(); ++it) {
|
||||
delete *it;
|
||||
}
|
||||
for (auto it = message_writer_pool_.begin();
|
||||
it != message_writer_pool_.end(); ++it) {
|
||||
delete *it;
|
||||
}
|
||||
for (auto it = send_queue_.begin(); it != send_queue_.end(); ++it) {
|
||||
delete *it;
|
||||
}
|
||||
|
||||
xe_socket_close(socket_id_);
|
||||
socket_id_ = 0;
|
||||
|
||||
xe_socket_loop_destroy(loop_);
|
||||
loop_ = NULL;
|
||||
|
||||
xe_mutex_unlock(mutex);
|
||||
xe_mutex_free(mutex);
|
||||
|
||||
xe_thread_release(thread_);
|
||||
}
|
||||
|
||||
int GDBClient::Setup() {
|
||||
// Prep the socket.
|
||||
xe_socket_set_keepalive(socket_id_, true);
|
||||
xe_socket_set_nodelay(socket_id_, true);
|
||||
|
||||
thread_ = xe_thread_create("GDB Debugger Client", StartCallback, this);
|
||||
return xe_thread_start(thread_);
|
||||
}
|
||||
|
||||
void GDBClient::Close() {
|
||||
xe_socket_close(socket_id_);
|
||||
socket_id_ = 0;
|
||||
}
|
||||
|
||||
void GDBClient::StartCallback(void* param) {
|
||||
GDBClient* client = reinterpret_cast<GDBClient*>(param);
|
||||
client->EventThread();
|
||||
}
|
||||
|
||||
int GDBClient::PerformHandshake() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void GDBClient::EventThread() {
|
||||
// Enable non-blocking IO on the socket.
|
||||
xe_socket_set_nonblock(socket_id_, true);
|
||||
|
||||
// First run the HTTP handshake.
|
||||
// This will fail if the connection is not for websockets.
|
||||
if (PerformHandshake()) {
|
||||
delete this;
|
||||
return;
|
||||
}
|
||||
|
||||
MakeReady();
|
||||
|
||||
// Loop forever.
|
||||
while (true) {
|
||||
// Wait on the event.
|
||||
if (xe_socket_loop_poll(loop_, true, send_queue_stalled_)) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Handle any self-generated events to queue messages.
|
||||
xe_mutex_lock(mutex_);
|
||||
for (auto it = pending_messages_.begin();
|
||||
it != pending_messages_.end(); it++) {
|
||||
MessageWriter* message = *it;
|
||||
send_queue_.push_back(message);
|
||||
}
|
||||
pending_messages_.clear();
|
||||
xe_mutex_unlock(mutex_);
|
||||
|
||||
// Handle websocket messages.
|
||||
if (xe_socket_loop_check_socket_recv(loop_) && CheckReceive()) {
|
||||
// Error handling the event.
|
||||
XELOGE("Error handling WebSocket data");
|
||||
break;
|
||||
}
|
||||
if (!send_queue_stalled_ || xe_socket_loop_check_socket_send(loop_)) {
|
||||
if (PumpSendQueue()) {
|
||||
// Error handling the event.
|
||||
XELOGE("Error handling WebSocket data");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int GDBClient::CheckReceive() {
|
||||
uint8_t buffer[4096];
|
||||
while (true) {
|
||||
int error_code = 0;
|
||||
int64_t r = xe_socket_recv(
|
||||
socket_id_, buffer, sizeof(buffer), 0, &error_code);
|
||||
if (r == -1) {
|
||||
if (error_code == EAGAIN || error_code == EWOULDBLOCK) {
|
||||
return 0;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
} else if (r == 0) {
|
||||
return 1;
|
||||
} else {
|
||||
// Append current reader until #.
|
||||
int64_t pos = 0;
|
||||
if (current_reader_) {
|
||||
for (; pos < r; pos++) {
|
||||
if (buffer[pos] == '#') {
|
||||
pos++;
|
||||
current_reader_->Append(buffer, pos);
|
||||
MessageReader* message = current_reader_;
|
||||
current_reader_ = NULL;
|
||||
DispatchMessage(message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Loop reading all messages in the buffer.
|
||||
for (; pos < r; pos++) {
|
||||
if (buffer[pos] == '$') {
|
||||
if (message_reader_pool_.size()) {
|
||||
current_reader_ = message_reader_pool_.back();
|
||||
message_reader_pool_.pop_back();
|
||||
current_reader_->Reset();
|
||||
} else {
|
||||
current_reader_ = new MessageReader();
|
||||
}
|
||||
size_t start_pos = pos;
|
||||
|
||||
// Scan until next #.
|
||||
bool found_end = false;
|
||||
for (; pos < r; pos++) {
|
||||
if (buffer[pos] == '#') {
|
||||
pos++;
|
||||
current_reader_->Append(buffer + start_pos, pos - start_pos);
|
||||
MessageReader* message = current_reader_;
|
||||
current_reader_ = NULL;
|
||||
DispatchMessage(message);
|
||||
found_end = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found_end) {
|
||||
// Ran out of bytes before message ended, keep around.
|
||||
current_reader_->Append(buffer + start_pos, r - start_pos);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void GDBClient::DispatchMessage(MessageReader* message) {
|
||||
// $[message]#
|
||||
const char* str = message->GetString();
|
||||
str++; // skip $
|
||||
|
||||
printf("GDB: %s", str);
|
||||
|
||||
bool handled = false;
|
||||
switch (str[0]) {
|
||||
case '!':
|
||||
// Enable extended mode.
|
||||
Send("OK");
|
||||
handled = true;
|
||||
break;
|
||||
case 'v':
|
||||
// Verbose packets.
|
||||
if (strstr(str, "vRun") == str) {
|
||||
Send("S05");
|
||||
handled = true;
|
||||
} else if (strstr(str, "vCont") == str) {
|
||||
Send("S05");
|
||||
handled = true;
|
||||
}
|
||||
break;
|
||||
case 'q':
|
||||
// Query packets.
|
||||
switch (str[1]) {
|
||||
case 'C':
|
||||
// Get current thread ID.
|
||||
Send("QC01");
|
||||
handled = true;
|
||||
break;
|
||||
case 'R':
|
||||
// Command.
|
||||
Send("OK");
|
||||
handled = true;
|
||||
break;
|
||||
default:
|
||||
if (strstr(str, "qfThreadInfo") == str) {
|
||||
// Start of thread list request.
|
||||
Send("m01");
|
||||
handled = true;
|
||||
} else if (strstr(str, "qsThreadInfo") == str) {
|
||||
// Continuation of thread list.
|
||||
Send("l"); // l = last
|
||||
handled = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
#if 0
|
||||
case 'H':
|
||||
// Set current thread.
|
||||
break;
|
||||
#endif
|
||||
|
||||
case 'g':
|
||||
// Read all registers.
|
||||
HandleReadRegisters(str + 1);
|
||||
handled = true;
|
||||
break;
|
||||
case 'G':
|
||||
// Write all registers.
|
||||
HandleWriteRegisters(str + 1);
|
||||
handled = true;
|
||||
break;
|
||||
case 'p':
|
||||
// Read register.
|
||||
HandleReadRegister(str + 1);
|
||||
handled = true;
|
||||
break;
|
||||
case 'P':
|
||||
// Write register.
|
||||
HandleWriteRegister(str + 1);
|
||||
handled = true;
|
||||
break;
|
||||
|
||||
case 'm':
|
||||
// Read memory.
|
||||
HandleReadMemory(str + 1);
|
||||
handled = true;
|
||||
case 'M':
|
||||
// Write memory.
|
||||
HandleWriteMemory(str + 1);
|
||||
handled = true;
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
// Insert breakpoint.
|
||||
HandleAddBreakpoint(str + 1);
|
||||
handled = true;
|
||||
break;
|
||||
case 'z':
|
||||
// Remove breakpoint.
|
||||
HandleRemoveBreakpoint(str + 1);
|
||||
handled = true;
|
||||
break;
|
||||
|
||||
case '?':
|
||||
// Query halt reason.
|
||||
Send("S05");
|
||||
handled = true;
|
||||
break;
|
||||
case 'c':
|
||||
// Continue.
|
||||
// Deprecated: vCont should be used instead.
|
||||
// NOTE: reply is sent on halt, not right now.
|
||||
Send("S05");
|
||||
handled = true;
|
||||
break;
|
||||
case 's':
|
||||
// Single step.
|
||||
// NOTE: reply is sent on halt, not right now.
|
||||
Send("S05");
|
||||
handled = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!handled) {
|
||||
// Unknown packet type. We should ACK just to keep IDA happy.
|
||||
XELOGW("Unknown GDB packet type: %c", str[0]);
|
||||
Send("");
|
||||
}
|
||||
}
|
||||
|
||||
int GDBClient::PumpSendQueue() {
|
||||
send_queue_stalled_ = false;
|
||||
for (auto it = send_queue_.begin(); it != send_queue_.end(); it++) {
|
||||
MessageWriter* message = *it;
|
||||
int error_code = 0;
|
||||
int64_t r;
|
||||
const uint8_t* data = message->buffer() + message->offset();
|
||||
size_t bytes_remaining = message->length();
|
||||
while (bytes_remaining) {
|
||||
r = xe_socket_send(
|
||||
socket_id_, data, bytes_remaining, 0, &error_code);
|
||||
if (r == -1) {
|
||||
if (error_code == EAGAIN || error_code == EWOULDBLOCK) {
|
||||
// Message did not finish sending.
|
||||
send_queue_stalled_ = true;
|
||||
return 0;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
bytes_remaining -= r;
|
||||
data += r;
|
||||
message->set_offset(message->offset() + r);
|
||||
}
|
||||
}
|
||||
if (!bytes_remaining) {
|
||||
xe_mutex_lock(mutex_);
|
||||
message_writer_pool_.push_back(message);
|
||||
xe_mutex_unlock(mutex_);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
MessageWriter* GDBClient::BeginSend() {
|
||||
MessageWriter* message = NULL;
|
||||
xe_mutex_lock(mutex_);
|
||||
if (message_writer_pool_.size()) {
|
||||
message = message_writer_pool_.back();
|
||||
message_writer_pool_.pop_back();
|
||||
}
|
||||
xe_mutex_unlock(mutex_);
|
||||
if (message) {
|
||||
message->Reset();
|
||||
} else {
|
||||
message = new MessageWriter();
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
void GDBClient::EndSend(MessageWriter* message) {
|
||||
message->Finalize();
|
||||
|
||||
xe_mutex_lock(mutex_);
|
||||
pending_messages_.push_back(message);
|
||||
bool needs_signal = pending_messages_.size() == 1;
|
||||
xe_mutex_unlock(mutex_);
|
||||
|
||||
if (needs_signal) {
|
||||
// Notify the poll().
|
||||
xe_socket_loop_set_queued_write(loop_);
|
||||
}
|
||||
}
|
||||
|
||||
void GDBClient::Send(const char* format, ...) {
|
||||
auto message = BeginSend();
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
message->AppendVarargs(format, args);
|
||||
va_end(args);
|
||||
EndSend(message);
|
||||
}
|
||||
|
||||
void GDBClient::HandleReadRegisters(const char* str) {
|
||||
auto message = BeginSend();
|
||||
for (int32_t n = 0; n < 32; n++) {
|
||||
// gpr
|
||||
message->Append("%08X", n);
|
||||
}
|
||||
for (int64_t n = 0; n < 32; n++) {
|
||||
// fpr
|
||||
message->Append("%016llX", n);
|
||||
}
|
||||
message->Append("%08X", 0x8202FB40); // pc
|
||||
message->Append("%08X", 65); // msr
|
||||
message->Append("%08X", 66); // cr
|
||||
message->Append("%08X", 67); // lr
|
||||
message->Append("%08X", 68); // ctr
|
||||
message->Append("%08X", 69); // xer
|
||||
message->Append("%08X", 70); // fpscr
|
||||
EndSend(message);
|
||||
}
|
||||
|
||||
void GDBClient::HandleWriteRegisters(const char* str) {
|
||||
Send("OK");
|
||||
}
|
||||
|
||||
void GDBClient::HandleReadRegister(const char* str) {
|
||||
// p...HH
|
||||
// HH = hex digit indicating register #
|
||||
auto message = BeginSend();
|
||||
message->Append("%.8X", 0x8202FB40);
|
||||
EndSend(message);
|
||||
}
|
||||
|
||||
void GDBClient::HandleWriteRegister(const char* str) {
|
||||
Send("OK");
|
||||
}
|
||||
|
||||
void GDBClient::HandleReadMemory(const char* str) {
|
||||
// m...ADDR,SIZE
|
||||
uint32_t addr;
|
||||
uint32_t size;
|
||||
scanf("%X,%X", &addr, &size);
|
||||
auto message = BeginSend();
|
||||
for (size_t n = 0; n < size; n++) {
|
||||
message->Append("00");
|
||||
}
|
||||
EndSend(message);
|
||||
}
|
||||
|
||||
void GDBClient::HandleWriteMemory(const char* str) {
|
||||
Send("OK");
|
||||
}
|
||||
|
||||
void GDBClient::HandleAddBreakpoint(const char* str) {
|
||||
Send("OK");
|
||||
}
|
||||
|
||||
void GDBClient::HandleRemoveBreakpoint(const char* str) {
|
||||
Send("OK");
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef XENIA_DEBUG_PROTOCOLS_GDB_GDB_CLIENT_H_
|
||||
#define XENIA_DEBUG_PROTOCOLS_GDB_GDB_CLIENT_H_
|
||||
|
||||
#include <xenia/common.h>
|
||||
#include <xenia/core.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <xenia/debug/debug_client.h>
|
||||
|
||||
|
||||
namespace xe {
|
||||
namespace debug {
|
||||
namespace protocols {
|
||||
namespace gdb {
|
||||
|
||||
class MessageReader;
|
||||
class MessageWriter;
|
||||
|
||||
|
||||
class GDBClient : public DebugClient {
|
||||
public:
|
||||
GDBClient(DebugServer* debug_server, socket_t socket_id);
|
||||
virtual ~GDBClient();
|
||||
|
||||
socket_t socket_id() const { return socket_id_; }
|
||||
|
||||
virtual int Setup();
|
||||
virtual void Close();
|
||||
|
||||
virtual void SendEvent(json_t* event_json) {}
|
||||
|
||||
private:
|
||||
static void StartCallback(void* param);
|
||||
|
||||
int PerformHandshake();
|
||||
void EventThread();
|
||||
int CheckReceive();
|
||||
void DispatchMessage(MessageReader* message);
|
||||
int PumpSendQueue();
|
||||
MessageWriter* BeginSend();
|
||||
void EndSend(MessageWriter* message);
|
||||
void Send(const char* format, ...);
|
||||
|
||||
void HandleReadRegisters(const char* str);
|
||||
void HandleWriteRegisters(const char* str);
|
||||
void HandleReadRegister(const char* str);
|
||||
void HandleWriteRegister(const char* str);
|
||||
void HandleReadMemory(const char* str);
|
||||
void HandleWriteMemory(const char* str);
|
||||
void HandleAddBreakpoint(const char* str);
|
||||
void HandleRemoveBreakpoint(const char* str);
|
||||
|
||||
xe_thread_ref thread_;
|
||||
|
||||
socket_t socket_id_;
|
||||
xe_socket_loop_t* loop_;
|
||||
xe_mutex_t* mutex_;
|
||||
std::vector<MessageReader*> message_reader_pool_;
|
||||
std::vector<MessageWriter*> message_writer_pool_;
|
||||
std::vector<MessageWriter*> pending_messages_;
|
||||
|
||||
MessageReader* current_reader_;
|
||||
std::vector<MessageWriter*> send_queue_;
|
||||
bool send_queue_stalled_;
|
||||
};
|
||||
|
||||
|
||||
} // namespace gdb
|
||||
} // namespace protocols
|
||||
} // namespace debug
|
||||
} // namespace xe
|
||||
|
||||
|
||||
#endif // XENIA_DEBUG_PROTOCOLS_GDB_GDB_CLIENT_H_
|
||||
@@ -1,105 +0,0 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include <xenia/debug/protocols/gdb/gdb_protocol.h>
|
||||
|
||||
#include <xenia/debug/protocols/gdb/gdb_client.h>
|
||||
|
||||
#include <gflags/gflags.h>
|
||||
|
||||
|
||||
DEFINE_int32(gdb_debug_port, 6201,
|
||||
"Remote debugging port for GDB TCP connections.");
|
||||
|
||||
|
||||
using namespace xe;
|
||||
using namespace xe::debug;
|
||||
using namespace xe::debug::protocols::gdb;
|
||||
|
||||
|
||||
GDBProtocol::GDBProtocol(DebugServer* debug_server) :
|
||||
port_(0), socket_id_(0), thread_(0), running_(false),
|
||||
Protocol(debug_server) {
|
||||
port_ = FLAGS_gdb_debug_port;
|
||||
}
|
||||
|
||||
GDBProtocol::~GDBProtocol() {
|
||||
if (thread_) {
|
||||
// Join thread.
|
||||
running_ = false;
|
||||
xe_thread_release(thread_);
|
||||
thread_ = 0;
|
||||
}
|
||||
if (socket_id_) {
|
||||
xe_socket_close(socket_id_);
|
||||
}
|
||||
}
|
||||
|
||||
int GDBProtocol::Setup() {
|
||||
if (port_ == 0 || port_ == -1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
xe_socket_init();
|
||||
|
||||
socket_id_ = xe_socket_create_tcp();
|
||||
if (socket_id_ == XE_INVALID_SOCKET) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
xe_socket_set_keepalive(socket_id_, true);
|
||||
xe_socket_set_reuseaddr(socket_id_, true);
|
||||
xe_socket_set_nodelay(socket_id_, true);
|
||||
|
||||
if (xe_socket_bind(socket_id_, port_)) {
|
||||
XELOGE("Could not bind listen socket: %d", errno);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (xe_socket_listen(socket_id_)) {
|
||||
xe_socket_close(socket_id_);
|
||||
return 1;
|
||||
}
|
||||
|
||||
thread_ = xe_thread_create("GDB Debugger Listener", StartCallback, this);
|
||||
running_ = true;
|
||||
return xe_thread_start(thread_);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void GDBProtocol::StartCallback(void* param) {
|
||||
GDBProtocol* protocol = reinterpret_cast<GDBProtocol*>(param);
|
||||
protocol->AcceptThread();
|
||||
}
|
||||
|
||||
void GDBProtocol::AcceptThread() {
|
||||
while (running_) {
|
||||
if (!socket_id_) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Accept the first connection we get.
|
||||
xe_socket_connection_t client_info;
|
||||
if (xe_socket_accept(socket_id_, &client_info)) {
|
||||
XELOGE("WS debugger failed to accept connection");
|
||||
break;
|
||||
}
|
||||
|
||||
XELOGI("WS debugger connected from %s", client_info.addr);
|
||||
|
||||
// Create the client object.
|
||||
// Note that the client will delete itself when done.
|
||||
GDBClient* client = new GDBClient(debug_server_, client_info.socket);
|
||||
if (client->Setup()) {
|
||||
// Client failed to setup - abort.
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef XENIA_DEBUG_PROTOCOLS_GDB_GDB_PROTOCOL_H_
|
||||
#define XENIA_DEBUG_PROTOCOLS_GDB_GDB_PROTOCOL_H_
|
||||
|
||||
#include <xenia/common.h>
|
||||
#include <xenia/core.h>
|
||||
|
||||
#include <xenia/debug/protocol.h>
|
||||
|
||||
|
||||
XEDECLARECLASS2(xe, debug, DebugServer);
|
||||
|
||||
|
||||
namespace xe {
|
||||
namespace debug {
|
||||
namespace protocols {
|
||||
namespace gdb {
|
||||
|
||||
|
||||
class GDBProtocol : public Protocol {
|
||||
public:
|
||||
GDBProtocol(DebugServer* debug_server);
|
||||
virtual ~GDBProtocol();
|
||||
|
||||
virtual int Setup();
|
||||
|
||||
private:
|
||||
static void StartCallback(void* param);
|
||||
void AcceptThread();
|
||||
|
||||
protected:
|
||||
uint32_t port_;
|
||||
|
||||
socket_t socket_id_;
|
||||
|
||||
xe_thread_ref thread_;
|
||||
bool running_;
|
||||
};
|
||||
|
||||
|
||||
} // namespace gdb
|
||||
} // namespace protocols
|
||||
} // namespace debug
|
||||
} // namespace xe
|
||||
|
||||
|
||||
#endif // XENIA_DEBUG_PROTOCOLS_GDB_GDB_PROTOCOL_H_
|
||||
@@ -1,90 +0,0 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include <xenia/debug/protocols/gdb/message.h>
|
||||
|
||||
|
||||
using namespace xe;
|
||||
using namespace xe::debug;
|
||||
using namespace xe::debug::protocols::gdb;
|
||||
|
||||
|
||||
MessageReader::MessageReader() {
|
||||
Reset();
|
||||
}
|
||||
|
||||
MessageReader::~MessageReader() {
|
||||
}
|
||||
|
||||
void MessageReader::Reset() {
|
||||
buffer_.Reset();
|
||||
}
|
||||
|
||||
void MessageReader::Append(const uint8_t* buffer, size_t length) {
|
||||
buffer_.AppendBytes(buffer, length);
|
||||
}
|
||||
|
||||
bool MessageReader::CheckComplete() {
|
||||
// TODO(benvanik): verify checksum.
|
||||
return true;
|
||||
}
|
||||
|
||||
const char* MessageReader::GetString() {
|
||||
return buffer_.GetString();
|
||||
}
|
||||
|
||||
|
||||
MessageWriter::MessageWriter() :
|
||||
offset_(0) {
|
||||
Reset();
|
||||
}
|
||||
|
||||
MessageWriter::~MessageWriter() {
|
||||
}
|
||||
|
||||
const uint8_t* MessageWriter::buffer() const {
|
||||
return (const uint8_t*)buffer_.GetString();
|
||||
}
|
||||
|
||||
size_t MessageWriter::length() const {
|
||||
return buffer_.length() + 1;
|
||||
}
|
||||
|
||||
void MessageWriter::Reset() {
|
||||
buffer_.Reset();
|
||||
buffer_.Append("$");
|
||||
offset_ = 0;
|
||||
}
|
||||
|
||||
void MessageWriter::Append(const char* format, ...) {
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
buffer_.AppendVarargs(format, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void MessageWriter::AppendVarargs(const char* format, va_list args) {
|
||||
buffer_.AppendVarargs(format, args);
|
||||
}
|
||||
|
||||
void MessageWriter::Finalize() {
|
||||
uint8_t checksum = 0;
|
||||
const uint8_t* data = (const uint8_t*)buffer_.GetString();
|
||||
data++; // skip $
|
||||
while (true) {
|
||||
uint8_t c = *data;
|
||||
if (!c) {
|
||||
break;
|
||||
}
|
||||
checksum += c;
|
||||
data++;
|
||||
}
|
||||
|
||||
buffer_.Append("#%.2X", checksum);
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef XENIA_DEBUG_PROTOCOLS_GDB_MESSAGE_H_
|
||||
#define XENIA_DEBUG_PROTOCOLS_GDB_MESSAGE_H_
|
||||
|
||||
#include <xenia/common.h>
|
||||
#include <xenia/core.h>
|
||||
|
||||
#include <alloy/string_buffer.h>
|
||||
|
||||
|
||||
namespace xe {
|
||||
namespace debug {
|
||||
namespace protocols {
|
||||
namespace gdb {
|
||||
|
||||
|
||||
class MessageReader {
|
||||
public:
|
||||
MessageReader();
|
||||
~MessageReader();
|
||||
|
||||
void Reset();
|
||||
void Append(const uint8_t* buffer, size_t length);
|
||||
bool CheckComplete();
|
||||
|
||||
const char* GetString();
|
||||
|
||||
private:
|
||||
alloy::StringBuffer buffer_;
|
||||
};
|
||||
|
||||
|
||||
class MessageWriter {
|
||||
public:
|
||||
MessageWriter();
|
||||
~MessageWriter();
|
||||
|
||||
const uint8_t* buffer() const;
|
||||
size_t length() const;
|
||||
size_t offset() const { return offset_; }
|
||||
void set_offset(size_t value) { offset_ = value; }
|
||||
|
||||
void Reset();
|
||||
|
||||
void Append(const char* format, ...);
|
||||
void AppendVarargs(const char* format, va_list args);
|
||||
|
||||
void Finalize();
|
||||
|
||||
private:
|
||||
alloy::StringBuffer buffer_;
|
||||
size_t offset_;
|
||||
};
|
||||
|
||||
|
||||
} // namespace gdb
|
||||
} // namespace protocols
|
||||
} // namespace debug
|
||||
} // namespace xe
|
||||
|
||||
|
||||
#endif // XENIA_DEBUG_PROTOCOLS_GDB_MESSAGE_H_
|
||||
@@ -1,11 +0,0 @@
|
||||
# Copyright 2013 Ben Vanik. All Rights Reserved.
|
||||
{
|
||||
'sources': [
|
||||
'gdb_client.cc',
|
||||
'gdb_client.h',
|
||||
'gdb_protocol.cc',
|
||||
'gdb_protocol.h',
|
||||
'message.cc',
|
||||
'message.h',
|
||||
],
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
# Copyright 2013 Ben Vanik. All Rights Reserved.
|
||||
{
|
||||
'includes': [
|
||||
'gdb/sources.gypi',
|
||||
'ws/sources.gypi',
|
||||
],
|
||||
}
|
||||
@@ -1,235 +0,0 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include <xenia/debug/protocols/ws/simple_sha1.h>
|
||||
|
||||
#if XE_PLATFORM_WIN32
|
||||
#include <winsock2.h>
|
||||
#else
|
||||
#include <arpa/inet.h>
|
||||
#endif // WIN32
|
||||
|
||||
|
||||
using namespace xe;
|
||||
using namespace xe::debug::protocols::ws;
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
|
||||
// http://git.kernel.org/?p=git/git.git;a=blob;f=block-sha1/sha1.c
|
||||
|
||||
/*
|
||||
* SHA1 routine optimized to do word accesses rather than byte accesses,
|
||||
* and to avoid unnecessary copies into the context array.
|
||||
*
|
||||
* This was initially based on the Mozilla SHA1 implementation, although
|
||||
* none of the original Mozilla code remains.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
size_t size;
|
||||
uint32_t H[5];
|
||||
uint32_t W[16];
|
||||
} SHA_CTX;
|
||||
|
||||
#define SHA_ROT(X,l,r) (((X) << (l)) | ((X) >> (r)))
|
||||
#define SHA_ROL(X,n) SHA_ROT(X,n,32-(n))
|
||||
#define SHA_ROR(X,n) SHA_ROT(X,32-(n),n)
|
||||
|
||||
#define setW(x, val) (*(volatile unsigned int *)&W(x) = (val))
|
||||
|
||||
#define get_be32(p) ntohl(*(unsigned int *)(p))
|
||||
#define put_be32(p, v) do { *(unsigned int *)(p) = htonl(v); } while (0)
|
||||
|
||||
#define W(x) (array[(x)&15])
|
||||
|
||||
#define SHA_SRC(t) get_be32((unsigned char *) block + (t)*4)
|
||||
#define SHA_MIX(t) SHA_ROL(W((t)+13) ^ W((t)+8) ^ W((t)+2) ^ W(t), 1);
|
||||
|
||||
#define SHA_ROUND(t, input, fn, constant, A, B, C, D, E) do { \
|
||||
unsigned int TEMP = input(t); setW(t, TEMP); \
|
||||
E += TEMP + SHA_ROL(A,5) + (fn) + (constant); \
|
||||
B = SHA_ROR(B, 2); } while (0)
|
||||
|
||||
#define T_0_15(t, A, B, C, D, E) SHA_ROUND(t, SHA_SRC, (((C^D)&B)^D) , 0x5a827999, A, B, C, D, E )
|
||||
#define T_16_19(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (((C^D)&B)^D) , 0x5a827999, A, B, C, D, E )
|
||||
#define T_20_39(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0x6ed9eba1, A, B, C, D, E )
|
||||
#define T_40_59(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, ((B&C)+(D&(B^C))) , 0x8f1bbcdc, A, B, C, D, E )
|
||||
#define T_60_79(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0xca62c1d6, A, B, C, D, E )
|
||||
|
||||
static void SHA1_Block(SHA_CTX *ctx, const void *block) {
|
||||
uint32_t A = ctx->H[0];
|
||||
uint32_t B = ctx->H[1];
|
||||
uint32_t C = ctx->H[2];
|
||||
uint32_t D = ctx->H[3];
|
||||
uint32_t E = ctx->H[4];
|
||||
|
||||
uint32_t array[16];
|
||||
|
||||
/* Round 1 - iterations 0-16 take their input from 'block' */
|
||||
T_0_15( 0, A, B, C, D, E);
|
||||
T_0_15( 1, E, A, B, C, D);
|
||||
T_0_15( 2, D, E, A, B, C);
|
||||
T_0_15( 3, C, D, E, A, B);
|
||||
T_0_15( 4, B, C, D, E, A);
|
||||
T_0_15( 5, A, B, C, D, E);
|
||||
T_0_15( 6, E, A, B, C, D);
|
||||
T_0_15( 7, D, E, A, B, C);
|
||||
T_0_15( 8, C, D, E, A, B);
|
||||
T_0_15( 9, B, C, D, E, A);
|
||||
T_0_15(10, A, B, C, D, E);
|
||||
T_0_15(11, E, A, B, C, D);
|
||||
T_0_15(12, D, E, A, B, C);
|
||||
T_0_15(13, C, D, E, A, B);
|
||||
T_0_15(14, B, C, D, E, A);
|
||||
T_0_15(15, A, B, C, D, E);
|
||||
|
||||
/* Round 1 - tail. Input from 512-bit mixing array */
|
||||
T_16_19(16, E, A, B, C, D);
|
||||
T_16_19(17, D, E, A, B, C);
|
||||
T_16_19(18, C, D, E, A, B);
|
||||
T_16_19(19, B, C, D, E, A);
|
||||
|
||||
/* Round 2 */
|
||||
T_20_39(20, A, B, C, D, E);
|
||||
T_20_39(21, E, A, B, C, D);
|
||||
T_20_39(22, D, E, A, B, C);
|
||||
T_20_39(23, C, D, E, A, B);
|
||||
T_20_39(24, B, C, D, E, A);
|
||||
T_20_39(25, A, B, C, D, E);
|
||||
T_20_39(26, E, A, B, C, D);
|
||||
T_20_39(27, D, E, A, B, C);
|
||||
T_20_39(28, C, D, E, A, B);
|
||||
T_20_39(29, B, C, D, E, A);
|
||||
T_20_39(30, A, B, C, D, E);
|
||||
T_20_39(31, E, A, B, C, D);
|
||||
T_20_39(32, D, E, A, B, C);
|
||||
T_20_39(33, C, D, E, A, B);
|
||||
T_20_39(34, B, C, D, E, A);
|
||||
T_20_39(35, A, B, C, D, E);
|
||||
T_20_39(36, E, A, B, C, D);
|
||||
T_20_39(37, D, E, A, B, C);
|
||||
T_20_39(38, C, D, E, A, B);
|
||||
T_20_39(39, B, C, D, E, A);
|
||||
|
||||
/* Round 3 */
|
||||
T_40_59(40, A, B, C, D, E);
|
||||
T_40_59(41, E, A, B, C, D);
|
||||
T_40_59(42, D, E, A, B, C);
|
||||
T_40_59(43, C, D, E, A, B);
|
||||
T_40_59(44, B, C, D, E, A);
|
||||
T_40_59(45, A, B, C, D, E);
|
||||
T_40_59(46, E, A, B, C, D);
|
||||
T_40_59(47, D, E, A, B, C);
|
||||
T_40_59(48, C, D, E, A, B);
|
||||
T_40_59(49, B, C, D, E, A);
|
||||
T_40_59(50, A, B, C, D, E);
|
||||
T_40_59(51, E, A, B, C, D);
|
||||
T_40_59(52, D, E, A, B, C);
|
||||
T_40_59(53, C, D, E, A, B);
|
||||
T_40_59(54, B, C, D, E, A);
|
||||
T_40_59(55, A, B, C, D, E);
|
||||
T_40_59(56, E, A, B, C, D);
|
||||
T_40_59(57, D, E, A, B, C);
|
||||
T_40_59(58, C, D, E, A, B);
|
||||
T_40_59(59, B, C, D, E, A);
|
||||
|
||||
/* Round 4 */
|
||||
T_60_79(60, A, B, C, D, E);
|
||||
T_60_79(61, E, A, B, C, D);
|
||||
T_60_79(62, D, E, A, B, C);
|
||||
T_60_79(63, C, D, E, A, B);
|
||||
T_60_79(64, B, C, D, E, A);
|
||||
T_60_79(65, A, B, C, D, E);
|
||||
T_60_79(66, E, A, B, C, D);
|
||||
T_60_79(67, D, E, A, B, C);
|
||||
T_60_79(68, C, D, E, A, B);
|
||||
T_60_79(69, B, C, D, E, A);
|
||||
T_60_79(70, A, B, C, D, E);
|
||||
T_60_79(71, E, A, B, C, D);
|
||||
T_60_79(72, D, E, A, B, C);
|
||||
T_60_79(73, C, D, E, A, B);
|
||||
T_60_79(74, B, C, D, E, A);
|
||||
T_60_79(75, A, B, C, D, E);
|
||||
T_60_79(76, E, A, B, C, D);
|
||||
T_60_79(77, D, E, A, B, C);
|
||||
T_60_79(78, C, D, E, A, B);
|
||||
T_60_79(79, B, C, D, E, A);
|
||||
|
||||
ctx->H[0] += A;
|
||||
ctx->H[1] += B;
|
||||
ctx->H[2] += C;
|
||||
ctx->H[3] += D;
|
||||
ctx->H[4] += E;
|
||||
}
|
||||
|
||||
void SHA1_Update(SHA_CTX *ctx, const void *data, unsigned long len)
|
||||
{
|
||||
uint32_t lenW = ctx->size & 63;
|
||||
ctx->size += len;
|
||||
|
||||
if (lenW) {
|
||||
uint32_t left = 64 - lenW;
|
||||
if (len < left) {
|
||||
left = len;
|
||||
}
|
||||
memcpy(lenW + (char *)ctx->W, data, left);
|
||||
lenW = (lenW + left) & 63;
|
||||
len -= left;
|
||||
data = ((const char *)data + left);
|
||||
if (lenW) {
|
||||
return;
|
||||
}
|
||||
SHA1_Block(ctx, ctx->W);
|
||||
}
|
||||
while (len >= 64) {
|
||||
SHA1_Block(ctx, data);
|
||||
data = ((const char *)data + 64);
|
||||
len -= 64;
|
||||
}
|
||||
if (len) {
|
||||
memcpy(ctx->W, data, len);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void xe::debug::protocols::ws::SHA1(
|
||||
const uint8_t* data, size_t length, uint8_t out_hash[20]) {
|
||||
static const uint8_t pad[64] = { 0x80 };
|
||||
|
||||
SHA_CTX ctx = {
|
||||
0,
|
||||
{
|
||||
0x67452301,
|
||||
0xefcdab89,
|
||||
0x98badcfe,
|
||||
0x10325476,
|
||||
0xc3d2e1f0,
|
||||
},
|
||||
{ 0 }
|
||||
};
|
||||
|
||||
SHA1_Update(&ctx, data, (unsigned long)length);
|
||||
|
||||
uint32_t padlen[2] = {
|
||||
htonl((uint32_t)(ctx.size >> 29)),
|
||||
htonl((uint32_t)(ctx.size << 3)),
|
||||
};
|
||||
|
||||
size_t i = ctx.size & 63;
|
||||
SHA1_Update(&ctx, pad, 1 + (63 & (55 - i)));
|
||||
SHA1_Update(&ctx, padlen, 8);
|
||||
|
||||
for (size_t n = 0; n < 5; n++) {
|
||||
put_be32(out_hash + n * 4, ctx.H[n]);
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license = see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef XENIA_DEBUG_PROTOCOLS_WS_SIMPLE_SHA1_H_
|
||||
#define XENIA_DEBUG_PROTOCOLS_WS_SIMPLE_SHA1_H_
|
||||
|
||||
#include <xenia/common.h>
|
||||
#include <xenia/core.h>
|
||||
|
||||
|
||||
namespace xe {
|
||||
namespace debug {
|
||||
namespace protocols {
|
||||
namespace ws {
|
||||
|
||||
|
||||
// This is a (likely) slow SHA1 designed for use on small values such as
|
||||
// Websocket security keys. If we need something more complex it'd be best
|
||||
// to use a real library.
|
||||
void SHA1(const uint8_t* data, size_t length, uint8_t out_hash[20]);
|
||||
|
||||
|
||||
} // namespace ws
|
||||
} // namespace protocols
|
||||
} // namespace debug
|
||||
} // namespace xe
|
||||
|
||||
|
||||
#endif // XENIA_DEBUG_PROTOCOLS_WS_SIMPLE_SHA1_H_
|
||||
@@ -1,11 +0,0 @@
|
||||
# Copyright 2013 Ben Vanik. All Rights Reserved.
|
||||
{
|
||||
'sources': [
|
||||
'simple_sha1.cc',
|
||||
'simple_sha1.h',
|
||||
'ws_client.cc',
|
||||
'ws_client.h',
|
||||
'ws_protocol.cc',
|
||||
'ws_protocol.h',
|
||||
],
|
||||
}
|
||||
@@ -1,531 +0,0 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include <xenia/debug/protocols/ws/ws_client.h>
|
||||
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <jansson.h>
|
||||
|
||||
#include <xenia/emulator.h>
|
||||
#include <xenia/debug/debug_server.h>
|
||||
#include <xenia/debug/debug_target.h>
|
||||
#include <xenia/debug/protocols/ws/simple_sha1.h>
|
||||
#include <xenia/kernel/kernel_state.h>
|
||||
#include <xenia/kernel/xboxkrnl_module.h>
|
||||
#include <xenia/kernel/objects/xuser_module.h>
|
||||
|
||||
#if XE_PLATFORM_WIN32
|
||||
// Required for wslay.
|
||||
typedef SSIZE_T ssize_t;
|
||||
#endif // WIN32
|
||||
#include <wslay/wslay.h>
|
||||
|
||||
|
||||
using namespace std;
|
||||
using namespace xe;
|
||||
using namespace xe::debug;
|
||||
using namespace xe::debug::protocols::ws;
|
||||
using namespace xe::kernel;
|
||||
|
||||
|
||||
WSClient::WSClient(DebugServer* debug_server, socket_t socket_id) :
|
||||
thread_(NULL),
|
||||
socket_id_(socket_id),
|
||||
DebugClient(debug_server) {
|
||||
mutex_ = xe_mutex_alloc(1000);
|
||||
|
||||
loop_ = xe_socket_loop_create(socket_id);
|
||||
}
|
||||
|
||||
WSClient::~WSClient() {
|
||||
xe_mutex_t* mutex = mutex_;
|
||||
xe_mutex_lock(mutex);
|
||||
|
||||
mutex_ = NULL;
|
||||
|
||||
xe_socket_close(socket_id_);
|
||||
socket_id_ = 0;
|
||||
|
||||
xe_socket_loop_destroy(loop_);
|
||||
loop_ = NULL;
|
||||
|
||||
xe_mutex_unlock(mutex);
|
||||
xe_mutex_free(mutex);
|
||||
|
||||
xe_thread_release(thread_);
|
||||
}
|
||||
|
||||
int WSClient::Setup() {
|
||||
// Prep the socket.
|
||||
xe_socket_set_keepalive(socket_id_, true);
|
||||
xe_socket_set_nodelay(socket_id_, true);
|
||||
|
||||
thread_ = xe_thread_create("WS Debugger Client", StartCallback, this);
|
||||
return xe_thread_start(thread_);
|
||||
}
|
||||
|
||||
void WSClient::Close() {
|
||||
xe_socket_close(socket_id_);
|
||||
socket_id_ = 0;
|
||||
}
|
||||
|
||||
void WSClient::StartCallback(void* param) {
|
||||
WSClient* client = reinterpret_cast<WSClient*>(param);
|
||||
client->EventThread();
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
int64_t WSClientSendCallback(wslay_event_context_ptr ctx,
|
||||
const uint8_t* data, size_t len, int flags,
|
||||
void* user_data) {
|
||||
WSClient* client = reinterpret_cast<WSClient*>(user_data);
|
||||
|
||||
int error_code = 0;
|
||||
int64_t r;
|
||||
while ((r = xe_socket_send(client->socket_id(), data, len, 0,
|
||||
&error_code)) == -1 && error_code == EINTR);
|
||||
if (r == -1) {
|
||||
if (error_code == EAGAIN || error_code == EWOULDBLOCK) {
|
||||
wslay_event_set_error(ctx, WSLAY_ERR_WOULDBLOCK);
|
||||
} else {
|
||||
wslay_event_set_error(ctx, WSLAY_ERR_CALLBACK_FAILURE);
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
int64_t WSClientRecvCallback(wslay_event_context_ptr ctx,
|
||||
uint8_t* data, size_t len, int flags,
|
||||
void* user_data) {
|
||||
WSClient* client = reinterpret_cast<WSClient*>(user_data);
|
||||
|
||||
int error_code = 0;
|
||||
int64_t r;
|
||||
while ((r = xe_socket_recv(client->socket_id(), data, len, 0,
|
||||
&error_code)) == -1 && error_code == EINTR);
|
||||
if (r == -1) {
|
||||
if (error_code == EAGAIN || error_code == EWOULDBLOCK) {
|
||||
wslay_event_set_error(ctx, WSLAY_ERR_WOULDBLOCK);
|
||||
} else {
|
||||
wslay_event_set_error(ctx, WSLAY_ERR_CALLBACK_FAILURE);
|
||||
}
|
||||
} else if (r == 0) {
|
||||
wslay_event_set_error(ctx, WSLAY_ERR_CALLBACK_FAILURE);
|
||||
r = -1;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
void WSClientOnMsgCallback(wslay_event_context_ptr ctx,
|
||||
const struct wslay_event_on_msg_recv_arg* arg,
|
||||
void* user_data) {
|
||||
if (wslay_is_ctrl_frame(arg->opcode)) {
|
||||
// Ignore control frames.
|
||||
return;
|
||||
}
|
||||
|
||||
WSClient* client = reinterpret_cast<WSClient*>(user_data);
|
||||
switch (arg->opcode) {
|
||||
case WSLAY_TEXT_FRAME:
|
||||
client->OnMessage(arg->msg, arg->msg_length);
|
||||
break;
|
||||
case WSLAY_BINARY_FRAME:
|
||||
// Ignored.
|
||||
break;
|
||||
default:
|
||||
// Unknown opcode - some frame stuff?
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::string EncodeBase64(const uint8_t* input, size_t length) {
|
||||
static const char b64[] =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
std::string result;
|
||||
size_t remaining = length;
|
||||
size_t n = 0;
|
||||
while (remaining) {
|
||||
result.push_back(b64[input[n] >> 2]);
|
||||
result.push_back(b64[((input[n] & 0x03) << 4) |
|
||||
((input[n + 1] & 0xf0) >> 4)]);
|
||||
remaining--;
|
||||
if (remaining) {
|
||||
result.push_back(b64[((input[n + 1] & 0x0f) << 2) |
|
||||
((input[n + 2] & 0xc0) >> 6)]);
|
||||
remaining--;
|
||||
} else {
|
||||
result.push_back('=');
|
||||
}
|
||||
if (remaining) {
|
||||
result.push_back(b64[input[n + 2] & 0x3f]);
|
||||
remaining--;
|
||||
} else {
|
||||
result.push_back('=');
|
||||
}
|
||||
n += 3;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int WSClient::PerformHandshake() {
|
||||
std::string headers;
|
||||
uint8_t buffer[4096];
|
||||
int error_code = 0;
|
||||
int64_t r;
|
||||
while (true) {
|
||||
while ((r = xe_socket_recv(socket_id_, buffer, sizeof(buffer), 0,
|
||||
&error_code)) == -1 && error_code == EINTR);
|
||||
if (r == -1) {
|
||||
if (error_code == EWOULDBLOCK || error_code == EAGAIN) {
|
||||
if (!headers.size()) {
|
||||
// Nothing read yet - spin.
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
} else {
|
||||
XELOGE("HTTP header read failure");
|
||||
return 1;
|
||||
}
|
||||
} else if (r == 0) {
|
||||
// EOF.
|
||||
XELOGE("HTTP header EOF");
|
||||
return 2;
|
||||
} else {
|
||||
headers.append(buffer, buffer + r);
|
||||
if (headers.size() > 8192) {
|
||||
XELOGE("HTTP headers exceeded max buffer size");
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (headers.find("\r\n\r\n") == std::string::npos) {
|
||||
XELOGE("Incomplete HTTP headers: %s", headers.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
// If this is a get for the session list, just produce that and return.
|
||||
// We could stub out better handling here, if we wanted.
|
||||
if (headers.find("GET /sessions") != std::string::npos) {
|
||||
Emulator* emulator = debug_server_->emulator();
|
||||
KernelState* kernel_state = emulator->xboxkrnl()->kernel_state();
|
||||
XUserModule* module = kernel_state->GetExecutableModule();
|
||||
const xe_xex2_header_t* header = module->xex_header();
|
||||
char title_id[9];
|
||||
xesnprintfa(title_id, XECOUNT(title_id), "%.8X",
|
||||
header->execution_info.title_id);
|
||||
|
||||
ostringstream response_body;
|
||||
if (module) {
|
||||
response_body << "[{";
|
||||
response_body <<
|
||||
"\"name\": \"" << module->name() << "\",";
|
||||
response_body <<
|
||||
"\"titleId\": \"" << title_id << "\"";
|
||||
response_body << "}]";
|
||||
} else {
|
||||
response_body <<
|
||||
"[]";
|
||||
}
|
||||
size_t content_length = response_body.str().length();
|
||||
ostringstream response;
|
||||
response <<
|
||||
"HTTP/1.0 200 OK\r\n"
|
||||
"Content-Type: application/json\r\n"
|
||||
"Connection: close\r\n"
|
||||
"Access-Control-Allow-Origin: *\r\n"
|
||||
"Content-Length: " << content_length << "\r\n"
|
||||
"\r\n";
|
||||
response << response_body.str();
|
||||
error_code = WriteResponse(response.str());
|
||||
if (error_code) {
|
||||
return error_code;
|
||||
}
|
||||
|
||||
// Eh, we just kill the connection here.
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Parse the headers to verify its a websocket request.
|
||||
std::string::size_type keyhdstart;
|
||||
if (headers.find("Upgrade: websocket\r\n") == std::string::npos ||
|
||||
headers.find("Connection: Upgrade\r\n") == std::string::npos ||
|
||||
(keyhdstart = headers.find("Sec-WebSocket-Key: ")) ==
|
||||
std::string::npos) {
|
||||
XELOGW("HTTP connection does not contain websocket headers");
|
||||
return 2;
|
||||
}
|
||||
keyhdstart += 19;
|
||||
std::string::size_type keyhdend = headers.find("\r\n", keyhdstart);
|
||||
std::string client_key = headers.substr(keyhdstart, keyhdend - keyhdstart);
|
||||
std::string accept_key = client_key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
||||
uint8_t accept_sha[20];
|
||||
SHA1((uint8_t*)accept_key.c_str(), accept_key.size(), accept_sha);
|
||||
accept_key = EncodeBase64(accept_sha, sizeof(accept_sha));
|
||||
|
||||
// Write the response to upgrade the connection.
|
||||
std::string response =
|
||||
"HTTP/1.1 101 Switching Protocols\r\n"
|
||||
"Upgrade: websocket\r\n"
|
||||
"Connection: Upgrade\r\n"
|
||||
"Sec-WebSocket-Accept: " + accept_key + "\r\n"
|
||||
"\r\n";
|
||||
return WriteResponse(response);
|
||||
}
|
||||
|
||||
int WSClient::WriteResponse(std::string& response) {
|
||||
int error_code = 0;
|
||||
int64_t r;
|
||||
size_t write_offset = 0;
|
||||
size_t write_length = response.size();
|
||||
while (true) {
|
||||
while ((r = xe_socket_send(socket_id_,
|
||||
(uint8_t*)response.c_str() + write_offset,
|
||||
write_length, 0, &error_code)) == -1 &&
|
||||
error_code == EINTR);
|
||||
if (r == -1) {
|
||||
if (error_code == EAGAIN || error_code == EWOULDBLOCK) {
|
||||
break;
|
||||
} else {
|
||||
XELOGE("HTTP response write failure");
|
||||
return 4;
|
||||
}
|
||||
} else {
|
||||
write_offset += r;
|
||||
write_length -= r;
|
||||
if (!write_length) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void WSClient::EventThread() {
|
||||
// Enable non-blocking IO on the socket.
|
||||
xe_socket_set_nonblock(socket_id_, true);
|
||||
|
||||
// First run the HTTP handshake.
|
||||
// This will fail if the connection is not for websockets.
|
||||
if (PerformHandshake()) {
|
||||
delete this;
|
||||
return;
|
||||
}
|
||||
|
||||
// Prep callbacks.
|
||||
struct wslay_event_callbacks callbacks = {
|
||||
(wslay_event_recv_callback)WSClientRecvCallback,
|
||||
(wslay_event_send_callback)WSClientSendCallback,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
WSClientOnMsgCallback,
|
||||
};
|
||||
|
||||
// Prep the websocket server context.
|
||||
wslay_event_context_ptr ctx;
|
||||
wslay_event_context_server_init(&ctx, &callbacks, this);
|
||||
|
||||
// Loop forever.
|
||||
while (wslay_event_want_read(ctx) || wslay_event_want_write(ctx)) {
|
||||
// Wait on the event.
|
||||
if (xe_socket_loop_poll(loop_,
|
||||
!!wslay_event_want_read(ctx),
|
||||
!!wslay_event_want_write(ctx))) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Handle any self-generated events to queue messages.
|
||||
if (xe_socket_loop_check_queued_write(loop_)) {
|
||||
xe_mutex_lock(mutex_);
|
||||
for (std::vector<struct wslay_event_msg>::iterator it =
|
||||
pending_messages_.begin(); it != pending_messages_.end(); it++) {
|
||||
struct wslay_event_msg* msg = &*it;
|
||||
wslay_event_queue_msg(ctx, msg);
|
||||
}
|
||||
pending_messages_.clear();
|
||||
xe_mutex_unlock(mutex_);
|
||||
}
|
||||
|
||||
// Handle websocket messages.
|
||||
if ((xe_socket_loop_check_socket_recv(loop_) && wslay_event_recv(ctx)) ||
|
||||
(xe_socket_loop_check_socket_send(loop_) && wslay_event_send(ctx))) {
|
||||
// Error handling the event.
|
||||
XELOGE("Error handling WebSocket data");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
wslay_event_context_free(ctx);
|
||||
delete this;
|
||||
}
|
||||
|
||||
void WSClient::SendEvent(json_t* event_json) {
|
||||
char* str = json_dumps(event_json, 0);
|
||||
Write(str);
|
||||
}
|
||||
|
||||
void WSClient::Write(char* value) {
|
||||
const uint8_t* buffers[] = {
|
||||
(uint8_t*)value,
|
||||
};
|
||||
size_t lengths[] = {
|
||||
sizeof(char) * xestrlena(value),
|
||||
};
|
||||
Write(buffers, lengths, XECOUNT(buffers), false);
|
||||
}
|
||||
|
||||
void WSClient::Write(const uint8_t** buffers, size_t* lengths, size_t count,
|
||||
bool binary) {
|
||||
if (!count) {
|
||||
return;
|
||||
}
|
||||
|
||||
size_t combined_length;
|
||||
uint8_t* combined_buffer;
|
||||
if (count == 1) {
|
||||
// Single buffer, just copy.
|
||||
combined_length = lengths[0];
|
||||
combined_buffer = (uint8_t*)xe_malloc(lengths[0]);
|
||||
XEIGNORE(xe_copy_memory(combined_buffer, combined_length,
|
||||
buffers[0], lengths[0]));
|
||||
} else {
|
||||
// Multiple buffers, merge.
|
||||
combined_length = 0;
|
||||
for (size_t n = 0; n < count; n++) {
|
||||
combined_length += lengths[n];
|
||||
}
|
||||
combined_buffer = (uint8_t*)xe_malloc(combined_length);
|
||||
for (size_t n = 0, offset = 0; n < count; n++) {
|
||||
XEIGNORE(xe_copy_memory(
|
||||
combined_buffer + offset, combined_length - offset,
|
||||
buffers[n], lengths[n]));
|
||||
offset += lengths[n];
|
||||
}
|
||||
}
|
||||
|
||||
struct wslay_event_msg msg = {
|
||||
binary ? WSLAY_BINARY_FRAME : WSLAY_TEXT_FRAME,
|
||||
combined_buffer,
|
||||
combined_length,
|
||||
};
|
||||
|
||||
xe_mutex_lock(mutex_);
|
||||
pending_messages_.push_back(msg);
|
||||
bool needs_signal = pending_messages_.size() == 1;
|
||||
xe_mutex_unlock(mutex_);
|
||||
|
||||
if (needs_signal) {
|
||||
// Notify the poll().
|
||||
xe_socket_loop_set_queued_write(loop_);
|
||||
}
|
||||
}
|
||||
|
||||
void WSClient::OnMessage(const uint8_t* data, size_t length) {
|
||||
const char* s = (const char*)data;
|
||||
|
||||
json_error_t error;
|
||||
json_t* request = json_loadb(
|
||||
(const char*)data, length, JSON_DECODE_INT_AS_REAL, &error);
|
||||
if (!request) {
|
||||
// Failed to parse.
|
||||
XELOGE("Failed to parse JSON request: %d:%d %s %s",
|
||||
error.line, error.column,
|
||||
error.source, error.text);
|
||||
return;
|
||||
}
|
||||
if (!json_is_object(request)) {
|
||||
XELOGE("Expected JSON object");
|
||||
json_decref(request);
|
||||
return;
|
||||
}
|
||||
|
||||
json_t* request_id_json = json_object_get(request, "requestId");
|
||||
if (!request_id_json || !json_is_number(request_id_json)) {
|
||||
XELOGE("Need requestId field");
|
||||
json_decref(request);
|
||||
return;
|
||||
}
|
||||
json_t* command_json = json_object_get(request, "command");
|
||||
if (!command_json || !json_is_string(command_json)) {
|
||||
XELOGE("Need command field");
|
||||
json_decref(request);
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle command.
|
||||
bool succeeded = false;
|
||||
const char* command = json_string_value(command_json);
|
||||
json_t* result_json = HandleMessage(command, request, succeeded);
|
||||
if (!result_json) {
|
||||
result_json = json_null();
|
||||
}
|
||||
|
||||
// Prepare response.
|
||||
json_t* response = json_object();
|
||||
json_object_set(response, "requestId", request_id_json);
|
||||
json_t* status_json = succeeded ? json_true() : json_false();
|
||||
json_object_set_new(response, "status", status_json);
|
||||
json_object_set_new(response, "result", result_json);
|
||||
|
||||
// Encode response to string and send back.
|
||||
// String freed by Write.
|
||||
char* response_string = json_dumps(response, JSON_INDENT(2));
|
||||
Write(response_string);
|
||||
|
||||
json_decref(request);
|
||||
json_decref(response);
|
||||
}
|
||||
|
||||
json_t* WSClient::HandleMessage(const char* command, json_t* request,
|
||||
bool& succeeded) {
|
||||
succeeded = false;
|
||||
|
||||
// Get target.
|
||||
char target_name[16] = { 0 };
|
||||
const char* dot = xestrchra(command, '.');
|
||||
if (!dot) {
|
||||
return json_string("No debug target specified.");
|
||||
}
|
||||
if (dot - command > XECOUNT(target_name)) {
|
||||
return NULL;
|
||||
}
|
||||
xestrncpya(target_name, XECOUNT(target_name),
|
||||
command, dot - command);
|
||||
const char* sub_command = command + (dot - command + 1);
|
||||
|
||||
// Check debugger meta commands.
|
||||
if (xestrcmpa(target_name, "debug") == 0) {
|
||||
succeeded = true;
|
||||
if (xestrcmpa(sub_command, "ping") == 0) {
|
||||
return json_true();
|
||||
} else if (xestrcmpa(sub_command, "make_ready") == 0) {
|
||||
MakeReady();
|
||||
return json_true();
|
||||
} else {
|
||||
succeeded = false;
|
||||
return json_string("Unknown command");
|
||||
}
|
||||
}
|
||||
|
||||
// Lookup target.
|
||||
DebugTarget* target = debug_server_->GetTarget(target_name);
|
||||
if (!target) {
|
||||
return json_string("Unknown debug target prefix.");
|
||||
}
|
||||
|
||||
// Dispatch.
|
||||
return target->OnDebugRequest(
|
||||
client_id(), sub_command, request, succeeded);
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef XENIA_DEBUG_PROTOCOLS_WS_WS_CLIENT_H_
|
||||
#define XENIA_DEBUG_PROTOCOLS_WS_WS_CLIENT_H_
|
||||
|
||||
#include <xenia/common.h>
|
||||
#include <xenia/core.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <xenia/debug/debug_client.h>
|
||||
|
||||
|
||||
struct wslay_event_msg;
|
||||
struct json_t;
|
||||
|
||||
|
||||
namespace xe {
|
||||
namespace debug {
|
||||
namespace protocols {
|
||||
namespace ws {
|
||||
|
||||
|
||||
class WSClient : public DebugClient {
|
||||
public:
|
||||
WSClient(DebugServer* debug_server, socket_t socket_id);
|
||||
virtual ~WSClient();
|
||||
|
||||
socket_t socket_id() const { return socket_id_; }
|
||||
|
||||
virtual int Setup();
|
||||
virtual void Close();
|
||||
|
||||
virtual void SendEvent(json_t* event_json);
|
||||
|
||||
void Write(char* value);
|
||||
void Write(const uint8_t** buffers, size_t* lengths, size_t count,
|
||||
bool binary = true);
|
||||
|
||||
void OnMessage(const uint8_t* data, size_t length);
|
||||
json_t* HandleMessage(const char* command, json_t* request, bool& succeeded);
|
||||
|
||||
private:
|
||||
static void StartCallback(void* param);
|
||||
|
||||
int PerformHandshake();
|
||||
int WriteResponse(std::string& response);
|
||||
void EventThread();
|
||||
|
||||
xe_thread_ref thread_;
|
||||
|
||||
socket_t socket_id_;
|
||||
xe_socket_loop_t* loop_;
|
||||
xe_mutex_t* mutex_;
|
||||
std::vector<struct wslay_event_msg> pending_messages_;
|
||||
};
|
||||
|
||||
|
||||
} // namespace ws
|
||||
} // namespace protocols
|
||||
} // namespace debug
|
||||
} // namespace xe
|
||||
|
||||
|
||||
#endif // XENIA_DEBUG_PROTOCOLS_WS_WS_CLIENT_H_
|
||||
@@ -1,104 +0,0 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include <xenia/debug/protocols/ws/ws_protocol.h>
|
||||
|
||||
#include <xenia/debug/debug_server.h>
|
||||
#include <xenia/debug/protocols/ws/ws_client.h>
|
||||
|
||||
#include <gflags/gflags.h>
|
||||
|
||||
|
||||
using namespace xe;
|
||||
using namespace xe::debug;
|
||||
using namespace xe::debug::protocols::ws;
|
||||
|
||||
|
||||
DEFINE_int32(ws_debug_port, 6200,
|
||||
"Remote debugging port for ws:// connections.");
|
||||
|
||||
|
||||
WSProtocol::WSProtocol(DebugServer* debug_server) :
|
||||
port_(0), socket_id_(0), thread_(0), running_(false),
|
||||
Protocol(debug_server) {
|
||||
port_ = FLAGS_ws_debug_port;
|
||||
}
|
||||
|
||||
WSProtocol::~WSProtocol() {
|
||||
if (thread_) {
|
||||
// Join thread.
|
||||
running_ = false;
|
||||
xe_thread_release(thread_);
|
||||
thread_ = 0;
|
||||
}
|
||||
if (socket_id_) {
|
||||
xe_socket_close(socket_id_);
|
||||
}
|
||||
}
|
||||
|
||||
int WSProtocol::Setup() {
|
||||
if (port_ == 0 || port_ == -1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
xe_socket_init();
|
||||
|
||||
socket_id_ = xe_socket_create_tcp();
|
||||
if (socket_id_ == XE_INVALID_SOCKET) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
xe_socket_set_keepalive(socket_id_, true);
|
||||
xe_socket_set_reuseaddr(socket_id_, true);
|
||||
xe_socket_set_nodelay(socket_id_, true);
|
||||
|
||||
if (xe_socket_bind(socket_id_, port_)) {
|
||||
XELOGE("Could not bind listen socket: %d", errno);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (xe_socket_listen(socket_id_)) {
|
||||
xe_socket_close(socket_id_);
|
||||
return 1;
|
||||
}
|
||||
|
||||
thread_ = xe_thread_create("WS Debugger Listener", StartCallback, this);
|
||||
running_ = true;
|
||||
return xe_thread_start(thread_);
|
||||
}
|
||||
|
||||
void WSProtocol::StartCallback(void* param) {
|
||||
WSProtocol* protocol = reinterpret_cast<WSProtocol*>(param);
|
||||
protocol->AcceptThread();
|
||||
}
|
||||
|
||||
void WSProtocol::AcceptThread() {
|
||||
while (running_) {
|
||||
if (!socket_id_) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Accept the first connection we get.
|
||||
xe_socket_connection_t client_info;
|
||||
if (xe_socket_accept(socket_id_, &client_info)) {
|
||||
XELOGE("WS debugger failed to accept connection");
|
||||
break;
|
||||
}
|
||||
|
||||
XELOGI("WS debugger connected from %s", client_info.addr);
|
||||
|
||||
// Create the client object.
|
||||
// Note that the client will delete itself when done.
|
||||
WSClient* client = new WSClient(debug_server_, client_info.socket);
|
||||
if (client->Setup()) {
|
||||
// Client failed to setup - abort.
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef XENIA_DEBUG_PROTOCOLS_WS_WS_PROTOCOL_H_
|
||||
#define XENIA_DEBUG_PROTOCOLS_WS_WS_PROTOCOL_H_
|
||||
|
||||
#include <xenia/common.h>
|
||||
#include <xenia/core.h>
|
||||
|
||||
#include <xenia/debug/protocol.h>
|
||||
|
||||
|
||||
XEDECLARECLASS2(xe, debug, DebugServer);
|
||||
|
||||
|
||||
namespace xe {
|
||||
namespace debug {
|
||||
namespace protocols {
|
||||
namespace ws {
|
||||
|
||||
|
||||
class WSProtocol : public Protocol {
|
||||
public:
|
||||
WSProtocol(DebugServer* debug_server);
|
||||
virtual ~WSProtocol();
|
||||
|
||||
virtual int Setup();
|
||||
|
||||
private:
|
||||
static void StartCallback(void* param);
|
||||
void AcceptThread();
|
||||
|
||||
protected:
|
||||
uint32_t port_;
|
||||
|
||||
socket_t socket_id_;
|
||||
|
||||
xe_thread_ref thread_;
|
||||
bool running_;
|
||||
};
|
||||
|
||||
|
||||
} // namespace ws
|
||||
} // namespace protocols
|
||||
} // namespace debug
|
||||
} // namespace xe
|
||||
|
||||
|
||||
#endif // XENIA_DEBUG_PROTOCOLS_WS_WS_PROTOCOL_H_
|
||||
@@ -1,16 +0,0 @@
|
||||
# Copyright 2013 Ben Vanik. All Rights Reserved.
|
||||
{
|
||||
'sources': [
|
||||
'debug_client.cc',
|
||||
'debug_client.h',
|
||||
'debug_server.cc',
|
||||
'debug_server.h',
|
||||
'debug_target.h',
|
||||
'protocol.cc',
|
||||
'protocol.h',
|
||||
],
|
||||
|
||||
'includes': [
|
||||
'protocols/sources.gypi',
|
||||
],
|
||||
}
|
||||
@@ -12,7 +12,6 @@
|
||||
#include <xenia/apu/apu.h>
|
||||
#include <xenia/cpu/cpu.h>
|
||||
#include <xenia/cpu/xenon_memory.h>
|
||||
#include <xenia/debug/debug_server.h>
|
||||
#include <xenia/gpu/gpu.h>
|
||||
#include <xenia/hid/hid.h>
|
||||
#include <xenia/kernel/kernel.h>
|
||||
@@ -25,7 +24,6 @@
|
||||
using namespace xe;
|
||||
using namespace xe::apu;
|
||||
using namespace xe::cpu;
|
||||
using namespace xe::debug;
|
||||
using namespace xe::gpu;
|
||||
using namespace xe::hid;
|
||||
using namespace xe::kernel;
|
||||
@@ -36,7 +34,6 @@ using namespace xe::ui;
|
||||
Emulator::Emulator(const xechar_t* command_line) :
|
||||
main_window_(0),
|
||||
memory_(0),
|
||||
debug_server_(0),
|
||||
processor_(0),
|
||||
audio_system_(0), graphics_system_(0), input_system_(0),
|
||||
export_resolver_(0), file_system_(0),
|
||||
@@ -51,11 +48,6 @@ Emulator::~Emulator() {
|
||||
main_window_->Close();
|
||||
}
|
||||
|
||||
// Disconnect all debug clients first.
|
||||
if (debug_server_) {
|
||||
debug_server_->Shutdown();
|
||||
}
|
||||
|
||||
delete xam_;
|
||||
delete xboxkrnl_;
|
||||
delete kernel_state_;
|
||||
@@ -72,8 +64,6 @@ Emulator::~Emulator() {
|
||||
|
||||
delete processor_;
|
||||
|
||||
delete debug_server_;
|
||||
|
||||
delete export_resolver_;
|
||||
}
|
||||
|
||||
@@ -90,10 +80,6 @@ X_STATUS Emulator::Setup() {
|
||||
export_resolver_ = new ExportResolver();
|
||||
XEEXPECTNOTNULL(export_resolver_);
|
||||
|
||||
// Create the debugger.
|
||||
debug_server_ = new DebugServer(this);
|
||||
XEEXPECTNOTNULL(debug_server_);
|
||||
|
||||
// Initialize the CPU.
|
||||
processor_ = new Processor(this);
|
||||
XEEXPECTNOTNULL(processor_);
|
||||
@@ -134,11 +120,6 @@ X_STATUS Emulator::Setup() {
|
||||
xam_ = new XamModule(this, kernel_state_);
|
||||
XEEXPECTNOTNULL(xam_);
|
||||
|
||||
// Prepare the debugger.
|
||||
// This may pause waiting for connections.
|
||||
result = debug_server_->Startup();
|
||||
XEEXPECTZERO(result);
|
||||
|
||||
return result;
|
||||
|
||||
XECLEANUP:
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
#include <xenia/emulator.h>
|
||||
#include <xenia/export_resolver.h>
|
||||
#include <xenia/debug/debug_server.h>
|
||||
#include <xenia/kernel/kernel_state.h>
|
||||
#include <xenia/kernel/xboxkrnl_private.h>
|
||||
#include <xenia/kernel/objects/xuser_module.h>
|
||||
@@ -164,12 +163,6 @@ int XboxkrnlModule::LaunchModule(const char* path) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Spin up the debugger and let it know we are starting.
|
||||
if (emulator_->debug_server()->BeforeEntry()) {
|
||||
XELOGE("Debugger failed to startup.");
|
||||
return 2;
|
||||
}
|
||||
|
||||
// Launch the module.
|
||||
// NOTE: this won't return until the module exits.
|
||||
result_code = module->Launch(0);
|
||||
|
||||
@@ -31,7 +31,6 @@
|
||||
'apu/sources.gypi',
|
||||
'core/sources.gypi',
|
||||
'cpu/sources.gypi',
|
||||
'debug/sources.gypi',
|
||||
'gpu/sources.gypi',
|
||||
'hid/sources.gypi',
|
||||
'kernel/sources.gypi',
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
|
||||
#include <xenia/apu/apu.h>
|
||||
#include <xenia/cpu/cpu.h>
|
||||
#include <xenia/debug/debug_server.h>
|
||||
#include <xenia/gpu/gpu.h>
|
||||
#include <xenia/kernel/kernel.h>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user