diff options
Diffstat (limited to 'src/core/gdbstub/gdbstub.cpp')
| -rw-r--r-- | src/core/gdbstub/gdbstub.cpp | 75 |
1 files changed, 37 insertions, 38 deletions
diff --git a/src/core/gdbstub/gdbstub.cpp b/src/core/gdbstub/gdbstub.cpp index e4f337a0a..46606b992 100644 --- a/src/core/gdbstub/gdbstub.cpp +++ b/src/core/gdbstub/gdbstub.cpp | |||
| @@ -6,7 +6,6 @@ | |||
| 6 | 6 | ||
| 7 | #include <algorithm> | 7 | #include <algorithm> |
| 8 | #include <atomic> | 8 | #include <atomic> |
| 9 | #include <cinttypes> | ||
| 10 | #include <climits> | 9 | #include <climits> |
| 11 | #include <csignal> | 10 | #include <csignal> |
| 12 | #include <cstdarg> | 11 | #include <cstdarg> |
| @@ -180,7 +179,7 @@ static u8 HexCharToValue(u8 hex) { | |||
| 180 | return hex - 'A' + 0xA; | 179 | return hex - 'A' + 0xA; |
| 181 | } | 180 | } |
| 182 | 181 | ||
| 183 | LOG_ERROR(Debug_GDBStub, "Invalid nibble: %c (%02x)\n", hex, hex); | 182 | NGLOG_ERROR(Debug_GDBStub, "Invalid nibble: {} ({:02X})", hex, hex); |
| 184 | return 0; | 183 | return 0; |
| 185 | } | 184 | } |
| 186 | 185 | ||
| @@ -320,7 +319,7 @@ static u8 ReadByte() { | |||
| 320 | u8 c; | 319 | u8 c; |
| 321 | size_t received_size = recv(gdbserver_socket, reinterpret_cast<char*>(&c), 1, MSG_WAITALL); | 320 | size_t received_size = recv(gdbserver_socket, reinterpret_cast<char*>(&c), 1, MSG_WAITALL); |
| 322 | if (received_size != 1) { | 321 | if (received_size != 1) { |
| 323 | LOG_ERROR(Debug_GDBStub, "recv failed : %ld", received_size); | 322 | NGLOG_ERROR(Debug_GDBStub, "recv failed: {}", received_size); |
| 324 | Shutdown(); | 323 | Shutdown(); |
| 325 | } | 324 | } |
| 326 | 325 | ||
| @@ -361,9 +360,8 @@ static void RemoveBreakpoint(BreakpointType type, PAddr addr) { | |||
| 361 | 360 | ||
| 362 | auto bp = p.find(static_cast<u64>(addr)); | 361 | auto bp = p.find(static_cast<u64>(addr)); |
| 363 | if (bp != p.end()) { | 362 | if (bp != p.end()) { |
| 364 | LOG_DEBUG(Debug_GDBStub, | 363 | NGLOG_DEBUG(Debug_GDBStub, "gdb: removed a breakpoint: {:016X} bytes at {:016X} of type {}", |
| 365 | "gdb: removed a breakpoint: %016" PRIx64 " bytes at %016" PRIx64 " of type %d\n", | 364 | bp->second.len, bp->second.addr, static_cast<int>(type)); |
| 366 | bp->second.len, bp->second.addr, static_cast<int>(type)); | ||
| 367 | p.erase(static_cast<u64>(addr)); | 365 | p.erase(static_cast<u64>(addr)); |
| 368 | } | 366 | } |
| 369 | } | 367 | } |
| @@ -408,10 +406,10 @@ bool CheckBreakpoint(PAddr addr, BreakpointType type) { | |||
| 408 | } | 406 | } |
| 409 | 407 | ||
| 410 | if (bp->second.active && (addr >= bp->second.addr && addr < bp->second.addr + len)) { | 408 | if (bp->second.active && (addr >= bp->second.addr && addr < bp->second.addr + len)) { |
| 411 | LOG_DEBUG(Debug_GDBStub, | 409 | NGLOG_DEBUG(Debug_GDBStub, |
| 412 | "Found breakpoint type %d @ %016" PRIx64 ", range: %016" PRIx64 | 410 | "Found breakpoint type {} @ {:016X}, range: {:016X}" |
| 413 | " - %016" PRIx64 " (%" PRIx64 " bytes)\n", | 411 | " - {:016X} ({:X} bytes)", |
| 414 | static_cast<int>(type), addr, bp->second.addr, bp->second.addr + len, len); | 412 | static_cast<int>(type), addr, bp->second.addr, bp->second.addr + len, len); |
| 415 | return true; | 413 | return true; |
| 416 | } | 414 | } |
| 417 | } | 415 | } |
| @@ -427,7 +425,7 @@ bool CheckBreakpoint(PAddr addr, BreakpointType type) { | |||
| 427 | static void SendPacket(const char packet) { | 425 | static void SendPacket(const char packet) { |
| 428 | size_t sent_size = send(gdbserver_socket, &packet, 1, 0); | 426 | size_t sent_size = send(gdbserver_socket, &packet, 1, 0); |
| 429 | if (sent_size != 1) { | 427 | if (sent_size != 1) { |
| 430 | LOG_ERROR(Debug_GDBStub, "send failed"); | 428 | NGLOG_ERROR(Debug_GDBStub, "send failed"); |
| 431 | } | 429 | } |
| 432 | } | 430 | } |
| 433 | 431 | ||
| @@ -445,7 +443,7 @@ static void SendReply(const char* reply) { | |||
| 445 | 443 | ||
| 446 | command_length = static_cast<u32>(strlen(reply)); | 444 | command_length = static_cast<u32>(strlen(reply)); |
| 447 | if (command_length + 4 > sizeof(command_buffer)) { | 445 | if (command_length + 4 > sizeof(command_buffer)) { |
| 448 | LOG_ERROR(Debug_GDBStub, "command_buffer overflow in SendReply"); | 446 | NGLOG_ERROR(Debug_GDBStub, "command_buffer overflow in SendReply"); |
| 449 | return; | 447 | return; |
| 450 | } | 448 | } |
| 451 | 449 | ||
| @@ -462,7 +460,7 @@ static void SendReply(const char* reply) { | |||
| 462 | while (left > 0) { | 460 | while (left > 0) { |
| 463 | int sent_size = send(gdbserver_socket, reinterpret_cast<char*>(ptr), left, 0); | 461 | int sent_size = send(gdbserver_socket, reinterpret_cast<char*>(ptr), left, 0); |
| 464 | if (sent_size < 0) { | 462 | if (sent_size < 0) { |
| 465 | LOG_ERROR(Debug_GDBStub, "gdb: send failed"); | 463 | NGLOG_ERROR(Debug_GDBStub, "gdb: send failed"); |
| 466 | return Shutdown(); | 464 | return Shutdown(); |
| 467 | } | 465 | } |
| 468 | 466 | ||
| @@ -473,7 +471,7 @@ static void SendReply(const char* reply) { | |||
| 473 | 471 | ||
| 474 | /// Handle query command from gdb client. | 472 | /// Handle query command from gdb client. |
| 475 | static void HandleQuery() { | 473 | static void HandleQuery() { |
| 476 | LOG_DEBUG(Debug_GDBStub, "gdb: query '%s'\n", command_buffer + 1); | 474 | NGLOG_DEBUG(Debug_GDBStub, "gdb: query '{}'", command_buffer + 1); |
| 477 | 475 | ||
| 478 | const char* query = reinterpret_cast<const char*>(command_buffer + 1); | 476 | const char* query = reinterpret_cast<const char*>(command_buffer + 1); |
| 479 | 477 | ||
| @@ -512,8 +510,8 @@ static void SendSignal(u32 signal) { | |||
| 512 | 510 | ||
| 513 | latest_signal = signal; | 511 | latest_signal = signal; |
| 514 | 512 | ||
| 515 | std::string buffer = Common::StringFromFormat("T%02x", latest_signal); | 513 | std::string buffer = fmt::format("T{:02x}", latest_signal); |
| 516 | LOG_DEBUG(Debug_GDBStub, "Response: %s", buffer.c_str()); | 514 | NGLOG_DEBUG(Debug_GDBStub, "Response: {}", buffer); |
| 517 | SendReply(buffer.c_str()); | 515 | SendReply(buffer.c_str()); |
| 518 | } | 516 | } |
| 519 | 517 | ||
| @@ -527,18 +525,18 @@ static void ReadCommand() { | |||
| 527 | // ignore ack | 525 | // ignore ack |
| 528 | return; | 526 | return; |
| 529 | } else if (c == 0x03) { | 527 | } else if (c == 0x03) { |
| 530 | LOG_INFO(Debug_GDBStub, "gdb: found break command\n"); | 528 | NGLOG_INFO(Debug_GDBStub, "gdb: found break command"); |
| 531 | halt_loop = true; | 529 | halt_loop = true; |
| 532 | SendSignal(SIGTRAP); | 530 | SendSignal(SIGTRAP); |
| 533 | return; | 531 | return; |
| 534 | } else if (c != GDB_STUB_START) { | 532 | } else if (c != GDB_STUB_START) { |
| 535 | LOG_DEBUG(Debug_GDBStub, "gdb: read invalid byte %02x\n", c); | 533 | NGLOG_DEBUG(Debug_GDBStub, "gdb: read invalid byte {:02X}", c); |
| 536 | return; | 534 | return; |
| 537 | } | 535 | } |
| 538 | 536 | ||
| 539 | while ((c = ReadByte()) != GDB_STUB_END) { | 537 | while ((c = ReadByte()) != GDB_STUB_END) { |
| 540 | if (command_length >= sizeof(command_buffer)) { | 538 | if (command_length >= sizeof(command_buffer)) { |
| 541 | LOG_ERROR(Debug_GDBStub, "gdb: command_buffer overflow\n"); | 539 | NGLOG_ERROR(Debug_GDBStub, "gdb: command_buffer overflow"); |
| 542 | SendPacket(GDB_STUB_NACK); | 540 | SendPacket(GDB_STUB_NACK); |
| 543 | return; | 541 | return; |
| 544 | } | 542 | } |
| @@ -551,9 +549,10 @@ static void ReadCommand() { | |||
| 551 | u8 checksum_calculated = CalculateChecksum(command_buffer, command_length); | 549 | u8 checksum_calculated = CalculateChecksum(command_buffer, command_length); |
| 552 | 550 | ||
| 553 | if (checksum_received != checksum_calculated) { | 551 | if (checksum_received != checksum_calculated) { |
| 554 | LOG_ERROR(Debug_GDBStub, | 552 | NGLOG_ERROR( |
| 555 | "gdb: invalid checksum: calculated %02x and read %02x for $%s# (length: %d)\n", | 553 | Debug_GDBStub, |
| 556 | checksum_calculated, checksum_received, command_buffer, command_length); | 554 | "gdb: invalid checksum: calculated {:02X} and read {:02X} for ${}# (length: {})", |
| 555 | checksum_calculated, checksum_received, command_buffer, command_length); | ||
| 557 | 556 | ||
| 558 | command_length = 0; | 557 | command_length = 0; |
| 559 | 558 | ||
| @@ -580,7 +579,7 @@ static bool IsDataAvailable() { | |||
| 580 | t.tv_usec = 0; | 579 | t.tv_usec = 0; |
| 581 | 580 | ||
| 582 | if (select(gdbserver_socket + 1, &fd_socket, nullptr, nullptr, &t) < 0) { | 581 | if (select(gdbserver_socket + 1, &fd_socket, nullptr, nullptr, &t) < 0) { |
| 583 | LOG_ERROR(Debug_GDBStub, "select failed"); | 582 | NGLOG_ERROR(Debug_GDBStub, "select failed"); |
| 584 | return false; | 583 | return false; |
| 585 | } | 584 | } |
| 586 | 585 | ||
| @@ -693,7 +692,7 @@ static void ReadMemory() { | |||
| 693 | u64 len = | 692 | u64 len = |
| 694 | HexToLong(start_offset, static_cast<u64>((command_buffer + command_length) - start_offset)); | 693 | HexToLong(start_offset, static_cast<u64>((command_buffer + command_length) - start_offset)); |
| 695 | 694 | ||
| 696 | LOG_DEBUG(Debug_GDBStub, "gdb: addr: %016lx len: %016lx\n", addr, len); | 695 | NGLOG_DEBUG(Debug_GDBStub, "gdb: addr: {:016X} len: {:016X}", addr, len); |
| 697 | 696 | ||
| 698 | if (len * 2 > sizeof(reply)) { | 697 | if (len * 2 > sizeof(reply)) { |
| 699 | SendReply("E01"); | 698 | SendReply("E01"); |
| @@ -781,8 +780,8 @@ static bool CommitBreakpoint(BreakpointType type, PAddr addr, u64 len) { | |||
| 781 | breakpoint.len = len; | 780 | breakpoint.len = len; |
| 782 | p.insert({addr, breakpoint}); | 781 | p.insert({addr, breakpoint}); |
| 783 | 782 | ||
| 784 | LOG_DEBUG(Debug_GDBStub, "gdb: added %d breakpoint: %016" PRIx64 " bytes at %016" PRIx64 "\n", | 783 | NGLOG_DEBUG(Debug_GDBStub, "gdb: added {} breakpoint: {:016X} bytes at {:016X}", |
| 785 | static_cast<int>(type), breakpoint.len, breakpoint.addr); | 784 | static_cast<int>(type), breakpoint.len, breakpoint.addr); |
| 786 | 785 | ||
| 787 | return true; | 786 | return true; |
| 788 | } | 787 | } |
| @@ -889,7 +888,7 @@ void HandlePacket() { | |||
| 889 | return; | 888 | return; |
| 890 | } | 889 | } |
| 891 | 890 | ||
| 892 | LOG_DEBUG(Debug_GDBStub, "Packet: %s", command_buffer); | 891 | NGLOG_DEBUG(Debug_GDBStub, "Packet: {}", command_buffer); |
| 893 | 892 | ||
| 894 | switch (command_buffer[0]) { | 893 | switch (command_buffer[0]) { |
| 895 | case 'q': | 894 | case 'q': |
| @@ -903,7 +902,7 @@ void HandlePacket() { | |||
| 903 | break; | 902 | break; |
| 904 | case 'k': | 903 | case 'k': |
| 905 | Shutdown(); | 904 | Shutdown(); |
| 906 | LOG_INFO(Debug_GDBStub, "killed by gdb"); | 905 | NGLOG_INFO(Debug_GDBStub, "killed by gdb"); |
| 907 | return; | 906 | return; |
| 908 | case 'g': | 907 | case 'g': |
| 909 | ReadRegisters(); | 908 | ReadRegisters(); |
| @@ -982,7 +981,7 @@ static void Init(u16 port) { | |||
| 982 | breakpoints_write.clear(); | 981 | breakpoints_write.clear(); |
| 983 | 982 | ||
| 984 | // Start gdb server | 983 | // Start gdb server |
| 985 | LOG_INFO(Debug_GDBStub, "Starting GDB server on port %d...", port); | 984 | NGLOG_INFO(Debug_GDBStub, "Starting GDB server on port {}...", port); |
| 986 | 985 | ||
| 987 | sockaddr_in saddr_server = {}; | 986 | sockaddr_in saddr_server = {}; |
| 988 | saddr_server.sin_family = AF_INET; | 987 | saddr_server.sin_family = AF_INET; |
| @@ -995,28 +994,28 @@ static void Init(u16 port) { | |||
| 995 | 994 | ||
| 996 | int tmpsock = static_cast<int>(socket(PF_INET, SOCK_STREAM, 0)); | 995 | int tmpsock = static_cast<int>(socket(PF_INET, SOCK_STREAM, 0)); |
| 997 | if (tmpsock == -1) { | 996 | if (tmpsock == -1) { |
| 998 | LOG_ERROR(Debug_GDBStub, "Failed to create gdb socket"); | 997 | NGLOG_ERROR(Debug_GDBStub, "Failed to create gdb socket"); |
| 999 | } | 998 | } |
| 1000 | 999 | ||
| 1001 | // Set socket to SO_REUSEADDR so it can always bind on the same port | 1000 | // Set socket to SO_REUSEADDR so it can always bind on the same port |
| 1002 | int reuse_enabled = 1; | 1001 | int reuse_enabled = 1; |
| 1003 | if (setsockopt(tmpsock, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse_enabled, | 1002 | if (setsockopt(tmpsock, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse_enabled, |
| 1004 | sizeof(reuse_enabled)) < 0) { | 1003 | sizeof(reuse_enabled)) < 0) { |
| 1005 | LOG_ERROR(Debug_GDBStub, "Failed to set gdb socket option"); | 1004 | NGLOG_ERROR(Debug_GDBStub, "Failed to set gdb socket option"); |
| 1006 | } | 1005 | } |
| 1007 | 1006 | ||
| 1008 | const sockaddr* server_addr = reinterpret_cast<const sockaddr*>(&saddr_server); | 1007 | const sockaddr* server_addr = reinterpret_cast<const sockaddr*>(&saddr_server); |
| 1009 | socklen_t server_addrlen = sizeof(saddr_server); | 1008 | socklen_t server_addrlen = sizeof(saddr_server); |
| 1010 | if (bind(tmpsock, server_addr, server_addrlen) < 0) { | 1009 | if (bind(tmpsock, server_addr, server_addrlen) < 0) { |
| 1011 | LOG_ERROR(Debug_GDBStub, "Failed to bind gdb socket"); | 1010 | NGLOG_ERROR(Debug_GDBStub, "Failed to bind gdb socket"); |
| 1012 | } | 1011 | } |
| 1013 | 1012 | ||
| 1014 | if (listen(tmpsock, 1) < 0) { | 1013 | if (listen(tmpsock, 1) < 0) { |
| 1015 | LOG_ERROR(Debug_GDBStub, "Failed to listen to gdb socket"); | 1014 | NGLOG_ERROR(Debug_GDBStub, "Failed to listen to gdb socket"); |
| 1016 | } | 1015 | } |
| 1017 | 1016 | ||
| 1018 | // Wait for gdb to connect | 1017 | // Wait for gdb to connect |
| 1019 | LOG_INFO(Debug_GDBStub, "Waiting for gdb to connect...\n"); | 1018 | NGLOG_INFO(Debug_GDBStub, "Waiting for gdb to connect..."); |
| 1020 | sockaddr_in saddr_client; | 1019 | sockaddr_in saddr_client; |
| 1021 | sockaddr* client_addr = reinterpret_cast<sockaddr*>(&saddr_client); | 1020 | sockaddr* client_addr = reinterpret_cast<sockaddr*>(&saddr_client); |
| 1022 | socklen_t client_addrlen = sizeof(saddr_client); | 1021 | socklen_t client_addrlen = sizeof(saddr_client); |
| @@ -1027,9 +1026,9 @@ static void Init(u16 port) { | |||
| 1027 | halt_loop = false; | 1026 | halt_loop = false; |
| 1028 | step_loop = false; | 1027 | step_loop = false; |
| 1029 | 1028 | ||
| 1030 | LOG_ERROR(Debug_GDBStub, "Failed to accept gdb client"); | 1029 | NGLOG_ERROR(Debug_GDBStub, "Failed to accept gdb client"); |
| 1031 | } else { | 1030 | } else { |
| 1032 | LOG_INFO(Debug_GDBStub, "Client connected.\n"); | 1031 | NGLOG_INFO(Debug_GDBStub, "Client connected."); |
| 1033 | saddr_client.sin_addr.s_addr = ntohl(saddr_client.sin_addr.s_addr); | 1032 | saddr_client.sin_addr.s_addr = ntohl(saddr_client.sin_addr.s_addr); |
| 1034 | } | 1033 | } |
| 1035 | 1034 | ||
| @@ -1048,7 +1047,7 @@ void Shutdown() { | |||
| 1048 | return; | 1047 | return; |
| 1049 | } | 1048 | } |
| 1050 | 1049 | ||
| 1051 | LOG_INFO(Debug_GDBStub, "Stopping GDB ..."); | 1050 | NGLOG_INFO(Debug_GDBStub, "Stopping GDB ..."); |
| 1052 | if (gdbserver_socket != -1) { | 1051 | if (gdbserver_socket != -1) { |
| 1053 | shutdown(gdbserver_socket, SHUT_RDWR); | 1052 | shutdown(gdbserver_socket, SHUT_RDWR); |
| 1054 | gdbserver_socket = -1; | 1053 | gdbserver_socket = -1; |
| @@ -1058,7 +1057,7 @@ void Shutdown() { | |||
| 1058 | WSACleanup(); | 1057 | WSACleanup(); |
| 1059 | #endif | 1058 | #endif |
| 1060 | 1059 | ||
| 1061 | LOG_INFO(Debug_GDBStub, "GDB stopped."); | 1060 | NGLOG_INFO(Debug_GDBStub, "GDB stopped."); |
| 1062 | } | 1061 | } |
| 1063 | 1062 | ||
| 1064 | bool IsServerEnabled() { | 1063 | bool IsServerEnabled() { |