2018-04-13 05:11:11 +00:00
|
|
|
// Copyright (c) 2017- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
2018-04-21 22:33:34 +00:00
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
2018-04-15 23:31:00 +00:00
|
|
|
#include "thread/threadutil.h"
|
2018-04-13 05:11:11 +00:00
|
|
|
#include "Core/Debugger/WebSocket.h"
|
2018-04-15 15:50:25 +00:00
|
|
|
#include "Core/Debugger/WebSocket/WebSocketUtils.h"
|
2018-04-13 05:11:11 +00:00
|
|
|
|
2018-04-14 21:19:24 +00:00
|
|
|
// This WebSocket (connected through the same port as disc sharing) allows API/debugger access to PPSSPP.
|
|
|
|
// Currently, the only subprotocol "debugger.ppsspp.org" uses a simple JSON based interface.
|
|
|
|
//
|
|
|
|
// Messages to and from PPSSPP follow the same basic format:
|
|
|
|
// { "event": "NAME", ... }
|
|
|
|
//
|
|
|
|
// And are primarily of these types:
|
|
|
|
// * Events from the debugger/client (you) to PPSSPP
|
|
|
|
// If there's a response, it will generally use the same name. It may not be immedate - it's an event.
|
|
|
|
// * Spontaneous events from PPSSPP
|
|
|
|
// Things like logs, breakpoint hits, etc. not directly requested.
|
|
|
|
//
|
|
|
|
// Otherwise you may see error events which indicate PPSSPP couldn't understand or failed internally:
|
|
|
|
// - "event": "error"
|
|
|
|
// - "message": A string describing what happened.
|
|
|
|
// - "level": Integer severity level. (1 = NOTICE, 2 = ERROR, 3 = WARN, 4 = INFO, 5 = DEBUG, 6 = VERBOSE)
|
|
|
|
// - "ticket": Optional, present if in response to an event with a "ticket" field, simply repeats that value.
|
2018-04-22 07:28:41 +00:00
|
|
|
//
|
|
|
|
// At start, please send a "version" event. See WebSocket/GameSubscriber.cpp for more details.
|
|
|
|
//
|
|
|
|
// For other events, look inside Core/Debugger/WebSocket/ for details on each event.
|
2018-04-14 21:19:24 +00:00
|
|
|
|
2018-04-15 15:50:25 +00:00
|
|
|
#include "Core/Debugger/WebSocket/GameBroadcaster.h"
|
|
|
|
#include "Core/Debugger/WebSocket/LogBroadcaster.h"
|
|
|
|
#include "Core/Debugger/WebSocket/SteppingBroadcaster.h"
|
2018-04-13 05:11:11 +00:00
|
|
|
|
2018-04-15 15:50:25 +00:00
|
|
|
#include "Core/Debugger/WebSocket/CPUCoreSubscriber.h"
|
2018-04-23 03:35:24 +00:00
|
|
|
#include "Core/Debugger/WebSocket/DisasmSubscriber.h"
|
2018-04-22 01:26:36 +00:00
|
|
|
#include "Core/Debugger/WebSocket/GameSubscriber.h"
|
2018-04-15 15:50:25 +00:00
|
|
|
|
2018-04-15 22:52:00 +00:00
|
|
|
typedef void *(*SubscriberInit)(DebuggerEventHandlerMap &map);
|
|
|
|
typedef void (*Subscribershutdown)(void *p);
|
|
|
|
struct SubscriberInfo {
|
|
|
|
SubscriberInit init;
|
|
|
|
Subscribershutdown shutdown;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const std::vector<SubscriberInfo> subscribers({
|
|
|
|
{ &WebSocketCPUCoreInit, nullptr },
|
2018-04-23 03:35:24 +00:00
|
|
|
{ &WebSocketDisasmInit, &WebSocketDisasmShutdown },
|
2018-04-22 01:26:36 +00:00
|
|
|
{ &WebSocketGameInit, nullptr },
|
2018-04-14 15:54:04 +00:00
|
|
|
});
|
2018-04-13 05:11:11 +00:00
|
|
|
|
2018-04-21 22:33:34 +00:00
|
|
|
// To handle webserver restart, keep track of how many running.
|
|
|
|
static volatile int debuggersConnected = 0;
|
|
|
|
static volatile bool stopRequested = false;
|
|
|
|
static std::mutex stopLock;
|
|
|
|
static std::condition_variable stopCond;
|
|
|
|
|
2018-04-22 15:33:22 +00:00
|
|
|
// Prevent threading surprises and obscure crashes by locking startup/shutdown.
|
|
|
|
static bool lifecycleLockSetup = false;
|
|
|
|
static std::mutex lifecycleLock;
|
|
|
|
|
2018-04-21 22:33:34 +00:00
|
|
|
static void UpdateConnected(int delta) {
|
|
|
|
std::lock_guard<std::mutex> guard(stopLock);
|
|
|
|
debuggersConnected += delta;
|
|
|
|
stopCond.notify_all();
|
|
|
|
}
|
|
|
|
|
2018-04-22 15:33:22 +00:00
|
|
|
static void WebSocketNotifyLifecycle(CoreLifecycle stage) {
|
|
|
|
switch (stage) {
|
|
|
|
case CoreLifecycle::STARTING:
|
|
|
|
case CoreLifecycle::STOPPING:
|
|
|
|
if (debuggersConnected > 0) {
|
|
|
|
DEBUG_LOG(SYSTEM, "Waiting for debugger to complete on shutdown");
|
|
|
|
}
|
|
|
|
lifecycleLock.lock();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CoreLifecycle::START_COMPLETE:
|
|
|
|
case CoreLifecycle::STOPPED:
|
|
|
|
lifecycleLock.unlock();
|
|
|
|
if (debuggersConnected > 0) {
|
|
|
|
DEBUG_LOG(SYSTEM, "Debugger ready for shutdown");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SetupDebuggerLock() {
|
|
|
|
if (!lifecycleLockSetup) {
|
|
|
|
Core_ListenLifecycle(&WebSocketNotifyLifecycle);
|
|
|
|
lifecycleLockSetup = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 05:11:11 +00:00
|
|
|
void HandleDebuggerRequest(const http::Request &request) {
|
|
|
|
net::WebSocketServer *ws = net::WebSocketServer::CreateAsUpgrade(request, "debugger.ppsspp.org");
|
|
|
|
if (!ws)
|
|
|
|
return;
|
|
|
|
|
2018-04-15 23:31:00 +00:00
|
|
|
setCurrentThreadName("Debugger");
|
2018-04-21 22:33:34 +00:00
|
|
|
UpdateConnected(1);
|
2018-04-22 15:33:22 +00:00
|
|
|
SetupDebuggerLock();
|
2018-04-15 23:31:00 +00:00
|
|
|
|
2018-04-14 15:54:04 +00:00
|
|
|
LogBroadcaster logger;
|
|
|
|
GameBroadcaster game;
|
|
|
|
SteppingBroadcaster stepping;
|
2018-04-13 05:11:11 +00:00
|
|
|
|
2018-04-15 22:52:00 +00:00
|
|
|
std::unordered_map<std::string, DebuggerEventHandler> eventHandlers;
|
|
|
|
std::vector<void *> subscriberData;
|
|
|
|
for (auto info : subscribers) {
|
2018-04-22 15:33:22 +00:00
|
|
|
std::lock_guard<std::mutex> guard(lifecycleLock);
|
2018-04-15 22:52:00 +00:00
|
|
|
subscriberData.push_back(info.init(eventHandlers));
|
|
|
|
}
|
|
|
|
|
2018-04-13 05:11:11 +00:00
|
|
|
ws->SetTextHandler([&](const std::string &t) {
|
2018-04-14 15:54:04 +00:00
|
|
|
JsonReader reader(t.c_str(), t.size());
|
|
|
|
if (!reader.ok()) {
|
|
|
|
ws->Send(DebuggerErrorEvent("Bad message: invalid JSON", LogTypes::LERROR));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const JsonGet root = reader.root();
|
|
|
|
const char *event = root ? root.getString("event", nullptr) : nullptr;
|
|
|
|
if (!event) {
|
2018-04-14 21:19:24 +00:00
|
|
|
ws->Send(DebuggerErrorEvent("Bad message: no event property", LogTypes::LERROR, root));
|
2018-04-14 15:54:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-15 15:50:25 +00:00
|
|
|
DebuggerRequest req(event, ws, root);
|
2018-04-15 22:52:00 +00:00
|
|
|
auto eventFunc = eventHandlers.find(event);
|
|
|
|
if (eventFunc != eventHandlers.end()) {
|
2018-04-22 15:33:22 +00:00
|
|
|
std::lock_guard<std::mutex> guard(lifecycleLock);
|
2018-04-15 15:50:25 +00:00
|
|
|
eventFunc->second(req);
|
|
|
|
req.Finish();
|
2018-04-14 15:54:04 +00:00
|
|
|
} else {
|
2018-04-15 15:50:25 +00:00
|
|
|
req.Fail("Bad message: unknown event");
|
2018-04-14 15:54:04 +00:00
|
|
|
}
|
2018-04-13 05:11:11 +00:00
|
|
|
});
|
|
|
|
ws->SetBinaryHandler([&](const std::vector<uint8_t> &d) {
|
2018-04-14 15:54:04 +00:00
|
|
|
ws->Send(DebuggerErrorEvent("Bad message", LogTypes::LERROR));
|
2018-04-13 05:11:11 +00:00
|
|
|
});
|
|
|
|
|
2018-04-14 15:54:04 +00:00
|
|
|
while (ws->Process(1.0f / 60.0f)) {
|
2018-04-22 15:33:22 +00:00
|
|
|
std::lock_guard<std::mutex> guard(lifecycleLock);
|
2018-04-14 15:54:04 +00:00
|
|
|
// These send events that aren't just responses to requests.
|
|
|
|
logger.Broadcast(ws);
|
|
|
|
game.Broadcast(ws);
|
|
|
|
stepping.Broadcast(ws);
|
2018-04-21 22:33:34 +00:00
|
|
|
|
|
|
|
if (stopRequested) {
|
|
|
|
ws->Close(net::WebSocketClose::GOING_AWAY);
|
|
|
|
}
|
2018-04-13 05:11:11 +00:00
|
|
|
}
|
|
|
|
|
2018-04-22 15:33:22 +00:00
|
|
|
std::lock_guard<std::mutex> guard(lifecycleLock);
|
2018-04-15 22:52:00 +00:00
|
|
|
for (size_t i = 0; i < subscribers.size(); ++i) {
|
|
|
|
if (subscribers[i].shutdown) {
|
|
|
|
subscribers[i].shutdown(subscriberData[i]);
|
|
|
|
} else {
|
|
|
|
assert(!subscriberData[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 05:11:11 +00:00
|
|
|
delete ws;
|
2018-04-21 22:33:34 +00:00
|
|
|
UpdateConnected(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StopAllDebuggers() {
|
|
|
|
std::unique_lock<std::mutex> guard(stopLock);
|
|
|
|
while (debuggersConnected != 0) {
|
|
|
|
stopRequested = true;
|
|
|
|
stopCond.wait(guard);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset it back for next time.
|
|
|
|
stopRequested = false;
|
2018-04-13 05:11:11 +00:00
|
|
|
}
|