gdbstub.c 49 KB

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