gdbstub.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146
  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. *
  658. * schema definitions:
  659. * Each schema parameter entry consists of 2 chars,
  660. * the first char represents the parameter type handling
  661. * the second char represents the delimiter for the next parameter
  662. *
  663. * Currently supported schema types:
  664. * 'l' -> unsigned long (stored in .val_ul)
  665. * 'L' -> unsigned long long (stored in .val_ull)
  666. * 's' -> string (stored in .data)
  667. * 'o' -> single char (stored in .opcode)
  668. * 't' -> thread id (stored in .thread_id)
  669. * '?' -> skip according to delimiter
  670. *
  671. * Currently supported delimiters:
  672. * '?' -> Stop at any delimiter (",;:=\0")
  673. * '0' -> Stop at "\0"
  674. * '.' -> Skip 1 char unless reached "\0"
  675. * Any other value is treated as the delimiter value itself
  676. */
  677. typedef struct GdbCmdParseEntry {
  678. GdbCmdHandler handler;
  679. const char *cmd;
  680. bool cmd_startswith;
  681. const char *schema;
  682. } GdbCmdParseEntry;
  683. static inline int startswith(const char *string, const char *pattern)
  684. {
  685. return !strncmp(string, pattern, strlen(pattern));
  686. }
  687. static int process_string_cmd(void *user_ctx, const char *data,
  688. const GdbCmdParseEntry *cmds, int num_cmds)
  689. {
  690. int i;
  691. g_autoptr(GArray) params = g_array_new(false, true, sizeof(GdbCmdVariant));
  692. if (!cmds) {
  693. return -1;
  694. }
  695. for (i = 0; i < num_cmds; i++) {
  696. const GdbCmdParseEntry *cmd = &cmds[i];
  697. g_assert(cmd->handler && cmd->cmd);
  698. if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) ||
  699. (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) {
  700. continue;
  701. }
  702. if (cmd->schema) {
  703. if (cmd_parse_params(&data[strlen(cmd->cmd)],
  704. cmd->schema, params)) {
  705. return -1;
  706. }
  707. }
  708. cmd->handler(params, user_ctx);
  709. return 0;
  710. }
  711. return -1;
  712. }
  713. static void run_cmd_parser(const char *data, const GdbCmdParseEntry *cmd)
  714. {
  715. if (!data) {
  716. return;
  717. }
  718. g_string_set_size(gdbserver_state.str_buf, 0);
  719. g_byte_array_set_size(gdbserver_state.mem_buf, 0);
  720. /* In case there was an error during the command parsing we must
  721. * send a NULL packet to indicate the command is not supported */
  722. if (process_string_cmd(NULL, data, cmd, 1)) {
  723. gdb_put_packet("");
  724. }
  725. }
  726. static void handle_detach(GArray *params, void *user_ctx)
  727. {
  728. GDBProcess *process;
  729. uint32_t pid = 1;
  730. if (gdbserver_state.multiprocess) {
  731. if (!params->len) {
  732. gdb_put_packet("E22");
  733. return;
  734. }
  735. pid = get_param(params, 0)->val_ul;
  736. }
  737. process = gdb_get_process(pid);
  738. gdb_process_breakpoint_remove_all(process);
  739. process->attached = false;
  740. if (pid == gdb_get_cpu_pid(gdbserver_state.c_cpu)) {
  741. gdbserver_state.c_cpu = gdb_first_attached_cpu();
  742. }
  743. if (pid == gdb_get_cpu_pid(gdbserver_state.g_cpu)) {
  744. gdbserver_state.g_cpu = gdb_first_attached_cpu();
  745. }
  746. if (!gdbserver_state.c_cpu) {
  747. /* No more process attached */
  748. gdb_disable_syscalls();
  749. gdb_continue();
  750. }
  751. gdb_put_packet("OK");
  752. }
  753. static void handle_thread_alive(GArray *params, void *user_ctx)
  754. {
  755. CPUState *cpu;
  756. if (!params->len) {
  757. gdb_put_packet("E22");
  758. return;
  759. }
  760. if (get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
  761. gdb_put_packet("E22");
  762. return;
  763. }
  764. cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid,
  765. get_param(params, 0)->thread_id.tid);
  766. if (!cpu) {
  767. gdb_put_packet("E22");
  768. return;
  769. }
  770. gdb_put_packet("OK");
  771. }
  772. static void handle_continue(GArray *params, void *user_ctx)
  773. {
  774. if (params->len) {
  775. gdb_set_cpu_pc(get_param(params, 0)->val_ull);
  776. }
  777. gdbserver_state.signal = 0;
  778. gdb_continue();
  779. }
  780. static void handle_cont_with_sig(GArray *params, void *user_ctx)
  781. {
  782. unsigned long signal = 0;
  783. /*
  784. * Note: C sig;[addr] is currently unsupported and we simply
  785. * omit the addr parameter
  786. */
  787. if (params->len) {
  788. signal = get_param(params, 0)->val_ul;
  789. }
  790. gdbserver_state.signal = gdb_signal_to_target(signal);
  791. if (gdbserver_state.signal == -1) {
  792. gdbserver_state.signal = 0;
  793. }
  794. gdb_continue();
  795. }
  796. static void handle_set_thread(GArray *params, void *user_ctx)
  797. {
  798. CPUState *cpu;
  799. if (params->len != 2) {
  800. gdb_put_packet("E22");
  801. return;
  802. }
  803. if (get_param(params, 1)->thread_id.kind == GDB_READ_THREAD_ERR) {
  804. gdb_put_packet("E22");
  805. return;
  806. }
  807. if (get_param(params, 1)->thread_id.kind != GDB_ONE_THREAD) {
  808. gdb_put_packet("OK");
  809. return;
  810. }
  811. cpu = gdb_get_cpu(get_param(params, 1)->thread_id.pid,
  812. get_param(params, 1)->thread_id.tid);
  813. if (!cpu) {
  814. gdb_put_packet("E22");
  815. return;
  816. }
  817. /*
  818. * Note: This command is deprecated and modern gdb's will be using the
  819. * vCont command instead.
  820. */
  821. switch (get_param(params, 0)->opcode) {
  822. case 'c':
  823. gdbserver_state.c_cpu = cpu;
  824. gdb_put_packet("OK");
  825. break;
  826. case 'g':
  827. gdbserver_state.g_cpu = cpu;
  828. gdb_put_packet("OK");
  829. break;
  830. default:
  831. gdb_put_packet("E22");
  832. break;
  833. }
  834. }
  835. static void handle_insert_bp(GArray *params, void *user_ctx)
  836. {
  837. int res;
  838. if (params->len != 3) {
  839. gdb_put_packet("E22");
  840. return;
  841. }
  842. res = gdb_breakpoint_insert(gdbserver_state.c_cpu,
  843. get_param(params, 0)->val_ul,
  844. get_param(params, 1)->val_ull,
  845. get_param(params, 2)->val_ull);
  846. if (res >= 0) {
  847. gdb_put_packet("OK");
  848. return;
  849. } else if (res == -ENOSYS) {
  850. gdb_put_packet("");
  851. return;
  852. }
  853. gdb_put_packet("E22");
  854. }
  855. static void handle_remove_bp(GArray *params, void *user_ctx)
  856. {
  857. int res;
  858. if (params->len != 3) {
  859. gdb_put_packet("E22");
  860. return;
  861. }
  862. res = gdb_breakpoint_remove(gdbserver_state.c_cpu,
  863. get_param(params, 0)->val_ul,
  864. get_param(params, 1)->val_ull,
  865. get_param(params, 2)->val_ull);
  866. if (res >= 0) {
  867. gdb_put_packet("OK");
  868. return;
  869. } else if (res == -ENOSYS) {
  870. gdb_put_packet("");
  871. return;
  872. }
  873. gdb_put_packet("E22");
  874. }
  875. /*
  876. * handle_set/get_reg
  877. *
  878. * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
  879. * This works, but can be very slow. Anything new enough to understand
  880. * XML also knows how to use this properly. However to use this we
  881. * need to define a local XML file as well as be talking to a
  882. * reasonably modern gdb. Responding with an empty packet will cause
  883. * the remote gdb to fallback to older methods.
  884. */
  885. static void handle_set_reg(GArray *params, void *user_ctx)
  886. {
  887. int reg_size;
  888. if (!gdb_has_xml) {
  889. gdb_put_packet("");
  890. return;
  891. }
  892. if (params->len != 2) {
  893. gdb_put_packet("E22");
  894. return;
  895. }
  896. reg_size = strlen(get_param(params, 1)->data) / 2;
  897. gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 1)->data, reg_size);
  898. gdb_write_register(gdbserver_state.g_cpu, gdbserver_state.mem_buf->data,
  899. get_param(params, 0)->val_ull);
  900. gdb_put_packet("OK");
  901. }
  902. static void handle_get_reg(GArray *params, void *user_ctx)
  903. {
  904. int reg_size;
  905. if (!gdb_has_xml) {
  906. gdb_put_packet("");
  907. return;
  908. }
  909. if (!params->len) {
  910. gdb_put_packet("E14");
  911. return;
  912. }
  913. reg_size = gdb_read_register(gdbserver_state.g_cpu,
  914. gdbserver_state.mem_buf,
  915. get_param(params, 0)->val_ull);
  916. if (!reg_size) {
  917. gdb_put_packet("E14");
  918. return;
  919. } else {
  920. g_byte_array_set_size(gdbserver_state.mem_buf, reg_size);
  921. }
  922. gdb_memtohex(gdbserver_state.str_buf,
  923. gdbserver_state.mem_buf->data, reg_size);
  924. gdb_put_strbuf();
  925. }
  926. static void handle_write_mem(GArray *params, void *user_ctx)
  927. {
  928. if (params->len != 3) {
  929. gdb_put_packet("E22");
  930. return;
  931. }
  932. /* gdb_hextomem() reads 2*len bytes */
  933. if (get_param(params, 1)->val_ull >
  934. strlen(get_param(params, 2)->data) / 2) {
  935. gdb_put_packet("E22");
  936. return;
  937. }
  938. gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 2)->data,
  939. get_param(params, 1)->val_ull);
  940. if (gdb_target_memory_rw_debug(gdbserver_state.g_cpu,
  941. get_param(params, 0)->val_ull,
  942. gdbserver_state.mem_buf->data,
  943. gdbserver_state.mem_buf->len, true)) {
  944. gdb_put_packet("E14");
  945. return;
  946. }
  947. gdb_put_packet("OK");
  948. }
  949. static void handle_read_mem(GArray *params, void *user_ctx)
  950. {
  951. if (params->len != 2) {
  952. gdb_put_packet("E22");
  953. return;
  954. }
  955. /* gdb_memtohex() doubles the required space */
  956. if (get_param(params, 1)->val_ull > MAX_PACKET_LENGTH / 2) {
  957. gdb_put_packet("E22");
  958. return;
  959. }
  960. g_byte_array_set_size(gdbserver_state.mem_buf,
  961. get_param(params, 1)->val_ull);
  962. if (gdb_target_memory_rw_debug(gdbserver_state.g_cpu,
  963. get_param(params, 0)->val_ull,
  964. gdbserver_state.mem_buf->data,
  965. gdbserver_state.mem_buf->len, false)) {
  966. gdb_put_packet("E14");
  967. return;
  968. }
  969. gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data,
  970. gdbserver_state.mem_buf->len);
  971. gdb_put_strbuf();
  972. }
  973. static void handle_write_all_regs(GArray *params, void *user_ctx)
  974. {
  975. int reg_id;
  976. size_t len;
  977. uint8_t *registers;
  978. int reg_size;
  979. if (!params->len) {
  980. return;
  981. }
  982. cpu_synchronize_state(gdbserver_state.g_cpu);
  983. len = strlen(get_param(params, 0)->data) / 2;
  984. gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 0)->data, len);
  985. registers = gdbserver_state.mem_buf->data;
  986. for (reg_id = 0;
  987. reg_id < gdbserver_state.g_cpu->gdb_num_g_regs && len > 0;
  988. reg_id++) {
  989. reg_size = gdb_write_register(gdbserver_state.g_cpu, registers, reg_id);
  990. len -= reg_size;
  991. registers += reg_size;
  992. }
  993. gdb_put_packet("OK");
  994. }
  995. static void handle_read_all_regs(GArray *params, void *user_ctx)
  996. {
  997. int reg_id;
  998. size_t len;
  999. cpu_synchronize_state(gdbserver_state.g_cpu);
  1000. g_byte_array_set_size(gdbserver_state.mem_buf, 0);
  1001. len = 0;
  1002. for (reg_id = 0; reg_id < gdbserver_state.g_cpu->gdb_num_g_regs; reg_id++) {
  1003. len += gdb_read_register(gdbserver_state.g_cpu,
  1004. gdbserver_state.mem_buf,
  1005. reg_id);
  1006. }
  1007. g_assert(len == gdbserver_state.mem_buf->len);
  1008. gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, len);
  1009. gdb_put_strbuf();
  1010. }
  1011. static void handle_step(GArray *params, void *user_ctx)
  1012. {
  1013. if (params->len) {
  1014. gdb_set_cpu_pc(get_param(params, 0)->val_ull);
  1015. }
  1016. cpu_single_step(gdbserver_state.c_cpu, gdbserver_state.sstep_flags);
  1017. gdb_continue();
  1018. }
  1019. static void handle_backward(GArray *params, void *user_ctx)
  1020. {
  1021. if (!gdb_can_reverse()) {
  1022. gdb_put_packet("E22");
  1023. }
  1024. if (params->len == 1) {
  1025. switch (get_param(params, 0)->opcode) {
  1026. case 's':
  1027. if (replay_reverse_step()) {
  1028. gdb_continue();
  1029. } else {
  1030. gdb_put_packet("E14");
  1031. }
  1032. return;
  1033. case 'c':
  1034. if (replay_reverse_continue()) {
  1035. gdb_continue();
  1036. } else {
  1037. gdb_put_packet("E14");
  1038. }
  1039. return;
  1040. }
  1041. }
  1042. /* Default invalid command */
  1043. gdb_put_packet("");
  1044. }
  1045. static void handle_v_cont_query(GArray *params, void *user_ctx)
  1046. {
  1047. gdb_put_packet("vCont;c;C;s;S");
  1048. }
  1049. static void handle_v_cont(GArray *params, void *user_ctx)
  1050. {
  1051. int res;
  1052. if (!params->len) {
  1053. return;
  1054. }
  1055. res = gdb_handle_vcont(get_param(params, 0)->data);
  1056. if ((res == -EINVAL) || (res == -ERANGE)) {
  1057. gdb_put_packet("E22");
  1058. } else if (res) {
  1059. gdb_put_packet("");
  1060. }
  1061. }
  1062. static void handle_v_attach(GArray *params, void *user_ctx)
  1063. {
  1064. GDBProcess *process;
  1065. CPUState *cpu;
  1066. g_string_assign(gdbserver_state.str_buf, "E22");
  1067. if (!params->len) {
  1068. goto cleanup;
  1069. }
  1070. process = gdb_get_process(get_param(params, 0)->val_ul);
  1071. if (!process) {
  1072. goto cleanup;
  1073. }
  1074. cpu = get_first_cpu_in_process(process);
  1075. if (!cpu) {
  1076. goto cleanup;
  1077. }
  1078. process->attached = true;
  1079. gdbserver_state.g_cpu = cpu;
  1080. gdbserver_state.c_cpu = cpu;
  1081. g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
  1082. gdb_append_thread_id(cpu, gdbserver_state.str_buf);
  1083. g_string_append_c(gdbserver_state.str_buf, ';');
  1084. cleanup:
  1085. gdb_put_strbuf();
  1086. }
  1087. static void handle_v_kill(GArray *params, void *user_ctx)
  1088. {
  1089. /* Kill the target */
  1090. gdb_put_packet("OK");
  1091. error_report("QEMU: Terminated via GDBstub");
  1092. gdb_exit(0);
  1093. exit(0);
  1094. }
  1095. static const GdbCmdParseEntry gdb_v_commands_table[] = {
  1096. /* Order is important if has same prefix */
  1097. {
  1098. .handler = handle_v_cont_query,
  1099. .cmd = "Cont?",
  1100. .cmd_startswith = 1
  1101. },
  1102. {
  1103. .handler = handle_v_cont,
  1104. .cmd = "Cont",
  1105. .cmd_startswith = 1,
  1106. .schema = "s0"
  1107. },
  1108. {
  1109. .handler = handle_v_attach,
  1110. .cmd = "Attach;",
  1111. .cmd_startswith = 1,
  1112. .schema = "l0"
  1113. },
  1114. {
  1115. .handler = handle_v_kill,
  1116. .cmd = "Kill;",
  1117. .cmd_startswith = 1
  1118. },
  1119. };
  1120. static void handle_v_commands(GArray *params, void *user_ctx)
  1121. {
  1122. if (!params->len) {
  1123. return;
  1124. }
  1125. if (process_string_cmd(NULL, get_param(params, 0)->data,
  1126. gdb_v_commands_table,
  1127. ARRAY_SIZE(gdb_v_commands_table))) {
  1128. gdb_put_packet("");
  1129. }
  1130. }
  1131. static void handle_query_qemu_sstepbits(GArray *params, void *user_ctx)
  1132. {
  1133. g_string_printf(gdbserver_state.str_buf, "ENABLE=%x", SSTEP_ENABLE);
  1134. if (gdbserver_state.supported_sstep_flags & SSTEP_NOIRQ) {
  1135. g_string_append_printf(gdbserver_state.str_buf, ",NOIRQ=%x",
  1136. SSTEP_NOIRQ);
  1137. }
  1138. if (gdbserver_state.supported_sstep_flags & SSTEP_NOTIMER) {
  1139. g_string_append_printf(gdbserver_state.str_buf, ",NOTIMER=%x",
  1140. SSTEP_NOTIMER);
  1141. }
  1142. gdb_put_strbuf();
  1143. }
  1144. static void handle_set_qemu_sstep(GArray *params, void *user_ctx)
  1145. {
  1146. int new_sstep_flags;
  1147. if (!params->len) {
  1148. return;
  1149. }
  1150. new_sstep_flags = get_param(params, 0)->val_ul;
  1151. if (new_sstep_flags & ~gdbserver_state.supported_sstep_flags) {
  1152. gdb_put_packet("E22");
  1153. return;
  1154. }
  1155. gdbserver_state.sstep_flags = new_sstep_flags;
  1156. gdb_put_packet("OK");
  1157. }
  1158. static void handle_query_qemu_sstep(GArray *params, void *user_ctx)
  1159. {
  1160. g_string_printf(gdbserver_state.str_buf, "0x%x",
  1161. gdbserver_state.sstep_flags);
  1162. gdb_put_strbuf();
  1163. }
  1164. static void handle_query_curr_tid(GArray *params, void *user_ctx)
  1165. {
  1166. CPUState *cpu;
  1167. GDBProcess *process;
  1168. /*
  1169. * "Current thread" remains vague in the spec, so always return
  1170. * the first thread of the current process (gdb returns the
  1171. * first thread).
  1172. */
  1173. process = gdb_get_cpu_process(gdbserver_state.g_cpu);
  1174. cpu = get_first_cpu_in_process(process);
  1175. g_string_assign(gdbserver_state.str_buf, "QC");
  1176. gdb_append_thread_id(cpu, gdbserver_state.str_buf);
  1177. gdb_put_strbuf();
  1178. }
  1179. static void handle_query_threads(GArray *params, void *user_ctx)
  1180. {
  1181. if (!gdbserver_state.query_cpu) {
  1182. gdb_put_packet("l");
  1183. return;
  1184. }
  1185. g_string_assign(gdbserver_state.str_buf, "m");
  1186. gdb_append_thread_id(gdbserver_state.query_cpu, gdbserver_state.str_buf);
  1187. gdb_put_strbuf();
  1188. gdbserver_state.query_cpu = gdb_next_attached_cpu(gdbserver_state.query_cpu);
  1189. }
  1190. static void handle_query_first_threads(GArray *params, void *user_ctx)
  1191. {
  1192. gdbserver_state.query_cpu = gdb_first_attached_cpu();
  1193. handle_query_threads(params, user_ctx);
  1194. }
  1195. static void handle_query_thread_extra(GArray *params, void *user_ctx)
  1196. {
  1197. g_autoptr(GString) rs = g_string_new(NULL);
  1198. CPUState *cpu;
  1199. if (!params->len ||
  1200. get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
  1201. gdb_put_packet("E22");
  1202. return;
  1203. }
  1204. cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid,
  1205. get_param(params, 0)->thread_id.tid);
  1206. if (!cpu) {
  1207. return;
  1208. }
  1209. cpu_synchronize_state(cpu);
  1210. if (gdbserver_state.multiprocess && (gdbserver_state.process_num > 1)) {
  1211. /* Print the CPU model and name in multiprocess mode */
  1212. ObjectClass *oc = object_get_class(OBJECT(cpu));
  1213. const char *cpu_model = object_class_get_name(oc);
  1214. const char *cpu_name =
  1215. object_get_canonical_path_component(OBJECT(cpu));
  1216. g_string_printf(rs, "%s %s [%s]", cpu_model, cpu_name,
  1217. cpu->halted ? "halted " : "running");
  1218. } else {
  1219. g_string_printf(rs, "CPU#%d [%s]", cpu->cpu_index,
  1220. cpu->halted ? "halted " : "running");
  1221. }
  1222. trace_gdbstub_op_extra_info(rs->str);
  1223. gdb_memtohex(gdbserver_state.str_buf, (uint8_t *)rs->str, rs->len);
  1224. gdb_put_strbuf();
  1225. }
  1226. static void handle_query_supported(GArray *params, void *user_ctx)
  1227. {
  1228. CPUClass *cc;
  1229. g_string_printf(gdbserver_state.str_buf, "PacketSize=%x", MAX_PACKET_LENGTH);
  1230. cc = CPU_GET_CLASS(first_cpu);
  1231. if (cc->gdb_core_xml_file) {
  1232. g_string_append(gdbserver_state.str_buf, ";qXfer:features:read+");
  1233. }
  1234. if (gdb_can_reverse()) {
  1235. g_string_append(gdbserver_state.str_buf,
  1236. ";ReverseStep+;ReverseContinue+");
  1237. }
  1238. #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX)
  1239. if (gdbserver_state.c_cpu->opaque) {
  1240. g_string_append(gdbserver_state.str_buf, ";qXfer:auxv:read+");
  1241. }
  1242. #endif
  1243. if (params->len &&
  1244. strstr(get_param(params, 0)->data, "multiprocess+")) {
  1245. gdbserver_state.multiprocess = true;
  1246. }
  1247. g_string_append(gdbserver_state.str_buf, ";vContSupported+;multiprocess+");
  1248. gdb_put_strbuf();
  1249. }
  1250. static void handle_query_xfer_features(GArray *params, void *user_ctx)
  1251. {
  1252. GDBProcess *process;
  1253. CPUClass *cc;
  1254. unsigned long len, total_len, addr;
  1255. const char *xml;
  1256. const char *p;
  1257. if (params->len < 3) {
  1258. gdb_put_packet("E22");
  1259. return;
  1260. }
  1261. process = gdb_get_cpu_process(gdbserver_state.g_cpu);
  1262. cc = CPU_GET_CLASS(gdbserver_state.g_cpu);
  1263. if (!cc->gdb_core_xml_file) {
  1264. gdb_put_packet("");
  1265. return;
  1266. }
  1267. gdb_has_xml = true;
  1268. p = get_param(params, 0)->data;
  1269. xml = get_feature_xml(p, &p, process);
  1270. if (!xml) {
  1271. gdb_put_packet("E00");
  1272. return;
  1273. }
  1274. addr = get_param(params, 1)->val_ul;
  1275. len = get_param(params, 2)->val_ul;
  1276. total_len = strlen(xml);
  1277. if (addr > total_len) {
  1278. gdb_put_packet("E00");
  1279. return;
  1280. }
  1281. if (len > (MAX_PACKET_LENGTH - 5) / 2) {
  1282. len = (MAX_PACKET_LENGTH - 5) / 2;
  1283. }
  1284. if (len < total_len - addr) {
  1285. g_string_assign(gdbserver_state.str_buf, "m");
  1286. gdb_memtox(gdbserver_state.str_buf, xml + addr, len);
  1287. } else {
  1288. g_string_assign(gdbserver_state.str_buf, "l");
  1289. gdb_memtox(gdbserver_state.str_buf, xml + addr, total_len - addr);
  1290. }
  1291. gdb_put_packet_binary(gdbserver_state.str_buf->str,
  1292. gdbserver_state.str_buf->len, true);
  1293. }
  1294. static void handle_query_qemu_supported(GArray *params, void *user_ctx)
  1295. {
  1296. g_string_printf(gdbserver_state.str_buf, "sstepbits;sstep");
  1297. #ifndef CONFIG_USER_ONLY
  1298. g_string_append(gdbserver_state.str_buf, ";PhyMemMode");
  1299. #endif
  1300. gdb_put_strbuf();
  1301. }
  1302. static const GdbCmdParseEntry gdb_gen_query_set_common_table[] = {
  1303. /* Order is important if has same prefix */
  1304. {
  1305. .handler = handle_query_qemu_sstepbits,
  1306. .cmd = "qemu.sstepbits",
  1307. },
  1308. {
  1309. .handler = handle_query_qemu_sstep,
  1310. .cmd = "qemu.sstep",
  1311. },
  1312. {
  1313. .handler = handle_set_qemu_sstep,
  1314. .cmd = "qemu.sstep=",
  1315. .cmd_startswith = 1,
  1316. .schema = "l0"
  1317. },
  1318. };
  1319. static const GdbCmdParseEntry gdb_gen_query_table[] = {
  1320. {
  1321. .handler = handle_query_curr_tid,
  1322. .cmd = "C",
  1323. },
  1324. {
  1325. .handler = handle_query_threads,
  1326. .cmd = "sThreadInfo",
  1327. },
  1328. {
  1329. .handler = handle_query_first_threads,
  1330. .cmd = "fThreadInfo",
  1331. },
  1332. {
  1333. .handler = handle_query_thread_extra,
  1334. .cmd = "ThreadExtraInfo,",
  1335. .cmd_startswith = 1,
  1336. .schema = "t0"
  1337. },
  1338. #ifdef CONFIG_USER_ONLY
  1339. {
  1340. .handler = gdb_handle_query_offsets,
  1341. .cmd = "Offsets",
  1342. },
  1343. #else
  1344. {
  1345. .handler = gdb_handle_query_rcmd,
  1346. .cmd = "Rcmd,",
  1347. .cmd_startswith = 1,
  1348. .schema = "s0"
  1349. },
  1350. #endif
  1351. {
  1352. .handler = handle_query_supported,
  1353. .cmd = "Supported:",
  1354. .cmd_startswith = 1,
  1355. .schema = "s0"
  1356. },
  1357. {
  1358. .handler = handle_query_supported,
  1359. .cmd = "Supported",
  1360. .schema = "s0"
  1361. },
  1362. {
  1363. .handler = handle_query_xfer_features,
  1364. .cmd = "Xfer:features:read:",
  1365. .cmd_startswith = 1,
  1366. .schema = "s:l,l0"
  1367. },
  1368. #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX)
  1369. {
  1370. .handler = gdb_handle_query_xfer_auxv,
  1371. .cmd = "Xfer:auxv:read::",
  1372. .cmd_startswith = 1,
  1373. .schema = "l,l0"
  1374. },
  1375. #endif
  1376. {
  1377. .handler = gdb_handle_query_attached,
  1378. .cmd = "Attached:",
  1379. .cmd_startswith = 1
  1380. },
  1381. {
  1382. .handler = gdb_handle_query_attached,
  1383. .cmd = "Attached",
  1384. },
  1385. {
  1386. .handler = handle_query_qemu_supported,
  1387. .cmd = "qemu.Supported",
  1388. },
  1389. #ifndef CONFIG_USER_ONLY
  1390. {
  1391. .handler = gdb_handle_query_qemu_phy_mem_mode,
  1392. .cmd = "qemu.PhyMemMode",
  1393. },
  1394. #endif
  1395. };
  1396. static const GdbCmdParseEntry gdb_gen_set_table[] = {
  1397. /* Order is important if has same prefix */
  1398. {
  1399. .handler = handle_set_qemu_sstep,
  1400. .cmd = "qemu.sstep:",
  1401. .cmd_startswith = 1,
  1402. .schema = "l0"
  1403. },
  1404. #ifndef CONFIG_USER_ONLY
  1405. {
  1406. .handler = gdb_handle_set_qemu_phy_mem_mode,
  1407. .cmd = "qemu.PhyMemMode:",
  1408. .cmd_startswith = 1,
  1409. .schema = "l0"
  1410. },
  1411. #endif
  1412. };
  1413. static void handle_gen_query(GArray *params, void *user_ctx)
  1414. {
  1415. if (!params->len) {
  1416. return;
  1417. }
  1418. if (!process_string_cmd(NULL, get_param(params, 0)->data,
  1419. gdb_gen_query_set_common_table,
  1420. ARRAY_SIZE(gdb_gen_query_set_common_table))) {
  1421. return;
  1422. }
  1423. if (process_string_cmd(NULL, get_param(params, 0)->data,
  1424. gdb_gen_query_table,
  1425. ARRAY_SIZE(gdb_gen_query_table))) {
  1426. gdb_put_packet("");
  1427. }
  1428. }
  1429. static void handle_gen_set(GArray *params, void *user_ctx)
  1430. {
  1431. if (!params->len) {
  1432. return;
  1433. }
  1434. if (!process_string_cmd(NULL, get_param(params, 0)->data,
  1435. gdb_gen_query_set_common_table,
  1436. ARRAY_SIZE(gdb_gen_query_set_common_table))) {
  1437. return;
  1438. }
  1439. if (process_string_cmd(NULL, get_param(params, 0)->data,
  1440. gdb_gen_set_table,
  1441. ARRAY_SIZE(gdb_gen_set_table))) {
  1442. gdb_put_packet("");
  1443. }
  1444. }
  1445. static void handle_target_halt(GArray *params, void *user_ctx)
  1446. {
  1447. g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
  1448. gdb_append_thread_id(gdbserver_state.c_cpu, gdbserver_state.str_buf);
  1449. g_string_append_c(gdbserver_state.str_buf, ';');
  1450. gdb_put_strbuf();
  1451. /*
  1452. * Remove all the breakpoints when this query is issued,
  1453. * because gdb is doing an initial connect and the state
  1454. * should be cleaned up.
  1455. */
  1456. gdb_breakpoint_remove_all(gdbserver_state.c_cpu);
  1457. }
  1458. static int gdb_handle_packet(const char *line_buf)
  1459. {
  1460. const GdbCmdParseEntry *cmd_parser = NULL;
  1461. trace_gdbstub_io_command(line_buf);
  1462. switch (line_buf[0]) {
  1463. case '!':
  1464. gdb_put_packet("OK");
  1465. break;
  1466. case '?':
  1467. {
  1468. static const GdbCmdParseEntry target_halted_cmd_desc = {
  1469. .handler = handle_target_halt,
  1470. .cmd = "?",
  1471. .cmd_startswith = 1
  1472. };
  1473. cmd_parser = &target_halted_cmd_desc;
  1474. }
  1475. break;
  1476. case 'c':
  1477. {
  1478. static const GdbCmdParseEntry continue_cmd_desc = {
  1479. .handler = handle_continue,
  1480. .cmd = "c",
  1481. .cmd_startswith = 1,
  1482. .schema = "L0"
  1483. };
  1484. cmd_parser = &continue_cmd_desc;
  1485. }
  1486. break;
  1487. case 'C':
  1488. {
  1489. static const GdbCmdParseEntry cont_with_sig_cmd_desc = {
  1490. .handler = handle_cont_with_sig,
  1491. .cmd = "C",
  1492. .cmd_startswith = 1,
  1493. .schema = "l0"
  1494. };
  1495. cmd_parser = &cont_with_sig_cmd_desc;
  1496. }
  1497. break;
  1498. case 'v':
  1499. {
  1500. static const GdbCmdParseEntry v_cmd_desc = {
  1501. .handler = handle_v_commands,
  1502. .cmd = "v",
  1503. .cmd_startswith = 1,
  1504. .schema = "s0"
  1505. };
  1506. cmd_parser = &v_cmd_desc;
  1507. }
  1508. break;
  1509. case 'k':
  1510. /* Kill the target */
  1511. error_report("QEMU: Terminated via GDBstub");
  1512. gdb_exit(0);
  1513. exit(0);
  1514. case 'D':
  1515. {
  1516. static const GdbCmdParseEntry detach_cmd_desc = {
  1517. .handler = handle_detach,
  1518. .cmd = "D",
  1519. .cmd_startswith = 1,
  1520. .schema = "?.l0"
  1521. };
  1522. cmd_parser = &detach_cmd_desc;
  1523. }
  1524. break;
  1525. case 's':
  1526. {
  1527. static const GdbCmdParseEntry step_cmd_desc = {
  1528. .handler = handle_step,
  1529. .cmd = "s",
  1530. .cmd_startswith = 1,
  1531. .schema = "L0"
  1532. };
  1533. cmd_parser = &step_cmd_desc;
  1534. }
  1535. break;
  1536. case 'b':
  1537. {
  1538. static const GdbCmdParseEntry backward_cmd_desc = {
  1539. .handler = handle_backward,
  1540. .cmd = "b",
  1541. .cmd_startswith = 1,
  1542. .schema = "o0"
  1543. };
  1544. cmd_parser = &backward_cmd_desc;
  1545. }
  1546. break;
  1547. case 'F':
  1548. {
  1549. static const GdbCmdParseEntry file_io_cmd_desc = {
  1550. .handler = gdb_handle_file_io,
  1551. .cmd = "F",
  1552. .cmd_startswith = 1,
  1553. .schema = "L,L,o0"
  1554. };
  1555. cmd_parser = &file_io_cmd_desc;
  1556. }
  1557. break;
  1558. case 'g':
  1559. {
  1560. static const GdbCmdParseEntry read_all_regs_cmd_desc = {
  1561. .handler = handle_read_all_regs,
  1562. .cmd = "g",
  1563. .cmd_startswith = 1
  1564. };
  1565. cmd_parser = &read_all_regs_cmd_desc;
  1566. }
  1567. break;
  1568. case 'G':
  1569. {
  1570. static const GdbCmdParseEntry write_all_regs_cmd_desc = {
  1571. .handler = handle_write_all_regs,
  1572. .cmd = "G",
  1573. .cmd_startswith = 1,
  1574. .schema = "s0"
  1575. };
  1576. cmd_parser = &write_all_regs_cmd_desc;
  1577. }
  1578. break;
  1579. case 'm':
  1580. {
  1581. static const GdbCmdParseEntry read_mem_cmd_desc = {
  1582. .handler = handle_read_mem,
  1583. .cmd = "m",
  1584. .cmd_startswith = 1,
  1585. .schema = "L,L0"
  1586. };
  1587. cmd_parser = &read_mem_cmd_desc;
  1588. }
  1589. break;
  1590. case 'M':
  1591. {
  1592. static const GdbCmdParseEntry write_mem_cmd_desc = {
  1593. .handler = handle_write_mem,
  1594. .cmd = "M",
  1595. .cmd_startswith = 1,
  1596. .schema = "L,L:s0"
  1597. };
  1598. cmd_parser = &write_mem_cmd_desc;
  1599. }
  1600. break;
  1601. case 'p':
  1602. {
  1603. static const GdbCmdParseEntry get_reg_cmd_desc = {
  1604. .handler = handle_get_reg,
  1605. .cmd = "p",
  1606. .cmd_startswith = 1,
  1607. .schema = "L0"
  1608. };
  1609. cmd_parser = &get_reg_cmd_desc;
  1610. }
  1611. break;
  1612. case 'P':
  1613. {
  1614. static const GdbCmdParseEntry set_reg_cmd_desc = {
  1615. .handler = handle_set_reg,
  1616. .cmd = "P",
  1617. .cmd_startswith = 1,
  1618. .schema = "L?s0"
  1619. };
  1620. cmd_parser = &set_reg_cmd_desc;
  1621. }
  1622. break;
  1623. case 'Z':
  1624. {
  1625. static const GdbCmdParseEntry insert_bp_cmd_desc = {
  1626. .handler = handle_insert_bp,
  1627. .cmd = "Z",
  1628. .cmd_startswith = 1,
  1629. .schema = "l?L?L0"
  1630. };
  1631. cmd_parser = &insert_bp_cmd_desc;
  1632. }
  1633. break;
  1634. case 'z':
  1635. {
  1636. static const GdbCmdParseEntry remove_bp_cmd_desc = {
  1637. .handler = handle_remove_bp,
  1638. .cmd = "z",
  1639. .cmd_startswith = 1,
  1640. .schema = "l?L?L0"
  1641. };
  1642. cmd_parser = &remove_bp_cmd_desc;
  1643. }
  1644. break;
  1645. case 'H':
  1646. {
  1647. static const GdbCmdParseEntry set_thread_cmd_desc = {
  1648. .handler = handle_set_thread,
  1649. .cmd = "H",
  1650. .cmd_startswith = 1,
  1651. .schema = "o.t0"
  1652. };
  1653. cmd_parser = &set_thread_cmd_desc;
  1654. }
  1655. break;
  1656. case 'T':
  1657. {
  1658. static const GdbCmdParseEntry thread_alive_cmd_desc = {
  1659. .handler = handle_thread_alive,
  1660. .cmd = "T",
  1661. .cmd_startswith = 1,
  1662. .schema = "t0"
  1663. };
  1664. cmd_parser = &thread_alive_cmd_desc;
  1665. }
  1666. break;
  1667. case 'q':
  1668. {
  1669. static const GdbCmdParseEntry gen_query_cmd_desc = {
  1670. .handler = handle_gen_query,
  1671. .cmd = "q",
  1672. .cmd_startswith = 1,
  1673. .schema = "s0"
  1674. };
  1675. cmd_parser = &gen_query_cmd_desc;
  1676. }
  1677. break;
  1678. case 'Q':
  1679. {
  1680. static const GdbCmdParseEntry gen_set_cmd_desc = {
  1681. .handler = handle_gen_set,
  1682. .cmd = "Q",
  1683. .cmd_startswith = 1,
  1684. .schema = "s0"
  1685. };
  1686. cmd_parser = &gen_set_cmd_desc;
  1687. }
  1688. break;
  1689. default:
  1690. /* put empty packet */
  1691. gdb_put_packet("");
  1692. break;
  1693. }
  1694. if (cmd_parser) {
  1695. run_cmd_parser(line_buf, cmd_parser);
  1696. }
  1697. return RS_IDLE;
  1698. }
  1699. void gdb_set_stop_cpu(CPUState *cpu)
  1700. {
  1701. GDBProcess *p = gdb_get_cpu_process(cpu);
  1702. if (!p->attached) {
  1703. /*
  1704. * Having a stop CPU corresponding to a process that is not attached
  1705. * confuses GDB. So we ignore the request.
  1706. */
  1707. return;
  1708. }
  1709. gdbserver_state.c_cpu = cpu;
  1710. gdbserver_state.g_cpu = cpu;
  1711. }
  1712. void gdb_read_byte(uint8_t ch)
  1713. {
  1714. uint8_t reply;
  1715. #ifndef CONFIG_USER_ONLY
  1716. if (gdbserver_state.last_packet->len) {
  1717. /* Waiting for a response to the last packet. If we see the start
  1718. of a new command then abandon the previous response. */
  1719. if (ch == '-') {
  1720. trace_gdbstub_err_got_nack();
  1721. gdb_put_buffer(gdbserver_state.last_packet->data,
  1722. gdbserver_state.last_packet->len);
  1723. } else if (ch == '+') {
  1724. trace_gdbstub_io_got_ack();
  1725. } else {
  1726. trace_gdbstub_io_got_unexpected(ch);
  1727. }
  1728. if (ch == '+' || ch == '$') {
  1729. g_byte_array_set_size(gdbserver_state.last_packet, 0);
  1730. }
  1731. if (ch != '$')
  1732. return;
  1733. }
  1734. if (runstate_is_running()) {
  1735. /* when the CPU is running, we cannot do anything except stop
  1736. it when receiving a char */
  1737. vm_stop(RUN_STATE_PAUSED);
  1738. } else
  1739. #endif
  1740. {
  1741. switch(gdbserver_state.state) {
  1742. case RS_IDLE:
  1743. if (ch == '$') {
  1744. /* start of command packet */
  1745. gdbserver_state.line_buf_index = 0;
  1746. gdbserver_state.line_sum = 0;
  1747. gdbserver_state.state = RS_GETLINE;
  1748. } else {
  1749. trace_gdbstub_err_garbage(ch);
  1750. }
  1751. break;
  1752. case RS_GETLINE:
  1753. if (ch == '}') {
  1754. /* start escape sequence */
  1755. gdbserver_state.state = RS_GETLINE_ESC;
  1756. gdbserver_state.line_sum += ch;
  1757. } else if (ch == '*') {
  1758. /* start run length encoding sequence */
  1759. gdbserver_state.state = RS_GETLINE_RLE;
  1760. gdbserver_state.line_sum += ch;
  1761. } else if (ch == '#') {
  1762. /* end of command, start of checksum*/
  1763. gdbserver_state.state = RS_CHKSUM1;
  1764. } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
  1765. trace_gdbstub_err_overrun();
  1766. gdbserver_state.state = RS_IDLE;
  1767. } else {
  1768. /* unescaped command character */
  1769. gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch;
  1770. gdbserver_state.line_sum += ch;
  1771. }
  1772. break;
  1773. case RS_GETLINE_ESC:
  1774. if (ch == '#') {
  1775. /* unexpected end of command in escape sequence */
  1776. gdbserver_state.state = RS_CHKSUM1;
  1777. } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
  1778. /* command buffer overrun */
  1779. trace_gdbstub_err_overrun();
  1780. gdbserver_state.state = RS_IDLE;
  1781. } else {
  1782. /* parse escaped character and leave escape state */
  1783. gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch ^ 0x20;
  1784. gdbserver_state.line_sum += ch;
  1785. gdbserver_state.state = RS_GETLINE;
  1786. }
  1787. break;
  1788. case RS_GETLINE_RLE:
  1789. /*
  1790. * Run-length encoding is explained in "Debugging with GDB /
  1791. * Appendix E GDB Remote Serial Protocol / Overview".
  1792. */
  1793. if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
  1794. /* invalid RLE count encoding */
  1795. trace_gdbstub_err_invalid_repeat(ch);
  1796. gdbserver_state.state = RS_GETLINE;
  1797. } else {
  1798. /* decode repeat length */
  1799. int repeat = ch - ' ' + 3;
  1800. if (gdbserver_state.line_buf_index + repeat >= sizeof(gdbserver_state.line_buf) - 1) {
  1801. /* that many repeats would overrun the command buffer */
  1802. trace_gdbstub_err_overrun();
  1803. gdbserver_state.state = RS_IDLE;
  1804. } else if (gdbserver_state.line_buf_index < 1) {
  1805. /* got a repeat but we have nothing to repeat */
  1806. trace_gdbstub_err_invalid_rle();
  1807. gdbserver_state.state = RS_GETLINE;
  1808. } else {
  1809. /* repeat the last character */
  1810. memset(gdbserver_state.line_buf + gdbserver_state.line_buf_index,
  1811. gdbserver_state.line_buf[gdbserver_state.line_buf_index - 1], repeat);
  1812. gdbserver_state.line_buf_index += repeat;
  1813. gdbserver_state.line_sum += ch;
  1814. gdbserver_state.state = RS_GETLINE;
  1815. }
  1816. }
  1817. break;
  1818. case RS_CHKSUM1:
  1819. /* get high hex digit of checksum */
  1820. if (!isxdigit(ch)) {
  1821. trace_gdbstub_err_checksum_invalid(ch);
  1822. gdbserver_state.state = RS_GETLINE;
  1823. break;
  1824. }
  1825. gdbserver_state.line_buf[gdbserver_state.line_buf_index] = '\0';
  1826. gdbserver_state.line_csum = fromhex(ch) << 4;
  1827. gdbserver_state.state = RS_CHKSUM2;
  1828. break;
  1829. case RS_CHKSUM2:
  1830. /* get low hex digit of checksum */
  1831. if (!isxdigit(ch)) {
  1832. trace_gdbstub_err_checksum_invalid(ch);
  1833. gdbserver_state.state = RS_GETLINE;
  1834. break;
  1835. }
  1836. gdbserver_state.line_csum |= fromhex(ch);
  1837. if (gdbserver_state.line_csum != (gdbserver_state.line_sum & 0xff)) {
  1838. trace_gdbstub_err_checksum_incorrect(gdbserver_state.line_sum, gdbserver_state.line_csum);
  1839. /* send NAK reply */
  1840. reply = '-';
  1841. gdb_put_buffer(&reply, 1);
  1842. gdbserver_state.state = RS_IDLE;
  1843. } else {
  1844. /* send ACK reply */
  1845. reply = '+';
  1846. gdb_put_buffer(&reply, 1);
  1847. gdbserver_state.state = gdb_handle_packet(gdbserver_state.line_buf);
  1848. }
  1849. break;
  1850. default:
  1851. abort();
  1852. }
  1853. }
  1854. }
  1855. /*
  1856. * Create the process that will contain all the "orphan" CPUs (that are not
  1857. * part of a CPU cluster). Note that if this process contains no CPUs, it won't
  1858. * be attachable and thus will be invisible to the user.
  1859. */
  1860. void gdb_create_default_process(GDBState *s)
  1861. {
  1862. GDBProcess *process;
  1863. int max_pid = 0;
  1864. if (gdbserver_state.process_num) {
  1865. max_pid = s->processes[s->process_num - 1].pid;
  1866. }
  1867. s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
  1868. process = &s->processes[s->process_num - 1];
  1869. /* We need an available PID slot for this process */
  1870. assert(max_pid < UINT32_MAX);
  1871. process->pid = max_pid + 1;
  1872. process->attached = false;
  1873. process->target_xml[0] = '\0';
  1874. }