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   SBFileSpec sb_lldb_python_filespec;
36   FileSpec lldb_python_spec;
37   if (HostInfo::GetLLDBPath(ePathTypePythonDir, lldb_python_spec)) {
38     sb_lldb_python_filespec.SetFileSpec(lldb_python_spec);
39   }
40   return sb_lldb_python_filespec;
41 }
42 
43 SBFileSpec SBHostOS::GetLLDBPath(lldb::PathType path_type) {
44   SBFileSpec sb_fspec;
45   FileSpec fspec;
46   bool Success = true;
47   if (path_type == ePathTypeClangDir)
48     fspec = GetClangResourceDir();
49   else
50     Success = HostInfo::GetLLDBPath(path_type, fspec);
51   if (Success)
52     sb_fspec.SetFileSpec(fspec);
53   return sb_fspec;
54 }
55 
56 SBFileSpec SBHostOS::GetUserHomeDirectory() {
57   SBFileSpec sb_fspec;
58 
59   llvm::SmallString<64> home_dir_path;
60   llvm::sys::path::home_directory(home_dir_path);
61   FileSpec homedir(home_dir_path.c_str(), true);
62 
63   sb_fspec.SetFileSpec(homedir);
64   return sb_fspec;
65 }
66 
67 lldb::thread_t SBHostOS::ThreadCreate(const char *name,
68                                       lldb::thread_func_t thread_function,
69                                       void *thread_arg, SBError *error_ptr) {
70   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
71 
72   if (log)
73     log->Printf(
74         "SBHostOS::ThreadCreate (name=\"%s\", thread_function=%p, "
75         "thread_arg=%p, error_ptr=%p)",
76         name,
77         reinterpret_cast<void *>(reinterpret_cast<intptr_t>(thread_function)),
78         static_cast<void *>(thread_arg), static_cast<void *>(error_ptr));
79 
80   // FIXME: You should log the return value?
81 
82   HostThread thread(ThreadLauncher::LaunchThread(
83       name, thread_function, thread_arg, error_ptr ? error_ptr->get() : NULL));
84   return thread.Release();
85 }
86 
87 void SBHostOS::ThreadCreated(const char *name) {}
88 
89 bool SBHostOS::ThreadCancel(lldb::thread_t thread, SBError *error_ptr) {
90   Status error;
91   HostThread host_thread(thread);
92   error = host_thread.Cancel();
93   if (error_ptr)
94     error_ptr->SetError(error);
95   host_thread.Release();
96   return error.Success();
97 }
98 
99 bool SBHostOS::ThreadDetach(lldb::thread_t thread, SBError *error_ptr) {
100   Status error;
101 #if defined(_WIN32)
102   if (error_ptr)
103     error_ptr->SetErrorString("ThreadDetach is not supported on this platform");
104 #else
105   HostThread host_thread(thread);
106   error = host_thread.GetNativeThread().Detach();
107   if (error_ptr)
108     error_ptr->SetError(error);
109   host_thread.Release();
110 #endif
111   return error.Success();
112 }
113 
114 bool SBHostOS::ThreadJoin(lldb::thread_t thread, lldb::thread_result_t *result,
115                           SBError *error_ptr) {
116   Status error;
117   HostThread host_thread(thread);
118   error = host_thread.Join(result);
119   if (error_ptr)
120     error_ptr->SetError(error);
121   host_thread.Release();
122   return error.Success();
123 }
124