1 //===-- SBHostOS.cpp ------------------------------------------------------===//
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
GetProgramFileSpec()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
GetLLDBPythonPath()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
GetLLDBPath(lldb::PathType path_type)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
GetUserHomeDirectory()90 SBFileSpec SBHostOS::GetUserHomeDirectory() {
91 LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBFileSpec, SBHostOS,
92 GetUserHomeDirectory);
93
94 FileSpec homedir;
95 FileSystem::Instance().GetHomeDirectory(homedir);
96 FileSystem::Instance().Resolve(homedir);
97
98 SBFileSpec sb_fspec;
99 sb_fspec.SetFileSpec(homedir);
100
101 return LLDB_RECORD_RESULT(sb_fspec);
102 }
103
ThreadCreate(const char * name,lldb::thread_func_t thread_function,void * thread_arg,SBError * error_ptr)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 llvm::Expected<HostThread> thread =
111 ThreadLauncher::LaunchThread(name, thread_function, thread_arg);
112 if (!thread) {
113 if (error_ptr)
114 error_ptr->SetError(Status(thread.takeError()));
115 else
116 llvm::consumeError(thread.takeError());
117 return LLDB_INVALID_HOST_THREAD;
118 }
119
120 return thread->Release();
121 }
122
ThreadCreated(const char * name)123 void SBHostOS::ThreadCreated(const char *name) {
124 LLDB_RECORD_STATIC_METHOD(void, SBHostOS, ThreadCreated, (const char *),
125 name);
126 }
127
ThreadCancel(lldb::thread_t thread,SBError * error_ptr)128 bool SBHostOS::ThreadCancel(lldb::thread_t thread, SBError *error_ptr) {
129 LLDB_RECORD_DUMMY(bool, SBHostOS, ThreadCancel,
130 (lldb::thread_t, lldb::SBError *), thread,
131 error_ptr);
132
133 Status error;
134 HostThread host_thread(thread);
135 error = host_thread.Cancel();
136 if (error_ptr)
137 error_ptr->SetError(error);
138 host_thread.Release();
139 return error.Success();
140 }
141
ThreadDetach(lldb::thread_t thread,SBError * error_ptr)142 bool SBHostOS::ThreadDetach(lldb::thread_t thread, SBError *error_ptr) {
143 LLDB_RECORD_DUMMY(bool, SBHostOS, ThreadDetach,
144 (lldb::thread_t, lldb::SBError *), thread,
145 error_ptr);
146
147 Status error;
148 #if defined(_WIN32)
149 if (error_ptr)
150 error_ptr->SetErrorString("ThreadDetach is not supported on this platform");
151 #else
152 HostThread host_thread(thread);
153 error = host_thread.GetNativeThread().Detach();
154 if (error_ptr)
155 error_ptr->SetError(error);
156 host_thread.Release();
157 #endif
158 return error.Success();
159 }
160
ThreadJoin(lldb::thread_t thread,lldb::thread_result_t * result,SBError * error_ptr)161 bool SBHostOS::ThreadJoin(lldb::thread_t thread, lldb::thread_result_t *result,
162 SBError *error_ptr) {
163 LLDB_RECORD_DUMMY(
164 bool, SBHostOS, ThreadJoin,
165 (lldb::thread_t, lldb::thread_result_t *, lldb::SBError *), thread,
166 result, error_ptr);
167
168 Status error;
169 HostThread host_thread(thread);
170 error = host_thread.Join(result);
171 if (error_ptr)
172 error_ptr->SetError(error);
173 host_thread.Release();
174 return error.Success();
175 }
176
177 namespace lldb_private {
178 namespace repro {
179
180 template <>
RegisterMethods(Registry & R)181 void RegisterMethods<SBHostOS>(Registry &R) {
182 LLDB_REGISTER_STATIC_METHOD(lldb::SBFileSpec, SBHostOS, GetProgramFileSpec,
183 ());
184 LLDB_REGISTER_STATIC_METHOD(lldb::SBFileSpec, SBHostOS, GetLLDBPythonPath,
185 ());
186 LLDB_REGISTER_STATIC_METHOD(lldb::SBFileSpec, SBHostOS, GetLLDBPath,
187 (lldb::PathType));
188 LLDB_REGISTER_STATIC_METHOD(lldb::SBFileSpec, SBHostOS,
189 GetUserHomeDirectory, ());
190 LLDB_REGISTER_STATIC_METHOD(void, SBHostOS, ThreadCreated, (const char *));
191 }
192
193 }
194 }
195