1 //===-- SBHostOS.cpp --------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "lldb/API/SBHostOS.h" 11 #include "lldb/API/SBError.h" 12 #include "lldb/Core/Log.h" 13 #include "lldb/Host/FileSpec.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 20 #include "llvm/ADT/SmallString.h" 21 #include "llvm/Support/Path.h" 22 23 using namespace lldb; 24 using namespace lldb_private; 25 26 SBFileSpec SBHostOS::GetProgramFileSpec() { 27 SBFileSpec sb_filespec; 28 sb_filespec.SetFileSpec(HostInfo::GetProgramFileSpec()); 29 return sb_filespec; 30 } 31 32 SBFileSpec SBHostOS::GetLLDBPythonPath() { 33 SBFileSpec sb_lldb_python_filespec; 34 FileSpec lldb_python_spec; 35 if (HostInfo::GetLLDBPath(ePathTypePythonDir, lldb_python_spec)) { 36 sb_lldb_python_filespec.SetFileSpec(lldb_python_spec); 37 } 38 return sb_lldb_python_filespec; 39 } 40 41 SBFileSpec SBHostOS::GetLLDBPath(lldb::PathType path_type) { 42 SBFileSpec sb_fspec; 43 FileSpec fspec; 44 if (HostInfo::GetLLDBPath(path_type, fspec)) 45 sb_fspec.SetFileSpec(fspec); 46 return sb_fspec; 47 } 48 49 SBFileSpec SBHostOS::GetUserHomeDirectory() { 50 SBFileSpec sb_fspec; 51 52 llvm::SmallString<64> home_dir_path; 53 llvm::sys::path::home_directory(home_dir_path); 54 FileSpec homedir(home_dir_path.c_str(), true); 55 56 sb_fspec.SetFileSpec(homedir); 57 return sb_fspec; 58 } 59 60 lldb::thread_t SBHostOS::ThreadCreate(const char *name, 61 lldb::thread_func_t thread_function, 62 void *thread_arg, SBError *error_ptr) { 63 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 64 65 if (log) 66 log->Printf( 67 "SBHostOS::ThreadCreate (name=\"%s\", thread_function=%p, " 68 "thread_arg=%p, error_ptr=%p)", 69 name, 70 reinterpret_cast<void *>(reinterpret_cast<intptr_t>(thread_function)), 71 static_cast<void *>(thread_arg), static_cast<void *>(error_ptr)); 72 73 // FIXME: You should log the return value? 74 75 HostThread thread(ThreadLauncher::LaunchThread( 76 name, thread_function, thread_arg, error_ptr ? error_ptr->get() : NULL)); 77 return thread.Release(); 78 } 79 80 void SBHostOS::ThreadCreated(const char *name) {} 81 82 bool SBHostOS::ThreadCancel(lldb::thread_t thread, SBError *error_ptr) { 83 Error error; 84 HostThread host_thread(thread); 85 error = host_thread.Cancel(); 86 if (error_ptr) 87 error_ptr->SetError(error); 88 host_thread.Release(); 89 return error.Success(); 90 } 91 92 bool SBHostOS::ThreadDetach(lldb::thread_t thread, SBError *error_ptr) { 93 Error error; 94 #if defined(_WIN32) 95 if (error_ptr) 96 error_ptr->SetErrorString("ThreadDetach is not supported on this platform"); 97 #else 98 HostThread host_thread(thread); 99 error = host_thread.GetNativeThread().Detach(); 100 if (error_ptr) 101 error_ptr->SetError(error); 102 host_thread.Release(); 103 #endif 104 return error.Success(); 105 } 106 107 bool SBHostOS::ThreadJoin(lldb::thread_t thread, lldb::thread_result_t *result, 108 SBError *error_ptr) { 109 Error error; 110 HostThread host_thread(thread); 111 error = host_thread.Join(result); 112 if (error_ptr) 113 error_ptr->SetError(error); 114 host_thread.Release(); 115 return error.Success(); 116 } 117