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>
|
2020-10-01 07:27:25 +00:00
|
|
|
#include "Common/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-05-06 17:40:02 +00:00
|
|
|
#include "Core/MemMap.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"
|
2021-01-19 01:51:35 +00:00
|
|
|
#include "Core/Debugger/WebSocket/InputBroadcaster.h"
|
2018-04-15 15:50:25 +00:00
|
|
|
#include "Core/Debugger/WebSocket/LogBroadcaster.h"
|
|
|
|
#include "Core/Debugger/WebSocket/SteppingBroadcaster.h"
|
2018-04-13 05:11:11 +00:00
|
|
|
|
2018-05-03 13:20:20 +00:00
|
|
|
#include "Core/Debugger/WebSocket/BreakpointSubscriber.h"
|
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-06-16 22:07:25 +00:00
|
|
|
#include "Core/Debugger/WebSocket/GPUBufferSubscriber.h"
|
2018-09-01 17:58:28 +00:00
|
|
|
#include "Core/Debugger/WebSocket/GPURecordSubscriber.h"
|
2021-09-19 21:24:43 +00:00
|
|
|
#include "Core/Debugger/WebSocket/GPUStatsSubscriber.h"
|
2018-05-09 00:23:14 +00:00
|
|
|
#include "Core/Debugger/WebSocket/HLESubscriber.h"
|
2021-01-19 00:58:34 +00:00
|
|
|
#include "Core/Debugger/WebSocket/InputSubscriber.h"
|
2021-04-05 03:23:51 +00:00
|
|
|
#include "Core/Debugger/WebSocket/MemoryInfoSubscriber.h"
|
2020-07-25 22:18:16 +00:00
|
|
|
#include "Core/Debugger/WebSocket/MemorySubscriber.h"
|
2021-05-24 04:23:13 +00:00
|
|
|
#include "Core/Debugger/WebSocket/ReplaySubscriber.h"
|
2018-04-30 05:02:04 +00:00
|
|
|
#include "Core/Debugger/WebSocket/SteppingSubscriber.h"
|
2023-05-31 16:17:43 +00:00
|
|
|
#include "Core/Debugger/WebSocket/ClientConfigSubscriber.h"
|
2018-04-15 15:50:25 +00:00
|
|
|
|
2018-09-01 17:15:22 +00:00
|
|
|
typedef DebuggerSubscriber *(*SubscriberInit)(DebuggerEventHandlerMap &map);
|
|
|
|
static const std::vector<SubscriberInit> subscribers({
|
|
|
|
&WebSocketBreakpointInit,
|
|
|
|
&WebSocketCPUCoreInit,
|
|
|
|
&WebSocketDisasmInit,
|
|
|
|
&WebSocketGameInit,
|
|
|
|
&WebSocketGPUBufferInit,
|
2018-09-01 17:58:28 +00:00
|
|
|
&WebSocketGPURecordInit,
|
2021-09-19 21:24:43 +00:00
|
|
|
&WebSocketGPUStatsInit,
|
2018-09-01 17:15:22 +00:00
|
|
|
&WebSocketHLEInit,
|
2021-01-19 00:58:34 +00:00
|
|
|
&WebSocketInputInit,
|
2021-04-05 03:23:51 +00:00
|
|
|
&WebSocketMemoryInfoInit,
|
2020-07-25 22:18:16 +00:00
|
|
|
&WebSocketMemoryInit,
|
2021-05-24 04:23:13 +00:00
|
|
|
&WebSocketReplayInit,
|
2018-09-01 17:15:22 +00:00
|
|
|
&WebSocketSteppingInit,
|
2023-05-31 16:17:43 +00:00
|
|
|
&WebSocketClientConfigInit,
|
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) {
|
2022-10-02 01:13:22 +00:00
|
|
|
// We'll likely already be locked during the reboot.
|
|
|
|
if (PSP_IsRebooting())
|
|
|
|
return;
|
|
|
|
|
2018-04-22 15:33:22 +00:00
|
|
|
switch (stage) {
|
|
|
|
case CoreLifecycle::STARTING:
|
|
|
|
case CoreLifecycle::STOPPING:
|
2018-05-06 17:40:02 +00:00
|
|
|
case CoreLifecycle::MEMORY_REINITING:
|
2018-04-22 15:33:22 +00:00
|
|
|
if (debuggersConnected > 0) {
|
|
|
|
DEBUG_LOG(SYSTEM, "Waiting for debugger to complete on shutdown");
|
|
|
|
}
|
|
|
|
lifecycleLock.lock();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CoreLifecycle::START_COMPLETE:
|
|
|
|
case CoreLifecycle::STOPPED:
|
2018-05-06 17:40:02 +00:00
|
|
|
case CoreLifecycle::MEMORY_REINITED:
|
2018-04-22 15:33:22 +00:00
|
|
|
lifecycleLock.unlock();
|
|
|
|
if (debuggersConnected > 0) {
|
|
|
|
DEBUG_LOG(SYSTEM, "Debugger ready for shutdown");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SetupDebuggerLock() {
|
|
|
|
if (!lifecycleLockSetup) {
|
|
|
|
Core_ListenLifecycle(&WebSocketNotifyLifecycle);
|
|
|
|
lifecycleLockSetup = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-21 20:04:05 +00:00
|
|
|
void HandleDebuggerRequest(const http::ServerRequest &request) {
|
2018-04-13 05:11:11 +00:00
|
|
|
net::WebSocketServer *ws = net::WebSocketServer::CreateAsUpgrade(request, "debugger.ppsspp.org");
|
|
|
|
if (!ws)
|
|
|
|
return;
|
|
|
|
|
2020-11-30 23:46:26 +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
|
|
|
|
2023-05-27 14:59:45 +00:00
|
|
|
WebSocketClientInfo client_info;
|
2023-06-05 18:51:13 +00:00
|
|
|
auto& disallowed_config = client_info.disallowed;
|
2023-05-27 14:59:45 +00:00
|
|
|
|
2018-04-14 15:54:04 +00:00
|
|
|
GameBroadcaster game;
|
2021-01-19 01:51:35 +00:00
|
|
|
LogBroadcaster logger;
|
|
|
|
InputBroadcaster input;
|
2018-04-14 15:54:04 +00:00
|
|
|
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;
|
2018-09-01 17:15:22 +00:00
|
|
|
std::vector<DebuggerSubscriber *> subscriberData;
|
|
|
|
for (auto init : subscribers) {
|
2018-04-22 15:33:22 +00:00
|
|
|
std::lock_guard<std::mutex> guard(lifecycleLock);
|
2018-09-01 17:15:22 +00:00
|
|
|
subscriberData.push_back(init(eventHandlers));
|
2018-04-15 22:52:00 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 03:39:18 +00:00
|
|
|
// There's a tradeoff between responsiveness to incoming events, and polling for changes.
|
|
|
|
int highActivity = 0;
|
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;
|
|
|
|
}
|
|
|
|
|
2023-05-27 14:59:45 +00:00
|
|
|
DebuggerRequest req(event, ws, root, &client_info);
|
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);
|
2018-05-01 03:39:18 +00:00
|
|
|
if (!req.Finish()) {
|
|
|
|
// Poll more frequently for a second in case this triggers something.
|
|
|
|
highActivity = 1000;
|
|
|
|
}
|
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-05-01 03:39:18 +00:00
|
|
|
while (ws->Process(highActivity ? 1.0f / 1000.0f : 1.0f / 60.0f)) {
|
2018-04-22 15:33:22 +00:00
|
|
|
std::lock_guard<std::mutex> guard(lifecycleLock);
|
2023-05-31 16:17:43 +00:00
|
|
|
// These send events that aren't just responses to requests
|
|
|
|
|
|
|
|
// The client can explicitly ask not to be notified about some events
|
2023-06-01 14:12:22 +00:00
|
|
|
// so we check the client settings first
|
2023-06-05 18:51:13 +00:00
|
|
|
if (!disallowed_config["logger"])
|
2023-05-31 16:17:43 +00:00
|
|
|
logger.Broadcast(ws);
|
2023-06-05 18:51:13 +00:00
|
|
|
if (!disallowed_config["game"])
|
2023-05-31 16:17:43 +00:00
|
|
|
game.Broadcast(ws);
|
2023-06-05 18:51:13 +00:00
|
|
|
if (!disallowed_config["stepping"])
|
2023-05-31 16:17:43 +00:00
|
|
|
stepping.Broadcast(ws);
|
2023-06-05 18:51:13 +00:00
|
|
|
if (!disallowed_config["input"])
|
2023-05-31 16:17:43 +00:00
|
|
|
input.Broadcast(ws);
|
2018-04-21 22:33:34 +00:00
|
|
|
|
2018-09-01 17:15:22 +00:00
|
|
|
for (size_t i = 0; i < subscribers.size(); ++i) {
|
|
|
|
if (subscriberData[i]) {
|
|
|
|
subscriberData[i]->Broadcast(ws);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-21 22:33:34 +00:00
|
|
|
if (stopRequested) {
|
|
|
|
ws->Close(net::WebSocketClose::GOING_AWAY);
|
|
|
|
}
|
2018-05-01 03:39:18 +00:00
|
|
|
if (highActivity > 0) {
|
|
|
|
highActivity--;
|
|
|
|
}
|
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) {
|
2018-09-01 17:15:22 +00:00
|
|
|
delete subscriberData[i];
|
2018-04-15 22:52:00 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 05:11:11 +00:00
|
|
|
delete ws;
|
2020-12-28 17:30:36 +00:00
|
|
|
request.In()->Discard();
|
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
|
|
|
}
|