2
0

gdbstub.c 54 KB

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