gdbstub.c 68 KB

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