gdbstub.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. // Copyright 2013 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. // Originally written by Sven Peter <sven@fail0verflow.com> for anergistic.
  5. #include <algorithm>
  6. #include <atomic>
  7. #include <climits>
  8. #include <csignal>
  9. #include <cstdarg>
  10. #include <cstdio>
  11. #include <cstring>
  12. #include <map>
  13. #include <numeric>
  14. #include <fcntl.h>
  15. #ifdef _WIN32
  16. #include <winsock2.h>
  17. // winsock2.h needs to be included first to prevent winsock.h being included by other includes
  18. #include <io.h>
  19. #include <iphlpapi.h>
  20. #include <ws2tcpip.h>
  21. #define SHUT_RDWR 2
  22. #else
  23. #include <netinet/in.h>
  24. #include <sys/select.h>
  25. #include <sys/socket.h>
  26. #include <sys/un.h>
  27. #include <unistd.h>
  28. #endif
  29. #include "common/logging/log.h"
  30. #include "common/string_util.h"
  31. #include "common/swap.h"
  32. #include "core/arm/arm_interface.h"
  33. #include "core/core.h"
  34. #include "core/core_cpu.h"
  35. #include "core/gdbstub/gdbstub.h"
  36. #include "core/hle/kernel/scheduler.h"
  37. #include "core/loader/loader.h"
  38. #include "core/memory.h"
  39. namespace GDBStub {
  40. namespace {
  41. constexpr int GDB_BUFFER_SIZE = 10000;
  42. constexpr char GDB_STUB_START = '$';
  43. constexpr char GDB_STUB_END = '#';
  44. constexpr char GDB_STUB_ACK = '+';
  45. constexpr char GDB_STUB_NACK = '-';
  46. #ifndef SIGTRAP
  47. constexpr u32 SIGTRAP = 5;
  48. #endif
  49. #ifndef SIGTERM
  50. constexpr u32 SIGTERM = 15;
  51. #endif
  52. #ifndef MSG_WAITALL
  53. constexpr u32 MSG_WAITALL = 8;
  54. #endif
  55. constexpr u32 LR_REGISTER = 30;
  56. constexpr u32 SP_REGISTER = 31;
  57. constexpr u32 PC_REGISTER = 32;
  58. constexpr u32 CPSR_REGISTER = 33;
  59. constexpr u32 UC_ARM64_REG_Q0 = 34;
  60. constexpr u32 FPSCR_REGISTER = 66;
  61. // TODO/WiP - Used while working on support for FPU
  62. constexpr u32 TODO_DUMMY_REG_997 = 997;
  63. constexpr u32 TODO_DUMMY_REG_998 = 998;
  64. // For sample XML files see the GDB source /gdb/features
  65. // GDB also wants the l character at the start
  66. // This XML defines what the registers are for this specific ARM device
  67. constexpr char target_xml[] =
  68. R"(l<?xml version="1.0"?>
  69. <!DOCTYPE target SYSTEM "gdb-target.dtd">
  70. <target version="1.0">
  71. <feature name="org.gnu.gdb.aarch64.core">
  72. <reg name="x0" bitsize="64"/>
  73. <reg name="x1" bitsize="64"/>
  74. <reg name="x2" bitsize="64"/>
  75. <reg name="x3" bitsize="64"/>
  76. <reg name="x4" bitsize="64"/>
  77. <reg name="x5" bitsize="64"/>
  78. <reg name="x6" bitsize="64"/>
  79. <reg name="x7" bitsize="64"/>
  80. <reg name="x8" bitsize="64"/>
  81. <reg name="x9" bitsize="64"/>
  82. <reg name="x10" bitsize="64"/>
  83. <reg name="x11" bitsize="64"/>
  84. <reg name="x12" bitsize="64"/>
  85. <reg name="x13" bitsize="64"/>
  86. <reg name="x14" bitsize="64"/>
  87. <reg name="x15" bitsize="64"/>
  88. <reg name="x16" bitsize="64"/>
  89. <reg name="x17" bitsize="64"/>
  90. <reg name="x18" bitsize="64"/>
  91. <reg name="x19" bitsize="64"/>
  92. <reg name="x20" bitsize="64"/>
  93. <reg name="x21" bitsize="64"/>
  94. <reg name="x22" bitsize="64"/>
  95. <reg name="x23" bitsize="64"/>
  96. <reg name="x24" bitsize="64"/>
  97. <reg name="x25" bitsize="64"/>
  98. <reg name="x26" bitsize="64"/>
  99. <reg name="x27" bitsize="64"/>
  100. <reg name="x28" bitsize="64"/>
  101. <reg name="x29" bitsize="64"/>
  102. <reg name="x30" bitsize="64"/>
  103. <reg name="sp" bitsize="64" type="data_ptr"/>
  104. <reg name="pc" bitsize="64" type="code_ptr"/>
  105. <flags id="cpsr_flags" size="4">
  106. <field name="SP" start="0" end="0"/>
  107. <field name="" start="1" end="1"/>
  108. <field name="EL" start="2" end="3"/>
  109. <field name="nRW" start="4" end="4"/>
  110. <field name="" start="5" end="5"/>
  111. <field name="F" start="6" end="6"/>
  112. <field name="I" start="7" end="7"/>
  113. <field name="A" start="8" end="8"/>
  114. <field name="D" start="9" end="9"/>
  115. <field name="IL" start="20" end="20"/>
  116. <field name="SS" start="21" end="21"/>
  117. <field name="V" start="28" end="28"/>
  118. <field name="C" start="29" end="29"/>
  119. <field name="Z" start="30" end="30"/>
  120. <field name="N" start="31" end="31"/>
  121. </flags>
  122. <reg name="cpsr" bitsize="32" type="cpsr_flags"/>
  123. </feature>
  124. <feature name="org.gnu.gdb.aarch64.fpu">
  125. </feature>
  126. </target>
  127. )";
  128. int gdbserver_socket = -1;
  129. u8 command_buffer[GDB_BUFFER_SIZE];
  130. u32 command_length;
  131. u32 latest_signal = 0;
  132. bool memory_break = false;
  133. Kernel::Thread* current_thread = nullptr;
  134. u32 current_core = 0;
  135. // Binding to a port within the reserved ports range (0-1023) requires root permissions,
  136. // so default to a port outside of that range.
  137. u16 gdbstub_port = 24689;
  138. bool halt_loop = true;
  139. bool step_loop = false;
  140. bool send_trap = false;
  141. // If set to false, the server will never be started and no
  142. // gdbstub-related functions will be executed.
  143. std::atomic<bool> server_enabled(false);
  144. #ifdef _WIN32
  145. WSADATA InitData;
  146. #endif
  147. struct Breakpoint {
  148. bool active;
  149. VAddr addr;
  150. u64 len;
  151. };
  152. std::map<u64, Breakpoint> breakpoints_execute;
  153. std::map<u64, Breakpoint> breakpoints_read;
  154. std::map<u64, Breakpoint> breakpoints_write;
  155. struct Module {
  156. std::string name;
  157. VAddr beg;
  158. VAddr end;
  159. };
  160. std::vector<Module> modules;
  161. } // Anonymous namespace
  162. void RegisterModule(std::string name, VAddr beg, VAddr end, bool add_elf_ext) {
  163. Module module;
  164. if (add_elf_ext) {
  165. Common::SplitPath(name, nullptr, &module.name, nullptr);
  166. module.name += ".elf";
  167. } else {
  168. module.name = std::move(name);
  169. }
  170. module.beg = beg;
  171. module.end = end;
  172. modules.push_back(std::move(module));
  173. }
  174. static Kernel::Thread* FindThreadById(int id) {
  175. for (u32 core = 0; core < Core::NUM_CPU_CORES; core++) {
  176. const auto& threads = Core::System::GetInstance().Scheduler(core)->GetThreadList();
  177. for (auto& thread : threads) {
  178. if (thread->GetThreadId() == static_cast<u32>(id)) {
  179. current_core = core;
  180. return thread.get();
  181. }
  182. }
  183. }
  184. return nullptr;
  185. }
  186. static u64 RegRead(std::size_t id, Kernel::Thread* thread = nullptr) {
  187. if (!thread) {
  188. return 0;
  189. }
  190. if (id < SP_REGISTER) {
  191. return thread->context.cpu_registers[id];
  192. } else if (id == SP_REGISTER) {
  193. return thread->context.sp;
  194. } else if (id == PC_REGISTER) {
  195. return thread->context.pc;
  196. } else if (id == CPSR_REGISTER) {
  197. return thread->context.cpsr;
  198. } else if (id > CPSR_REGISTER && id < FPSCR_REGISTER) {
  199. return thread->context.fpu_registers[id - UC_ARM64_REG_Q0][0];
  200. } else {
  201. return 0;
  202. }
  203. }
  204. static void RegWrite(std::size_t id, u64 val, Kernel::Thread* thread = nullptr) {
  205. if (!thread) {
  206. return;
  207. }
  208. if (id < SP_REGISTER) {
  209. thread->context.cpu_registers[id] = val;
  210. } else if (id == SP_REGISTER) {
  211. thread->context.sp = val;
  212. } else if (id == PC_REGISTER) {
  213. thread->context.pc = val;
  214. } else if (id == CPSR_REGISTER) {
  215. thread->context.cpsr = val;
  216. } else if (id > CPSR_REGISTER && id < FPSCR_REGISTER) {
  217. thread->context.fpu_registers[id - (CPSR_REGISTER + 1)][0] = val;
  218. }
  219. }
  220. /**
  221. * Turns hex string character into the equivalent byte.
  222. *
  223. * @param hex Input hex character to be turned into byte.
  224. */
  225. static u8 HexCharToValue(u8 hex) {
  226. if (hex >= '0' && hex <= '9') {
  227. return hex - '0';
  228. } else if (hex >= 'a' && hex <= 'f') {
  229. return hex - 'a' + 0xA;
  230. } else if (hex >= 'A' && hex <= 'F') {
  231. return hex - 'A' + 0xA;
  232. }
  233. LOG_ERROR(Debug_GDBStub, "Invalid nibble: {} ({:02X})", hex, hex);
  234. return 0;
  235. }
  236. /**
  237. * Turn nibble of byte into hex string character.
  238. *
  239. * @param n Nibble to be turned into hex character.
  240. */
  241. static u8 NibbleToHex(u8 n) {
  242. n &= 0xF;
  243. if (n < 0xA) {
  244. return '0' + n;
  245. } else {
  246. return 'a' + n - 0xA;
  247. }
  248. }
  249. /**
  250. * Converts input hex string characters into an array of equivalent of u8 bytes.
  251. *
  252. * @param src Pointer to array of output hex string characters.
  253. * @param len Length of src array.
  254. */
  255. static u32 HexToInt(const u8* src, size_t len) {
  256. u32 output = 0;
  257. while (len-- > 0) {
  258. output = (output << 4) | HexCharToValue(src[0]);
  259. src++;
  260. }
  261. return output;
  262. }
  263. /**
  264. * Converts input hex string characters into an array of equivalent of u8 bytes.
  265. *
  266. * @param src Pointer to array of output hex string characters.
  267. * @param len Length of src array.
  268. */
  269. static u64 HexToLong(const u8* src, size_t len) {
  270. u64 output = 0;
  271. while (len-- > 0) {
  272. output = (output << 4) | HexCharToValue(src[0]);
  273. src++;
  274. }
  275. return output;
  276. }
  277. /**
  278. * Converts input array of u8 bytes into their equivalent hex string characters.
  279. *
  280. * @param dest Pointer to buffer to store output hex string characters.
  281. * @param src Pointer to array of u8 bytes.
  282. * @param len Length of src array.
  283. */
  284. static void MemToGdbHex(u8* dest, const u8* src, size_t len) {
  285. while (len-- > 0) {
  286. u8 tmp = *src++;
  287. *dest++ = NibbleToHex(tmp >> 4);
  288. *dest++ = NibbleToHex(tmp);
  289. }
  290. }
  291. /**
  292. * Converts input gdb-formatted hex string characters into an array of equivalent of u8 bytes.
  293. *
  294. * @param dest Pointer to buffer to store u8 bytes.
  295. * @param src Pointer to array of output hex string characters.
  296. * @param len Length of src array.
  297. */
  298. static void GdbHexToMem(u8* dest, const u8* src, size_t len) {
  299. while (len-- > 0) {
  300. *dest++ = (HexCharToValue(src[0]) << 4) | HexCharToValue(src[1]);
  301. src += 2;
  302. }
  303. }
  304. /**
  305. * Convert a u32 into a gdb-formatted hex string.
  306. *
  307. * @param dest Pointer to buffer to store output hex string characters.
  308. * @param v Value to convert.
  309. */
  310. static void IntToGdbHex(u8* dest, u32 v) {
  311. for (int i = 0; i < 8; i += 2) {
  312. dest[i + 1] = NibbleToHex(static_cast<u8>(v >> (4 * i)));
  313. dest[i] = NibbleToHex(static_cast<u8>(v >> (4 * (i + 1))));
  314. }
  315. }
  316. /**
  317. * Convert a u64 into a gdb-formatted hex string.
  318. *
  319. * @param dest Pointer to buffer to store output hex string characters.
  320. * @param v Value to convert.
  321. */
  322. static void LongToGdbHex(u8* dest, u64 v) {
  323. for (int i = 0; i < 16; i += 2) {
  324. dest[i + 1] = NibbleToHex(static_cast<u8>(v >> (4 * i)));
  325. dest[i] = NibbleToHex(static_cast<u8>(v >> (4 * (i + 1))));
  326. }
  327. }
  328. /**
  329. * Convert a gdb-formatted hex string into a u32.
  330. *
  331. * @param src Pointer to hex string.
  332. */
  333. static u32 GdbHexToInt(const u8* src) {
  334. u32 output = 0;
  335. for (int i = 0; i < 8; i += 2) {
  336. output = (output << 4) | HexCharToValue(src[7 - i - 1]);
  337. output = (output << 4) | HexCharToValue(src[7 - i]);
  338. }
  339. return output;
  340. }
  341. /**
  342. * Convert a gdb-formatted hex string into a u64.
  343. *
  344. * @param src Pointer to hex string.
  345. */
  346. static u64 GdbHexToLong(const u8* src) {
  347. u64 output = 0;
  348. for (int i = 0; i < 16; i += 2) {
  349. output = (output << 4) | HexCharToValue(src[15 - i - 1]);
  350. output = (output << 4) | HexCharToValue(src[15 - i]);
  351. }
  352. return output;
  353. }
  354. /// Read a byte from the gdb client.
  355. static u8 ReadByte() {
  356. u8 c;
  357. size_t received_size = recv(gdbserver_socket, reinterpret_cast<char*>(&c), 1, MSG_WAITALL);
  358. if (received_size != 1) {
  359. LOG_ERROR(Debug_GDBStub, "recv failed: {}", received_size);
  360. Shutdown();
  361. }
  362. return c;
  363. }
  364. /// Calculate the checksum of the current command buffer.
  365. static u8 CalculateChecksum(const u8* buffer, size_t length) {
  366. return static_cast<u8>(std::accumulate(buffer, buffer + length, 0, std::plus<u8>()));
  367. }
  368. /**
  369. * Get the list of breakpoints for a given breakpoint type.
  370. *
  371. * @param type Type of breakpoint list.
  372. */
  373. static std::map<u64, Breakpoint>& GetBreakpointList(BreakpointType type) {
  374. switch (type) {
  375. case BreakpointType::Execute:
  376. return breakpoints_execute;
  377. case BreakpointType::Read:
  378. return breakpoints_read;
  379. case BreakpointType::Write:
  380. return breakpoints_write;
  381. default:
  382. return breakpoints_read;
  383. }
  384. }
  385. /**
  386. * Remove the breakpoint from the given address of the specified type.
  387. *
  388. * @param type Type of breakpoint.
  389. * @param addr Address of breakpoint.
  390. */
  391. static void RemoveBreakpoint(BreakpointType type, VAddr addr) {
  392. std::map<u64, Breakpoint>& p = GetBreakpointList(type);
  393. auto bp = p.find(static_cast<u64>(addr));
  394. if (bp != p.end()) {
  395. LOG_DEBUG(Debug_GDBStub, "gdb: removed a breakpoint: {:016X} bytes at {:016X} of type {}",
  396. bp->second.len, bp->second.addr, static_cast<int>(type));
  397. p.erase(static_cast<u64>(addr));
  398. }
  399. }
  400. BreakpointAddress GetNextBreakpointFromAddress(VAddr addr, BreakpointType type) {
  401. std::map<u64, Breakpoint>& p = GetBreakpointList(type);
  402. auto next_breakpoint = p.lower_bound(static_cast<u64>(addr));
  403. BreakpointAddress breakpoint;
  404. if (next_breakpoint != p.end()) {
  405. breakpoint.address = next_breakpoint->first;
  406. breakpoint.type = type;
  407. } else {
  408. breakpoint.address = 0;
  409. breakpoint.type = BreakpointType::None;
  410. }
  411. return breakpoint;
  412. }
  413. bool CheckBreakpoint(VAddr addr, BreakpointType type) {
  414. if (!IsConnected()) {
  415. return false;
  416. }
  417. std::map<u64, Breakpoint>& p = GetBreakpointList(type);
  418. auto bp = p.find(static_cast<u64>(addr));
  419. if (bp != p.end()) {
  420. u64 len = bp->second.len;
  421. // IDA Pro defaults to 4-byte breakpoints for all non-hardware breakpoints
  422. // no matter if it's a 4-byte or 2-byte instruction. When you execute a
  423. // Thumb instruction with a 4-byte breakpoint set, it will set a breakpoint on
  424. // two instructions instead of the single instruction you placed the breakpoint
  425. // on. So, as a way to make sure that execution breakpoints are only breaking
  426. // on the instruction that was specified, set the length of an execution
  427. // breakpoint to 1. This should be fine since the CPU should never begin executing
  428. // an instruction anywhere except the beginning of the instruction.
  429. if (type == BreakpointType::Execute) {
  430. len = 1;
  431. }
  432. if (bp->second.active && (addr >= bp->second.addr && addr < bp->second.addr + len)) {
  433. LOG_DEBUG(Debug_GDBStub,
  434. "Found breakpoint type {} @ {:016X}, range: {:016X}"
  435. " - {:016X} ({:X} bytes)",
  436. static_cast<int>(type), addr, bp->second.addr, bp->second.addr + len, len);
  437. return true;
  438. }
  439. }
  440. return false;
  441. }
  442. /**
  443. * Send packet to gdb client.
  444. *
  445. * @param packet Packet to be sent to client.
  446. */
  447. static void SendPacket(const char packet) {
  448. size_t sent_size = send(gdbserver_socket, &packet, 1, 0);
  449. if (sent_size != 1) {
  450. LOG_ERROR(Debug_GDBStub, "send failed");
  451. }
  452. }
  453. /**
  454. * Send reply to gdb client.
  455. *
  456. * @param reply Reply to be sent to client.
  457. */
  458. static void SendReply(const char* reply) {
  459. if (!IsConnected()) {
  460. return;
  461. }
  462. LOG_DEBUG(Debug_GDBStub, "Reply: {}", reply);
  463. memset(command_buffer, 0, sizeof(command_buffer));
  464. command_length = static_cast<u32>(strlen(reply));
  465. if (command_length + 4 > sizeof(command_buffer)) {
  466. LOG_ERROR(Debug_GDBStub, "command_buffer overflow in SendReply");
  467. return;
  468. }
  469. memcpy(command_buffer + 1, reply, command_length);
  470. u8 checksum = CalculateChecksum(command_buffer, command_length + 1);
  471. command_buffer[0] = GDB_STUB_START;
  472. command_buffer[command_length + 1] = GDB_STUB_END;
  473. command_buffer[command_length + 2] = NibbleToHex(checksum >> 4);
  474. command_buffer[command_length + 3] = NibbleToHex(checksum);
  475. u8* ptr = command_buffer;
  476. u32 left = command_length + 4;
  477. while (left > 0) {
  478. int sent_size = send(gdbserver_socket, reinterpret_cast<char*>(ptr), left, 0);
  479. if (sent_size < 0) {
  480. LOG_ERROR(Debug_GDBStub, "gdb: send failed");
  481. return Shutdown();
  482. }
  483. left -= sent_size;
  484. ptr += sent_size;
  485. }
  486. }
  487. /// Handle query command from gdb client.
  488. static void HandleQuery() {
  489. LOG_DEBUG(Debug_GDBStub, "gdb: query '{}'", command_buffer + 1);
  490. const char* query = reinterpret_cast<const char*>(command_buffer + 1);
  491. if (strcmp(query, "TStatus") == 0) {
  492. SendReply("T0");
  493. } else if (strncmp(query, "Supported", strlen("Supported")) == 0) {
  494. // PacketSize needs to be large enough for target xml
  495. std::string buffer = "PacketSize=2000;qXfer:features:read+;qXfer:threads:read+";
  496. if (!modules.empty()) {
  497. buffer += ";qXfer:libraries:read+";
  498. }
  499. SendReply(buffer.c_str());
  500. } else if (strncmp(query, "Xfer:features:read:target.xml:",
  501. strlen("Xfer:features:read:target.xml:")) == 0) {
  502. SendReply(target_xml);
  503. } else if (strncmp(query, "Offsets", strlen("Offsets")) == 0) {
  504. std::string buffer = fmt::format("TextSeg={:0x}", Memory::PROCESS_IMAGE_VADDR);
  505. SendReply(buffer.c_str());
  506. } else if (strncmp(query, "fThreadInfo", strlen("fThreadInfo")) == 0) {
  507. std::string val = "m";
  508. for (u32 core = 0; core < Core::NUM_CPU_CORES; core++) {
  509. const auto& threads = Core::System::GetInstance().Scheduler(core)->GetThreadList();
  510. for (const auto& thread : threads) {
  511. val += fmt::format("{:x}", thread->GetThreadId());
  512. val += ",";
  513. }
  514. }
  515. val.pop_back();
  516. SendReply(val.c_str());
  517. } else if (strncmp(query, "sThreadInfo", strlen("sThreadInfo")) == 0) {
  518. SendReply("l");
  519. } else if (strncmp(query, "Xfer:threads:read", strlen("Xfer:threads:read")) == 0) {
  520. std::string buffer;
  521. buffer += "l<?xml version=\"1.0\"?>";
  522. buffer += "<threads>";
  523. for (u32 core = 0; core < Core::NUM_CPU_CORES; core++) {
  524. const auto& threads = Core::System::GetInstance().Scheduler(core)->GetThreadList();
  525. for (const auto& thread : threads) {
  526. buffer +=
  527. fmt::format(R"*(<thread id="{:x}" core="{:d}" name="Thread {:x}"></thread>)*",
  528. thread->GetThreadId(), core, thread->GetThreadId());
  529. }
  530. }
  531. buffer += "</threads>";
  532. SendReply(buffer.c_str());
  533. } else if (strncmp(query, "Xfer:libraries:read", strlen("Xfer:libraries:read")) == 0) {
  534. std::string buffer;
  535. buffer += "l<?xml version=\"1.0\"?>";
  536. buffer += "<library-list>";
  537. for (const auto& module : modules) {
  538. buffer +=
  539. fmt::format(R"*("<library name = "{}"><segment address = "0x{:x}"/></library>)*",
  540. module.name, module.beg);
  541. }
  542. buffer += "</library-list>";
  543. SendReply(buffer.c_str());
  544. } else {
  545. SendReply("");
  546. }
  547. }
  548. /// Handle set thread command from gdb client.
  549. static void HandleSetThread() {
  550. int thread_id = -1;
  551. if (command_buffer[2] != '-') {
  552. thread_id = static_cast<int>(HexToInt(command_buffer + 2, command_length - 2));
  553. }
  554. if (thread_id >= 1) {
  555. current_thread = FindThreadById(thread_id);
  556. }
  557. if (!current_thread) {
  558. thread_id = 1;
  559. current_thread = FindThreadById(thread_id);
  560. }
  561. if (current_thread) {
  562. SendReply("OK");
  563. return;
  564. }
  565. SendReply("E01");
  566. }
  567. /// Handle thread alive command from gdb client.
  568. static void HandleThreadAlive() {
  569. int thread_id = static_cast<int>(HexToInt(command_buffer + 1, command_length - 1));
  570. if (thread_id == 0) {
  571. thread_id = 1;
  572. }
  573. if (FindThreadById(thread_id)) {
  574. SendReply("OK");
  575. return;
  576. }
  577. SendReply("E01");
  578. }
  579. /**
  580. * Send signal packet to client.
  581. *
  582. * @param signal Signal to be sent to client.
  583. */
  584. static void SendSignal(Kernel::Thread* thread, u32 signal, bool full = true) {
  585. if (gdbserver_socket == -1) {
  586. return;
  587. }
  588. latest_signal = signal;
  589. if (!thread) {
  590. full = false;
  591. }
  592. std::string buffer;
  593. if (full) {
  594. buffer = fmt::format("T{:02x}{:02x}:{:016x};{:02x}:{:016x};{:02x}:{:016x}", latest_signal,
  595. PC_REGISTER, Common::swap64(RegRead(PC_REGISTER, thread)), SP_REGISTER,
  596. Common::swap64(RegRead(SP_REGISTER, thread)), LR_REGISTER,
  597. Common::swap64(RegRead(LR_REGISTER, thread)));
  598. } else {
  599. buffer = fmt::format("T{:02x}", latest_signal);
  600. }
  601. if (thread) {
  602. buffer += fmt::format(";thread:{:x};", thread->GetThreadId());
  603. }
  604. SendReply(buffer.c_str());
  605. }
  606. /// Read command from gdb client.
  607. static void ReadCommand() {
  608. command_length = 0;
  609. memset(command_buffer, 0, sizeof(command_buffer));
  610. u8 c = ReadByte();
  611. if (c == '+') {
  612. // ignore ack
  613. return;
  614. } else if (c == 0x03) {
  615. LOG_INFO(Debug_GDBStub, "gdb: found break command");
  616. halt_loop = true;
  617. SendSignal(current_thread, SIGTRAP);
  618. return;
  619. } else if (c != GDB_STUB_START) {
  620. LOG_DEBUG(Debug_GDBStub, "gdb: read invalid byte {:02X}", c);
  621. return;
  622. }
  623. while ((c = ReadByte()) != GDB_STUB_END) {
  624. if (command_length >= sizeof(command_buffer)) {
  625. LOG_ERROR(Debug_GDBStub, "gdb: command_buffer overflow");
  626. SendPacket(GDB_STUB_NACK);
  627. return;
  628. }
  629. command_buffer[command_length++] = c;
  630. }
  631. u8 checksum_received = HexCharToValue(ReadByte()) << 4;
  632. checksum_received |= HexCharToValue(ReadByte());
  633. u8 checksum_calculated = CalculateChecksum(command_buffer, command_length);
  634. if (checksum_received != checksum_calculated) {
  635. LOG_ERROR(Debug_GDBStub,
  636. "gdb: invalid checksum: calculated {:02X} and read {:02X} for ${}# (length: {})",
  637. checksum_calculated, checksum_received, command_buffer, command_length);
  638. command_length = 0;
  639. SendPacket(GDB_STUB_NACK);
  640. return;
  641. }
  642. SendPacket(GDB_STUB_ACK);
  643. }
  644. /// Check if there is data to be read from the gdb client.
  645. static bool IsDataAvailable() {
  646. if (!IsConnected()) {
  647. return false;
  648. }
  649. fd_set fd_socket;
  650. FD_ZERO(&fd_socket);
  651. FD_SET(static_cast<u32>(gdbserver_socket), &fd_socket);
  652. struct timeval t;
  653. t.tv_sec = 0;
  654. t.tv_usec = 0;
  655. if (select(gdbserver_socket + 1, &fd_socket, nullptr, nullptr, &t) < 0) {
  656. LOG_ERROR(Debug_GDBStub, "select failed");
  657. return false;
  658. }
  659. return FD_ISSET(gdbserver_socket, &fd_socket) != 0;
  660. }
  661. /// Send requested register to gdb client.
  662. static void ReadRegister() {
  663. static u8 reply[64];
  664. memset(reply, 0, sizeof(reply));
  665. u32 id = HexCharToValue(command_buffer[1]);
  666. if (command_buffer[2] != '\0') {
  667. id <<= 4;
  668. id |= HexCharToValue(command_buffer[2]);
  669. }
  670. if (id <= SP_REGISTER) {
  671. LongToGdbHex(reply, RegRead(id, current_thread));
  672. } else if (id == PC_REGISTER) {
  673. LongToGdbHex(reply, RegRead(id, current_thread));
  674. } else if (id == CPSR_REGISTER) {
  675. IntToGdbHex(reply, (u32)RegRead(id, current_thread));
  676. } else if (id >= UC_ARM64_REG_Q0 && id < FPSCR_REGISTER) {
  677. LongToGdbHex(reply, RegRead(id, current_thread));
  678. } else if (id == FPSCR_REGISTER) {
  679. LongToGdbHex(reply, RegRead(TODO_DUMMY_REG_998, current_thread));
  680. } else {
  681. LongToGdbHex(reply, RegRead(TODO_DUMMY_REG_997, current_thread));
  682. }
  683. SendReply(reinterpret_cast<char*>(reply));
  684. }
  685. /// Send all registers to the gdb client.
  686. static void ReadRegisters() {
  687. static u8 buffer[GDB_BUFFER_SIZE - 4];
  688. memset(buffer, 0, sizeof(buffer));
  689. u8* bufptr = buffer;
  690. for (u32 reg = 0; reg <= SP_REGISTER; reg++) {
  691. LongToGdbHex(bufptr + reg * 16, RegRead(reg, current_thread));
  692. }
  693. bufptr += 32 * 16;
  694. LongToGdbHex(bufptr, RegRead(PC_REGISTER, current_thread));
  695. bufptr += 16;
  696. IntToGdbHex(bufptr, (u32)RegRead(CPSR_REGISTER, current_thread));
  697. bufptr += 8;
  698. for (u32 reg = UC_ARM64_REG_Q0; reg <= UC_ARM64_REG_Q0 + 31; reg++) {
  699. LongToGdbHex(bufptr + reg * 16, RegRead(reg, current_thread));
  700. }
  701. bufptr += 32 * 32;
  702. LongToGdbHex(bufptr, RegRead(TODO_DUMMY_REG_998, current_thread));
  703. bufptr += 8;
  704. SendReply(reinterpret_cast<char*>(buffer));
  705. }
  706. /// Modify data of register specified by gdb client.
  707. static void WriteRegister() {
  708. const u8* buffer_ptr = command_buffer + 3;
  709. u32 id = HexCharToValue(command_buffer[1]);
  710. if (command_buffer[2] != '=') {
  711. ++buffer_ptr;
  712. id <<= 4;
  713. id |= HexCharToValue(command_buffer[2]);
  714. }
  715. if (id <= SP_REGISTER) {
  716. RegWrite(id, GdbHexToLong(buffer_ptr), current_thread);
  717. } else if (id == PC_REGISTER) {
  718. RegWrite(id, GdbHexToLong(buffer_ptr), current_thread);
  719. } else if (id == CPSR_REGISTER) {
  720. RegWrite(id, GdbHexToInt(buffer_ptr), current_thread);
  721. } else if (id >= UC_ARM64_REG_Q0 && id < FPSCR_REGISTER) {
  722. RegWrite(id, GdbHexToLong(buffer_ptr), current_thread);
  723. } else if (id == FPSCR_REGISTER) {
  724. RegWrite(TODO_DUMMY_REG_998, GdbHexToLong(buffer_ptr), current_thread);
  725. } else {
  726. RegWrite(TODO_DUMMY_REG_997, GdbHexToLong(buffer_ptr), current_thread);
  727. }
  728. // Update Unicorn context skipping scheduler, no running threads at this point
  729. Core::System::GetInstance().ArmInterface(current_core).LoadContext(current_thread->context);
  730. SendReply("OK");
  731. }
  732. /// Modify all registers with data received from the client.
  733. static void WriteRegisters() {
  734. const u8* buffer_ptr = command_buffer + 1;
  735. if (command_buffer[0] != 'G')
  736. return SendReply("E01");
  737. for (u32 i = 0, reg = 0; reg <= FPSCR_REGISTER; i++, reg++) {
  738. if (reg <= SP_REGISTER) {
  739. RegWrite(reg, GdbHexToLong(buffer_ptr + i * 16), current_thread);
  740. } else if (reg == PC_REGISTER) {
  741. RegWrite(PC_REGISTER, GdbHexToLong(buffer_ptr + i * 16), current_thread);
  742. } else if (reg == CPSR_REGISTER) {
  743. RegWrite(CPSR_REGISTER, GdbHexToInt(buffer_ptr + i * 16), current_thread);
  744. } else if (reg >= UC_ARM64_REG_Q0 && reg < FPSCR_REGISTER) {
  745. RegWrite(reg, GdbHexToLong(buffer_ptr + i * 16), current_thread);
  746. } else if (reg == FPSCR_REGISTER) {
  747. RegWrite(TODO_DUMMY_REG_998, GdbHexToLong(buffer_ptr + i * 16), current_thread);
  748. } else {
  749. UNIMPLEMENTED();
  750. }
  751. }
  752. // Update Unicorn context skipping scheduler, no running threads at this point
  753. Core::System::GetInstance().ArmInterface(current_core).LoadContext(current_thread->context);
  754. SendReply("OK");
  755. }
  756. /// Read location in memory specified by gdb client.
  757. static void ReadMemory() {
  758. static u8 reply[GDB_BUFFER_SIZE - 4];
  759. auto start_offset = command_buffer + 1;
  760. auto addr_pos = std::find(start_offset, command_buffer + command_length, ',');
  761. VAddr addr = HexToLong(start_offset, static_cast<u64>(addr_pos - start_offset));
  762. start_offset = addr_pos + 1;
  763. u64 len =
  764. HexToLong(start_offset, static_cast<u64>((command_buffer + command_length) - start_offset));
  765. LOG_DEBUG(Debug_GDBStub, "gdb: addr: {:016X} len: {:016X}", addr, len);
  766. if (len * 2 > sizeof(reply)) {
  767. SendReply("E01");
  768. }
  769. if (addr < Memory::PROCESS_IMAGE_VADDR || addr >= Memory::MAP_REGION_VADDR_END) {
  770. return SendReply("E00");
  771. }
  772. if (!Memory::IsValidVirtualAddress(addr)) {
  773. return SendReply("E00");
  774. }
  775. std::vector<u8> data(len);
  776. Memory::ReadBlock(addr, data.data(), len);
  777. MemToGdbHex(reply, data.data(), len);
  778. reply[len * 2] = '\0';
  779. SendReply(reinterpret_cast<char*>(reply));
  780. }
  781. /// Modify location in memory with data received from the gdb client.
  782. static void WriteMemory() {
  783. auto start_offset = command_buffer + 1;
  784. auto addr_pos = std::find(start_offset, command_buffer + command_length, ',');
  785. VAddr addr = HexToLong(start_offset, static_cast<u64>(addr_pos - start_offset));
  786. start_offset = addr_pos + 1;
  787. auto len_pos = std::find(start_offset, command_buffer + command_length, ':');
  788. u64 len = HexToLong(start_offset, static_cast<u64>(len_pos - start_offset));
  789. if (!Memory::IsValidVirtualAddress(addr)) {
  790. return SendReply("E00");
  791. }
  792. std::vector<u8> data(len);
  793. GdbHexToMem(data.data(), len_pos + 1, len);
  794. Memory::WriteBlock(addr, data.data(), len);
  795. SendReply("OK");
  796. }
  797. void Break(bool is_memory_break) {
  798. send_trap = true;
  799. memory_break = is_memory_break;
  800. }
  801. /// Tell the CPU that it should perform a single step.
  802. static void Step() {
  803. if (command_length > 1) {
  804. RegWrite(PC_REGISTER, GdbHexToLong(command_buffer + 1), current_thread);
  805. // Update Unicorn context skipping scheduler, no running threads at this point
  806. Core::System::GetInstance().ArmInterface(current_core).LoadContext(current_thread->context);
  807. }
  808. step_loop = true;
  809. halt_loop = true;
  810. send_trap = true;
  811. }
  812. /// Tell the CPU if we hit a memory breakpoint.
  813. bool IsMemoryBreak() {
  814. if (IsConnected()) {
  815. return false;
  816. }
  817. return memory_break;
  818. }
  819. /// Tell the CPU to continue executing.
  820. static void Continue() {
  821. memory_break = false;
  822. step_loop = false;
  823. halt_loop = false;
  824. }
  825. /**
  826. * Commit breakpoint to list of breakpoints.
  827. *
  828. * @param type Type of breakpoint.
  829. * @param addr Address of breakpoint.
  830. * @param len Length of breakpoint.
  831. */
  832. static bool CommitBreakpoint(BreakpointType type, VAddr addr, u64 len) {
  833. std::map<u64, Breakpoint>& p = GetBreakpointList(type);
  834. Breakpoint breakpoint;
  835. breakpoint.active = true;
  836. breakpoint.addr = addr;
  837. breakpoint.len = len;
  838. p.insert({addr, breakpoint});
  839. LOG_DEBUG(Debug_GDBStub, "gdb: added {} breakpoint: {:016X} bytes at {:016X}",
  840. static_cast<int>(type), breakpoint.len, breakpoint.addr);
  841. return true;
  842. }
  843. /// Handle add breakpoint command from gdb client.
  844. static void AddBreakpoint() {
  845. BreakpointType type;
  846. u8 type_id = HexCharToValue(command_buffer[1]);
  847. switch (type_id) {
  848. case 0:
  849. case 1:
  850. type = BreakpointType::Execute;
  851. break;
  852. case 2:
  853. type = BreakpointType::Write;
  854. break;
  855. case 3:
  856. type = BreakpointType::Read;
  857. break;
  858. case 4:
  859. type = BreakpointType::Access;
  860. break;
  861. default:
  862. return SendReply("E01");
  863. }
  864. auto start_offset = command_buffer + 3;
  865. auto addr_pos = std::find(start_offset, command_buffer + command_length, ',');
  866. VAddr addr = HexToLong(start_offset, static_cast<u64>(addr_pos - start_offset));
  867. start_offset = addr_pos + 1;
  868. u64 len =
  869. HexToLong(start_offset, static_cast<u64>((command_buffer + command_length) - start_offset));
  870. if (type == BreakpointType::Access) {
  871. // Access is made up of Read and Write types, so add both breakpoints
  872. type = BreakpointType::Read;
  873. if (!CommitBreakpoint(type, addr, len)) {
  874. return SendReply("E02");
  875. }
  876. type = BreakpointType::Write;
  877. }
  878. if (!CommitBreakpoint(type, addr, len)) {
  879. return SendReply("E02");
  880. }
  881. SendReply("OK");
  882. }
  883. /// Handle remove breakpoint command from gdb client.
  884. static void RemoveBreakpoint() {
  885. BreakpointType type;
  886. u8 type_id = HexCharToValue(command_buffer[1]);
  887. switch (type_id) {
  888. case 0:
  889. case 1:
  890. type = BreakpointType::Execute;
  891. break;
  892. case 2:
  893. type = BreakpointType::Write;
  894. break;
  895. case 3:
  896. type = BreakpointType::Read;
  897. break;
  898. case 4:
  899. type = BreakpointType::Access;
  900. break;
  901. default:
  902. return SendReply("E01");
  903. }
  904. auto start_offset = command_buffer + 3;
  905. auto addr_pos = std::find(start_offset, command_buffer + command_length, ',');
  906. VAddr addr = HexToLong(start_offset, static_cast<u64>(addr_pos - start_offset));
  907. if (type == BreakpointType::Access) {
  908. // Access is made up of Read and Write types, so add both breakpoints
  909. type = BreakpointType::Read;
  910. RemoveBreakpoint(type, addr);
  911. type = BreakpointType::Write;
  912. }
  913. RemoveBreakpoint(type, addr);
  914. SendReply("OK");
  915. }
  916. void HandlePacket() {
  917. if (!IsConnected()) {
  918. return;
  919. }
  920. if (!IsDataAvailable()) {
  921. return;
  922. }
  923. ReadCommand();
  924. if (command_length == 0) {
  925. return;
  926. }
  927. LOG_DEBUG(Debug_GDBStub, "Packet: {}", command_buffer);
  928. switch (command_buffer[0]) {
  929. case 'q':
  930. HandleQuery();
  931. break;
  932. case 'H':
  933. HandleSetThread();
  934. break;
  935. case '?':
  936. SendSignal(current_thread, latest_signal);
  937. break;
  938. case 'k':
  939. Shutdown();
  940. LOG_INFO(Debug_GDBStub, "killed by gdb");
  941. return;
  942. case 'g':
  943. ReadRegisters();
  944. break;
  945. case 'G':
  946. WriteRegisters();
  947. break;
  948. case 'p':
  949. ReadRegister();
  950. break;
  951. case 'P':
  952. WriteRegister();
  953. break;
  954. case 'm':
  955. ReadMemory();
  956. break;
  957. case 'M':
  958. WriteMemory();
  959. break;
  960. case 's':
  961. Step();
  962. return;
  963. case 'C':
  964. case 'c':
  965. Continue();
  966. return;
  967. case 'z':
  968. RemoveBreakpoint();
  969. break;
  970. case 'Z':
  971. AddBreakpoint();
  972. break;
  973. case 'T':
  974. HandleThreadAlive();
  975. break;
  976. default:
  977. SendReply("");
  978. break;
  979. }
  980. }
  981. void SetServerPort(u16 port) {
  982. gdbstub_port = port;
  983. }
  984. void ToggleServer(bool status) {
  985. if (status) {
  986. server_enabled = status;
  987. // Start server
  988. if (!IsConnected() && Core::System::GetInstance().IsPoweredOn()) {
  989. Init();
  990. }
  991. } else {
  992. // Stop server
  993. if (IsConnected()) {
  994. Shutdown();
  995. }
  996. server_enabled = status;
  997. }
  998. }
  999. static void Init(u16 port) {
  1000. if (!server_enabled) {
  1001. // Set the halt loop to false in case the user enabled the gdbstub mid-execution.
  1002. // This way the CPU can still execute normally.
  1003. halt_loop = false;
  1004. step_loop = false;
  1005. return;
  1006. }
  1007. // Setup initial gdbstub status
  1008. halt_loop = true;
  1009. step_loop = false;
  1010. breakpoints_execute.clear();
  1011. breakpoints_read.clear();
  1012. breakpoints_write.clear();
  1013. modules.clear();
  1014. // Start gdb server
  1015. LOG_INFO(Debug_GDBStub, "Starting GDB server on port {}...", port);
  1016. sockaddr_in saddr_server = {};
  1017. saddr_server.sin_family = AF_INET;
  1018. saddr_server.sin_port = htons(port);
  1019. saddr_server.sin_addr.s_addr = INADDR_ANY;
  1020. #ifdef _WIN32
  1021. WSAStartup(MAKEWORD(2, 2), &InitData);
  1022. #endif
  1023. int tmpsock = static_cast<int>(socket(PF_INET, SOCK_STREAM, 0));
  1024. if (tmpsock == -1) {
  1025. LOG_ERROR(Debug_GDBStub, "Failed to create gdb socket");
  1026. }
  1027. // Set socket to SO_REUSEADDR so it can always bind on the same port
  1028. int reuse_enabled = 1;
  1029. if (setsockopt(tmpsock, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse_enabled,
  1030. sizeof(reuse_enabled)) < 0) {
  1031. LOG_ERROR(Debug_GDBStub, "Failed to set gdb socket option");
  1032. }
  1033. const sockaddr* server_addr = reinterpret_cast<const sockaddr*>(&saddr_server);
  1034. socklen_t server_addrlen = sizeof(saddr_server);
  1035. if (bind(tmpsock, server_addr, server_addrlen) < 0) {
  1036. LOG_ERROR(Debug_GDBStub, "Failed to bind gdb socket");
  1037. }
  1038. if (listen(tmpsock, 1) < 0) {
  1039. LOG_ERROR(Debug_GDBStub, "Failed to listen to gdb socket");
  1040. }
  1041. // Wait for gdb to connect
  1042. LOG_INFO(Debug_GDBStub, "Waiting for gdb to connect...");
  1043. sockaddr_in saddr_client;
  1044. sockaddr* client_addr = reinterpret_cast<sockaddr*>(&saddr_client);
  1045. socklen_t client_addrlen = sizeof(saddr_client);
  1046. gdbserver_socket = static_cast<int>(accept(tmpsock, client_addr, &client_addrlen));
  1047. if (gdbserver_socket < 0) {
  1048. // In the case that we couldn't start the server for whatever reason, just start CPU
  1049. // execution like normal.
  1050. halt_loop = false;
  1051. step_loop = false;
  1052. LOG_ERROR(Debug_GDBStub, "Failed to accept gdb client");
  1053. } else {
  1054. LOG_INFO(Debug_GDBStub, "Client connected.");
  1055. saddr_client.sin_addr.s_addr = ntohl(saddr_client.sin_addr.s_addr);
  1056. }
  1057. // Clean up temporary socket if it's still alive at this point.
  1058. if (tmpsock != -1) {
  1059. shutdown(tmpsock, SHUT_RDWR);
  1060. }
  1061. }
  1062. void Init() {
  1063. Init(gdbstub_port);
  1064. }
  1065. void Shutdown() {
  1066. if (!server_enabled) {
  1067. return;
  1068. }
  1069. LOG_INFO(Debug_GDBStub, "Stopping GDB ...");
  1070. if (gdbserver_socket != -1) {
  1071. shutdown(gdbserver_socket, SHUT_RDWR);
  1072. gdbserver_socket = -1;
  1073. }
  1074. #ifdef _WIN32
  1075. WSACleanup();
  1076. #endif
  1077. LOG_INFO(Debug_GDBStub, "GDB stopped.");
  1078. }
  1079. bool IsServerEnabled() {
  1080. return server_enabled;
  1081. }
  1082. bool IsConnected() {
  1083. return IsServerEnabled() && gdbserver_socket != -1;
  1084. }
  1085. bool GetCpuHaltFlag() {
  1086. return halt_loop;
  1087. }
  1088. bool GetCpuStepFlag() {
  1089. return step_loop;
  1090. }
  1091. void SetCpuStepFlag(bool is_step) {
  1092. step_loop = is_step;
  1093. }
  1094. void SendTrap(Kernel::Thread* thread, int trap) {
  1095. if (send_trap) {
  1096. if (!halt_loop || current_thread == thread) {
  1097. current_thread = thread;
  1098. SendSignal(thread, trap);
  1099. }
  1100. halt_loop = true;
  1101. send_trap = false;
  1102. }
  1103. }
  1104. }; // namespace GDBStub