1 //===-- ThreadLauncher.cpp ------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // lldb Includes
10 #include "lldb/Host/ThreadLauncher.h"
11 #include "lldb/Host/HostNativeThread.h"
12 #include "lldb/Host/HostThread.h"
13 #include "lldb/Utility/Log.h"
14
15 #if defined(_WIN32)
16 #include "lldb/Host/windows/windows.h"
17 #endif
18
19 #include "llvm/Support/WindowsError.h"
20
21 using namespace lldb;
22 using namespace lldb_private;
23
LaunchThread(llvm::StringRef name,lldb::thread_func_t thread_function,lldb::thread_arg_t thread_arg,size_t min_stack_byte_size)24 llvm::Expected<HostThread> ThreadLauncher::LaunchThread(
25 llvm::StringRef name, lldb::thread_func_t thread_function,
26 lldb::thread_arg_t thread_arg, size_t min_stack_byte_size) {
27 // Host::ThreadCreateTrampoline will delete this pointer for us.
28 HostThreadCreateInfo *info_ptr =
29 new HostThreadCreateInfo(name.data(), thread_function, thread_arg);
30 lldb::thread_t thread;
31 #ifdef _WIN32
32 thread = (lldb::thread_t)::_beginthreadex(
33 0, (unsigned)min_stack_byte_size,
34 HostNativeThread::ThreadCreateTrampoline, info_ptr, 0, NULL);
35 if (thread == LLDB_INVALID_HOST_THREAD)
36 return llvm::errorCodeToError(llvm::mapWindowsError(GetLastError()));
37 #else
38
39 // ASAN instrumentation adds a lot of bookkeeping overhead on stack frames.
40 #if __has_feature(address_sanitizer)
41 const size_t eight_megabytes = 8 * 1024 * 1024;
42 if (min_stack_byte_size < eight_megabytes) {
43 min_stack_byte_size += eight_megabytes;
44 }
45 #endif
46
47 pthread_attr_t *thread_attr_ptr = nullptr;
48 pthread_attr_t thread_attr;
49 bool destroy_attr = false;
50 if (min_stack_byte_size > 0) {
51 if (::pthread_attr_init(&thread_attr) == 0) {
52 destroy_attr = true;
53 size_t default_min_stack_byte_size = 0;
54 if (::pthread_attr_getstacksize(&thread_attr,
55 &default_min_stack_byte_size) == 0) {
56 if (default_min_stack_byte_size < min_stack_byte_size) {
57 if (::pthread_attr_setstacksize(&thread_attr, min_stack_byte_size) ==
58 0)
59 thread_attr_ptr = &thread_attr;
60 }
61 }
62 }
63 }
64 int err =
65 ::pthread_create(&thread, thread_attr_ptr,
66 HostNativeThread::ThreadCreateTrampoline, info_ptr);
67
68 if (destroy_attr)
69 ::pthread_attr_destroy(&thread_attr);
70
71 if (err)
72 return llvm::errorCodeToError(
73 std::error_code(err, std::generic_category()));
74 #endif
75
76 return HostThread(thread);
77 }
78