diff --git a/lldb/include/lldb/Utility/ArchSpec.h b/lldb/include/lldb/Utility/ArchSpec.h --- a/lldb/include/lldb/Utility/ArchSpec.h +++ b/lldb/include/lldb/Utility/ArchSpec.h @@ -16,6 +16,7 @@ #include "lldb/lldb-private-enumerations.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/Triple.h" +#include "llvm/Support/YAMLTraits.h" #include #include #include @@ -541,4 +542,16 @@ } // namespace lldb_private +namespace llvm { +namespace yaml { +template <> struct ScalarTraits { + static void output(const lldb_private::ArchSpec &, void *, raw_ostream &); + static StringRef input(StringRef, void *, lldb_private::ArchSpec &); + static QuotingType mustQuote(StringRef S) { return QuotingType::Double; } +}; +} // namespace yaml +} // namespace llvm + +LLVM_YAML_IS_SEQUENCE_VECTOR(lldb_private::ArchSpec) + #endif // LLDB_UTILITY_ARCHSPEC_H diff --git a/lldb/include/lldb/Utility/ProcessInfo.h b/lldb/include/lldb/Utility/ProcessInfo.h --- a/lldb/include/lldb/Utility/ProcessInfo.h +++ b/lldb/include/lldb/Utility/ProcessInfo.h @@ -9,13 +9,12 @@ #ifndef LLDB_UTILITY_PROCESSINFO_H #define LLDB_UTILITY_PROCESSINFO_H -// LLDB headers #include "lldb/Utility/ArchSpec.h" #include "lldb/Utility/Args.h" #include "lldb/Utility/Environment.h" #include "lldb/Utility/FileSpec.h" #include "lldb/Utility/NameMatches.h" - +#include "llvm/Support/YAMLTraits.h" #include namespace lldb_private { @@ -89,6 +88,7 @@ const Environment &GetEnvironment() const { return m_environment; } protected: + template friend struct llvm::yaml::MappingTraits; FileSpec m_executable; std::string m_arg0; // argv[0] if supported. If empty, then use m_executable. // Not all process plug-ins support specifying an argv[0] that differs from @@ -150,6 +150,7 @@ bool verbose) const; protected: + friend struct llvm::yaml::MappingTraits; uint32_t m_euid; uint32_t m_egid; lldb::pid_t m_parent_pid; @@ -216,4 +217,14 @@ } // namespace lldb_private +LLVM_YAML_IS_SEQUENCE_VECTOR(lldb_private::ProcessInstanceInfo) + +namespace llvm { +namespace yaml { +template <> struct MappingTraits { + static void mapping(IO &io, lldb_private::ProcessInstanceInfo &PII); +}; +} // namespace yaml +} // namespace llvm + #endif // LLDB_UTILITY_PROCESSINFO_H diff --git a/lldb/source/Utility/ArchSpec.cpp b/lldb/source/Utility/ArchSpec.cpp --- a/lldb/source/Utility/ArchSpec.cpp +++ b/lldb/source/Utility/ArchSpec.cpp @@ -1467,3 +1467,15 @@ if (!environ_str.empty()) s << "-" << environ_str; } + +void llvm::yaml::ScalarTraits::output(const ArchSpec &Val, void *, + raw_ostream &Out) { + Val.DumpTriple(Out); +} + +llvm::StringRef +llvm::yaml::ScalarTraits::input(llvm::StringRef Scalar, void *, + ArchSpec &Val) { + Val = ArchSpec(Scalar); + return {}; +} diff --git a/lldb/source/Utility/ProcessInfo.cpp b/lldb/source/Utility/ProcessInfo.cpp --- a/lldb/source/Utility/ProcessInfo.cpp +++ b/lldb/source/Utility/ProcessInfo.cpp @@ -331,3 +331,16 @@ m_name_match_type = NameMatch::Ignore; m_match_all_users = false; } + +void llvm::yaml::MappingTraits::mapping( + IO &io, ProcessInstanceInfo &Info) { + io.mapRequired("executable", Info.m_executable); + io.mapRequired("arg0", Info.m_arg0); + io.mapRequired("arch", Info.m_arch); + io.mapRequired("uid", Info.m_uid); + io.mapRequired("gid", Info.m_gid); + io.mapRequired("pid", Info.m_pid); + io.mapRequired("effective-uid", Info.m_euid); + io.mapRequired("effective-gid", Info.m_egid); + io.mapRequired("parent-pid", Info.m_parent_pid); +} diff --git a/lldb/unittests/Utility/ArchSpecTest.cpp b/lldb/unittests/Utility/ArchSpecTest.cpp --- a/lldb/unittests/Utility/ArchSpecTest.cpp +++ b/lldb/unittests/Utility/ArchSpecTest.cpp @@ -9,8 +9,9 @@ #include "gtest/gtest.h" #include "lldb/Utility/ArchSpec.h" -#include "llvm/BinaryFormat/MachO.h" #include "llvm/BinaryFormat/ELF.h" +#include "llvm/BinaryFormat/MachO.h" +#include "llvm/Support/YAMLParser.h" using namespace lldb; using namespace lldb_private; @@ -200,14 +201,14 @@ EXPECT_TRUE(A.IsValid()); EXPECT_TRUE(B.IsValid()); - + EXPECT_EQ(llvm::Triple::ArchType::arm, B.GetTriple().getArch()); EXPECT_EQ(llvm::Triple::VendorType::UnknownVendor, B.GetTriple().getVendor()); EXPECT_EQ(llvm::Triple::OSType::Linux, B.GetTriple().getOS()); EXPECT_EQ(llvm::Triple::EnvironmentType::UnknownEnvironment, B.GetTriple().getEnvironment()); - + A.MergeFrom(B); EXPECT_EQ(llvm::Triple::ArchType::arm, A.GetTriple().getArch()); EXPECT_EQ(llvm::Triple::VendorType::UnknownVendor, @@ -406,3 +407,23 @@ ASSERT_TRUE(D.TripleEnvironmentWasSpecified()); } } + +TEST(ArchSpecTest, YAML) { + std::string buffer; + llvm::raw_string_ostream os(buffer); + + // Serialize. + llvm::yaml::Output yout(os); + std::vector archs = {ArchSpec("x86_64-pc-linux"), + ArchSpec("x86_64-apple-macosx10.12"), + ArchSpec("i686-pc-windows")}; + yout << archs; + os.flush(); + + // Deserialize. + std::vector deserialized; + llvm::yaml::Input yin(buffer); + yin >> deserialized; + + EXPECT_EQ(archs, deserialized); +} diff --git a/lldb/unittests/Utility/ProcessInstanceInfoTest.cpp b/lldb/unittests/Utility/ProcessInstanceInfoTest.cpp --- a/lldb/unittests/Utility/ProcessInstanceInfoTest.cpp +++ b/lldb/unittests/Utility/ProcessInstanceInfoTest.cpp @@ -108,3 +108,60 @@ EXPECT_TRUE(match.Matches(info_bar)); EXPECT_TRUE(match.Matches(info_empty)); } + +TEST(ProcessInstanceInfo, Yaml) { + std::string buffer; + llvm::raw_string_ostream os(buffer); + + // Serialize. + ProcessInstanceInfo info("a.out", ArchSpec("x86_64-pc-linux"), 47); + info.SetUserID(1); + info.SetEffectiveUserID(2); + info.SetGroupID(3); + info.SetEffectiveGroupID(4); + llvm::yaml::Output yout(os); + yout << info; + os.flush(); + + // Deserialize. + ProcessInstanceInfo deserialized; + llvm::yaml::Input yin(buffer); + yin >> deserialized; + + EXPECT_EQ(deserialized.GetNameAsStringRef(), info.GetNameAsStringRef()); + EXPECT_EQ(deserialized.GetArchitecture(), info.GetArchitecture()); + EXPECT_EQ(deserialized.GetUserID(), info.GetUserID()); + EXPECT_EQ(deserialized.GetGroupID(), info.GetGroupID()); + EXPECT_EQ(deserialized.GetEffectiveUserID(), info.GetEffectiveUserID()); + EXPECT_EQ(deserialized.GetEffectiveGroupID(), info.GetEffectiveGroupID()); +} + +TEST(ProcessInstanceInfoList, Yaml) { + std::string buffer; + llvm::raw_string_ostream os(buffer); + + // Serialize. + ProcessInstanceInfo info("a.out", ArchSpec("x86_64-pc-linux"), 47); + info.SetUserID(1); + info.SetEffectiveUserID(2); + info.SetGroupID(3); + info.SetEffectiveGroupID(4); + ProcessInstanceInfoList list; + list.push_back(info); + llvm::yaml::Output yout(os); + yout << list; + os.flush(); + + // Deserialize. + ProcessInstanceInfoList deserialized; + llvm::yaml::Input yin(buffer); + yin >> deserialized; + + ASSERT_EQ(deserialized.size(), static_cast(1)); + EXPECT_EQ(deserialized[0].GetNameAsStringRef(), info.GetNameAsStringRef()); + EXPECT_EQ(deserialized[0].GetArchitecture(), info.GetArchitecture()); + EXPECT_EQ(deserialized[0].GetUserID(), info.GetUserID()); + EXPECT_EQ(deserialized[0].GetGroupID(), info.GetGroupID()); + EXPECT_EQ(deserialized[0].GetEffectiveUserID(), info.GetEffectiveUserID()); + EXPECT_EQ(deserialized[0].GetEffectiveGroupID(), info.GetEffectiveGroupID()); +}