1ac7ddfbfSEd Maste //===-- SBHostOS.cpp --------------------------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste // The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste
104ba319b5SDimitry Andric #ifndef LLDB_DISABLE_PYTHON
114ba319b5SDimitry Andric #include "Plugins/ScriptInterpreter/Python/lldb-python.h"
124ba319b5SDimitry Andric #endif
134ba319b5SDimitry Andric
14ac7ddfbfSEd Maste #include "lldb/API/SBError.h"
15*b5893f02SDimitry Andric #include "lldb/API/SBHostOS.h"
16*b5893f02SDimitry Andric #include "lldb/Host/FileSystem.h"
17ac7ddfbfSEd Maste #include "lldb/Host/Host.h"
180127ef0fSEd Maste #include "lldb/Host/HostInfo.h"
197aa51b79SEd Maste #include "lldb/Host/HostNativeThread.h"
207aa51b79SEd Maste #include "lldb/Host/HostThread.h"
217aa51b79SEd Maste #include "lldb/Host/ThreadLauncher.h"
22f678e45dSDimitry Andric #include "lldb/Utility/FileSpec.h"
23f678e45dSDimitry Andric #include "lldb/Utility/Log.h"
24ac7ddfbfSEd Maste
254ba319b5SDimitry Andric #include "Plugins/ExpressionParser/Clang/ClangHost.h"
264ba319b5SDimitry Andric #ifndef LLDB_DISABLE_PYTHON
274ba319b5SDimitry Andric #include "Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h"
284ba319b5SDimitry Andric #endif
294ba319b5SDimitry Andric
304bb0738eSEd Maste #include "llvm/ADT/SmallString.h"
31435933ddSDimitry Andric #include "llvm/Support/Path.h"
324bb0738eSEd Maste
33ac7ddfbfSEd Maste using namespace lldb;
34ac7ddfbfSEd Maste using namespace lldb_private;
35ac7ddfbfSEd Maste
GetProgramFileSpec()36435933ddSDimitry Andric SBFileSpec SBHostOS::GetProgramFileSpec() {
37ac7ddfbfSEd Maste SBFileSpec sb_filespec;
380127ef0fSEd Maste sb_filespec.SetFileSpec(HostInfo::GetProgramFileSpec());
39ac7ddfbfSEd Maste return sb_filespec;
40ac7ddfbfSEd Maste }
41ac7ddfbfSEd Maste
GetLLDBPythonPath()42435933ddSDimitry Andric SBFileSpec SBHostOS::GetLLDBPythonPath() {
434ba319b5SDimitry Andric return GetLLDBPath(ePathTypePythonDir);
44ac7ddfbfSEd Maste }
45ac7ddfbfSEd Maste
GetLLDBPath(lldb::PathType path_type)46435933ddSDimitry Andric SBFileSpec SBHostOS::GetLLDBPath(lldb::PathType path_type) {
470127ef0fSEd Maste FileSpec fspec;
484ba319b5SDimitry Andric switch (path_type) {
494ba319b5SDimitry Andric case ePathTypeLLDBShlibDir:
504ba319b5SDimitry Andric fspec = HostInfo::GetShlibDir();
514ba319b5SDimitry Andric break;
524ba319b5SDimitry Andric case ePathTypeSupportExecutableDir:
534ba319b5SDimitry Andric fspec = HostInfo::GetSupportExeDir();
544ba319b5SDimitry Andric break;
554ba319b5SDimitry Andric case ePathTypeHeaderDir:
564ba319b5SDimitry Andric fspec = HostInfo::GetHeaderDir();
574ba319b5SDimitry Andric break;
584ba319b5SDimitry Andric case ePathTypePythonDir:
594ba319b5SDimitry Andric #ifndef LLDB_DISABLE_PYTHON
604ba319b5SDimitry Andric fspec = ScriptInterpreterPython::GetPythonDir();
614ba319b5SDimitry Andric #endif
624ba319b5SDimitry Andric break;
634ba319b5SDimitry Andric case ePathTypeLLDBSystemPlugins:
644ba319b5SDimitry Andric fspec = HostInfo::GetSystemPluginDir();
654ba319b5SDimitry Andric break;
664ba319b5SDimitry Andric case ePathTypeLLDBUserPlugins:
674ba319b5SDimitry Andric fspec = HostInfo::GetUserPluginDir();
684ba319b5SDimitry Andric break;
694ba319b5SDimitry Andric case ePathTypeLLDBTempSystemDir:
704ba319b5SDimitry Andric fspec = HostInfo::GetProcessTempDir();
714ba319b5SDimitry Andric break;
724ba319b5SDimitry Andric case ePathTypeGlobalLLDBTempSystemDir:
734ba319b5SDimitry Andric fspec = HostInfo::GetGlobalTempDir();
744ba319b5SDimitry Andric break;
754ba319b5SDimitry Andric case ePathTypeClangDir:
764ba319b5SDimitry Andric fspec = GetClangResourceDir();
774ba319b5SDimitry Andric break;
784ba319b5SDimitry Andric }
794ba319b5SDimitry Andric
804ba319b5SDimitry Andric SBFileSpec sb_fspec;
810127ef0fSEd Maste sb_fspec.SetFileSpec(fspec);
820127ef0fSEd Maste return sb_fspec;
830127ef0fSEd Maste }
840127ef0fSEd Maste
GetUserHomeDirectory()85435933ddSDimitry Andric SBFileSpec SBHostOS::GetUserHomeDirectory() {
864bb0738eSEd Maste SBFileSpec sb_fspec;
874bb0738eSEd Maste
884bb0738eSEd Maste llvm::SmallString<64> home_dir_path;
894bb0738eSEd Maste llvm::sys::path::home_directory(home_dir_path);
90*b5893f02SDimitry Andric FileSpec homedir(home_dir_path.c_str());
91*b5893f02SDimitry Andric FileSystem::Instance().Resolve(homedir);
924bb0738eSEd Maste
934bb0738eSEd Maste sb_fspec.SetFileSpec(homedir);
944bb0738eSEd Maste return sb_fspec;
954bb0738eSEd Maste }
964bb0738eSEd Maste
ThreadCreate(const char * name,lldb::thread_func_t thread_function,void * thread_arg,SBError * error_ptr)97435933ddSDimitry Andric lldb::thread_t SBHostOS::ThreadCreate(const char *name,
980127ef0fSEd Maste lldb::thread_func_t thread_function,
99435933ddSDimitry Andric void *thread_arg, SBError *error_ptr) {
100ac7ddfbfSEd Maste Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
101ac7ddfbfSEd Maste
102ac7ddfbfSEd Maste if (log)
103435933ddSDimitry Andric log->Printf(
104435933ddSDimitry Andric "SBHostOS::ThreadCreate (name=\"%s\", thread_function=%p, "
105435933ddSDimitry Andric "thread_arg=%p, error_ptr=%p)",
106435933ddSDimitry Andric name,
107435933ddSDimitry Andric reinterpret_cast<void *>(reinterpret_cast<intptr_t>(thread_function)),
108435933ddSDimitry Andric static_cast<void *>(thread_arg), static_cast<void *>(error_ptr));
109ac7ddfbfSEd Maste
110ac7ddfbfSEd Maste // FIXME: You should log the return value?
111ac7ddfbfSEd Maste
112435933ddSDimitry Andric HostThread thread(ThreadLauncher::LaunchThread(
113435933ddSDimitry Andric name, thread_function, thread_arg, error_ptr ? error_ptr->get() : NULL));
1147aa51b79SEd Maste return thread.Release();
115ac7ddfbfSEd Maste }
116ac7ddfbfSEd Maste
ThreadCreated(const char * name)117435933ddSDimitry Andric void SBHostOS::ThreadCreated(const char *name) {}
118ac7ddfbfSEd Maste
ThreadCancel(lldb::thread_t thread,SBError * error_ptr)119435933ddSDimitry Andric bool SBHostOS::ThreadCancel(lldb::thread_t thread, SBError *error_ptr) {
1205517e702SDimitry Andric Status error;
1217aa51b79SEd Maste HostThread host_thread(thread);
1227aa51b79SEd Maste error = host_thread.Cancel();
1237aa51b79SEd Maste if (error_ptr)
1247aa51b79SEd Maste error_ptr->SetError(error);
1257aa51b79SEd Maste host_thread.Release();
1267aa51b79SEd Maste return error.Success();
127ac7ddfbfSEd Maste }
128ac7ddfbfSEd Maste
ThreadDetach(lldb::thread_t thread,SBError * error_ptr)129435933ddSDimitry Andric bool SBHostOS::ThreadDetach(lldb::thread_t thread, SBError *error_ptr) {
1305517e702SDimitry Andric Status error;
1317aa51b79SEd Maste #if defined(_WIN32)
1327aa51b79SEd Maste if (error_ptr)
1337aa51b79SEd Maste error_ptr->SetErrorString("ThreadDetach is not supported on this platform");
1347aa51b79SEd Maste #else
1357aa51b79SEd Maste HostThread host_thread(thread);
1367aa51b79SEd Maste error = host_thread.GetNativeThread().Detach();
1377aa51b79SEd Maste if (error_ptr)
1387aa51b79SEd Maste error_ptr->SetError(error);
1397aa51b79SEd Maste host_thread.Release();
1407aa51b79SEd Maste #endif
1417aa51b79SEd Maste return error.Success();
142ac7ddfbfSEd Maste }
143ac7ddfbfSEd Maste
ThreadJoin(lldb::thread_t thread,lldb::thread_result_t * result,SBError * error_ptr)144435933ddSDimitry Andric bool SBHostOS::ThreadJoin(lldb::thread_t thread, lldb::thread_result_t *result,
145435933ddSDimitry Andric SBError *error_ptr) {
1465517e702SDimitry Andric Status error;
1477aa51b79SEd Maste HostThread host_thread(thread);
1487aa51b79SEd Maste error = host_thread.Join(result);
1497aa51b79SEd Maste if (error_ptr)
1507aa51b79SEd Maste error_ptr->SetError(error);
1517aa51b79SEd Maste host_thread.Release();
1527aa51b79SEd Maste return error.Success();
153ac7ddfbfSEd Maste }
154