system.c 16 KB

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