gdbstub.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745
  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. /* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
  572. static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
  573. {
  574. static const int xlat[] = {
  575. [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
  576. [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
  577. [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
  578. };
  579. CPUClass *cc = CPU_GET_CLASS(cpu);
  580. int cputype = xlat[gdbtype];
  581. if (cc->gdb_stop_before_watchpoint) {
  582. cputype |= BP_STOP_BEFORE_ACCESS;
  583. }
  584. return cputype;
  585. }
  586. #endif
  587. static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
  588. {
  589. CPUState *cpu;
  590. int err = 0;
  591. if (kvm_enabled()) {
  592. return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
  593. }
  594. switch (type) {
  595. case GDB_BREAKPOINT_SW:
  596. case GDB_BREAKPOINT_HW:
  597. CPU_FOREACH(cpu) {
  598. err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
  599. if (err) {
  600. break;
  601. }
  602. }
  603. return err;
  604. #ifndef CONFIG_USER_ONLY
  605. case GDB_WATCHPOINT_WRITE:
  606. case GDB_WATCHPOINT_READ:
  607. case GDB_WATCHPOINT_ACCESS:
  608. CPU_FOREACH(cpu) {
  609. err = cpu_watchpoint_insert(cpu, addr, len,
  610. xlat_gdb_type(cpu, type), NULL);
  611. if (err) {
  612. break;
  613. }
  614. }
  615. return err;
  616. #endif
  617. default:
  618. return -ENOSYS;
  619. }
  620. }
  621. static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
  622. {
  623. CPUState *cpu;
  624. int err = 0;
  625. if (kvm_enabled()) {
  626. return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
  627. }
  628. switch (type) {
  629. case GDB_BREAKPOINT_SW:
  630. case GDB_BREAKPOINT_HW:
  631. CPU_FOREACH(cpu) {
  632. err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
  633. if (err) {
  634. break;
  635. }
  636. }
  637. return err;
  638. #ifndef CONFIG_USER_ONLY
  639. case GDB_WATCHPOINT_WRITE:
  640. case GDB_WATCHPOINT_READ:
  641. case GDB_WATCHPOINT_ACCESS:
  642. CPU_FOREACH(cpu) {
  643. err = cpu_watchpoint_remove(cpu, addr, len,
  644. xlat_gdb_type(cpu, type));
  645. if (err)
  646. break;
  647. }
  648. return err;
  649. #endif
  650. default:
  651. return -ENOSYS;
  652. }
  653. }
  654. static void gdb_breakpoint_remove_all(void)
  655. {
  656. CPUState *cpu;
  657. if (kvm_enabled()) {
  658. kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
  659. return;
  660. }
  661. CPU_FOREACH(cpu) {
  662. cpu_breakpoint_remove_all(cpu, BP_GDB);
  663. #ifndef CONFIG_USER_ONLY
  664. cpu_watchpoint_remove_all(cpu, BP_GDB);
  665. #endif
  666. }
  667. }
  668. static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
  669. {
  670. CPUState *cpu = s->c_cpu;
  671. CPUClass *cc = CPU_GET_CLASS(cpu);
  672. cpu_synchronize_state(cpu);
  673. if (cc->set_pc) {
  674. cc->set_pc(cpu, pc);
  675. }
  676. }
  677. static CPUState *find_cpu(uint32_t thread_id)
  678. {
  679. CPUState *cpu;
  680. CPU_FOREACH(cpu) {
  681. if (cpu_index(cpu) == thread_id) {
  682. return cpu;
  683. }
  684. }
  685. return NULL;
  686. }
  687. static int gdb_handle_packet(GDBState *s, const char *line_buf)
  688. {
  689. CPUState *cpu;
  690. CPUClass *cc;
  691. const char *p;
  692. uint32_t thread;
  693. int ch, reg_size, type, res;
  694. char buf[MAX_PACKET_LENGTH];
  695. uint8_t mem_buf[MAX_PACKET_LENGTH];
  696. uint8_t *registers;
  697. target_ulong addr, len;
  698. #ifdef DEBUG_GDB
  699. printf("command='%s'\n", line_buf);
  700. #endif
  701. p = line_buf;
  702. ch = *p++;
  703. switch(ch) {
  704. case '?':
  705. /* TODO: Make this return the correct value for user-mode. */
  706. snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
  707. cpu_index(s->c_cpu));
  708. put_packet(s, buf);
  709. /* Remove all the breakpoints when this query is issued,
  710. * because gdb is doing and initial connect and the state
  711. * should be cleaned up.
  712. */
  713. gdb_breakpoint_remove_all();
  714. break;
  715. case 'c':
  716. if (*p != '\0') {
  717. addr = strtoull(p, (char **)&p, 16);
  718. gdb_set_cpu_pc(s, addr);
  719. }
  720. s->signal = 0;
  721. gdb_continue(s);
  722. return RS_IDLE;
  723. case 'C':
  724. s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
  725. if (s->signal == -1)
  726. s->signal = 0;
  727. gdb_continue(s);
  728. return RS_IDLE;
  729. case 'v':
  730. if (strncmp(p, "Cont", 4) == 0) {
  731. int res_signal, res_thread;
  732. p += 4;
  733. if (*p == '?') {
  734. put_packet(s, "vCont;c;C;s;S");
  735. break;
  736. }
  737. res = 0;
  738. res_signal = 0;
  739. res_thread = 0;
  740. while (*p) {
  741. int action, signal;
  742. if (*p++ != ';') {
  743. res = 0;
  744. break;
  745. }
  746. action = *p++;
  747. signal = 0;
  748. if (action == 'C' || action == 'S') {
  749. signal = gdb_signal_to_target(strtoul(p, (char **)&p, 16));
  750. if (signal == -1) {
  751. signal = 0;
  752. }
  753. } else if (action != 'c' && action != 's') {
  754. res = 0;
  755. break;
  756. }
  757. thread = 0;
  758. if (*p == ':') {
  759. thread = strtoull(p+1, (char **)&p, 16);
  760. }
  761. action = tolower(action);
  762. if (res == 0 || (res == 'c' && action == 's')) {
  763. res = action;
  764. res_signal = signal;
  765. res_thread = thread;
  766. }
  767. }
  768. if (res) {
  769. if (res_thread != -1 && res_thread != 0) {
  770. cpu = find_cpu(res_thread);
  771. if (cpu == NULL) {
  772. put_packet(s, "E22");
  773. break;
  774. }
  775. s->c_cpu = cpu;
  776. }
  777. if (res == 's') {
  778. cpu_single_step(s->c_cpu, sstep_flags);
  779. }
  780. s->signal = res_signal;
  781. gdb_continue(s);
  782. return RS_IDLE;
  783. }
  784. break;
  785. } else {
  786. goto unknown_command;
  787. }
  788. case 'k':
  789. #ifdef CONFIG_USER_ONLY
  790. /* Kill the target */
  791. fprintf(stderr, "\nQEMU: Terminated via GDBstub\n");
  792. exit(0);
  793. #endif
  794. case 'D':
  795. /* Detach packet */
  796. gdb_breakpoint_remove_all();
  797. gdb_syscall_mode = GDB_SYS_DISABLED;
  798. gdb_continue(s);
  799. put_packet(s, "OK");
  800. break;
  801. case 's':
  802. if (*p != '\0') {
  803. addr = strtoull(p, (char **)&p, 16);
  804. gdb_set_cpu_pc(s, addr);
  805. }
  806. cpu_single_step(s->c_cpu, sstep_flags);
  807. gdb_continue(s);
  808. return RS_IDLE;
  809. case 'F':
  810. {
  811. target_ulong ret;
  812. target_ulong err;
  813. ret = strtoull(p, (char **)&p, 16);
  814. if (*p == ',') {
  815. p++;
  816. err = strtoull(p, (char **)&p, 16);
  817. } else {
  818. err = 0;
  819. }
  820. if (*p == ',')
  821. p++;
  822. type = *p;
  823. if (s->current_syscall_cb) {
  824. s->current_syscall_cb(s->c_cpu, ret, err);
  825. s->current_syscall_cb = NULL;
  826. }
  827. if (type == 'C') {
  828. put_packet(s, "T02");
  829. } else {
  830. gdb_continue(s);
  831. }
  832. }
  833. break;
  834. case 'g':
  835. cpu_synchronize_state(s->g_cpu);
  836. len = 0;
  837. for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) {
  838. reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
  839. len += reg_size;
  840. }
  841. memtohex(buf, mem_buf, len);
  842. put_packet(s, buf);
  843. break;
  844. case 'G':
  845. cpu_synchronize_state(s->g_cpu);
  846. registers = mem_buf;
  847. len = strlen(p) / 2;
  848. hextomem((uint8_t *)registers, p, len);
  849. for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0; addr++) {
  850. reg_size = gdb_write_register(s->g_cpu, registers, addr);
  851. len -= reg_size;
  852. registers += reg_size;
  853. }
  854. put_packet(s, "OK");
  855. break;
  856. case 'm':
  857. addr = strtoull(p, (char **)&p, 16);
  858. if (*p == ',')
  859. p++;
  860. len = strtoull(p, NULL, 16);
  861. if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
  862. put_packet (s, "E14");
  863. } else {
  864. memtohex(buf, mem_buf, len);
  865. put_packet(s, buf);
  866. }
  867. break;
  868. case 'M':
  869. addr = strtoull(p, (char **)&p, 16);
  870. if (*p == ',')
  871. p++;
  872. len = strtoull(p, (char **)&p, 16);
  873. if (*p == ':')
  874. p++;
  875. hextomem(mem_buf, p, len);
  876. if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
  877. true) != 0) {
  878. put_packet(s, "E14");
  879. } else {
  880. put_packet(s, "OK");
  881. }
  882. break;
  883. case 'p':
  884. /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
  885. This works, but can be very slow. Anything new enough to
  886. understand XML also knows how to use this properly. */
  887. if (!gdb_has_xml)
  888. goto unknown_command;
  889. addr = strtoull(p, (char **)&p, 16);
  890. reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
  891. if (reg_size) {
  892. memtohex(buf, mem_buf, reg_size);
  893. put_packet(s, buf);
  894. } else {
  895. put_packet(s, "E14");
  896. }
  897. break;
  898. case 'P':
  899. if (!gdb_has_xml)
  900. goto unknown_command;
  901. addr = strtoull(p, (char **)&p, 16);
  902. if (*p == '=')
  903. p++;
  904. reg_size = strlen(p) / 2;
  905. hextomem(mem_buf, p, reg_size);
  906. gdb_write_register(s->g_cpu, mem_buf, addr);
  907. put_packet(s, "OK");
  908. break;
  909. case 'Z':
  910. case 'z':
  911. type = strtoul(p, (char **)&p, 16);
  912. if (*p == ',')
  913. p++;
  914. addr = strtoull(p, (char **)&p, 16);
  915. if (*p == ',')
  916. p++;
  917. len = strtoull(p, (char **)&p, 16);
  918. if (ch == 'Z')
  919. res = gdb_breakpoint_insert(addr, len, type);
  920. else
  921. res = gdb_breakpoint_remove(addr, len, type);
  922. if (res >= 0)
  923. put_packet(s, "OK");
  924. else if (res == -ENOSYS)
  925. put_packet(s, "");
  926. else
  927. put_packet(s, "E22");
  928. break;
  929. case 'H':
  930. type = *p++;
  931. thread = strtoull(p, (char **)&p, 16);
  932. if (thread == -1 || thread == 0) {
  933. put_packet(s, "OK");
  934. break;
  935. }
  936. cpu = find_cpu(thread);
  937. if (cpu == NULL) {
  938. put_packet(s, "E22");
  939. break;
  940. }
  941. switch (type) {
  942. case 'c':
  943. s->c_cpu = cpu;
  944. put_packet(s, "OK");
  945. break;
  946. case 'g':
  947. s->g_cpu = cpu;
  948. put_packet(s, "OK");
  949. break;
  950. default:
  951. put_packet(s, "E22");
  952. break;
  953. }
  954. break;
  955. case 'T':
  956. thread = strtoull(p, (char **)&p, 16);
  957. cpu = find_cpu(thread);
  958. if (cpu != NULL) {
  959. put_packet(s, "OK");
  960. } else {
  961. put_packet(s, "E22");
  962. }
  963. break;
  964. case 'q':
  965. case 'Q':
  966. /* parse any 'q' packets here */
  967. if (!strcmp(p,"qemu.sstepbits")) {
  968. /* Query Breakpoint bit definitions */
  969. snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
  970. SSTEP_ENABLE,
  971. SSTEP_NOIRQ,
  972. SSTEP_NOTIMER);
  973. put_packet(s, buf);
  974. break;
  975. } else if (strncmp(p,"qemu.sstep",10) == 0) {
  976. /* Display or change the sstep_flags */
  977. p += 10;
  978. if (*p != '=') {
  979. /* Display current setting */
  980. snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
  981. put_packet(s, buf);
  982. break;
  983. }
  984. p++;
  985. type = strtoul(p, (char **)&p, 16);
  986. sstep_flags = type;
  987. put_packet(s, "OK");
  988. break;
  989. } else if (strcmp(p,"C") == 0) {
  990. /* "Current thread" remains vague in the spec, so always return
  991. * the first CPU (gdb returns the first thread). */
  992. put_packet(s, "QC1");
  993. break;
  994. } else if (strcmp(p,"fThreadInfo") == 0) {
  995. s->query_cpu = first_cpu;
  996. goto report_cpuinfo;
  997. } else if (strcmp(p,"sThreadInfo") == 0) {
  998. report_cpuinfo:
  999. if (s->query_cpu) {
  1000. snprintf(buf, sizeof(buf), "m%x", cpu_index(s->query_cpu));
  1001. put_packet(s, buf);
  1002. s->query_cpu = CPU_NEXT(s->query_cpu);
  1003. } else
  1004. put_packet(s, "l");
  1005. break;
  1006. } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
  1007. thread = strtoull(p+16, (char **)&p, 16);
  1008. cpu = find_cpu(thread);
  1009. if (cpu != NULL) {
  1010. cpu_synchronize_state(cpu);
  1011. len = snprintf((char *)mem_buf, sizeof(mem_buf),
  1012. "CPU#%d [%s]", cpu->cpu_index,
  1013. cpu->halted ? "halted " : "running");
  1014. memtohex(buf, mem_buf, len);
  1015. put_packet(s, buf);
  1016. }
  1017. break;
  1018. }
  1019. #ifdef CONFIG_USER_ONLY
  1020. else if (strncmp(p, "Offsets", 7) == 0) {
  1021. TaskState *ts = s->c_cpu->opaque;
  1022. snprintf(buf, sizeof(buf),
  1023. "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
  1024. ";Bss=" TARGET_ABI_FMT_lx,
  1025. ts->info->code_offset,
  1026. ts->info->data_offset,
  1027. ts->info->data_offset);
  1028. put_packet(s, buf);
  1029. break;
  1030. }
  1031. #else /* !CONFIG_USER_ONLY */
  1032. else if (strncmp(p, "Rcmd,", 5) == 0) {
  1033. int len = strlen(p + 5);
  1034. if ((len % 2) != 0) {
  1035. put_packet(s, "E01");
  1036. break;
  1037. }
  1038. hextomem(mem_buf, p + 5, len);
  1039. len = len / 2;
  1040. mem_buf[len++] = 0;
  1041. qemu_chr_be_write(s->mon_chr, mem_buf, len);
  1042. put_packet(s, "OK");
  1043. break;
  1044. }
  1045. #endif /* !CONFIG_USER_ONLY */
  1046. if (strncmp(p, "Supported", 9) == 0) {
  1047. snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
  1048. cc = CPU_GET_CLASS(first_cpu);
  1049. if (cc->gdb_core_xml_file != NULL) {
  1050. pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
  1051. }
  1052. put_packet(s, buf);
  1053. break;
  1054. }
  1055. if (strncmp(p, "Xfer:features:read:", 19) == 0) {
  1056. const char *xml;
  1057. target_ulong total_len;
  1058. cc = CPU_GET_CLASS(first_cpu);
  1059. if (cc->gdb_core_xml_file == NULL) {
  1060. goto unknown_command;
  1061. }
  1062. gdb_has_xml = true;
  1063. p += 19;
  1064. xml = get_feature_xml(p, &p, cc);
  1065. if (!xml) {
  1066. snprintf(buf, sizeof(buf), "E00");
  1067. put_packet(s, buf);
  1068. break;
  1069. }
  1070. if (*p == ':')
  1071. p++;
  1072. addr = strtoul(p, (char **)&p, 16);
  1073. if (*p == ',')
  1074. p++;
  1075. len = strtoul(p, (char **)&p, 16);
  1076. total_len = strlen(xml);
  1077. if (addr > total_len) {
  1078. snprintf(buf, sizeof(buf), "E00");
  1079. put_packet(s, buf);
  1080. break;
  1081. }
  1082. if (len > (MAX_PACKET_LENGTH - 5) / 2)
  1083. len = (MAX_PACKET_LENGTH - 5) / 2;
  1084. if (len < total_len - addr) {
  1085. buf[0] = 'm';
  1086. len = memtox(buf + 1, xml + addr, len);
  1087. } else {
  1088. buf[0] = 'l';
  1089. len = memtox(buf + 1, xml + addr, total_len - addr);
  1090. }
  1091. put_packet_binary(s, buf, len + 1);
  1092. break;
  1093. }
  1094. /* Unrecognised 'q' command. */
  1095. goto unknown_command;
  1096. default:
  1097. unknown_command:
  1098. /* put empty packet */
  1099. buf[0] = '\0';
  1100. put_packet(s, buf);
  1101. break;
  1102. }
  1103. return RS_IDLE;
  1104. }
  1105. void gdb_set_stop_cpu(CPUState *cpu)
  1106. {
  1107. gdbserver_state->c_cpu = cpu;
  1108. gdbserver_state->g_cpu = cpu;
  1109. }
  1110. #ifndef CONFIG_USER_ONLY
  1111. static void gdb_vm_state_change(void *opaque, int running, RunState state)
  1112. {
  1113. GDBState *s = gdbserver_state;
  1114. CPUArchState *env = s->c_cpu->env_ptr;
  1115. CPUState *cpu = s->c_cpu;
  1116. char buf[256];
  1117. const char *type;
  1118. int ret;
  1119. if (running || s->state == RS_INACTIVE) {
  1120. return;
  1121. }
  1122. /* Is there a GDB syscall waiting to be sent? */
  1123. if (s->current_syscall_cb) {
  1124. put_packet(s, s->syscall_buf);
  1125. return;
  1126. }
  1127. switch (state) {
  1128. case RUN_STATE_DEBUG:
  1129. if (cpu->watchpoint_hit) {
  1130. switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
  1131. case BP_MEM_READ:
  1132. type = "r";
  1133. break;
  1134. case BP_MEM_ACCESS:
  1135. type = "a";
  1136. break;
  1137. default:
  1138. type = "";
  1139. break;
  1140. }
  1141. snprintf(buf, sizeof(buf),
  1142. "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
  1143. GDB_SIGNAL_TRAP, cpu_index(cpu), type,
  1144. (target_ulong)cpu->watchpoint_hit->vaddr);
  1145. cpu->watchpoint_hit = NULL;
  1146. goto send_packet;
  1147. }
  1148. tb_flush(env);
  1149. ret = GDB_SIGNAL_TRAP;
  1150. break;
  1151. case RUN_STATE_PAUSED:
  1152. ret = GDB_SIGNAL_INT;
  1153. break;
  1154. case RUN_STATE_SHUTDOWN:
  1155. ret = GDB_SIGNAL_QUIT;
  1156. break;
  1157. case RUN_STATE_IO_ERROR:
  1158. ret = GDB_SIGNAL_IO;
  1159. break;
  1160. case RUN_STATE_WATCHDOG:
  1161. ret = GDB_SIGNAL_ALRM;
  1162. break;
  1163. case RUN_STATE_INTERNAL_ERROR:
  1164. ret = GDB_SIGNAL_ABRT;
  1165. break;
  1166. case RUN_STATE_SAVE_VM:
  1167. case RUN_STATE_RESTORE_VM:
  1168. return;
  1169. case RUN_STATE_FINISH_MIGRATE:
  1170. ret = GDB_SIGNAL_XCPU;
  1171. break;
  1172. default:
  1173. ret = GDB_SIGNAL_UNKNOWN;
  1174. break;
  1175. }
  1176. snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_index(cpu));
  1177. send_packet:
  1178. put_packet(s, buf);
  1179. /* disable single step if it was enabled */
  1180. cpu_single_step(cpu, 0);
  1181. }
  1182. #endif
  1183. /* Send a gdb syscall request.
  1184. This accepts limited printf-style format specifiers, specifically:
  1185. %x - target_ulong argument printed in hex.
  1186. %lx - 64-bit argument printed in hex.
  1187. %s - string pointer (target_ulong) and length (int) pair. */
  1188. void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
  1189. {
  1190. va_list va;
  1191. char *p;
  1192. char *p_end;
  1193. target_ulong addr;
  1194. uint64_t i64;
  1195. GDBState *s;
  1196. s = gdbserver_state;
  1197. if (!s)
  1198. return;
  1199. s->current_syscall_cb = cb;
  1200. #ifndef CONFIG_USER_ONLY
  1201. vm_stop(RUN_STATE_DEBUG);
  1202. #endif
  1203. va_start(va, fmt);
  1204. p = s->syscall_buf;
  1205. p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
  1206. *(p++) = 'F';
  1207. while (*fmt) {
  1208. if (*fmt == '%') {
  1209. fmt++;
  1210. switch (*fmt++) {
  1211. case 'x':
  1212. addr = va_arg(va, target_ulong);
  1213. p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
  1214. break;
  1215. case 'l':
  1216. if (*(fmt++) != 'x')
  1217. goto bad_format;
  1218. i64 = va_arg(va, uint64_t);
  1219. p += snprintf(p, p_end - p, "%" PRIx64, i64);
  1220. break;
  1221. case 's':
  1222. addr = va_arg(va, target_ulong);
  1223. p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
  1224. addr, va_arg(va, int));
  1225. break;
  1226. default:
  1227. bad_format:
  1228. fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
  1229. fmt - 1);
  1230. break;
  1231. }
  1232. } else {
  1233. *(p++) = *(fmt++);
  1234. }
  1235. }
  1236. *p = 0;
  1237. va_end(va);
  1238. #ifdef CONFIG_USER_ONLY
  1239. put_packet(s, s->syscall_buf);
  1240. gdb_handlesig(s->c_cpu, 0);
  1241. #else
  1242. /* In this case wait to send the syscall packet until notification that
  1243. the CPU has stopped. This must be done because if the packet is sent
  1244. now the reply from the syscall request could be received while the CPU
  1245. is still in the running state, which can cause packets to be dropped
  1246. and state transition 'T' packets to be sent while the syscall is still
  1247. being processed. */
  1248. cpu_exit(s->c_cpu);
  1249. #endif
  1250. }
  1251. static void gdb_read_byte(GDBState *s, int ch)
  1252. {
  1253. int i, csum;
  1254. uint8_t reply;
  1255. #ifndef CONFIG_USER_ONLY
  1256. if (s->last_packet_len) {
  1257. /* Waiting for a response to the last packet. If we see the start
  1258. of a new command then abandon the previous response. */
  1259. if (ch == '-') {
  1260. #ifdef DEBUG_GDB
  1261. printf("Got NACK, retransmitting\n");
  1262. #endif
  1263. put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
  1264. }
  1265. #ifdef DEBUG_GDB
  1266. else if (ch == '+')
  1267. printf("Got ACK\n");
  1268. else
  1269. printf("Got '%c' when expecting ACK/NACK\n", ch);
  1270. #endif
  1271. if (ch == '+' || ch == '$')
  1272. s->last_packet_len = 0;
  1273. if (ch != '$')
  1274. return;
  1275. }
  1276. if (runstate_is_running()) {
  1277. /* when the CPU is running, we cannot do anything except stop
  1278. it when receiving a char */
  1279. vm_stop(RUN_STATE_PAUSED);
  1280. } else
  1281. #endif
  1282. {
  1283. switch(s->state) {
  1284. case RS_IDLE:
  1285. if (ch == '$') {
  1286. s->line_buf_index = 0;
  1287. s->state = RS_GETLINE;
  1288. }
  1289. break;
  1290. case RS_GETLINE:
  1291. if (ch == '#') {
  1292. s->state = RS_CHKSUM1;
  1293. } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
  1294. s->state = RS_IDLE;
  1295. } else {
  1296. s->line_buf[s->line_buf_index++] = ch;
  1297. }
  1298. break;
  1299. case RS_CHKSUM1:
  1300. s->line_buf[s->line_buf_index] = '\0';
  1301. s->line_csum = fromhex(ch) << 4;
  1302. s->state = RS_CHKSUM2;
  1303. break;
  1304. case RS_CHKSUM2:
  1305. s->line_csum |= fromhex(ch);
  1306. csum = 0;
  1307. for(i = 0; i < s->line_buf_index; i++) {
  1308. csum += s->line_buf[i];
  1309. }
  1310. if (s->line_csum != (csum & 0xff)) {
  1311. reply = '-';
  1312. put_buffer(s, &reply, 1);
  1313. s->state = RS_IDLE;
  1314. } else {
  1315. reply = '+';
  1316. put_buffer(s, &reply, 1);
  1317. s->state = gdb_handle_packet(s, s->line_buf);
  1318. }
  1319. break;
  1320. default:
  1321. abort();
  1322. }
  1323. }
  1324. }
  1325. /* Tell the remote gdb that the process has exited. */
  1326. void gdb_exit(CPUArchState *env, int code)
  1327. {
  1328. GDBState *s;
  1329. char buf[4];
  1330. s = gdbserver_state;
  1331. if (!s) {
  1332. return;
  1333. }
  1334. #ifdef CONFIG_USER_ONLY
  1335. if (gdbserver_fd < 0 || s->fd < 0) {
  1336. return;
  1337. }
  1338. #endif
  1339. snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
  1340. put_packet(s, buf);
  1341. #ifndef CONFIG_USER_ONLY
  1342. if (s->chr) {
  1343. qemu_chr_delete(s->chr);
  1344. }
  1345. #endif
  1346. }
  1347. #ifdef CONFIG_USER_ONLY
  1348. int
  1349. gdb_queuesig (void)
  1350. {
  1351. GDBState *s;
  1352. s = gdbserver_state;
  1353. if (gdbserver_fd < 0 || s->fd < 0)
  1354. return 0;
  1355. else
  1356. return 1;
  1357. }
  1358. int
  1359. gdb_handlesig(CPUState *cpu, int sig)
  1360. {
  1361. CPUArchState *env = cpu->env_ptr;
  1362. GDBState *s;
  1363. char buf[256];
  1364. int n;
  1365. s = gdbserver_state;
  1366. if (gdbserver_fd < 0 || s->fd < 0) {
  1367. return sig;
  1368. }
  1369. /* disable single step if it was enabled */
  1370. cpu_single_step(cpu, 0);
  1371. tb_flush(env);
  1372. if (sig != 0) {
  1373. snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
  1374. put_packet(s, buf);
  1375. }
  1376. /* put_packet() might have detected that the peer terminated the
  1377. connection. */
  1378. if (s->fd < 0) {
  1379. return sig;
  1380. }
  1381. sig = 0;
  1382. s->state = RS_IDLE;
  1383. s->running_state = 0;
  1384. while (s->running_state == 0) {
  1385. n = read(s->fd, buf, 256);
  1386. if (n > 0) {
  1387. int i;
  1388. for (i = 0; i < n; i++) {
  1389. gdb_read_byte(s, buf[i]);
  1390. }
  1391. } else if (n == 0 || errno != EAGAIN) {
  1392. /* XXX: Connection closed. Should probably wait for another
  1393. connection before continuing. */
  1394. return sig;
  1395. }
  1396. }
  1397. sig = s->signal;
  1398. s->signal = 0;
  1399. return sig;
  1400. }
  1401. /* Tell the remote gdb that the process has exited due to SIG. */
  1402. void gdb_signalled(CPUArchState *env, int sig)
  1403. {
  1404. GDBState *s;
  1405. char buf[4];
  1406. s = gdbserver_state;
  1407. if (gdbserver_fd < 0 || s->fd < 0) {
  1408. return;
  1409. }
  1410. snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
  1411. put_packet(s, buf);
  1412. }
  1413. static void gdb_accept(void)
  1414. {
  1415. GDBState *s;
  1416. struct sockaddr_in sockaddr;
  1417. socklen_t len;
  1418. int fd;
  1419. for(;;) {
  1420. len = sizeof(sockaddr);
  1421. fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
  1422. if (fd < 0 && errno != EINTR) {
  1423. perror("accept");
  1424. return;
  1425. } else if (fd >= 0) {
  1426. #ifndef _WIN32
  1427. fcntl(fd, F_SETFD, FD_CLOEXEC);
  1428. #endif
  1429. break;
  1430. }
  1431. }
  1432. /* set short latency */
  1433. socket_set_nodelay(fd);
  1434. s = g_malloc0(sizeof(GDBState));
  1435. s->c_cpu = first_cpu;
  1436. s->g_cpu = first_cpu;
  1437. s->fd = fd;
  1438. gdb_has_xml = false;
  1439. gdbserver_state = s;
  1440. fcntl(fd, F_SETFL, O_NONBLOCK);
  1441. }
  1442. static int gdbserver_open(int port)
  1443. {
  1444. struct sockaddr_in sockaddr;
  1445. int fd, ret;
  1446. fd = socket(PF_INET, SOCK_STREAM, 0);
  1447. if (fd < 0) {
  1448. perror("socket");
  1449. return -1;
  1450. }
  1451. #ifndef _WIN32
  1452. fcntl(fd, F_SETFD, FD_CLOEXEC);
  1453. #endif
  1454. socket_set_fast_reuse(fd);
  1455. sockaddr.sin_family = AF_INET;
  1456. sockaddr.sin_port = htons(port);
  1457. sockaddr.sin_addr.s_addr = 0;
  1458. ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
  1459. if (ret < 0) {
  1460. perror("bind");
  1461. close(fd);
  1462. return -1;
  1463. }
  1464. ret = listen(fd, 0);
  1465. if (ret < 0) {
  1466. perror("listen");
  1467. close(fd);
  1468. return -1;
  1469. }
  1470. return fd;
  1471. }
  1472. int gdbserver_start(int port)
  1473. {
  1474. gdbserver_fd = gdbserver_open(port);
  1475. if (gdbserver_fd < 0)
  1476. return -1;
  1477. /* accept connections */
  1478. gdb_accept();
  1479. return 0;
  1480. }
  1481. /* Disable gdb stub for child processes. */
  1482. void gdbserver_fork(CPUArchState *env)
  1483. {
  1484. CPUState *cpu = ENV_GET_CPU(env);
  1485. GDBState *s = gdbserver_state;
  1486. if (gdbserver_fd < 0 || s->fd < 0) {
  1487. return;
  1488. }
  1489. close(s->fd);
  1490. s->fd = -1;
  1491. cpu_breakpoint_remove_all(cpu, BP_GDB);
  1492. cpu_watchpoint_remove_all(cpu, BP_GDB);
  1493. }
  1494. #else
  1495. static int gdb_chr_can_receive(void *opaque)
  1496. {
  1497. /* We can handle an arbitrarily large amount of data.
  1498. Pick the maximum packet size, which is as good as anything. */
  1499. return MAX_PACKET_LENGTH;
  1500. }
  1501. static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
  1502. {
  1503. int i;
  1504. for (i = 0; i < size; i++) {
  1505. gdb_read_byte(gdbserver_state, buf[i]);
  1506. }
  1507. }
  1508. static void gdb_chr_event(void *opaque, int event)
  1509. {
  1510. switch (event) {
  1511. case CHR_EVENT_OPENED:
  1512. vm_stop(RUN_STATE_PAUSED);
  1513. gdb_has_xml = false;
  1514. break;
  1515. default:
  1516. break;
  1517. }
  1518. }
  1519. static void gdb_monitor_output(GDBState *s, const char *msg, int len)
  1520. {
  1521. char buf[MAX_PACKET_LENGTH];
  1522. buf[0] = 'O';
  1523. if (len > (MAX_PACKET_LENGTH/2) - 1)
  1524. len = (MAX_PACKET_LENGTH/2) - 1;
  1525. memtohex(buf + 1, (uint8_t *)msg, len);
  1526. put_packet(s, buf);
  1527. }
  1528. static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len)
  1529. {
  1530. const char *p = (const char *)buf;
  1531. int max_sz;
  1532. max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
  1533. for (;;) {
  1534. if (len <= max_sz) {
  1535. gdb_monitor_output(gdbserver_state, p, len);
  1536. break;
  1537. }
  1538. gdb_monitor_output(gdbserver_state, p, max_sz);
  1539. p += max_sz;
  1540. len -= max_sz;
  1541. }
  1542. return len;
  1543. }
  1544. #ifndef _WIN32
  1545. static void gdb_sigterm_handler(int signal)
  1546. {
  1547. if (runstate_is_running()) {
  1548. vm_stop(RUN_STATE_PAUSED);
  1549. }
  1550. }
  1551. #endif
  1552. int gdbserver_start(const char *device)
  1553. {
  1554. GDBState *s;
  1555. char gdbstub_device_name[128];
  1556. CharDriverState *chr = NULL;
  1557. CharDriverState *mon_chr;
  1558. if (!device)
  1559. return -1;
  1560. if (strcmp(device, "none") != 0) {
  1561. if (strstart(device, "tcp:", NULL)) {
  1562. /* enforce required TCP attributes */
  1563. snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
  1564. "%s,nowait,nodelay,server", device);
  1565. device = gdbstub_device_name;
  1566. }
  1567. #ifndef _WIN32
  1568. else if (strcmp(device, "stdio") == 0) {
  1569. struct sigaction act;
  1570. memset(&act, 0, sizeof(act));
  1571. act.sa_handler = gdb_sigterm_handler;
  1572. sigaction(SIGINT, &act, NULL);
  1573. }
  1574. #endif
  1575. chr = qemu_chr_new("gdb", device, NULL);
  1576. if (!chr)
  1577. return -1;
  1578. qemu_chr_fe_claim_no_fail(chr);
  1579. qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive,
  1580. gdb_chr_event, NULL);
  1581. }
  1582. s = gdbserver_state;
  1583. if (!s) {
  1584. s = g_malloc0(sizeof(GDBState));
  1585. gdbserver_state = s;
  1586. qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
  1587. /* Initialize a monitor terminal for gdb */
  1588. mon_chr = qemu_chr_alloc();
  1589. mon_chr->chr_write = gdb_monitor_write;
  1590. monitor_init(mon_chr, 0);
  1591. } else {
  1592. if (s->chr)
  1593. qemu_chr_delete(s->chr);
  1594. mon_chr = s->mon_chr;
  1595. memset(s, 0, sizeof(GDBState));
  1596. }
  1597. s->c_cpu = first_cpu;
  1598. s->g_cpu = first_cpu;
  1599. s->chr = chr;
  1600. s->state = chr ? RS_IDLE : RS_INACTIVE;
  1601. s->mon_chr = mon_chr;
  1602. s->current_syscall_cb = NULL;
  1603. return 0;
  1604. }
  1605. #endif