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 "lldb/API/SBHostOS.h"
10 #include "SBReproducerPrivate.h"
11 #include "lldb/API/SBError.h"
12 #include "lldb/Host/Config.h"
13 #include "lldb/Host/FileSystem.h"
14 #include "lldb/Host/Host.h"
15 #include "lldb/Host/HostInfo.h"
16 #include "lldb/Host/HostNativeThread.h"
17 #include "lldb/Host/HostThread.h"
18 #include "lldb/Host/ThreadLauncher.h"
19 #include "lldb/Utility/FileSpec.h"
21 #include "Plugins/ExpressionParser/Clang/ClangHost.h"
22 #if LLDB_ENABLE_PYTHON
23 #include "Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/Support/Path.h"
30 using namespace lldb_private;
32 SBFileSpec SBHostOS::GetProgramFileSpec() {
33 LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBFileSpec, SBHostOS,
36 SBFileSpec sb_filespec;
37 sb_filespec.SetFileSpec(HostInfo::GetProgramFileSpec());
38 return LLDB_RECORD_RESULT(sb_filespec);
41 SBFileSpec SBHostOS::GetLLDBPythonPath() {
42 LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBFileSpec, SBHostOS,
45 return LLDB_RECORD_RESULT(GetLLDBPath(ePathTypePythonDir));
48 SBFileSpec SBHostOS::GetLLDBPath(lldb::PathType path_type) {
49 LLDB_RECORD_STATIC_METHOD(lldb::SBFileSpec, SBHostOS, GetLLDBPath,
50 (lldb::PathType), path_type);
54 case ePathTypeLLDBShlibDir:
55 fspec = HostInfo::GetShlibDir();
57 case ePathTypeSupportExecutableDir:
58 fspec = HostInfo::GetSupportExeDir();
60 case ePathTypeHeaderDir:
61 fspec = HostInfo::GetHeaderDir();
63 case ePathTypePythonDir:
64 #if LLDB_ENABLE_PYTHON
65 fspec = ScriptInterpreterPython::GetPythonDir();
68 case ePathTypeLLDBSystemPlugins:
69 fspec = HostInfo::GetSystemPluginDir();
71 case ePathTypeLLDBUserPlugins:
72 fspec = HostInfo::GetUserPluginDir();
74 case ePathTypeLLDBTempSystemDir:
75 fspec = HostInfo::GetProcessTempDir();
77 case ePathTypeGlobalLLDBTempSystemDir:
78 fspec = HostInfo::GetGlobalTempDir();
80 case ePathTypeClangDir:
81 fspec = GetClangResourceDir();
86 sb_fspec.SetFileSpec(fspec);
87 return LLDB_RECORD_RESULT(sb_fspec);
90 SBFileSpec SBHostOS::GetUserHomeDirectory() {
91 LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBFileSpec, SBHostOS,
92 GetUserHomeDirectory);
96 llvm::SmallString<64> home_dir_path;
97 llvm::sys::path::home_directory(home_dir_path);
98 FileSpec homedir(home_dir_path.c_str());
99 FileSystem::Instance().Resolve(homedir);
101 sb_fspec.SetFileSpec(homedir);
102 return LLDB_RECORD_RESULT(sb_fspec);
105 lldb::thread_t SBHostOS::ThreadCreate(const char *name,
106 lldb::thread_func_t thread_function,
107 void *thread_arg, SBError *error_ptr) {
108 LLDB_RECORD_DUMMY(lldb::thread_t, SBHostOS, ThreadCreate,
109 (lldb::thread_func_t, void *, SBError *), name,
110 thread_function, thread_arg, error_ptr);
111 llvm::Expected<HostThread> thread =
112 ThreadLauncher::LaunchThread(name, thread_function, thread_arg);
115 error_ptr->SetError(Status(thread.takeError()));
117 llvm::consumeError(thread.takeError());
118 return LLDB_INVALID_HOST_THREAD;
121 return thread->Release();
124 void SBHostOS::ThreadCreated(const char *name) {
125 LLDB_RECORD_STATIC_METHOD(void, SBHostOS, ThreadCreated, (const char *),
129 bool SBHostOS::ThreadCancel(lldb::thread_t thread, SBError *error_ptr) {
130 LLDB_RECORD_DUMMY(bool, SBHostOS, ThreadCancel,
131 (lldb::thread_t, lldb::SBError *), thread,
135 HostThread host_thread(thread);
136 error = host_thread.Cancel();
138 error_ptr->SetError(error);
139 host_thread.Release();
140 return error.Success();
143 bool SBHostOS::ThreadDetach(lldb::thread_t thread, SBError *error_ptr) {
144 LLDB_RECORD_DUMMY(bool, SBHostOS, ThreadDetach,
145 (lldb::thread_t, lldb::SBError *), thread,
151 error_ptr->SetErrorString("ThreadDetach is not supported on this platform");
153 HostThread host_thread(thread);
154 error = host_thread.GetNativeThread().Detach();
156 error_ptr->SetError(error);
157 host_thread.Release();
159 return error.Success();
162 bool SBHostOS::ThreadJoin(lldb::thread_t thread, lldb::thread_result_t *result,
163 SBError *error_ptr) {
165 bool, SBHostOS, ThreadJoin,
166 (lldb::thread_t, lldb::thread_result_t *, lldb::SBError *), thread,
170 HostThread host_thread(thread);
171 error = host_thread.Join(result);
173 error_ptr->SetError(error);
174 host_thread.Release();
175 return error.Success();
178 namespace lldb_private {
182 void RegisterMethods<SBHostOS>(Registry &R) {
183 LLDB_REGISTER_STATIC_METHOD(lldb::SBFileSpec, SBHostOS, GetProgramFileSpec,
185 LLDB_REGISTER_STATIC_METHOD(lldb::SBFileSpec, SBHostOS, GetLLDBPythonPath,
187 LLDB_REGISTER_STATIC_METHOD(lldb::SBFileSpec, SBHostOS, GetLLDBPath,
189 LLDB_REGISTER_STATIC_METHOD(lldb::SBFileSpec, SBHostOS,
190 GetUserHomeDirectory, ());
191 LLDB_REGISTER_STATIC_METHOD(void, SBHostOS, ThreadCreated, (const char *));