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_LINUX) | 6 #if defined(TARGET_OS_LINUX) |
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 DECLARE_FLAG(bool, thread_interrupter); | 18 DECLARE_FLAG(bool, thread_interrupter); |
19 DECLARE_FLAG(bool, trace_thread_interrupter); | 19 DECLARE_FLAG(bool, trace_thread_interrupter); |
20 | 20 |
21 class ThreadInterrupterLinux : public AllStatic { | 21 |
22 public: | 22 static void ThreadInterruptSignalHandler(int signal, siginfo_t* info, |
23 static void ThreadInterruptSignalHandler(int signal, siginfo_t* info, | 23 void* context_) { |
24 void* context_) { | 24 if (signal != SIGPROF) { |
25 if (signal != SIGPROF) { | 25 return; |
26 return; | |
27 } | |
28 Thread* thread = Thread::Current(); | |
29 if (thread == NULL) { | |
30 return; | |
31 } | |
32 // Extract thread state. | |
33 ucontext_t* context = reinterpret_cast<ucontext_t*>(context_); | |
34 mcontext_t mcontext = context->uc_mcontext; | |
35 InterruptedThreadState its; | |
36 its.pc = SignalHandler::GetProgramCounter(mcontext); | |
37 its.fp = SignalHandler::GetFramePointer(mcontext); | |
38 its.csp = SignalHandler::GetCStackPointer(mcontext); | |
39 its.dsp = SignalHandler::GetDartStackPointer(mcontext); | |
40 its.lr = SignalHandler::GetLinkRegister(mcontext); | |
41 Profiler::SampleThread(thread, its); | |
42 } | 26 } |
43 }; | 27 Thread* thread = Thread::Current(); |
| 28 if (thread == NULL) { |
| 29 return; |
| 30 } |
| 31 // Extract thread state. |
| 32 ucontext_t* context = reinterpret_cast<ucontext_t*>(context_); |
| 33 mcontext_t mcontext = context->uc_mcontext; |
| 34 InterruptedThreadState its; |
| 35 its.pc = SignalHandler::GetProgramCounter(mcontext); |
| 36 its.fp = SignalHandler::GetFramePointer(mcontext); |
| 37 its.csp = SignalHandler::GetCStackPointer(mcontext); |
| 38 its.dsp = SignalHandler::GetDartStackPointer(mcontext); |
| 39 its.lr = SignalHandler::GetLinkRegister(mcontext); |
| 40 Profiler::SampleThread(thread, its); |
| 41 } |
44 | 42 |
45 | 43 |
46 void ThreadInterrupter::InterruptThread(OSThread* thread) { | 44 void ThreadInterrupter::InterruptThread(OSThread* thread) { |
47 if (FLAG_trace_thread_interrupter) { | 45 if (FLAG_trace_thread_interrupter) { |
48 OS::Print("ThreadInterrupter interrupting %p\n", | 46 OS::Print("ThreadInterrupter interrupting %p\n", |
49 reinterpret_cast<void*>(thread->id())); | 47 reinterpret_cast<void*>(thread->id())); |
50 } | 48 } |
51 int result = pthread_kill(thread->id(), SIGPROF); | 49 int result = pthread_kill(thread->id(), SIGPROF); |
52 ASSERT((result == 0) || (result == ESRCH)); | 50 ASSERT((result == 0) || (result == ESRCH)); |
53 } | 51 } |
54 | 52 |
55 | 53 |
56 void ThreadInterrupter::InstallSignalHandler() { | 54 void ThreadInterrupter::InstallSignalHandler() { |
57 SignalHandler::Install(ThreadInterrupterLinux::ThreadInterruptSignalHandler); | 55 SignalHandler::Install(ThreadInterruptSignalHandler); |
58 } | 56 } |
59 | 57 |
60 | 58 |
61 void ThreadInterrupter::RemoveSignalHandler() { | 59 void ThreadInterrupter::RemoveSignalHandler() { |
62 SignalHandler::Remove(); | 60 SignalHandler::Remove(); |
63 } | 61 } |
64 | 62 |
65 | 63 |
66 } // namespace dart | 64 } // namespace dart |
67 | 65 |
68 #endif // defined(TARGET_OS_LINUX) | 66 #endif // defined(TARGET_OS_LINUX) |
OLD | NEW |