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