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