1 // license:BSD-3-Clause
2 // copyright-holders:Ramiro Polla
3 //============================================================
4 //
5 //  debuggdbstub.cpp - GDB stub debugger
6 //
7 //============================================================
8 
9 #include "emu.h"
10 #include "debug/debugcon.h"
11 #include "debug/debugcpu.h"
12 #include "debug/points.h"
13 #include "debug/textbuf.h"
14 #include "debug_module.h"
15 #include "debugger.h"
16 #include "modules/lib/osdobj_common.h"
17 #include "modules/osdmodule.h"
18 
19 #include <cinttypes>
20 
21 //-------------------------------------------------------------------------
22 #define MAX_PACKET_SIZE 16384
23 
24 //-------------------------------------------------------------------------
25 enum gdb_register_type
26 {
27 	TYPE_INT,
28 	TYPE_CODE_POINTER,
29 	TYPE_DATA_POINTER,
30 	TYPE_I387_EXT,
31 };
32 static const char *const gdb_register_type_str[] = {
33 	"int",
34 	"code_ptr",
35 	"data_ptr",
36 	"i387_ext",
37 };
38 struct gdb_register_map
39 {
40 	const char *arch;
41 	const char *feature;
42 	struct gdb_register_description
43 	{
44 		const char *state_name;
45 		const char *gdb_name;
46 		bool stop_packet;
47 		gdb_register_type gdb_type;
48 		int override_bitsize;
49 
gdb_register_descriptiongdb_register_map::gdb_register_description50 		gdb_register_description(const char *_state_name=nullptr, const char *_gdb_name=nullptr, bool _stop_packet=false, gdb_register_type _gdb_type=TYPE_INT, int _override_bitsize=-1)
51 		: state_name(_state_name)
52 		, gdb_name(_gdb_name)
53 		, stop_packet(_stop_packet)
54 		, gdb_type(_gdb_type)
55 		, override_bitsize(_override_bitsize)
56 		{
57 		}
58 	};
59 	std::vector<gdb_register_description> registers;
60 };
61 
62 //-------------------------------------------------------------------------
63 static const gdb_register_map gdb_register_map_i486 =
64 {
65 	"i386",
66 	"org.gnu.gdb.i386.core",
67 	{
68 		{ "EAX",     "eax",    false, TYPE_INT },
69 		{ "ECX",     "ecx",    false, TYPE_INT },
70 		{ "EDX",     "edx",    false, TYPE_INT },
71 		{ "EBX",     "ebx",    false, TYPE_INT },
72 		{ "ESP",     "esp",    true,  TYPE_DATA_POINTER },
73 		{ "EBP",     "ebp",    true,  TYPE_DATA_POINTER },
74 		{ "ESI",     "esi",    false, TYPE_INT },
75 		{ "EDI",     "edi",    false, TYPE_INT },
76 		{ "EIP",     "eip",    true,  TYPE_CODE_POINTER },
77 		{ "EFLAGS",  "eflags", false, TYPE_INT }, // TODO describe bitfield
78 		{ "CS",      "cs",     false, TYPE_INT },
79 		{ "SS",      "ss",     false, TYPE_INT },
80 		{ "DS",      "ds",     false, TYPE_INT },
81 		{ "ES",      "es",     false, TYPE_INT },
82 		{ "FS",      "fs",     false, TYPE_INT },
83 		{ "GS",      "gs",     false, TYPE_INT },
84 		// TODO fix x87 registers!
85 		// The x87 registers are just plain wrong for a few reasons:
86 		//  - The st* registers use a dummy variable in i386_device, so we
87 		//    don't retrieve the real value (also the bitsize is wrong);
88 		//  - The seg/off/op registers don't seem to be exported in the
89 		//    state.
90 		{ "ST0",     "st0",    false, TYPE_I387_EXT },
91 		{ "ST1",     "st1",    false, TYPE_I387_EXT },
92 		{ "ST2",     "st2",    false, TYPE_I387_EXT },
93 		{ "ST3",     "st3",    false, TYPE_I387_EXT },
94 		{ "ST4",     "st4",    false, TYPE_I387_EXT },
95 		{ "ST5",     "st5",    false, TYPE_I387_EXT },
96 		{ "ST6",     "st6",    false, TYPE_I387_EXT },
97 		{ "ST7",     "st7",    false, TYPE_I387_EXT },
98 		{ "x87_CW",  "fctrl",  false, TYPE_INT },
99 		{ "x87_SW",  "fstat",  false, TYPE_INT },
100 		{ "x87_TAG", "ftag",   false, TYPE_INT },
101 		{ "EAX",     "fiseg",  false, TYPE_INT },
102 		{ "EAX",     "fioff",  false, TYPE_INT },
103 		{ "EAX",     "foseg",  false, TYPE_INT },
104 		{ "EAX",     "fooff",  false, TYPE_INT },
105 		{ "EAX",     "fop",    false, TYPE_INT },
106 	}
107 };
108 
109 //-------------------------------------------------------------------------
110 static const gdb_register_map gdb_register_map_arm7 =
111 {
112 	"arm",
113 	"org.gnu.gdb.arm.core",
114 	{
115 		{ "R0",   "r0",   false, TYPE_INT },
116 		{ "R1",   "r1",   false, TYPE_INT },
117 		{ "R2",   "r2",   false, TYPE_INT },
118 		{ "R3",   "r3",   false, TYPE_INT },
119 		{ "R4",   "r4",   false, TYPE_INT },
120 		{ "R5",   "r5",   false, TYPE_INT },
121 		{ "R6",   "r6",   false, TYPE_INT },
122 		{ "R7",   "r7",   false, TYPE_INT },
123 		{ "R8",   "r8",   false, TYPE_INT },
124 		{ "R9",   "r9",   false, TYPE_INT },
125 		{ "R10",  "r10",  false, TYPE_INT },
126 		{ "R11",  "r11",  false, TYPE_INT },
127 		{ "R12",  "r12",  false, TYPE_INT },
128 		{ "R13",  "sp",   true,  TYPE_DATA_POINTER },
129 		{ "R14",  "lr",   true,  TYPE_INT },
130 		{ "R15",  "pc",   true,  TYPE_CODE_POINTER },
131 		{ "CPSR", "cpsr", false, TYPE_INT }, // TODO describe bitfield
132 	}
133 };
134 
135 //-------------------------------------------------------------------------
136 static const gdb_register_map gdb_register_map_ppc601 =
137 {
138 	"powerpc:common",
139 	"org.gnu.gdb.power.core",
140 	{
141 		{ "R0",   "r0",   false, TYPE_INT },
142 		{ "R1",   "r1",   false, TYPE_INT },
143 		{ "R2",   "r2",   false, TYPE_INT },
144 		{ "R3",   "r3",   false, TYPE_INT },
145 		{ "R4",   "r4",   false, TYPE_INT },
146 		{ "R5",   "r5",   false, TYPE_INT },
147 		{ "R6",   "r6",   false, TYPE_INT },
148 		{ "R7",   "r7",   false, TYPE_INT },
149 		{ "R8",   "r8",   false, TYPE_INT },
150 		{ "R9",   "r9",   false, TYPE_INT },
151 		{ "R10",  "r10",  false, TYPE_INT },
152 		{ "R11",  "r11",  false, TYPE_INT },
153 		{ "R12",  "r12",  false, TYPE_INT },
154 		{ "R13",  "r13",  false, TYPE_INT },
155 		{ "R14",  "r14",  false, TYPE_INT },
156 		{ "R15",  "r15",  false, TYPE_INT },
157 		{ "R16",  "r16",  false, TYPE_INT },
158 		{ "R17",  "r17",  false, TYPE_INT },
159 		{ "R18",  "r18",  false, TYPE_INT },
160 		{ "R19",  "r19",  false, TYPE_INT },
161 		{ "R20",  "r20",  false, TYPE_INT },
162 		{ "R21",  "r21",  false, TYPE_INT },
163 		{ "R22",  "r22",  false, TYPE_INT },
164 		{ "R23",  "r23",  false, TYPE_INT },
165 		{ "R24",  "r24",  false, TYPE_INT },
166 		{ "R25",  "r25",  false, TYPE_INT },
167 		{ "R26",  "r26",  false, TYPE_INT },
168 		{ "R27",  "r27",  false, TYPE_INT },
169 		{ "R28",  "r28",  false, TYPE_INT },
170 		{ "R29",  "r29",  false, TYPE_INT },
171 		{ "R30",  "r30",  false, TYPE_INT },
172 		{ "R31",  "r31",  false, TYPE_INT },
173 		{ "PC",   "pc",   true,  TYPE_CODE_POINTER },
174 		{ "MSR",  "msr",  false, TYPE_INT },
175 		{ "CR",   "cr",   false, TYPE_INT },
176 		{ "LR",   "lr",   true,  TYPE_CODE_POINTER },
177 		{ "CTR",  "ctr",  false, TYPE_INT },
178 		{ "XER",  "xer",  false, TYPE_INT },
179 	}
180 };
181 
182 //-------------------------------------------------------------------------
183 static const gdb_register_map gdb_register_map_r4600 =
184 {
185 	"mips",
186 	"org.gnu.gdb.mips.cpu",
187 	{
188 		{ "zero", "r0",   false, TYPE_INT, 32 },
189 		{ "at",   "r1",   false, TYPE_INT, 32 },
190 		{ "v0",   "r2",   false, TYPE_INT, 32 },
191 		{ "v1",   "r3",   false, TYPE_INT, 32 },
192 		{ "a0",   "r4",   false, TYPE_INT, 32 },
193 		{ "a1",   "r5",   false, TYPE_INT, 32 },
194 		{ "a2",   "r6",   false, TYPE_INT, 32 },
195 		{ "a3",   "r7",   false, TYPE_INT, 32 },
196 		{ "t0",   "r8",   false, TYPE_INT, 32 },
197 		{ "t1",   "r9",   false, TYPE_INT, 32 },
198 		{ "t2",   "r10",  false, TYPE_INT, 32 },
199 		{ "t3",   "r11",  false, TYPE_INT, 32 },
200 		{ "t4",   "r12",  false, TYPE_INT, 32 },
201 		{ "t5",   "r13",  false, TYPE_INT, 32 },
202 		{ "t6",   "r14",  false, TYPE_INT, 32 },
203 		{ "t7",   "r15",  false, TYPE_INT, 32 },
204 		{ "s0",   "r16",  false, TYPE_INT, 32 },
205 		{ "s1",   "r17",  false, TYPE_INT, 32 },
206 		{ "s2",   "r18",  false, TYPE_INT, 32 },
207 		{ "s3",   "r19",  false, TYPE_INT, 32 },
208 		{ "s4",   "r20",  false, TYPE_INT, 32 },
209 		{ "s5",   "r21",  false, TYPE_INT, 32 },
210 		{ "s6",   "r22",  false, TYPE_INT, 32 },
211 		{ "s7",   "r23",  false, TYPE_INT, 32 },
212 		{ "t8",   "r24",  false, TYPE_INT, 32 },
213 		{ "t9",   "r25",  false, TYPE_INT, 32 },
214 		{ "k0",   "r26",  false, TYPE_INT, 32 },
215 		{ "k1",   "r27",  false, TYPE_INT, 32 },
216 		{ "gp",   "r28",  false, TYPE_INT, 32 },
217 		{ "sp",   "r29",  false, TYPE_INT, 32 },
218 		{ "fp",   "r30",  false, TYPE_INT, 32 },
219 		{ "ra",   "r31",  false, TYPE_INT, 32 },
220 		{ "LO",   "lo",   false, TYPE_INT, 32 },
221 		{ "HI",   "hi",   false, TYPE_INT, 32 },
222 		{ "PC",   "pc",   true,  TYPE_CODE_POINTER, 32 },
223 	}
224 };
225 
226 //-------------------------------------------------------------------------
227 static const gdb_register_map gdb_register_map_m68020pmmu =
228 {
229 	"m68k",
230 	"org.gnu.gdb.m68k.core",
231 	{
232 		{ "D0", "d0", false, TYPE_INT },
233 		{ "D1", "d1", false, TYPE_INT },
234 		{ "D2", "d2", false, TYPE_INT },
235 		{ "D3", "d3", false, TYPE_INT },
236 		{ "D4", "d4", false, TYPE_INT },
237 		{ "D5", "d5", false, TYPE_INT },
238 		{ "D6", "d6", false, TYPE_INT },
239 		{ "D7", "d7", false, TYPE_INT },
240 		{ "A0", "a0", false, TYPE_INT },
241 		{ "A1", "a1", false, TYPE_INT },
242 		{ "A2", "a2", false, TYPE_INT },
243 		{ "A3", "a3", false, TYPE_INT },
244 		{ "A4", "a4", false, TYPE_INT },
245 		{ "A5", "a5", false, TYPE_INT },
246 		{ "A6", "fp", true,  TYPE_INT },
247 		{ "A7", "sp", true,  TYPE_INT },
248 		{ "SR", "ps", false, TYPE_INT }, // NOTE GDB named it ps, but it's actually sr
249 		{ "PC", "pc", true,  TYPE_CODE_POINTER },
250 	}
251 };
252 
253 //-------------------------------------------------------------------------
254 static const gdb_register_map gdb_register_map_z80 =
255 {
256 	"z80",
257 	"mame.z80",
258 	{
259 		{ "AF",  "af",  false, TYPE_INT },
260 		{ "BC",  "bc",  false, TYPE_INT },
261 		{ "DE",  "de",  false, TYPE_INT },
262 		{ "HL",  "hl",  false, TYPE_INT },
263 		{ "AF2", "af'", false, TYPE_INT },
264 		{ "BC2", "bc'", false, TYPE_INT },
265 		{ "DE2", "de'", false, TYPE_INT },
266 		{ "HL2", "hl'", false, TYPE_INT },
267 		{ "IX",  "ix",  false, TYPE_INT },
268 		{ "IY",  "iy",  false, TYPE_INT },
269 		{ "SP",  "sp",  true,  TYPE_DATA_POINTER },
270 		{ "PC",  "pc",  true,  TYPE_CODE_POINTER },
271 	}
272 };
273 
274 //-------------------------------------------------------------------------
275 static const gdb_register_map gdb_register_map_m6502 =
276 {
277 	"m6502",
278 	"mame.m6502",
279 	{
280 		{ "A",  "a",   false, TYPE_INT },
281 		{ "X",  "x",   false, TYPE_INT },
282 		{ "Y",  "y",   false, TYPE_INT },
283 		{ "P",  "p",   false, TYPE_INT },
284 		{ "PC", "pc",  true,  TYPE_CODE_POINTER },
285 		{ "SP", "sp",  true,  TYPE_DATA_POINTER },
286 	}
287 };
288 
289 //-------------------------------------------------------------------------
290 static const std::map<std::string, const gdb_register_map &> gdb_register_maps = {
291 	{ "i486",       gdb_register_map_i486 },
292 	{ "arm7_le",    gdb_register_map_arm7 },
293 	{ "r4600",      gdb_register_map_r4600 },
294 	{ "ppc601",     gdb_register_map_ppc601 },
295 	{ "m68020pmmu", gdb_register_map_m68020pmmu },
296 	{ "z80",        gdb_register_map_z80 },
297 	{ "m6502",      gdb_register_map_m6502 },
298 };
299 
300 //-------------------------------------------------------------------------
301 class debug_gdbstub : public osd_module, public debug_module
302 {
303 public:
debug_gdbstub()304 	debug_gdbstub()
305 	: osd_module(OSD_DEBUG_PROVIDER, "gdbstub"), debug_module(),
306 		m_machine(nullptr),
307 		m_maincpu(nullptr),
308 		m_state(nullptr),
309 		m_memory(nullptr),
310 		m_address_space(nullptr),
311 		m_debugger_cpu(nullptr),
312 		m_debugger_console(nullptr),
313 		m_debugger_port(0),
314 		m_socket(OPEN_FLAG_WRITE | OPEN_FLAG_CREATE),
315 		m_is_be(false),
316 		m_initialized(false),
317 		m_dettached(false),
318 		m_extended_mode(false),
319 		m_send_stop_packet(false),
320 		m_target_xml_sent(false),
321 		m_triggered_breakpoint(nullptr),
322 		m_triggered_watchpoint(nullptr),
323 		m_readbuf_len(0),
324 		m_readbuf_offset(0),
325 		m_packet_len(0),
326 		m_packet_checksum(0),
327 		m_recv_checksum(0)
328 	{
329 	}
330 
~debug_gdbstub()331 	virtual ~debug_gdbstub() { }
332 
333 	virtual int init(const osd_options &options) override;
334 	virtual void exit() override;
335 
336 	virtual void init_debugger(running_machine &machine) override;
337 	virtual void wait_for_debugger(device_t &device, bool firststop) override;
338 	virtual void debugger_update() override;
339 
340 	std::string get_register_string(int gdb_regnum);
341 	bool parse_register_string(uint64_t *pvalue, const char *buf, int gdb_regnum);
342 
343 	bool parse_zZ(int *ptype, uint64_t *paddress, int *pkind, const char *buf);
344 
345 	void set_register_value(int gdb_regnum, uint64_t value);
346 
347 	bool is_thread_id_ok(const char *buf);
348 
349 	void handle_character(char ch);
350 	void send_nack();
351 	void send_ack();
352 	void handle_packet();
353 
354 	enum cmd_reply
355 	{
356 		REPLY_NONE,
357 		REPLY_OK,
358 		REPLY_ENN,
359 		REPLY_UNSUPPORTED,
360 	};
361 
362 	cmd_reply handle_exclamation(const char *buf);
363 	cmd_reply handle_question(const char *buf);
364 	cmd_reply handle_c(const char *buf);
365 	cmd_reply handle_D(const char *buf);
366 	cmd_reply handle_g(const char *buf);
367 	cmd_reply handle_G(const char *buf);
368 	cmd_reply handle_H(const char *buf);
369 	cmd_reply handle_i(const char *buf);
370 	cmd_reply handle_I(const char *buf);
371 	cmd_reply handle_k(const char *buf);
372 	cmd_reply handle_m(const char *buf);
373 	cmd_reply handle_M(const char *buf);
374 	cmd_reply handle_p(const char *buf);
375 	cmd_reply handle_P(const char *buf);
376 	cmd_reply handle_q(const char *buf);
377 	cmd_reply handle_s(const char *buf);
378 	cmd_reply handle_z(const char *buf);
379 	cmd_reply handle_Z(const char *buf);
380 
381 	enum readbuf_state
382 	{
383 		PACKET_START,
384 		PACKET_DATA,
385 		PACKET_CHECKSUM1,
386 		PACKET_CHECKSUM2,
387 	};
388 
389 	readbuf_state m_readbuf_state;
390 
391 	void generate_target_xml();
392 
393 	int readchar();
394 
395 	void send_reply(const char *str);
396 	void send_stop_packet();
397 
398 private:
399 	running_machine *m_machine;
400 	device_t *m_maincpu;
401 	device_state_interface *m_state;
402 	device_memory_interface *m_memory;
403 	address_space *m_address_space;
404 	debugger_cpu *m_debugger_cpu;
405 	debugger_console *m_debugger_console;
406 	int m_debugger_port;
407 	emu_file m_socket;
408 	bool m_is_be;
409 	bool m_initialized;
410 	bool m_dettached;
411 	bool m_extended_mode;
412 	bool m_send_stop_packet;
413 	bool m_target_xml_sent;     // the 'g', 'G', 'p', and 'P' commands only work once target.xml has been sent
414 
415 	struct gdb_register
416 	{
417 		std::string gdb_name;
418 		int gdb_regnum;
419 		gdb_register_type gdb_type;
420 		int gdb_bitsize;
421 		int state_index;
422 	};
423 	std::vector<gdb_register> m_gdb_registers;
424 	std::set<int> m_stop_reply_registers;
425 	std::string m_gdb_arch;
426 	std::string m_gdb_feature;
427 
428 	std::map<offs_t, uint64_t> m_address_map;
429 
430 	debug_breakpoint *m_triggered_breakpoint;
431 	debug_watchpoint *m_triggered_watchpoint;
432 
433 	std::string m_target_xml;
434 
435 	uint8_t  m_readbuf[512];
436 	uint32_t m_readbuf_len;
437 	uint32_t m_readbuf_offset;
438 
439 	uint8_t m_packet_buf[MAX_PACKET_SIZE+1];
440 	int     m_packet_len;
441 	uint8_t m_packet_checksum;
442 	uint8_t m_recv_checksum;
443 };
444 
445 //-------------------------------------------------------------------------
init(const osd_options & options)446 int debug_gdbstub::init(const osd_options &options)
447 {
448 	m_debugger_port = options.debugger_port();
449 	return 0;
450 }
451 
452 //-------------------------------------------------------------------------
exit()453 void debug_gdbstub::exit()
454 {
455 }
456 
457 //-------------------------------------------------------------------------
init_debugger(running_machine & machine)458 void debug_gdbstub::init_debugger(running_machine &machine)
459 {
460 	m_machine = &machine;
461 }
462 
463 //-------------------------------------------------------------------------
readchar()464 int debug_gdbstub::readchar()
465 {
466 	// NOTE: we don't use m_socket.getc() because it does not work with
467 	//       sockets (it assumes seeking is possible).
468 
469 	if ( !m_socket.is_open() )
470 		return -1;
471 
472 	if ( m_readbuf_offset == m_readbuf_len )
473 	{
474 		m_readbuf_offset = 0;
475 		m_readbuf_len = m_socket.read(m_readbuf, sizeof(m_readbuf));
476 		if ( m_readbuf_len == 0 )
477 			return -1;
478 	}
479 
480 	return (int) m_readbuf[m_readbuf_offset++];
481 }
482 
483 //-------------------------------------------------------------------------
escape_packet(const std::string src)484 static std::string escape_packet(const std::string src)
485 {
486 	std::string result;
487 	result.reserve(src.length());
488 	for ( char ch: src )
489 	{
490 		if ( ch == '#' || ch == '$' || ch == '}' )
491 		{
492 			result += '}';
493 			ch ^= 0x20;
494 		}
495 		result += ch;
496 	}
497 	return result;
498 }
499 
500 //-------------------------------------------------------------------------
generate_target_xml()501 void debug_gdbstub::generate_target_xml()
502 {
503 	// Note: we do not attempt to replicate the regnum values from old
504 	//       GDB clients that did not support target.xml.
505 	std::string target_xml;
506 	target_xml += "<?xml version=\"1.0\"?>\n";
507 	target_xml += "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n";
508 	target_xml += "<target version=\"1.0\">\n";
509 	target_xml += string_format("<architecture>%s</architecture>\n", m_gdb_arch.c_str());
510 	target_xml += string_format("  <feature name=\"%s\">\n", m_gdb_feature.c_str());
511 	for ( const auto &reg: m_gdb_registers )
512 		target_xml += string_format("    <reg name=\"%s\" bitsize=\"%d\" type=\"%s\"/>\n", reg.gdb_name.c_str(), reg.gdb_bitsize, gdb_register_type_str[reg.gdb_type]);
513 	target_xml += "  </feature>\n";
514 	target_xml += "</target>\n";
515 	m_target_xml = escape_packet(target_xml);
516 }
517 
518 //-------------------------------------------------------------------------
wait_for_debugger(device_t & device,bool firststop)519 void debug_gdbstub::wait_for_debugger(device_t &device, bool firststop)
520 {
521 	if ( m_dettached )
522 		return;
523 
524 	if ( firststop && !m_initialized )
525 	{
526 		m_maincpu = m_machine->root_device().subdevice(":maincpu");
527 		const char *cpuname = m_maincpu->shortname();
528 		auto it = gdb_register_maps.find(cpuname);
529 		if ( it == gdb_register_maps.end() )
530 			fatalerror("gdbstub: cpuname %s not found in gdb stub descriptions\n", cpuname);
531 
532 		m_state = &m_maincpu->state();
533 		m_memory = &m_maincpu->memory();
534 		m_address_space = &m_memory->space(AS_PROGRAM);
535 		m_debugger_cpu = &m_machine->debugger().cpu();
536 		m_debugger_console = &m_machine->debugger().console();
537 
538 		m_is_be = m_address_space->endianness() == ENDIANNESS_BIG;
539 
540 #if 0
541 		for ( const auto &entry: m_state->state_entries() )
542 		{
543 			const char *symbol = entry->symbol();
544 			uint8_t datasize = entry->datasize();
545 			uint64_t datamask = entry->datamask();
546 			int index = entry->index();
547 			const std::string &format_string = entry->format_string();
548 			osd_printf_info("[%3d] datasize %d mask %016" PRIx64 " [%s] [%s]\n", index, datasize, datamask, symbol, format_string);
549 		}
550 #endif
551 
552 		const gdb_register_map &register_map = it->second;
553 		m_gdb_arch = register_map.arch;
554 		m_gdb_feature = register_map.feature;
555 		int cur_gdb_regnum = 0;
556 		for ( const auto &reg: register_map.registers )
557 		{
558 			bool added = false;
559 			for ( const auto &entry: m_state->state_entries() )
560 			{
561 				const char *symbol = entry->symbol();
562 				if ( strcmp(symbol, reg.state_name) == 0 )
563 				{
564 					gdb_register new_reg;
565 					new_reg.gdb_name = reg.gdb_name;
566 					new_reg.gdb_regnum = cur_gdb_regnum;
567 					new_reg.gdb_type = reg.gdb_type;
568 					if ( reg.override_bitsize != -1 )
569 						new_reg.gdb_bitsize = reg.override_bitsize;
570 					else
571 						new_reg.gdb_bitsize = entry->datasize() * 8;
572 					new_reg.state_index = entry->index();
573 					m_gdb_registers.push_back(std::move(new_reg));
574 					if ( reg.stop_packet )
575 						m_stop_reply_registers.insert(cur_gdb_regnum);
576 					added = true;
577 					cur_gdb_regnum++;
578 					break;
579 				}
580 			}
581 			if ( !added )
582 				osd_printf_info("gdbstub: could not find register [%s]\n", reg.gdb_name);
583 		}
584 
585 #if 0
586 		for ( const auto &reg: m_gdb_registers )
587 			osd_printf_info(" %3d (%d) %d %d [%s]\n", reg.gdb_regnum, reg.state_index, reg.gdb_bitsize, reg.gdb_type, reg.gdb_name);
588 #endif
589 
590 		std::string socket_name = string_format("socket.localhost:%d", m_debugger_port);
591 		osd_file::error filerr = m_socket.open(socket_name);
592 		if ( filerr != osd_file::error::NONE )
593 			fatalerror("gdbstub: failed to start listening on port %d\n", m_debugger_port);
594 		osd_printf_info("gdbstub: listening on port %d\n", m_debugger_port);
595 
596 		m_initialized = true;
597 	}
598 	else
599 	{
600 		device_debug *debug = m_debugger_console->get_visible_cpu()->debug();
601 		m_triggered_watchpoint = debug->triggered_watchpoint();
602 		m_triggered_breakpoint = debug->triggered_breakpoint();
603 		if ( m_send_stop_packet )
604 		{
605 			send_stop_packet();
606 			m_send_stop_packet = false;
607 		}
608 	}
609 
610 	while ( m_debugger_cpu->is_stopped() )
611 	{
612 		int ch = readchar();
613 		if ( ch < 0 )
614 		{
615 			// TODO add support for timeout in *_osd_socket.
616 			// To prevent 100% cpu usage while waiting for data to
617 			// arrive from the socket, we sleep for 1 millisecond.
618 			osd_sleep(osd_ticks_per_second() / 1000);
619 			continue;
620 		}
621 		handle_character((char) ch);
622 	}
623 }
624 
625 //-------------------------------------------------------------------------
debugger_update()626 void debug_gdbstub::debugger_update()
627 {
628 	while ( true )
629 	{
630 		int ch = readchar();
631 		if ( ch < 0 )
632 			break;
633 		handle_character((char) ch);
634 	}
635 }
636 
637 //-------------------------------------------------------------------------
send_nack()638 void debug_gdbstub::send_nack()
639 {
640 	m_socket.puts("-");
641 }
642 
643 //-------------------------------------------------------------------------
send_ack()644 void debug_gdbstub::send_ack()
645 {
646 	m_socket.puts("+");
647 }
648 
649 //-------------------------------------------------------------------------
send_reply(const char * str)650 void debug_gdbstub::send_reply(const char *str)
651 {
652 	size_t length = strlen(str);
653 
654 	uint8_t checksum = 0;
655 	for ( size_t i = 0; i < length; i++ )
656 		checksum += str[i];
657 
658 	std::string reply = string_format("$%s#%02x", str, checksum);
659 	m_socket.puts(reply.c_str());
660 }
661 
662 
663 //-------------------------------------------------------------------------
664 // Enable extended mode.
handle_exclamation(const char * buf)665 debug_gdbstub::cmd_reply debug_gdbstub::handle_exclamation(const char *buf)
666 {
667 	m_extended_mode = true;
668 	return REPLY_OK;
669 }
670 
671 //-------------------------------------------------------------------------
672 // Indicate the reason the target halted.
handle_question(const char * buf)673 debug_gdbstub::cmd_reply debug_gdbstub::handle_question(const char *buf)
674 {
675 	send_stop_packet();
676 	return REPLY_NONE;
677 }
678 
679 //-------------------------------------------------------------------------
680 // Continue at addr.
handle_c(const char * buf)681 debug_gdbstub::cmd_reply debug_gdbstub::handle_c(const char *buf)
682 {
683 	// We don't support continuing with addr.
684 	if ( *buf != '\0' )
685 		return REPLY_UNSUPPORTED;
686 
687 	m_debugger_console->get_visible_cpu()->debug()->go();
688 	m_send_stop_packet = true;
689 	return REPLY_NONE;
690 }
691 
692 //-------------------------------------------------------------------------
693 // Detach.
handle_D(const char * buf)694 debug_gdbstub::cmd_reply debug_gdbstub::handle_D(const char *buf)
695 {
696 	// We don't support dettaching with pid.
697 	if ( *buf != '\0' )
698 		return REPLY_UNSUPPORTED;
699 
700 	m_debugger_console->get_visible_cpu()->debug()->go();
701 	m_dettached = true;
702 
703 	return REPLY_OK;
704 }
705 
706 //-------------------------------------------------------------------------
707 // Read general registers.
handle_g(const char * buf)708 debug_gdbstub::cmd_reply debug_gdbstub::handle_g(const char *buf)
709 {
710 	if ( !m_target_xml_sent )
711 		return REPLY_ENN;
712 	if ( *buf != '\0' )
713 		return REPLY_UNSUPPORTED;
714 	std::string reply;
715 	for ( const auto &reg: m_gdb_registers )
716 		reply += get_register_string(reg.gdb_regnum);
717 	send_reply(reply.c_str());
718 	return REPLY_NONE;
719 }
720 
721 //-------------------------------------------------------------------------
722 // Write general registers.
handle_G(const char * buf)723 debug_gdbstub::cmd_reply debug_gdbstub::handle_G(const char *buf)
724 {
725 	if ( !m_target_xml_sent )
726 		return REPLY_ENN;
727 	for ( const auto &reg: m_gdb_registers )
728 	{
729 		uint64_t value;
730 		if ( !parse_register_string(&value, buf, reg.gdb_regnum) )
731 			return REPLY_ENN;
732 		set_register_value(reg.gdb_regnum, value);
733 		buf += (reg.gdb_bitsize / 8) * 2;
734 	}
735 	if ( *buf != '\0' )
736 		return REPLY_ENN;
737 	return REPLY_OK;
738 }
739 
740 //-------------------------------------------------------------------------
741 // Set thread for subsequent operations.
handle_H(const char * buf)742 debug_gdbstub::cmd_reply debug_gdbstub::handle_H(const char *buf)
743 {
744 	// accept threads 'any', 1, and 'all'
745 	if ( (buf[0] == 'c' || buf[0] == 'g') && is_thread_id_ok(buf + 1) )
746 		return REPLY_OK;
747 	// otherwise silently ignore
748 	return REPLY_UNSUPPORTED;
749 }
750 
751 //-------------------------------------------------------------------------
752 // Kill request.
handle_k(const char * buf)753 debug_gdbstub::cmd_reply debug_gdbstub::handle_k(const char *buf)
754 {
755 	m_machine->schedule_exit();
756 	m_debugger_console->get_visible_cpu()->debug()->go();
757 	m_dettached = true;
758 	m_socket.close();
759 	return REPLY_NONE;
760 }
761 
762 //-------------------------------------------------------------------------
763 // Read memory.
handle_m(const char * buf)764 debug_gdbstub::cmd_reply debug_gdbstub::handle_m(const char *buf)
765 {
766 	uint64_t address;
767 	uint64_t length;
768 	if ( sscanf(buf, "%" PRIx64 ",%" PRIx64, &address, &length) != 2 )
769 		return REPLY_ENN;
770 
771 	offs_t offset = address;
772 	if ( !m_memory->translate(m_address_space->spacenum(), TRANSLATE_READ_DEBUG, offset) )
773 		return REPLY_ENN;
774 
775 	// Disable side effects while reading memory.
776 	auto dis = m_machine->disable_side_effects();
777 
778 	std::string reply;
779 	reply.reserve(length * 2);
780 	for ( int i = 0; i < length; i++ )
781 	{
782 		uint8_t value = m_address_space->read_byte(offset + i);
783 		reply += string_format("%02x", value);
784 	}
785 	send_reply(reply.c_str());
786 
787 	return REPLY_NONE;
788 }
789 
790 //-------------------------------------------------------------------------
hex_decode(std::vector<uint8_t> * _data,const char * buf,size_t length)791 static bool hex_decode(std::vector<uint8_t> *_data, const char *buf, size_t length)
792 {
793 	std::vector<uint8_t> &data = *_data;
794 	data.resize(length);
795 	for ( int i = 0; i < length; i++ )
796 	{
797 		if ( sscanf(buf, "%02hhx", &data[i]) != 1 )
798 			return false;
799 		buf += 2;
800 	}
801 	if ( *buf != '\0' )
802 		return false;
803 	return true;
804 }
805 
806 //-------------------------------------------------------------------------
807 // Write memory.
handle_M(const char * buf)808 debug_gdbstub::cmd_reply debug_gdbstub::handle_M(const char *buf)
809 {
810 	uint64_t address;
811 	uint64_t length;
812 	int buf_offset;
813 	if ( sscanf(buf, "%" PRIx64 ",%" PRIx64 ":%n", &address, &length, &buf_offset) != 2 )
814 		return REPLY_ENN;
815 
816 	offs_t offset = address;
817 	if ( !m_memory->translate(m_address_space->spacenum(), TRANSLATE_READ_DEBUG, offset) )
818 		return REPLY_ENN;
819 
820 	std::vector<uint8_t> data;
821 	if ( !hex_decode(&data, buf + buf_offset, length) )
822 		return REPLY_ENN;
823 
824 	for ( int i = 0; i < length; i++ )
825 		m_address_space->write_byte(offset + i, data[i]);
826 
827 	return REPLY_OK;
828 }
829 
830 //-------------------------------------------------------------------------
831 // Read the value of register n.
handle_p(const char * buf)832 debug_gdbstub::cmd_reply debug_gdbstub::handle_p(const char *buf)
833 {
834 	if ( !m_target_xml_sent )
835 		return REPLY_ENN;
836 	int gdb_regnum;
837 	if ( sscanf(buf, "%x", &gdb_regnum) != 1 || gdb_regnum >= m_gdb_registers.size() )
838 		return REPLY_ENN;
839 	std::string reply = get_register_string(gdb_regnum);
840 	send_reply(reply.c_str());
841 	return REPLY_NONE;
842 }
843 
844 //-------------------------------------------------------------------------
845 // Write register n… with value r….
handle_P(const char * buf)846 debug_gdbstub::cmd_reply debug_gdbstub::handle_P(const char *buf)
847 {
848 	if ( !m_target_xml_sent )
849 		return REPLY_ENN;
850 	int gdb_regnum;
851 	int buf_offset;
852 	if ( sscanf(buf, "%x=%n", &gdb_regnum, &buf_offset) != 1 || gdb_regnum >= m_gdb_registers.size() )
853 		return REPLY_ENN;
854 	buf += buf_offset;
855 	uint64_t value;
856 	if ( !parse_register_string(&value, buf, gdb_regnum) )
857 		return REPLY_ENN;
858 	set_register_value(gdb_regnum, value);
859 	return REPLY_OK;
860 }
861 
862 //-------------------------------------------------------------------------
863 // General query.
handle_q(const char * buf)864 debug_gdbstub::cmd_reply debug_gdbstub::handle_q(const char *buf)
865 {
866 	// First check for packets that predate the qname:params convention.
867 	if ( *buf == 'C' )
868 	{
869 		// Return the current thread ID.
870 		send_reply("QC1");
871 		return REPLY_NONE;
872 	}
873 	else if ( *buf == 'P' )
874 	{
875 		// Returns information on thread-id.
876 		return REPLY_UNSUPPORTED;
877 	}
878 	else if ( *buf == 'L' )
879 	{
880 		// Obtain thread information from RTOS.
881 		return REPLY_UNSUPPORTED;
882 	}
883 
884 	// Check packets that use qname,params convention.
885 	if ( strncmp(buf, "Rcmd,", 5) == 0 )
886 	{
887 		buf += 5;
888 		std::vector<uint8_t> data;
889 		if ( !hex_decode(&data, buf, strlen(buf) / 2) )
890 			return REPLY_ENN;
891 		std::string command(data.begin(), data.end());
892 		text_buffer &textbuf = m_debugger_console->get_console_textbuf();
893 		text_buffer_clear(textbuf);
894 		m_debugger_console->execute_command(command, false);
895 		uint32_t nlines = text_buffer_num_lines(textbuf);
896 		if ( nlines == 0 )
897 			return REPLY_OK;
898 		std::string reply;
899 		for ( uint32_t i = 0; i < nlines; i++ )
900 		{
901 			const char *line = text_buffer_get_seqnum_line(textbuf, i);
902 			reply.reserve(reply.length() + (strlen(line)+1)*2);
903 			while ( *line != '\0' )
904 				reply += string_format("%02x", *line++);
905 			reply += "0A";
906 		}
907 		send_reply(reply.c_str());
908 		return REPLY_NONE;
909 	}
910 
911 	// Split name and parameters
912 	const char *ptr = buf;
913 	while ( *ptr != '\0' && *ptr != ':' )
914 		ptr++;
915 	std::string name(buf, ptr-buf);
916 	std::string params;
917 	if ( *ptr != '\0' )
918 		params = ptr+1;
919 
920 	if ( name == "Supported" )
921 	{
922 		std::string reply = string_format("PacketSize=%x", MAX_PACKET_SIZE);
923 		reply += ";qXfer:features:read+";
924 		send_reply(reply.c_str());
925 		return REPLY_NONE;
926 	}
927 	else if ( name == "Xfer" )
928 	{
929 		// "features:read:target.xml:0,3fff"
930 		if ( strncmp(params.c_str(), "features:read:", 14) == 0 )
931 		{
932 			int offset = 0;
933 			int length = 0;
934 			if ( sscanf(params.c_str() + 14, "target.xml:%x,%x", &offset, &length) == 2 )
935 			{
936 				if ( m_target_xml.empty() )
937 					generate_target_xml();
938 				length = std::min(length, (int) m_target_xml.length()-offset);
939 				std::string reply;
940 				if ( offset + length < m_target_xml.length() )
941 					reply += 'm';
942 				else
943 					reply += 'l';
944 				reply += m_target_xml.substr(offset, length);
945 				send_reply(reply.c_str());
946 				m_target_xml_sent = true;
947 				return REPLY_NONE;
948 			}
949 		}
950 	}
951 	else if ( name == "fThreadInfo" )
952 	{
953 		send_reply("m1");
954 		return REPLY_NONE;
955 	}
956 	else if ( name == "sThreadInfo" )
957 	{
958 		send_reply("l");
959 		return REPLY_NONE;
960 	}
961 
962 	return REPLY_UNSUPPORTED;
963 }
964 
965 //-------------------------------------------------------------------------
966 // Single step, resuming at addr.
handle_s(const char * buf)967 debug_gdbstub::cmd_reply debug_gdbstub::handle_s(const char *buf)
968 {
969 	// We don't support stepping with addr.
970 	if ( *buf != '\0' )
971 		return REPLY_UNSUPPORTED;
972 
973 	m_debugger_console->get_visible_cpu()->debug()->single_step();
974 	m_send_stop_packet = true;
975 	return REPLY_NONE;
976 }
977 
978 //-------------------------------------------------------------------------
remove_breakpoint(device_debug * debug,uint64_t address,int)979 static bool remove_breakpoint(device_debug *debug, uint64_t address, int /*kind*/)
980 {
981 	const debug_breakpoint *bp = debug->breakpoint_find(address);
982 	if (bp != nullptr)
983 		return debug->breakpoint_clear(bp->index());
984 	return false;
985 }
986 
987 //-------------------------------------------------------------------------
remove_watchpoint(device_debug * debug,read_or_write type,uint64_t address,int kind)988 static bool remove_watchpoint(device_debug *debug, read_or_write type, uint64_t address, int kind)
989 {
990 	const auto &watchpoints = debug->watchpoint_vector(AS_PROGRAM);
991 	for ( const auto &wp: watchpoints )
992 		if ( wp->type() == type && wp->address() == address && wp->length() == kind )
993 			return debug->watchpoint_clear(wp->index());
994 	return false;
995 }
996 
997 //-------------------------------------------------------------------------
parse_zZ(int * ptype,uint64_t * paddress,int * pkind,const char * buf)998 bool debug_gdbstub::parse_zZ(int *ptype, uint64_t *paddress, int *pkind, const char *buf)
999 {
1000 	int buf_offset;
1001 	if ( sscanf(buf, "%d,%" PRIx64 ",%x%n", ptype, paddress, pkind, &buf_offset) != 3 || buf[buf_offset] != '\0' )
1002 		return false;
1003 	return true;
1004 }
1005 
1006 //-------------------------------------------------------------------------
1007 // Remove breakpoint or watchpoint.
handle_z(const char * buf)1008 debug_gdbstub::cmd_reply debug_gdbstub::handle_z(const char *buf)
1009 {
1010 	int type;
1011 	uint64_t address;
1012 	int kind;
1013 	if ( !parse_zZ(&type, &address, &kind, buf) )
1014 		return REPLY_ENN;
1015 
1016 	// watchpoints
1017 	offs_t offset = address;
1018 	if ( type == 2 || type == 3 || type == 4 )
1019 	{
1020 		if ( !m_memory->translate(m_address_space->spacenum(), TRANSLATE_READ_DEBUG, offset) )
1021 			return REPLY_ENN;
1022 		m_address_map.erase(offset);
1023 	}
1024 
1025 	device_debug *debug = m_debugger_console->get_visible_cpu()->debug();
1026 	switch ( type )
1027 	{
1028 		// Note: software and hardware breakpoints are treated both the
1029 		//       same way, and the 'kind' parameter is ignored.
1030 		case 0: // software breakpoint
1031 		case 1: // hardware breakpoint
1032 			return remove_breakpoint(debug, address, kind) ? REPLY_OK : REPLY_ENN;
1033 		case 2:
1034 			// write watchpoint
1035 			return remove_watchpoint(debug, read_or_write::WRITE, offset, kind) ? REPLY_OK : REPLY_ENN;
1036 		case 3:
1037 			// read watchpoint
1038 			return remove_watchpoint(debug, read_or_write::READ, offset, kind) ? REPLY_OK : REPLY_ENN;
1039 		case 4:
1040 			// access watchpoint
1041 			return remove_watchpoint(debug, read_or_write::READWRITE, offset, kind) ? REPLY_OK : REPLY_ENN;
1042 	}
1043 
1044 	return REPLY_UNSUPPORTED;
1045 }
1046 
1047 //-------------------------------------------------------------------------
1048 // Insert breakpoint or watchpoint.
handle_Z(const char * buf)1049 debug_gdbstub::cmd_reply debug_gdbstub::handle_Z(const char *buf)
1050 {
1051 	int type;
1052 	uint64_t address;
1053 	int kind;
1054 	if ( !parse_zZ(&type, &address, &kind, buf) )
1055 		return REPLY_ENN;
1056 
1057 	// watchpoints
1058 	offs_t offset = address;
1059 	if ( type == 2 || type == 3 || type == 4 )
1060 	{
1061 		if ( !m_memory->translate(m_address_space->spacenum(), TRANSLATE_READ_DEBUG, offset) )
1062 			return REPLY_ENN;
1063 		m_address_map[offset] = address;
1064 	}
1065 
1066 	device_debug *debug = m_debugger_console->get_visible_cpu()->debug();
1067 	switch ( type )
1068 	{
1069 		// Note: software and hardware breakpoints are treated both the
1070 		//       same way, and the 'kind' parameter is ignored.
1071 		case 0: // software breakpoint
1072 		case 1: // hardware breakpoint
1073 			debug->breakpoint_set(address);
1074 			return REPLY_OK;
1075 		case 2:
1076 			// write watchpoint
1077 			debug->watchpoint_set(*m_address_space, read_or_write::WRITE, offset, kind, nullptr, nullptr);
1078 			return REPLY_OK;
1079 		case 3:
1080 			// read watchpoint
1081 			debug->watchpoint_set(*m_address_space, read_or_write::READ, offset, kind, nullptr, nullptr);
1082 			return REPLY_OK;
1083 		case 4:
1084 			// access watchpoint
1085 			debug->watchpoint_set(*m_address_space, read_or_write::READWRITE, offset, kind, nullptr, nullptr);
1086 			return REPLY_OK;
1087 	}
1088 
1089 	return REPLY_UNSUPPORTED;
1090 }
1091 
1092 
1093 //-------------------------------------------------------------------------
send_stop_packet()1094 void debug_gdbstub::send_stop_packet()
1095 {
1096 	int signal = 5; // GDB_SIGNAL_TRAP
1097 	std::string reply = string_format("T%02x", signal);
1098 	if ( m_triggered_watchpoint != nullptr )
1099 	{
1100 		switch ( m_triggered_watchpoint->type() )
1101 		{
1102 			case read_or_write::WRITE:
1103 				reply += "watch";
1104 				break;
1105 			case read_or_write::READ:
1106 				reply += "rwatch";
1107 				break;
1108 			case read_or_write::READWRITE:
1109 				reply += "awatch";
1110 				break;
1111 		}
1112 		offs_t offset = m_triggered_watchpoint->address();
1113 		uint64_t address = m_address_map[offset];
1114 		reply += string_format(":%" PRIx64 ";", address);
1115 	}
1116 	if ( m_target_xml_sent )
1117 		for ( const auto &gdb_regnum: m_stop_reply_registers )
1118 			reply += string_format("%02x:%s;", gdb_regnum, get_register_string(gdb_regnum));
1119 	send_reply(reply.c_str());
1120 }
1121 
1122 //-------------------------------------------------------------------------
handle_packet()1123 void debug_gdbstub::handle_packet()
1124 {
1125 	// For any command not supported by the stub, an empty response
1126 	// (‘$#00’) should be returned. That way it is possible to extend
1127 	// the protocol. A newer GDB can tell if a packet is supported
1128 	// based on that response.
1129 	cmd_reply reply = REPLY_UNSUPPORTED;
1130 
1131 	const char *buf = (const char *) m_packet_buf+1;
1132 	switch ( m_packet_buf[0] )
1133 	{
1134 		case '!': reply = handle_exclamation(buf); break;
1135 		case '?': reply = handle_question(buf); break;
1136 		case 'c': reply = handle_c(buf); break;
1137 		case 'D': reply = handle_D(buf); break;
1138 		case 'g': reply = handle_g(buf); break;
1139 		case 'G': reply = handle_G(buf); break;
1140 		case 'H': reply = handle_H(buf); break;
1141 		case 'k': reply = handle_k(buf); break;
1142 		case 'm': reply = handle_m(buf); break;
1143 		case 'M': reply = handle_M(buf); break;
1144 		case 'p': reply = handle_p(buf); break;
1145 		case 'P': reply = handle_P(buf); break;
1146 		case 'q': reply = handle_q(buf); break;
1147 		case 's': reply = handle_s(buf); break;
1148 		case 'z': reply = handle_z(buf); break;
1149 		case 'Z': reply = handle_Z(buf); break;
1150 	}
1151 	if ( reply == REPLY_OK )
1152 		send_reply("OK");
1153 	else if ( reply == REPLY_ENN )
1154 		send_reply("E01");
1155 	else if ( reply == REPLY_UNSUPPORTED )
1156 		send_reply("");
1157 }
1158 
1159 //-------------------------------------------------------------------------
1160 #define BYTESWAP_64(x) ((((x) << 56) & 0xFF00000000000000) \
1161 					  | (((x) << 40) & 0x00FF000000000000) \
1162 					  | (((x) << 24) & 0x0000FF0000000000) \
1163 					  | (((x) <<  8) & 0x000000FF00000000) \
1164 					  | (((x) >>  8) & 0x00000000FF000000) \
1165 					  | (((x) >> 24) & 0x0000000000FF0000) \
1166 					  | (((x) >> 40) & 0x000000000000FF00) \
1167 					  | (((x) >> 56) & 0x00000000000000FF))
1168 #define BYTESWAP_32(x) ((((x) << 24) & 0xFF000000) \
1169 					  | (((x) <<  8) & 0x00FF0000) \
1170 					  | (((x) >>  8) & 0x0000FF00) \
1171 					  | (((x) >> 24) & 0x000000FF))
1172 #define BYTESWAP_16(x) ((((x) <<  8) & 0xFF00) \
1173 					  | (((x) >>  8) & 0x00FF))
1174 
1175 //-------------------------------------------------------------------------
get_register_string(int gdb_regnum)1176 std::string debug_gdbstub::get_register_string(int gdb_regnum)
1177 {
1178 	const gdb_register &reg = m_gdb_registers[gdb_regnum];
1179 	const char *fmt = (reg.gdb_bitsize == 64) ? "%016" PRIx64
1180 					: (reg.gdb_bitsize == 32) ? "%08"  PRIx64
1181 					: (reg.gdb_bitsize == 16) ? "%04"  PRIx64
1182 					:                           "%02"  PRIx64;
1183 	uint64_t value = m_state->state_int(reg.state_index);
1184 	if ( reg.gdb_bitsize < 64 )
1185 		value &= (1ULL << reg.gdb_bitsize) - 1;
1186 	if ( !m_is_be )
1187 	{
1188 		value = (reg.gdb_bitsize == 64) ? BYTESWAP_64(value)
1189 			  : (reg.gdb_bitsize == 32) ? BYTESWAP_32(value)
1190 			  : (reg.gdb_bitsize == 16) ? BYTESWAP_16(value)
1191 			  :                           value;
1192 	}
1193 	return string_format(fmt, value);
1194 }
1195 
1196 //-------------------------------------------------------------------------
parse_register_string(uint64_t * pvalue,const char * buf,int gdb_regnum)1197 bool debug_gdbstub::parse_register_string(uint64_t *pvalue, const char *buf, int gdb_regnum)
1198 {
1199 	const gdb_register &reg = m_gdb_registers[gdb_regnum];
1200 	const char *fmt = (reg.gdb_bitsize == 64) ? "%016" PRIx64
1201 					: (reg.gdb_bitsize == 32) ? "%08"  PRIx64
1202 					: (reg.gdb_bitsize == 16) ? "%04"  PRIx64
1203 					:                           "%02"  PRIx64;
1204 	uint64_t value;
1205 	if ( sscanf(buf, fmt, &value) != 1 )
1206 		return false;
1207 	if ( !m_is_be )
1208 	{
1209 		value = (reg.gdb_bitsize == 64) ? BYTESWAP_64(value)
1210 			  : (reg.gdb_bitsize == 32) ? BYTESWAP_32(value)
1211 			  : (reg.gdb_bitsize == 16) ? BYTESWAP_16(value)
1212 			  :                           value;
1213 	}
1214 	*pvalue = value;
1215 	return true;
1216 }
1217 
1218 //-------------------------------------------------------------------------
set_register_value(int gdb_regnum,uint64_t value)1219 void debug_gdbstub::set_register_value(int gdb_regnum, uint64_t value)
1220 {
1221 	const gdb_register &reg = m_gdb_registers[gdb_regnum];
1222 	m_state->set_state_int(reg.state_index, value);
1223 }
1224 
1225 //-------------------------------------------------------------------------
is_thread_id_ok(const char * buf)1226 bool debug_gdbstub::is_thread_id_ok(const char *buf)
1227 {
1228 	// 'any'
1229 	if ( buf[0] == '0' && buf[1] == '\0' )
1230 		return true;
1231 	// The thread id we reported.
1232 	if ( buf[0] == '1' && buf[1] == '\0' )
1233 		return true;
1234 	// 'all'
1235 	if ( buf[0] == '-' && buf[1] == '1' && buf[2] == '\0' )
1236 		return true;
1237 	return false;
1238 }
1239 
1240 //-------------------------------------------------------------------------
handle_character(char ch)1241 void debug_gdbstub::handle_character(char ch)
1242 {
1243 	int8_t nibble;
1244 	switch ( m_readbuf_state )
1245 	{
1246 		case PACKET_START:
1247 			if ( ch == '$' )
1248 			{
1249 				m_packet_len = 0;
1250 				m_packet_checksum = 0;
1251 				m_readbuf_state = PACKET_DATA;
1252 			}
1253 			else if ( ch == '\x03' )
1254 			{
1255 				m_debugger_cpu->set_execution_stopped();
1256 			}
1257 			break;
1258 		case PACKET_DATA:
1259 			if ( ch == '#' )
1260 			{
1261 				m_readbuf_state = PACKET_CHECKSUM1;
1262 			}
1263 			else if ( m_packet_len >= MAX_PACKET_SIZE )
1264 			{
1265 				osd_printf_info("gdbstub: packet buffer overflow!\n");
1266 				m_readbuf_state = PACKET_START;
1267 			}
1268 			else
1269 			{
1270 				m_packet_buf[m_packet_len++] = ch;
1271 				m_packet_checksum += ch;
1272 			}
1273 			break;
1274 		case PACKET_CHECKSUM1:
1275 			if ( sscanf(&ch, "%01hhx", &nibble) != 1 )
1276 			{
1277 				osd_printf_info("gdbstub: invalid checksum!\n");
1278 				m_readbuf_state = PACKET_START;
1279 				break;
1280 			}
1281 			m_recv_checksum = nibble;
1282 			m_readbuf_state = PACKET_CHECKSUM2;
1283 			break;
1284 		case PACKET_CHECKSUM2:
1285 			if ( sscanf(&ch, "%01hhx", &nibble) != 1 )
1286 			{
1287 				osd_printf_info("gdbstub: invalid checksum!\n");
1288 				m_readbuf_state = PACKET_START;
1289 				break;
1290 			}
1291 			m_recv_checksum <<= 4;
1292 			m_recv_checksum |= nibble;
1293 			if ( m_recv_checksum != m_packet_checksum )
1294 			{
1295 				osd_printf_info("gdbstub: bad checksum!\n");
1296 				send_nack();
1297 				m_readbuf_state = PACKET_START;
1298 				break;
1299 			}
1300 			m_packet_buf[m_packet_len] = '\0';
1301 			send_ack();
1302 			handle_packet();
1303 			m_readbuf_state = PACKET_START;
1304 			break;
1305 	}
1306 }
1307 
1308 //-------------------------------------------------------------------------
1309 MODULE_DEFINITION(DEBUG_GDBSTUB, debug_gdbstub)
1310