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() { |
| 35 | SBFileSpec sb_lldb_python_filespec; |
| 36 | FileSpec lldb_python_spec; |
| 37 | if (HostInfo::GetLLDBPath(ePathTypePythonDir, lldb_python_spec)) { |
| 38 | sb_lldb_python_filespec.SetFileSpec(lldb_python_spec); |
| 39 | } |
| 40 | return sb_lldb_python_filespec; |
Jim Ingham | e2231ac | 2012-12-21 22:22:26 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 43 | SBFileSpec SBHostOS::GetLLDBPath(lldb::PathType path_type) { |
| 44 | SBFileSpec sb_fspec; |
| 45 | FileSpec fspec; |
Zachary Turner | bdf0892 | 2018-06-04 17:41:00 +0000 | [diff] [blame^] | 46 | bool Success = true; |
| 47 | if (path_type == ePathTypeClangDir) |
| 48 | fspec = GetClangResourceDir(); |
| 49 | else |
| 50 | Success = HostInfo::GetLLDBPath(path_type, fspec); |
| 51 | if (Success) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 52 | sb_fspec.SetFileSpec(fspec); |
| 53 | return sb_fspec; |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | SBFileSpec SBHostOS::GetUserHomeDirectory() { |
| 57 | SBFileSpec sb_fspec; |
Jason Molenda | 878ae01 | 2016-02-19 00:05:17 +0000 | [diff] [blame] | 58 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 59 | llvm::SmallString<64> home_dir_path; |
| 60 | llvm::sys::path::home_directory(home_dir_path); |
| 61 | FileSpec homedir(home_dir_path.c_str(), true); |
Jason Molenda | 878ae01 | 2016-02-19 00:05:17 +0000 | [diff] [blame] | 62 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | sb_fspec.SetFileSpec(homedir); |
| 64 | return sb_fspec; |
Jason Molenda | 878ae01 | 2016-02-19 00:05:17 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 67 | lldb::thread_t SBHostOS::ThreadCreate(const char *name, |
| 68 | lldb::thread_func_t thread_function, |
| 69 | void *thread_arg, SBError *error_ptr) { |
| 70 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 71 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 72 | if (log) |
| 73 | log->Printf( |
| 74 | "SBHostOS::ThreadCreate (name=\"%s\", thread_function=%p, " |
| 75 | "thread_arg=%p, error_ptr=%p)", |
| 76 | name, |
| 77 | reinterpret_cast<void *>(reinterpret_cast<intptr_t>(thread_function)), |
| 78 | static_cast<void *>(thread_arg), static_cast<void *>(error_ptr)); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 79 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 80 | // FIXME: You should log the return value? |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 81 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 82 | HostThread thread(ThreadLauncher::LaunchThread( |
| 83 | name, thread_function, thread_arg, error_ptr ? error_ptr->get() : NULL)); |
| 84 | return thread.Release(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 87 | void SBHostOS::ThreadCreated(const char *name) {} |
| 88 | |
| 89 | bool SBHostOS::ThreadCancel(lldb::thread_t thread, SBError *error_ptr) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 90 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | HostThread host_thread(thread); |
| 92 | error = host_thread.Cancel(); |
| 93 | if (error_ptr) |
| 94 | error_ptr->SetError(error); |
| 95 | host_thread.Release(); |
| 96 | return error.Success(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 99 | bool SBHostOS::ThreadDetach(lldb::thread_t thread, SBError *error_ptr) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 100 | Status error; |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 101 | #if defined(_WIN32) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 102 | if (error_ptr) |
| 103 | error_ptr->SetErrorString("ThreadDetach is not supported on this platform"); |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 104 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 105 | HostThread host_thread(thread); |
| 106 | error = host_thread.GetNativeThread().Detach(); |
| 107 | if (error_ptr) |
| 108 | error_ptr->SetError(error); |
| 109 | host_thread.Release(); |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 110 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 111 | return error.Success(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | bool SBHostOS::ThreadJoin(lldb::thread_t thread, lldb::thread_result_t *result, |
| 115 | SBError *error_ptr) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 116 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 117 | HostThread host_thread(thread); |
| 118 | error = host_thread.Join(result); |
| 119 | if (error_ptr) |
| 120 | error_ptr->SetError(error); |
| 121 | host_thread.Release(); |
| 122 | return error.Success(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 123 | } |