CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Core/Debugger/WebSocket.cpp
Views: 1401
// Copyright (c) 2017- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#include <mutex>18#include <condition_variable>19#include "Common/Thread/ThreadUtil.h"20#include "Core/Debugger/WebSocket.h"21#include "Core/Debugger/WebSocket/WebSocketUtils.h"22#include "Core/MemMap.h"2324// This WebSocket (connected through the same port as disc sharing) allows API/debugger access to PPSSPP.25// Currently, the only subprotocol "debugger.ppsspp.org" uses a simple JSON based interface.26//27// Messages to and from PPSSPP follow the same basic format:28// { "event": "NAME", ... }29//30// And are primarily of these types:31// * Events from the debugger/client (you) to PPSSPP32// If there's a response, it will generally use the same name. It may not be immedate - it's an event.33// * Spontaneous events from PPSSPP34// Things like logs, breakpoint hits, etc. not directly requested.35//36// Otherwise you may see error events which indicate PPSSPP couldn't understand or failed internally:37// - "event": "error"38// - "message": A string describing what happened.39// - "level": Integer severity level. (1 = NOTICE, 2 = ERROR, 3 = WARN, 4 = INFO, 5 = DEBUG, 6 = VERBOSE)40// - "ticket": Optional, present if in response to an event with a "ticket" field, simply repeats that value.41//42// At start, please send a "version" event. See WebSocket/GameSubscriber.cpp for more details.43//44// For other events, look inside Core/Debugger/WebSocket/ for details on each event.4546#include "Core/Debugger/WebSocket/GameBroadcaster.h"47#include "Core/Debugger/WebSocket/InputBroadcaster.h"48#include "Core/Debugger/WebSocket/LogBroadcaster.h"49#include "Core/Debugger/WebSocket/SteppingBroadcaster.h"5051#include "Core/Debugger/WebSocket/BreakpointSubscriber.h"52#include "Core/Debugger/WebSocket/CPUCoreSubscriber.h"53#include "Core/Debugger/WebSocket/DisasmSubscriber.h"54#include "Core/Debugger/WebSocket/GameSubscriber.h"55#include "Core/Debugger/WebSocket/GPUBufferSubscriber.h"56#include "Core/Debugger/WebSocket/GPURecordSubscriber.h"57#include "Core/Debugger/WebSocket/GPUStatsSubscriber.h"58#include "Core/Debugger/WebSocket/HLESubscriber.h"59#include "Core/Debugger/WebSocket/InputSubscriber.h"60#include "Core/Debugger/WebSocket/MemoryInfoSubscriber.h"61#include "Core/Debugger/WebSocket/MemorySubscriber.h"62#include "Core/Debugger/WebSocket/ReplaySubscriber.h"63#include "Core/Debugger/WebSocket/SteppingSubscriber.h"64#include "Core/Debugger/WebSocket/ClientConfigSubscriber.h"6566typedef DebuggerSubscriber *(*SubscriberInit)(DebuggerEventHandlerMap &map);67static const std::vector<SubscriberInit> subscribers({68&WebSocketBreakpointInit,69&WebSocketCPUCoreInit,70&WebSocketDisasmInit,71&WebSocketGameInit,72&WebSocketGPUBufferInit,73&WebSocketGPURecordInit,74&WebSocketGPUStatsInit,75&WebSocketHLEInit,76&WebSocketInputInit,77&WebSocketMemoryInfoInit,78&WebSocketMemoryInit,79&WebSocketReplayInit,80&WebSocketSteppingInit,81&WebSocketClientConfigInit,82});8384// To handle webserver restart, keep track of how many running.85static volatile int debuggersConnected = 0;86static volatile bool stopRequested = false;87static std::mutex stopLock;88static std::condition_variable stopCond;8990// Prevent threading surprises and obscure crashes by locking startup/shutdown.91static bool lifecycleLockSetup = false;92static std::mutex lifecycleLock;9394static void UpdateConnected(int delta) {95std::lock_guard<std::mutex> guard(stopLock);96debuggersConnected += delta;97stopCond.notify_all();98}99100static void WebSocketNotifyLifecycle(CoreLifecycle stage) {101// We'll likely already be locked during the reboot.102if (PSP_IsRebooting())103return;104105switch (stage) {106case CoreLifecycle::STARTING:107case CoreLifecycle::STOPPING:108case CoreLifecycle::MEMORY_REINITING:109if (debuggersConnected > 0) {110DEBUG_LOG(Log::System, "Waiting for debugger to complete on shutdown");111}112lifecycleLock.lock();113break;114115case CoreLifecycle::START_COMPLETE:116case CoreLifecycle::STOPPED:117case CoreLifecycle::MEMORY_REINITED:118lifecycleLock.unlock();119if (debuggersConnected > 0) {120DEBUG_LOG(Log::System, "Debugger ready for shutdown");121}122break;123}124}125126static void SetupDebuggerLock() {127if (!lifecycleLockSetup) {128Core_ListenLifecycle(&WebSocketNotifyLifecycle);129lifecycleLockSetup = true;130}131}132133void HandleDebuggerRequest(const http::ServerRequest &request) {134net::WebSocketServer *ws = net::WebSocketServer::CreateAsUpgrade(request, "debugger.ppsspp.org");135if (!ws)136return;137138SetCurrentThreadName("Debugger");139UpdateConnected(1);140SetupDebuggerLock();141142WebSocketClientInfo client_info;143auto& disallowed_config = client_info.disallowed;144145GameBroadcaster game;146LogBroadcaster logger;147InputBroadcaster input;148SteppingBroadcaster stepping;149150std::unordered_map<std::string, DebuggerEventHandler> eventHandlers;151std::vector<DebuggerSubscriber *> subscriberData;152for (auto init : subscribers) {153std::lock_guard<std::mutex> guard(lifecycleLock);154subscriberData.push_back(init(eventHandlers));155}156157// There's a tradeoff between responsiveness to incoming events, and polling for changes.158int highActivity = 0;159ws->SetTextHandler([&](const std::string &t) {160JsonReader reader(t.c_str(), t.size());161if (!reader.ok()) {162ws->Send(DebuggerErrorEvent("Bad message: invalid JSON", LogLevel::LERROR));163return;164}165166const JsonGet root = reader.root();167const char *event = root ? root.getStringOr("event", nullptr) : nullptr;168if (!event) {169ws->Send(DebuggerErrorEvent("Bad message: no event property", LogLevel::LERROR, root));170return;171}172173DebuggerRequest req(event, ws, root, &client_info);174auto eventFunc = eventHandlers.find(event);175if (eventFunc != eventHandlers.end()) {176std::lock_guard<std::mutex> guard(lifecycleLock);177eventFunc->second(req);178if (!req.Finish()) {179// Poll more frequently for a second in case this triggers something.180highActivity = 1000;181}182} else {183req.Fail("Bad message: unknown event");184}185});186ws->SetBinaryHandler([&](const std::vector<uint8_t> &d) {187ws->Send(DebuggerErrorEvent("Bad message", LogLevel::LERROR));188});189190while (ws->Process(highActivity ? 1.0f / 1000.0f : 1.0f / 60.0f)) {191std::lock_guard<std::mutex> guard(lifecycleLock);192// These send events that aren't just responses to requests193194// The client can explicitly ask not to be notified about some events195// so we check the client settings first196if (!disallowed_config["logger"])197logger.Broadcast(ws);198if (!disallowed_config["game"])199game.Broadcast(ws);200if (!disallowed_config["stepping"])201stepping.Broadcast(ws);202if (!disallowed_config["input"])203input.Broadcast(ws);204205for (size_t i = 0; i < subscribers.size(); ++i) {206if (subscriberData[i]) {207subscriberData[i]->Broadcast(ws);208}209}210211if (stopRequested) {212ws->Close(net::WebSocketClose::GOING_AWAY);213}214if (highActivity > 0) {215highActivity--;216}217}218219std::lock_guard<std::mutex> guard(lifecycleLock);220for (size_t i = 0; i < subscribers.size(); ++i) {221delete subscriberData[i];222}223224delete ws;225request.In()->Discard();226UpdateConnected(-1);227}228229void StopAllDebuggers() {230std::unique_lock<std::mutex> guard(stopLock);231while (debuggersConnected != 0) {232stopRequested = true;233stopCond.wait(guard);234}235236// Reset it back for next time.237stopRequested = false;238}239240241