180814287SRaphael Isemann //===-- OptionArgParser.cpp -----------------------------------------------===//
247cbf4a0SPavel Labath //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
647cbf4a0SPavel Labath //
747cbf4a0SPavel Labath //===----------------------------------------------------------------------===//
847cbf4a0SPavel Labath
947cbf4a0SPavel Labath #include "lldb/Interpreter/OptionArgParser.h"
1047cbf4a0SPavel Labath #include "lldb/DataFormatters/FormatManager.h"
1147cbf4a0SPavel Labath #include "lldb/Target/Target.h"
1247cbf4a0SPavel Labath #include "lldb/Utility/Status.h"
1347cbf4a0SPavel Labath #include "lldb/Utility/StreamString.h"
1447cbf4a0SPavel Labath
1547cbf4a0SPavel Labath using namespace lldb_private;
1647cbf4a0SPavel Labath using namespace lldb;
1747cbf4a0SPavel Labath
ToBoolean(llvm::StringRef ref,bool fail_value,bool * success_ptr)1847cbf4a0SPavel Labath bool OptionArgParser::ToBoolean(llvm::StringRef ref, bool fail_value,
1947cbf4a0SPavel Labath bool *success_ptr) {
2047cbf4a0SPavel Labath if (success_ptr)
2147cbf4a0SPavel Labath *success_ptr = true;
2247cbf4a0SPavel Labath ref = ref.trim();
23*e50f9c41SMartin Storsjö if (ref.equals_insensitive("false") || ref.equals_insensitive("off") ||
24*e50f9c41SMartin Storsjö ref.equals_insensitive("no") || ref.equals_insensitive("0")) {
2547cbf4a0SPavel Labath return false;
26*e50f9c41SMartin Storsjö } else if (ref.equals_insensitive("true") || ref.equals_insensitive("on") ||
27*e50f9c41SMartin Storsjö ref.equals_insensitive("yes") || ref.equals_insensitive("1")) {
2847cbf4a0SPavel Labath return true;
2947cbf4a0SPavel Labath }
3047cbf4a0SPavel Labath if (success_ptr)
3147cbf4a0SPavel Labath *success_ptr = false;
3247cbf4a0SPavel Labath return fail_value;
3347cbf4a0SPavel Labath }
3447cbf4a0SPavel Labath
ToChar(llvm::StringRef s,char fail_value,bool * success_ptr)3547cbf4a0SPavel Labath char OptionArgParser::ToChar(llvm::StringRef s, char fail_value,
3647cbf4a0SPavel Labath bool *success_ptr) {
3747cbf4a0SPavel Labath if (success_ptr)
3847cbf4a0SPavel Labath *success_ptr = false;
3947cbf4a0SPavel Labath if (s.size() != 1)
4047cbf4a0SPavel Labath return fail_value;
4147cbf4a0SPavel Labath
4247cbf4a0SPavel Labath if (success_ptr)
4347cbf4a0SPavel Labath *success_ptr = true;
4447cbf4a0SPavel Labath return s[0];
4547cbf4a0SPavel Labath }
4647cbf4a0SPavel Labath
ToOptionEnum(llvm::StringRef s,const OptionEnumValues & enum_values,int32_t fail_value,Status & error)4747cbf4a0SPavel Labath int64_t OptionArgParser::ToOptionEnum(llvm::StringRef s,
488fe53c49STatyana Krasnukha const OptionEnumValues &enum_values,
4947cbf4a0SPavel Labath int32_t fail_value, Status &error) {
5047cbf4a0SPavel Labath error.Clear();
518fe53c49STatyana Krasnukha if (enum_values.empty()) {
5247cbf4a0SPavel Labath error.SetErrorString("invalid enumeration argument");
5347cbf4a0SPavel Labath return fail_value;
5447cbf4a0SPavel Labath }
5547cbf4a0SPavel Labath
5647cbf4a0SPavel Labath if (s.empty()) {
5747cbf4a0SPavel Labath error.SetErrorString("empty enumeration string");
5847cbf4a0SPavel Labath return fail_value;
5947cbf4a0SPavel Labath }
6047cbf4a0SPavel Labath
618fe53c49STatyana Krasnukha for (const auto &enum_value : enum_values) {
628fe53c49STatyana Krasnukha llvm::StringRef this_enum(enum_value.string_value);
6347cbf4a0SPavel Labath if (this_enum.startswith(s))
648fe53c49STatyana Krasnukha return enum_value.value;
6547cbf4a0SPavel Labath }
6647cbf4a0SPavel Labath
6747cbf4a0SPavel Labath StreamString strm;
6847cbf4a0SPavel Labath strm.PutCString("invalid enumeration value, valid values are: ");
698fe53c49STatyana Krasnukha bool is_first = true;
708fe53c49STatyana Krasnukha for (const auto &enum_value : enum_values) {
718fe53c49STatyana Krasnukha strm.Printf("%s\"%s\"",
728fe53c49STatyana Krasnukha is_first ? is_first = false,"" : ", ", enum_value.string_value);
7347cbf4a0SPavel Labath }
7447cbf4a0SPavel Labath error.SetErrorString(strm.GetString());
7547cbf4a0SPavel Labath return fail_value;
7647cbf4a0SPavel Labath }
7747cbf4a0SPavel Labath
ToFormat(const char * s,lldb::Format & format,size_t * byte_size_ptr)7847cbf4a0SPavel Labath Status OptionArgParser::ToFormat(const char *s, lldb::Format &format,
7947cbf4a0SPavel Labath size_t *byte_size_ptr) {
8047cbf4a0SPavel Labath format = eFormatInvalid;
8147cbf4a0SPavel Labath Status error;
8247cbf4a0SPavel Labath
8347cbf4a0SPavel Labath if (s && s[0]) {
8447cbf4a0SPavel Labath if (byte_size_ptr) {
8547cbf4a0SPavel Labath if (isdigit(s[0])) {
8647cbf4a0SPavel Labath char *format_char = nullptr;
8747cbf4a0SPavel Labath unsigned long byte_size = ::strtoul(s, &format_char, 0);
8847cbf4a0SPavel Labath if (byte_size != ULONG_MAX)
8947cbf4a0SPavel Labath *byte_size_ptr = byte_size;
9047cbf4a0SPavel Labath s = format_char;
9147cbf4a0SPavel Labath } else
9247cbf4a0SPavel Labath *byte_size_ptr = 0;
9347cbf4a0SPavel Labath }
9447cbf4a0SPavel Labath
9547cbf4a0SPavel Labath const bool partial_match_ok = true;
9647cbf4a0SPavel Labath if (!FormatManager::GetFormatFromCString(s, partial_match_ok, format)) {
9747cbf4a0SPavel Labath StreamString error_strm;
9847cbf4a0SPavel Labath error_strm.Printf(
9947cbf4a0SPavel Labath "Invalid format character or name '%s'. Valid values are:\n", s);
10047cbf4a0SPavel Labath for (Format f = eFormatDefault; f < kNumFormats; f = Format(f + 1)) {
10147cbf4a0SPavel Labath char format_char = FormatManager::GetFormatAsFormatChar(f);
10247cbf4a0SPavel Labath if (format_char)
10347cbf4a0SPavel Labath error_strm.Printf("'%c' or ", format_char);
10447cbf4a0SPavel Labath
10547cbf4a0SPavel Labath error_strm.Printf("\"%s\"", FormatManager::GetFormatAsCString(f));
10647cbf4a0SPavel Labath error_strm.EOL();
10747cbf4a0SPavel Labath }
10847cbf4a0SPavel Labath
10947cbf4a0SPavel Labath if (byte_size_ptr)
11047cbf4a0SPavel Labath error_strm.PutCString(
11147cbf4a0SPavel Labath "An optional byte size can precede the format character.\n");
11247cbf4a0SPavel Labath error.SetErrorString(error_strm.GetString());
11347cbf4a0SPavel Labath }
11447cbf4a0SPavel Labath
11547cbf4a0SPavel Labath if (error.Fail())
11647cbf4a0SPavel Labath return error;
11747cbf4a0SPavel Labath } else {
11847cbf4a0SPavel Labath error.SetErrorStringWithFormat("%s option string", s ? "empty" : "invalid");
11947cbf4a0SPavel Labath }
12047cbf4a0SPavel Labath return error;
12147cbf4a0SPavel Labath }
12247cbf4a0SPavel Labath
ToScriptLanguage(llvm::StringRef s,lldb::ScriptLanguage fail_value,bool * success_ptr)12347cbf4a0SPavel Labath lldb::ScriptLanguage OptionArgParser::ToScriptLanguage(
12447cbf4a0SPavel Labath llvm::StringRef s, lldb::ScriptLanguage fail_value, bool *success_ptr) {
12547cbf4a0SPavel Labath if (success_ptr)
12647cbf4a0SPavel Labath *success_ptr = true;
12747cbf4a0SPavel Labath
128*e50f9c41SMartin Storsjö if (s.equals_insensitive("python"))
12947cbf4a0SPavel Labath return eScriptLanguagePython;
130*e50f9c41SMartin Storsjö if (s.equals_insensitive("lua"))
13167de8962SJonas Devlieghere return eScriptLanguageLua;
132*e50f9c41SMartin Storsjö if (s.equals_insensitive("default"))
13347cbf4a0SPavel Labath return eScriptLanguageDefault;
134*e50f9c41SMartin Storsjö if (s.equals_insensitive("none"))
13547cbf4a0SPavel Labath return eScriptLanguageNone;
13647cbf4a0SPavel Labath
13747cbf4a0SPavel Labath if (success_ptr)
13847cbf4a0SPavel Labath *success_ptr = false;
13947cbf4a0SPavel Labath return fail_value;
14047cbf4a0SPavel Labath }
14147cbf4a0SPavel Labath
ToAddress(const ExecutionContext * exe_ctx,llvm::StringRef s,lldb::addr_t fail_value,Status * error_ptr)14247cbf4a0SPavel Labath lldb::addr_t OptionArgParser::ToAddress(const ExecutionContext *exe_ctx,
14347cbf4a0SPavel Labath llvm::StringRef s,
14447cbf4a0SPavel Labath lldb::addr_t fail_value,
14547cbf4a0SPavel Labath Status *error_ptr) {
14647cbf4a0SPavel Labath bool error_set = false;
14747cbf4a0SPavel Labath if (s.empty()) {
14847cbf4a0SPavel Labath if (error_ptr)
14947cbf4a0SPavel Labath error_ptr->SetErrorStringWithFormat("invalid address expression \"%s\"",
15047cbf4a0SPavel Labath s.str().c_str());
15147cbf4a0SPavel Labath return fail_value;
15247cbf4a0SPavel Labath }
15347cbf4a0SPavel Labath
15447cbf4a0SPavel Labath llvm::StringRef sref = s;
15547cbf4a0SPavel Labath
15647cbf4a0SPavel Labath lldb::addr_t addr = LLDB_INVALID_ADDRESS;
15747cbf4a0SPavel Labath if (!s.getAsInteger(0, addr)) {
15847cbf4a0SPavel Labath if (error_ptr)
15947cbf4a0SPavel Labath error_ptr->Clear();
16047cbf4a0SPavel Labath return addr;
16147cbf4a0SPavel Labath }
16247cbf4a0SPavel Labath
16347cbf4a0SPavel Labath // Try base 16 with no prefix...
16447cbf4a0SPavel Labath if (!s.getAsInteger(16, addr)) {
16547cbf4a0SPavel Labath if (error_ptr)
16647cbf4a0SPavel Labath error_ptr->Clear();
16747cbf4a0SPavel Labath return addr;
16847cbf4a0SPavel Labath }
16947cbf4a0SPavel Labath
17047cbf4a0SPavel Labath Target *target = nullptr;
17147cbf4a0SPavel Labath if (!exe_ctx || !(target = exe_ctx->GetTargetPtr())) {
17247cbf4a0SPavel Labath if (error_ptr)
17347cbf4a0SPavel Labath error_ptr->SetErrorStringWithFormat("invalid address expression \"%s\"",
17447cbf4a0SPavel Labath s.str().c_str());
17547cbf4a0SPavel Labath return fail_value;
17647cbf4a0SPavel Labath }
17747cbf4a0SPavel Labath
17847cbf4a0SPavel Labath lldb::ValueObjectSP valobj_sp;
17947cbf4a0SPavel Labath EvaluateExpressionOptions options;
18047cbf4a0SPavel Labath options.SetCoerceToId(false);
18147cbf4a0SPavel Labath options.SetUnwindOnError(true);
18247cbf4a0SPavel Labath options.SetKeepInMemory(false);
18347cbf4a0SPavel Labath options.SetTryAllThreads(true);
18447cbf4a0SPavel Labath
18547cbf4a0SPavel Labath ExpressionResults expr_result =
18647cbf4a0SPavel Labath target->EvaluateExpression(s, exe_ctx->GetFramePtr(), valobj_sp, options);
18747cbf4a0SPavel Labath
18847cbf4a0SPavel Labath bool success = false;
18947cbf4a0SPavel Labath if (expr_result == eExpressionCompleted) {
19047cbf4a0SPavel Labath if (valobj_sp)
19147cbf4a0SPavel Labath valobj_sp = valobj_sp->GetQualifiedRepresentationIfAvailable(
19247cbf4a0SPavel Labath valobj_sp->GetDynamicValueType(), true);
19347cbf4a0SPavel Labath // Get the address to watch.
19447cbf4a0SPavel Labath if (valobj_sp)
19547cbf4a0SPavel Labath addr = valobj_sp->GetValueAsUnsigned(fail_value, &success);
19647cbf4a0SPavel Labath if (success) {
19747cbf4a0SPavel Labath if (error_ptr)
19847cbf4a0SPavel Labath error_ptr->Clear();
19947cbf4a0SPavel Labath return addr;
20047cbf4a0SPavel Labath } else {
20147cbf4a0SPavel Labath if (error_ptr) {
20247cbf4a0SPavel Labath error_set = true;
20347cbf4a0SPavel Labath error_ptr->SetErrorStringWithFormat(
20447cbf4a0SPavel Labath "address expression \"%s\" resulted in a value whose type "
20547cbf4a0SPavel Labath "can't be converted to an address: %s",
20647cbf4a0SPavel Labath s.str().c_str(), valobj_sp->GetTypeName().GetCString());
20747cbf4a0SPavel Labath }
20847cbf4a0SPavel Labath }
20947cbf4a0SPavel Labath
21047cbf4a0SPavel Labath } else {
21105097246SAdrian Prantl // Since the compiler can't handle things like "main + 12" we should try to
21205097246SAdrian Prantl // do this for now. The compiler doesn't like adding offsets to function
21305097246SAdrian Prantl // pointer types.
21447cbf4a0SPavel Labath static RegularExpression g_symbol_plus_offset_regex(
21547cbf4a0SPavel Labath "^(.*)([-\\+])[[:space:]]*(0x[0-9A-Fa-f]+|[0-9]+)[[:space:]]*$");
21647cbf4a0SPavel Labath
2173af3f1e8SJonas Devlieghere llvm::SmallVector<llvm::StringRef, 4> matches;
2183af3f1e8SJonas Devlieghere if (g_symbol_plus_offset_regex.Execute(sref, &matches)) {
2193af3f1e8SJonas Devlieghere uint64_t offset = 0;
2203af3f1e8SJonas Devlieghere std::string name = matches[1].str();
2213af3f1e8SJonas Devlieghere std::string sign = matches[2].str();
2223af3f1e8SJonas Devlieghere std::string str_offset = matches[3].str();
2233af3f1e8SJonas Devlieghere if (!llvm::StringRef(str_offset).getAsInteger(0, offset)) {
22447cbf4a0SPavel Labath Status error;
2253af3f1e8SJonas Devlieghere addr = ToAddress(exe_ctx, name.c_str(), LLDB_INVALID_ADDRESS, &error);
22647cbf4a0SPavel Labath if (addr != LLDB_INVALID_ADDRESS) {
2273af3f1e8SJonas Devlieghere if (sign[0] == '+')
22847cbf4a0SPavel Labath return addr + offset;
22947cbf4a0SPavel Labath else
23047cbf4a0SPavel Labath return addr - offset;
23147cbf4a0SPavel Labath }
23247cbf4a0SPavel Labath }
23347cbf4a0SPavel Labath }
23447cbf4a0SPavel Labath
23547cbf4a0SPavel Labath if (error_ptr) {
23647cbf4a0SPavel Labath error_set = true;
23747cbf4a0SPavel Labath error_ptr->SetErrorStringWithFormat(
23847cbf4a0SPavel Labath "address expression \"%s\" evaluation failed", s.str().c_str());
23947cbf4a0SPavel Labath }
24047cbf4a0SPavel Labath }
24147cbf4a0SPavel Labath
24247cbf4a0SPavel Labath if (error_ptr) {
24347cbf4a0SPavel Labath if (!error_set)
24447cbf4a0SPavel Labath error_ptr->SetErrorStringWithFormat("invalid address expression \"%s\"",
24547cbf4a0SPavel Labath s.str().c_str());
24647cbf4a0SPavel Labath }
24747cbf4a0SPavel Labath return fail_value;
24847cbf4a0SPavel Labath }
249