system.c 16 KB

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