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 | |
Pavel Labath | 7eafdce | 2016-04-18 12:18:35 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallString.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Path.h" |
Jason Molenda | 878ae01 | 2016-02-19 00:05:17 +0000 | [diff] [blame] | 22 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | using namespace lldb; |
| 24 | using namespace lldb_private; |
| 25 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 26 | SBFileSpec SBHostOS::GetProgramFileSpec() { |
| 27 | SBFileSpec sb_filespec; |
| 28 | sb_filespec.SetFileSpec(HostInfo::GetProgramFileSpec()); |
| 29 | return sb_filespec; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Jim Ingham | e2231ac | 2012-12-21 22:22:26 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | SBFileSpec SBHostOS::GetUserHomeDirectory() { |
| 50 | SBFileSpec sb_fspec; |
Jason Molenda | 878ae01 | 2016-02-19 00:05:17 +0000 | [diff] [blame] | 51 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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); |
Jason Molenda | 878ae01 | 2016-02-19 00:05:17 +0000 | [diff] [blame] | 55 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | sb_fspec.SetFileSpec(homedir); |
| 57 | return sb_fspec; |
Jason Molenda | 878ae01 | 2016-02-19 00:05:17 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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)); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 64 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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)); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 72 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 73 | // FIXME: You should log the return value? |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 74 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 75 | HostThread thread(ThreadLauncher::LaunchThread( |
| 76 | name, thread_function, thread_arg, error_ptr ? error_ptr->get() : NULL)); |
| 77 | return thread.Release(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 80 | void SBHostOS::ThreadCreated(const char *name) {} |
| 81 | |
| 82 | bool SBHostOS::ThreadCancel(lldb::thread_t thread, SBError *error_ptr) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame^] | 83 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 92 | bool SBHostOS::ThreadDetach(lldb::thread_t thread, SBError *error_ptr) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame^] | 93 | Status error; |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 94 | #if defined(_WIN32) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 95 | if (error_ptr) |
| 96 | error_ptr->SetErrorString("ThreadDetach is not supported on this platform"); |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 97 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 98 | HostThread host_thread(thread); |
| 99 | error = host_thread.GetNativeThread().Detach(); |
| 100 | if (error_ptr) |
| 101 | error_ptr->SetError(error); |
| 102 | host_thread.Release(); |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 103 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 104 | return error.Success(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 107 | bool SBHostOS::ThreadJoin(lldb::thread_t thread, lldb::thread_result_t *result, |
| 108 | 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.Join(result); |
| 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 | } |