// 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/. #include #include #include #include #include "Common/Thread/ThreadUtil.h" #include "Common/TimeUtil.h" #include "Core/Core.h" #include "Core/Debugger/WebSocket.h" #include "Core/Debugger/WebSocket/WebSocketUtils.h" // 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. // // 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. #include "Core/Debugger/WebSocket/GameBroadcaster.h" #include "Core/Debugger/WebSocket/InputBroadcaster.h" #include "Core/Debugger/WebSocket/LogBroadcaster.h" #include "Core/Debugger/WebSocket/SteppingBroadcaster.h" #include "Core/Debugger/WebSocket/BreakpointSubscriber.h" #include "Core/Debugger/WebSocket/CPUCoreSubscriber.h" #include "Core/Debugger/WebSocket/DisasmSubscriber.h" #include "Core/Debugger/WebSocket/GameSubscriber.h" #include "Core/Debugger/WebSocket/GPUBufferSubscriber.h" #include "Core/Debugger/WebSocket/GPUDisasmSubscriber.h" #include "Core/Debugger/WebSocket/GPURecordSubscriber.h" #include "Core/Debugger/WebSocket/GPUStatsSubscriber.h" #include "Core/Debugger/WebSocket/HLESubscriber.h" #include "Core/Debugger/WebSocket/InputSubscriber.h" #include "Core/Debugger/WebSocket/MemoryInfoSubscriber.h" #include "Core/Debugger/WebSocket/MemorySubscriber.h" #include "Core/Debugger/WebSocket/ReplaySubscriber.h" #include "Core/Debugger/WebSocket/SteppingSubscriber.h" #include "Core/Debugger/WebSocket/ClientConfigSubscriber.h" typedef DebuggerSubscriber *(*SubscriberInit)(DebuggerEventHandlerMap &map); static const std::vector subscribers({ &WebSocketBreakpointInit, &WebSocketCPUCoreInit, &WebSocketDisasmInit, &WebSocketGameInit, &WebSocketGPUBufferInit, &WebSocketGPUDisasmInit, &WebSocketGPURecordInit, &WebSocketGPUStatsInit, &WebSocketHLEInit, &WebSocketInputInit, &WebSocketMemoryInfoInit, &WebSocketMemoryInit, &WebSocketReplayInit, &WebSocketSteppingInit, &WebSocketClientConfigInit, }); // 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; // Prevent threading surprises and obscure crashes by locking startup/shutdown. static bool lifecycleLockSetup = false; static std::mutex lifecycleLock; static void UpdateConnected(int delta) { std::lock_guard guard(stopLock); debuggersConnected += delta; stopCond.notify_all(); } // Per-connection mailbox for events the CPU thread produces (cpu.stepping, game.start, ...). // // These used to be polled per connection from the WebSocket thread, which meant every connected // debugger was reading pc, the tick count, the UI state and the param SFO out from under the CPU // thread on every lap of its loop. Now the CPU thread notices the transition once, formats the // event, and drops it in here; the connection's own thread just drains and sends. struct DebuggerEventSink { std::mutex lock; std::vector> pending; // A debugger that connects while the CPU is already stopped still wants to hear about it. bool needsSteppingPrime = true; void Push(const char *category, std::string json) { std::lock_guard guard(lock); pending.emplace_back(category, std::move(json)); } void Take(std::vector> *out) { std::lock_guard guard(lock); out->swap(pending); pending.clear(); } }; static std::mutex g_sinkLock; static std::vector g_sinks; static void RegisterSink(DebuggerEventSink *sink) { std::lock_guard guard(g_sinkLock); g_sinks.push_back(sink); } static void UnregisterSink(DebuggerEventSink *sink) { std::lock_guard guard(g_sinkLock); g_sinks.erase(std::remove(g_sinks.begin(), g_sinks.end(), sink), g_sinks.end()); } void WebSocketDebuggerTick() { // Poll unconditionally, even with nothing connected: these track transitions, and skipping them // would let the "previous" state go stale and fire a bogus event at whoever connects next. const std::string gameEvent = GameBroadcaster::PollChange(); const std::string steppingEvent = SteppingBroadcaster::PollChange(); std::lock_guard guard(g_sinkLock); if (g_sinks.empty()) return; std::string steppingPrime; for (DebuggerEventSink *sink : g_sinks) { if (sink->needsSteppingPrime) { sink->needsSteppingPrime = false; // Only format it if somebody actually needs it. if (steppingPrime.empty()) steppingPrime = SteppingBroadcaster::CurrentState(); if (!steppingPrime.empty()) sink->Push("stepping", steppingPrime); continue; } if (!gameEvent.empty()) sink->Push("game", gameEvent); if (!steppingEvent.empty()) sink->Push("stepping", steppingEvent); } } static void WebSocketNotifyLifecycle(CoreLifecycle stage) { switch (stage) { case CoreLifecycle::STARTING: case CoreLifecycle::STOPPING: case CoreLifecycle::MEMORY_REINITING: if (debuggersConnected > 0) { DEBUG_LOG(Log::System, "Waiting for debugger to complete on shutdown"); } // Keep draining the CPU queue while we wait, instead of a plain blocking lock(). We're on // the CPU thread here, and a debugger thread holding this lock may be parked inside // Core_RunOnCPUThread() waiting for us to run its callback - so blocking outright means // neither side can ever move. Core state is still fully alive at this point (STOPPING is // notified before CPU_Shutdown), so running those callbacks now is safe. while (!lifecycleLock.try_lock()) { Core_ProcessCPUQueue(); sleep_ms(1, "debugger-lifecycle"); } break; case CoreLifecycle::START_COMPLETE: case CoreLifecycle::STOPPED: case CoreLifecycle::MEMORY_REINITED: lifecycleLock.unlock(); if (debuggersConnected > 0) { DEBUG_LOG(Log::System, "Debugger ready for shutdown"); } break; } } static void SetupDebuggerLock() { if (!lifecycleLockSetup) { Core_ListenLifecycle(&WebSocketNotifyLifecycle); lifecycleLockSetup = true; } } void HandleDebuggerRequest(const http::ServerRequest &request) { SetCurrentThreadName("WebSocketDebugger"); net::WebSocketServer *ws = net::WebSocketServer::CreateAsUpgrade(request, "debugger.ppsspp.org"); if (!ws) { return; } UpdateConnected(1); SetupDebuggerLock(); WebSocketClientInfo client_info; auto& disallowed_config = client_info.disallowed; LogBroadcaster logger; InputBroadcaster input; DebuggerEventSink sink; RegisterSink(&sink); DebuggerEventHandlerMap eventHandlers; std::vector subscriberData; for (auto init : subscribers) { std::lock_guard guard(lifecycleLock); subscriberData.push_back(init(eventHandlers)); } // There's a tradeoff between responsiveness to incoming events, and polling for changes. int highActivity = 0; ws->SetTextHandler([&](const std::string &t) { JsonReader reader(t.c_str(), t.size()); if (!reader.ok()) { ws->Send(DebuggerErrorEvent("Bad message: invalid JSON", LogLevel::LERROR)); return; } const JsonGet root = reader.root(); const char *event = root ? root.getStringOr("event", nullptr) : nullptr; if (!event) { ws->Send(DebuggerErrorEvent("Bad message: no event property", LogLevel::LERROR, root)); return; } DEBUG_LOG(Log::Debugger, "WS: Handling '%s'", event); DebuggerRequest req(event, ws, root, &client_info); auto eventFunc = eventHandlers.find(event); if (eventFunc != eventHandlers.end()) { std::lock_guard guard(lifecycleLock); eventFunc->second(req); if (!req.Finish()) { // Poll more frequently for a second in case this triggers something. highActivity = 1000; } } else { req.Fail("Bad message: unknown event"); } }); ws->SetBinaryHandler([&](const std::vector &d) { ERROR_LOG(Log::Debugger, "Received binary WebSocket frame, not supported"); ws->Send(DebuggerErrorEvent("Bad message: binary WebSocket frames are not supported", LogLevel::LERROR)); }); // Don't out-line the highActivity check, it needs to recompute on every lap. constexpr float lowActivityPollTimeStep = 1.0f / 60.0f; constexpr float highActivityPollTimeStep = 1.0f / 1000.0f; while (ws->Process(highActivity ? highActivityPollTimeStep : lowActivityPollTimeStep)) { std::lock_guard guard(lifecycleLock); // These send events that aren't just responses to requests // The client can explicitly ask not to be notified about some events // so we check the client settings first if (!disallowed_config["logger"]) logger.Broadcast(ws); if (!disallowed_config["input"]) input.Broadcast(ws); // Whatever the CPU thread queued up for us since last lap. std::vector> events; sink.Take(&events); for (const auto &ev : events) { if (!disallowed_config[ev.first]) ws->Send(ev.second); } for (size_t i = 0; i < subscribers.size(); ++i) { if (subscriberData[i]) { subscriberData[i]->Broadcast(ws); } } if (stopRequested) { ws->Close(net::WebSocketClose::GOING_AWAY); } if (highActivity > 0) { highActivity--; } } UnregisterSink(&sink); std::lock_guard guard(lifecycleLock); for (size_t i = 0; i < subscribers.size(); ++i) { delete subscriberData[i]; } delete ws; request.In()->Discard(); UpdateConnected(-1); } void StopAllDebuggers() { std::unique_lock guard(stopLock); while (debuggersConnected != 0) { stopRequested = true; stopCond.wait(guard); } // Reset it back for next time. stopRequested = false; }