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