gdbstub.c 64 KB

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