gdbstub.c 52 KB

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