LLDB mainline
OptionArgParser.cpp
Go to the documentation of this file.
1//===-- OptionArgParser.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
11#include "lldb/Target/ABI.h"
12#include "lldb/Target/Target.h"
13#include "lldb/Utility/Status.h"
15
16using namespace lldb_private;
17using namespace lldb;
18
19bool OptionArgParser::ToBoolean(llvm::StringRef ref, bool fail_value,
20 bool *success_ptr) {
21 if (success_ptr)
22 *success_ptr = true;
23 ref = ref.trim();
24 if (ref.equals_insensitive("false") || ref.equals_insensitive("off") ||
25 ref.equals_insensitive("no") || ref.equals_insensitive("0")) {
26 return false;
27 } else if (ref.equals_insensitive("true") || ref.equals_insensitive("on") ||
28 ref.equals_insensitive("yes") || ref.equals_insensitive("1")) {
29 return true;
30 }
31 if (success_ptr)
32 *success_ptr = false;
33 return fail_value;
34}
35
36char OptionArgParser::ToChar(llvm::StringRef s, char fail_value,
37 bool *success_ptr) {
38 if (success_ptr)
39 *success_ptr = false;
40 if (s.size() != 1)
41 return fail_value;
42
43 if (success_ptr)
44 *success_ptr = true;
45 return s[0];
46}
47
48int64_t OptionArgParser::ToOptionEnum(llvm::StringRef s,
49 const OptionEnumValues &enum_values,
50 int32_t fail_value, Status &error) {
51 error.Clear();
52 if (enum_values.empty()) {
53 error.SetErrorString("invalid enumeration argument");
54 return fail_value;
55 }
56
57 if (s.empty()) {
58 error.SetErrorString("empty enumeration string");
59 return fail_value;
60 }
61
62 for (const auto &enum_value : enum_values) {
63 llvm::StringRef this_enum(enum_value.string_value);
64 if (this_enum.startswith(s))
65 return enum_value.value;
66 }
67
68 StreamString strm;
69 strm.PutCString("invalid enumeration value, valid values are: ");
70 bool is_first = true;
71 for (const auto &enum_value : enum_values) {
72 strm.Printf("%s\"%s\"",
73 is_first ? is_first = false,"" : ", ", enum_value.string_value);
74 }
75 error.SetErrorString(strm.GetString());
76 return fail_value;
77}
78
80 size_t *byte_size_ptr) {
81 format = eFormatInvalid;
83
84 if (s && s[0]) {
85 if (byte_size_ptr) {
86 if (isdigit(s[0])) {
87 char *format_char = nullptr;
88 unsigned long byte_size = ::strtoul(s, &format_char, 0);
89 if (byte_size != ULONG_MAX)
90 *byte_size_ptr = byte_size;
91 s = format_char;
92 } else
93 *byte_size_ptr = 0;
94 }
95
96 const bool partial_match_ok = true;
97 if (!FormatManager::GetFormatFromCString(s, partial_match_ok, format)) {
98 StreamString error_strm;
99 error_strm.Printf(
100 "Invalid format character or name '%s'. Valid values are:\n", s);
101 for (Format f = eFormatDefault; f < kNumFormats; f = Format(f + 1)) {
102 char format_char = FormatManager::GetFormatAsFormatChar(f);
103 if (format_char)
104 error_strm.Printf("'%c' or ", format_char);
105
106 error_strm.Printf("\"%s\"", FormatManager::GetFormatAsCString(f));
107 error_strm.EOL();
108 }
109
110 if (byte_size_ptr)
111 error_strm.PutCString(
112 "An optional byte size can precede the format character.\n");
113 error.SetErrorString(error_strm.GetString());
114 }
115
116 if (error.Fail())
117 return error;
118 } else {
119 error.SetErrorStringWithFormat("%s option string", s ? "empty" : "invalid");
120 }
121 return error;
122}
123
125 llvm::StringRef s, lldb::ScriptLanguage fail_value, bool *success_ptr) {
126 if (success_ptr)
127 *success_ptr = true;
128
129 if (s.equals_insensitive("python"))
131 if (s.equals_insensitive("lua"))
132 return eScriptLanguageLua;
133 if (s.equals_insensitive("default"))
135 if (s.equals_insensitive("none"))
136 return eScriptLanguageNone;
137
138 if (success_ptr)
139 *success_ptr = false;
140 return fail_value;
141}
142
144 llvm::StringRef s,
145 lldb::addr_t fail_value,
146 Status *error_ptr) {
147 std::optional<lldb::addr_t> maybe_addr = DoToAddress(exe_ctx, s, error_ptr);
148 return maybe_addr ? *maybe_addr : fail_value;
149}
150
152 llvm::StringRef s,
153 lldb::addr_t fail_value,
154 Status *error_ptr) {
155 std::optional<lldb::addr_t> maybe_addr = DoToAddress(exe_ctx, s, error_ptr);
156 if (!maybe_addr)
157 return fail_value;
158
159 lldb::addr_t addr = *maybe_addr;
160
161 if (Process *process = exe_ctx->GetProcessPtr())
162 if (ABISP abi_sp = process->GetABI())
163 addr = abi_sp->FixCodeAddress(addr);
164
165 return addr;
166}
167
168std::optional<lldb::addr_t>
169OptionArgParser::DoToAddress(const ExecutionContext *exe_ctx, llvm::StringRef s,
170 Status *error_ptr) {
171 bool error_set = false;
172 if (s.empty()) {
173 if (error_ptr)
174 error_ptr->SetErrorStringWithFormat("invalid address expression \"%s\"",
175 s.str().c_str());
176 return {};
177 }
178
179 llvm::StringRef sref = s;
180
182 if (!s.getAsInteger(0, addr)) {
183 if (error_ptr)
184 error_ptr->Clear();
185
186 return addr;
187 }
188
189 // Try base 16 with no prefix...
190 if (!s.getAsInteger(16, addr)) {
191 if (error_ptr)
192 error_ptr->Clear();
193 return addr;
194 }
195
196 Target *target = nullptr;
197 if (!exe_ctx || !(target = exe_ctx->GetTargetPtr())) {
198 if (error_ptr)
199 error_ptr->SetErrorStringWithFormat("invalid address expression \"%s\"",
200 s.str().c_str());
201 return {};
202 }
203
204 lldb::ValueObjectSP valobj_sp;
206 options.SetCoerceToId(false);
207 options.SetUnwindOnError(true);
208 options.SetKeepInMemory(false);
209 options.SetTryAllThreads(true);
210
211 ExpressionResults expr_result =
212 target->EvaluateExpression(s, exe_ctx->GetFramePtr(), valobj_sp, options);
213
214 bool success = false;
215 if (expr_result == eExpressionCompleted) {
216 if (valobj_sp)
217 valobj_sp = valobj_sp->GetQualifiedRepresentationIfAvailable(
218 valobj_sp->GetDynamicValueType(), true);
219 // Get the address to watch.
220 if (valobj_sp)
221 addr = valobj_sp->GetValueAsUnsigned(0, &success);
222 if (success) {
223 if (error_ptr)
224 error_ptr->Clear();
225 return addr;
226 } else {
227 if (error_ptr) {
228 error_set = true;
229 error_ptr->SetErrorStringWithFormat(
230 "address expression \"%s\" resulted in a value whose type "
231 "can't be converted to an address: %s",
232 s.str().c_str(), valobj_sp->GetTypeName().GetCString());
233 }
234 }
235
236 } else {
237 // Since the compiler can't handle things like "main + 12" we should try to
238 // do this for now. The compiler doesn't like adding offsets to function
239 // pointer types.
240 static RegularExpression g_symbol_plus_offset_regex(
241 "^(.*)([-\\+])[[:space:]]*(0x[0-9A-Fa-f]+|[0-9]+)[[:space:]]*$");
242
243 llvm::SmallVector<llvm::StringRef, 4> matches;
244 if (g_symbol_plus_offset_regex.Execute(sref, &matches)) {
245 uint64_t offset = 0;
246 std::string name = matches[1].str();
247 std::string sign = matches[2].str();
248 std::string str_offset = matches[3].str();
249 if (!llvm::StringRef(str_offset).getAsInteger(0, offset)) {
251 addr = ToAddress(exe_ctx, name.c_str(), LLDB_INVALID_ADDRESS, &error);
252 if (addr != LLDB_INVALID_ADDRESS) {
253 if (sign[0] == '+')
254 return addr + offset;
255 else
256 return addr - offset;
257 }
258 }
259 }
260
261 if (error_ptr) {
262 error_set = true;
263 error_ptr->SetErrorStringWithFormat(
264 "address expression \"%s\" evaluation failed", s.str().c_str());
265 }
266 }
267
268 if (error_ptr) {
269 if (!error_set)
270 error_ptr->SetErrorStringWithFormat("invalid address expression \"%s\"",
271 s.str().c_str());
272 }
273 return {};
274}
static llvm::raw_ostream & error(Stream &strm)
void SetUnwindOnError(bool unwind=false)
Definition: Target.h:332
void SetKeepInMemory(bool keep=true)
Definition: Target.h:342
void SetCoerceToId(bool coerce=true)
Definition: Target.h:328
void SetTryAllThreads(bool try_others=true)
Definition: Target.h:365
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
StackFrame * GetFramePtr() const
Returns a pointer to the frame object.
Target * GetTargetPtr() const
Returns a pointer to the target object.
Process * GetProcessPtr() const
Returns a pointer to the process object.
static bool GetFormatFromCString(const char *format_cstr, bool partial_match_ok, lldb::Format &format)
static const char * GetFormatAsCString(lldb::Format format)
static char GetFormatAsFormatChar(lldb::Format format)
A plug-in interface definition class for debugging a process.
Definition: Process.h:336
bool Execute(llvm::StringRef string, llvm::SmallVectorImpl< llvm::StringRef > *matches=nullptr) const
Execute a regular expression match using the compiled regular expression that is already in this obje...
An error handling class.
Definition: Status.h:44
void Clear()
Clear the object state.
Definition: Status.cpp:167
int SetErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Set the current error string to a formatted error string.
Definition: Status.cpp:255
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:128
lldb::ExpressionResults EvaluateExpression(llvm::StringRef expression, ExecutionContextScope *exe_scope, lldb::ValueObjectSP &result_valobj_sp, const EvaluateExpressionOptions &options=EvaluateExpressionOptions(), std::string *fixed_expression=nullptr, ValueObject *ctx_obj=nullptr)
Definition: Target.cpp:2611
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:76
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
llvm::ArrayRef< OptionEnumValueElement > OptionEnumValues
Definition: SBAddress.h:15
ScriptLanguage
Script interpreter types.
@ eScriptLanguageLua
@ eScriptLanguageDefault
@ eScriptLanguageNone
@ eScriptLanguagePython
std::shared_ptr< lldb_private::ABI > ABISP
Definition: lldb-forward.h:300
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
Definition: lldb-forward.h:458
Format
Display format definitions.
ExpressionResults
The results of expression evaluation.
@ eExpressionCompleted
uint64_t addr_t
Definition: lldb-types.h:79
static int64_t ToOptionEnum(llvm::StringRef s, const OptionEnumValues &enum_values, int32_t fail_value, Status &error)
static lldb::ScriptLanguage ToScriptLanguage(llvm::StringRef s, lldb::ScriptLanguage fail_value, bool *success_ptr)
static char ToChar(llvm::StringRef s, char fail_value, bool *success_ptr)
static lldb::addr_t ToAddress(const ExecutionContext *exe_ctx, llvm::StringRef s, lldb::addr_t fail_value, Status *error_ptr)
Try to parse an address.
static lldb::addr_t ToRawAddress(const ExecutionContext *exe_ctx, llvm::StringRef s, lldb::addr_t fail_value, Status *error_ptr)
As for ToAddress but do not remove non-address bits from the result.
static std::optional< lldb::addr_t > DoToAddress(const ExecutionContext *exe_ctx, llvm::StringRef s, Status *error)
static Status ToFormat(const char *s, lldb::Format &format, size_t *byte_size_ptr)
static bool ToBoolean(llvm::StringRef s, bool fail_value, bool *success_ptr)