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 HostThread thread(ThreadLauncher::LaunchThread( 111 name, thread_function, thread_arg, error_ptr ? error_ptr->get() : NULL)); 112 return thread.Release(); 113 } 114 115 void SBHostOS::ThreadCreated(const char *name) { 116 LLDB_RECORD_STATIC_METHOD(void, SBHostOS, ThreadCreated, (const char *), 117 name); 118 } 119 120 bool SBHostOS::ThreadCancel(lldb::thread_t thread, SBError *error_ptr) { 121 LLDB_RECORD_DUMMY(bool, SBHostOS, ThreadCancel, 122 (lldb::thread_t, lldb::SBError *), thread, 123 error_ptr); 124 125 Status error; 126 HostThread host_thread(thread); 127 error = host_thread.Cancel(); 128 if (error_ptr) 129 error_ptr->SetError(error); 130 host_thread.Release(); 131 return error.Success(); 132 } 133 134 bool SBHostOS::ThreadDetach(lldb::thread_t thread, SBError *error_ptr) { 135 LLDB_RECORD_DUMMY(bool, SBHostOS, ThreadDetach, 136 (lldb::thread_t, lldb::SBError *), thread, 137 error_ptr); 138 139 Status error; 140 #if defined(_WIN32) 141 if (error_ptr) 142 error_ptr->SetErrorString("ThreadDetach is not supported on this platform"); 143 #else 144 HostThread host_thread(thread); 145 error = host_thread.GetNativeThread().Detach(); 146 if (error_ptr) 147 error_ptr->SetError(error); 148 host_thread.Release(); 149 #endif 150 return error.Success(); 151 } 152 153 bool SBHostOS::ThreadJoin(lldb::thread_t thread, lldb::thread_result_t *result, 154 SBError *error_ptr) { 155 LLDB_RECORD_DUMMY( 156 bool, SBHostOS, ThreadJoin, 157 (lldb::thread_t, lldb::thread_result_t *, lldb::SBError *), thread, 158 result, error_ptr); 159 160 Status error; 161 HostThread host_thread(thread); 162 error = host_thread.Join(result); 163 if (error_ptr) 164 error_ptr->SetError(error); 165 host_thread.Release(); 166 return error.Success(); 167 } 168 169 namespace lldb_private { 170 namespace repro { 171 172 template <> 173 void RegisterMethods<SBHostOS>(Registry &R) { 174 LLDB_REGISTER_STATIC_METHOD(lldb::SBFileSpec, SBHostOS, GetProgramFileSpec, 175 ()); 176 LLDB_REGISTER_STATIC_METHOD(lldb::SBFileSpec, SBHostOS, GetLLDBPythonPath, 177 ()); 178 LLDB_REGISTER_STATIC_METHOD(lldb::SBFileSpec, SBHostOS, GetLLDBPath, 179 (lldb::PathType)); 180 LLDB_REGISTER_STATIC_METHOD(lldb::SBFileSpec, SBHostOS, 181 GetUserHomeDirectory, ()); 182 LLDB_REGISTER_STATIC_METHOD(void, SBHostOS, ThreadCreated, (const char *)); 183 } 184 185 } 186 } 187