Flutter Engine
The Flutter Engine
signal_handler_linux.cc
Go to the documentation of this file.
1// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "vm/globals.h"
6#include "vm/instructions.h"
7#include "vm/signal_handler.h"
8#include "vm/simulator.h"
9#if defined(DART_HOST_OS_LINUX)
10
11namespace dart {
12
13uintptr_t SignalHandler::GetProgramCounter(const mcontext_t& mcontext) {
14 uintptr_t pc = 0;
15
16#if defined(HOST_ARCH_IA32)
17 pc = static_cast<uintptr_t>(mcontext.gregs[REG_EIP]);
18#elif defined(HOST_ARCH_X64)
19 pc = static_cast<uintptr_t>(mcontext.gregs[REG_RIP]);
20#elif defined(HOST_ARCH_ARM)
21 pc = static_cast<uintptr_t>(mcontext.arm_pc);
22#elif defined(HOST_ARCH_ARM64)
23 pc = static_cast<uintptr_t>(mcontext.pc);
24#elif defined(HOST_ARCH_RISCV32)
25 pc = static_cast<uintptr_t>(mcontext.__gregs[REG_PC]);
26#elif defined(HOST_ARCH_RISCV64)
27 pc = static_cast<uintptr_t>(mcontext.__gregs[REG_PC]);
28#else
29#error Unsupported architecture.
30#endif // HOST_ARCH_...
31 return pc;
32}
33
34uintptr_t SignalHandler::GetFramePointer(const mcontext_t& mcontext) {
35 uintptr_t fp = 0;
36
37#if defined(HOST_ARCH_IA32)
38 fp = static_cast<uintptr_t>(mcontext.gregs[REG_EBP]);
39#elif defined(HOST_ARCH_X64)
40 fp = static_cast<uintptr_t>(mcontext.gregs[REG_RBP]);
41#elif defined(HOST_ARCH_ARM)
42 // B1.3.3 Program Status Registers (PSRs)
43 if ((mcontext.arm_cpsr & (1 << 5)) != 0) {
44 // Thumb mode.
45 fp = static_cast<uintptr_t>(mcontext.arm_r7);
46 } else {
47 // ARM mode.
48 fp = static_cast<uintptr_t>(mcontext.arm_fp);
49 }
50#elif defined(HOST_ARCH_ARM64)
51 fp = static_cast<uintptr_t>(mcontext.regs[29]);
52#elif defined(HOST_ARCH_RISCV32)
53 fp = static_cast<uintptr_t>(mcontext.__gregs[REG_S0]);
54#elif defined(HOST_ARCH_RISCV64)
55 fp = static_cast<uintptr_t>(mcontext.__gregs[REG_S0]);
56#else
57#error Unsupported architecture.
58#endif // HOST_ARCH_...
59
60 return fp;
61}
62
63uintptr_t SignalHandler::GetCStackPointer(const mcontext_t& mcontext) {
64 uintptr_t sp = 0;
65
66#if defined(HOST_ARCH_IA32)
67 sp = static_cast<uintptr_t>(mcontext.gregs[REG_ESP]);
68#elif defined(HOST_ARCH_X64)
69 sp = static_cast<uintptr_t>(mcontext.gregs[REG_RSP]);
70#elif defined(HOST_ARCH_ARM)
71 sp = static_cast<uintptr_t>(mcontext.arm_sp);
72#elif defined(HOST_ARCH_ARM64)
73 sp = static_cast<uintptr_t>(mcontext.sp);
74#elif defined(HOST_ARCH_RISCV32)
75 sp = static_cast<uintptr_t>(mcontext.__gregs[REG_SP]);
76#elif defined(HOST_ARCH_RISCV64)
77 sp = static_cast<uintptr_t>(mcontext.__gregs[REG_SP]);
78#else
79#error Unsupported architecture.
80#endif // HOST_ARCH_...
81 return sp;
82}
83
84uintptr_t SignalHandler::GetDartStackPointer(const mcontext_t& mcontext) {
85#if defined(TARGET_ARCH_ARM64) && !defined(USING_SIMULATOR)
86 return static_cast<uintptr_t>(mcontext.regs[SPREG]);
87#else
88 return GetCStackPointer(mcontext);
89#endif
90}
91
92uintptr_t SignalHandler::GetLinkRegister(const mcontext_t& mcontext) {
93 uintptr_t lr = 0;
94
95#if defined(HOST_ARCH_IA32)
96 lr = 0;
97#elif defined(HOST_ARCH_X64)
98 lr = 0;
99#elif defined(HOST_ARCH_ARM)
100 lr = static_cast<uintptr_t>(mcontext.arm_lr);
101#elif defined(HOST_ARCH_ARM64)
102 lr = static_cast<uintptr_t>(mcontext.regs[30]);
103#elif defined(HOST_ARCH_RISCV32)
104 lr = static_cast<uintptr_t>(mcontext.__gregs[REG_RA]);
105#elif defined(HOST_ARCH_RISCV64)
106 lr = static_cast<uintptr_t>(mcontext.__gregs[REG_RA]);
107#else
108#error Unsupported architecture.
109#endif // HOST_ARCH_...
110 return lr;
111}
112
114 struct sigaction act = {};
115 act.sa_handler = nullptr;
116 act.sa_sigaction = action;
117 sigemptyset(&act.sa_mask);
118 sigaddset(&act.sa_mask, SIGPROF); // Prevent nested signals.
119 act.sa_flags = SA_RESTART | SA_SIGINFO;
120 int r = sigaction(SIGPROF, &act, nullptr);
121 ASSERT(r == 0);
122}
123
125 // Ignore future SIGPROF signals because by default SIGPROF will terminate
126 // the process and we may have some signals in flight.
127 struct sigaction act = {};
128 act.sa_handler = SIG_IGN;
129 sigemptyset(&act.sa_mask);
130 act.sa_flags = 0;
131 int r = sigaction(SIGPROF, &act, nullptr);
132 ASSERT(r == 0);
133}
134
135} // namespace dart
136
137#endif // defined(DART_HOST_OS_LINUX)
static void Remove()
static uintptr_t GetDartStackPointer(const mcontext_t &mcontext)
static uintptr_t GetCStackPointer(const mcontext_t &mcontext)
static uintptr_t GetFramePointer(const mcontext_t &mcontext)
static uintptr_t GetProgramCounter(const mcontext_t &mcontext)
static void Install(SignalAction action)
static uintptr_t GetLinkRegister(const mcontext_t &mcontext)
#define ASSERT(E)
Definition: dart_vm.cc:33
void(* SignalAction)(int signal, siginfo_t *info, void *context)
const uint32_t fp
const Register SPREG