system.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. * gdb server stub - softmmu specific bits
  3. *
  4. * Debug integration depends on support from the individual
  5. * accelerators so most of this involves calling the ops helpers.
  6. *
  7. * Copyright (c) 2003-2005 Fabrice Bellard
  8. * Copyright (c) 2022 Linaro Ltd
  9. *
  10. * SPDX-License-Identifier: LGPL-2.0+
  11. */
  12. #include "qemu/osdep.h"
  13. #include "qapi/error.h"
  14. #include "qemu/error-report.h"
  15. #include "qemu/cutils.h"
  16. #include "exec/gdbstub.h"
  17. #include "gdbstub/syscalls.h"
  18. #include "exec/hwaddr.h"
  19. #include "exec/tb-flush.h"
  20. #include "sysemu/cpus.h"
  21. #include "sysemu/runstate.h"
  22. #include "sysemu/replay.h"
  23. #include "hw/core/cpu.h"
  24. #include "hw/cpu/cluster.h"
  25. #include "hw/boards.h"
  26. #include "chardev/char.h"
  27. #include "chardev/char-fe.h"
  28. #include "monitor/monitor.h"
  29. #include "trace.h"
  30. #include "internals.h"
  31. /* System emulation specific state */
  32. typedef struct {
  33. CharBackend chr;
  34. Chardev *mon_chr;
  35. } GDBSystemState;
  36. GDBSystemState gdbserver_system_state;
  37. static void reset_gdbserver_state(void)
  38. {
  39. g_free(gdbserver_state.processes);
  40. gdbserver_state.processes = NULL;
  41. gdbserver_state.process_num = 0;
  42. gdbserver_state.allow_stop_reply = false;
  43. }
  44. /*
  45. * Return the GDB index for a given vCPU state.
  46. *
  47. * In system mode GDB numbers CPUs from 1 as 0 is reserved as an "any
  48. * cpu" index.
  49. */
  50. int gdb_get_cpu_index(CPUState *cpu)
  51. {
  52. return cpu->cpu_index + 1;
  53. }
  54. /*
  55. * We check the status of the last message in the chardev receive code
  56. */
  57. bool gdb_got_immediate_ack(void)
  58. {
  59. return true;
  60. }
  61. /*
  62. * GDB Connection management. For system emulation we do all of this
  63. * via our existing Chardev infrastructure which allows us to support
  64. * network and unix sockets.
  65. */
  66. void gdb_put_buffer(const uint8_t *buf, int len)
  67. {
  68. /*
  69. * XXX this blocks entire thread. Rewrite to use
  70. * qemu_chr_fe_write and background I/O callbacks
  71. */
  72. qemu_chr_fe_write_all(&gdbserver_system_state.chr, buf, len);
  73. }
  74. static void gdb_chr_event(void *opaque, QEMUChrEvent event)
  75. {
  76. int i;
  77. GDBState *s = (GDBState *) opaque;
  78. switch (event) {
  79. case CHR_EVENT_OPENED:
  80. /* Start with first process attached, others detached */
  81. for (i = 0; i < s->process_num; i++) {
  82. s->processes[i].attached = !i;
  83. }
  84. s->c_cpu = gdb_first_attached_cpu();
  85. s->g_cpu = s->c_cpu;
  86. vm_stop(RUN_STATE_PAUSED);
  87. replay_gdb_attached();
  88. break;
  89. default:
  90. break;
  91. }
  92. }
  93. /*
  94. * In system-mode we stop the VM and wait to send the syscall packet
  95. * until notification that the CPU has stopped. This must be done
  96. * because if the packet is sent now the reply from the syscall
  97. * request could be received while the CPU is still in the running
  98. * state, which can cause packets to be dropped and state transition
  99. * 'T' packets to be sent while the syscall is still being processed.
  100. */
  101. void gdb_syscall_handling(const char *syscall_packet)
  102. {
  103. vm_stop(RUN_STATE_DEBUG);
  104. qemu_cpu_kick(gdbserver_state.c_cpu);
  105. }
  106. static void gdb_vm_state_change(void *opaque, bool running, RunState state)
  107. {
  108. CPUState *cpu = gdbserver_state.c_cpu;
  109. g_autoptr(GString) buf = g_string_new(NULL);
  110. g_autoptr(GString) tid = g_string_new(NULL);
  111. const char *type;
  112. int ret;
  113. if (running || gdbserver_state.state == RS_INACTIVE) {
  114. return;
  115. }
  116. /* Is there a GDB syscall waiting to be sent? */
  117. if (gdb_handled_syscall()) {
  118. return;
  119. }
  120. if (cpu == NULL) {
  121. /* No process attached */
  122. return;
  123. }
  124. if (!gdbserver_state.allow_stop_reply) {
  125. return;
  126. }
  127. gdb_append_thread_id(cpu, tid);
  128. switch (state) {
  129. case RUN_STATE_DEBUG:
  130. if (cpu->watchpoint_hit) {
  131. switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
  132. case BP_MEM_READ:
  133. type = "r";
  134. break;
  135. case BP_MEM_ACCESS:
  136. type = "a";
  137. break;
  138. default:
  139. type = "";
  140. break;
  141. }
  142. trace_gdbstub_hit_watchpoint(type,
  143. gdb_get_cpu_index(cpu),
  144. cpu->watchpoint_hit->vaddr);
  145. g_string_printf(buf, "T%02xthread:%s;%swatch:%" VADDR_PRIx ";",
  146. GDB_SIGNAL_TRAP, tid->str, type,
  147. cpu->watchpoint_hit->vaddr);
  148. cpu->watchpoint_hit = NULL;
  149. goto send_packet;
  150. } else {
  151. trace_gdbstub_hit_break();
  152. }
  153. tb_flush(cpu);
  154. ret = GDB_SIGNAL_TRAP;
  155. break;
  156. case RUN_STATE_PAUSED:
  157. trace_gdbstub_hit_paused();
  158. ret = GDB_SIGNAL_INT;
  159. break;
  160. case RUN_STATE_SHUTDOWN:
  161. trace_gdbstub_hit_shutdown();
  162. ret = GDB_SIGNAL_QUIT;
  163. break;
  164. case RUN_STATE_IO_ERROR:
  165. trace_gdbstub_hit_io_error();
  166. ret = GDB_SIGNAL_IO;
  167. break;
  168. case RUN_STATE_WATCHDOG:
  169. trace_gdbstub_hit_watchdog();
  170. ret = GDB_SIGNAL_ALRM;
  171. break;
  172. case RUN_STATE_INTERNAL_ERROR:
  173. trace_gdbstub_hit_internal_error();
  174. ret = GDB_SIGNAL_ABRT;
  175. break;
  176. case RUN_STATE_SAVE_VM:
  177. case RUN_STATE_RESTORE_VM:
  178. return;
  179. case RUN_STATE_FINISH_MIGRATE:
  180. ret = GDB_SIGNAL_XCPU;
  181. break;
  182. default:
  183. trace_gdbstub_hit_unknown(state);
  184. ret = GDB_SIGNAL_UNKNOWN;
  185. break;
  186. }
  187. gdb_set_stop_cpu(cpu);
  188. g_string_printf(buf, "T%02xthread:%s;", ret, tid->str);
  189. send_packet:
  190. gdb_put_packet(buf->str);
  191. gdbserver_state.allow_stop_reply = false;
  192. /* disable single step if it was enabled */
  193. cpu_single_step(cpu, 0);
  194. }
  195. #ifndef _WIN32
  196. static void gdb_sigterm_handler(int signal)
  197. {
  198. if (runstate_is_running()) {
  199. vm_stop(RUN_STATE_PAUSED);
  200. }
  201. }
  202. #endif
  203. static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
  204. {
  205. g_autoptr(GString) hex_buf = g_string_new("O");
  206. gdb_memtohex(hex_buf, buf, len);
  207. gdb_put_packet(hex_buf->str);
  208. return len;
  209. }
  210. static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
  211. bool *be_opened, Error **errp)
  212. {
  213. *be_opened = false;
  214. }
  215. static void char_gdb_class_init(ObjectClass *oc, void *data)
  216. {
  217. ChardevClass *cc = CHARDEV_CLASS(oc);
  218. cc->internal = true;
  219. cc->open = gdb_monitor_open;
  220. cc->chr_write = gdb_monitor_write;
  221. }
  222. #define TYPE_CHARDEV_GDB "chardev-gdb"
  223. static const TypeInfo char_gdb_type_info = {
  224. .name = TYPE_CHARDEV_GDB,
  225. .parent = TYPE_CHARDEV,
  226. .class_init = char_gdb_class_init,
  227. };
  228. static int gdb_chr_can_receive(void *opaque)
  229. {
  230. /*
  231. * We can handle an arbitrarily large amount of data.
  232. * Pick the maximum packet size, which is as good as anything.
  233. */
  234. return MAX_PACKET_LENGTH;
  235. }
  236. static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
  237. {
  238. int i;
  239. for (i = 0; i < size; i++) {
  240. gdb_read_byte(buf[i]);
  241. }
  242. }
  243. static int find_cpu_clusters(Object *child, void *opaque)
  244. {
  245. if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) {
  246. GDBState *s = (GDBState *) opaque;
  247. CPUClusterState *cluster = CPU_CLUSTER(child);
  248. GDBProcess *process;
  249. s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
  250. process = &s->processes[s->process_num - 1];
  251. /*
  252. * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
  253. * runtime, we enforce here that the machine does not use a cluster ID
  254. * that would lead to PID 0.
  255. */
  256. assert(cluster->cluster_id != UINT32_MAX);
  257. process->pid = cluster->cluster_id + 1;
  258. process->attached = false;
  259. process->target_xml = NULL;
  260. return 0;
  261. }
  262. return object_child_foreach(child, find_cpu_clusters, opaque);
  263. }
  264. static int pid_order(const void *a, const void *b)
  265. {
  266. GDBProcess *pa = (GDBProcess *) a;
  267. GDBProcess *pb = (GDBProcess *) b;
  268. if (pa->pid < pb->pid) {
  269. return -1;
  270. } else if (pa->pid > pb->pid) {
  271. return 1;
  272. } else {
  273. return 0;
  274. }
  275. }
  276. static void create_processes(GDBState *s)
  277. {
  278. object_child_foreach(object_get_root(), find_cpu_clusters, s);
  279. if (gdbserver_state.processes) {
  280. /* Sort by PID */
  281. qsort(gdbserver_state.processes,
  282. gdbserver_state.process_num,
  283. sizeof(gdbserver_state.processes[0]),
  284. pid_order);
  285. }
  286. gdb_create_default_process(s);
  287. }
  288. int gdbserver_start(const char *device)
  289. {
  290. Chardev *chr = NULL;
  291. Chardev *mon_chr;
  292. g_autoptr(GString) cs = g_string_new(device);
  293. if (!first_cpu) {
  294. error_report("gdbstub: meaningless to attach gdb to a "
  295. "machine without any CPU.");
  296. return -1;
  297. }
  298. if (!gdb_supports_guest_debug()) {
  299. error_report("gdbstub: current accelerator doesn't "
  300. "support guest debugging");
  301. return -1;
  302. }
  303. if (cs->len == 0) {
  304. return -1;
  305. }
  306. trace_gdbstub_op_start(cs->str);
  307. if (g_strcmp0(cs->str, "none") != 0) {
  308. if (g_str_has_prefix(cs->str, "tcp:")) {
  309. /* enforce required TCP attributes */
  310. g_string_append_printf(cs, ",wait=off,nodelay=on,server=on");
  311. }
  312. #ifndef _WIN32
  313. else if (strcmp(device, "stdio") == 0) {
  314. struct sigaction act;
  315. memset(&act, 0, sizeof(act));
  316. act.sa_handler = gdb_sigterm_handler;
  317. sigaction(SIGINT, &act, NULL);
  318. }
  319. #endif
  320. /*
  321. * FIXME: it's a bit weird to allow using a mux chardev here
  322. * and implicitly setup a monitor. We may want to break this.
  323. */
  324. chr = qemu_chr_new_noreplay("gdb", cs->str, true, NULL);
  325. if (!chr) {
  326. return -1;
  327. }
  328. }
  329. if (!gdbserver_state.init) {
  330. gdb_init_gdbserver_state();
  331. qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
  332. /* Initialize a monitor terminal for gdb */
  333. mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
  334. NULL, NULL, &error_abort);
  335. monitor_init_hmp(mon_chr, false, &error_abort);
  336. } else {
  337. qemu_chr_fe_deinit(&gdbserver_system_state.chr, true);
  338. mon_chr = gdbserver_system_state.mon_chr;
  339. reset_gdbserver_state();
  340. }
  341. create_processes(&gdbserver_state);
  342. if (chr) {
  343. qemu_chr_fe_init(&gdbserver_system_state.chr, chr, &error_abort);
  344. qemu_chr_fe_set_handlers(&gdbserver_system_state.chr,
  345. gdb_chr_can_receive,
  346. gdb_chr_receive, gdb_chr_event,
  347. NULL, &gdbserver_state, NULL, true);
  348. }
  349. gdbserver_state.state = chr ? RS_IDLE : RS_INACTIVE;
  350. gdbserver_system_state.mon_chr = mon_chr;
  351. gdb_syscall_reset();
  352. return 0;
  353. }
  354. static void register_types(void)
  355. {
  356. type_register_static(&char_gdb_type_info);
  357. }
  358. type_init(register_types);
  359. /* Tell the remote gdb that the process has exited. */
  360. void gdb_exit(int code)
  361. {
  362. char buf[4];
  363. if (!gdbserver_state.init) {
  364. return;
  365. }
  366. trace_gdbstub_op_exiting((uint8_t)code);
  367. if (gdbserver_state.allow_stop_reply) {
  368. snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
  369. gdb_put_packet(buf);
  370. gdbserver_state.allow_stop_reply = false;
  371. }
  372. qemu_chr_fe_deinit(&gdbserver_system_state.chr, true);
  373. }
  374. /*
  375. * Memory access
  376. */
  377. static int phy_memory_mode;
  378. int gdb_target_memory_rw_debug(CPUState *cpu, hwaddr addr,
  379. uint8_t *buf, int len, bool is_write)
  380. {
  381. CPUClass *cc;
  382. if (phy_memory_mode) {
  383. if (is_write) {
  384. cpu_physical_memory_write(addr, buf, len);
  385. } else {
  386. cpu_physical_memory_read(addr, buf, len);
  387. }
  388. return 0;
  389. }
  390. cc = CPU_GET_CLASS(cpu);
  391. if (cc->memory_rw_debug) {
  392. return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
  393. }
  394. return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
  395. }
  396. /*
  397. * cpu helpers
  398. */
  399. unsigned int gdb_get_max_cpus(void)
  400. {
  401. MachineState *ms = MACHINE(qdev_get_machine());
  402. return ms->smp.max_cpus;
  403. }
  404. bool gdb_can_reverse(void)
  405. {
  406. return replay_mode == REPLAY_MODE_PLAY;
  407. }
  408. /*
  409. * Softmmu specific command helpers
  410. */
  411. void gdb_handle_query_qemu_phy_mem_mode(GArray *params,
  412. void *user_ctx)
  413. {
  414. g_string_printf(gdbserver_state.str_buf, "%d", phy_memory_mode);
  415. gdb_put_strbuf();
  416. }
  417. void gdb_handle_set_qemu_phy_mem_mode(GArray *params, void *user_ctx)
  418. {
  419. if (!params->len) {
  420. gdb_put_packet("E22");
  421. return;
  422. }
  423. if (!get_param(params, 0)->val_ul) {
  424. phy_memory_mode = 0;
  425. } else {
  426. phy_memory_mode = 1;
  427. }
  428. gdb_put_packet("OK");
  429. }
  430. void gdb_handle_query_rcmd(GArray *params, void *user_ctx)
  431. {
  432. const guint8 zero = 0;
  433. int len;
  434. if (!params->len) {
  435. gdb_put_packet("E22");
  436. return;
  437. }
  438. len = strlen(get_param(params, 0)->data);
  439. if (len % 2) {
  440. gdb_put_packet("E01");
  441. return;
  442. }
  443. g_assert(gdbserver_state.mem_buf->len == 0);
  444. len = len / 2;
  445. gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 0)->data, len);
  446. g_byte_array_append(gdbserver_state.mem_buf, &zero, 1);
  447. qemu_chr_be_write(gdbserver_system_state.mon_chr,
  448. gdbserver_state.mem_buf->data,
  449. gdbserver_state.mem_buf->len);
  450. gdb_put_packet("OK");
  451. }
  452. /*
  453. * Execution state helpers
  454. */
  455. void gdb_handle_query_attached(GArray *params, void *user_ctx)
  456. {
  457. gdb_put_packet("1");
  458. }
  459. void gdb_continue(void)
  460. {
  461. if (!runstate_needs_reset()) {
  462. trace_gdbstub_op_continue();
  463. vm_start();
  464. }
  465. }
  466. /*
  467. * Resume execution, per CPU actions.
  468. */
  469. int gdb_continue_partial(char *newstates)
  470. {
  471. CPUState *cpu;
  472. int res = 0;
  473. int flag = 0;
  474. if (!runstate_needs_reset()) {
  475. bool step_requested = false;
  476. CPU_FOREACH(cpu) {
  477. if (newstates[cpu->cpu_index] == 's') {
  478. step_requested = true;
  479. break;
  480. }
  481. }
  482. if (vm_prepare_start(step_requested)) {
  483. return 0;
  484. }
  485. CPU_FOREACH(cpu) {
  486. switch (newstates[cpu->cpu_index]) {
  487. case 0:
  488. case 1:
  489. break; /* nothing to do here */
  490. case 's':
  491. trace_gdbstub_op_stepping(cpu->cpu_index);
  492. cpu_single_step(cpu, gdbserver_state.sstep_flags);
  493. cpu_resume(cpu);
  494. flag = 1;
  495. break;
  496. case 'c':
  497. trace_gdbstub_op_continue_cpu(cpu->cpu_index);
  498. cpu_resume(cpu);
  499. flag = 1;
  500. break;
  501. default:
  502. res = -1;
  503. break;
  504. }
  505. }
  506. }
  507. if (flag) {
  508. qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
  509. }
  510. return res;
  511. }
  512. /*
  513. * Signal Handling - in system mode we only need SIGINT and SIGTRAP; other
  514. * signals are not yet supported.
  515. */
  516. enum {
  517. TARGET_SIGINT = 2,
  518. TARGET_SIGTRAP = 5
  519. };
  520. int gdb_signal_to_target(int sig)
  521. {
  522. switch (sig) {
  523. case 2:
  524. return TARGET_SIGINT;
  525. case 5:
  526. return TARGET_SIGTRAP;
  527. default:
  528. return -1;
  529. }
  530. }
  531. /*
  532. * Break/Watch point helpers
  533. */
  534. bool gdb_supports_guest_debug(void)
  535. {
  536. const AccelOpsClass *ops = cpus_get_accel();
  537. if (ops->supports_guest_debug) {
  538. return ops->supports_guest_debug();
  539. }
  540. return false;
  541. }
  542. int gdb_breakpoint_insert(CPUState *cs, int type, vaddr addr, vaddr len)
  543. {
  544. const AccelOpsClass *ops = cpus_get_accel();
  545. if (ops->insert_breakpoint) {
  546. return ops->insert_breakpoint(cs, type, addr, len);
  547. }
  548. return -ENOSYS;
  549. }
  550. int gdb_breakpoint_remove(CPUState *cs, int type, vaddr addr, vaddr len)
  551. {
  552. const AccelOpsClass *ops = cpus_get_accel();
  553. if (ops->remove_breakpoint) {
  554. return ops->remove_breakpoint(cs, type, addr, len);
  555. }
  556. return -ENOSYS;
  557. }
  558. void gdb_breakpoint_remove_all(CPUState *cs)
  559. {
  560. const AccelOpsClass *ops = cpus_get_accel();
  561. if (ops->remove_all_breakpoints) {
  562. ops->remove_all_breakpoints(cs);
  563. }
  564. }