LLDB mainline
ScriptedInterface.h
Go to the documentation of this file.
1//===-- ScriptedInterface.h -------------------------------------*- C++ -*-===//
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#ifndef LLDB_INTERPRETER_INTERFACES_SCRIPTEDINTERFACE_H
10#define LLDB_INTERPRETER_INTERFACES_SCRIPTEDINTERFACE_H
11
13
16#include "lldb/Utility/Log.h"
19#include "lldb/lldb-private.h"
20
21#include "llvm/Support/Compiler.h"
22
23#include <optional>
24#include <string>
25
26namespace lldb_private {
28public:
29 ScriptedInterface() = default;
30 virtual ~ScriptedInterface() = default;
31
35
36 const std::optional<ScriptedMetadata> &GetScriptedMetadata() const {
38 }
39
40 /// Whether the user can invoke this extension directly, the way a scripted
41 /// command can. Those never introduce the target's API mutex bypass, so at
42 /// top level they serialize like any other command; nested inside an
43 /// already-bypassed callback every extension inherits the ambient policy.
44 virtual bool UserCanRunDirectly() const { return false; }
45
47 llvm::StringLiteral name;
48 size_t min_arg_count = 0;
49 };
50
51 virtual llvm::SmallVector<AbstractMethodRequirement>
53
54 virtual llvm::Expected<FileSpec> GetScriptedModulePath() {
55 return llvm::make_error<UnimplementedError>();
56 }
57
58 llvm::SmallVector<llvm::StringLiteral> const GetAbstractMethods() const {
59 llvm::SmallVector<llvm::StringLiteral> abstract_methods;
60 llvm::transform(GetAbstractMethodRequirements(), abstract_methods.begin(),
61 [](const AbstractMethodRequirement &requirement) {
62 return requirement.name;
63 });
64 return abstract_methods;
65 }
66
67 template <typename Ret>
68 static Ret ErrorWithMessage(llvm::StringRef caller_name,
69 llvm::StringRef user_msg, Status &error,
70 LLDBLog log_category = LLDBLog::Process) {
71 LLDB_LOGF(GetLog(log_category), "%s ERROR = %s", caller_name.data(),
72 user_msg.data());
73
74 // If `error` already has detailed content (e.g. a Python traceback),
75 // prepend this call's friendlier message to it instead of discarding
76 // either one.
77 std::string existing_error = error.Fail() ? error.AsCString() : "";
78 if (existing_error.empty())
79 error = Status::FromErrorString(user_msg.data());
80 else
81 error = Status::FromErrorStringWithFormatv("{0}: {1}", user_msg,
82 existing_error);
83
84 return {};
85 }
86
87 template <typename T = StructuredData::ObjectSP>
88 static bool CheckStructuredDataObject(llvm::StringRef caller, T obj,
89 Status &error) {
90 if (!obj)
91 return ErrorWithMessage<bool>(caller, "Null Structured Data object",
92 error);
93
94 if (!obj->IsValid()) {
95 return ErrorWithMessage<bool>(caller, "Invalid StructuredData object",
96 error);
97 }
98
99 if (error.Fail())
100 return ErrorWithMessage<bool>(caller, error.AsCString(), error);
101
102 return true;
103 }
104
107 return false;
108 }
109
110protected:
112 std::optional<ScriptedMetadata> m_scripted_metadata;
113};
114} // namespace lldb_private
115
116#endif // LLDB_INTERPRETER_INTERFACES_SCRIPTEDINTERFACE_H
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition Log.h:389
static Ret ErrorWithMessage(llvm::StringRef caller_name, llvm::StringRef user_msg, Status &error, LLDBLog log_category=LLDBLog::Process)
virtual ~ScriptedInterface()=default
std::optional< ScriptedMetadata > m_scripted_metadata
llvm::SmallVector< llvm::StringLiteral > const GetAbstractMethods() const
virtual llvm::SmallVector< AbstractMethodRequirement > GetAbstractMethodRequirements() const =0
static bool CheckStructuredDataObject(llvm::StringRef caller, T obj, Status &error)
virtual bool UserCanRunDirectly() const
Whether the user can invoke this extension directly, the way a scripted command can.
virtual llvm::Expected< FileSpec > GetScriptedModulePath()
StructuredData::GenericSP GetScriptObjectInstance()
static bool CreateInstance(lldb::ScriptLanguage language, ScriptedInterfaceUsages usages)
const std::optional< ScriptedMetadata > & GetScriptedMetadata() const
StructuredData::GenericSP m_object_instance_sp
An error handling class.
Definition Status.h:118
static Status FromErrorString(const char *str)
Definition Status.h:141
static Status static Status FromErrorStringWithFormatv(const char *format, Args &&...args)
Definition Status.h:151
std::shared_ptr< Generic > GenericSP
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:338
ScriptLanguage
Script interpreter types.