1 //===-- SBHostOS.cpp --------------------------------------------*- 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 #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" 19 20 #include "Plugins/ExpressionParser/Clang/ClangHost.h" 21 #ifndef LLDB_DISABLE_PYTHON 22 #include "Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h" 23 #endif 24 25 #include "llvm/ADT/SmallString.h" 26 #include "llvm/Support/Path.h" 27 28 using namespace lldb; 29 using namespace lldb_private; 30 31 SBFileSpec SBHostOS::GetProgramFileSpec() { 32 LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBFileSpec, SBHostOS, 33 GetProgramFileSpec); 34 35 SBFileSpec sb_filespec; 36 sb_filespec.SetFileSpec(HostInfo::GetProgramFileSpec()); 37 return LLDB_RECORD_RESULT(sb_filespec); 38 } 39 40 SBFileSpec SBHostOS::GetLLDBPythonPath() { 41 LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBFileSpec, SBHostOS, 42 GetLLDBPythonPath); 43 44 return LLDB_RECORD_RESULT(GetLLDBPath(ePathTypePythonDir)); 45 } 46 47 SBFileSpec SBHostOS::GetLLDBPath(lldb::PathType path_type) { 48 LLDB_RECORD_STATIC_METHOD(lldb::SBFileSpec, SBHostOS, GetLLDBPath, 49 (lldb::PathType), path_type); 50 51 FileSpec fspec; 52 switch (path_type) { 53 case ePathTypeLLDBShlibDir: 54 fspec = HostInfo::GetShlibDir(); 55 break; 56 case ePathTypeSupportExecutableDir: 57 fspec = HostInfo::GetSupportExeDir(); 58 break; 59 case ePathTypeHeaderDir: 60 fspec = HostInfo::GetHeaderDir(); 61 break; 62 case ePathTypePythonDir: 63 #ifndef LLDB_DISABLE_PYTHON 64 fspec = ScriptInterpreterPython::GetPythonDir(); 65 #endif 66 break; 67 case ePathTypeLLDBSystemPlugins: 68 fspec = HostInfo::GetSystemPluginDir(); 69 break; 70 case ePathTypeLLDBUserPlugins: 71 fspec = HostInfo::GetUserPluginDir(); 72 break; 73 case ePathTypeLLDBTempSystemDir: 74 fspec = HostInfo::GetProcessTempDir(); 75 break; 76 case ePathTypeGlobalLLDBTempSystemDir: 77 fspec = HostInfo::GetGlobalTempDir(); 78 break; 79 case ePathTypeClangDir: 80 fspec = GetClangResourceDir(); 81 break; 82 } 83 84 SBFileSpec sb_fspec; 85 sb_fspec.SetFileSpec(fspec); 86 return LLDB_RECORD_RESULT(sb_fspec); 87 } 88 89 SBFileSpec SBHostOS::GetUserHomeDirectory() { 90 LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBFileSpec, SBHostOS, 91 GetUserHomeDirectory); 92 93 SBFileSpec sb_fspec; 94 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); 99 100 sb_fspec.SetFileSpec(homedir); 101 return LLDB_RECORD_RESULT(sb_fspec); 102 } 103 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); 112 if (!thread) { 113 if (error_ptr) 114 error_ptr->SetError(Status(thread.takeError())); 115 else 116 llvm::consumeError(thread.takeError()); 117 return LLDB_INVALID_HOST_THREAD; 118 } 119 120 return thread->Release(); 121 } 122 123 void SBHostOS::ThreadCreated(const char *name) { 124 LLDB_RECORD_STATIC_METHOD(void, SBHostOS, ThreadCreated, (const char *), 125 name); 126 } 127 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, 131 error_ptr); 132 133 Status error; 134 HostThread host_thread(thread); 135 error = host_thread.Cancel(); 136 if (error_ptr) 137 error_ptr->SetError(error); 138 host_thread.Release(); 139 return error.Success(); 140 } 141 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, 145 error_ptr); 146 147 Status error; 148 #if defined(_WIN32) 149 if (error_ptr) 150 error_ptr->SetErrorString("ThreadDetach is not supported on this platform"); 151 #else 152 HostThread host_thread(thread); 153 error = host_thread.GetNativeThread().Detach(); 154 if (error_ptr) 155 error_ptr->SetError(error); 156 host_thread.Release(); 157 #endif 158 return error.Success(); 159 } 160 161 bool SBHostOS::ThreadJoin(lldb::thread_t thread, lldb::thread_result_t *result, 162 SBError *error_ptr) { 163 LLDB_RECORD_DUMMY( 164 bool, SBHostOS, ThreadJoin, 165 (lldb::thread_t, lldb::thread_result_t *, lldb::SBError *), thread, 166 result, error_ptr); 167 168 Status error; 169 HostThread host_thread(thread); 170 error = host_thread.Join(result); 171 if (error_ptr) 172 error_ptr->SetError(error); 173 host_thread.Release(); 174 return error.Success(); 175 } 176 177 namespace lldb_private { 178 namespace repro { 179 180 template <> 181 void RegisterMethods<SBHostOS>(Registry &R) { 182 LLDB_REGISTER_STATIC_METHOD(lldb::SBFileSpec, SBHostOS, GetProgramFileSpec, 183 ()); 184 LLDB_REGISTER_STATIC_METHOD(lldb::SBFileSpec, SBHostOS, GetLLDBPythonPath, 185 ()); 186 LLDB_REGISTER_STATIC_METHOD(lldb::SBFileSpec, SBHostOS, GetLLDBPath, 187 (lldb::PathType)); 188 LLDB_REGISTER_STATIC_METHOD(lldb::SBFileSpec, SBHostOS, 189 GetUserHomeDirectory, ()); 190 LLDB_REGISTER_STATIC_METHOD(void, SBHostOS, ThreadCreated, (const char *)); 191 } 192 193 } 194 } 195