xref: /openbsd-src/gnu/llvm/lldb/source/Interpreter/OptionArgParser.cpp (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1dda28197Spatrick //===-- OptionArgParser.cpp -----------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9061da546Spatrick #include "lldb/Interpreter/OptionArgParser.h"
10061da546Spatrick #include "lldb/DataFormatters/FormatManager.h"
11*f6aab3d8Srobert #include "lldb/Target/ABI.h"
12061da546Spatrick #include "lldb/Target/Target.h"
13061da546Spatrick #include "lldb/Utility/Status.h"
14061da546Spatrick #include "lldb/Utility/StreamString.h"
15061da546Spatrick 
16061da546Spatrick using namespace lldb_private;
17061da546Spatrick using namespace lldb;
18061da546Spatrick 
ToBoolean(llvm::StringRef ref,bool fail_value,bool * success_ptr)19061da546Spatrick bool OptionArgParser::ToBoolean(llvm::StringRef ref, bool fail_value,
20061da546Spatrick                                 bool *success_ptr) {
21061da546Spatrick   if (success_ptr)
22061da546Spatrick     *success_ptr = true;
23061da546Spatrick   ref = ref.trim();
24be691f3bSpatrick   if (ref.equals_insensitive("false") || ref.equals_insensitive("off") ||
25be691f3bSpatrick       ref.equals_insensitive("no") || ref.equals_insensitive("0")) {
26061da546Spatrick     return false;
27be691f3bSpatrick   } else if (ref.equals_insensitive("true") || ref.equals_insensitive("on") ||
28be691f3bSpatrick              ref.equals_insensitive("yes") || ref.equals_insensitive("1")) {
29061da546Spatrick     return true;
30061da546Spatrick   }
31061da546Spatrick   if (success_ptr)
32061da546Spatrick     *success_ptr = false;
33061da546Spatrick   return fail_value;
34061da546Spatrick }
35061da546Spatrick 
ToChar(llvm::StringRef s,char fail_value,bool * success_ptr)36061da546Spatrick char OptionArgParser::ToChar(llvm::StringRef s, char fail_value,
37061da546Spatrick                              bool *success_ptr) {
38061da546Spatrick   if (success_ptr)
39061da546Spatrick     *success_ptr = false;
40061da546Spatrick   if (s.size() != 1)
41061da546Spatrick     return fail_value;
42061da546Spatrick 
43061da546Spatrick   if (success_ptr)
44061da546Spatrick     *success_ptr = true;
45061da546Spatrick   return s[0];
46061da546Spatrick }
47061da546Spatrick 
ToOptionEnum(llvm::StringRef s,const OptionEnumValues & enum_values,int32_t fail_value,Status & error)48061da546Spatrick int64_t OptionArgParser::ToOptionEnum(llvm::StringRef s,
49061da546Spatrick                                       const OptionEnumValues &enum_values,
50061da546Spatrick                                       int32_t fail_value, Status &error) {
51061da546Spatrick   error.Clear();
52061da546Spatrick   if (enum_values.empty()) {
53061da546Spatrick     error.SetErrorString("invalid enumeration argument");
54061da546Spatrick     return fail_value;
55061da546Spatrick   }
56061da546Spatrick 
57061da546Spatrick   if (s.empty()) {
58061da546Spatrick     error.SetErrorString("empty enumeration string");
59061da546Spatrick     return fail_value;
60061da546Spatrick   }
61061da546Spatrick 
62061da546Spatrick   for (const auto &enum_value : enum_values) {
63061da546Spatrick     llvm::StringRef this_enum(enum_value.string_value);
64061da546Spatrick     if (this_enum.startswith(s))
65061da546Spatrick       return enum_value.value;
66061da546Spatrick   }
67061da546Spatrick 
68061da546Spatrick   StreamString strm;
69061da546Spatrick   strm.PutCString("invalid enumeration value, valid values are: ");
70061da546Spatrick   bool is_first = true;
71061da546Spatrick   for (const auto &enum_value : enum_values) {
72061da546Spatrick     strm.Printf("%s\"%s\"",
73061da546Spatrick         is_first ? is_first = false,"" : ", ", enum_value.string_value);
74061da546Spatrick   }
75061da546Spatrick   error.SetErrorString(strm.GetString());
76061da546Spatrick   return fail_value;
77061da546Spatrick }
78061da546Spatrick 
ToFormat(const char * s,lldb::Format & format,size_t * byte_size_ptr)79061da546Spatrick Status OptionArgParser::ToFormat(const char *s, lldb::Format &format,
80061da546Spatrick                                  size_t *byte_size_ptr) {
81061da546Spatrick   format = eFormatInvalid;
82061da546Spatrick   Status error;
83061da546Spatrick 
84061da546Spatrick   if (s && s[0]) {
85061da546Spatrick     if (byte_size_ptr) {
86061da546Spatrick       if (isdigit(s[0])) {
87061da546Spatrick         char *format_char = nullptr;
88061da546Spatrick         unsigned long byte_size = ::strtoul(s, &format_char, 0);
89061da546Spatrick         if (byte_size != ULONG_MAX)
90061da546Spatrick           *byte_size_ptr = byte_size;
91061da546Spatrick         s = format_char;
92061da546Spatrick       } else
93061da546Spatrick         *byte_size_ptr = 0;
94061da546Spatrick     }
95061da546Spatrick 
96061da546Spatrick     const bool partial_match_ok = true;
97061da546Spatrick     if (!FormatManager::GetFormatFromCString(s, partial_match_ok, format)) {
98061da546Spatrick       StreamString error_strm;
99061da546Spatrick       error_strm.Printf(
100061da546Spatrick           "Invalid format character or name '%s'. Valid values are:\n", s);
101061da546Spatrick       for (Format f = eFormatDefault; f < kNumFormats; f = Format(f + 1)) {
102061da546Spatrick         char format_char = FormatManager::GetFormatAsFormatChar(f);
103061da546Spatrick         if (format_char)
104061da546Spatrick           error_strm.Printf("'%c' or ", format_char);
105061da546Spatrick 
106061da546Spatrick         error_strm.Printf("\"%s\"", FormatManager::GetFormatAsCString(f));
107061da546Spatrick         error_strm.EOL();
108061da546Spatrick       }
109061da546Spatrick 
110061da546Spatrick       if (byte_size_ptr)
111061da546Spatrick         error_strm.PutCString(
112061da546Spatrick             "An optional byte size can precede the format character.\n");
113061da546Spatrick       error.SetErrorString(error_strm.GetString());
114061da546Spatrick     }
115061da546Spatrick 
116061da546Spatrick     if (error.Fail())
117061da546Spatrick       return error;
118061da546Spatrick   } else {
119061da546Spatrick     error.SetErrorStringWithFormat("%s option string", s ? "empty" : "invalid");
120061da546Spatrick   }
121061da546Spatrick   return error;
122061da546Spatrick }
123061da546Spatrick 
ToScriptLanguage(llvm::StringRef s,lldb::ScriptLanguage fail_value,bool * success_ptr)124061da546Spatrick lldb::ScriptLanguage OptionArgParser::ToScriptLanguage(
125061da546Spatrick     llvm::StringRef s, lldb::ScriptLanguage fail_value, bool *success_ptr) {
126061da546Spatrick   if (success_ptr)
127061da546Spatrick     *success_ptr = true;
128061da546Spatrick 
129be691f3bSpatrick   if (s.equals_insensitive("python"))
130061da546Spatrick     return eScriptLanguagePython;
131be691f3bSpatrick   if (s.equals_insensitive("lua"))
132061da546Spatrick     return eScriptLanguageLua;
133be691f3bSpatrick   if (s.equals_insensitive("default"))
134061da546Spatrick     return eScriptLanguageDefault;
135be691f3bSpatrick   if (s.equals_insensitive("none"))
136061da546Spatrick     return eScriptLanguageNone;
137061da546Spatrick 
138061da546Spatrick   if (success_ptr)
139061da546Spatrick     *success_ptr = false;
140061da546Spatrick   return fail_value;
141061da546Spatrick }
142061da546Spatrick 
ToAddress(const ExecutionContext * exe_ctx,llvm::StringRef s,lldb::addr_t fail_value,Status * error_ptr)143061da546Spatrick lldb::addr_t OptionArgParser::ToAddress(const ExecutionContext *exe_ctx,
144061da546Spatrick                                         llvm::StringRef s,
145061da546Spatrick                                         lldb::addr_t fail_value,
146061da546Spatrick                                         Status *error_ptr) {
147061da546Spatrick   bool error_set = false;
148061da546Spatrick   if (s.empty()) {
149061da546Spatrick     if (error_ptr)
150061da546Spatrick       error_ptr->SetErrorStringWithFormat("invalid address expression \"%s\"",
151061da546Spatrick                                           s.str().c_str());
152061da546Spatrick     return fail_value;
153061da546Spatrick   }
154061da546Spatrick 
155061da546Spatrick   llvm::StringRef sref = s;
156061da546Spatrick 
157061da546Spatrick   lldb::addr_t addr = LLDB_INVALID_ADDRESS;
158061da546Spatrick   if (!s.getAsInteger(0, addr)) {
159061da546Spatrick     if (error_ptr)
160061da546Spatrick       error_ptr->Clear();
161*f6aab3d8Srobert     Process *process = exe_ctx->GetProcessPtr();
162*f6aab3d8Srobert     if (process)
163*f6aab3d8Srobert       if (ABISP abi_sp = process->GetABI())
164*f6aab3d8Srobert         addr = abi_sp->FixCodeAddress(addr);
165061da546Spatrick     return addr;
166061da546Spatrick   }
167061da546Spatrick 
168061da546Spatrick   // Try base 16 with no prefix...
169061da546Spatrick   if (!s.getAsInteger(16, addr)) {
170061da546Spatrick     if (error_ptr)
171061da546Spatrick       error_ptr->Clear();
172061da546Spatrick     return addr;
173061da546Spatrick   }
174061da546Spatrick 
175061da546Spatrick   Target *target = nullptr;
176061da546Spatrick   if (!exe_ctx || !(target = exe_ctx->GetTargetPtr())) {
177061da546Spatrick     if (error_ptr)
178061da546Spatrick       error_ptr->SetErrorStringWithFormat("invalid address expression \"%s\"",
179061da546Spatrick                                           s.str().c_str());
180061da546Spatrick     return fail_value;
181061da546Spatrick   }
182061da546Spatrick 
183061da546Spatrick   lldb::ValueObjectSP valobj_sp;
184061da546Spatrick   EvaluateExpressionOptions options;
185061da546Spatrick   options.SetCoerceToId(false);
186061da546Spatrick   options.SetUnwindOnError(true);
187061da546Spatrick   options.SetKeepInMemory(false);
188061da546Spatrick   options.SetTryAllThreads(true);
189061da546Spatrick 
190061da546Spatrick   ExpressionResults expr_result =
191061da546Spatrick       target->EvaluateExpression(s, exe_ctx->GetFramePtr(), valobj_sp, options);
192061da546Spatrick 
193061da546Spatrick   bool success = false;
194061da546Spatrick   if (expr_result == eExpressionCompleted) {
195061da546Spatrick     if (valobj_sp)
196061da546Spatrick       valobj_sp = valobj_sp->GetQualifiedRepresentationIfAvailable(
197061da546Spatrick           valobj_sp->GetDynamicValueType(), true);
198061da546Spatrick     // Get the address to watch.
199061da546Spatrick     if (valobj_sp)
200061da546Spatrick       addr = valobj_sp->GetValueAsUnsigned(fail_value, &success);
201061da546Spatrick     if (success) {
202061da546Spatrick       if (error_ptr)
203061da546Spatrick         error_ptr->Clear();
204061da546Spatrick       return addr;
205061da546Spatrick     } else {
206061da546Spatrick       if (error_ptr) {
207061da546Spatrick         error_set = true;
208061da546Spatrick         error_ptr->SetErrorStringWithFormat(
209061da546Spatrick             "address expression \"%s\" resulted in a value whose type "
210061da546Spatrick             "can't be converted to an address: %s",
211061da546Spatrick             s.str().c_str(), valobj_sp->GetTypeName().GetCString());
212061da546Spatrick       }
213061da546Spatrick     }
214061da546Spatrick 
215061da546Spatrick   } else {
216061da546Spatrick     // Since the compiler can't handle things like "main + 12" we should try to
217061da546Spatrick     // do this for now. The compiler doesn't like adding offsets to function
218061da546Spatrick     // pointer types.
219061da546Spatrick     static RegularExpression g_symbol_plus_offset_regex(
220061da546Spatrick         "^(.*)([-\\+])[[:space:]]*(0x[0-9A-Fa-f]+|[0-9]+)[[:space:]]*$");
221061da546Spatrick 
222061da546Spatrick     llvm::SmallVector<llvm::StringRef, 4> matches;
223061da546Spatrick     if (g_symbol_plus_offset_regex.Execute(sref, &matches)) {
224061da546Spatrick       uint64_t offset = 0;
225061da546Spatrick       std::string name = matches[1].str();
226061da546Spatrick       std::string sign = matches[2].str();
227061da546Spatrick       std::string str_offset = matches[3].str();
228061da546Spatrick       if (!llvm::StringRef(str_offset).getAsInteger(0, offset)) {
229061da546Spatrick         Status error;
230061da546Spatrick         addr = ToAddress(exe_ctx, name.c_str(), LLDB_INVALID_ADDRESS, &error);
231061da546Spatrick         if (addr != LLDB_INVALID_ADDRESS) {
232061da546Spatrick           if (sign[0] == '+')
233061da546Spatrick             return addr + offset;
234061da546Spatrick           else
235061da546Spatrick             return addr - offset;
236061da546Spatrick         }
237061da546Spatrick       }
238061da546Spatrick     }
239061da546Spatrick 
240061da546Spatrick     if (error_ptr) {
241061da546Spatrick       error_set = true;
242061da546Spatrick       error_ptr->SetErrorStringWithFormat(
243061da546Spatrick           "address expression \"%s\" evaluation failed", s.str().c_str());
244061da546Spatrick     }
245061da546Spatrick   }
246061da546Spatrick 
247061da546Spatrick   if (error_ptr) {
248061da546Spatrick     if (!error_set)
249061da546Spatrick       error_ptr->SetErrorStringWithFormat("invalid address expression \"%s\"",
250061da546Spatrick                                           s.str().c_str());
251061da546Spatrick   }
252061da546Spatrick   return fail_value;
253061da546Spatrick }
254