My QEMU builds
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1790 lines
45 KiB

qemu-char: add logfile facility to all chardev backends Typically a UNIX guest OS will log boot messages to a serial port in addition to any graphical console. An admin user may also wish to use the serial port for an interactive console. A virtualization management system may wish to collect system boot messages by logging the serial port, but also wish to allow admins interactive access. Currently providing such a feature forces the mgmt app to either provide 2 separate serial ports, one for logging boot messages and one for interactive console login, or to proxy all output via a separate service that can multiplex the two needs onto one serial port. While both are valid approaches, they each have their own downsides. The former causes confusion and extra setup work for VM admins creating disk images. The latter places an extra burden to re-implement much of the QEMU chardev backends logic in libvirt or even higher level mgmt apps and adds extra hops in the data transfer path. A simpler approach that is satisfactory for many use cases is to allow the QEMU chardev backends to have a "logfile" property associated with them. $QEMU -chardev socket,host=localhost,port=9000,\ server=on,nowait,id-charserial0,\ logfile=/var/log/libvirt/qemu/test-serial0.log -device isa-serial,chardev=charserial0,id=serial0 This patch introduces a 'ChardevCommon' struct which is setup as a base for all the ChardevBackend types. Ideally this would be registered directly as a base against ChardevBackend, rather than each type, but the QAPI generator doesn't allow that since the ChardevBackend is a non-discriminated union. The ChardevCommon struct provides the optional 'logfile' parameter, as well as 'logappend' which controls whether QEMU truncates or appends (default truncate). Signed-off-by: Daniel P. Berrange <berrange@redhat.com> Message-Id: <1452516281-27519-1-git-send-email-berrange@redhat.com> [Call qemu_chr_parse_common if cd->parse is NULL. - Paolo] Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
6 years ago
qemu-char: add logfile facility to all chardev backends Typically a UNIX guest OS will log boot messages to a serial port in addition to any graphical console. An admin user may also wish to use the serial port for an interactive console. A virtualization management system may wish to collect system boot messages by logging the serial port, but also wish to allow admins interactive access. Currently providing such a feature forces the mgmt app to either provide 2 separate serial ports, one for logging boot messages and one for interactive console login, or to proxy all output via a separate service that can multiplex the two needs onto one serial port. While both are valid approaches, they each have their own downsides. The former causes confusion and extra setup work for VM admins creating disk images. The latter places an extra burden to re-implement much of the QEMU chardev backends logic in libvirt or even higher level mgmt apps and adds extra hops in the data transfer path. A simpler approach that is satisfactory for many use cases is to allow the QEMU chardev backends to have a "logfile" property associated with them. $QEMU -chardev socket,host=localhost,port=9000,\ server=on,nowait,id-charserial0,\ logfile=/var/log/libvirt/qemu/test-serial0.log -device isa-serial,chardev=charserial0,id=serial0 This patch introduces a 'ChardevCommon' struct which is setup as a base for all the ChardevBackend types. Ideally this would be registered directly as a base against ChardevBackend, rather than each type, but the QAPI generator doesn't allow that since the ChardevBackend is a non-discriminated union. The ChardevCommon struct provides the optional 'logfile' parameter, as well as 'logappend' which controls whether QEMU truncates or appends (default truncate). Signed-off-by: Daniel P. Berrange <berrange@redhat.com> Message-Id: <1452516281-27519-1-git-send-email-berrange@redhat.com> [Call qemu_chr_parse_common if cd->parse is NULL. - Paolo] Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
6 years ago
  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 "qemu-common.h"
  21. #ifdef CONFIG_USER_ONLY
  22. #include "qemu.h"
  23. #else
  24. #include "monitor/monitor.h"
  25. #include "sysemu/char.h"
  26. #include "sysemu/sysemu.h"
  27. #include "exec/gdbstub.h"
  28. #endif
  29. #define MAX_PACKET_LENGTH 4096
  30. #include "cpu.h"
  31. #include "qemu/sockets.h"
  32. #include "sysemu/kvm.h"
  33. #include "exec/semihost.h"
  34. #ifdef CONFIG_USER_ONLY
  35. #define GDB_ATTACHED "0"
  36. #else
  37. #define GDB_ATTACHED "1"
  38. #endif
  39. static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
  40. uint8_t *buf, int len, bool is_write)
  41. {
  42. CPUClass *cc = CPU_GET_CLASS(cpu);
  43. if (cc->memory_rw_debug) {
  44. return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
  45. }
  46. return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
  47. }
  48. enum {
  49. GDB_SIGNAL_0 = 0,
  50. GDB_SIGNAL_INT = 2,
  51. GDB_SIGNAL_QUIT = 3,
  52. GDB_SIGNAL_TRAP = 5,
  53. GDB_SIGNAL_ABRT = 6,
  54. GDB_SIGNAL_ALRM = 14,
  55. GDB_SIGNAL_IO = 23,
  56. GDB_SIGNAL_XCPU = 24,
  57. GDB_SIGNAL_UNKNOWN = 143
  58. };
  59. #ifdef CONFIG_USER_ONLY
  60. /* Map target signal numbers to GDB protocol signal numbers and vice
  61. * versa. For user emulation's currently supported systems, we can
  62. * assume most signals are defined.
  63. */
  64. static int gdb_signal_table[] = {
  65. 0,
  66. TARGET_SIGHUP,
  67. TARGET_SIGINT,
  68. TARGET_SIGQUIT,
  69. TARGET_SIGILL,
  70. TARGET_SIGTRAP,
  71. TARGET_SIGABRT,
  72. -1, /* SIGEMT */
  73. TARGET_SIGFPE,
  74. TARGET_SIGKILL,
  75. TARGET_SIGBUS,
  76. TARGET_SIGSEGV,
  77. TARGET_SIGSYS,
  78. TARGET_SIGPIPE,
  79. TARGET_SIGALRM,
  80. TARGET_SIGTERM,
  81. TARGET_SIGURG,
  82. TARGET_SIGSTOP,
  83. TARGET_SIGTSTP,
  84. TARGET_SIGCONT,
  85. TARGET_SIGCHLD,
  86. TARGET_SIGTTIN,
  87. TARGET_SIGTTOU,
  88. TARGET_SIGIO,
  89. TARGET_SIGXCPU,
  90. TARGET_SIGXFSZ,
  91. TARGET_SIGVTALRM,
  92. TARGET_SIGPROF,
  93. TARGET_SIGWINCH,
  94. -1, /* SIGLOST */
  95. TARGET_SIGUSR1,
  96. TARGET_SIGUSR2,
  97. #ifdef TARGET_SIGPWR
  98. TARGET_SIGPWR,
  99. #else
  100. -1,
  101. #endif
  102. -1, /* SIGPOLL */
  103. -1,
  104. -1,
  105. -1,
  106. -1,
  107. -1,
  108. -1,
  109. -1,
  110. -1,
  111. -1,
  112. -1,
  113. -1,
  114. #ifdef __SIGRTMIN
  115. __SIGRTMIN + 1,
  116. __SIGRTMIN + 2,
  117. __SIGRTMIN + 3,
  118. __SIGRTMIN + 4,
  119. __SIGRTMIN + 5,
  120. __SIGRTMIN + 6,
  121. __SIGRTMIN + 7,
  122. __SIGRTMIN + 8,
  123. __SIGRTMIN + 9,
  124. __SIGRTMIN + 10,
  125. __SIGRTMIN + 11,
  126. __SIGRTMIN + 12,
  127. __SIGRTMIN + 13,
  128. __SIGRTMIN + 14,
  129. __SIGRTMIN + 15,
  130. __SIGRTMIN + 16,
  131. __SIGRTMIN + 17,
  132. __SIGRTMIN + 18,
  133. __SIGRTMIN + 19,
  134. __SIGRTMIN + 20,
  135. __SIGRTMIN + 21,
  136. __SIGRTMIN + 22,
  137. __SIGRTMIN + 23,
  138. __SIGRTMIN + 24,
  139. __SIGRTMIN + 25,
  140. __SIGRTMIN + 26,
  141. __SIGRTMIN + 27,
  142. __SIGRTMIN + 28,
  143. __SIGRTMIN + 29,
  144. __SIGRTMIN + 30,
  145. __SIGRTMIN + 31,
  146. -1, /* SIGCANCEL */
  147. __SIGRTMIN,
  148. __SIGRTMIN + 32,
  149. __SIGRTMIN + 33,
  150. __SIGRTMIN + 34,
  151. __SIGRTMIN + 35,
  152. __SIGRTMIN + 36,
  153. __SIGRTMIN + 37,
  154. __SIGRTMIN + 38,
  155. __SIGRTMIN + 39,
  156. __SIGRTMIN + 40,
  157. __SIGRTMIN + 41,
  158. __SIGRTMIN + 42,
  159. __SIGRTMIN + 43,
  160. __SIGRTMIN + 44,
  161. __SIGRTMIN + 45,
  162. __SIGRTMIN + 46,
  163. __SIGRTMIN + 47,
  164. __SIGRTMIN + 48,
  165. __SIGRTMIN + 49,
  166. __SIGRTMIN + 50,
  167. __SIGRTMIN + 51,
  168. __SIGRTMIN + 52,
  169. __SIGRTMIN + 53,
  170. __SIGRTMIN + 54,
  171. __SIGRTMIN + 55,
  172. __SIGRTMIN + 56,
  173. __SIGRTMIN + 57,
  174. __SIGRTMIN + 58,
  175. __SIGRTMIN + 59,
  176. __SIGRTMIN + 60,
  177. __SIGRTMIN + 61,
  178. __SIGRTMIN + 62,
  179. __SIGRTMIN + 63,
  180. __SIGRTMIN + 64,
  181. __SIGRTMIN + 65,
  182. __SIGRTMIN + 66,
  183. __SIGRTMIN + 67,
  184. __SIGRTMIN + 68,
  185. __SIGRTMIN + 69,
  186. __SIGRTMIN + 70,
  187. __SIGRTMIN + 71,
  188. __SIGRTMIN + 72,
  189. __SIGRTMIN + 73,
  190. __SIGRTMIN + 74,
  191. __SIGRTMIN + 75,
  192. __SIGRTMIN + 76,
  193. __SIGRTMIN + 77,
  194. __SIGRTMIN + 78,
  195. __SIGRTMIN + 79,
  196. __SIGRTMIN + 80,
  197. __SIGRTMIN + 81,
  198. __SIGRTMIN + 82,
  199. __SIGRTMIN + 83,
  200. __SIGRTMIN + 84,
  201. __SIGRTMIN + 85,
  202. __SIGRTMIN + 86,
  203. __SIGRTMIN + 87,
  204. __SIGRTMIN + 88,
  205. __SIGRTMIN + 89,
  206. __SIGRTMIN + 90,
  207. __SIGRTMIN + 91,
  208. __SIGRTMIN + 92,
  209. __SIGRTMIN + 93,
  210. __SIGRTMIN + 94,
  211. __SIGRTMIN + 95,
  212. -1, /* SIGINFO */
  213. -1, /* UNKNOWN */
  214. -1, /* DEFAULT */
  215. -1,
  216. -1,
  217. -1,
  218. -1,
  219. -1,
  220. -1
  221. #endif
  222. };
  223. #else
  224. /* In system mode we only need SIGINT and SIGTRAP; other signals
  225. are not yet supported. */
  226. enum {
  227. TARGET_SIGINT = 2,
  228. TARGET_SIGTRAP = 5
  229. };
  230. static int gdb_signal_table[] = {
  231. -1,
  232. -1,
  233. TARGET_SIGINT,
  234. -1,
  235. -1,
  236. TARGET_SIGTRAP
  237. };
  238. #endif
  239. #ifdef CONFIG_USER_ONLY
  240. static int target_signal_to_gdb (int sig)
  241. {
  242. int i;
  243. for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
  244. if (gdb_signal_table[i] == sig)
  245. return i;
  246. return GDB_SIGNAL_UNKNOWN;
  247. }
  248. #endif
  249. static int gdb_signal_to_target (int sig)
  250. {
  251. if (sig < ARRAY_SIZE (gdb_signal_table))
  252. return gdb_signal_table[sig];
  253. else
  254. return -1;
  255. }
  256. //#define DEBUG_GDB
  257. typedef struct GDBRegisterState {
  258. int base_reg;
  259. int num_regs;
  260. gdb_reg_cb get_reg;
  261. gdb_reg_cb set_reg;
  262. const char *xml;
  263. struct GDBRegisterState *next;
  264. } GDBRegisterState;
  265. enum RSState {
  266. RS_INACTIVE,
  267. RS_IDLE,
  268. RS_GETLINE,
  269. RS_CHKSUM1,
  270. RS_CHKSUM2,
  271. };
  272. typedef struct GDBState {
  273. CPUState *c_cpu; /* current CPU for step/continue ops */
  274. CPUState *g_cpu; /* current CPU for other ops */
  275. CPUState *query_cpu; /* for q{f|s}ThreadInfo */
  276. enum RSState state; /* parsing state */
  277. char line_buf[MAX_PACKET_LENGTH];
  278. int line_buf_index;
  279. int line_csum;
  280. uint8_t last_packet[MAX_PACKET_LENGTH + 4];
  281. int last_packet_len;
  282. int signal;
  283. #ifdef CONFIG_USER_ONLY
  284. int fd;
  285. int running_state;
  286. #else
  287. CharDriverState *chr;
  288. CharDriverState *mon_chr;
  289. #endif
  290. char syscall_buf[256];
  291. gdb_syscall_complete_cb current_syscall_cb;
  292. } GDBState;
  293. /* By default use no IRQs and no timers while single stepping so as to
  294. * make single stepping like an ICE HW step.
  295. */
  296. static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
  297. static GDBState *gdbserver_state;
  298. bool gdb_has_xml;
  299. #ifdef CONFIG_USER_ONLY
  300. /* XXX: This is not thread safe. Do we care? */
  301. static int gdbserver_fd = -1;
  302. static int get_char(GDBState *s)
  303. {
  304. uint8_t ch;
  305. int ret;
  306. for(;;) {
  307. ret = qemu_recv(s->fd, &ch, 1, 0);
  308. if (ret < 0) {
  309. if (errno == ECONNRESET)
  310. s->fd = -1;
  311. if (errno != EINTR && errno != EAGAIN)
  312. return -1;
  313. } else if (ret == 0) {
  314. close(s->fd);
  315. s->fd = -1;
  316. return -1;
  317. } else {
  318. break;
  319. }
  320. }
  321. return ch;
  322. }
  323. #endif
  324. static enum {
  325. GDB_SYS_UNKNOWN,
  326. GDB_SYS_ENABLED,
  327. GDB_SYS_DISABLED,
  328. } gdb_syscall_mode;
  329. /* Decide if either remote gdb syscalls or native file IO should be used. */
  330. int use_gdb_syscalls(void)
  331. {
  332. SemihostingTarget target = semihosting_get_target();
  333. if (target == SEMIHOSTING_TARGET_NATIVE) {
  334. /* -semihosting-config target=native */
  335. return false;
  336. } else if (target == SEMIHOSTING_TARGET_GDB) {
  337. /* -semihosting-config target=gdb */
  338. return true;
  339. }
  340. /* -semihosting-config target=auto */
  341. /* On the first call check if gdb is connected and remember. */
  342. if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
  343. gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
  344. : GDB_SYS_DISABLED);
  345. }
  346. return gdb_syscall_mode == GDB_SYS_ENABLED;
  347. }
  348. /* Resume execution. */
  349. static inline void gdb_continue(GDBState *s)
  350. {
  351. #ifdef CONFIG_USER_ONLY
  352. s->running_state = 1;
  353. #else
  354. if (!runstate_needs_reset()) {
  355. vm_start();
  356. }
  357. #endif
  358. }
  359. static void put_buffer(GDBState *s, const uint8_t *buf, int len)
  360. {
  361. #ifdef CONFIG_USER_ONLY
  362. int ret;
  363. while (len > 0) {
  364. ret = send(s->fd, buf, len, 0);
  365. if (ret < 0) {
  366. if (errno != EINTR && errno != EAGAIN)
  367. return;
  368. } else {
  369. buf += ret;
  370. len -= ret;
  371. }
  372. }
  373. #else
  374. qemu_chr_fe_write(s->chr, buf, len);
  375. #endif
  376. }
  377. static inline int fromhex(int v)
  378. {
  379. if (v >= '0' && v <= '9')
  380. return v - '0';
  381. else if (v >= 'A' && v <= 'F')
  382. return v - 'A' + 10;
  383. else if (v >= 'a' && v <= 'f')
  384. return v - 'a' + 10;
  385. else
  386. return 0;
  387. }
  388. static inline int tohex(int v)
  389. {
  390. if (v < 10)
  391. return v + '0';
  392. else
  393. return v - 10 + 'a';
  394. }
  395. static void memtohex(char *buf, const uint8_t *mem, int len)
  396. {
  397. int i, c;
  398. char *q;
  399. q = buf;
  400. for(i = 0; i < len; i++) {
  401. c = mem[i];
  402. *q++ = tohex(c >> 4);
  403. *q++ = tohex(c & 0xf);
  404. }
  405. *q = '\0';
  406. }
  407. static void hextomem(uint8_t *mem, const char *buf, int len)
  408. {
  409. int i;
  410. for(i = 0; i < len; i++) {
  411. mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
  412. buf += 2;
  413. }
  414. }
  415. /* return -1 if error, 0 if OK */
  416. static int put_packet_binary(GDBState *s, const char *buf, int len)
  417. {
  418. int csum, i;
  419. uint8_t *p;
  420. for(;;) {
  421. p = s->last_packet;
  422. *(p++) = '$';
  423. memcpy(p, buf, len);
  424. p += len;
  425. csum = 0;
  426. for(i = 0; i < len; i++) {
  427. csum += buf[i];
  428. }
  429. *(p++) = '#';
  430. *(p++) = tohex((csum >> 4) & 0xf);
  431. *(p++) = tohex((csum) & 0xf);
  432. s->last_packet_len = p - s->last_packet;
  433. put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
  434. #ifdef CONFIG_USER_ONLY
  435. i = get_char(s);
  436. if (i < 0)
  437. return -1;
  438. if (i == '+')
  439. break;
  440. #else
  441. break;
  442. #endif
  443. }
  444. return 0;
  445. }
  446. /* return -1 if error, 0 if OK */
  447. static int put_packet(GDBState *s, const char *buf)
  448. {
  449. #ifdef DEBUG_GDB
  450. printf("reply='%s'\n", buf);
  451. #endif
  452. return put_packet_binary(s, buf, strlen(buf));
  453. }
  454. /* Encode data using the encoding for 'x' packets. */
  455. static int memtox(char *buf, const char *mem, int len)
  456. {
  457. char *p = buf;
  458. char c;
  459. while (len--) {
  460. c = *(mem++);
  461. switch (c) {
  462. case '#': case '$': case '*': case '}':
  463. *(p++) = '}';
  464. *(p++) = c ^ 0x20;
  465. break;
  466. default:
  467. *(p++) = c;
  468. break;
  469. }
  470. }
  471. return p - buf;
  472. }
  473. static const char *get_feature_xml(const char *p, const char **newp,
  474. CPUClass *cc)
  475. {
  476. size_t len;
  477. int i;
  478. const char *name;
  479. static char target_xml[1024];
  480. len = 0;
  481. while (p[len] && p[len] != ':')
  482. len++;
  483. *newp = p + len;
  484. name = NULL;
  485. if (strncmp(p, "target.xml", len) == 0) {
  486. /* Generate the XML description for this CPU. */
  487. if (!target_xml[0]) {