34#include "llvm/ADT/ScopeExit.h"
35#include "llvm/Support/ConvertUTF.h"
48 const bool is_host =
false;
51 if (!create && arch && arch->IsValid()) {
52 const llvm::Triple &triple = arch->GetTriple();
53 switch (triple.getVendor()) {
54 case llvm::Triple::PC:
58 case llvm::Triple::UnknownVendor:
59 create = !arch->TripleVendorWasSpecified();
67 switch (triple.getOS()) {
68 case llvm::Triple::Win32:
71 case llvm::Triple::UnknownOS:
72 create = arch->TripleOSWasSpecified();
87 return is_host ?
"Local Windows user platform plug-in."
88 :
"Remote Windows user platform plug-in.";
98 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
120 const auto &AddArch = [&](
const ArchSpec &spec) {
122 return spec.IsExactMatch(rhs);
128 AddArch(HostInfo::GetArchitecture(HostInfo::eArchKindDefault));
129 AddArch(HostInfo::GetArchitecture(HostInfo::eArchKind32));
130 AddArch(HostInfo::GetArchitecture(HostInfo::eArchKind64));
137 "can't connect to the host platform '{0}', always connected",
146 if (
error.Success()) {
151 "\"platform connect\" takes a single argument: <connect-url>");
156 "failed to create a 'remote-gdb-server' platform");
167 const std::vector<std::string> *paths,
172 loaded_image->
Clear();
177 "LoadLibrary error: no thread available to invoke LoadLibrary");
182 thread->CalculateExecutionContext(context);
189 if (loader ==
nullptr)
195 "LoadLibrary error: could not get function caller");
200 llvm::SmallVector<llvm::UTF16, 261> name;
201 if (!llvm::convertUTF8ToUTF16String(remote_file.
GetPath(), name)) {
203 "LoadLibrary error: could not convert path to UCS2");
206 name.emplace_back(L
'\0');
211 ePermissionsReadable | ePermissionsWritable,
215 "LoadLibrary error: unable to allocate memory for name: %s",
220 auto name_cleanup = llvm::make_scope_exit([process, injected_name]() {
225 name.size() *
sizeof(llvm::UTF16), status);
228 "LoadLibrary error: unable to write name: %s", status.
AsCString());
234 std::optional<llvm::detail::scope_exit<std::function<void()>>> paths_cleanup;
236 llvm::SmallVector<llvm::UTF16, 261> search_paths;
238 for (
const auto &path : *paths) {
242 llvm::SmallVector<llvm::UTF16, 261> buffer;
243 if (!llvm::convertUTF8ToUTF16String(path, buffer))
246 search_paths.append(std::begin(buffer), std::end(buffer));
247 search_paths.emplace_back(L
'\0');
249 search_paths.emplace_back(L
'\0');
252 process->
AllocateMemory(search_paths.size() *
sizeof(llvm::UTF16),
253 ePermissionsReadable | ePermissionsWritable,
257 "LoadLibrary error: unable to allocate memory for paths: %s",
262 paths_cleanup.emplace([process, injected_paths]() {
266 process->
WriteMemory(injected_paths, search_paths.data(),
267 search_paths.size() *
sizeof(llvm::UTF16), status);
270 "LoadLibrary error: unable to write paths: %s", status.
AsCString());
279 unsigned injected_length = 261;
283 ePermissionsReadable | ePermissionsWritable,
287 "LoadLibrary error: unable to allocate memory for module location: %s",
292 auto injected_module_path_cleanup =
293 llvm::make_scope_exit([process, injected_module_path]() {
301 ePermissionsReadable | ePermissionsWritable,
305 "LoadLibrary error: could not allocate memory for result: %s",
310 auto result_cleanup = llvm::make_scope_exit([process, injected_result]() {
315 injected_module_path, status);
318 "LoadLibrary error: could not initialize result: %s",
325 Scalar{injected_length},
sizeof(unsigned),
329 "LoadLibrary error: could not initialize result: %s",
343 parameters, diagnostics)) {
346 "LoadLibrary error: unable to write function parameters:"));
350 auto parameter_cleanup =
351 llvm::make_scope_exit([invocation, &context, injected_parameters]() {
357 if (!scratch_ts_sp) {
359 "LoadLibrary error: unable to get (clang) type system");
389 "LoadLibrary error: failed to execute LoadLibrary helper:"));
397 "LoadLibrary error: could not read the result: %s", status.
AsCString());
403 uint64_t error_code =
408 "LoadLibrary error: could not read error status: %s",
418 std::string module_path;
422 "LoadLibrary error: could not read module path: %s",
428 loaded_image->
SetFile(module_path, llvm::sys::path::Style::native);
438 expression.
Printf(
"FreeLibrary((HMODULE)0x%" PRIx64
")", address);
446 if (value->GetError().Fail())
450 if (value->ResolveValue(scalar)) {
465 "can't disconnect from the host platform '{0}', always connected",
511 return Attach(attach_info, debugger, &target,
error);
523 error = process_sp->Launch(launch_info);
543 if (target ==
nullptr) {
550 target = new_target_sp.get();
553 if (!target ||
error.Fail())
562 error = process_sp->Attach(attach_info);
571 llvm::VersionTuple version = HostInfo::GetOSVersion();
572 strm <<
" Host: Windows " << version.getAsString() <<
'\n';
592 const uint8_t *trap_opcode =
nullptr;
593 size_t trap_opcode_size = 0;
596 case llvm::Triple::aarch64: {
597 static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x3e, 0xd4};
598 trap_opcode = g_aarch64_opcode;
599 trap_opcode_size =
sizeof(g_aarch64_opcode);
602 return trap_opcode_size;
606 case llvm::Triple::arm:
607 case llvm::Triple::thumb: {
608 static const uint8_t g_thumb_opcode[] = {0xfe, 0xde};
609 trap_opcode = g_thumb_opcode;
610 trap_opcode_size =
sizeof(g_thumb_opcode);
613 return trap_opcode_size;
622std::unique_ptr<UtilityFunction>
626 static constexpr const char kLoaderDecls[] = R
"(
630// `LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32 | LOAD_LIBRARY_SEARCH_USER_DIRS`
632// Directories in the standard search path are not searched. This value cannot
633// be combined with `LOAD_WITH_ALTERED_SEARCH_PATH`.
635// This value represents the recommended maximum number of directories an
636// application should include in its DLL search path.
637#define LOAD_LIBRARY_SEARCH_DEFAULT_DIRS 0x00001000
639// WINBASEAPI DWORD WINAPI GetLastError(VOID);
640/* __declspec(dllimport) */ uint32_t __stdcall GetLastError();
644// WINBASEAPI DLL_DIRECTORY_COOKIE WINAPI AddDllDirectory(LPCWSTR);
645/* __declspec(dllimport) */ void * __stdcall AddDllDirectory(const wchar_t *);
647// WINBASEAPI BOOL WINAPI FreeModule(HMODULE);
648/* __declspec(dllimport) */ int __stdcall FreeModule(void *hLibModule);
650// WINBASEAPI DWORD WINAPI GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize);
651/* __declspec(dllimport) */ uint32_t GetModuleFileNameA(void *, char *, uint32_t);
653// WINBASEAPI HMODULE WINAPI LoadLibraryExW(LPCWSTR, HANDLE, DWORD);
654/* __declspec(dllimport) */ void * __stdcall LoadLibraryExW(const wchar_t *, void *, uint32_t);
658// _ACRTIMP size_t __cdecl wcslen(wchar_t const *_String);
659/* __declspec(dllimport) */ size_t __cdecl wcslen(const wchar_t *);
663struct __lldb_LoadLibraryResult {
670_Static_assert(sizeof(struct __lldb_LoadLibraryResult) <= 3 * sizeof(void *),
671 "__lldb_LoadLibraryResult size mismatch");
673void * __lldb_LoadLibraryHelper(const wchar_t *name, const wchar_t *paths,
674 __lldb_LoadLibraryResult *result) {
675 for (const wchar_t *path = paths; path && *path; ) {
676 (void)AddDllDirectory(path);
677 path += wcslen(path) + 1;
680 result->ImageBase = LoadLibraryExW(name, nullptr,
681 LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
682 if (result->ImageBase == nullptr)
683 result->ErrorCode = GetLastError();
685 result->Length = GetModuleFileNameA(result->ImageBase, result->ModulePath,
688 return result->ImageBase;
693 static constexpr const char kName[] =
"__lldb_LoadLibraryHelper";
696 Target &target = process->GetTarget();
702 std::string
error = llvm::toString(function.takeError());
704 "LoadLibrary error: could not create utility function: %s",
732 std::unique_ptr<UtilityFunction> utility{std::move(*function)};
733 utility->MakeFunctionCaller(VoidPtrTy, parameters, context.
GetThreadSP(),
737 "LoadLibrary error: could not create function caller: %s",
742 if (!utility->GetFunctionCaller()) {
744 "LoadLibrary error: could not get function caller");
752 const char *expression,
755 static constexpr const char kLoaderDecls[] = R
"(
759// WINBASEAPI DLL_DIRECTORY_COOKIE WINAPI AddDllDirectory(LPCWSTR);
760/* __declspec(dllimport) */ void * __stdcall AddDllDirectory(const wchar_t *);
762// WINBASEAPI BOOL WINAPI FreeModule(HMODULE);
763/* __declspec(dllimport) */ int __stdcall FreeModule(void *);
765// WINBASEAPI DWORD WINAPI GetModuleFileNameA(HMODULE, LPSTR, DWORD);
766/* __declspec(dllimport) */ uint32_t GetModuleFileNameA(void *, char *, uint32_t);
768// WINBASEAPI HMODULE WINAPI LoadLibraryExW(LPCWSTR, HANDLE, DWORD);
769/* __declspec(dllimport) */ void * __stdcall LoadLibraryExW(const wchar_t *, void *, uint32_t);
774 Status result = loader->CanLoadImage();
788 frame->CalculateExecutionContext(context);
802 context, options, expression, kLoaderDecls, value);
804 return value ? value->GetError().Clone() :
Status(
"unknown error");
806 if (value && value->GetError().Fail())
807 return value->GetError().Clone();
static const size_t word_size
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_PLUGIN_DEFINE(PluginName)
An architecture specification class.
bool IsValid() const
Tests if this ArchSpec is valid.
llvm::Triple::ArchType GetMachine() const
Returns a machine family for the current architecture.
A command line argument class.
Class that manages the actual breakpoint that will be inserted into the running program.
bool SetTrapOpcode(const uint8_t *trap_opcode, uint32_t trap_opcode_size)
Sets the trap opcode.
Generic representation of a type in a programming language.
CompilerType GetPointerType() const
Return a new CompilerType that is a pointer to this type.
A uniqued constant string class.
bool IsEmpty() const
Test for empty string.
const char * GetCString() const
Get the string value as a C string.
A class to manage flag bits.
TargetList & GetTargetList()
Get accessor for the target list.
llvm::Error GetAsError(lldb::ExpressionResults result, llvm::Twine message={}) const
Returns an ExpressionError with arg as error code.
A plug-in interface definition class for dynamic loaders.
void SetUnwindOnError(bool unwind=false)
void SetExecutionPolicy(ExecutionPolicy policy=eExecutionPolicyAlways)
void SetLanguage(lldb::LanguageType language_type)
void SetTrapExceptions(bool b)
void SetTimeout(const Timeout< std::micro > &timeout)
void SetIsForUtilityExpr(bool b)
void SetIgnoreBreakpoints(bool ignore=false)
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
const lldb::ProcessSP & GetProcessSP() const
Get accessor to get the process shared pointer.
const lldb::ThreadSP & GetThreadSP() const
Get accessor to get the thread shared pointer.
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
void Clear()
Clears the object state.
ValueType Set(ValueType mask)
Set one or more flags by logical OR'ing mask with the current flags.
Encapsulates a function that can be called.
ValueList GetArgumentValues() const
void DeallocateFunctionResults(ExecutionContext &exe_ctx, lldb::addr_t args_addr)
Deallocate the arguments structure.
lldb::ExpressionResults ExecuteFunction(ExecutionContext &exe_ctx, lldb::addr_t *args_addr_ptr, const EvaluateExpressionOptions &options, DiagnosticManager &diagnostic_manager, Value &results)
Run the function this FunctionCaller was created with.
bool WriteFunctionArguments(ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, DiagnosticManager &diagnostic_manager)
Insert the default function argument struct.
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
lldb::ListenerSP GetListenerForProcess(Debugger &debugger)
llvm::StringRef GetProcessPluginName() const
lldb::ListenerSP GetHijackListener() const
lldb::pid_t GetProcessID() const
lldb::ListenerSP GetListener() const
llvm::StringRef GetProcessPluginName() const
std::chrono::seconds GetUtilityExpressionTimeout() const
A plug-in interface definition class for debugging a process.
UtilityFunction * GetLoadImageUtilityFunction(Platform *platform, llvm::function_ref< std::unique_ptr< UtilityFunction >()> factory)
Get the cached UtilityFunction that assists in loading binary images into the process.
void ResetImageToken(size_t token)
ThreadList & GetThreadList()
lldb::addr_t AllocateMemory(size_t size, uint32_t permissions, Status &error)
The public interface to allocating memory in the process.
size_t ReadCStringFromMemory(lldb::addr_t vm_addr, char *cstr, size_t cstr_max_len, Status &error)
Read a NULL terminated C string from memory.
bool WritePointerToMemory(lldb::addr_t vm_addr, lldb::addr_t ptr_value, Status &error)
size_t AddImageToken(lldb::addr_t image_ptr)
uint64_t ReadUnsignedIntegerFromMemory(lldb::addr_t load_addr, size_t byte_size, uint64_t fail_value, Status &error)
Reads an unsigned integer of the specified byte size from process memory.
lldb::addr_t ReadPointerFromMemory(lldb::addr_t vm_addr, Status &error)
Status DeallocateMemory(lldb::addr_t ptr)
The public interface to deallocating memory in the process.
lldb::addr_t GetImagePtrFromToken(size_t token) const
uint32_t GetAddressByteSize() const
size_t WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size, Status &error)
Write memory to a process.
virtual DynamicLoader * GetDynamicLoader()
Get the dynamic loader plug-in for this process.
size_t WriteScalarToMemory(lldb::addr_t vm_addr, const Scalar &scalar, size_t size, Status &error)
Write all or part of a scalar value to memory.
Target & GetTarget()
Get the target object pointer for this module.
unsigned int UInt(unsigned int fail_value=0) const
static lldb::TypeSystemClangSP GetForTarget(Target &target, std::optional< IsolatedASTKind > ast_kind=DefaultAST, bool create_on_demand=true)
Returns the scratch TypeSystemClang for the given target.
ValueType GetError() const
Access the error value.
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
static Status FromErrorString(const char *str)
bool Fail() const
Test for error condition.
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
static Status static Status FromErrorStringWithFormatv(const char *format, Args &&...args)
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
const char * GetData() const
llvm::StringRef GetString() const
A stream class that can stream formatted output to a file.
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Status CreateTarget(Debugger &debugger, llvm::StringRef user_exe_path, llvm::StringRef triple_str, LoadDependentFiles get_dependent_modules, const OptionGroupPlatform *platform_options, lldb::TargetSP &target_sp)
Create a new Target.
const lldb::ProcessSP & CreateProcess(lldb::ListenerSP listener_sp, llvm::StringRef plugin_name, const FileSpec *crash_file, bool can_connect)
llvm::Expected< std::unique_ptr< UtilityFunction > > CreateUtilityFunction(std::string expression, std::string name, lldb::LanguageType language, ExecutionContext &exe_ctx)
Creates and installs a UtilityFunction for the given language.
const ArchSpec & GetArchitecture() const
lldb::ThreadSP GetExpressionExecutionThread()
static lldb::ExpressionResults Evaluate(ExecutionContext &exe_ctx, const EvaluateExpressionOptions &options, llvm::StringRef expr_cstr, llvm::StringRef expr_prefix, lldb::ValueObjectSP &result_valobj_sp, std::string *fixed_expression=nullptr, ValueObject *ctx_obj=nullptr)
Evaluate one expression in the scratch context of the target passed in the exe_ctx and return its res...
"lldb/Expression/UtilityFunction.h" Encapsulates a bit of source code that provides a function that i...
FunctionCaller * GetFunctionCaller()
void PushValue(const Value &value)
Value * GetValueAtIndex(size_t idx)
const Scalar & GetScalar() const
@ Scalar
A raw scalar value.
void SetCompilerType(const CompilerType &compiler_type)
void SetValueType(ValueType value_type)
#define LLDB_INVALID_IMAGE_TOKEN
#define LLDB_INVALID_ADDRESS
#define LLDB_INVALID_PROCESS_ID
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::Thread > ThreadSP
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
std::shared_ptr< lldb_private::Platform > PlatformSP
@ eLanguageTypeC_plus_plus
ISO C++:1998.
ExpressionResults
The results of expression evaluation.
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::TypeSystemClang > TypeSystemClangSP
std::shared_ptr< lldb_private::Target > TargetSP