Coverage Report

Created: 2023-09-21 18:56

/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>
25
ThreadLauncher::LaunchThread(llvm::StringRef name,
26
                             std::function<thread_result_t()> impl,
27
7.41k
                             size_t min_stack_byte_size) {
28
  // Host::ThreadCreateTrampoline will take ownership if thread creation is
29
  // successful.
30
7.41k
  auto info_up = std::make_unique<HostThreadCreateInfo>(name.str(), impl);
31
7.41k
  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
7.41k
  pthread_attr_t *thread_attr_ptr = nullptr;
49
7.41k
  pthread_attr_t thread_attr;
50
7.41k
  bool destroy_attr = false;
51
7.41k
  if (min_stack_byte_size > 0) {
52
2.97k
    if (::pthread_attr_init(&thread_attr) == 0) {
53
2.97k
      destroy_attr = true;
54
2.97k
      size_t default_min_stack_byte_size = 0;
55
2.97k
      if (::pthread_attr_getstacksize(&thread_attr,
56
2.97k
                                      &default_min_stack_byte_size) == 0) {
57
2.97k
        if (default_min_stack_byte_size < min_stack_byte_size) {
58
2.97k
          if (::pthread_attr_setstacksize(&thread_attr, min_stack_byte_size) ==
59
2.97k
              0)
60
2.97k
            thread_attr_ptr = &thread_attr;
61
2.97k
        }
62
2.97k
      }
63
2.97k
    }
64
2.97k
  }
65
7.41k
  int err =
66
7.41k
      ::pthread_create(&thread, thread_attr_ptr,
67
7.41k
                       HostNativeThread::ThreadCreateTrampoline, info_up.get());
68
69
7.41k
  if (destroy_attr)
70
2.97k
    ::pthread_attr_destroy(&thread_attr);
71
72
7.41k
  if (err)
73
0
    return llvm::errorCodeToError(
74
0
        std::error_code(err, std::generic_category()));
75
7.41k
#endif
76
77
7.41k
  info_up.release();
78
7.41k
  return HostThread(thread);
79
7.41k
}