|
| 1 | +/*************************************************************************** |
| 2 | + * Copyright (C) 2020 PCSX-Redux authors * |
| 3 | + * * |
| 4 | + * This program is free software; you can redistribute it and/or modify * |
| 5 | + * it under the terms of the GNU General Public License as published by * |
| 6 | + * the Free Software Foundation; either version 2 of the License, or * |
| 7 | + * (at your option) any later version. * |
| 8 | + * * |
| 9 | + * This program is distributed in the hope that it will be useful, * |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 12 | + * GNU General Public License for more details. * |
| 13 | + * * |
| 14 | + * You should have received a copy of the GNU General Public License * |
| 15 | + * along with this program; if not, write to the * |
| 16 | + * Free Software Foundation, Inc., * |
| 17 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * |
| 18 | + ***************************************************************************/ |
| 19 | + |
| 20 | +#include "core/gadp-server.h" |
| 21 | + |
| 22 | +#include <assert.h> |
| 23 | + |
| 24 | +#include "core/debug.h" |
| 25 | +#include "core/misc.h" |
| 26 | +#include "core/psxemulator.h" |
| 27 | +#include "core/psxmem.h" |
| 28 | +#include "core/r3000a.h" |
| 29 | +#include "core/system.h" |
| 30 | +#include "support/protobuf.h" |
| 31 | +#include "uvw.hpp" |
| 32 | + |
| 33 | +#undef VOID |
| 34 | + |
| 35 | +enum class ErrorCode { |
| 36 | + UNKNOWN = 0, |
| 37 | + BAD_REQUEST = 1, |
| 38 | + NO_VERSION = 2, |
| 39 | + NO_OBJECT = 3, |
| 40 | + NO_INTERFACE = 4, |
| 41 | + BAD_ARGUMENT = 5, |
| 42 | + BAD_ADDRESS = 6, |
| 43 | + NOT_SUPPORTED = 7, |
| 44 | + MEMORY_ACCESS = 8, |
| 45 | + REGISTER_ACCESS = 9, |
| 46 | + USER_ERROR = 10, |
| 47 | + MODEL_ACCESS = 11, |
| 48 | +}; |
| 49 | + |
| 50 | +enum class StepKind { |
| 51 | + INTO = 0, |
| 52 | + ADVANCE = 1, |
| 53 | + FINISH = 2, |
| 54 | + LINE = 3, |
| 55 | + OVER = 4, |
| 56 | + OVER_LINE = 5, |
| 57 | + SKIP = 6, |
| 58 | + RETURN = 7, |
| 59 | + UNTIL = 8, |
| 60 | +}; |
| 61 | + |
| 62 | +enum class AttachKind { |
| 63 | + BY_OBJECT_REF = 0, |
| 64 | + BY_ID = 1, |
| 65 | +}; |
| 66 | + |
| 67 | +enum class ExecutionState { |
| 68 | + INACTIVE = 0, |
| 69 | + ACTIVE = 1, |
| 70 | + STOPPED = 2, |
| 71 | + RUNNING = 3, |
| 72 | + TERMINATED = 4, |
| 73 | +}; |
| 74 | + |
| 75 | +enum class PrimitiveKind { |
| 76 | + UNDEFINED = 0, |
| 77 | + VOID = 1, |
| 78 | + UINT = 2, |
| 79 | + SINT = 3, |
| 80 | + FLOAT = 4, |
| 81 | + COMPLEX = 5, |
| 82 | +}; |
| 83 | + |
| 84 | +enum class UpdateMode { |
| 85 | + UNSOLICITED = 0, |
| 86 | + SOLICITED = 1, |
| 87 | + FIXED = 2, |
| 88 | +}; |
| 89 | + |
| 90 | +enum class ValueType { |
| 91 | + VT_VOID = 0, |
| 92 | + VT_BOOL = 1, |
| 93 | + VT_INT = 2, |
| 94 | + VT_LONG = 3, |
| 95 | + VT_FLOAT = 4, |
| 96 | + VT_DOUBLE = 5, |
| 97 | + VT_BYTES = 6, |
| 98 | + VT_STRING = 7, |
| 99 | + VT_STRING_LIST = 8, |
| 100 | + VT_ADDRESS = 9, |
| 101 | + VT_RANGE = 10, |
| 102 | + VT_BREAK_KIND_SET = 11, |
| 103 | + VT_EXECUTION_STATE = 12, |
| 104 | + VT_STEP_KIND_SET = 13, |
| 105 | + VT_PRIMITIVE_KIND = 14, |
| 106 | + VT_DATA_TYPE = 15, |
| 107 | + VT_UPDATE_MODE = 16, |
| 108 | + VT_PATH = 17, |
| 109 | + VT_PATH_LIST = 18, |
| 110 | + VT_TYPE = 19, |
| 111 | + VT_ATTACH_KIND_SET = 20, |
| 112 | +}; |
| 113 | + |
| 114 | +enum class TargetEventType { |
| 115 | + EV_STOPPED = 0, |
| 116 | + EV_RUNNING = 1, |
| 117 | + PROCESS_CREATED = 2, |
| 118 | + PROCESS_EXITED = 3, |
| 119 | + THREAD_CREATED = 4, |
| 120 | + THREAD_EXITED = 5, |
| 121 | + MODULE_LOADED = 6, |
| 122 | + MODULE_UNLOADED = 7, |
| 123 | + BREAKPOINT_HIT = 8, |
| 124 | + STEP_COMPLETED = 9, |
| 125 | + EXCEPTION = 10, |
| 126 | + SIGNAL = 11, |
| 127 | +}; |
| 128 | + |
| 129 | +PCSX::GadpServer::GadpServer() : m_listener(g_system->m_eventBus) { |
| 130 | + m_listener.listen<Events::SettingsLoaded>([this](const auto& event) { |
| 131 | + if (g_emulator->settings.get<Emulator::SettingGadpServer>()) { |
| 132 | + startServer(g_emulator->settings.get<Emulator::SettingGadpServerPort>()); |
| 133 | + } |
| 134 | + }); |
| 135 | + m_listener.listen<Events::Quitting>([this](const auto& event) { |
| 136 | + if (m_serverStatus == SERVER_STARTED) stopServer(); |
| 137 | + }); |
| 138 | +} |
| 139 | + |
| 140 | +void PCSX::GadpServer::stopServer() { |
| 141 | + assert(m_serverStatus == SERVER_STARTED); |
| 142 | + for (auto& client : m_clients) client.close(); |
| 143 | + m_server->close(); |
| 144 | +} |
| 145 | + |
| 146 | +void PCSX::GadpServer::startServer(int port) { |
| 147 | + assert(m_serverStatus == SERVER_STOPPED); |
| 148 | + m_server = g_emulator->m_loop->resource<uvw::TCPHandle>(); |
| 149 | + m_server->on<uvw::ListenEvent>([this](const uvw::ListenEvent&, uvw::TCPHandle& srv) { onNewConnection(); }); |
| 150 | + m_server->on<uvw::CloseEvent>( |
| 151 | + [this](const uvw::CloseEvent&, uvw::TCPHandle& srv) { m_serverStatus = SERVER_STOPPED; }); |
| 152 | + m_server->on<uvw::ErrorEvent>( |
| 153 | + [this](const uvw::ErrorEvent& event, uvw::TCPHandle& srv) { m_gotError = event.what(); }); |
| 154 | + m_gotError = ""; |
| 155 | + m_server->bind("0.0.0.0", port); |
| 156 | + if (!m_gotError.empty()) { |
| 157 | + g_system->printf("Error while trying to bind to port %i: %s\n", port, m_gotError.c_str()); |
| 158 | + m_server->close(); |
| 159 | + return; |
| 160 | + } |
| 161 | + m_server->listen(); |
| 162 | + if (!m_gotError.empty()) { |
| 163 | + g_system->printf("Error while trying to listen to port %i: %s\n", port, m_gotError.c_str()); |
| 164 | + m_server->close(); |
| 165 | + return; |
| 166 | + } |
| 167 | + |
| 168 | + m_serverStatus = SERVER_STARTED; |
| 169 | +} |
| 170 | + |
| 171 | +void PCSX::GadpServer::onNewConnection() { |
| 172 | + GadpClient* client = new GadpClient(m_server); |
| 173 | + m_clients.push_back(client); |
| 174 | + client->accept(m_server); |
| 175 | +} |
| 176 | + |
| 177 | +PCSX::GadpClient::GadpClient(std::shared_ptr<uvw::TCPHandle> srv) |
| 178 | + : m_tcp(srv->loop().resource<uvw::TCPHandle>()), m_listener(g_system->m_eventBus) { |
| 179 | + m_listener.listen<Events::ExecutionFlow::Pause>([this](const auto& event) {}); |
| 180 | + m_listener.listen<Events::ExecutionFlow::ShellReached>([this](const auto& event) {}); |
| 181 | +} |
| 182 | + |
| 183 | +void PCSX::GadpClient::processData(const Slice& slice) { |
| 184 | + const uint8_t* ptr = reinterpret_cast<const uint8_t*>(slice.data()); |
| 185 | + auto size = slice.size(); |
| 186 | + int v = 0; |
| 187 | + |
| 188 | + while (size) { |
| 189 | + switch (m_state) { |
| 190 | + case WAIT_FOR_LEN: |
| 191 | + m_lenBuffer[m_length++] = *ptr++; |
| 192 | + size--; |
| 193 | + if (m_length == 4) { |
| 194 | + m_length = m_lenBuffer[0] | (m_lenBuffer[1] << 8) | (m_lenBuffer[2] << 16) | (m_lenBuffer[3] << 24); |
| 195 | + m_protoBuffer.clear(); |
| 196 | + if (m_length != 0) { |
| 197 | + m_state = READING_DATA; |
| 198 | + } else { |
| 199 | + // process empty proto |
| 200 | + } |
| 201 | + } |
| 202 | + break; |
| 203 | + case READING_DATA: { |
| 204 | + auto copySize = std::min(size, m_length); |
| 205 | + m_protoBuffer += std::string((const char*)ptr, copySize); |
| 206 | + ptr += copySize; |
| 207 | + size -= copySize; |
| 208 | + m_length -= copySize; |
| 209 | + |
| 210 | + if (m_length == 0) { |
| 211 | + m_state = WAIT_FOR_LEN; |
| 212 | + // process proto |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | +} |
0 commit comments