1 //===-- SBEnvironment.cpp -------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/API/SBEnvironment.h"
10 #include "SBReproducerPrivate.h"
11 #include "Utils.h"
12 #include "lldb/API/SBStringList.h"
13 #include "lldb/Utility/Environment.h"
14 
15 using namespace lldb;
16 using namespace lldb_private;
17 
18 SBEnvironment::SBEnvironment() : m_opaque_up(new Environment()) {
19   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBEnvironment);
20 }
21 
22 SBEnvironment::SBEnvironment(const SBEnvironment &rhs)
23     : m_opaque_up(clone(rhs.m_opaque_up)) {
24   LLDB_RECORD_CONSTRUCTOR(SBEnvironment, (const lldb::SBEnvironment &), rhs);
25 }
26 
27 SBEnvironment::SBEnvironment(Environment rhs)
28     : m_opaque_up(new Environment(std::move(rhs))) {}
29 
30 SBEnvironment::~SBEnvironment() = default;
31 
32 SBEnvironment &SBEnvironment::operator=(const SBEnvironment &rhs) {
33   LLDB_RECORD_METHOD(lldb::SBEnvironment &,
34                      SBEnvironment, operator=,(const lldb::SBEnvironment &),
35                      rhs);
36 
37   if (this != &rhs)
38     m_opaque_up = clone(rhs.m_opaque_up);
39   return LLDB_RECORD_RESULT(*this);
40 }
41 
42 size_t SBEnvironment::GetNumValues() {
43   LLDB_RECORD_METHOD_NO_ARGS(size_t, SBEnvironment, GetNumValues);
44   return LLDB_RECORD_RESULT(m_opaque_up->size());
45 }
46 
47 const char *SBEnvironment::Get(const char *name) {
48   LLDB_RECORD_METHOD(const char *, SBEnvironment, Get, (const char *), name);
49   auto entry = m_opaque_up->find(name);
50   if (entry == m_opaque_up->end())
51     return LLDB_RECORD_RESULT(nullptr);
52   return LLDB_RECORD_RESULT(ConstString(entry->second).AsCString(""));
53 }
54 
55 const char *SBEnvironment::GetNameAtIndex(size_t index) {
56   LLDB_RECORD_METHOD(const char *, SBEnvironment, GetNameAtIndex, (size_t),
57                      index);
58   if (index >= GetNumValues())
59     return LLDB_RECORD_RESULT(nullptr);
60   return LLDB_RECORD_RESULT(
61       ConstString(std::next(m_opaque_up->begin(), index)->first())
62           .AsCString(""));
63 }
64 
65 const char *SBEnvironment::GetValueAtIndex(size_t index) {
66   LLDB_RECORD_METHOD(const char *, SBEnvironment, GetValueAtIndex, (size_t),
67                      index);
68   if (index < 0 || index >= GetNumValues())
69     return LLDB_RECORD_RESULT(nullptr);
70   return LLDB_RECORD_RESULT(
71       ConstString(std::next(m_opaque_up->begin(), index)->second)
72           .AsCString(""));
73 }
74 
75 bool SBEnvironment::Set(const char *name, const char *value, bool overwrite) {
76   LLDB_RECORD_METHOD(bool, SBEnvironment, Set,
77                      (const char *, const char *, bool), name, value,
78                      overwrite);
79   if (overwrite) {
80     m_opaque_up->insert_or_assign(name, std::string(value));
81     return LLDB_RECORD_RESULT(true);
82   }
83   return LLDB_RECORD_RESULT(
84       m_opaque_up->try_emplace(name, std::string(value)).second);
85 }
86 
87 bool SBEnvironment::Unset(const char *name) {
88   LLDB_RECORD_METHOD(bool, SBEnvironment, Unset, (const char *), name);
89   return LLDB_RECORD_RESULT(m_opaque_up->erase(name));
90 }
91 
92 SBStringList SBEnvironment::GetEntries() {
93   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBStringList, SBEnvironment, GetEntries);
94   SBStringList entries;
95   for (const auto &KV : *m_opaque_up) {
96     entries.AppendString(Environment::compose(KV).c_str());
97   }
98   return LLDB_RECORD_RESULT(entries);
99 }
100 
101 void SBEnvironment::PutEntry(const char *name_and_value) {
102   LLDB_RECORD_METHOD(void, SBEnvironment, PutEntry, (const char *),
103                      name_and_value);
104   auto split = llvm::StringRef(name_and_value).split('=');
105   m_opaque_up->insert_or_assign(split.first.str(), split.second.str());
106 }
107 
108 void SBEnvironment::SetEntries(const SBStringList &entries, bool append) {
109   LLDB_RECORD_METHOD(void, SBEnvironment, SetEntries,
110                      (const SBStringList &, bool), entries, append);
111   if (!append)
112     m_opaque_up->clear();
113   for (size_t i = 0; i < entries.GetSize(); i++) {
114     PutEntry(entries.GetStringAtIndex(i));
115   }
116 }
117 
118 void SBEnvironment::Clear() {
119   LLDB_RECORD_METHOD_NO_ARGS(void, SBEnvironment, Clear);
120   m_opaque_up->clear();
121 }
122 
123 Environment &SBEnvironment::ref() const { return *m_opaque_up; }
124 
125 namespace lldb_private {
126 namespace repro {
127 
128 template <> void RegisterMethods<SBEnvironment>(Registry &R) {
129   LLDB_REGISTER_CONSTRUCTOR(SBEnvironment, ());
130   LLDB_REGISTER_CONSTRUCTOR(SBEnvironment, (const lldb::SBEnvironment &));
131   LLDB_REGISTER_METHOD(lldb::SBEnvironment &,
132                        SBEnvironment, operator=,(const lldb::SBEnvironment &));
133   LLDB_REGISTER_METHOD(size_t, SBEnvironment, GetNumValues, ());
134   LLDB_REGISTER_METHOD(const char *, SBEnvironment, GetNameAtIndex, (size_t));
135   LLDB_REGISTER_METHOD(const char *, SBEnvironment, GetValueAtIndex, (size_t));
136   LLDB_REGISTER_METHOD(const char *, SBEnvironment, Get, (const char *));
137   LLDB_REGISTER_METHOD(bool, SBEnvironment, Set,
138                        (const char *, const char *, bool));
139   LLDB_REGISTER_METHOD(bool, SBEnvironment, Unset, (const char *));
140   LLDB_REGISTER_METHOD(lldb::SBStringList, SBEnvironment, GetEntries, ());
141   LLDB_REGISTER_METHOD(void, SBEnvironment, PutEntry, (const char *));
142   LLDB_REGISTER_METHOD(void, SBEnvironment, SetEntries,
143                        (const SBStringList &, bool));
144 }
145 
146 } // namespace repro
147 } // namespace lldb_private
148