2
0

gdbstub.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728
  1. /*
  2. * gdb server stub
  3. *
  4. * Copyright (c) 2003-2005 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "config.h"
  20. #include "qemu-common.h"
  21. #ifdef CONFIG_USER_ONLY
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <stdarg.h>
  25. #include <string.h>
  26. #include <errno.h>
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include "qemu.h"
  30. #else
  31. #include "monitor/monitor.h"
  32. #include "sysemu/char.h"
  33. #include "sysemu/sysemu.h"
  34. #include "exec/gdbstub.h"
  35. #endif
  36. #define MAX_PACKET_LENGTH 4096
  37. #include "cpu.h"
  38. #include "qemu/sockets.h"
  39. #include "sysemu/kvm.h"
  40. static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
  41. uint8_t *buf, int len, bool is_write)
  42. {
  43. CPUClass *cc = CPU_GET_CLASS(cpu);
  44. if (cc->memory_rw_debug) {
  45. return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
  46. }
  47. return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
  48. }
  49. enum {
  50. GDB_SIGNAL_0 = 0,
  51. GDB_SIGNAL_INT = 2,
  52. GDB_SIGNAL_QUIT = 3,
  53. GDB_SIGNAL_TRAP = 5,
  54. GDB_SIGNAL_ABRT = 6,
  55. GDB_SIGNAL_ALRM = 14,
  56. GDB_SIGNAL_IO = 23,
  57. GDB_SIGNAL_XCPU = 24,
  58. GDB_SIGNAL_UNKNOWN = 143
  59. };
  60. #ifdef CONFIG_USER_ONLY
  61. /* Map target signal numbers to GDB protocol signal numbers and vice
  62. * versa. For user emulation's currently supported systems, we can
  63. * assume most signals are defined.
  64. */
  65. static int gdb_signal_table[] = {
  66. 0,
  67. TARGET_SIGHUP,
  68. TARGET_SIGINT,
  69. TARGET_SIGQUIT,
  70. TARGET_SIGILL,
  71. TARGET_SIGTRAP,
  72. TARGET_SIGABRT,
  73. -1, /* SIGEMT */
  74. TARGET_SIGFPE,
  75. TARGET_SIGKILL,
  76. TARGET_SIGBUS,
  77. TARGET_SIGSEGV,
  78. TARGET_SIGSYS,
  79. TARGET_SIGPIPE,
  80. TARGET_SIGALRM,
  81. TARGET_SIGTERM,
  82. TARGET_SIGURG,
  83. TARGET_SIGSTOP,
  84. TARGET_SIGTSTP,
  85. TARGET_SIGCONT,
  86. TARGET_SIGCHLD,
  87. TARGET_SIGTTIN,
  88. TARGET_SIGTTOU,
  89. TARGET_SIGIO,
  90. TARGET_SIGXCPU,
  91. TARGET_SIGXFSZ,
  92. TARGET_SIGVTALRM,
  93. TARGET_SIGPROF,
  94. TARGET_SIGWINCH,
  95. -1, /* SIGLOST */
  96. TARGET_SIGUSR1,
  97. TARGET_SIGUSR2,
  98. #ifdef TARGET_SIGPWR
  99. TARGET_SIGPWR,
  100. #else
  101. -1,
  102. #endif
  103. -1, /* SIGPOLL */
  104. -1,
  105. -1,
  106. -1,
  107. -1,
  108. -1,
  109. -1,
  110. -1,
  111. -1,
  112. -1,
  113. -1,
  114. -1,
  115. #ifdef __SIGRTMIN
  116. __SIGRTMIN + 1,
  117. __SIGRTMIN + 2,
  118. __SIGRTMIN + 3,
  119. __SIGRTMIN + 4,
  120. __SIGRTMIN + 5,
  121. __SIGRTMIN + 6,
  122. __SIGRTMIN + 7,
  123. __SIGRTMIN + 8,
  124. __SIGRTMIN + 9,
  125. __SIGRTMIN + 10,
  126. __SIGRTMIN + 11,
  127. __SIGRTMIN + 12,
  128. __SIGRTMIN + 13,
  129. __SIGRTMIN + 14,
  130. __SIGRTMIN + 15,
  131. __SIGRTMIN + 16,
  132. __SIGRTMIN + 17,
  133. __SIGRTMIN + 18,
  134. __SIGRTMIN + 19,
  135. __SIGRTMIN + 20,
  136. __SIGRTMIN + 21,
  137. __SIGRTMIN + 22,
  138. __SIGRTMIN + 23,
  139. __SIGRTMIN + 24,
  140. __SIGRTMIN + 25,
  141. __SIGRTMIN + 26,
  142. __SIGRTMIN + 27,
  143. __SIGRTMIN + 28,
  144. __SIGRTMIN + 29,
  145. __SIGRTMIN + 30,
  146. __SIGRTMIN + 31,
  147. -1, /* SIGCANCEL */
  148. __SIGRTMIN,
  149. __SIGRTMIN + 32,
  150. __SIGRTMIN + 33,
  151. __SIGRTMIN + 34,
  152. __SIGRTMIN + 35,
  153. __SIGRTMIN + 36,
  154. __SIGRTMIN + 37,
  155. __SIGRTMIN + 38,
  156. __SIGRTMIN + 39,
  157. __SIGRTMIN + 40,
  158. __SIGRTMIN + 41,
  159. __SIGRTMIN + 42,
  160. __SIGRTMIN + 43,
  161. __SIGRTMIN + 44,
  162. __SIGRTMIN + 45,
  163. __SIGRTMIN + 46,
  164. __SIGRTMIN + 47,
  165. __SIGRTMIN + 48,
  166. __SIGRTMIN + 49,
  167. __SIGRTMIN + 50,
  168. __SIGRTMIN + 51,
  169. __SIGRTMIN + 52,
  170. __SIGRTMIN + 53,
  171. __SIGRTMIN + 54,
  172. __SIGRTMIN + 55,
  173. __SIGRTMIN + 56,
  174. __SIGRTMIN + 57,
  175. __SIGRTMIN + 58,
  176. __SIGRTMIN + 59,
  177. __SIGRTMIN + 60,
  178. __SIGRTMIN + 61,
  179. __SIGRTMIN + 62,
  180. __SIGRTMIN + 63,
  181. __SIGRTMIN + 64,
  182. __SIGRTMIN + 65,
  183. __SIGRTMIN + 66,
  184. __SIGRTMIN + 67,
  185. __SIGRTMIN + 68,
  186. __SIGRTMIN + 69,
  187. __SIGRTMIN + 70,
  188. __SIGRTMIN + 71,
  189. __SIGRTMIN + 72,
  190. __SIGRTMIN + 73,
  191. __SIGRTMIN + 74,
  192. __SIGRTMIN + 75,
  193. __SIGRTMIN + 76,
  194. __SIGRTMIN + 77,
  195. __SIGRTMIN + 78,
  196. __SIGRTMIN + 79,
  197. __SIGRTMIN + 80,
  198. __SIGRTMIN + 81,
  199. __SIGRTMIN + 82,
  200. __SIGRTMIN + 83,
  201. __SIGRTMIN + 84,
  202. __SIGRTMIN + 85,
  203. __SIGRTMIN + 86,
  204. __SIGRTMIN + 87,
  205. __SIGRTMIN + 88,
  206. __SIGRTMIN + 89,
  207. __SIGRTMIN + 90,
  208. __SIGRTMIN + 91,
  209. __SIGRTMIN + 92,
  210. __SIGRTMIN + 93,
  211. __SIGRTMIN + 94,
  212. __SIGRTMIN + 95,
  213. -1, /* SIGINFO */
  214. -1, /* UNKNOWN */
  215. -1, /* DEFAULT */
  216. -1,
  217. -1,
  218. -1,
  219. -1,
  220. -1,
  221. -1
  222. #endif
  223. };
  224. #else
  225. /* In system mode we only need SIGINT and SIGTRAP; other signals
  226. are not yet supported. */
  227. enum {
  228. TARGET_SIGINT = 2,
  229. TARGET_SIGTRAP = 5
  230. };
  231. static int gdb_signal_table[] = {
  232. -1,
  233. -1,
  234. TARGET_SIGINT,
  235. -1,
  236. -1,
  237. TARGET_SIGTRAP
  238. };
  239. #endif
  240. #ifdef CONFIG_USER_ONLY
  241. static int target_signal_to_gdb (int sig)
  242. {
  243. int i;
  244. for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
  245. if (gdb_signal_table[i] == sig)
  246. return i;
  247. return GDB_SIGNAL_UNKNOWN;
  248. }
  249. #endif
  250. static int gdb_signal_to_target (int sig)
  251. {
  252. if (sig < ARRAY_SIZE (gdb_signal_table))
  253. return gdb_signal_table[sig];
  254. else
  255. return -1;
  256. }
  257. //#define DEBUG_GDB
  258. typedef struct GDBRegisterState {
  259. int base_reg;
  260. int num_regs;
  261. gdb_reg_cb get_reg;
  262. gdb_reg_cb set_reg;
  263. const char *xml;
  264. struct GDBRegisterState *next;
  265. } GDBRegisterState;
  266. enum RSState {
  267. RS_INACTIVE,
  268. RS_IDLE,
  269. RS_GETLINE,
  270. RS_CHKSUM1,
  271. RS_CHKSUM2,
  272. };
  273. typedef struct GDBState {
  274. CPUState *c_cpu; /* current CPU for step/continue ops */
  275. CPUState *g_cpu; /* current CPU for other ops */
  276. CPUState *query_cpu; /* for q{f|s}ThreadInfo */
  277. enum RSState state; /* parsing state */
  278. char line_buf[MAX_PACKET_LENGTH];
  279. int line_buf_index;
  280. int line_csum;
  281. uint8_t last_packet[MAX_PACKET_LENGTH + 4];
  282. int last_packet_len;
  283. int signal;
  284. #ifdef CONFIG_USER_ONLY
  285. int fd;
  286. int running_state;
  287. #else
  288. CharDriverState *chr;
  289. CharDriverState *mon_chr;
  290. #endif
  291. char syscall_buf[256];
  292. gdb_syscall_complete_cb current_syscall_cb;
  293. } GDBState;
  294. /* By default use no IRQs and no timers while single stepping so as to
  295. * make single stepping like an ICE HW step.
  296. */
  297. static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
  298. static GDBState *gdbserver_state;
  299. bool gdb_has_xml;
  300. #ifdef CONFIG_USER_ONLY
  301. /* XXX: This is not thread safe. Do we care? */
  302. static int gdbserver_fd = -1;
  303. static int get_char(GDBState *s)
  304. {
  305. uint8_t ch;
  306. int ret;
  307. for(;;) {
  308. ret = qemu_recv(s->fd, &ch, 1, 0);
  309. if (ret < 0) {
  310. if (errno == ECONNRESET)
  311. s->fd = -1;
  312. if (errno != EINTR && errno != EAGAIN)
  313. return -1;
  314. } else if (ret == 0) {
  315. close(s->fd);
  316. s->fd = -1;
  317. return -1;
  318. } else {
  319. break;
  320. }
  321. }
  322. return ch;
  323. }
  324. #endif
  325. static enum {
  326. GDB_SYS_UNKNOWN,
  327. GDB_SYS_ENABLED,
  328. GDB_SYS_DISABLED,
  329. } gdb_syscall_mode;
  330. /* If gdb is connected when the first semihosting syscall occurs then use
  331. remote gdb syscalls. Otherwise use native file IO. */
  332. int use_gdb_syscalls(void)
  333. {
  334. if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
  335. gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
  336. : GDB_SYS_DISABLED);
  337. }
  338. return gdb_syscall_mode == GDB_SYS_ENABLED;
  339. }
  340. /* Resume execution. */
  341. static inline void gdb_continue(GDBState *s)
  342. {
  343. #ifdef CONFIG_USER_ONLY
  344. s->running_state = 1;
  345. #else
  346. if (!runstate_needs_reset()) {
  347. vm_start();
  348. }
  349. #endif
  350. }
  351. static void put_buffer(GDBState *s, const uint8_t *buf, int len)
  352. {
  353. #ifdef CONFIG_USER_ONLY
  354. int ret;
  355. while (len > 0) {
  356. ret = send(s->fd, buf, len, 0);
  357. if (ret < 0) {
  358. if (errno != EINTR && errno != EAGAIN)
  359. return;
  360. } else {
  361. buf += ret;
  362. len -= ret;
  363. }
  364. }
  365. #else
  366. qemu_chr_fe_write(s->chr, buf, len);
  367. #endif
  368. }
  369. static inline int fromhex(int v)
  370. {
  371. if (v >= '0' && v <= '9')
  372. return v - '0';
  373. else if (v >= 'A' && v <= 'F')
  374. return v - 'A' + 10;
  375. else if (v >= 'a' && v <= 'f')
  376. return v - 'a' + 10;
  377. else
  378. return 0;
  379. }
  380. static inline int tohex(int v)
  381. {
  382. if (v < 10)
  383. return v + '0';
  384. else
  385. return v - 10 + 'a';
  386. }
  387. static void memtohex(char *buf, const uint8_t *mem, int len)
  388. {
  389. int i, c;
  390. char *q;
  391. q = buf;
  392. for(i = 0; i < len; i++) {
  393. c = mem[i];
  394. *q++ = tohex(c >> 4);
  395. *q++ = tohex(c & 0xf);
  396. }
  397. *q = '\0';
  398. }
  399. static void hextomem(uint8_t *mem, const char *buf, int len)
  400. {
  401. int i;
  402. for(i = 0; i < len; i++) {
  403. mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
  404. buf += 2;
  405. }
  406. }
  407. /* return -1 if error, 0 if OK */
  408. static int put_packet_binary(GDBState *s, const char *buf, int len)
  409. {
  410. int csum, i;
  411. uint8_t *p;
  412. for(;;) {
  413. p = s->last_packet;
  414. *(p++) = '$';
  415. memcpy(p, buf, len);
  416. p += len;
  417. csum = 0;
  418. for(i = 0; i < len; i++) {
  419. csum += buf[i];
  420. }
  421. *(p++) = '#';
  422. *(p++) = tohex((csum >> 4) & 0xf);
  423. *(p++) = tohex((csum) & 0xf);
  424. s->last_packet_len = p - s->last_packet;
  425. put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
  426. #ifdef CONFIG_USER_ONLY
  427. i = get_char(s);
  428. if (i < 0)
  429. return -1;
  430. if (i == '+')
  431. break;
  432. #else
  433. break;
  434. #endif
  435. }
  436. return 0;
  437. }
  438. /* return -1 if error, 0 if OK */
  439. static int put_packet(GDBState *s, const char *buf)
  440. {
  441. #ifdef DEBUG_GDB
  442. printf("reply='%s'\n", buf);
  443. #endif
  444. return put_packet_binary(s, buf, strlen(buf));
  445. }
  446. /* Encode data using the encoding for 'x' packets. */
  447. static int memtox(char *buf, const char *mem, int len)
  448. {
  449. char *p = buf;
  450. char c;
  451. while (len--) {
  452. c = *(mem++);
  453. switch (c) {
  454. case '#': case '$': case '*': case '}':
  455. *(p++) = '}';
  456. *(p++) = c ^ 0x20;
  457. break;
  458. default:
  459. *(p++) = c;
  460. break;
  461. }
  462. }
  463. return p - buf;
  464. }
  465. static const char *get_feature_xml(const char *p, const char **newp,
  466. CPUClass *cc)
  467. {
  468. size_t len;
  469. int i;
  470. const char *name;
  471. static char target_xml[1024];
  472. len = 0;
  473. while (p[len] && p[len] != ':')
  474. len++;
  475. *newp = p + len;
  476. name = NULL;
  477. if (strncmp(p, "target.xml", len) == 0) {
  478. /* Generate the XML description for this CPU. */
  479. if (!target_xml[0]) {
  480. GDBRegisterState *r;
  481. CPUState *cpu = first_cpu;
  482. snprintf(target_xml, sizeof(target_xml),
  483. "<?xml version=\"1.0\"?>"
  484. "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
  485. "<target>"
  486. "<xi:include href=\"%s\"/>",
  487. cc->gdb_core_xml_file);
  488. for (r = cpu->gdb_regs; r; r = r->next) {
  489. pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
  490. pstrcat(target_xml, sizeof(target_xml), r->xml);
  491. pstrcat(target_xml, sizeof(target_xml), "\"/>");
  492. }
  493. pstrcat(target_xml, sizeof(target_xml), "</target>");
  494. }
  495. return target_xml;
  496. }
  497. for (i = 0; ; i++) {
  498. name = xml_builtin[i][0];
  499. if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
  500. break;
  501. }
  502. return name ? xml_builtin[i][1] : NULL;
  503. }
  504. static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
  505. {
  506. CPUClass *cc = CPU_GET_CLASS(cpu);
  507. CPUArchState *env = cpu->env_ptr;
  508. GDBRegisterState *r;
  509. if (reg < cc->gdb_num_core_regs) {
  510. return cc->gdb_read_register(cpu, mem_buf, reg);
  511. }
  512. for (r = cpu->gdb_regs; r; r = r->next) {
  513. if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
  514. return r->get_reg(env, mem_buf, reg - r->base_reg);
  515. }
  516. }
  517. return 0;
  518. }
  519. static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
  520. {
  521. CPUClass *cc = CPU_GET_CLASS(cpu);
  522. CPUArchState *env = cpu->env_ptr;
  523. GDBRegisterState *r;
  524. if (reg < cc->gdb_num_core_regs) {
  525. return cc->gdb_write_register(cpu, mem_buf, reg);
  526. }
  527. for (r = cpu->gdb_regs; r; r = r->next) {
  528. if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
  529. return r->set_reg(env, mem_buf, reg - r->base_reg);
  530. }
  531. }
  532. return 0;
  533. }
  534. /* Register a supplemental set of CPU registers. If g_pos is nonzero it
  535. specifies the first register number and these registers are included in
  536. a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
  537. gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
  538. */
  539. void gdb_register_coprocessor(CPUState *cpu,
  540. gdb_reg_cb get_reg, gdb_reg_cb set_reg,
  541. int num_regs, const char *xml, int g_pos)
  542. {
  543. GDBRegisterState *s;
  544. GDBRegisterState **p;
  545. p = &cpu->gdb_regs;
  546. while (*p) {
  547. /* Check for duplicates. */
  548. if (strcmp((*p)->xml, xml) == 0)
  549. return;
  550. p = &(*p)->next;
  551. }
  552. s = g_new0(GDBRegisterState, 1);
  553. s->base_reg = cpu->gdb_num_regs;
  554. s->num_regs = num_regs;
  555. s->get_reg = get_reg;
  556. s->set_reg = set_reg;
  557. s->xml = xml;
  558. /* Add to end of list. */
  559. cpu->gdb_num_regs += num_regs;
  560. *p = s;
  561. if (g_pos) {
  562. if (g_pos != s->base_reg) {
  563. fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n"
  564. "Expected %d got %d\n", xml, g_pos, s->base_reg);
  565. } else {
  566. cpu->gdb_num_g_regs = cpu->gdb_num_regs;
  567. }
  568. }
  569. }
  570. #ifndef CONFIG_USER_ONLY
  571. static const int xlat_gdb_type[] = {
  572. [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
  573. [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
  574. [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
  575. };
  576. #endif
  577. static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
  578. {
  579. CPUState *cpu;
  580. int err = 0;
  581. if (kvm_enabled()) {
  582. return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
  583. }
  584. switch (type) {
  585. case GDB_BREAKPOINT_SW:
  586. case GDB_BREAKPOINT_HW:
  587. CPU_FOREACH(cpu) {
  588. err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
  589. if (err) {
  590. break;
  591. }
  592. }
  593. return err;
  594. #ifndef CONFIG_USER_ONLY
  595. case GDB_WATCHPOINT_WRITE:
  596. case GDB_WATCHPOINT_READ:
  597. case GDB_WATCHPOINT_ACCESS:
  598. CPU_FOREACH(cpu) {
  599. err = cpu_watchpoint_insert(cpu, addr, len, xlat_gdb_type[type],
  600. NULL);
  601. if (err)
  602. break;
  603. }
  604. return err;
  605. #endif
  606. default:
  607. return -ENOSYS;
  608. }
  609. }
  610. static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
  611. {
  612. CPUState *cpu;
  613. int err = 0;
  614. if (kvm_enabled()) {
  615. return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
  616. }
  617. switch (type) {
  618. case GDB_BREAKPOINT_SW:
  619. case GDB_BREAKPOINT_HW:
  620. CPU_FOREACH(cpu) {
  621. err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
  622. if (err) {
  623. break;
  624. }
  625. }
  626. return err;
  627. #ifndef CONFIG_USER_ONLY
  628. case GDB_WATCHPOINT_WRITE:
  629. case GDB_WATCHPOINT_READ:
  630. case GDB_WATCHPOINT_ACCESS:
  631. CPU_FOREACH(cpu) {
  632. err = cpu_watchpoint_remove(cpu, addr, len, xlat_gdb_type[type]);
  633. if (err)
  634. break;
  635. }
  636. return err;
  637. #endif
  638. default:
  639. return -ENOSYS;
  640. }
  641. }
  642. static void gdb_breakpoint_remove_all(void)
  643. {
  644. CPUState *cpu;
  645. if (kvm_enabled()) {
  646. kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
  647. return;
  648. }
  649. CPU_FOREACH(cpu) {
  650. cpu_breakpoint_remove_all(cpu, BP_GDB);
  651. #ifndef CONFIG_USER_ONLY
  652. cpu_watchpoint_remove_all(cpu, BP_GDB);
  653. #endif
  654. }
  655. }
  656. static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
  657. {
  658. CPUState *cpu = s->c_cpu;
  659. CPUClass *cc = CPU_GET_CLASS(cpu);
  660. cpu_synchronize_state(cpu);
  661. if (cc->set_pc) {
  662. cc->set_pc(cpu, pc);
  663. }
  664. }
  665. static CPUState *find_cpu(uint32_t thread_id)
  666. {
  667. CPUState *cpu;
  668. CPU_FOREACH(cpu) {
  669. if (cpu_index(cpu) == thread_id) {
  670. return cpu;
  671. }
  672. }
  673. return NULL;
  674. }
  675. static int gdb_handle_packet(GDBState *s, const char *line_buf)
  676. {
  677. CPUState *cpu;
  678. CPUClass *cc;
  679. const char *p;
  680. uint32_t thread;
  681. int ch, reg_size, type, res;
  682. char buf[MAX_PACKET_LENGTH];
  683. uint8_t mem_buf[MAX_PACKET_LENGTH];
  684. uint8_t *registers;
  685. target_ulong addr, len;
  686. #ifdef DEBUG_GDB
  687. printf("command='%s'\n", line_buf);
  688. #endif
  689. p = line_buf;
  690. ch = *p++;
  691. switch(ch) {
  692. case '?':
  693. /* TODO: Make this return the correct value for user-mode. */
  694. snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
  695. cpu_index(s->c_cpu));
  696. put_packet(s, buf);
  697. /* Remove all the breakpoints when this query is issued,
  698. * because gdb is doing and initial connect and the state
  699. * should be cleaned up.
  700. */
  701. gdb_breakpoint_remove_all();
  702. break;
  703. case 'c':
  704. if (*p != '\0') {
  705. addr = strtoull(p, (char **)&p, 16);
  706. gdb_set_cpu_pc(s, addr);
  707. }
  708. s->signal = 0;
  709. gdb_continue(s);
  710. return RS_IDLE;
  711. case 'C':
  712. s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
  713. if (s->signal == -1)
  714. s->signal = 0;
  715. gdb_continue(s);
  716. return RS_IDLE;
  717. case 'v':
  718. if (strncmp(p, "Cont", 4) == 0) {
  719. int res_signal, res_thread;
  720. p += 4;
  721. if (*p == '?') {
  722. put_packet(s, "vCont;c;C;s;S");
  723. break;
  724. }
  725. res = 0;
  726. res_signal = 0;
  727. res_thread = 0;
  728. while (*p) {
  729. int action, signal;
  730. if (*p++ != ';') {
  731. res = 0;
  732. break;
  733. }
  734. action = *p++;
  735. signal = 0;
  736. if (action == 'C' || action == 'S') {
  737. signal = strtoul(p, (char **)&p, 16);
  738. } else if (action != 'c' && action != 's') {
  739. res = 0;
  740. break;
  741. }
  742. thread = 0;
  743. if (*p == ':') {
  744. thread = strtoull(p+1, (char **)&p, 16);
  745. }
  746. action = tolower(action);
  747. if (res == 0 || (res == 'c' && action == 's')) {
  748. res = action;
  749. res_signal = signal;
  750. res_thread = thread;
  751. }
  752. }
  753. if (res) {
  754. if (res_thread != -1 && res_thread != 0) {
  755. cpu = find_cpu(res_thread);
  756. if (cpu == NULL) {
  757. put_packet(s, "E22");
  758. break;
  759. }
  760. s->c_cpu = cpu;
  761. }
  762. if (res == 's') {
  763. cpu_single_step(s->c_cpu, sstep_flags);
  764. }
  765. s->signal = res_signal;
  766. gdb_continue(s);
  767. return RS_IDLE;
  768. }
  769. break;
  770. } else {
  771. goto unknown_command;
  772. }
  773. case 'k':
  774. #ifdef CONFIG_USER_ONLY
  775. /* Kill the target */
  776. fprintf(stderr, "\nQEMU: Terminated via GDBstub\n");
  777. exit(0);
  778. #endif
  779. case 'D':
  780. /* Detach packet */
  781. gdb_breakpoint_remove_all();
  782. gdb_syscall_mode = GDB_SYS_DISABLED;
  783. gdb_continue(s);
  784. put_packet(s, "OK");
  785. break;
  786. case 's':
  787. if (*p != '\0') {
  788. addr = strtoull(p, (char **)&p, 16);
  789. gdb_set_cpu_pc(s, addr);
  790. }
  791. cpu_single_step(s->c_cpu, sstep_flags);
  792. gdb_continue(s);
  793. return RS_IDLE;
  794. case 'F':
  795. {
  796. target_ulong ret;
  797. target_ulong err;
  798. ret = strtoull(p, (char **)&p, 16);
  799. if (*p == ',') {
  800. p++;
  801. err = strtoull(p, (char **)&p, 16);
  802. } else {
  803. err = 0;
  804. }
  805. if (*p == ',')
  806. p++;
  807. type = *p;
  808. if (s->current_syscall_cb) {
  809. s->current_syscall_cb(s->c_cpu, ret, err);
  810. s->current_syscall_cb = NULL;
  811. }
  812. if (type == 'C') {
  813. put_packet(s, "T02");
  814. } else {
  815. gdb_continue(s);
  816. }
  817. }
  818. break;
  819. case 'g':
  820. cpu_synchronize_state(s->g_cpu);
  821. len = 0;
  822. for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) {
  823. reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
  824. len += reg_size;
  825. }
  826. memtohex(buf, mem_buf, len);
  827. put_packet(s, buf);
  828. break;
  829. case 'G':
  830. cpu_synchronize_state(s->g_cpu);
  831. registers = mem_buf;
  832. len = strlen(p) / 2;
  833. hextomem((uint8_t *)registers, p, len);
  834. for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0; addr++) {
  835. reg_size = gdb_write_register(s->g_cpu, registers, addr);
  836. len -= reg_size;
  837. registers += reg_size;
  838. }
  839. put_packet(s, "OK");
  840. break;
  841. case 'm':
  842. addr = strtoull(p, (char **)&p, 16);
  843. if (*p == ',')
  844. p++;
  845. len = strtoull(p, NULL, 16);
  846. if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
  847. put_packet (s, "E14");
  848. } else {
  849. memtohex(buf, mem_buf, len);
  850. put_packet(s, buf);
  851. }
  852. break;
  853. case 'M':
  854. addr = strtoull(p, (char **)&p, 16);
  855. if (*p == ',')
  856. p++;
  857. len = strtoull(p, (char **)&p, 16);
  858. if (*p == ':')
  859. p++;
  860. hextomem(mem_buf, p, len);
  861. if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
  862. true) != 0) {
  863. put_packet(s, "E14");
  864. } else {
  865. put_packet(s, "OK");
  866. }
  867. break;
  868. case 'p':
  869. /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
  870. This works, but can be very slow. Anything new enough to
  871. understand XML also knows how to use this properly. */
  872. if (!gdb_has_xml)
  873. goto unknown_command;
  874. addr = strtoull(p, (char **)&p, 16);
  875. reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
  876. if (reg_size) {
  877. memtohex(buf, mem_buf, reg_size);
  878. put_packet(s, buf);
  879. } else {
  880. put_packet(s, "E14");
  881. }
  882. break;
  883. case 'P':
  884. if (!gdb_has_xml)
  885. goto unknown_command;
  886. addr = strtoull(p, (char **)&p, 16);
  887. if (*p == '=')
  888. p++;
  889. reg_size = strlen(p) / 2;
  890. hextomem(mem_buf, p, reg_size);
  891. gdb_write_register(s->g_cpu, mem_buf, addr);
  892. put_packet(s, "OK");
  893. break;
  894. case 'Z':
  895. case 'z':
  896. type = strtoul(p, (char **)&p, 16);
  897. if (*p == ',')
  898. p++;
  899. addr = strtoull(p, (char **)&p, 16);
  900. if (*p == ',')
  901. p++;
  902. len = strtoull(p, (char **)&p, 16);
  903. if (ch == 'Z')
  904. res = gdb_breakpoint_insert(addr, len, type);
  905. else
  906. res = gdb_breakpoint_remove(addr, len, type);
  907. if (res >= 0)
  908. put_packet(s, "OK");
  909. else if (res == -ENOSYS)
  910. put_packet(s, "");
  911. else
  912. put_packet(s, "E22");
  913. break;
  914. case 'H':
  915. type = *p++;
  916. thread = strtoull(p, (char **)&p, 16);
  917. if (thread == -1 || thread == 0) {
  918. put_packet(s, "OK");
  919. break;
  920. }
  921. cpu = find_cpu(thread);
  922. if (cpu == NULL) {
  923. put_packet(s, "E22");
  924. break;
  925. }
  926. switch (type) {
  927. case 'c':
  928. s->c_cpu = cpu;
  929. put_packet(s, "OK");
  930. break;
  931. case 'g':
  932. s->g_cpu = cpu;
  933. put_packet(s, "OK");
  934. break;
  935. default:
  936. put_packet(s, "E22");
  937. break;
  938. }
  939. break;
  940. case 'T':
  941. thread = strtoull(p, (char **)&p, 16);
  942. cpu = find_cpu(thread);
  943. if (cpu != NULL) {
  944. put_packet(s, "OK");
  945. } else {
  946. put_packet(s, "E22");
  947. }
  948. break;
  949. case 'q':
  950. case 'Q':
  951. /* parse any 'q' packets here */
  952. if (!strcmp(p,"qemu.sstepbits")) {
  953. /* Query Breakpoint bit definitions */
  954. snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
  955. SSTEP_ENABLE,
  956. SSTEP_NOIRQ,
  957. SSTEP_NOTIMER);
  958. put_packet(s, buf);
  959. break;
  960. } else if (strncmp(p,"qemu.sstep",10) == 0) {
  961. /* Display or change the sstep_flags */
  962. p += 10;
  963. if (*p != '=') {
  964. /* Display current setting */
  965. snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
  966. put_packet(s, buf);
  967. break;
  968. }
  969. p++;
  970. type = strtoul(p, (char **)&p, 16);
  971. sstep_flags = type;
  972. put_packet(s, "OK");
  973. break;
  974. } else if (strcmp(p,"C") == 0) {
  975. /* "Current thread" remains vague in the spec, so always return
  976. * the first CPU (gdb returns the first thread). */
  977. put_packet(s, "QC1");
  978. break;
  979. } else if (strcmp(p,"fThreadInfo") == 0) {
  980. s->query_cpu = first_cpu;
  981. goto report_cpuinfo;
  982. } else if (strcmp(p,"sThreadInfo") == 0) {
  983. report_cpuinfo:
  984. if (s->query_cpu) {
  985. snprintf(buf, sizeof(buf), "m%x", cpu_index(s->query_cpu));
  986. put_packet(s, buf);
  987. s->query_cpu = CPU_NEXT(s->query_cpu);
  988. } else
  989. put_packet(s, "l");
  990. break;
  991. } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
  992. thread = strtoull(p+16, (char **)&p, 16);
  993. cpu = find_cpu(thread);
  994. if (cpu != NULL) {
  995. cpu_synchronize_state(cpu);
  996. len = snprintf((char *)mem_buf, sizeof(mem_buf),
  997. "CPU#%d [%s]", cpu->cpu_index,
  998. cpu->halted ? "halted " : "running");
  999. memtohex(buf, mem_buf, len);
  1000. put_packet(s, buf);
  1001. }
  1002. break;
  1003. }
  1004. #ifdef CONFIG_USER_ONLY
  1005. else if (strncmp(p, "Offsets", 7) == 0) {
  1006. TaskState *ts = s->c_cpu->opaque;
  1007. snprintf(buf, sizeof(buf),
  1008. "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
  1009. ";Bss=" TARGET_ABI_FMT_lx,
  1010. ts->info->code_offset,
  1011. ts->info->data_offset,
  1012. ts->info->data_offset);
  1013. put_packet(s, buf);
  1014. break;
  1015. }
  1016. #else /* !CONFIG_USER_ONLY */
  1017. else if (strncmp(p, "Rcmd,", 5) == 0) {
  1018. int len = strlen(p + 5);
  1019. if ((len % 2) != 0) {
  1020. put_packet(s, "E01");
  1021. break;
  1022. }
  1023. hextomem(mem_buf, p + 5, len);
  1024. len = len / 2;
  1025. mem_buf[len++] = 0;
  1026. qemu_chr_be_write(s->mon_chr, mem_buf, len);
  1027. put_packet(s, "OK");
  1028. break;
  1029. }
  1030. #endif /* !CONFIG_USER_ONLY */
  1031. if (strncmp(p, "Supported", 9) == 0) {
  1032. snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
  1033. cc = CPU_GET_CLASS(first_cpu);
  1034. if (cc->gdb_core_xml_file != NULL) {
  1035. pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
  1036. }
  1037. put_packet(s, buf);
  1038. break;
  1039. }
  1040. if (strncmp(p, "Xfer:features:read:", 19) == 0) {
  1041. const char *xml;
  1042. target_ulong total_len;
  1043. cc = CPU_GET_CLASS(first_cpu);
  1044. if (cc->gdb_core_xml_file == NULL) {
  1045. goto unknown_command;
  1046. }
  1047. gdb_has_xml = true;
  1048. p += 19;
  1049. xml = get_feature_xml(p, &p, cc);
  1050. if (!xml) {
  1051. snprintf(buf, sizeof(buf), "E00");
  1052. put_packet(s, buf);
  1053. break;
  1054. }
  1055. if (*p == ':')
  1056. p++;
  1057. addr = strtoul(p, (char **)&p, 16);
  1058. if (*p == ',')
  1059. p++;
  1060. len = strtoul(p, (char **)&p, 16);
  1061. total_len = strlen(xml);
  1062. if (addr > total_len) {
  1063. snprintf(buf, sizeof(buf), "E00");
  1064. put_packet(s, buf);
  1065. break;
  1066. }
  1067. if (len > (MAX_PACKET_LENGTH - 5) / 2)
  1068. len = (MAX_PACKET_LENGTH - 5) / 2;
  1069. if (len < total_len - addr) {
  1070. buf[0] = 'm';
  1071. len = memtox(buf + 1, xml + addr, len);
  1072. } else {
  1073. buf[0] = 'l';
  1074. len = memtox(buf + 1, xml + addr, total_len - addr);
  1075. }
  1076. put_packet_binary(s, buf, len + 1);
  1077. break;
  1078. }
  1079. /* Unrecognised 'q' command. */
  1080. goto unknown_command;
  1081. default:
  1082. unknown_command:
  1083. /* put empty packet */
  1084. buf[0] = '\0';
  1085. put_packet(s, buf);
  1086. break;
  1087. }
  1088. return RS_IDLE;
  1089. }
  1090. void gdb_set_stop_cpu(CPUState *cpu)
  1091. {
  1092. gdbserver_state->c_cpu = cpu;
  1093. gdbserver_state->g_cpu = cpu;
  1094. }
  1095. #ifndef CONFIG_USER_ONLY
  1096. static void gdb_vm_state_change(void *opaque, int running, RunState state)
  1097. {
  1098. GDBState *s = gdbserver_state;
  1099. CPUArchState *env = s->c_cpu->env_ptr;
  1100. CPUState *cpu = s->c_cpu;
  1101. char buf[256];
  1102. const char *type;
  1103. int ret;
  1104. if (running || s->state == RS_INACTIVE) {
  1105. return;
  1106. }
  1107. /* Is there a GDB syscall waiting to be sent? */
  1108. if (s->current_syscall_cb) {
  1109. put_packet(s, s->syscall_buf);
  1110. return;
  1111. }
  1112. switch (state) {
  1113. case RUN_STATE_DEBUG:
  1114. if (cpu->watchpoint_hit) {
  1115. switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
  1116. case BP_MEM_READ:
  1117. type = "r";
  1118. break;
  1119. case BP_MEM_ACCESS:
  1120. type = "a";
  1121. break;
  1122. default:
  1123. type = "";
  1124. break;
  1125. }
  1126. snprintf(buf, sizeof(buf),
  1127. "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
  1128. GDB_SIGNAL_TRAP, cpu_index(cpu), type,
  1129. (target_ulong)cpu->watchpoint_hit->vaddr);
  1130. cpu->watchpoint_hit = NULL;
  1131. goto send_packet;
  1132. }
  1133. tb_flush(env);
  1134. ret = GDB_SIGNAL_TRAP;
  1135. break;
  1136. case RUN_STATE_PAUSED:
  1137. ret = GDB_SIGNAL_INT;
  1138. break;
  1139. case RUN_STATE_SHUTDOWN:
  1140. ret = GDB_SIGNAL_QUIT;
  1141. break;
  1142. case RUN_STATE_IO_ERROR:
  1143. ret = GDB_SIGNAL_IO;
  1144. break;
  1145. case RUN_STATE_WATCHDOG:
  1146. ret = GDB_SIGNAL_ALRM;
  1147. break;
  1148. case RUN_STATE_INTERNAL_ERROR:
  1149. ret = GDB_SIGNAL_ABRT;
  1150. break;
  1151. case RUN_STATE_SAVE_VM:
  1152. case RUN_STATE_RESTORE_VM:
  1153. return;
  1154. case RUN_STATE_FINISH_MIGRATE:
  1155. ret = GDB_SIGNAL_XCPU;
  1156. break;
  1157. default:
  1158. ret = GDB_SIGNAL_UNKNOWN;
  1159. break;
  1160. }
  1161. snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_index(cpu));
  1162. send_packet:
  1163. put_packet(s, buf);
  1164. /* disable single step if it was enabled */
  1165. cpu_single_step(cpu, 0);
  1166. }
  1167. #endif
  1168. /* Send a gdb syscall request.
  1169. This accepts limited printf-style format specifiers, specifically:
  1170. %x - target_ulong argument printed in hex.
  1171. %lx - 64-bit argument printed in hex.
  1172. %s - string pointer (target_ulong) and length (int) pair. */
  1173. void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
  1174. {
  1175. va_list va;
  1176. char *p;
  1177. char *p_end;
  1178. target_ulong addr;
  1179. uint64_t i64;
  1180. GDBState *s;
  1181. s = gdbserver_state;
  1182. if (!s)
  1183. return;
  1184. s->current_syscall_cb = cb;
  1185. #ifndef CONFIG_USER_ONLY
  1186. vm_stop(RUN_STATE_DEBUG);
  1187. #endif
  1188. va_start(va, fmt);
  1189. p = s->syscall_buf;
  1190. p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
  1191. *(p++) = 'F';
  1192. while (*fmt) {
  1193. if (*fmt == '%') {
  1194. fmt++;
  1195. switch (*fmt++) {
  1196. case 'x':
  1197. addr = va_arg(va, target_ulong);
  1198. p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
  1199. break;
  1200. case 'l':
  1201. if (*(fmt++) != 'x')
  1202. goto bad_format;
  1203. i64 = va_arg(va, uint64_t);
  1204. p += snprintf(p, p_end - p, "%" PRIx64, i64);
  1205. break;
  1206. case 's':
  1207. addr = va_arg(va, target_ulong);
  1208. p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
  1209. addr, va_arg(va, int));
  1210. break;
  1211. default:
  1212. bad_format:
  1213. fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
  1214. fmt - 1);
  1215. break;
  1216. }
  1217. } else {
  1218. *(p++) = *(fmt++);
  1219. }
  1220. }
  1221. *p = 0;
  1222. va_end(va);
  1223. #ifdef CONFIG_USER_ONLY
  1224. put_packet(s, s->syscall_buf);
  1225. gdb_handlesig(s->c_cpu, 0);
  1226. #else
  1227. /* In this case wait to send the syscall packet until notification that
  1228. the CPU has stopped. This must be done because if the packet is sent
  1229. now the reply from the syscall request could be received while the CPU
  1230. is still in the running state, which can cause packets to be dropped
  1231. and state transition 'T' packets to be sent while the syscall is still
  1232. being processed. */
  1233. cpu_exit(s->c_cpu);
  1234. #endif
  1235. }
  1236. static void gdb_read_byte(GDBState *s, int ch)
  1237. {
  1238. int i, csum;
  1239. uint8_t reply;
  1240. #ifndef CONFIG_USER_ONLY
  1241. if (s->last_packet_len) {
  1242. /* Waiting for a response to the last packet. If we see the start
  1243. of a new command then abandon the previous response. */
  1244. if (ch == '-') {
  1245. #ifdef DEBUG_GDB
  1246. printf("Got NACK, retransmitting\n");
  1247. #endif
  1248. put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
  1249. }
  1250. #ifdef DEBUG_GDB
  1251. else if (ch == '+')
  1252. printf("Got ACK\n");
  1253. else
  1254. printf("Got '%c' when expecting ACK/NACK\n", ch);
  1255. #endif
  1256. if (ch == '+' || ch == '$')
  1257. s->last_packet_len = 0;
  1258. if (ch != '$')
  1259. return;
  1260. }
  1261. if (runstate_is_running()) {
  1262. /* when the CPU is running, we cannot do anything except stop
  1263. it when receiving a char */
  1264. vm_stop(RUN_STATE_PAUSED);
  1265. } else
  1266. #endif
  1267. {
  1268. switch(s->state) {
  1269. case RS_IDLE:
  1270. if (ch == '$') {
  1271. s->line_buf_index = 0;
  1272. s->state = RS_GETLINE;
  1273. }
  1274. break;
  1275. case RS_GETLINE:
  1276. if (ch == '#') {
  1277. s->state = RS_CHKSUM1;
  1278. } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
  1279. s->state = RS_IDLE;
  1280. } else {
  1281. s->line_buf[s->line_buf_index++] = ch;
  1282. }
  1283. break;
  1284. case RS_CHKSUM1:
  1285. s->line_buf[s->line_buf_index] = '\0';
  1286. s->line_csum = fromhex(ch) << 4;
  1287. s->state = RS_CHKSUM2;
  1288. break;
  1289. case RS_CHKSUM2:
  1290. s->line_csum |= fromhex(ch);
  1291. csum = 0;
  1292. for(i = 0; i < s->line_buf_index; i++) {
  1293. csum += s->line_buf[i];
  1294. }
  1295. if (s->line_csum != (csum & 0xff)) {
  1296. reply = '-';
  1297. put_buffer(s, &reply, 1);
  1298. s->state = RS_IDLE;
  1299. } else {
  1300. reply = '+';
  1301. put_buffer(s, &reply, 1);
  1302. s->state = gdb_handle_packet(s, s->line_buf);
  1303. }
  1304. break;
  1305. default:
  1306. abort();
  1307. }
  1308. }
  1309. }
  1310. /* Tell the remote gdb that the process has exited. */
  1311. void gdb_exit(CPUArchState *env, int code)
  1312. {
  1313. GDBState *s;
  1314. char buf[4];
  1315. s = gdbserver_state;
  1316. if (!s) {
  1317. return;
  1318. }
  1319. #ifdef CONFIG_USER_ONLY
  1320. if (gdbserver_fd < 0 || s->fd < 0) {
  1321. return;
  1322. }
  1323. #endif
  1324. snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
  1325. put_packet(s, buf);
  1326. #ifndef CONFIG_USER_ONLY
  1327. if (s->chr) {
  1328. qemu_chr_delete(s->chr);
  1329. }
  1330. #endif
  1331. }
  1332. #ifdef CONFIG_USER_ONLY
  1333. int
  1334. gdb_queuesig (void)
  1335. {
  1336. GDBState *s;
  1337. s = gdbserver_state;
  1338. if (gdbserver_fd < 0 || s->fd < 0)
  1339. return 0;
  1340. else
  1341. return 1;
  1342. }
  1343. int
  1344. gdb_handlesig(CPUState *cpu, int sig)
  1345. {
  1346. CPUArchState *env = cpu->env_ptr;
  1347. GDBState *s;
  1348. char buf[256];
  1349. int n;
  1350. s = gdbserver_state;
  1351. if (gdbserver_fd < 0 || s->fd < 0) {
  1352. return sig;
  1353. }
  1354. /* disable single step if it was enabled */
  1355. cpu_single_step(cpu, 0);
  1356. tb_flush(env);
  1357. if (sig != 0) {
  1358. snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
  1359. put_packet(s, buf);
  1360. }
  1361. /* put_packet() might have detected that the peer terminated the
  1362. connection. */
  1363. if (s->fd < 0) {
  1364. return sig;
  1365. }
  1366. sig = 0;
  1367. s->state = RS_IDLE;
  1368. s->running_state = 0;
  1369. while (s->running_state == 0) {
  1370. n = read(s->fd, buf, 256);
  1371. if (n > 0) {
  1372. int i;
  1373. for (i = 0; i < n; i++) {
  1374. gdb_read_byte(s, buf[i]);
  1375. }
  1376. } else if (n == 0 || errno != EAGAIN) {
  1377. /* XXX: Connection closed. Should probably wait for another
  1378. connection before continuing. */
  1379. return sig;
  1380. }
  1381. }
  1382. sig = s->signal;
  1383. s->signal = 0;
  1384. return sig;
  1385. }
  1386. /* Tell the remote gdb that the process has exited due to SIG. */
  1387. void gdb_signalled(CPUArchState *env, int sig)
  1388. {
  1389. GDBState *s;
  1390. char buf[4];
  1391. s = gdbserver_state;
  1392. if (gdbserver_fd < 0 || s->fd < 0) {
  1393. return;
  1394. }
  1395. snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
  1396. put_packet(s, buf);
  1397. }
  1398. static void gdb_accept(void)
  1399. {
  1400. GDBState *s;
  1401. struct sockaddr_in sockaddr;
  1402. socklen_t len;
  1403. int fd;
  1404. for(;;) {
  1405. len = sizeof(sockaddr);
  1406. fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
  1407. if (fd < 0 && errno != EINTR) {
  1408. perror("accept");
  1409. return;
  1410. } else if (fd >= 0) {
  1411. #ifndef _WIN32
  1412. fcntl(fd, F_SETFD, FD_CLOEXEC);
  1413. #endif
  1414. break;
  1415. }
  1416. }
  1417. /* set short latency */
  1418. socket_set_nodelay(fd);
  1419. s = g_malloc0(sizeof(GDBState));
  1420. s->c_cpu = first_cpu;
  1421. s->g_cpu = first_cpu;
  1422. s->fd = fd;
  1423. gdb_has_xml = false;
  1424. gdbserver_state = s;
  1425. fcntl(fd, F_SETFL, O_NONBLOCK);
  1426. }
  1427. static int gdbserver_open(int port)
  1428. {
  1429. struct sockaddr_in sockaddr;
  1430. int fd, ret;
  1431. fd = socket(PF_INET, SOCK_STREAM, 0);
  1432. if (fd < 0) {
  1433. perror("socket");
  1434. return -1;
  1435. }
  1436. #ifndef _WIN32
  1437. fcntl(fd, F_SETFD, FD_CLOEXEC);
  1438. #endif
  1439. socket_set_fast_reuse(fd);
  1440. sockaddr.sin_family = AF_INET;
  1441. sockaddr.sin_port = htons(port);
  1442. sockaddr.sin_addr.s_addr = 0;
  1443. ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
  1444. if (ret < 0) {
  1445. perror("bind");
  1446. close(fd);
  1447. return -1;
  1448. }
  1449. ret = listen(fd, 0);
  1450. if (ret < 0) {
  1451. perror("listen");
  1452. close(fd);
  1453. return -1;
  1454. }
  1455. return fd;
  1456. }
  1457. int gdbserver_start(int port)
  1458. {
  1459. gdbserver_fd = gdbserver_open(port);
  1460. if (gdbserver_fd < 0)
  1461. return -1;
  1462. /* accept connections */
  1463. gdb_accept();
  1464. return 0;
  1465. }
  1466. /* Disable gdb stub for child processes. */
  1467. void gdbserver_fork(CPUArchState *env)
  1468. {
  1469. CPUState *cpu = ENV_GET_CPU(env);
  1470. GDBState *s = gdbserver_state;
  1471. if (gdbserver_fd < 0 || s->fd < 0) {
  1472. return;
  1473. }
  1474. close(s->fd);
  1475. s->fd = -1;
  1476. cpu_breakpoint_remove_all(cpu, BP_GDB);
  1477. cpu_watchpoint_remove_all(cpu, BP_GDB);
  1478. }
  1479. #else
  1480. static int gdb_chr_can_receive(void *opaque)
  1481. {
  1482. /* We can handle an arbitrarily large amount of data.
  1483. Pick the maximum packet size, which is as good as anything. */
  1484. return MAX_PACKET_LENGTH;
  1485. }
  1486. static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
  1487. {
  1488. int i;
  1489. for (i = 0; i < size; i++) {
  1490. gdb_read_byte(gdbserver_state, buf[i]);
  1491. }
  1492. }
  1493. static void gdb_chr_event(void *opaque, int event)
  1494. {
  1495. switch (event) {
  1496. case CHR_EVENT_OPENED:
  1497. vm_stop(RUN_STATE_PAUSED);
  1498. gdb_has_xml = false;
  1499. break;
  1500. default:
  1501. break;
  1502. }
  1503. }
  1504. static void gdb_monitor_output(GDBState *s, const char *msg, int len)
  1505. {
  1506. char buf[MAX_PACKET_LENGTH];
  1507. buf[0] = 'O';
  1508. if (len > (MAX_PACKET_LENGTH/2) - 1)
  1509. len = (MAX_PACKET_LENGTH/2) - 1;
  1510. memtohex(buf + 1, (uint8_t *)msg, len);
  1511. put_packet(s, buf);
  1512. }
  1513. static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len)
  1514. {
  1515. const char *p = (const char *)buf;
  1516. int max_sz;
  1517. max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
  1518. for (;;) {
  1519. if (len <= max_sz) {
  1520. gdb_monitor_output(gdbserver_state, p, len);
  1521. break;
  1522. }
  1523. gdb_monitor_output(gdbserver_state, p, max_sz);
  1524. p += max_sz;
  1525. len -= max_sz;
  1526. }
  1527. return len;
  1528. }
  1529. #ifndef _WIN32
  1530. static void gdb_sigterm_handler(int signal)
  1531. {
  1532. if (runstate_is_running()) {
  1533. vm_stop(RUN_STATE_PAUSED);
  1534. }
  1535. }
  1536. #endif
  1537. int gdbserver_start(const char *device)
  1538. {
  1539. GDBState *s;
  1540. char gdbstub_device_name[128];
  1541. CharDriverState *chr = NULL;
  1542. CharDriverState *mon_chr;
  1543. if (!device)
  1544. return -1;
  1545. if (strcmp(device, "none") != 0) {
  1546. if (strstart(device, "tcp:", NULL)) {
  1547. /* enforce required TCP attributes */
  1548. snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
  1549. "%s,nowait,nodelay,server", device);
  1550. device = gdbstub_device_name;
  1551. }
  1552. #ifndef _WIN32
  1553. else if (strcmp(device, "stdio") == 0) {
  1554. struct sigaction act;
  1555. memset(&act, 0, sizeof(act));
  1556. act.sa_handler = gdb_sigterm_handler;
  1557. sigaction(SIGINT, &act, NULL);
  1558. }
  1559. #endif
  1560. chr = qemu_chr_new("gdb", device, NULL);
  1561. if (!chr)
  1562. return -1;
  1563. qemu_chr_fe_claim_no_fail(chr);
  1564. qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive,
  1565. gdb_chr_event, NULL);
  1566. }
  1567. s = gdbserver_state;
  1568. if (!s) {
  1569. s = g_malloc0(sizeof(GDBState));
  1570. gdbserver_state = s;
  1571. qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
  1572. /* Initialize a monitor terminal for gdb */
  1573. mon_chr = g_malloc0(sizeof(*mon_chr));
  1574. mon_chr->chr_write = gdb_monitor_write;
  1575. monitor_init(mon_chr, 0);
  1576. } else {
  1577. if (s->chr)
  1578. qemu_chr_delete(s->chr);
  1579. mon_chr = s->mon_chr;
  1580. memset(s, 0, sizeof(GDBState));
  1581. }
  1582. s->c_cpu = first_cpu;
  1583. s->g_cpu = first_cpu;
  1584. s->chr = chr;
  1585. s->state = chr ? RS_IDLE : RS_INACTIVE;
  1586. s->mon_chr = mon_chr;
  1587. s->current_syscall_cb = NULL;
  1588. return 0;
  1589. }
  1590. #endif