gdbstub.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165
  1. /*
  2. * gdb server stub
  3. *
  4. * This implements a subset of the remote protocol as described in:
  5. *
  6. * https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
  7. *
  8. * Copyright (c) 2003-2005 Fabrice Bellard
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * SPDX-License-Identifier: LGPL-2.0+
  24. */
  25. #include "qemu/osdep.h"
  26. #include "qemu/ctype.h"
  27. #include "qemu/cutils.h"
  28. #include "qemu/module.h"
  29. #include "qemu/error-report.h"
  30. #include "trace.h"
  31. #include "exec/gdbstub.h"
  32. #include "gdbstub/syscalls.h"
  33. #ifdef CONFIG_USER_ONLY
  34. #include "gdbstub/user.h"
  35. #else
  36. #include "hw/cpu/cluster.h"
  37. #include "hw/boards.h"
  38. #endif
  39. #include "sysemu/hw_accel.h"
  40. #include "sysemu/runstate.h"
  41. #include "exec/replay-core.h"
  42. #include "exec/hwaddr.h"
  43. #include "internals.h"
  44. typedef struct GDBRegisterState {
  45. int base_reg;
  46. int num_regs;
  47. gdb_get_reg_cb get_reg;
  48. gdb_set_reg_cb set_reg;
  49. const char *xml;
  50. struct GDBRegisterState *next;
  51. } GDBRegisterState;
  52. GDBState gdbserver_state;
  53. void gdb_init_gdbserver_state(void)
  54. {
  55. g_assert(!gdbserver_state.init);
  56. memset(&gdbserver_state, 0, sizeof(GDBState));
  57. gdbserver_state.init = true;
  58. gdbserver_state.str_buf = g_string_new(NULL);
  59. gdbserver_state.mem_buf = g_byte_array_sized_new(MAX_PACKET_LENGTH);
  60. gdbserver_state.last_packet = g_byte_array_sized_new(MAX_PACKET_LENGTH + 4);
  61. /*
  62. * What single-step modes are supported is accelerator dependent.
  63. * By default try to use no IRQs and no timers while single
  64. * stepping so as to make single stepping like a typical ICE HW step.
  65. */
  66. gdbserver_state.supported_sstep_flags = accel_supported_gdbstub_sstep_flags();
  67. gdbserver_state.sstep_flags = SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER;
  68. gdbserver_state.sstep_flags &= gdbserver_state.supported_sstep_flags;
  69. }
  70. bool gdb_has_xml;
  71. /* writes 2*len+1 bytes in buf */
  72. void gdb_memtohex(GString *buf, const uint8_t *mem, int len)
  73. {
  74. int i, c;
  75. for(i = 0; i < len; i++) {
  76. c = mem[i];
  77. g_string_append_c(buf, tohex(c >> 4));
  78. g_string_append_c(buf, tohex(c & 0xf));
  79. }
  80. g_string_append_c(buf, '\0');
  81. }
  82. void gdb_hextomem(GByteArray *mem, const char *buf, int len)
  83. {
  84. int i;
  85. for(i = 0; i < len; i++) {
  86. guint8 byte = fromhex(buf[0]) << 4 | fromhex(buf[1]);
  87. g_byte_array_append(mem, &byte, 1);
  88. buf += 2;
  89. }
  90. }
  91. static void hexdump(const char *buf, int len,
  92. void (*trace_fn)(size_t ofs, char const *text))
  93. {
  94. char line_buffer[3 * 16 + 4 + 16 + 1];
  95. size_t i;
  96. for (i = 0; i < len || (i & 0xF); ++i) {
  97. size_t byte_ofs = i & 15;
  98. if (byte_ofs == 0) {
  99. memset(line_buffer, ' ', 3 * 16 + 4 + 16);
  100. line_buffer[3 * 16 + 4 + 16] = 0;
  101. }
  102. size_t col_group = (i >> 2) & 3;
  103. size_t hex_col = byte_ofs * 3 + col_group;
  104. size_t txt_col = 3 * 16 + 4 + byte_ofs;
  105. if (i < len) {
  106. char value = buf[i];
  107. line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
  108. line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
  109. line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
  110. ? value
  111. : '.';
  112. }
  113. if (byte_ofs == 0xF)
  114. trace_fn(i & -16, line_buffer);
  115. }
  116. }
  117. /* return -1 if error, 0 if OK */
  118. int gdb_put_packet_binary(const char *buf, int len, bool dump)
  119. {
  120. int csum, i;
  121. uint8_t footer[3];
  122. if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
  123. hexdump(buf, len, trace_gdbstub_io_binaryreply);
  124. }
  125. for(;;) {
  126. g_byte_array_set_size(gdbserver_state.last_packet, 0);
  127. g_byte_array_append(gdbserver_state.last_packet,
  128. (const uint8_t *) "$", 1);
  129. g_byte_array_append(gdbserver_state.last_packet,
  130. (const uint8_t *) buf, len);
  131. csum = 0;
  132. for(i = 0; i < len; i++) {
  133. csum += buf[i];
  134. }
  135. footer[0] = '#';
  136. footer[1] = tohex((csum >> 4) & 0xf);
  137. footer[2] = tohex((csum) & 0xf);
  138. g_byte_array_append(gdbserver_state.last_packet, footer, 3);
  139. gdb_put_buffer(gdbserver_state.last_packet->data,
  140. gdbserver_state.last_packet->len);
  141. if (gdb_got_immediate_ack()) {
  142. break;
  143. }
  144. }
  145. return 0;
  146. }
  147. /* return -1 if error, 0 if OK */
  148. int gdb_put_packet(const char *buf)
  149. {
  150. trace_gdbstub_io_reply(buf);
  151. return gdb_put_packet_binary(buf, strlen(buf), false);
  152. }
  153. void gdb_put_strbuf(void)
  154. {
  155. gdb_put_packet(gdbserver_state.str_buf->str);
  156. }
  157. /* Encode data using the encoding for 'x' packets. */
  158. void gdb_memtox(GString *buf, const char *mem, int len)
  159. {
  160. char c;
  161. while (len--) {
  162. c = *(mem++);
  163. switch (c) {
  164. case '#': case '$': case '*': case '}':
  165. g_string_append_c(buf, '}');
  166. g_string_append_c(buf, c ^ 0x20);
  167. break;
  168. default:
  169. g_string_append_c(buf, c);
  170. break;
  171. }
  172. }
  173. }
  174. static uint32_t gdb_get_cpu_pid(CPUState *cpu)
  175. {
  176. /* TODO: In user mode, we should use the task state PID */
  177. if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
  178. /* Return the default process' PID */
  179. int index = gdbserver_state.process_num - 1;
  180. return gdbserver_state.processes[index].pid;
  181. }
  182. return cpu->cluster_index + 1;
  183. }
  184. static GDBProcess *gdb_get_process(uint32_t pid)
  185. {
  186. int i;
  187. if (!pid) {
  188. /* 0 means any process, we take the first one */
  189. return &gdbserver_state.processes[0];
  190. }
  191. for (i = 0; i < gdbserver_state.process_num; i++) {
  192. if (gdbserver_state.processes[i].pid == pid) {
  193. return &gdbserver_state.processes[i];
  194. }
  195. }
  196. return NULL;
  197. }
  198. static GDBProcess *gdb_get_cpu_process(CPUState *cpu)
  199. {
  200. return gdb_get_process(gdb_get_cpu_pid(cpu));
  201. }
  202. static CPUState *find_cpu(uint32_t thread_id)
  203. {
  204. CPUState *cpu;
  205. CPU_FOREACH(cpu) {
  206. if (gdb_get_cpu_index(cpu) == thread_id) {
  207. return cpu;
  208. }
  209. }
  210. return NULL;
  211. }
  212. static CPUState *get_first_cpu_in_process(GDBProcess *process)
  213. {
  214. CPUState *cpu;
  215. CPU_FOREACH(cpu) {
  216. if (gdb_get_cpu_pid(cpu) == process->pid) {
  217. return cpu;
  218. }
  219. }
  220. return NULL;
  221. }
  222. static CPUState *gdb_next_cpu_in_process(CPUState *cpu)
  223. {
  224. uint32_t pid = gdb_get_cpu_pid(cpu);
  225. cpu = CPU_NEXT(cpu);
  226. while (cpu) {
  227. if (gdb_get_cpu_pid(cpu) == pid) {
  228. break;
  229. }
  230. cpu = CPU_NEXT(cpu);
  231. }
  232. return cpu;
  233. }
  234. /* Return the cpu following @cpu, while ignoring unattached processes. */
  235. static CPUState *gdb_next_attached_cpu(CPUState *cpu)
  236. {
  237. cpu = CPU_NEXT(cpu);
  238. while (cpu) {
  239. if (gdb_get_cpu_process(cpu)->attached) {
  240. break;
  241. }
  242. cpu = CPU_NEXT(cpu);
  243. }
  244. return cpu;
  245. }
  246. /* Return the first attached cpu */
  247. CPUState *gdb_first_attached_cpu(void)
  248. {
  249. CPUState *cpu = first_cpu;
  250. GDBProcess *process = gdb_get_cpu_process(cpu);
  251. if (!process->attached) {
  252. return gdb_next_attached_cpu(cpu);
  253. }
  254. return cpu;
  255. }
  256. static CPUState *gdb_get_cpu(uint32_t pid, uint32_t tid)
  257. {
  258. GDBProcess *process;
  259. CPUState *cpu;
  260. if (!pid && !tid) {
  261. /* 0 means any process/thread, we take the first attached one */
  262. return gdb_first_attached_cpu();
  263. } else if (pid && !tid) {
  264. /* any thread in a specific process */
  265. process = gdb_get_process(pid);
  266. if (process == NULL) {
  267. return NULL;
  268. }
  269. if (!process->attached) {
  270. return NULL;
  271. }
  272. return get_first_cpu_in_process(process);
  273. } else {
  274. /* a specific thread */
  275. cpu = find_cpu(tid);
  276. if (cpu == NULL) {
  277. return NULL;
  278. }
  279. process = gdb_get_cpu_process(cpu);
  280. if (pid && process->pid != pid) {
  281. return NULL;
  282. }
  283. if (!process->attached) {
  284. return NULL;
  285. }
  286. return cpu;
  287. }
  288. }
  289. static const char *get_feature_xml(const char *p, const char **newp,
  290. GDBProcess *process)
  291. {
  292. size_t len;
  293. int i;
  294. const char *name;
  295. CPUState *cpu = get_first_cpu_in_process(process);
  296. CPUClass *cc = CPU_GET_CLASS(cpu);
  297. len = 0;
  298. while (p[len] && p[len] != ':')
  299. len++;
  300. *newp = p + len;
  301. name = NULL;
  302. if (strncmp(p, "target.xml", len) == 0) {
  303. char *buf = process->target_xml;
  304. const size_t buf_sz = sizeof(process->target_xml);
  305. /* Generate the XML description for this CPU. */
  306. if (!buf[0]) {
  307. GDBRegisterState *r;
  308. pstrcat(buf, buf_sz,
  309. "<?xml version=\"1.0\"?>"
  310. "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
  311. "<target>");
  312. if (cc->gdb_arch_name) {
  313. gchar *arch = cc->gdb_arch_name(cpu);
  314. pstrcat(buf, buf_sz, "<architecture>");
  315. pstrcat(buf, buf_sz, arch);
  316. pstrcat(buf, buf_sz, "</architecture>");
  317. g_free(arch);
  318. }
  319. pstrcat(buf, buf_sz, "<xi:include href=\"");
  320. pstrcat(buf, buf_sz, cc->gdb_core_xml_file);
  321. pstrcat(buf, buf_sz, "\"/>");
  322. for (r = cpu->gdb_regs; r; r = r->next) {
  323. pstrcat(buf, buf_sz, "<xi:include href=\"");
  324. pstrcat(buf, buf_sz, r->xml);
  325. pstrcat(buf, buf_sz, "\"/>");
  326. }
  327. pstrcat(buf, buf_sz, "</target>");
  328. }
  329. return buf;
  330. }
  331. if (cc->gdb_get_dynamic_xml) {
  332. char *xmlname = g_strndup(p, len);
  333. const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
  334. g_free(xmlname);
  335. if (xml) {
  336. return xml;
  337. }
  338. }
  339. for (i = 0; ; i++) {
  340. name = xml_builtin[i][0];
  341. if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
  342. break;
  343. }
  344. return name ? xml_builtin[i][1] : NULL;
  345. }
  346. static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)
  347. {
  348. CPUClass *cc = CPU_GET_CLASS(cpu);
  349. CPUArchState *env = cpu->env_ptr;
  350. GDBRegisterState *r;
  351. if (reg < cc->gdb_num_core_regs) {
  352. return cc->gdb_read_register(cpu, buf, reg);
  353. }
  354. for (r = cpu->gdb_regs; r; r = r->next) {
  355. if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
  356. return r->get_reg(env, buf, reg - r->base_reg);
  357. }
  358. }
  359. return 0;
  360. }
  361. static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
  362. {
  363. CPUClass *cc = CPU_GET_CLASS(cpu);
  364. CPUArchState *env = cpu->env_ptr;
  365. GDBRegisterState *r;
  366. if (reg < cc->gdb_num_core_regs) {
  367. return cc->gdb_write_register(cpu, mem_buf, reg);
  368. }
  369. for (r = cpu->gdb_regs; r; r = r->next) {
  370. if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
  371. return r->set_reg(env, mem_buf, reg - r->base_reg);
  372. }
  373. }
  374. return 0;
  375. }
  376. /* Register a supplemental set of CPU registers. If g_pos is nonzero it
  377. specifies the first register number and these registers are included in
  378. a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
  379. gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
  380. */
  381. void gdb_register_coprocessor(CPUState *cpu,
  382. gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg,
  383. int num_regs, const char *xml, int g_pos)
  384. {
  385. GDBRegisterState *s;
  386. GDBRegisterState **p;
  387. p = &cpu->gdb_regs;
  388. while (*p) {
  389. /* Check for duplicates. */
  390. if (strcmp((*p)->xml, xml) == 0)
  391. return;
  392. p = &(*p)->next;
  393. }
  394. s = g_new0(GDBRegisterState, 1);
  395. s->base_reg = cpu->gdb_num_regs;
  396. s->num_regs = num_regs;
  397. s->get_reg = get_reg;
  398. s->set_reg = set_reg;
  399. s->xml = xml;
  400. /* Add to end of list. */
  401. cpu->gdb_num_regs += num_regs;
  402. *p = s;
  403. if (g_pos) {
  404. if (g_pos != s->base_reg) {
  405. error_report("Error: Bad gdb register numbering for '%s', "
  406. "expected %d got %d", xml, g_pos, s->base_reg);
  407. } else {
  408. cpu->gdb_num_g_regs = cpu->gdb_num_regs;
  409. }
  410. }
  411. }
  412. static void gdb_process_breakpoint_remove_all(GDBProcess *p)
  413. {
  414. CPUState *cpu = get_first_cpu_in_process(p);
  415. while (cpu) {
  416. gdb_breakpoint_remove_all(cpu);
  417. cpu = gdb_next_cpu_in_process(cpu);
  418. }
  419. }
  420. static void gdb_set_cpu_pc(vaddr pc)
  421. {
  422. CPUState *cpu = gdbserver_state.c_cpu;
  423. cpu_synchronize_state(cpu);
  424. cpu_set_pc(cpu, pc);
  425. }
  426. void gdb_append_thread_id(CPUState *cpu, GString *buf)
  427. {
  428. if (gdbserver_state.multiprocess) {
  429. g_string_append_printf(buf, "p%02x.%02x",
  430. gdb_get_cpu_pid(cpu), gdb_get_cpu_index(cpu));
  431. } else {
  432. g_string_append_printf(buf, "%02x", gdb_get_cpu_index(cpu));
  433. }
  434. }
  435. static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
  436. uint32_t *pid, uint32_t *tid)
  437. {
  438. unsigned long p, t;
  439. int ret;
  440. if (*buf == 'p') {
  441. buf++;
  442. ret = qemu_strtoul(buf, &buf, 16, &p);
  443. if (ret) {
  444. return GDB_READ_THREAD_ERR;
  445. }
  446. /* Skip '.' */
  447. buf++;
  448. } else {
  449. p = 1;
  450. }
  451. ret = qemu_strtoul(buf, &buf, 16, &t);
  452. if (ret) {
  453. return GDB_READ_THREAD_ERR;
  454. }
  455. *end_buf = buf;
  456. if (p == -1) {
  457. return GDB_ALL_PROCESSES;
  458. }
  459. if (pid) {
  460. *pid = p;
  461. }
  462. if (t == -1) {
  463. return GDB_ALL_THREADS;
  464. }
  465. if (tid) {
  466. *tid = t;
  467. }
  468. return GDB_ONE_THREAD;
  469. }
  470. /**
  471. * gdb_handle_vcont - Parses and handles a vCont packet.
  472. * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
  473. * a format error, 0 on success.
  474. */
  475. static int gdb_handle_vcont(const char *p)
  476. {
  477. int res, signal = 0;
  478. char cur_action;
  479. char *newstates;
  480. unsigned long tmp;
  481. uint32_t pid, tid;
  482. GDBProcess *process;
  483. CPUState *cpu;
  484. GDBThreadIdKind kind;
  485. unsigned int max_cpus = gdb_get_max_cpus();
  486. /* uninitialised CPUs stay 0 */
  487. newstates = g_new0(char, max_cpus);
  488. /* mark valid CPUs with 1 */
  489. CPU_FOREACH(cpu) {
  490. newstates[cpu->cpu_index] = 1;
  491. }
  492. /*
  493. * res keeps track of what error we are returning, with -ENOTSUP meaning
  494. * that the command is unknown or unsupported, thus returning an empty
  495. * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
  496. * or incorrect parameters passed.
  497. */
  498. res = 0;
  499. while (*p) {
  500. if (*p++ != ';') {
  501. res = -ENOTSUP;
  502. goto out;
  503. }
  504. cur_action = *p++;
  505. if (cur_action == 'C' || cur_action == 'S') {
  506. cur_action = qemu_tolower(cur_action);
  507. res = qemu_strtoul(p, &p, 16, &tmp);
  508. if (res) {
  509. goto out;
  510. }
  511. signal = gdb_signal_to_target(tmp);
  512. } else if (cur_action != 'c' && cur_action != 's') {
  513. /* unknown/invalid/unsupported command */
  514. res = -ENOTSUP;
  515. goto out;
  516. }
  517. if (*p == '\0' || *p == ';') {
  518. /*
  519. * No thread specifier, action is on "all threads". The
  520. * specification is unclear regarding the process to act on. We
  521. * choose all processes.
  522. */
  523. kind = GDB_ALL_PROCESSES;
  524. } else if (*p++ == ':') {
  525. kind = read_thread_id(p, &p, &pid, &tid);
  526. } else {
  527. res = -ENOTSUP;
  528. goto out;
  529. }
  530. switch (kind) {
  531. case GDB_READ_THREAD_ERR:
  532. res = -EINVAL;
  533. goto out;
  534. case GDB_ALL_PROCESSES:
  535. cpu = gdb_first_attached_cpu();
  536. while (cpu) {
  537. if (newstates[cpu->cpu_index] == 1) {
  538. newstates[cpu->cpu_index] = cur_action;
  539. }
  540. cpu = gdb_next_attached_cpu(cpu);
  541. }
  542. break;
  543. case GDB_ALL_THREADS:
  544. process = gdb_get_process(pid);
  545. if (!process->attached) {
  546. res = -EINVAL;
  547. goto out;
  548. }
  549. cpu = get_first_cpu_in_process(process);
  550. while (cpu) {
  551. if (newstates[cpu->cpu_index] == 1) {
  552. newstates[cpu->cpu_index] = cur_action;
  553. }
  554. cpu = gdb_next_cpu_in_process(cpu);
  555. }
  556. break;
  557. case GDB_ONE_THREAD:
  558. cpu = gdb_get_cpu(pid, tid);
  559. /* invalid CPU/thread specified */
  560. if (!cpu) {
  561. res = -EINVAL;
  562. goto out;
  563. }
  564. /* only use if no previous match occourred */
  565. if (newstates[cpu->cpu_index] == 1) {
  566. newstates[cpu->cpu_index] = cur_action;
  567. }
  568. break;
  569. }
  570. }
  571. gdbserver_state.signal = signal;
  572. gdb_continue_partial(newstates);
  573. out:
  574. g_free(newstates);
  575. return res;
  576. }
  577. static const char *cmd_next_param(const char *param, const char delimiter)
  578. {
  579. static const char all_delimiters[] = ",;:=";
  580. char curr_delimiters[2] = {0};
  581. const char *delimiters;
  582. if (delimiter == '?') {
  583. delimiters = all_delimiters;
  584. } else if (delimiter == '0') {
  585. return strchr(param, '\0');
  586. } else if (delimiter == '.' && *param) {
  587. return param + 1;
  588. } else {
  589. curr_delimiters[0] = delimiter;
  590. delimiters = curr_delimiters;
  591. }
  592. param += strcspn(param, delimiters);
  593. if (*param) {
  594. param++;
  595. }
  596. return param;
  597. }
  598. static int cmd_parse_params(const char *data, const char *schema,
  599. GArray *params)
  600. {
  601. const char *curr_schema, *curr_data;
  602. g_assert(schema);
  603. g_assert(params->len == 0);
  604. curr_schema = schema;
  605. curr_data = data;
  606. while (curr_schema[0] && curr_schema[1] && *curr_data) {
  607. GdbCmdVariant this_param;
  608. switch (curr_schema[0]) {
  609. case 'l':
  610. if (qemu_strtoul(curr_data, &curr_data, 16,
  611. &this_param.val_ul)) {
  612. return -EINVAL;
  613. }
  614. curr_data = cmd_next_param(curr_data, curr_schema[1]);
  615. g_array_append_val(params, this_param);
  616. break;
  617. case 'L':
  618. if (qemu_strtou64(curr_data, &curr_data, 16,
  619. (uint64_t *)&this_param.val_ull)) {
  620. return -EINVAL;
  621. }
  622. curr_data = cmd_next_param(curr_data, curr_schema[1]);
  623. g_array_append_val(params, this_param);
  624. break;
  625. case 's':
  626. this_param.data = curr_data;
  627. curr_data = cmd_next_param(curr_data, curr_schema[1]);
  628. g_array_append_val(params, this_param);
  629. break;
  630. case 'o':
  631. this_param.opcode = *(uint8_t *)curr_data;
  632. curr_data = cmd_next_param(curr_data, curr_schema[1]);
  633. g_array_append_val(params, this_param);
  634. break;
  635. case 't':
  636. this_param.thread_id.kind =
  637. read_thread_id(curr_data, &curr_data,
  638. &this_param.thread_id.pid,
  639. &this_param.thread_id.tid);
  640. curr_data = cmd_next_param(curr_data, curr_schema[1]);
  641. g_array_append_val(params, this_param);
  642. break;
  643. case '?':
  644. curr_data = cmd_next_param(curr_data, curr_schema[1]);
  645. break;
  646. default:
  647. return -EINVAL;
  648. }
  649. curr_schema += 2;
  650. }
  651. return 0;
  652. }
  653. typedef void (*GdbCmdHandler)(GArray *params, void *user_ctx);
  654. /*
  655. * cmd_startswith -> cmd is compared using startswith
  656. *
  657. * allow_stop_reply -> true iff the gdbstub can respond to this command with a
  658. * "stop reply" packet. The list of commands that accept such response is
  659. * defined at the GDB Remote Serial Protocol documentation. see:
  660. * https://sourceware.org/gdb/onlinedocs/gdb/Stop-Reply-Packets.html#Stop-Reply-Packets.
  661. *
  662. * schema definitions:
  663. * Each schema parameter entry consists of 2 chars,
  664. * the first char represents the parameter type handling
  665. * the second char represents the delimiter for the next parameter
  666. *
  667. * Currently supported schema types:
  668. * 'l' -> unsigned long (stored in .val_ul)
  669. * 'L' -> unsigned long long (stored in .val_ull)
  670. * 's' -> string (stored in .data)
  671. * 'o' -> single char (stored in .opcode)
  672. * 't' -> thread id (stored in .thread_id)
  673. * '?' -> skip according to delimiter
  674. *
  675. * Currently supported delimiters:
  676. * '?' -> Stop at any delimiter (",;:=\0")
  677. * '0' -> Stop at "\0"
  678. * '.' -> Skip 1 char unless reached "\0"
  679. * Any other value is treated as the delimiter value itself
  680. */
  681. typedef struct GdbCmdParseEntry {
  682. GdbCmdHandler handler;
  683. const char *cmd;
  684. bool cmd_startswith;
  685. const char *schema;
  686. bool allow_stop_reply;
  687. } GdbCmdParseEntry;
  688. static inline int startswith(const char *string, const char *pattern)
  689. {
  690. return !strncmp(string, pattern, strlen(pattern));
  691. }
  692. static int process_string_cmd(void *user_ctx, const char *data,
  693. const GdbCmdParseEntry *cmds, int num_cmds)
  694. {
  695. int i;
  696. g_autoptr(GArray) params = g_array_new(false, true, sizeof(GdbCmdVariant));
  697. if (!cmds) {
  698. return -1;
  699. }
  700. for (i = 0; i < num_cmds; i++) {
  701. const GdbCmdParseEntry *cmd = &cmds[i];
  702. g_assert(cmd->handler && cmd->cmd);
  703. if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) ||
  704. (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) {
  705. continue;
  706. }
  707. if (cmd->schema) {
  708. if (cmd_parse_params(&data[strlen(cmd->cmd)],
  709. cmd->schema, params)) {
  710. return -1;
  711. }
  712. }
  713. gdbserver_state.allow_stop_reply = cmd->allow_stop_reply;
  714. cmd->handler(params, user_ctx);
  715. return 0;
  716. }
  717. return -1;
  718. }
  719. static void run_cmd_parser(const char *data, const GdbCmdParseEntry *cmd)
  720. {
  721. if (!data) {
  722. return;
  723. }
  724. g_string_set_size(gdbserver_state.str_buf, 0);
  725. g_byte_array_set_size(gdbserver_state.mem_buf, 0);
  726. /* In case there was an error during the command parsing we must
  727. * send a NULL packet to indicate the command is not supported */
  728. if (process_string_cmd(NULL, data, cmd, 1)) {
  729. gdb_put_packet("");
  730. }
  731. }
  732. static void handle_detach(GArray *params, void *user_ctx)
  733. {
  734. GDBProcess *process;
  735. uint32_t pid = 1;
  736. if (gdbserver_state.multiprocess) {
  737. if (!params->len) {
  738. gdb_put_packet("E22");
  739. return;
  740. }
  741. pid = get_param(params, 0)->val_ul;
  742. }
  743. process = gdb_get_process(pid);
  744. gdb_process_breakpoint_remove_all(process);
  745. process->attached = false;
  746. if (pid == gdb_get_cpu_pid(gdbserver_state.c_cpu)) {
  747. gdbserver_state.c_cpu = gdb_first_attached_cpu();
  748. }
  749. if (pid == gdb_get_cpu_pid(gdbserver_state.g_cpu)) {
  750. gdbserver_state.g_cpu = gdb_first_attached_cpu();
  751. }
  752. if (!gdbserver_state.c_cpu) {
  753. /* No more process attached */
  754. gdb_disable_syscalls();
  755. gdb_continue();
  756. }
  757. gdb_put_packet("OK");
  758. }
  759. static void handle_thread_alive(GArray *params, void *user_ctx)
  760. {
  761. CPUState *cpu;
  762. if (!params->len) {
  763. gdb_put_packet("E22");
  764. return;
  765. }
  766. if (get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
  767. gdb_put_packet("E22");
  768. return;
  769. }
  770. cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid,
  771. get_param(params, 0)->thread_id.tid);
  772. if (!cpu) {
  773. gdb_put_packet("E22");
  774. return;
  775. }
  776. gdb_put_packet("OK");
  777. }
  778. static void handle_continue(GArray *params, void *user_ctx)
  779. {
  780. if (params->len) {
  781. gdb_set_cpu_pc(get_param(params, 0)->val_ull);
  782. }
  783. gdbserver_state.signal = 0;
  784. gdb_continue();
  785. }
  786. static void handle_cont_with_sig(GArray *params, void *user_ctx)
  787. {
  788. unsigned long signal = 0;
  789. /*
  790. * Note: C sig;[addr] is currently unsupported and we simply
  791. * omit the addr parameter
  792. */
  793. if (params->len) {
  794. signal = get_param(params, 0)->val_ul;
  795. }
  796. gdbserver_state.signal = gdb_signal_to_target(signal);
  797. if (gdbserver_state.signal == -1) {
  798. gdbserver_state.signal = 0;
  799. }
  800. gdb_continue();
  801. }
  802. static void handle_set_thread(GArray *params, void *user_ctx)
  803. {
  804. CPUState *cpu;
  805. if (params->len != 2) {
  806. gdb_put_packet("E22");
  807. return;
  808. }
  809. if (get_param(params, 1)->thread_id.kind == GDB_READ_THREAD_ERR) {
  810. gdb_put_packet("E22");
  811. return;
  812. }
  813. if (get_param(params, 1)->thread_id.kind != GDB_ONE_THREAD) {
  814. gdb_put_packet("OK");
  815. return;
  816. }
  817. cpu = gdb_get_cpu(get_param(params, 1)->thread_id.pid,
  818. get_param(params, 1)->thread_id.tid);
  819. if (!cpu) {
  820. gdb_put_packet("E22");
  821. return;
  822. }
  823. /*
  824. * Note: This command is deprecated and modern gdb's will be using the
  825. * vCont command instead.
  826. */
  827. switch (get_param(params, 0)->opcode) {
  828. case 'c':
  829. gdbserver_state.c_cpu = cpu;
  830. gdb_put_packet("OK");
  831. break;
  832. case 'g':
  833. gdbserver_state.g_cpu = cpu;
  834. gdb_put_packet("OK");
  835. break;
  836. default:
  837. gdb_put_packet("E22");
  838. break;
  839. }
  840. }
  841. static void handle_insert_bp(GArray *params, void *user_ctx)
  842. {
  843. int res;
  844. if (params->len != 3) {
  845. gdb_put_packet("E22");
  846. return;
  847. }
  848. res = gdb_breakpoint_insert(gdbserver_state.c_cpu,
  849. get_param(params, 0)->val_ul,
  850. get_param(params, 1)->val_ull,
  851. get_param(params, 2)->val_ull);
  852. if (res >= 0) {
  853. gdb_put_packet("OK");
  854. return;
  855. } else if (res == -ENOSYS) {
  856. gdb_put_packet("");
  857. return;
  858. }
  859. gdb_put_packet("E22");
  860. }
  861. static void handle_remove_bp(GArray *params, void *user_ctx)
  862. {
  863. int res;
  864. if (params->len != 3) {
  865. gdb_put_packet("E22");
  866. return;
  867. }
  868. res = gdb_breakpoint_remove(gdbserver_state.c_cpu,
  869. get_param(params, 0)->val_ul,
  870. get_param(params, 1)->val_ull,
  871. get_param(params, 2)->val_ull);
  872. if (res >= 0) {
  873. gdb_put_packet("OK");
  874. return;
  875. } else if (res == -ENOSYS) {
  876. gdb_put_packet("");
  877. return;
  878. }
  879. gdb_put_packet("E22");
  880. }
  881. /*
  882. * handle_set/get_reg
  883. *
  884. * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
  885. * This works, but can be very slow. Anything new enough to understand
  886. * XML also knows how to use this properly. However to use this we
  887. * need to define a local XML file as well as be talking to a
  888. * reasonably modern gdb. Responding with an empty packet will cause
  889. * the remote gdb to fallback to older methods.
  890. */
  891. static void handle_set_reg(GArray *params, void *user_ctx)
  892. {
  893. int reg_size;
  894. if (!gdb_has_xml) {
  895. gdb_put_packet("");
  896. return;
  897. }
  898. if (params->len != 2) {
  899. gdb_put_packet("E22");
  900. return;
  901. }
  902. reg_size = strlen(get_param(params, 1)->data) / 2;
  903. gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 1)->data, reg_size);
  904. gdb_write_register(gdbserver_state.g_cpu, gdbserver_state.mem_buf->data,
  905. get_param(params, 0)->val_ull);
  906. gdb_put_packet("OK");
  907. }
  908. static void handle_get_reg(GArray *params, void *user_ctx)
  909. {
  910. int reg_size;
  911. if (!gdb_has_xml) {
  912. gdb_put_packet("");
  913. return;
  914. }
  915. if (!params->len) {
  916. gdb_put_packet("E14");
  917. return;
  918. }
  919. reg_size = gdb_read_register(gdbserver_state.g_cpu,
  920. gdbserver_state.mem_buf,
  921. get_param(params, 0)->val_ull);
  922. if (!reg_size) {
  923. gdb_put_packet("E14");
  924. return;
  925. } else {
  926. g_byte_array_set_size(gdbserver_state.mem_buf, reg_size);
  927. }
  928. gdb_memtohex(gdbserver_state.str_buf,
  929. gdbserver_state.mem_buf->data, reg_size);
  930. gdb_put_strbuf();
  931. }
  932. static void handle_write_mem(GArray *params, void *user_ctx)
  933. {
  934. if (params->len != 3) {
  935. gdb_put_packet("E22");
  936. return;
  937. }
  938. /* gdb_hextomem() reads 2*len bytes */
  939. if (get_param(params, 1)->val_ull >
  940. strlen(get_param(params, 2)->data) / 2) {
  941. gdb_put_packet("E22");
  942. return;
  943. }
  944. gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 2)->data,
  945. get_param(params, 1)->val_ull);
  946. if (gdb_target_memory_rw_debug(gdbserver_state.g_cpu,
  947. get_param(params, 0)->val_ull,
  948. gdbserver_state.mem_buf->data,
  949. gdbserver_state.mem_buf->len, true)) {
  950. gdb_put_packet("E14");
  951. return;
  952. }
  953. gdb_put_packet("OK");
  954. }
  955. static void handle_read_mem(GArray *params, void *user_ctx)
  956. {
  957. if (params->len != 2) {
  958. gdb_put_packet("E22");
  959. return;
  960. }
  961. /* gdb_memtohex() doubles the required space */
  962. if (get_param(params, 1)->val_ull > MAX_PACKET_LENGTH / 2) {
  963. gdb_put_packet("E22");
  964. return;
  965. }
  966. g_byte_array_set_size(gdbserver_state.mem_buf,
  967. get_param(params, 1)->val_ull);
  968. if (gdb_target_memory_rw_debug(gdbserver_state.g_cpu,
  969. get_param(params, 0)->val_ull,
  970. gdbserver_state.mem_buf->data,
  971. gdbserver_state.mem_buf->len, false)) {
  972. gdb_put_packet("E14");
  973. return;
  974. }
  975. gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data,
  976. gdbserver_state.mem_buf->len);
  977. gdb_put_strbuf();
  978. }
  979. static void handle_write_all_regs(GArray *params, void *user_ctx)
  980. {
  981. int reg_id;
  982. size_t len;
  983. uint8_t *registers;
  984. int reg_size;
  985. if (!params->len) {
  986. return;
  987. }
  988. cpu_synchronize_state(gdbserver_state.g_cpu);
  989. len = strlen(get_param(params, 0)->data) / 2;
  990. gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 0)->data, len);
  991. registers = gdbserver_state.mem_buf->data;
  992. for (reg_id = 0;
  993. reg_id < gdbserver_state.g_cpu->gdb_num_g_regs && len > 0;
  994. reg_id++) {
  995. reg_size = gdb_write_register(gdbserver_state.g_cpu, registers, reg_id);
  996. len -= reg_size;
  997. registers += reg_size;
  998. }
  999. gdb_put_packet("OK");
  1000. }
  1001. static void handle_read_all_regs(GArray *params, void *user_ctx)
  1002. {
  1003. int reg_id;
  1004. size_t len;
  1005. cpu_synchronize_state(gdbserver_state.g_cpu);
  1006. g_byte_array_set_size(gdbserver_state.mem_buf, 0);
  1007. len = 0;
  1008. for (reg_id = 0; reg_id < gdbserver_state.g_cpu->gdb_num_g_regs; reg_id++) {
  1009. len += gdb_read_register(gdbserver_state.g_cpu,
  1010. gdbserver_state.mem_buf,
  1011. reg_id);
  1012. }
  1013. g_assert(len == gdbserver_state.mem_buf->len);
  1014. gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, len);
  1015. gdb_put_strbuf();
  1016. }
  1017. static void handle_step(GArray *params, void *user_ctx)
  1018. {
  1019. if (params->len) {
  1020. gdb_set_cpu_pc(get_param(params, 0)->val_ull);
  1021. }
  1022. cpu_single_step(gdbserver_state.c_cpu, gdbserver_state.sstep_flags);
  1023. gdb_continue();
  1024. }
  1025. static void handle_backward(GArray *params, void *user_ctx)
  1026. {
  1027. if (!gdb_can_reverse()) {
  1028. gdb_put_packet("E22");
  1029. }
  1030. if (params->len == 1) {
  1031. switch (get_param(params, 0)->opcode) {
  1032. case 's':
  1033. if (replay_reverse_step()) {
  1034. gdb_continue();
  1035. } else {
  1036. gdb_put_packet("E14");
  1037. }
  1038. return;
  1039. case 'c':
  1040. if (replay_reverse_continue()) {
  1041. gdb_continue();
  1042. } else {
  1043. gdb_put_packet("E14");
  1044. }
  1045. return;
  1046. }
  1047. }
  1048. /* Default invalid command */
  1049. gdb_put_packet("");
  1050. }
  1051. static void handle_v_cont_query(GArray *params, void *user_ctx)
  1052. {
  1053. gdb_put_packet("vCont;c;C;s;S");
  1054. }
  1055. static void handle_v_cont(GArray *params, void *user_ctx)
  1056. {
  1057. int res;
  1058. if (!params->len) {
  1059. return;
  1060. }
  1061. res = gdb_handle_vcont(get_param(params, 0)->data);
  1062. if ((res == -EINVAL) || (res == -ERANGE)) {
  1063. gdb_put_packet("E22");
  1064. } else if (res) {
  1065. gdb_put_packet("");
  1066. }
  1067. }
  1068. static void handle_v_attach(GArray *params, void *user_ctx)
  1069. {
  1070. GDBProcess *process;
  1071. CPUState *cpu;
  1072. g_string_assign(gdbserver_state.str_buf, "E22");
  1073. if (!params->len) {
  1074. goto cleanup;
  1075. }
  1076. process = gdb_get_process(get_param(params, 0)->val_ul);
  1077. if (!process) {
  1078. goto cleanup;
  1079. }
  1080. cpu = get_first_cpu_in_process(process);
  1081. if (!cpu) {
  1082. goto cleanup;
  1083. }
  1084. process->attached = true;
  1085. gdbserver_state.g_cpu = cpu;
  1086. gdbserver_state.c_cpu = cpu;
  1087. if (gdbserver_state.allow_stop_reply) {
  1088. g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
  1089. gdb_append_thread_id(cpu, gdbserver_state.str_buf);
  1090. g_string_append_c(gdbserver_state.str_buf, ';');
  1091. gdbserver_state.allow_stop_reply = false;
  1092. cleanup:
  1093. gdb_put_strbuf();
  1094. }
  1095. }
  1096. static void handle_v_kill(GArray *params, void *user_ctx)
  1097. {
  1098. /* Kill the target */
  1099. gdb_put_packet("OK");
  1100. error_report("QEMU: Terminated via GDBstub");
  1101. gdb_exit(0);
  1102. exit(0);
  1103. }
  1104. static const GdbCmdParseEntry gdb_v_commands_table[] = {
  1105. /* Order is important if has same prefix */
  1106. {
  1107. .handler = handle_v_cont_query,
  1108. .cmd = "Cont?",
  1109. .cmd_startswith = 1
  1110. },
  1111. {
  1112. .handler = handle_v_cont,
  1113. .cmd = "Cont",
  1114. .cmd_startswith = 1,
  1115. .allow_stop_reply = true,
  1116. .schema = "s0"
  1117. },
  1118. {
  1119. .handler = handle_v_attach,
  1120. .cmd = "Attach;",
  1121. .cmd_startswith = 1,
  1122. .allow_stop_reply = true,
  1123. .schema = "l0"
  1124. },
  1125. {
  1126. .handler = handle_v_kill,
  1127. .cmd = "Kill;",
  1128. .cmd_startswith = 1
  1129. },
  1130. };
  1131. static void handle_v_commands(GArray *params, void *user_ctx)
  1132. {
  1133. if (!params->len) {
  1134. return;
  1135. }
  1136. if (process_string_cmd(NULL, get_param(params, 0)->data,
  1137. gdb_v_commands_table,
  1138. ARRAY_SIZE(gdb_v_commands_table))) {
  1139. gdb_put_packet("");
  1140. }
  1141. }
  1142. static void handle_query_qemu_sstepbits(GArray *params, void *user_ctx)
  1143. {
  1144. g_string_printf(gdbserver_state.str_buf, "ENABLE=%x", SSTEP_ENABLE);
  1145. if (gdbserver_state.supported_sstep_flags & SSTEP_NOIRQ) {
  1146. g_string_append_printf(gdbserver_state.str_buf, ",NOIRQ=%x",
  1147. SSTEP_NOIRQ);
  1148. }
  1149. if (gdbserver_state.supported_sstep_flags & SSTEP_NOTIMER) {
  1150. g_string_append_printf(gdbserver_state.str_buf, ",NOTIMER=%x",
  1151. SSTEP_NOTIMER);
  1152. }
  1153. gdb_put_strbuf();
  1154. }
  1155. static void handle_set_qemu_sstep(GArray *params, void *user_ctx)
  1156. {
  1157. int new_sstep_flags;
  1158. if (!params->len) {
  1159. return;
  1160. }
  1161. new_sstep_flags = get_param(params, 0)->val_ul;
  1162. if (new_sstep_flags & ~gdbserver_state.supported_sstep_flags) {
  1163. gdb_put_packet("E22");
  1164. return;
  1165. }
  1166. gdbserver_state.sstep_flags = new_sstep_flags;
  1167. gdb_put_packet("OK");
  1168. }
  1169. static void handle_query_qemu_sstep(GArray *params, void *user_ctx)
  1170. {
  1171. g_string_printf(gdbserver_state.str_buf, "0x%x",
  1172. gdbserver_state.sstep_flags);
  1173. gdb_put_strbuf();
  1174. }
  1175. static void handle_query_curr_tid(GArray *params, void *user_ctx)
  1176. {
  1177. CPUState *cpu;
  1178. GDBProcess *process;
  1179. /*
  1180. * "Current thread" remains vague in the spec, so always return
  1181. * the first thread of the current process (gdb returns the
  1182. * first thread).
  1183. */
  1184. process = gdb_get_cpu_process(gdbserver_state.g_cpu);
  1185. cpu = get_first_cpu_in_process(process);
  1186. g_string_assign(gdbserver_state.str_buf, "QC");
  1187. gdb_append_thread_id(cpu, gdbserver_state.str_buf);
  1188. gdb_put_strbuf();
  1189. }
  1190. static void handle_query_threads(GArray *params, void *user_ctx)
  1191. {
  1192. if (!gdbserver_state.query_cpu) {
  1193. gdb_put_packet("l");
  1194. return;
  1195. }
  1196. g_string_assign(gdbserver_state.str_buf, "m");
  1197. gdb_append_thread_id(gdbserver_state.query_cpu, gdbserver_state.str_buf);
  1198. gdb_put_strbuf();
  1199. gdbserver_state.query_cpu = gdb_next_attached_cpu(gdbserver_state.query_cpu);
  1200. }
  1201. static void handle_query_first_threads(GArray *params, void *user_ctx)
  1202. {
  1203. gdbserver_state.query_cpu = gdb_first_attached_cpu();
  1204. handle_query_threads(params, user_ctx);
  1205. }
  1206. static void handle_query_thread_extra(GArray *params, void *user_ctx)
  1207. {
  1208. g_autoptr(GString) rs = g_string_new(NULL);
  1209. CPUState *cpu;
  1210. if (!params->len ||
  1211. get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
  1212. gdb_put_packet("E22");
  1213. return;
  1214. }
  1215. cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid,
  1216. get_param(params, 0)->thread_id.tid);
  1217. if (!cpu) {
  1218. return;
  1219. }
  1220. cpu_synchronize_state(cpu);
  1221. if (gdbserver_state.multiprocess && (gdbserver_state.process_num > 1)) {
  1222. /* Print the CPU model and name in multiprocess mode */
  1223. ObjectClass *oc = object_get_class(OBJECT(cpu));
  1224. const char *cpu_model = object_class_get_name(oc);
  1225. const char *cpu_name =
  1226. object_get_canonical_path_component(OBJECT(cpu));
  1227. g_string_printf(rs, "%s %s [%s]", cpu_model, cpu_name,
  1228. cpu->halted ? "halted " : "running");
  1229. } else {
  1230. g_string_printf(rs, "CPU#%d [%s]", cpu->cpu_index,
  1231. cpu->halted ? "halted " : "running");
  1232. }
  1233. trace_gdbstub_op_extra_info(rs->str);
  1234. gdb_memtohex(gdbserver_state.str_buf, (uint8_t *)rs->str, rs->len);
  1235. gdb_put_strbuf();
  1236. }
  1237. static void handle_query_supported(GArray *params, void *user_ctx)
  1238. {
  1239. CPUClass *cc;
  1240. g_string_printf(gdbserver_state.str_buf, "PacketSize=%x", MAX_PACKET_LENGTH);
  1241. cc = CPU_GET_CLASS(first_cpu);
  1242. if (cc->gdb_core_xml_file) {
  1243. g_string_append(gdbserver_state.str_buf, ";qXfer:features:read+");
  1244. }
  1245. if (gdb_can_reverse()) {
  1246. g_string_append(gdbserver_state.str_buf,
  1247. ";ReverseStep+;ReverseContinue+");
  1248. }
  1249. #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX)
  1250. if (gdbserver_state.c_cpu->opaque) {
  1251. g_string_append(gdbserver_state.str_buf, ";qXfer:auxv:read+");
  1252. }
  1253. #endif
  1254. if (params->len &&
  1255. strstr(get_param(params, 0)->data, "multiprocess+")) {
  1256. gdbserver_state.multiprocess = true;
  1257. }
  1258. g_string_append(gdbserver_state.str_buf, ";vContSupported+;multiprocess+");
  1259. gdb_put_strbuf();
  1260. }
  1261. static void handle_query_xfer_features(GArray *params, void *user_ctx)
  1262. {
  1263. GDBProcess *process;
  1264. CPUClass *cc;
  1265. unsigned long len, total_len, addr;
  1266. const char *xml;
  1267. const char *p;
  1268. if (params->len < 3) {
  1269. gdb_put_packet("E22");
  1270. return;
  1271. }
  1272. process = gdb_get_cpu_process(gdbserver_state.g_cpu);
  1273. cc = CPU_GET_CLASS(gdbserver_state.g_cpu);
  1274. if (!cc->gdb_core_xml_file) {
  1275. gdb_put_packet("");
  1276. return;
  1277. }
  1278. gdb_has_xml = true;
  1279. p = get_param(params, 0)->data;
  1280. xml = get_feature_xml(p, &p, process);
  1281. if (!xml) {
  1282. gdb_put_packet("E00");
  1283. return;
  1284. }
  1285. addr = get_param(params, 1)->val_ul;
  1286. len = get_param(params, 2)->val_ul;
  1287. total_len = strlen(xml);
  1288. if (addr > total_len) {
  1289. gdb_put_packet("E00");
  1290. return;
  1291. }
  1292. if (len > (MAX_PACKET_LENGTH - 5) / 2) {
  1293. len = (MAX_PACKET_LENGTH - 5) / 2;
  1294. }
  1295. if (len < total_len - addr) {
  1296. g_string_assign(gdbserver_state.str_buf, "m");
  1297. gdb_memtox(gdbserver_state.str_buf, xml + addr, len);
  1298. } else {
  1299. g_string_assign(gdbserver_state.str_buf, "l");
  1300. gdb_memtox(gdbserver_state.str_buf, xml + addr, total_len - addr);
  1301. }
  1302. gdb_put_packet_binary(gdbserver_state.str_buf->str,
  1303. gdbserver_state.str_buf->len, true);
  1304. }
  1305. static void handle_query_qemu_supported(GArray *params, void *user_ctx)
  1306. {
  1307. g_string_printf(gdbserver_state.str_buf, "sstepbits;sstep");
  1308. #ifndef CONFIG_USER_ONLY
  1309. g_string_append(gdbserver_state.str_buf, ";PhyMemMode");
  1310. #endif
  1311. gdb_put_strbuf();
  1312. }
  1313. static const GdbCmdParseEntry gdb_gen_query_set_common_table[] = {
  1314. /* Order is important if has same prefix */
  1315. {
  1316. .handler = handle_query_qemu_sstepbits,
  1317. .cmd = "qemu.sstepbits",
  1318. },
  1319. {
  1320. .handler = handle_query_qemu_sstep,
  1321. .cmd = "qemu.sstep",
  1322. },
  1323. {
  1324. .handler = handle_set_qemu_sstep,
  1325. .cmd = "qemu.sstep=",
  1326. .cmd_startswith = 1,
  1327. .schema = "l0"
  1328. },
  1329. };
  1330. static const GdbCmdParseEntry gdb_gen_query_table[] = {
  1331. {
  1332. .handler = handle_query_curr_tid,
  1333. .cmd = "C",
  1334. },
  1335. {
  1336. .handler = handle_query_threads,
  1337. .cmd = "sThreadInfo",
  1338. },
  1339. {
  1340. .handler = handle_query_first_threads,
  1341. .cmd = "fThreadInfo",
  1342. },
  1343. {
  1344. .handler = handle_query_thread_extra,
  1345. .cmd = "ThreadExtraInfo,",
  1346. .cmd_startswith = 1,
  1347. .schema = "t0"
  1348. },
  1349. #ifdef CONFIG_USER_ONLY
  1350. {
  1351. .handler = gdb_handle_query_offsets,
  1352. .cmd = "Offsets",
  1353. },
  1354. #else
  1355. {
  1356. .handler = gdb_handle_query_rcmd,
  1357. .cmd = "Rcmd,",
  1358. .cmd_startswith = 1,
  1359. .schema = "s0"
  1360. },
  1361. #endif
  1362. {
  1363. .handler = handle_query_supported,
  1364. .cmd = "Supported:",
  1365. .cmd_startswith = 1,
  1366. .schema = "s0"
  1367. },
  1368. {
  1369. .handler = handle_query_supported,
  1370. .cmd = "Supported",
  1371. .schema = "s0"
  1372. },
  1373. {
  1374. .handler = handle_query_xfer_features,
  1375. .cmd = "Xfer:features:read:",
  1376. .cmd_startswith = 1,
  1377. .schema = "s:l,l0"
  1378. },
  1379. #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX)
  1380. {
  1381. .handler = gdb_handle_query_xfer_auxv,
  1382. .cmd = "Xfer:auxv:read::",
  1383. .cmd_startswith = 1,
  1384. .schema = "l,l0"
  1385. },
  1386. #endif
  1387. {
  1388. .handler = gdb_handle_query_attached,
  1389. .cmd = "Attached:",
  1390. .cmd_startswith = 1
  1391. },
  1392. {
  1393. .handler = gdb_handle_query_attached,
  1394. .cmd = "Attached",
  1395. },
  1396. {
  1397. .handler = handle_query_qemu_supported,
  1398. .cmd = "qemu.Supported",
  1399. },
  1400. #ifndef CONFIG_USER_ONLY
  1401. {
  1402. .handler = gdb_handle_query_qemu_phy_mem_mode,
  1403. .cmd = "qemu.PhyMemMode",
  1404. },
  1405. #endif
  1406. };
  1407. static const GdbCmdParseEntry gdb_gen_set_table[] = {
  1408. /* Order is important if has same prefix */
  1409. {
  1410. .handler = handle_set_qemu_sstep,
  1411. .cmd = "qemu.sstep:",
  1412. .cmd_startswith = 1,
  1413. .schema = "l0"
  1414. },
  1415. #ifndef CONFIG_USER_ONLY
  1416. {
  1417. .handler = gdb_handle_set_qemu_phy_mem_mode,
  1418. .cmd = "qemu.PhyMemMode:",
  1419. .cmd_startswith = 1,
  1420. .schema = "l0"
  1421. },
  1422. #endif
  1423. };
  1424. static void handle_gen_query(GArray *params, void *user_ctx)
  1425. {
  1426. if (!params->len) {
  1427. return;
  1428. }
  1429. if (!process_string_cmd(NULL, get_param(params, 0)->data,
  1430. gdb_gen_query_set_common_table,
  1431. ARRAY_SIZE(gdb_gen_query_set_common_table))) {
  1432. return;
  1433. }
  1434. if (process_string_cmd(NULL, get_param(params, 0)->data,
  1435. gdb_gen_query_table,
  1436. ARRAY_SIZE(gdb_gen_query_table))) {
  1437. gdb_put_packet("");
  1438. }
  1439. }
  1440. static void handle_gen_set(GArray *params, void *user_ctx)
  1441. {
  1442. if (!params->len) {
  1443. return;
  1444. }
  1445. if (!process_string_cmd(NULL, get_param(params, 0)->data,
  1446. gdb_gen_query_set_common_table,
  1447. ARRAY_SIZE(gdb_gen_query_set_common_table))) {
  1448. return;
  1449. }
  1450. if (process_string_cmd(NULL, get_param(params, 0)->data,
  1451. gdb_gen_set_table,
  1452. ARRAY_SIZE(gdb_gen_set_table))) {
  1453. gdb_put_packet("");
  1454. }
  1455. }
  1456. static void handle_target_halt(GArray *params, void *user_ctx)
  1457. {
  1458. if (gdbserver_state.allow_stop_reply) {
  1459. g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
  1460. gdb_append_thread_id(gdbserver_state.c_cpu, gdbserver_state.str_buf);
  1461. g_string_append_c(gdbserver_state.str_buf, ';');
  1462. gdb_put_strbuf();
  1463. gdbserver_state.allow_stop_reply = false;
  1464. }
  1465. /*
  1466. * Remove all the breakpoints when this query is issued,
  1467. * because gdb is doing an initial connect and the state
  1468. * should be cleaned up.
  1469. */
  1470. gdb_breakpoint_remove_all(gdbserver_state.c_cpu);
  1471. }
  1472. static int gdb_handle_packet(const char *line_buf)
  1473. {
  1474. const GdbCmdParseEntry *cmd_parser = NULL;
  1475. trace_gdbstub_io_command(line_buf);
  1476. switch (line_buf[0]) {
  1477. case '!':
  1478. gdb_put_packet("OK");
  1479. break;
  1480. case '?':
  1481. {
  1482. static const GdbCmdParseEntry target_halted_cmd_desc = {
  1483. .handler = handle_target_halt,
  1484. .cmd = "?",
  1485. .cmd_startswith = 1,
  1486. .allow_stop_reply = true,
  1487. };
  1488. cmd_parser = &target_halted_cmd_desc;
  1489. }
  1490. break;
  1491. case 'c':
  1492. {
  1493. static const GdbCmdParseEntry continue_cmd_desc = {
  1494. .handler = handle_continue,
  1495. .cmd = "c",
  1496. .cmd_startswith = 1,
  1497. .allow_stop_reply = true,
  1498. .schema = "L0"
  1499. };
  1500. cmd_parser = &continue_cmd_desc;
  1501. }
  1502. break;
  1503. case 'C':
  1504. {
  1505. static const GdbCmdParseEntry cont_with_sig_cmd_desc = {
  1506. .handler = handle_cont_with_sig,
  1507. .cmd = "C",
  1508. .cmd_startswith = 1,
  1509. .allow_stop_reply = true,
  1510. .schema = "l0"
  1511. };
  1512. cmd_parser = &cont_with_sig_cmd_desc;
  1513. }
  1514. break;
  1515. case 'v':
  1516. {
  1517. static const GdbCmdParseEntry v_cmd_desc = {
  1518. .handler = handle_v_commands,
  1519. .cmd = "v",
  1520. .cmd_startswith = 1,
  1521. .schema = "s0"
  1522. };
  1523. cmd_parser = &v_cmd_desc;
  1524. }
  1525. break;
  1526. case 'k':
  1527. /* Kill the target */
  1528. error_report("QEMU: Terminated via GDBstub");
  1529. gdb_exit(0);
  1530. exit(0);
  1531. case 'D':
  1532. {
  1533. static const GdbCmdParseEntry detach_cmd_desc = {
  1534. .handler = handle_detach,
  1535. .cmd = "D",
  1536. .cmd_startswith = 1,
  1537. .schema = "?.l0"
  1538. };
  1539. cmd_parser = &detach_cmd_desc;
  1540. }
  1541. break;
  1542. case 's':
  1543. {
  1544. static const GdbCmdParseEntry step_cmd_desc = {
  1545. .handler = handle_step,
  1546. .cmd = "s",
  1547. .cmd_startswith = 1,
  1548. .allow_stop_reply = true,
  1549. .schema = "L0"
  1550. };
  1551. cmd_parser = &step_cmd_desc;
  1552. }
  1553. break;
  1554. case 'b':
  1555. {
  1556. static const GdbCmdParseEntry backward_cmd_desc = {
  1557. .handler = handle_backward,
  1558. .cmd = "b",
  1559. .cmd_startswith = 1,
  1560. .schema = "o0"
  1561. };
  1562. cmd_parser = &backward_cmd_desc;
  1563. }
  1564. break;
  1565. case 'F':
  1566. {
  1567. static const GdbCmdParseEntry file_io_cmd_desc = {
  1568. .handler = gdb_handle_file_io,
  1569. .cmd = "F",
  1570. .cmd_startswith = 1,
  1571. .schema = "L,L,o0"
  1572. };
  1573. cmd_parser = &file_io_cmd_desc;
  1574. }
  1575. break;
  1576. case 'g':
  1577. {
  1578. static const GdbCmdParseEntry read_all_regs_cmd_desc = {
  1579. .handler = handle_read_all_regs,
  1580. .cmd = "g",
  1581. .cmd_startswith = 1
  1582. };
  1583. cmd_parser = &read_all_regs_cmd_desc;
  1584. }
  1585. break;
  1586. case 'G':
  1587. {
  1588. static const GdbCmdParseEntry write_all_regs_cmd_desc = {
  1589. .handler = handle_write_all_regs,
  1590. .cmd = "G",
  1591. .cmd_startswith = 1,
  1592. .schema = "s0"
  1593. };
  1594. cmd_parser = &write_all_regs_cmd_desc;
  1595. }
  1596. break;
  1597. case 'm':
  1598. {
  1599. static const GdbCmdParseEntry read_mem_cmd_desc = {
  1600. .handler = handle_read_mem,
  1601. .cmd = "m",
  1602. .cmd_startswith = 1,
  1603. .schema = "L,L0"
  1604. };
  1605. cmd_parser = &read_mem_cmd_desc;
  1606. }
  1607. break;
  1608. case 'M':
  1609. {
  1610. static const GdbCmdParseEntry write_mem_cmd_desc = {
  1611. .handler = handle_write_mem,
  1612. .cmd = "M",
  1613. .cmd_startswith = 1,
  1614. .schema = "L,L:s0"
  1615. };
  1616. cmd_parser = &write_mem_cmd_desc;
  1617. }
  1618. break;
  1619. case 'p':
  1620. {
  1621. static const GdbCmdParseEntry get_reg_cmd_desc = {
  1622. .handler = handle_get_reg,
  1623. .cmd = "p",
  1624. .cmd_startswith = 1,
  1625. .schema = "L0"
  1626. };
  1627. cmd_parser = &get_reg_cmd_desc;
  1628. }
  1629. break;
  1630. case 'P':
  1631. {
  1632. static const GdbCmdParseEntry set_reg_cmd_desc = {
  1633. .handler = handle_set_reg,
  1634. .cmd = "P",
  1635. .cmd_startswith = 1,
  1636. .schema = "L?s0"
  1637. };
  1638. cmd_parser = &set_reg_cmd_desc;
  1639. }
  1640. break;
  1641. case 'Z':
  1642. {
  1643. static const GdbCmdParseEntry insert_bp_cmd_desc = {
  1644. .handler = handle_insert_bp,
  1645. .cmd = "Z",
  1646. .cmd_startswith = 1,
  1647. .schema = "l?L?L0"
  1648. };
  1649. cmd_parser = &insert_bp_cmd_desc;
  1650. }
  1651. break;
  1652. case 'z':
  1653. {
  1654. static const GdbCmdParseEntry remove_bp_cmd_desc = {
  1655. .handler = handle_remove_bp,
  1656. .cmd = "z",
  1657. .cmd_startswith = 1,
  1658. .schema = "l?L?L0"
  1659. };
  1660. cmd_parser = &remove_bp_cmd_desc;
  1661. }
  1662. break;
  1663. case 'H':
  1664. {
  1665. static const GdbCmdParseEntry set_thread_cmd_desc = {
  1666. .handler = handle_set_thread,
  1667. .cmd = "H",
  1668. .cmd_startswith = 1,
  1669. .schema = "o.t0"
  1670. };
  1671. cmd_parser = &set_thread_cmd_desc;
  1672. }
  1673. break;
  1674. case 'T':
  1675. {
  1676. static const GdbCmdParseEntry thread_alive_cmd_desc = {
  1677. .handler = handle_thread_alive,
  1678. .cmd = "T",
  1679. .cmd_startswith = 1,
  1680. .schema = "t0"
  1681. };
  1682. cmd_parser = &thread_alive_cmd_desc;
  1683. }
  1684. break;
  1685. case 'q':
  1686. {
  1687. static const GdbCmdParseEntry gen_query_cmd_desc = {
  1688. .handler = handle_gen_query,
  1689. .cmd = "q",
  1690. .cmd_startswith = 1,
  1691. .schema = "s0"
  1692. };
  1693. cmd_parser = &gen_query_cmd_desc;
  1694. }
  1695. break;
  1696. case 'Q':
  1697. {
  1698. static const GdbCmdParseEntry gen_set_cmd_desc = {
  1699. .handler = handle_gen_set,
  1700. .cmd = "Q",
  1701. .cmd_startswith = 1,
  1702. .schema = "s0"
  1703. };
  1704. cmd_parser = &gen_set_cmd_desc;
  1705. }
  1706. break;
  1707. default:
  1708. /* put empty packet */
  1709. gdb_put_packet("");
  1710. break;
  1711. }
  1712. if (cmd_parser) {
  1713. run_cmd_parser(line_buf, cmd_parser);
  1714. }
  1715. return RS_IDLE;
  1716. }
  1717. void gdb_set_stop_cpu(CPUState *cpu)
  1718. {
  1719. GDBProcess *p = gdb_get_cpu_process(cpu);
  1720. if (!p->attached) {
  1721. /*
  1722. * Having a stop CPU corresponding to a process that is not attached
  1723. * confuses GDB. So we ignore the request.
  1724. */
  1725. return;
  1726. }
  1727. gdbserver_state.c_cpu = cpu;
  1728. gdbserver_state.g_cpu = cpu;
  1729. }
  1730. void gdb_read_byte(uint8_t ch)
  1731. {
  1732. uint8_t reply;
  1733. gdbserver_state.allow_stop_reply = false;
  1734. #ifndef CONFIG_USER_ONLY
  1735. if (gdbserver_state.last_packet->len) {
  1736. /* Waiting for a response to the last packet. If we see the start
  1737. of a new command then abandon the previous response. */
  1738. if (ch == '-') {
  1739. trace_gdbstub_err_got_nack();
  1740. gdb_put_buffer(gdbserver_state.last_packet->data,
  1741. gdbserver_state.last_packet->len);
  1742. } else if (ch == '+') {
  1743. trace_gdbstub_io_got_ack();
  1744. } else {
  1745. trace_gdbstub_io_got_unexpected(ch);
  1746. }
  1747. if (ch == '+' || ch == '$') {
  1748. g_byte_array_set_size(gdbserver_state.last_packet, 0);
  1749. }
  1750. if (ch != '$')
  1751. return;
  1752. }
  1753. if (runstate_is_running()) {
  1754. /* when the CPU is running, we cannot do anything except stop
  1755. it when receiving a char */
  1756. vm_stop(RUN_STATE_PAUSED);
  1757. } else
  1758. #endif
  1759. {
  1760. switch(gdbserver_state.state) {
  1761. case RS_IDLE:
  1762. if (ch == '$') {
  1763. /* start of command packet */
  1764. gdbserver_state.line_buf_index = 0;
  1765. gdbserver_state.line_sum = 0;
  1766. gdbserver_state.state = RS_GETLINE;
  1767. } else {
  1768. trace_gdbstub_err_garbage(ch);
  1769. }
  1770. break;
  1771. case RS_GETLINE:
  1772. if (ch == '}') {
  1773. /* start escape sequence */
  1774. gdbserver_state.state = RS_GETLINE_ESC;
  1775. gdbserver_state.line_sum += ch;
  1776. } else if (ch == '*') {
  1777. /* start run length encoding sequence */
  1778. gdbserver_state.state = RS_GETLINE_RLE;
  1779. gdbserver_state.line_sum += ch;
  1780. } else if (ch == '#') {
  1781. /* end of command, start of checksum*/
  1782. gdbserver_state.state = RS_CHKSUM1;
  1783. } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
  1784. trace_gdbstub_err_overrun();
  1785. gdbserver_state.state = RS_IDLE;
  1786. } else {
  1787. /* unescaped command character */
  1788. gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch;
  1789. gdbserver_state.line_sum += ch;
  1790. }
  1791. break;
  1792. case RS_GETLINE_ESC:
  1793. if (ch == '#') {
  1794. /* unexpected end of command in escape sequence */
  1795. gdbserver_state.state = RS_CHKSUM1;
  1796. } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
  1797. /* command buffer overrun */
  1798. trace_gdbstub_err_overrun();
  1799. gdbserver_state.state = RS_IDLE;
  1800. } else {
  1801. /* parse escaped character and leave escape state */
  1802. gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch ^ 0x20;
  1803. gdbserver_state.line_sum += ch;
  1804. gdbserver_state.state = RS_GETLINE;
  1805. }
  1806. break;
  1807. case RS_GETLINE_RLE:
  1808. /*
  1809. * Run-length encoding is explained in "Debugging with GDB /
  1810. * Appendix E GDB Remote Serial Protocol / Overview".
  1811. */
  1812. if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
  1813. /* invalid RLE count encoding */
  1814. trace_gdbstub_err_invalid_repeat(ch);
  1815. gdbserver_state.state = RS_GETLINE;
  1816. } else {
  1817. /* decode repeat length */
  1818. int repeat = ch - ' ' + 3;
  1819. if (gdbserver_state.line_buf_index + repeat >= sizeof(gdbserver_state.line_buf) - 1) {
  1820. /* that many repeats would overrun the command buffer */
  1821. trace_gdbstub_err_overrun();
  1822. gdbserver_state.state = RS_IDLE;
  1823. } else if (gdbserver_state.line_buf_index < 1) {
  1824. /* got a repeat but we have nothing to repeat */
  1825. trace_gdbstub_err_invalid_rle();
  1826. gdbserver_state.state = RS_GETLINE;
  1827. } else {
  1828. /* repeat the last character */
  1829. memset(gdbserver_state.line_buf + gdbserver_state.line_buf_index,
  1830. gdbserver_state.line_buf[gdbserver_state.line_buf_index - 1], repeat);
  1831. gdbserver_state.line_buf_index += repeat;
  1832. gdbserver_state.line_sum += ch;
  1833. gdbserver_state.state = RS_GETLINE;
  1834. }
  1835. }
  1836. break;
  1837. case RS_CHKSUM1:
  1838. /* get high hex digit of checksum */
  1839. if (!isxdigit(ch)) {
  1840. trace_gdbstub_err_checksum_invalid(ch);
  1841. gdbserver_state.state = RS_GETLINE;
  1842. break;
  1843. }
  1844. gdbserver_state.line_buf[gdbserver_state.line_buf_index] = '\0';
  1845. gdbserver_state.line_csum = fromhex(ch) << 4;
  1846. gdbserver_state.state = RS_CHKSUM2;
  1847. break;
  1848. case RS_CHKSUM2:
  1849. /* get low hex digit of checksum */
  1850. if (!isxdigit(ch)) {
  1851. trace_gdbstub_err_checksum_invalid(ch);
  1852. gdbserver_state.state = RS_GETLINE;
  1853. break;
  1854. }
  1855. gdbserver_state.line_csum |= fromhex(ch);
  1856. if (gdbserver_state.line_csum != (gdbserver_state.line_sum & 0xff)) {
  1857. trace_gdbstub_err_checksum_incorrect(gdbserver_state.line_sum, gdbserver_state.line_csum);
  1858. /* send NAK reply */
  1859. reply = '-';
  1860. gdb_put_buffer(&reply, 1);
  1861. gdbserver_state.state = RS_IDLE;
  1862. } else {
  1863. /* send ACK reply */
  1864. reply = '+';
  1865. gdb_put_buffer(&reply, 1);
  1866. gdbserver_state.state = gdb_handle_packet(gdbserver_state.line_buf);
  1867. }
  1868. break;
  1869. default:
  1870. abort();
  1871. }
  1872. }
  1873. }
  1874. /*
  1875. * Create the process that will contain all the "orphan" CPUs (that are not
  1876. * part of a CPU cluster). Note that if this process contains no CPUs, it won't
  1877. * be attachable and thus will be invisible to the user.
  1878. */
  1879. void gdb_create_default_process(GDBState *s)
  1880. {
  1881. GDBProcess *process;
  1882. int max_pid = 0;
  1883. if (gdbserver_state.process_num) {
  1884. max_pid = s->processes[s->process_num - 1].pid;
  1885. }
  1886. s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
  1887. process = &s->processes[s->process_num - 1];
  1888. /* We need an available PID slot for this process */
  1889. assert(max_pid < UINT32_MAX);
  1890. process->pid = max_pid + 1;
  1891. process->attached = false;
  1892. process->target_xml[0] = '\0';
  1893. }