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