LLDB  mainline
SBEnvironment.cpp
Go to the documentation of this file.
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 
10 #include "SBReproducerPrivate.h"
11 #include "Utils.h"
12 #include "lldb/API/SBStringList.h"
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
19 SBEnvironment::SBEnvironment() : m_opaque_up(new Environment()) {
21 }
22 
24  : m_opaque_up(clone(rhs.m_opaque_up)) {
26 }
27 
29  : m_opaque_up(new Environment(std::move(rhs))) {}
30 
32 
35  SBEnvironment, operator=,(const lldb::SBEnvironment &),
36  rhs);
37 
38  if (this != &rhs)
40  return LLDB_RECORD_RESULT(*this);
41 }
42 
45 
46  return m_opaque_up->size();
47 }
48 
49 const char *SBEnvironment::Get(const char *name) {
50  LLDB_RECORD_METHOD(const char *, SBEnvironment, Get, (const char *), name);
51 
52  auto entry = m_opaque_up->find(name);
53  if (entry == m_opaque_up->end()) {
54  return nullptr;
55  }
56  return ConstString(entry->second).AsCString("");
57 }
58 
59 const char *SBEnvironment::GetNameAtIndex(size_t index) {
60  LLDB_RECORD_METHOD(const char *, SBEnvironment, GetNameAtIndex, (size_t),
61  index);
62 
63  if (index >= GetNumValues())
64  return nullptr;
65  return ConstString(std::next(m_opaque_up->begin(), index)->first())
66  .AsCString("");
67 }
68 
69 const char *SBEnvironment::GetValueAtIndex(size_t index) {
70  LLDB_RECORD_METHOD(const char *, SBEnvironment, GetValueAtIndex, (size_t),
71  index);
72 
73  if (index >= GetNumValues())
74  return nullptr;
75  return ConstString(std::next(m_opaque_up->begin(), index)->second)
76  .AsCString("");
77 }
78 
79 bool SBEnvironment::Set(const char *name, const char *value, bool overwrite) {
81  (const char *, const char *, bool), name, value,
82  overwrite);
83 
84  if (overwrite) {
85  m_opaque_up->insert_or_assign(name, std::string(value));
86  return true;
87  }
88  return m_opaque_up->try_emplace(name, std::string(value)).second;
89 }
90 
91 bool SBEnvironment::Unset(const char *name) {
92  LLDB_RECORD_METHOD(bool, SBEnvironment, Unset, (const char *), name);
93 
94  return m_opaque_up->erase(name);
95 }
96 
99 
100  SBStringList entries;
101  for (const auto &KV : *m_opaque_up) {
102  entries.AppendString(Environment::compose(KV).c_str());
103  }
104  return LLDB_RECORD_RESULT(entries);
105 }
106 
107 void SBEnvironment::PutEntry(const char *name_and_value) {
108  LLDB_RECORD_METHOD(void, SBEnvironment, PutEntry, (const char *),
109  name_and_value);
110 
111  auto split = llvm::StringRef(name_and_value).split('=');
112  m_opaque_up->insert_or_assign(split.first.str(), split.second.str());
113 }
114 
115 void SBEnvironment::SetEntries(const SBStringList &entries, bool append) {
117  (const lldb::SBStringList &, bool), entries, append);
118 
119  if (!append)
120  m_opaque_up->clear();
121  for (size_t i = 0; i < entries.GetSize(); i++) {
122  PutEntry(entries.GetStringAtIndex(i));
123  }
124 }
125 
128 
129  m_opaque_up->clear();
130 }
131 
133 
134 namespace lldb_private {
135 namespace repro {
140  SBEnvironment, operator=,(const lldb::SBEnvironment &));
141  LLDB_REGISTER_METHOD(size_t, SBEnvironment, GetNumValues, ());
142  LLDB_REGISTER_METHOD(const char *, SBEnvironment, Get, (const char *));
143  LLDB_REGISTER_METHOD(const char *, SBEnvironment, GetNameAtIndex, (size_t));
144  LLDB_REGISTER_METHOD(const char *, SBEnvironment, GetValueAtIndex, (size_t));
146  (const char *, const char *, bool));
147  LLDB_REGISTER_METHOD(bool, SBEnvironment, Unset, (const char *));
149  LLDB_REGISTER_METHOD(void, SBEnvironment, PutEntry, (const char *));
150  LLDB_REGISTER_METHOD(void, SBEnvironment, SetEntries,
151  (const lldb::SBStringList &, bool));
152  LLDB_REGISTER_METHOD(void, SBEnvironment, Clear, ());
153 }
154 } // namespace repro
155 } // namespace lldb_private
lldb::SBStringList
Definition: SBStringList.h:16
lldb::SBStringList::AppendString
void AppendString(const char *str)
Definition: SBStringList.cpp:62
lldb::SBStringList::GetSize
uint32_t GetSize() const
Definition: SBStringList.cpp:102
lldb::SBEnvironment::operator=
const lldb::SBEnvironment & operator=(const lldb::SBEnvironment &rhs)
Definition: SBEnvironment.cpp:33
LLDB_RECORD_RESULT
#define LLDB_RECORD_RESULT(Result)
Definition: ReproducerInstrumentation.h:211
lldb::SBEnvironment::ref
lldb_private::Environment & ref() const
Definition: SBEnvironment.cpp:132
SBStringList.h
LLDB_RECORD_METHOD_NO_ARGS
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
Definition: ReproducerInstrumentation.h:165
LLDB_RECORD_CONSTRUCTOR_NO_ARGS
#define LLDB_RECORD_CONSTRUCTOR_NO_ARGS(Class)
Definition: ReproducerInstrumentation.h:136
lldb_private::ConstString::AsCString
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:193
lldb::SBEnvironment::Clear
void Clear()
Delete all the environment variables.
Definition: SBEnvironment.cpp:126
SBReproducerPrivate.h
LLDB_REGISTER_CONSTRUCTOR
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
Definition: ReproducerInstrumentation.h:89
lldb::SBEnvironment::SBEnvironment
SBEnvironment()
Definition: SBEnvironment.cpp:19
lldb::SBEnvironment::Get
const char * Get(const char *name)
Return the value of a given environment variable.
Definition: SBEnvironment.cpp:49
lldb::SBStringList::GetStringAtIndex
const char * GetStringAtIndex(size_t idx)
Definition: SBStringList.cpp:111
LLDB_RECORD_METHOD
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
Definition: ReproducerInstrumentation.h:158
lldb_private::clone
std::unique_ptr< T > clone(const std::unique_ptr< T > &src)
Definition: Utils.h:17
Utils.h
lldb::SBEnvironment::GetEntries
SBStringList GetEntries()
Return all environment variables contained in this object.
Definition: SBEnvironment.cpp:97
lldb_private::ConstString
Definition: ConstString.h:40
lldb::SBEnvironment::~SBEnvironment
~SBEnvironment()
lldb::SBEnvironment
Definition: SBEnvironment.h:16
lldb_private::repro::RegisterMethods< SBEnvironment >
void RegisterMethods< SBEnvironment >(Registry &R)
Definition: SBEnvironment.cpp:136
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:38
lldb::SBEnvironment::Unset
bool Unset(const char *name)
Unset an environment variable if exists.
Definition: SBEnvironment.cpp:91
LLDB_RECORD_CONSTRUCTOR
#define LLDB_RECORD_CONSTRUCTOR(Class, Signature,...)
Definition: ReproducerInstrumentation.h:133
lldb::SBEnvironment::GetValueAtIndex
const char * GetValueAtIndex(size_t index)
Return the value of the environment variable at a given index from the internal list of environment v...
Definition: SBEnvironment.cpp:69
lldb_private::repro::Registry
The registry contains a unique mapping between functions and their ID.
Definition: ReproducerInstrumentation.h:517
lldb_private::Environment
Definition: Environment.h:18
lldb::SBEnvironment::m_opaque_up
std::unique_ptr< lldb_private::Environment > m_opaque_up
Definition: SBEnvironment.h:132
lldb::SBEnvironment::PutEntry
void PutEntry(const char *name_and_value)
Add or replace an existing environment variable.
Definition: SBEnvironment.cpp:107
Environment.h
SBEnvironment.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb::SBEnvironment::GetNameAtIndex
const char * GetNameAtIndex(size_t index)
Return the name of the environment variable at a given index from the internal list of environment va...
Definition: SBEnvironment.cpp:59
ConstString.h
lldb::SBEnvironment::SetEntries
void SetEntries(const SBStringList &entries, bool append)
Update this object with the given environment variables.
Definition: SBEnvironment.cpp:115
lldb::SBEnvironment::Set
bool Set(const char *name, const char *value, bool overwrite)
Set the value of a given environment variable.
Definition: SBEnvironment.cpp:79
lldb
Definition: SBAddress.h:15
LLDB_REGISTER_METHOD
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
Definition: ReproducerInstrumentation.h:93
lldb::SBEnvironment::GetNumValues
size_t GetNumValues()
Definition: SBEnvironment.cpp:43