gdbstub.c 68 KB

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