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(HOST_OS_MACOS) |
10 | |
11 | namespace dart { |
12 | |
13 | uintptr_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->__ss.__eip); |
18 | #elif defined(HOST_ARCH_X64) |
19 | pc = static_cast<uintptr_t>(mcontext->__ss.__rip); |
20 | #elif defined(HOST_ARCH_ARM) |
21 | pc = static_cast<uintptr_t>(mcontext->__ss.__pc); |
22 | #elif defined(HOST_ARCH_ARM64) |
23 | pc = static_cast<uintptr_t>(mcontext->__ss.__pc); |
24 | #else |
25 | #error Unsuported architecture. |
26 | #endif // HOST_ARCH_... |
27 | |
28 | return pc; |
29 | } |
30 | |
31 | uintptr_t SignalHandler::GetFramePointer(const mcontext_t& mcontext) { |
32 | uintptr_t fp = 0; |
33 | |
34 | #if defined(HOST_ARCH_IA32) |
35 | fp = static_cast<uintptr_t>(mcontext->__ss.__ebp); |
36 | #elif defined(HOST_ARCH_X64) |
37 | fp = static_cast<uintptr_t>(mcontext->__ss.__rbp); |
38 | #elif defined(HOST_ARCH_ARM) |
39 | fp = static_cast<uintptr_t>(mcontext->__ss.__r[7]); |
40 | #elif defined(HOST_ARCH_ARM64) |
41 | fp = static_cast<uintptr_t>(mcontext->__ss.__fp); |
42 | #else |
43 | #error Unsuported architecture. |
44 | #endif // HOST_ARCH_... |
45 | |
46 | return fp; |
47 | } |
48 | |
49 | uintptr_t SignalHandler::GetCStackPointer(const mcontext_t& mcontext) { |
50 | uintptr_t sp = 0; |
51 | |
52 | #if defined(HOST_ARCH_IA32) |
53 | sp = static_cast<uintptr_t>(mcontext->__ss.__esp); |
54 | #elif defined(HOST_ARCH_X64) |
55 | sp = static_cast<uintptr_t>(mcontext->__ss.__rsp); |
56 | #elif defined(HOST_ARCH_ARM) |
57 | sp = static_cast<uintptr_t>(mcontext->__ss.__sp); |
58 | #elif defined(HOST_ARCH_ARM64) |
59 | sp = static_cast<uintptr_t>(mcontext->__ss.__sp); |
60 | #else |
61 | UNIMPLEMENTED(); |
62 | #endif // HOST_ARCH_... |
63 | |
64 | return sp; |
65 | } |
66 | |
67 | uintptr_t SignalHandler::GetDartStackPointer(const mcontext_t& mcontext) { |
68 | #if defined(TARGET_ARCH_ARM64) && !defined(USING_SIMULATOR) |
69 | return static_cast<uintptr_t>(mcontext->__ss.__x[SPREG]); |
70 | #else |
71 | return GetCStackPointer(mcontext); |
72 | #endif |
73 | } |
74 | |
75 | uintptr_t SignalHandler::GetLinkRegister(const mcontext_t& mcontext) { |
76 | uintptr_t lr = 0; |
77 | |
78 | #if defined(HOST_ARCH_IA32) |
79 | lr = 0; |
80 | #elif defined(HOST_ARCH_X64) |
81 | lr = 0; |
82 | #elif defined(HOST_ARCH_ARM) |
83 | lr = static_cast<uintptr_t>(mcontext->__ss.__lr); |
84 | #elif defined(HOST_ARCH_ARM64) |
85 | lr = static_cast<uintptr_t>(mcontext->__ss.__lr); |
86 | #else |
87 | #error Unsupported architecture. |
88 | #endif // HOST_ARCH_... |
89 | |
90 | return lr; |
91 | } |
92 | |
93 | void SignalHandler::InstallImpl(SignalAction action) { |
94 | struct sigaction act = {}; |
95 | act.sa_handler = NULL; |
96 | act.sa_sigaction = action; |
97 | sigemptyset(&act.sa_mask); |
98 | act.sa_flags = SA_RESTART | SA_SIGINFO; |
99 | int r = sigaction(SIGPROF, &act, NULL); |
100 | ASSERT(r == 0); |
101 | } |
102 | |
103 | void SignalHandler::Remove() { |
104 | // Ignore future SIGPROF signals because by default SIGPROF will terminate |
105 | // the process and we may have some signals in flight. |
106 | struct sigaction act = {}; |
107 | act.sa_handler = SIG_IGN; |
108 | sigemptyset(&act.sa_mask); |
109 | act.sa_flags = 0; |
110 | int r = sigaction(SIGPROF, &act, NULL); |
111 | ASSERT(r == 0); |
112 | } |
113 | |
114 | } // namespace dart |
115 | |
116 | #endif // defined(HOST_OS_MACOS) |
117 | |