1 //===-- OptionArgParser.cpp -----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Interpreter/OptionArgParser.h"
10 #include "lldb/DataFormatters/FormatManager.h"
11 #include "lldb/Target/ABI.h"
12 #include "lldb/Target/Target.h"
13 #include "lldb/Utility/Status.h"
14 #include "lldb/Utility/StreamString.h"
16 using namespace lldb_private
;
19 bool OptionArgParser::ToBoolean(llvm::StringRef ref
, bool fail_value
,
24 if (ref
.equals_insensitive("false") || ref
.equals_insensitive("off") ||
25 ref
.equals_insensitive("no") || ref
.equals_insensitive("0")) {
27 } else if (ref
.equals_insensitive("true") || ref
.equals_insensitive("on") ||
28 ref
.equals_insensitive("yes") || ref
.equals_insensitive("1")) {
36 char OptionArgParser::ToChar(llvm::StringRef s
, char fail_value
,
48 int64_t OptionArgParser::ToOptionEnum(llvm::StringRef s
,
49 const OptionEnumValues
&enum_values
,
50 int32_t fail_value
, Status
&error
) {
52 if (enum_values
.empty()) {
53 error
.SetErrorString("invalid enumeration argument");
58 error
.SetErrorString("empty enumeration string");
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
;
69 strm
.PutCString("invalid enumeration value, valid values are: ");
71 for (const auto &enum_value
: enum_values
) {
72 strm
.Printf("%s\"%s\"",
73 is_first
? is_first
= false,"" : ", ", enum_value
.string_value
);
75 error
.SetErrorString(strm
.GetString());
79 Status
OptionArgParser::ToFormat(const char *s
, lldb::Format
&format
,
80 size_t *byte_size_ptr
) {
81 format
= eFormatInvalid
;
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
;
96 const bool partial_match_ok
= true;
97 if (!FormatManager::GetFormatFromCString(s
, partial_match_ok
, format
)) {
98 StreamString error_strm
;
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
);
104 error_strm
.Printf("'%c' or ", format_char
);
106 error_strm
.Printf("\"%s\"", FormatManager::GetFormatAsCString(f
));
111 error_strm
.PutCString(
112 "An optional byte size can precede the format character.\n");
113 error
.SetErrorString(error_strm
.GetString());
119 error
.SetErrorStringWithFormat("%s option string", s
? "empty" : "invalid");
124 lldb::ScriptLanguage
OptionArgParser::ToScriptLanguage(
125 llvm::StringRef s
, lldb::ScriptLanguage fail_value
, bool *success_ptr
) {
129 if (s
.equals_insensitive("python"))
130 return eScriptLanguagePython
;
131 if (s
.equals_insensitive("lua"))
132 return eScriptLanguageLua
;
133 if (s
.equals_insensitive("default"))
134 return eScriptLanguageDefault
;
135 if (s
.equals_insensitive("none"))
136 return eScriptLanguageNone
;
139 *success_ptr
= false;
143 lldb::addr_t
OptionArgParser::ToRawAddress(const ExecutionContext
*exe_ctx
,
145 lldb::addr_t fail_value
,
147 std::optional
<lldb::addr_t
> maybe_addr
= DoToAddress(exe_ctx
, s
, error_ptr
);
148 return maybe_addr
? *maybe_addr
: fail_value
;
151 lldb::addr_t
OptionArgParser::ToAddress(const ExecutionContext
*exe_ctx
,
153 lldb::addr_t fail_value
,
155 std::optional
<lldb::addr_t
> maybe_addr
= DoToAddress(exe_ctx
, s
, error_ptr
);
159 lldb::addr_t addr
= *maybe_addr
;
161 if (Process
*process
= exe_ctx
->GetProcessPtr())
162 if (ABISP abi_sp
= process
->GetABI())
163 addr
= abi_sp
->FixCodeAddress(addr
);
168 std::optional
<lldb::addr_t
>
169 OptionArgParser::DoToAddress(const ExecutionContext
*exe_ctx
, llvm::StringRef s
,
171 bool error_set
= false;
174 error_ptr
->SetErrorStringWithFormat("invalid address expression \"%s\"",
179 llvm::StringRef sref
= s
;
181 lldb::addr_t addr
= LLDB_INVALID_ADDRESS
;
182 if (!s
.getAsInteger(0, addr
)) {
189 // Try base 16 with no prefix...
190 if (!s
.getAsInteger(16, addr
)) {
196 Target
*target
= nullptr;
197 if (!exe_ctx
|| !(target
= exe_ctx
->GetTargetPtr())) {
199 error_ptr
->SetErrorStringWithFormat("invalid address expression \"%s\"",
204 lldb::ValueObjectSP valobj_sp
;
205 EvaluateExpressionOptions options
;
206 options
.SetCoerceToId(false);
207 options
.SetUnwindOnError(true);
208 options
.SetKeepInMemory(false);
209 options
.SetTryAllThreads(true);
211 ExpressionResults expr_result
=
212 target
->EvaluateExpression(s
, exe_ctx
->GetFramePtr(), valobj_sp
, options
);
214 bool success
= false;
215 if (expr_result
== eExpressionCompleted
) {
217 valobj_sp
= valobj_sp
->GetQualifiedRepresentationIfAvailable(
218 valobj_sp
->GetDynamicValueType(), true);
219 // Get the address to watch.
221 addr
= valobj_sp
->GetValueAsUnsigned(0, &success
);
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());
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
240 static RegularExpression
g_symbol_plus_offset_regex(
241 "^(.*)([-\\+])[[:space:]]*(0x[0-9A-Fa-f]+|[0-9]+)[[:space:]]*$");
243 llvm::SmallVector
<llvm::StringRef
, 4> matches
;
244 if (g_symbol_plus_offset_regex
.Execute(sref
, &matches
)) {
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
) {
254 return addr
+ offset
;
256 return addr
- offset
;
263 error_ptr
->SetErrorStringWithFormat(
264 "address expression \"%s\" evaluation failed", s
.str().c_str());
270 error_ptr
->SetErrorStringWithFormat("invalid address expression \"%s\"",