1 //===-- ThreadLauncher.cpp ---------------------------------------*- C++
2 //-*-===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 
11 // lldb Includes
12 #include "lldb/Host/ThreadLauncher.h"
13 #include "lldb/Host/HostNativeThread.h"
14 #include "lldb/Host/HostThread.h"
15 #include "lldb/Utility/Log.h"
16 
17 #if defined(_WIN32)
18 #include "lldb/Host/windows/windows.h"
19 #endif
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,Status * error_ptr,size_t min_stack_byte_size)24 HostThread ThreadLauncher::LaunchThread(llvm::StringRef name,
25                                         lldb::thread_func_t thread_function,
26                                         lldb::thread_arg_t thread_arg,
27                                         Status *error_ptr,
28                                         size_t min_stack_byte_size) {
29   Status error;
30   if (error_ptr)
31     error_ptr->Clear();
32 
33   // Host::ThreadCreateTrampoline will delete this pointer for us.
34   HostThreadCreateInfo *info_ptr =
35       new HostThreadCreateInfo(name.data(), thread_function, thread_arg);
36   lldb::thread_t thread;
37 #ifdef _WIN32
38   thread = (lldb::thread_t)::_beginthreadex(
39       0, (unsigned)min_stack_byte_size,
40       HostNativeThread::ThreadCreateTrampoline, info_ptr, 0, NULL);
41   if (thread == (lldb::thread_t)(-1L))
42     error.SetError(::GetLastError(), eErrorTypeWin32);
43 #else
44 
45 // ASAN instrumentation adds a lot of bookkeeping overhead on stack frames.
46 #if __has_feature(address_sanitizer)
47   const size_t eight_megabytes = 8 * 1024 * 1024;
48   if (min_stack_byte_size < eight_megabytes) {
49     min_stack_byte_size += eight_megabytes;
50   }
51 #endif
52 
53   pthread_attr_t *thread_attr_ptr = NULL;
54   pthread_attr_t thread_attr;
55   bool destroy_attr = false;
56   if (min_stack_byte_size > 0) {
57     if (::pthread_attr_init(&thread_attr) == 0) {
58       destroy_attr = true;
59       size_t default_min_stack_byte_size = 0;
60       if (::pthread_attr_getstacksize(&thread_attr,
61                                       &default_min_stack_byte_size) == 0) {
62         if (default_min_stack_byte_size < min_stack_byte_size) {
63           if (::pthread_attr_setstacksize(&thread_attr, min_stack_byte_size) ==
64               0)
65             thread_attr_ptr = &thread_attr;
66         }
67       }
68     }
69   }
70   int err =
71       ::pthread_create(&thread, thread_attr_ptr,
72                        HostNativeThread::ThreadCreateTrampoline, info_ptr);
73 
74   if (destroy_attr)
75     ::pthread_attr_destroy(&thread_attr);
76 
77   error.SetError(err, eErrorTypePOSIX);
78 #endif
79   if (error_ptr)
80     *error_ptr = error;
81   if (!error.Success())
82     thread = LLDB_INVALID_HOST_THREAD;
83 
84   return HostThread(thread);
85 }
86