1 //===-- OptionArgParser.cpp -------------------------------------*- C++ -*-===//
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/Target.h"
12 #include "lldb/Utility/Status.h"
13 #include "lldb/Utility/StreamString.h"
15 using namespace lldb_private;
18 bool OptionArgParser::ToBoolean(llvm::StringRef ref, bool fail_value,
23 if (ref.equals_lower("false") || ref.equals_lower("off") ||
24 ref.equals_lower("no") || ref.equals_lower("0")) {
26 } else if (ref.equals_lower("true") || ref.equals_lower("on") ||
27 ref.equals_lower("yes") || ref.equals_lower("1")) {
35 char OptionArgParser::ToChar(llvm::StringRef s, char fail_value,
47 int64_t OptionArgParser::ToOptionEnum(llvm::StringRef s,
48 const OptionEnumValues &enum_values,
49 int32_t fail_value, Status &error) {
51 if (enum_values.empty()) {
52 error.SetErrorString("invalid enumeration argument");
57 error.SetErrorString("empty enumeration string");
61 for (const auto &enum_value : enum_values) {
62 llvm::StringRef this_enum(enum_value.string_value);
63 if (this_enum.startswith(s))
64 return enum_value.value;
68 strm.PutCString("invalid enumeration value, valid values are: ");
70 for (const auto &enum_value : enum_values) {
71 strm.Printf("%s\"%s\"",
72 is_first ? is_first = false,"" : ", ", enum_value.string_value);
74 error.SetErrorString(strm.GetString());
78 Status OptionArgParser::ToFormat(const char *s, lldb::Format &format,
79 size_t *byte_size_ptr) {
80 format = eFormatInvalid;
86 char *format_char = nullptr;
87 unsigned long byte_size = ::strtoul(s, &format_char, 0);
88 if (byte_size != ULONG_MAX)
89 *byte_size_ptr = byte_size;
95 const bool partial_match_ok = true;
96 if (!FormatManager::GetFormatFromCString(s, partial_match_ok, format)) {
97 StreamString error_strm;
99 "Invalid format character or name '%s'. Valid values are:\n", s);
100 for (Format f = eFormatDefault; f < kNumFormats; f = Format(f + 1)) {
101 char format_char = FormatManager::GetFormatAsFormatChar(f);
103 error_strm.Printf("'%c' or ", format_char);
105 error_strm.Printf("\"%s\"", FormatManager::GetFormatAsCString(f));
110 error_strm.PutCString(
111 "An optional byte size can precede the format character.\n");
112 error.SetErrorString(error_strm.GetString());
118 error.SetErrorStringWithFormat("%s option string", s ? "empty" : "invalid");
123 lldb::ScriptLanguage OptionArgParser::ToScriptLanguage(
124 llvm::StringRef s, lldb::ScriptLanguage fail_value, bool *success_ptr) {
128 if (s.equals_lower("python"))
129 return eScriptLanguagePython;
130 if (s.equals_lower("lua"))
131 return eScriptLanguageLua;
132 if (s.equals_lower("default"))
133 return eScriptLanguageDefault;
134 if (s.equals_lower("none"))
135 return eScriptLanguageNone;
138 *success_ptr = false;
142 lldb::addr_t OptionArgParser::ToAddress(const ExecutionContext *exe_ctx,
144 lldb::addr_t fail_value,
146 bool error_set = false;
149 error_ptr->SetErrorStringWithFormat("invalid address expression \"%s\"",
154 llvm::StringRef sref = s;
156 lldb::addr_t addr = LLDB_INVALID_ADDRESS;
157 if (!s.getAsInteger(0, addr)) {
163 // Try base 16 with no prefix...
164 if (!s.getAsInteger(16, addr)) {
170 Target *target = nullptr;
171 if (!exe_ctx || !(target = exe_ctx->GetTargetPtr())) {
173 error_ptr->SetErrorStringWithFormat("invalid address expression \"%s\"",
178 lldb::ValueObjectSP valobj_sp;
179 EvaluateExpressionOptions options;
180 options.SetCoerceToId(false);
181 options.SetUnwindOnError(true);
182 options.SetKeepInMemory(false);
183 options.SetTryAllThreads(true);
185 ExpressionResults expr_result =
186 target->EvaluateExpression(s, exe_ctx->GetFramePtr(), valobj_sp, options);
188 bool success = false;
189 if (expr_result == eExpressionCompleted) {
191 valobj_sp = valobj_sp->GetQualifiedRepresentationIfAvailable(
192 valobj_sp->GetDynamicValueType(), true);
193 // Get the address to watch.
195 addr = valobj_sp->GetValueAsUnsigned(fail_value, &success);
203 error_ptr->SetErrorStringWithFormat(
204 "address expression \"%s\" resulted in a value whose type "
205 "can't be converted to an address: %s",
206 s.str().c_str(), valobj_sp->GetTypeName().GetCString());
211 // Since the compiler can't handle things like "main + 12" we should try to
212 // do this for now. The compiler doesn't like adding offsets to function
214 static RegularExpression g_symbol_plus_offset_regex(
215 "^(.*)([-\\+])[[:space:]]*(0x[0-9A-Fa-f]+|[0-9]+)[[:space:]]*$");
217 llvm::SmallVector<llvm::StringRef, 4> matches;
218 if (g_symbol_plus_offset_regex.Execute(sref, &matches)) {
220 std::string name = matches[1].str();
221 std::string sign = matches[2].str();
222 std::string str_offset = matches[3].str();
223 if (!llvm::StringRef(str_offset).getAsInteger(0, offset)) {
225 addr = ToAddress(exe_ctx, name.c_str(), LLDB_INVALID_ADDRESS, &error);
226 if (addr != LLDB_INVALID_ADDRESS) {
228 return addr + offset;
230 return addr - offset;
237 error_ptr->SetErrorStringWithFormat(
238 "address expression \"%s\" evaluation failed", s.str().c_str());
244 error_ptr->SetErrorStringWithFormat("invalid address expression \"%s\"",