[gdb-remote] Sort entries in QEnvironment*

Similar to ae316ac66f for
QEMU_(UN)SET_ENV.
The iteration order of StringMap is not guaranteed to be deterministic.
Sort the entries to give deterministic packets for the tests added by
D108018.
This commit is contained in:
Fangrui Song 2023-07-22 16:45:32 -07:00
parent 4c73549ebf
commit 5d66f9fd8e
2 changed files with 13 additions and 8 deletions

View File

@ -35,6 +35,7 @@
#include "lldb/Host/Config.h"
#include "lldb/Utility/StringExtractorGDBRemote.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/JSON.h"
@ -826,8 +827,12 @@ llvm::Error GDBRemoteCommunicationClient::LaunchProcess(const Args &args) {
}
int GDBRemoteCommunicationClient::SendEnvironment(const Environment &env) {
for (const auto &KV : env) {
int r = SendEnvironmentPacket(Environment::compose(KV).c_str());
llvm::SmallVector<std::pair<llvm::StringRef, llvm::StringRef>, 0> vec;
for (const auto &kv : env)
vec.emplace_back(kv.first(), kv.second);
llvm::sort(vec, llvm::less_first());
for (const auto &[k, v] : vec) {
int r = SendEnvironmentPacket((k + "=" + v).str().c_str());
if (r != 0)
return r;
}

View File

@ -371,12 +371,12 @@ class TestGDBRemoteClient(GDBRemoteTestBase):
self.assertPacketLogContains(
[
"QEnvironment:EQUALS=foo=bar",
"QEnvironmentHexEncoded:4e45454453454e433d66726f6224",
"QEnvironmentHexEncoded:4e45454453454e43323d66722a6f62",
"QEnvironmentHexEncoded:4e45454453454e43333d66726f7d62",
"QEnvironmentHexEncoded:4e45454453454e43343d6623726f62",
"QEnvironment:PLAIN=foo",
"QEnvironmentHexEncoded:4e45454453454e43323d66722a6f62",
"QEnvironmentHexEncoded:4e45454453454e433d66726f6224",
"QEnvironment:EQUALS=foo=bar",
]
)
@ -425,12 +425,12 @@ class TestGDBRemoteClient(GDBRemoteTestBase):
self.assertPacketLogContains(
[
"QEnvironmentHexEncoded:455155414c533d666f6f3d626172",
"QEnvironmentHexEncoded:4e45454453454e433d66726f6224",
"QEnvironmentHexEncoded:4e45454453454e43323d66722a6f62",
"QEnvironmentHexEncoded:4e45454453454e43333d66726f7d62",
"QEnvironmentHexEncoded:4e45454453454e43343d6623726f62",
"QEnvironmentHexEncoded:504c41494e3d666f6f",
"QEnvironmentHexEncoded:4e45454453454e43323d66722a6f62",
"QEnvironmentHexEncoded:4e45454453454e433d66726f6224",
"QEnvironmentHexEncoded:455155414c533d666f6f3d626172",
]
)