v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
stack_trace_zos.cc
Go to the documentation of this file.
1// Copyright (c) 2024 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Slightly adapted for inclusion in V8.
6// Copyright 2024 the V8 project authors. All rights reserved.
7
8#include <signal.h>
9
13#include "src/base/logging.h"
14#include "src/base/macros.h"
15
16namespace v8 {
17namespace base {
18namespace debug {
19
20namespace {
21
22bool IsDumpStackInSignalHandler = true;
23
24bool StartThread(void* (*threadEntry)(void*)) {
25 // based on Thread::Start()
26 int result;
27 pthread_attr_t attr;
28 memset(&attr, 0, sizeof(attr));
29 result = pthread_attr_init(&attr);
30 if (result != 0) return false;
31 constexpr size_t kDefaultStackSize = 4 * 1024 * 1024;
32 size_t stack_size;
33 result = pthread_attr_getstacksize(&attr, &stack_size);
34 DCHECK_EQ(0, result);
35 if (stack_size < kDefaultStackSize) stack_size = kDefaultStackSize;
36
37 result = pthread_attr_setstacksize(&attr, stack_size);
38 if (result != 0) return pthread_attr_destroy(&attr), false;
39 {
40 std::mutex lock_guard;
41 pthread_t thread_;
42 result = pthread_create(&thread_, &attr, threadEntry, nullptr);
43 if (result != 0) {
44 perror("pthread_create");
45 return pthread_attr_destroy(&attr), false;
46 }
47 }
48 result = pthread_attr_destroy(&attr);
49 return result == 0;
50}
51
52void StackDumpSignalHandler(int signal, siginfo_t* info, void* void_context) {
53 fprintf(stderr, "Received signal %d\n", signal);
54 if (signal == SIGABRT) {
55 // From third_party/zoslib, will first call __display_traceback().
56 abort();
57 }
58 if (IsDumpStackInSignalHandler) __display_backtrace(STDERR_FILENO);
59 raise(signal);
60}
61
62void* StackDumpingSignalThread(void* data) {
63 struct sigaction sigpipe_action;
64 memset(&sigpipe_action, 0, sizeof(sigpipe_action));
65 sigpipe_action.sa_handler = SIG_IGN;
66 sigemptyset(&sigpipe_action.sa_mask);
67 bool success = (sigaction(SIGPIPE, &sigpipe_action, nullptr) == 0);
68
69 struct sigaction action;
70 memset(&action, 0, sizeof(action));
71 action.sa_flags = SA_RESETHAND | SA_SIGINFO | SA_ONSTACK;
72 action.sa_sigaction = &StackDumpSignalHandler;
73 sigemptyset(&action.sa_mask);
74
75 success &= (sigaction(SIGILL, &action, nullptr) == 0);
76 success &= (sigaction(SIGABRT, &action, nullptr) == 0);
77 success &= (sigaction(SIGFPE, &action, nullptr) == 0);
78 success &= (sigaction(SIGBUS, &action, nullptr) == 0);
79 success &= (sigaction(SIGSEGV, &action, nullptr) == 0);
80 success &= (sigaction(SIGSYS, &action, nullptr) == 0);
81 success &= (sigaction(SIGINT, &action, nullptr) == 0);
82 success &= (sigaction(SIGTERM, &action, nullptr) == 0);
83
84 CHECK_EQ(true, success);
85
86 while (1) {
87 CHECK_EQ(pause(), -1);
88 CHECK_EQ(errno, EINTR);
89 }
90}
91
92} // namespace
93
95 IsDumpStackInSignalHandler = true;
96 bool success = StartThread(StackDumpingSignalThread);
97 CHECK_EQ(true, success);
98 // Block all signals on the main thread:
99 sigset_t set;
100 sigfillset(&set);
101 CHECK_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
102 return success;
103}
104
106 IsDumpStackInSignalHandler = false;
107 // zoslib's abort() displays backtrace by default, so disable it:
108 __set_backtrace_on_abort(false);
109}
110
112
113void StackTrace::Print() const { __display_backtrace(STDERR_FILENO); }
114
115void StackTrace::OutputToStream(std::ostream* os) const {
116 // TODO(gabylb): zos - pending std::osstream version in zoslib:
117 // __display_backtrace(os);
118 UNREACHABLE();
119}
120
121} // namespace debug
122} // namespace base
123} // namespace v8
void OutputToStream(std::ostream *os) const
ZoneVector< RpoNumber > & result
V8_BASE_EXPORT bool EnableInProcessStackDumping()
V8_BASE_EXPORT void DisableSignalStackDump()
#define UNREACHABLE()
Definition logging.h:67
#define CHECK_EQ(lhs, rhs)
#define DCHECK_EQ(v1, v2)
Definition logging.h:485