blob: 8f47eab5be6ee23eb13db3bed205e156c1f0d5f9 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Host/Host.h"
Zachary Turner42ff0ad2014-08-21 17:29:12 +000013#include "lldb/Host/HostInfo.h"
Zachary Turnerc3018992014-11-17 22:42:57 +000014#include "lldb/Host/HostNativeThread.h"
Zachary Turner39de3112014-09-09 20:54:56 +000015#include "lldb/Host/HostThread.h"
16#include "lldb/Host/ThreadLauncher.h"
Zachary Turner5713a052017-03-22 18:40:07 +000017#include "lldb/Utility/FileSpec.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000018#include "lldb/Utility/Log.h"
Zachary Turner39de3112014-09-09 20:54:56 +000019
Zachary Turnerbdf08922018-06-04 17:41:00 +000020#include "Plugins/ExpressionParser/Clang/ClangHost.h"
21
Pavel Labath7eafdce2016-04-18 12:18:35 +000022#include "llvm/ADT/SmallString.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000023#include "llvm/Support/Path.h"
Jason Molenda878ae012016-02-19 00:05:17 +000024
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025using namespace lldb;
26using namespace lldb_private;
27
Kate Stoneb9c1b512016-09-06 20:57:50 +000028SBFileSpec SBHostOS::GetProgramFileSpec() {
29 SBFileSpec sb_filespec;
30 sb_filespec.SetFileSpec(HostInfo::GetProgramFileSpec());
31 return sb_filespec;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032}
33
Kate Stoneb9c1b512016-09-06 20:57:50 +000034SBFileSpec 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;
Jim Inghame2231ac2012-12-21 22:22:26 +000041}
42
Kate Stoneb9c1b512016-09-06 20:57:50 +000043SBFileSpec SBHostOS::GetLLDBPath(lldb::PathType path_type) {
44 SBFileSpec sb_fspec;
45 FileSpec fspec;
Zachary Turnerbdf08922018-06-04 17:41:00 +000046 bool Success = true;
47 if (path_type == ePathTypeClangDir)
48 fspec = GetClangResourceDir();
49 else
50 Success = HostInfo::GetLLDBPath(path_type, fspec);
51 if (Success)
Kate Stoneb9c1b512016-09-06 20:57:50 +000052 sb_fspec.SetFileSpec(fspec);
53 return sb_fspec;
Greg Clayton06357c92014-07-30 17:38:47 +000054}
55
Kate Stoneb9c1b512016-09-06 20:57:50 +000056SBFileSpec SBHostOS::GetUserHomeDirectory() {
57 SBFileSpec sb_fspec;
Jason Molenda878ae012016-02-19 00:05:17 +000058
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 llvm::SmallString<64> home_dir_path;
60 llvm::sys::path::home_directory(home_dir_path);
61 FileSpec homedir(home_dir_path.c_str(), true);
Jason Molenda878ae012016-02-19 00:05:17 +000062
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 sb_fspec.SetFileSpec(homedir);
64 return sb_fspec;
Jason Molenda878ae012016-02-19 00:05:17 +000065}
66
Kate Stoneb9c1b512016-09-06 20:57:50 +000067lldb::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));
Caroline Ticeceb6b132010-10-26 03:11:13 +000071
Kate Stoneb9c1b512016-09-06 20:57:50 +000072 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));
Caroline Ticeceb6b132010-10-26 03:11:13 +000079
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 // FIXME: You should log the return value?
Caroline Ticeceb6b132010-10-26 03:11:13 +000081
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 HostThread thread(ThreadLauncher::LaunchThread(
83 name, thread_function, thread_arg, error_ptr ? error_ptr->get() : NULL));
84 return thread.Release();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085}
86
Kate Stoneb9c1b512016-09-06 20:57:50 +000087void SBHostOS::ThreadCreated(const char *name) {}
88
89bool SBHostOS::ThreadCancel(lldb::thread_t thread, SBError *error_ptr) {
Zachary Turner97206d52017-05-12 04:51:55 +000090 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 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();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097}
98
Kate Stoneb9c1b512016-09-06 20:57:50 +000099bool SBHostOS::ThreadDetach(lldb::thread_t thread, SBError *error_ptr) {
Zachary Turner97206d52017-05-12 04:51:55 +0000100 Status error;
Zachary Turner39de3112014-09-09 20:54:56 +0000101#if defined(_WIN32)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 if (error_ptr)
103 error_ptr->SetErrorString("ThreadDetach is not supported on this platform");
Zachary Turner39de3112014-09-09 20:54:56 +0000104#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105 HostThread host_thread(thread);
106 error = host_thread.GetNativeThread().Detach();
107 if (error_ptr)
108 error_ptr->SetError(error);
109 host_thread.Release();
Zachary Turner39de3112014-09-09 20:54:56 +0000110#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111 return error.Success();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000112}
113
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114bool SBHostOS::ThreadJoin(lldb::thread_t thread, lldb::thread_result_t *result,
115 SBError *error_ptr) {
Zachary Turner97206d52017-05-12 04:51:55 +0000116 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117 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();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000123}