Skip to content

Commit 02ef0f5

Browse files
committed
[lldb] [gdb-remote client] Refactor SetCurrentThread*()
Refactor SetCurrentThread() and SetCurrentThreadForRun() to reduce code duplication and simplify it. Both methods now call common SendSetCurrentThreadPacket() that implements the common protocol exchange part (the only variable is sending `Hg` vs `Hc`) and returns the selected TID. The logic is rewritten to use a StreamString instead of snprintf(). A side effect of the change is that thread-id sent is now zero-padded. However, this should not have practical impact on the server as both forms are equivalent. Differential Revision: https://reviews.llvm.org/D100459
1 parent cdca178 commit 02ef0f5

File tree

3 files changed

+33
-53
lines changed

3 files changed

+33
-53
lines changed

lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp

Lines changed: 29 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2639,75 +2639,53 @@ bool GDBRemoteCommunicationClient::KillSpawnedProcess(lldb::pid_t pid) {
26392639
return false;
26402640
}
26412641

2642-
bool GDBRemoteCommunicationClient::SetCurrentThread(uint64_t tid) {
2643-
if (m_curr_tid == tid)
2644-
return true;
2645-
2646-
char packet[32];
2647-
int packet_len;
2642+
llvm::Optional<uint64_t>
2643+
GDBRemoteCommunicationClient::SendSetCurrentThreadPacket(uint64_t tid,
2644+
char op) {
2645+
lldb_private::StreamString packet;
2646+
packet.PutChar('H');
2647+
packet.PutChar(op);
26482648
if (tid == UINT64_MAX)
2649-
packet_len = ::snprintf(packet, sizeof(packet), "Hg-1");
2649+
packet.PutCString("-1");
26502650
else
2651-
packet_len = ::snprintf(packet, sizeof(packet), "Hg%" PRIx64, tid);
2652-
assert(packet_len + 1 < (int)sizeof(packet));
2653-
UNUSED_IF_ASSERT_DISABLED(packet_len);
2651+
packet.PutHex64(tid);
26542652
StringExtractorGDBRemote response;
2655-
if (SendPacketAndWaitForResponse(packet, response, false) ==
2653+
if (SendPacketAndWaitForResponse(packet.GetString(), response, false) ==
26562654
PacketResult::Success) {
2657-
if (response.IsOKResponse()) {
2658-
m_curr_tid = tid;
2659-
return true;
2660-
}
2655+
if (response.IsOKResponse())
2656+
return tid;
26612657

26622658
/*
26632659
* Connected bare-iron target (like YAMON gdb-stub) may not have support for
26642660
* Hg packet.
26652661
* The reply from '?' packet could be as simple as 'S05'. There is no packet
26662662
* which can
26672663
* give us pid and/or tid. Assume pid=tid=1 in such cases.
2668-
*/
2669-
if (response.IsUnsupportedResponse() && IsConnected()) {
2670-
m_curr_tid = 1;
2671-
return true;
2672-
}
2664+
*/
2665+
if (response.IsUnsupportedResponse() && IsConnected())
2666+
return 1;
26732667
}
2674-
return false;
2668+
return llvm::None;
26752669
}
26762670

2677-
bool GDBRemoteCommunicationClient::SetCurrentThreadForRun(uint64_t tid) {
2678-
if (m_curr_tid_run == tid)
2671+
bool GDBRemoteCommunicationClient::SetCurrentThread(uint64_t tid) {
2672+
if (m_curr_tid == tid)
26792673
return true;
26802674

2681-
char packet[32];
2682-
int packet_len;
2683-
if (tid == UINT64_MAX)
2684-
packet_len = ::snprintf(packet, sizeof(packet), "Hc-1");
2685-
else
2686-
packet_len = ::snprintf(packet, sizeof(packet), "Hc%" PRIx64, tid);
2675+
llvm::Optional<uint64_t> ret = SendSetCurrentThreadPacket(tid, 'g');
2676+
if (ret.hasValue())
2677+
m_curr_tid = ret.getValue();
2678+
return ret.hasValue();
2679+
}
26872680

2688-
assert(packet_len + 1 < (int)sizeof(packet));
2689-
UNUSED_IF_ASSERT_DISABLED(packet_len);
2690-
StringExtractorGDBRemote response;
2691-
if (SendPacketAndWaitForResponse(packet, response, false) ==
2692-
PacketResult::Success) {
2693-
if (response.IsOKResponse()) {
2694-
m_curr_tid_run = tid;
2695-
return true;
2696-
}
2681+
bool GDBRemoteCommunicationClient::SetCurrentThreadForRun(uint64_t tid) {
2682+
if (m_curr_tid_run == tid)
2683+
return true;
26972684

2698-
/*
2699-
* Connected bare-iron target (like YAMON gdb-stub) may not have support for
2700-
* Hc packet.
2701-
* The reply from '?' packet could be as simple as 'S05'. There is no packet
2702-
* which can
2703-
* give us pid and/or tid. Assume pid=tid=1 in such cases.
2704-
*/
2705-
if (response.IsUnsupportedResponse() && IsConnected()) {
2706-
m_curr_tid_run = 1;
2707-
return true;
2708-
}
2709-
}
2710-
return false;
2685+
llvm::Optional<uint64_t> ret = SendSetCurrentThreadPacket(tid, 'c');
2686+
if (ret.hasValue())
2687+
m_curr_tid_run = ret.getValue();
2688+
return ret.hasValue();
27112689
}
27122690

27132691
bool GDBRemoteCommunicationClient::GetStopReply(

lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ class GDBRemoteCommunicationClient : public GDBRemoteClientBase {
336336
// and response times.
337337
bool SendSpeedTestPacket(uint32_t send_size, uint32_t recv_size);
338338

339+
llvm::Optional<uint64_t> SendSetCurrentThreadPacket(uint64_t tid, char op);
340+
339341
bool SetCurrentThread(uint64_t tid);
340342

341343
bool SetCurrentThreadForRun(uint64_t tid);

lldb/unittests/Process/gdb-remote/GDBRemoteCommunicationClientTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ TEST_F(GDBRemoteCommunicationClientTest, WriteRegisterNoSuffix) {
9898
});
9999

100100
Handle_QThreadSuffixSupported(server, false);
101-
HandlePacket(server, "Hg47", "OK");
101+
HandlePacket(server, "Hg0000000000000047", "OK");
102102
HandlePacket(server, "P4=" + one_register_hex, "OK");
103103
ASSERT_TRUE(write_result.get());
104104

@@ -143,7 +143,7 @@ TEST_F(GDBRemoteCommunicationClientTest, SaveRestoreRegistersNoSuffix) {
143143
return client.SaveRegisterState(tid, save_id);
144144
});
145145
Handle_QThreadSuffixSupported(server, false);
146-
HandlePacket(server, "Hg47", "OK");
146+
HandlePacket(server, "Hg0000000000000047", "OK");
147147
HandlePacket(server, "QSaveRegisterState", "1");
148148
ASSERT_TRUE(async_result.get());
149149
EXPECT_EQ(1u, save_id);

0 commit comments

Comments
 (0)