Removing the debugger and dependencies. Needs rethinking.
This commit is contained in:
@@ -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',
|
||||
],
|
||||
}
|
||||
Reference in New Issue
Block a user