gdbstub.c 61 KB

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