Coverage Report

Created: 2022-01-25 06:29

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Host/common/ThreadLauncher.cpp
Line
Count
Source (jump to first uncovered line)
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
24
llvm::Expected<HostThread> ThreadLauncher::LaunchThread(
25
    llvm::StringRef name, lldb::thread_func_t thread_function,
26
8.60k
    lldb::thread_arg_t thread_arg, size_t min_stack_byte_size) {
27
  // Host::ThreadCreateTrampoline will delete this pointer for us.
28
8.60k
  HostThreadCreateInfo *info_ptr =
29
8.60k
      new HostThreadCreateInfo(name.data(), thread_function, thread_arg);
30
8.60k
  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
8.60k
  pthread_attr_t *thread_attr_ptr = nullptr;
48
8.60k
  pthread_attr_t thread_attr;
49
8.60k
  bool destroy_attr = false;
50
8.60k
  if (min_stack_byte_size > 0) {
51
3.29k
    if (::pthread_attr_init(&thread_attr) == 0) {
52
3.29k
      destroy_attr = true;
53
3.29k
      size_t default_min_stack_byte_size = 0;
54
3.29k
      if (::pthread_attr_getstacksize(&thread_attr,
55
3.29k
                                      &default_min_stack_byte_size) == 0) {
56
3.29k
        if (default_min_stack_byte_size < min_stack_byte_size) {
57
3.29k
          if (::pthread_attr_setstacksize(&thread_attr, min_stack_byte_size) ==
58
3.29k
              0)
59
3.29k
            thread_attr_ptr = &thread_attr;
60
3.29k
        }
61
3.29k
      }
62
3.29k
    }
63
3.29k
  }
64
8.60k
  int err =
65
8.60k
      ::pthread_create(&thread, thread_attr_ptr,
66
8.60k
                       HostNativeThread::ThreadCreateTrampoline, info_ptr);
67
68
8.60k
  if (destroy_attr)
69
3.29k
    ::pthread_attr_destroy(&thread_attr);
70
71
8.60k
  if (err)
72
0
    return llvm::errorCodeToError(
73
0
        std::error_code(err, std::generic_category()));
74
8.60k
#endif
75
76
8.60k
  return HostThread(thread);
77
8.60k
}