Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | #include "lldb/Host/Host.h" |
Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 13 | #include "lldb/Host/HostInfo.h" |
Zachary Turner | c301899 | 2014-11-17 22:42:57 +0000 | [diff] [blame] | 14 | #include "lldb/Host/HostNativeThread.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 15 | #include "lldb/Host/HostThread.h" |
| 16 | #include "lldb/Host/ThreadLauncher.h" |
Zachary Turner | 5713a05 | 2017-03-22 18:40:07 +0000 | [diff] [blame] | 17 | #include "lldb/Utility/FileSpec.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 18 | #include "lldb/Utility/Log.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 19 | |
Zachary Turner | bdf0892 | 2018-06-04 17:41:00 +0000 | [diff] [blame] | 20 | #include "Plugins/ExpressionParser/Clang/ClangHost.h" |
| 21 | |
Pavel Labath | 7eafdce | 2016-04-18 12:18:35 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Path.h" |
Jason Molenda | 878ae01 | 2016-02-19 00:05:17 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 25 | using namespace lldb; |
| 26 | using namespace lldb_private; |
| 27 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 28 | SBFileSpec SBHostOS::GetProgramFileSpec() { |
| 29 | SBFileSpec sb_filespec; |
| 30 | sb_filespec.SetFileSpec(HostInfo::GetProgramFileSpec()); |
| 31 | return sb_filespec; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 34 | SBFileSpec SBHostOS::GetLLDBPythonPath() { |
Pavel Labath | 60f028f | 2018-06-19 15:09:07 +0000 | [diff] [blame^] | 35 | return GetLLDBPath(ePathTypePythonDir); |
Jim Ingham | e2231ac | 2012-12-21 22:22:26 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 38 | SBFileSpec SBHostOS::GetLLDBPath(lldb::PathType path_type) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 39 | FileSpec fspec; |
Pavel Labath | 60f028f | 2018-06-19 15:09:07 +0000 | [diff] [blame^] | 40 | switch (path_type) { |
| 41 | case ePathTypeLLDBShlibDir: |
| 42 | fspec = HostInfo::GetShlibDir(); |
| 43 | break; |
| 44 | case ePathTypeSupportExecutableDir: |
| 45 | fspec = HostInfo::GetSupportExeDir(); |
| 46 | break; |
| 47 | case ePathTypeHeaderDir: |
| 48 | fspec = HostInfo::GetHeaderDir(); |
| 49 | break; |
| 50 | case ePathTypePythonDir: |
| 51 | fspec = HostInfo::GetPythonDir(); |
| 52 | break; |
| 53 | case ePathTypeLLDBSystemPlugins: |
| 54 | fspec = HostInfo::GetSystemPluginDir(); |
| 55 | break; |
| 56 | case ePathTypeLLDBUserPlugins: |
| 57 | fspec = HostInfo::GetUserPluginDir(); |
| 58 | break; |
| 59 | case ePathTypeLLDBTempSystemDir: |
| 60 | fspec = HostInfo::GetProcessTempDir(); |
| 61 | break; |
| 62 | case ePathTypeGlobalLLDBTempSystemDir: |
| 63 | fspec = HostInfo::GetGlobalTempDir(); |
| 64 | break; |
| 65 | case ePathTypeClangDir: |
Zachary Turner | bdf0892 | 2018-06-04 17:41:00 +0000 | [diff] [blame] | 66 | fspec = GetClangResourceDir(); |
Pavel Labath | 60f028f | 2018-06-19 15:09:07 +0000 | [diff] [blame^] | 67 | break; |
| 68 | } |
| 69 | |
| 70 | SBFileSpec sb_fspec; |
| 71 | sb_fspec.SetFileSpec(fspec); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 72 | return sb_fspec; |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 75 | SBFileSpec SBHostOS::GetUserHomeDirectory() { |
| 76 | SBFileSpec sb_fspec; |
Jason Molenda | 878ae01 | 2016-02-19 00:05:17 +0000 | [diff] [blame] | 77 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 78 | llvm::SmallString<64> home_dir_path; |
| 79 | llvm::sys::path::home_directory(home_dir_path); |
| 80 | FileSpec homedir(home_dir_path.c_str(), true); |
Jason Molenda | 878ae01 | 2016-02-19 00:05:17 +0000 | [diff] [blame] | 81 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 82 | sb_fspec.SetFileSpec(homedir); |
| 83 | return sb_fspec; |
Jason Molenda | 878ae01 | 2016-02-19 00:05:17 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 86 | lldb::thread_t SBHostOS::ThreadCreate(const char *name, |
| 87 | lldb::thread_func_t thread_function, |
| 88 | void *thread_arg, SBError *error_ptr) { |
| 89 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 90 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | if (log) |
| 92 | log->Printf( |
| 93 | "SBHostOS::ThreadCreate (name=\"%s\", thread_function=%p, " |
| 94 | "thread_arg=%p, error_ptr=%p)", |
| 95 | name, |
| 96 | reinterpret_cast<void *>(reinterpret_cast<intptr_t>(thread_function)), |
| 97 | static_cast<void *>(thread_arg), static_cast<void *>(error_ptr)); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 98 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 99 | // FIXME: You should log the return value? |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 100 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | HostThread thread(ThreadLauncher::LaunchThread( |
| 102 | name, thread_function, thread_arg, error_ptr ? error_ptr->get() : NULL)); |
| 103 | return thread.Release(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 106 | void SBHostOS::ThreadCreated(const char *name) {} |
| 107 | |
| 108 | bool SBHostOS::ThreadCancel(lldb::thread_t thread, SBError *error_ptr) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 109 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 110 | HostThread host_thread(thread); |
| 111 | error = host_thread.Cancel(); |
| 112 | if (error_ptr) |
| 113 | error_ptr->SetError(error); |
| 114 | host_thread.Release(); |
| 115 | return error.Success(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 118 | bool SBHostOS::ThreadDetach(lldb::thread_t thread, SBError *error_ptr) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 119 | Status error; |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 120 | #if defined(_WIN32) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 121 | if (error_ptr) |
| 122 | error_ptr->SetErrorString("ThreadDetach is not supported on this platform"); |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 123 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 124 | HostThread host_thread(thread); |
| 125 | error = host_thread.GetNativeThread().Detach(); |
| 126 | if (error_ptr) |
| 127 | error_ptr->SetError(error); |
| 128 | host_thread.Release(); |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 129 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 130 | return error.Success(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 133 | bool SBHostOS::ThreadJoin(lldb::thread_t thread, lldb::thread_result_t *result, |
| 134 | SBError *error_ptr) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 135 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 136 | HostThread host_thread(thread); |
| 137 | error = host_thread.Join(result); |
| 138 | if (error_ptr) |
| 139 | error_ptr->SetError(error); |
| 140 | host_thread.Release(); |
| 141 | return error.Success(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 142 | } |