Skip to content

Commit

Permalink
[lldb] Add YAML traits for ArchSpec and ProcessInstanceInfo
Browse files Browse the repository at this point in the history
Add YAML traits for ArchSpec and ProcessInstanceInfo so they can be
serialized for the reproducers.

Differential revision: https://reviews.llvm.org/D76004
  • Loading branch information
JDevlieghere committed Mar 12, 2020
1 parent 396a42d commit 0ce3b71
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 5 deletions.
13 changes: 13 additions & 0 deletions lldb/include/lldb/Utility/ArchSpec.h
Expand Up @@ -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 <cstddef>
#include <cstdint>
#include <string>
Expand Down Expand Up @@ -541,4 +542,16 @@ bool ParseMachCPUDashSubtypeTriple(llvm::StringRef triple_str, ArchSpec &arch);

} // namespace lldb_private

namespace llvm {
namespace yaml {
template <> struct ScalarTraits<lldb_private::ArchSpec> {
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
15 changes: 13 additions & 2 deletions lldb/include/lldb/Utility/ProcessInfo.h
Expand Up @@ -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 <vector>

namespace lldb_private {
Expand Down Expand Up @@ -89,6 +88,7 @@ class ProcessInfo {
const Environment &GetEnvironment() const { return m_environment; }

protected:
template <class T> 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
Expand Down Expand Up @@ -150,6 +150,7 @@ class ProcessInstanceInfo : public ProcessInfo {
bool verbose) const;

protected:
friend struct llvm::yaml::MappingTraits<ProcessInstanceInfo>;
uint32_t m_euid;
uint32_t m_egid;
lldb::pid_t m_parent_pid;
Expand Down Expand Up @@ -216,4 +217,14 @@ class ProcessInstanceInfoMatch {

} // namespace lldb_private

LLVM_YAML_IS_SEQUENCE_VECTOR(lldb_private::ProcessInstanceInfo)

namespace llvm {
namespace yaml {
template <> struct MappingTraits<lldb_private::ProcessInstanceInfo> {
static void mapping(IO &io, lldb_private::ProcessInstanceInfo &PII);
};
} // namespace yaml
} // namespace llvm

#endif // LLDB_UTILITY_PROCESSINFO_H
12 changes: 12 additions & 0 deletions lldb/source/Utility/ArchSpec.cpp
Expand Up @@ -1467,3 +1467,15 @@ void ArchSpec::DumpTriple(llvm::raw_ostream &s) const {
if (!environ_str.empty())
s << "-" << environ_str;
}

void llvm::yaml::ScalarTraits<ArchSpec>::output(const ArchSpec &Val, void *,
raw_ostream &Out) {
Val.DumpTriple(Out);
}

llvm::StringRef
llvm::yaml::ScalarTraits<ArchSpec>::input(llvm::StringRef Scalar, void *,
ArchSpec &Val) {
Val = ArchSpec(Scalar);
return {};
}
13 changes: 13 additions & 0 deletions lldb/source/Utility/ProcessInfo.cpp
Expand Up @@ -331,3 +331,16 @@ void ProcessInstanceInfoMatch::Clear() {
m_name_match_type = NameMatch::Ignore;
m_match_all_users = false;
}

void llvm::yaml::MappingTraits<ProcessInstanceInfo>::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);
}
27 changes: 24 additions & 3 deletions lldb/unittests/Utility/ArchSpecTest.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -200,14 +201,14 @@ TEST(ArchSpecTest, MergeFrom) {

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,
Expand Down Expand Up @@ -406,3 +407,23 @@ TEST(ArchSpecTest, TripleComponentsWereSpecified) {
ASSERT_TRUE(D.TripleEnvironmentWasSpecified());
}
}

TEST(ArchSpecTest, YAML) {
std::string buffer;
llvm::raw_string_ostream os(buffer);

// Serialize.
llvm::yaml::Output yout(os);
std::vector<ArchSpec> archs = {ArchSpec("x86_64-pc-linux"),
ArchSpec("x86_64-apple-macosx10.12"),
ArchSpec("i686-pc-windows")};
yout << archs;
os.flush();

// Deserialize.
std::vector<ArchSpec> deserialized;
llvm::yaml::Input yin(buffer);
yin >> deserialized;

EXPECT_EQ(archs, deserialized);
}
57 changes: 57 additions & 0 deletions lldb/unittests/Utility/ProcessInstanceInfoTest.cpp
Expand Up @@ -108,3 +108,60 @@ TEST(ProcessInstanceInfoMatch, Name) {
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<size_t>(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());
}

0 comments on commit 0ce3b71

Please sign in to comment.