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