OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/globals.h" | 5 #include "platform/globals.h" |
6 #if defined(TARGET_OS_MACOS) | 6 #if defined(TARGET_OS_MACOS) |
7 | 7 |
8 #include <errno.h> // NOLINT | 8 #include <errno.h> // NOLINT |
9 | 9 |
10 #include "vm/flags.h" | 10 #include "vm/flags.h" |
11 #include "vm/os.h" | 11 #include "vm/os.h" |
12 #include "vm/profiler.h" | 12 #include "vm/profiler.h" |
13 #include "vm/signal_handler.h" | 13 #include "vm/signal_handler.h" |
14 #include "vm/thread_interrupter.h" | 14 #include "vm/thread_interrupter.h" |
15 | 15 |
16 namespace dart { | 16 namespace dart { |
17 | 17 |
18 #ifndef PRODUCT | 18 #ifndef PRODUCT |
19 | 19 |
20 DECLARE_FLAG(bool, thread_interrupter); | 20 DECLARE_FLAG(bool, thread_interrupter); |
21 DECLARE_FLAG(bool, trace_thread_interrupter); | 21 DECLARE_FLAG(bool, trace_thread_interrupter); |
22 | 22 |
23 class ThreadInterrupterMacOS : public AllStatic { | 23 |
24 public: | 24 static void ThreadInterruptSignalHandler(int signal, siginfo_t* info, |
25 static void ThreadInterruptSignalHandler(int signal, siginfo_t* info, | 25 void* context_) { |
26 void* context_) { | 26 if (signal != SIGPROF) { |
27 if (signal != SIGPROF) { | 27 return; |
28 return; | |
29 } | |
30 Thread* thread = Thread::Current(); | |
31 if (thread == NULL) { | |
32 return; | |
33 } | |
34 // Extract thread state. | |
35 ucontext_t* context = reinterpret_cast<ucontext_t*>(context_); | |
36 mcontext_t mcontext = context->uc_mcontext; | |
37 InterruptedThreadState its; | |
38 its.pc = SignalHandler::GetProgramCounter(mcontext); | |
39 its.fp = SignalHandler::GetFramePointer(mcontext); | |
40 its.csp = SignalHandler::GetCStackPointer(mcontext); | |
41 its.dsp = SignalHandler::GetDartStackPointer(mcontext); | |
42 its.lr = SignalHandler::GetLinkRegister(mcontext); | |
43 Profiler::SampleThread(thread, its); | |
44 } | 28 } |
45 }; | 29 Thread* thread = Thread::Current(); |
| 30 if (thread == NULL) { |
| 31 return; |
| 32 } |
| 33 // Extract thread state. |
| 34 ucontext_t* context = reinterpret_cast<ucontext_t*>(context_); |
| 35 mcontext_t mcontext = context->uc_mcontext; |
| 36 InterruptedThreadState its; |
| 37 its.pc = SignalHandler::GetProgramCounter(mcontext); |
| 38 its.fp = SignalHandler::GetFramePointer(mcontext); |
| 39 its.csp = SignalHandler::GetCStackPointer(mcontext); |
| 40 its.dsp = SignalHandler::GetDartStackPointer(mcontext); |
| 41 its.lr = SignalHandler::GetLinkRegister(mcontext); |
| 42 Profiler::SampleThread(thread, its); |
| 43 } |
46 | 44 |
47 | 45 |
48 void ThreadInterrupter::InterruptThread(OSThread* thread) { | 46 void ThreadInterrupter::InterruptThread(OSThread* thread) { |
49 if (FLAG_trace_thread_interrupter) { | 47 if (FLAG_trace_thread_interrupter) { |
50 OS::Print("ThreadInterrupter interrupting %p\n", thread->id()); | 48 OS::Print("ThreadInterrupter interrupting %p\n", thread->id()); |
51 } | 49 } |
52 int result = pthread_kill(thread->id(), SIGPROF); | 50 int result = pthread_kill(thread->id(), SIGPROF); |
53 ASSERT((result == 0) || (result == ESRCH)); | 51 ASSERT((result == 0) || (result == ESRCH)); |
54 } | 52 } |
55 | 53 |
56 | 54 |
57 void ThreadInterrupter::InstallSignalHandler() { | 55 void ThreadInterrupter::InstallSignalHandler() { |
58 SignalHandler::Install(ThreadInterrupterMacOS::ThreadInterruptSignalHandler); | 56 SignalHandler::Install(ThreadInterruptSignalHandler); |
59 } | 57 } |
60 | 58 |
61 | 59 |
62 void ThreadInterrupter::RemoveSignalHandler() { | 60 void ThreadInterrupter::RemoveSignalHandler() { |
63 SignalHandler::Remove(); | 61 SignalHandler::Remove(); |
64 } | 62 } |
65 | 63 |
66 #endif // !PRODUCT | 64 #endif // !PRODUCT |
67 | 65 |
68 } // namespace dart | 66 } // namespace dart |
69 | 67 |
70 #endif // defined(TARGET_OS_MACOS) | 68 #endif // defined(TARGET_OS_MACOS) |
OLD | NEW |