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
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"
15
16 using namespace lldb_private;
17 using namespace lldb;
18
ToBoolean(llvm::StringRef ref,bool fail_value,bool * success_ptr)19 bool 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
ToChar(llvm::StringRef s,char fail_value,bool * success_ptr)36 char 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
ToOptionEnum(llvm::StringRef s,const OptionEnumValues & enum_values,int32_t fail_value,Status & error)48 int64_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
ToFormat(const char * s,lldb::Format & format,size_t * byte_size_ptr)79 Status OptionArgParser::ToFormat(const char *s, lldb::Format &format,
80 size_t *byte_size_ptr) {
81 format = eFormatInvalid;
82 Status error;
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
ToScriptLanguage(llvm::StringRef s,lldb::ScriptLanguage fail_value,bool * success_ptr)124 lldb::ScriptLanguage OptionArgParser::ToScriptLanguage(
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"))
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;
137
138 if (success_ptr)
139 *success_ptr = false;
140 return fail_value;
141 }
142
ToAddress(const ExecutionContext * exe_ctx,llvm::StringRef s,lldb::addr_t fail_value,Status * error_ptr)143 lldb::addr_t OptionArgParser::ToAddress(const ExecutionContext *exe_ctx,
144 llvm::StringRef s,
145 lldb::addr_t fail_value,
146 Status *error_ptr) {
147 bool error_set = false;
148 if (s.empty()) {
149 if (error_ptr)
150 error_ptr->SetErrorStringWithFormat("invalid address expression \"%s\"",
151 s.str().c_str());
152 return fail_value;
153 }
154
155 llvm::StringRef sref = s;
156
157 lldb::addr_t addr = LLDB_INVALID_ADDRESS;
158 if (!s.getAsInteger(0, addr)) {
159 if (error_ptr)
160 error_ptr->Clear();
161 Process *process = exe_ctx->GetProcessPtr();
162 if (process)
163 if (ABISP abi_sp = process->GetABI())
164 addr = abi_sp->FixCodeAddress(addr);
165 return addr;
166 }
167
168 // Try base 16 with no prefix...
169 if (!s.getAsInteger(16, addr)) {
170 if (error_ptr)
171 error_ptr->Clear();
172 return addr;
173 }
174
175 Target *target = nullptr;
176 if (!exe_ctx || !(target = exe_ctx->GetTargetPtr())) {
177 if (error_ptr)
178 error_ptr->SetErrorStringWithFormat("invalid address expression \"%s\"",
179 s.str().c_str());
180 return fail_value;
181 }
182
183 lldb::ValueObjectSP valobj_sp;
184 EvaluateExpressionOptions options;
185 options.SetCoerceToId(false);
186 options.SetUnwindOnError(true);
187 options.SetKeepInMemory(false);
188 options.SetTryAllThreads(true);
189
190 ExpressionResults expr_result =
191 target->EvaluateExpression(s, exe_ctx->GetFramePtr(), valobj_sp, options);
192
193 bool success = false;
194 if (expr_result == eExpressionCompleted) {
195 if (valobj_sp)
196 valobj_sp = valobj_sp->GetQualifiedRepresentationIfAvailable(
197 valobj_sp->GetDynamicValueType(), true);
198 // Get the address to watch.
199 if (valobj_sp)
200 addr = valobj_sp->GetValueAsUnsigned(fail_value, &success);
201 if (success) {
202 if (error_ptr)
203 error_ptr->Clear();
204 return addr;
205 } else {
206 if (error_ptr) {
207 error_set = true;
208 error_ptr->SetErrorStringWithFormat(
209 "address expression \"%s\" resulted in a value whose type "
210 "can't be converted to an address: %s",
211 s.str().c_str(), valobj_sp->GetTypeName().GetCString());
212 }
213 }
214
215 } else {
216 // Since the compiler can't handle things like "main + 12" we should try to
217 // do this for now. The compiler doesn't like adding offsets to function
218 // pointer types.
219 static RegularExpression g_symbol_plus_offset_regex(
220 "^(.*)([-\\+])[[:space:]]*(0x[0-9A-Fa-f]+|[0-9]+)[[:space:]]*$");
221
222 llvm::SmallVector<llvm::StringRef, 4> matches;
223 if (g_symbol_plus_offset_regex.Execute(sref, &matches)) {
224 uint64_t offset = 0;
225 std::string name = matches[1].str();
226 std::string sign = matches[2].str();
227 std::string str_offset = matches[3].str();
228 if (!llvm::StringRef(str_offset).getAsInteger(0, offset)) {
229 Status error;
230 addr = ToAddress(exe_ctx, name.c_str(), LLDB_INVALID_ADDRESS, &error);
231 if (addr != LLDB_INVALID_ADDRESS) {
232 if (sign[0] == '+')
233 return addr + offset;
234 else
235 return addr - offset;
236 }
237 }
238 }
239
240 if (error_ptr) {
241 error_set = true;
242 error_ptr->SetErrorStringWithFormat(
243 "address expression \"%s\" evaluation failed", s.str().c_str());
244 }
245 }
246
247 if (error_ptr) {
248 if (!error_set)
249 error_ptr->SetErrorStringWithFormat("invalid address expression \"%s\"",
250 s.str().c_str());
251 }
252 return fail_value;
253 }
254