1 //===-- SBHostOS.cpp --------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "SBReproducerPrivate.h"
10 #include "lldb/API/SBError.h"
11 #include "lldb/API/SBHostOS.h"
12 #include "lldb/Host/FileSystem.h"
13 #include "lldb/Host/Host.h"
14 #include "lldb/Host/HostInfo.h"
15 #include "lldb/Host/HostNativeThread.h"
16 #include "lldb/Host/HostThread.h"
17 #include "lldb/Host/ThreadLauncher.h"
18 #include "lldb/Utility/FileSpec.h"
20 #include "Plugins/ExpressionParser/Clang/ClangHost.h"
21 #ifndef LLDB_DISABLE_PYTHON
22 #include "Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h"
25 #include "llvm/ADT/SmallString.h"
26 #include "llvm/Support/Path.h"
29 using namespace lldb_private;
31 SBFileSpec SBHostOS::GetProgramFileSpec() {
32 LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBFileSpec, SBHostOS,
35 SBFileSpec sb_filespec;
36 sb_filespec.SetFileSpec(HostInfo::GetProgramFileSpec());
37 return LLDB_RECORD_RESULT(sb_filespec);
40 SBFileSpec SBHostOS::GetLLDBPythonPath() {
41 LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBFileSpec, SBHostOS,
44 return LLDB_RECORD_RESULT(GetLLDBPath(ePathTypePythonDir));
47 SBFileSpec SBHostOS::GetLLDBPath(lldb::PathType path_type) {
48 LLDB_RECORD_STATIC_METHOD(lldb::SBFileSpec, SBHostOS, GetLLDBPath,
49 (lldb::PathType), path_type);
53 case ePathTypeLLDBShlibDir:
54 fspec = HostInfo::GetShlibDir();
56 case ePathTypeSupportExecutableDir:
57 fspec = HostInfo::GetSupportExeDir();
59 case ePathTypeHeaderDir:
60 fspec = HostInfo::GetHeaderDir();
62 case ePathTypePythonDir:
63 #ifndef LLDB_DISABLE_PYTHON
64 fspec = ScriptInterpreterPython::GetPythonDir();
67 case ePathTypeLLDBSystemPlugins:
68 fspec = HostInfo::GetSystemPluginDir();
70 case ePathTypeLLDBUserPlugins:
71 fspec = HostInfo::GetUserPluginDir();
73 case ePathTypeLLDBTempSystemDir:
74 fspec = HostInfo::GetProcessTempDir();
76 case ePathTypeGlobalLLDBTempSystemDir:
77 fspec = HostInfo::GetGlobalTempDir();
79 case ePathTypeClangDir:
80 fspec = GetClangResourceDir();
85 sb_fspec.SetFileSpec(fspec);
86 return LLDB_RECORD_RESULT(sb_fspec);
89 SBFileSpec SBHostOS::GetUserHomeDirectory() {
90 LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBFileSpec, SBHostOS,
91 GetUserHomeDirectory);
95 llvm::SmallString<64> home_dir_path;
96 llvm::sys::path::home_directory(home_dir_path);
97 FileSpec homedir(home_dir_path.c_str());
98 FileSystem::Instance().Resolve(homedir);
100 sb_fspec.SetFileSpec(homedir);
101 return LLDB_RECORD_RESULT(sb_fspec);
104 lldb::thread_t SBHostOS::ThreadCreate(const char *name,
105 lldb::thread_func_t thread_function,
106 void *thread_arg, SBError *error_ptr) {
107 LLDB_RECORD_DUMMY(lldb::thread_t, SBHostOS, ThreadCreate,
108 (lldb::thread_func_t, void *, SBError *), name,
109 thread_function, thread_arg, error_ptr);
110 llvm::Expected<HostThread> thread =
111 ThreadLauncher::LaunchThread(name, thread_function, thread_arg);
114 error_ptr->SetError(Status(thread.takeError()));
116 llvm::consumeError(thread.takeError());
117 return LLDB_INVALID_HOST_THREAD;
120 return thread->Release();
123 void SBHostOS::ThreadCreated(const char *name) {
124 LLDB_RECORD_STATIC_METHOD(void, SBHostOS, ThreadCreated, (const char *),
128 bool SBHostOS::ThreadCancel(lldb::thread_t thread, SBError *error_ptr) {
129 LLDB_RECORD_DUMMY(bool, SBHostOS, ThreadCancel,
130 (lldb::thread_t, lldb::SBError *), thread,
134 HostThread host_thread(thread);
135 error = host_thread.Cancel();
137 error_ptr->SetError(error);
138 host_thread.Release();
139 return error.Success();
142 bool SBHostOS::ThreadDetach(lldb::thread_t thread, SBError *error_ptr) {
143 LLDB_RECORD_DUMMY(bool, SBHostOS, ThreadDetach,
144 (lldb::thread_t, lldb::SBError *), thread,
150 error_ptr->SetErrorString("ThreadDetach is not supported on this platform");
152 HostThread host_thread(thread);
153 error = host_thread.GetNativeThread().Detach();
155 error_ptr->SetError(error);
156 host_thread.Release();
158 return error.Success();
161 bool SBHostOS::ThreadJoin(lldb::thread_t thread, lldb::thread_result_t *result,
162 SBError *error_ptr) {
164 bool, SBHostOS, ThreadJoin,
165 (lldb::thread_t, lldb::thread_result_t *, lldb::SBError *), thread,
169 HostThread host_thread(thread);
170 error = host_thread.Join(result);
172 error_ptr->SetError(error);
173 host_thread.Release();
174 return error.Success();
177 namespace lldb_private {
181 void RegisterMethods<SBHostOS>(Registry &R) {
182 LLDB_REGISTER_STATIC_METHOD(lldb::SBFileSpec, SBHostOS, GetProgramFileSpec,
184 LLDB_REGISTER_STATIC_METHOD(lldb::SBFileSpec, SBHostOS, GetLLDBPythonPath,
186 LLDB_REGISTER_STATIC_METHOD(lldb::SBFileSpec, SBHostOS, GetLLDBPath,
188 LLDB_REGISTER_STATIC_METHOD(lldb::SBFileSpec, SBHostOS,
189 GetUserHomeDirectory, ());
190 LLDB_REGISTER_STATIC_METHOD(void, SBHostOS, ThreadCreated, (const char *));