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 | |
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(RHS: "false" ) || ref.equals_insensitive(RHS: "off" ) || |
25 | ref.equals_insensitive(RHS: "no" ) || ref.equals_insensitive(RHS: "0" )) { |
26 | return false; |
27 | } else if (ref.equals_insensitive(RHS: "true" ) || ref.equals_insensitive(RHS: "on" ) || |
28 | ref.equals_insensitive(RHS: "yes" ) || ref.equals_insensitive(RHS: "1" )) { |
29 | return true; |
30 | } |
31 | if (success_ptr) |
32 | *success_ptr = false; |
33 | return fail_value; |
34 | } |
35 | |
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 | |
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.starts_with(Prefix: s)) |
65 | return enum_value.value; |
66 | } |
67 | |
68 | StreamString strm; |
69 | strm.PutCString(cstr: "invalid enumeration value, valid values are: " ); |
70 | bool is_first = true; |
71 | for (const auto &enum_value : enum_values) { |
72 | strm.Printf(format: "%s\"%s\"" , |
73 | is_first ? is_first = false,"" : ", " , enum_value.string_value); |
74 | } |
75 | error.SetErrorString(strm.GetString()); |
76 | return fail_value; |
77 | } |
78 | |
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(nptr: s, endptr: &format_char, base: 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 | if (!FormatManager::GetFormatFromCString(format_cstr: s, format)) { |
97 | StreamString error_strm; |
98 | error_strm.Printf( |
99 | format: "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(format: f); |
102 | if (format_char) |
103 | error_strm.Printf(format: "'%c' or " , format_char); |
104 | |
105 | error_strm.Printf(format: "\"%s\"" , FormatManager::GetFormatAsCString(format: f)); |
106 | error_strm.EOL(); |
107 | } |
108 | |
109 | if (byte_size_ptr) |
110 | error_strm.PutCString( |
111 | cstr: "An optional byte size can precede the format character.\n" ); |
112 | error.SetErrorString(error_strm.GetString()); |
113 | } |
114 | |
115 | if (error.Fail()) |
116 | return error; |
117 | } else { |
118 | error.SetErrorStringWithFormat("%s option string" , s ? "empty" : "invalid" ); |
119 | } |
120 | return error; |
121 | } |
122 | |
123 | lldb::ScriptLanguage OptionArgParser::ToScriptLanguage( |
124 | llvm::StringRef s, lldb::ScriptLanguage fail_value, bool *success_ptr) { |
125 | if (success_ptr) |
126 | *success_ptr = true; |
127 | |
128 | if (s.equals_insensitive(RHS: "python" )) |
129 | return eScriptLanguagePython; |
130 | if (s.equals_insensitive(RHS: "lua" )) |
131 | return eScriptLanguageLua; |
132 | if (s.equals_insensitive(RHS: "default" )) |
133 | return eScriptLanguageDefault; |
134 | if (s.equals_insensitive(RHS: "none" )) |
135 | return eScriptLanguageNone; |
136 | |
137 | if (success_ptr) |
138 | *success_ptr = false; |
139 | return fail_value; |
140 | } |
141 | |
142 | lldb::addr_t OptionArgParser::ToRawAddress(const ExecutionContext *exe_ctx, |
143 | llvm::StringRef s, |
144 | lldb::addr_t fail_value, |
145 | Status *error_ptr) { |
146 | std::optional<lldb::addr_t> maybe_addr = DoToAddress(exe_ctx, s, error: error_ptr); |
147 | return maybe_addr ? *maybe_addr : fail_value; |
148 | } |
149 | |
150 | lldb::addr_t OptionArgParser::ToAddress(const ExecutionContext *exe_ctx, |
151 | llvm::StringRef s, |
152 | lldb::addr_t fail_value, |
153 | Status *error_ptr) { |
154 | std::optional<lldb::addr_t> maybe_addr = DoToAddress(exe_ctx, s, error: error_ptr); |
155 | if (!maybe_addr) |
156 | return fail_value; |
157 | |
158 | lldb::addr_t addr = *maybe_addr; |
159 | |
160 | if (Process *process = exe_ctx->GetProcessPtr()) |
161 | if (ABISP abi_sp = process->GetABI()) |
162 | addr = abi_sp->FixCodeAddress(pc: addr); |
163 | |
164 | return addr; |
165 | } |
166 | |
167 | std::optional<lldb::addr_t> |
168 | OptionArgParser::DoToAddress(const ExecutionContext *exe_ctx, llvm::StringRef s, |
169 | Status *error_ptr) { |
170 | if (s.empty()) { |
171 | if (error_ptr) |
172 | error_ptr->SetErrorStringWithFormat("invalid address expression \"%s\"" , |
173 | s.str().c_str()); |
174 | return {}; |
175 | } |
176 | |
177 | llvm::StringRef sref = s; |
178 | |
179 | lldb::addr_t addr = LLDB_INVALID_ADDRESS; |
180 | if (!s.getAsInteger(Radix: 0, Result&: addr)) { |
181 | if (error_ptr) |
182 | error_ptr->Clear(); |
183 | |
184 | return addr; |
185 | } |
186 | |
187 | // Try base 16 with no prefix... |
188 | if (!s.getAsInteger(Radix: 16, Result&: addr)) { |
189 | if (error_ptr) |
190 | error_ptr->Clear(); |
191 | return addr; |
192 | } |
193 | |
194 | Target *target = nullptr; |
195 | if (!exe_ctx || !(target = exe_ctx->GetTargetPtr())) { |
196 | if (error_ptr) |
197 | error_ptr->SetErrorStringWithFormat("invalid address expression \"%s\"" , |
198 | s.str().c_str()); |
199 | return {}; |
200 | } |
201 | |
202 | lldb::ValueObjectSP valobj_sp; |
203 | EvaluateExpressionOptions options; |
204 | options.SetCoerceToId(false); |
205 | options.SetUnwindOnError(true); |
206 | options.SetKeepInMemory(false); |
207 | options.SetTryAllThreads(true); |
208 | |
209 | ExpressionResults expr_result = |
210 | target->EvaluateExpression(expression: s, exe_scope: exe_ctx->GetFramePtr(), result_valobj_sp&: valobj_sp, options); |
211 | |
212 | bool success = false; |
213 | if (expr_result == eExpressionCompleted) { |
214 | if (valobj_sp) |
215 | valobj_sp = valobj_sp->GetQualifiedRepresentationIfAvailable( |
216 | dynValue: valobj_sp->GetDynamicValueType(), synthValue: true); |
217 | // Get the address to watch. |
218 | if (valobj_sp) |
219 | addr = valobj_sp->GetValueAsUnsigned(fail_value: 0, success: &success); |
220 | if (success) { |
221 | if (error_ptr) |
222 | error_ptr->Clear(); |
223 | return addr; |
224 | } |
225 | if (error_ptr) |
226 | error_ptr->SetErrorStringWithFormat( |
227 | "address expression \"%s\" resulted in a value whose type " |
228 | "can't be converted to an address: %s" , |
229 | s.str().c_str(), valobj_sp->GetTypeName().GetCString()); |
230 | return {}; |
231 | } |
232 | |
233 | // Since the compiler can't handle things like "main + 12" we should try to |
234 | // do this for now. The compiler doesn't like adding offsets to function |
235 | // pointer types. |
236 | static RegularExpression g_symbol_plus_offset_regex( |
237 | "^(.*)([-\\+])[[:space:]]*(0x[0-9A-Fa-f]+|[0-9]+)[[:space:]]*$" ); |
238 | |
239 | llvm::SmallVector<llvm::StringRef, 4> matches; |
240 | if (g_symbol_plus_offset_regex.Execute(string: sref, matches: &matches)) { |
241 | uint64_t offset = 0; |
242 | llvm::StringRef name = matches[1]; |
243 | llvm::StringRef sign = matches[2]; |
244 | llvm::StringRef str_offset = matches[3]; |
245 | if (!str_offset.getAsInteger(Radix: 0, Result&: offset)) { |
246 | Status error; |
247 | addr = ToAddress(exe_ctx, s: name, LLDB_INVALID_ADDRESS, error_ptr: &error); |
248 | if (addr != LLDB_INVALID_ADDRESS) { |
249 | if (sign[0] == '+') |
250 | return addr + offset; |
251 | return addr - offset; |
252 | } |
253 | } |
254 | } |
255 | |
256 | if (error_ptr) |
257 | error_ptr->SetErrorStringWithFormat( |
258 | "address expression \"%s\" evaluation failed" , s.str().c_str()); |
259 | return {}; |
260 | } |
261 | |