1 //===-- SBHostOS.cpp --------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_DISABLE_PYTHON
10 #include "Plugins/ScriptInterpreter/Python/lldb-python.h"
11 #endif
12 
13 #include "lldb/API/SBError.h"
14 #include "lldb/API/SBHostOS.h"
15 #include "lldb/Host/FileSystem.h"
16 #include "lldb/Host/Host.h"
17 #include "lldb/Host/HostInfo.h"
18 #include "lldb/Host/HostNativeThread.h"
19 #include "lldb/Host/HostThread.h"
20 #include "lldb/Host/ThreadLauncher.h"
21 #include "lldb/Utility/FileSpec.h"
22 #include "lldb/Utility/Log.h"
23 
24 #include "Plugins/ExpressionParser/Clang/ClangHost.h"
25 #ifndef LLDB_DISABLE_PYTHON
26 #include "Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h"
27 #endif
28 
29 #include "llvm/ADT/SmallString.h"
30 #include "llvm/Support/Path.h"
31 
32 using namespace lldb;
33 using namespace lldb_private;
34 
35 SBFileSpec SBHostOS::GetProgramFileSpec() {
36   SBFileSpec sb_filespec;
37   sb_filespec.SetFileSpec(HostInfo::GetProgramFileSpec());
38   return sb_filespec;
39 }
40 
41 SBFileSpec SBHostOS::GetLLDBPythonPath() {
42   return GetLLDBPath(ePathTypePythonDir);
43 }
44 
45 SBFileSpec SBHostOS::GetLLDBPath(lldb::PathType path_type) {
46   FileSpec fspec;
47   switch (path_type) {
48   case ePathTypeLLDBShlibDir:
49     fspec = HostInfo::GetShlibDir();
50     break;
51   case ePathTypeSupportExecutableDir:
52     fspec = HostInfo::GetSupportExeDir();
53     break;
54   case ePathTypeHeaderDir:
55     fspec = HostInfo::GetHeaderDir();
56     break;
57   case ePathTypePythonDir:
58 #ifndef LLDB_DISABLE_PYTHON
59     fspec = ScriptInterpreterPython::GetPythonDir();
60 #endif
61     break;
62   case ePathTypeLLDBSystemPlugins:
63     fspec = HostInfo::GetSystemPluginDir();
64     break;
65   case ePathTypeLLDBUserPlugins:
66     fspec = HostInfo::GetUserPluginDir();
67     break;
68   case ePathTypeLLDBTempSystemDir:
69     fspec = HostInfo::GetProcessTempDir();
70     break;
71   case ePathTypeGlobalLLDBTempSystemDir:
72     fspec = HostInfo::GetGlobalTempDir();
73     break;
74   case ePathTypeClangDir:
75     fspec = GetClangResourceDir();
76     break;
77   }
78 
79   SBFileSpec sb_fspec;
80   sb_fspec.SetFileSpec(fspec);
81   return sb_fspec;
82 }
83 
84 SBFileSpec SBHostOS::GetUserHomeDirectory() {
85   SBFileSpec sb_fspec;
86 
87   llvm::SmallString<64> home_dir_path;
88   llvm::sys::path::home_directory(home_dir_path);
89   FileSpec homedir(home_dir_path.c_str());
90   FileSystem::Instance().Resolve(homedir);
91 
92   sb_fspec.SetFileSpec(homedir);
93   return sb_fspec;
94 }
95 
96 lldb::thread_t SBHostOS::ThreadCreate(const char *name,
97                                       lldb::thread_func_t thread_function,
98                                       void *thread_arg, SBError *error_ptr) {
99   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
100 
101   if (log)
102     log->Printf(
103         "SBHostOS::ThreadCreate (name=\"%s\", thread_function=%p, "
104         "thread_arg=%p, error_ptr=%p)",
105         name,
106         reinterpret_cast<void *>(reinterpret_cast<intptr_t>(thread_function)),
107         static_cast<void *>(thread_arg), static_cast<void *>(error_ptr));
108 
109   // FIXME: You should log the return value?
110 
111   HostThread thread(ThreadLauncher::LaunchThread(
112       name, thread_function, thread_arg, error_ptr ? error_ptr->get() : NULL));
113   return thread.Release();
114 }
115 
116 void SBHostOS::ThreadCreated(const char *name) {}
117 
118 bool SBHostOS::ThreadCancel(lldb::thread_t thread, SBError *error_ptr) {
119   Status error;
120   HostThread host_thread(thread);
121   error = host_thread.Cancel();
122   if (error_ptr)
123     error_ptr->SetError(error);
124   host_thread.Release();
125   return error.Success();
126 }
127 
128 bool SBHostOS::ThreadDetach(lldb::thread_t thread, SBError *error_ptr) {
129   Status error;
130 #if defined(_WIN32)
131   if (error_ptr)
132     error_ptr->SetErrorString("ThreadDetach is not supported on this platform");
133 #else
134   HostThread host_thread(thread);
135   error = host_thread.GetNativeThread().Detach();
136   if (error_ptr)
137     error_ptr->SetError(error);
138   host_thread.Release();
139 #endif
140   return error.Success();
141 }
142 
143 bool SBHostOS::ThreadJoin(lldb::thread_t thread, lldb::thread_result_t *result,
144                           SBError *error_ptr) {
145   Status error;
146   HostThread host_thread(thread);
147   error = host_thread.Join(result);
148   if (error_ptr)
149     error_ptr->SetError(error);
150   host_thread.Release();
151   return error.Success();
152 }
153