LLDB mainline
ThreadLauncher.cpp
Go to the documentation of this file.
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
13#include "lldb/Utility/Log.h"
14
15#if defined(_WIN32)
17#endif
18
19#include "llvm/Support/WindowsError.h"
20
21using namespace lldb;
22using namespace lldb_private;
23
24llvm::Expected<HostThread>
25ThreadLauncher::LaunchThread(llvm::StringRef name,
26 std::function<thread_result_t()> impl,
27 size_t min_stack_byte_size) {
28 // Host::ThreadCreateTrampoline will take ownership if thread creation is
29 // successful.
30 auto info_up = std::make_unique<HostThreadCreateInfo>(name.str(), impl);
31 lldb::thread_t thread;
32#ifdef _WIN32
33 thread = (lldb::thread_t)::_beginthreadex(
34 0, (unsigned)min_stack_byte_size,
35 HostNativeThread::ThreadCreateTrampoline, info_up.get(), 0, NULL);
36 if (thread == LLDB_INVALID_HOST_THREAD)
37 return llvm::errorCodeToError(llvm::mapWindowsError(GetLastError()));
38#else
39
40// ASAN instrumentation adds a lot of bookkeeping overhead on stack frames.
41#if __has_feature(address_sanitizer)
42 const size_t eight_megabytes = 8 * 1024 * 1024;
43 if (min_stack_byte_size < eight_megabytes) {
44 min_stack_byte_size += eight_megabytes;
45 }
46#endif
47
48 pthread_attr_t *thread_attr_ptr = nullptr;
49 pthread_attr_t thread_attr;
50 bool destroy_attr = false;
51 if (min_stack_byte_size > 0) {
52 if (::pthread_attr_init(&thread_attr) == 0) {
53 destroy_attr = true;
54 size_t default_min_stack_byte_size = 0;
55 if (::pthread_attr_getstacksize(&thread_attr,
56 &default_min_stack_byte_size) == 0) {
57 if (default_min_stack_byte_size < min_stack_byte_size) {
58 if (::pthread_attr_setstacksize(&thread_attr, min_stack_byte_size) ==
59 0)
60 thread_attr_ptr = &thread_attr;
61 }
62 }
63 }
64 }
65 int err =
66 ::pthread_create(&thread, thread_attr_ptr,
68
69 if (destroy_attr)
70 ::pthread_attr_destroy(&thread_attr);
71
72 if (err)
73 return llvm::errorCodeToError(
74 std::error_code(err, std::generic_category()));
75#endif
76
77 info_up.release();
78 return HostThread(thread);
79}
static lldb::thread_result_t THREAD_ROUTINE ThreadCreateTrampoline(lldb::thread_arg_t arg)
static llvm::Expected< HostThread > LaunchThread(llvm::StringRef name, std::function< lldb::thread_result_t()> thread_function, size_t min_stack_byte_size=0)
#define LLDB_INVALID_HOST_THREAD
Definition: lldb-types.h:69
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
void * thread_result_t
Definition: lldb-types.h:62
pthread_t thread_t
Definition: lldb-types.h:58