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