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 "SBReproducerPrivate.h"
14 #include "lldb/API/SBError.h"
15 #include "lldb/API/SBHostOS.h"
16 #include "lldb/Host/FileSystem.h"
17 #include "lldb/Host/Host.h"
18 #include "lldb/Host/HostInfo.h"
19 #include "lldb/Host/HostNativeThread.h"
20 #include "lldb/Host/HostThread.h"
21 #include "lldb/Host/ThreadLauncher.h"
22 #include "lldb/Utility/FileSpec.h"
23 #include "lldb/Utility/Log.h"
24 
25 #include "Plugins/ExpressionParser/Clang/ClangHost.h"
26 #ifndef LLDB_DISABLE_PYTHON
27 #include "Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h"
28 #endif
29 
30 #include "llvm/ADT/SmallString.h"
31 #include "llvm/Support/Path.h"
32 
33 using namespace lldb;
34 using namespace lldb_private;
35 
36 SBFileSpec SBHostOS::GetProgramFileSpec() {
37   LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBFileSpec, SBHostOS,
38                                     GetProgramFileSpec);
39 
40   SBFileSpec sb_filespec;
41   sb_filespec.SetFileSpec(HostInfo::GetProgramFileSpec());
42   return LLDB_RECORD_RESULT(sb_filespec);
43 }
44 
45 SBFileSpec SBHostOS::GetLLDBPythonPath() {
46   LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBFileSpec, SBHostOS,
47                                     GetLLDBPythonPath);
48 
49   return LLDB_RECORD_RESULT(GetLLDBPath(ePathTypePythonDir));
50 }
51 
52 SBFileSpec SBHostOS::GetLLDBPath(lldb::PathType path_type) {
53   LLDB_RECORD_STATIC_METHOD(lldb::SBFileSpec, SBHostOS, GetLLDBPath,
54                             (lldb::PathType), path_type);
55 
56   FileSpec fspec;
57   switch (path_type) {
58   case ePathTypeLLDBShlibDir:
59     fspec = HostInfo::GetShlibDir();
60     break;
61   case ePathTypeSupportExecutableDir:
62     fspec = HostInfo::GetSupportExeDir();
63     break;
64   case ePathTypeHeaderDir:
65     fspec = HostInfo::GetHeaderDir();
66     break;
67   case ePathTypePythonDir:
68 #ifndef LLDB_DISABLE_PYTHON
69     fspec = ScriptInterpreterPython::GetPythonDir();
70 #endif
71     break;
72   case ePathTypeLLDBSystemPlugins:
73     fspec = HostInfo::GetSystemPluginDir();
74     break;
75   case ePathTypeLLDBUserPlugins:
76     fspec = HostInfo::GetUserPluginDir();
77     break;
78   case ePathTypeLLDBTempSystemDir:
79     fspec = HostInfo::GetProcessTempDir();
80     break;
81   case ePathTypeGlobalLLDBTempSystemDir:
82     fspec = HostInfo::GetGlobalTempDir();
83     break;
84   case ePathTypeClangDir:
85     fspec = GetClangResourceDir();
86     break;
87   }
88 
89   SBFileSpec sb_fspec;
90   sb_fspec.SetFileSpec(fspec);
91   return LLDB_RECORD_RESULT(sb_fspec);
92 }
93 
94 SBFileSpec SBHostOS::GetUserHomeDirectory() {
95   LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBFileSpec, SBHostOS,
96                                     GetUserHomeDirectory);
97 
98   SBFileSpec sb_fspec;
99 
100   llvm::SmallString<64> home_dir_path;
101   llvm::sys::path::home_directory(home_dir_path);
102   FileSpec homedir(home_dir_path.c_str());
103   FileSystem::Instance().Resolve(homedir);
104 
105   sb_fspec.SetFileSpec(homedir);
106   return LLDB_RECORD_RESULT(sb_fspec);
107 }
108 
109 lldb::thread_t SBHostOS::ThreadCreate(const char *name,
110                                       lldb::thread_func_t thread_function,
111                                       void *thread_arg, SBError *error_ptr) {
112   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
113 
114   if (log)
115     log->Printf(
116         "SBHostOS::ThreadCreate (name=\"%s\", thread_function=%p, "
117         "thread_arg=%p, error_ptr=%p)",
118         name,
119         reinterpret_cast<void *>(reinterpret_cast<intptr_t>(thread_function)),
120         static_cast<void *>(thread_arg), static_cast<void *>(error_ptr));
121 
122   // FIXME: You should log the return value?
123 
124   HostThread thread(ThreadLauncher::LaunchThread(
125       name, thread_function, thread_arg, error_ptr ? error_ptr->get() : NULL));
126   return thread.Release();
127 }
128 
129 void SBHostOS::ThreadCreated(const char *name) {
130   LLDB_RECORD_STATIC_METHOD(void, SBHostOS, ThreadCreated, (const char *),
131                             name);
132 }
133 
134 bool SBHostOS::ThreadCancel(lldb::thread_t thread, SBError *error_ptr) {
135   LLDB_RECORD_STATIC_METHOD(bool, SBHostOS, ThreadCancel,
136                             (lldb::thread_t, lldb::SBError *), thread,
137                             error_ptr);
138 
139   Status error;
140   HostThread host_thread(thread);
141   error = host_thread.Cancel();
142   if (error_ptr)
143     error_ptr->SetError(error);
144   host_thread.Release();
145   return error.Success();
146 }
147 
148 bool SBHostOS::ThreadDetach(lldb::thread_t thread, SBError *error_ptr) {
149   LLDB_RECORD_STATIC_METHOD(bool, SBHostOS, ThreadDetach,
150                             (lldb::thread_t, lldb::SBError *), thread,
151                             error_ptr);
152 
153   Status error;
154 #if defined(_WIN32)
155   if (error_ptr)
156     error_ptr->SetErrorString("ThreadDetach is not supported on this platform");
157 #else
158   HostThread host_thread(thread);
159   error = host_thread.GetNativeThread().Detach();
160   if (error_ptr)
161     error_ptr->SetError(error);
162   host_thread.Release();
163 #endif
164   return error.Success();
165 }
166 
167 bool SBHostOS::ThreadJoin(lldb::thread_t thread, lldb::thread_result_t *result,
168                           SBError *error_ptr) {
169   LLDB_RECORD_STATIC_METHOD(
170       bool, SBHostOS, ThreadJoin,
171       (lldb::thread_t, lldb::thread_result_t *, lldb::SBError *), thread,
172       result, error_ptr);
173 
174   Status error;
175   HostThread host_thread(thread);
176   error = host_thread.Join(result);
177   if (error_ptr)
178     error_ptr->SetError(error);
179   host_thread.Release();
180   return error.Success();
181 }
182