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_ANDROID) | 6 #if defined(TARGET_OS_ANDROID) |
7 | 7 |
8 #include <sys/syscall.h> // NOLINT | 8 #include <sys/syscall.h> // NOLINT |
| 9 #include <errno.h> // NOLINT |
9 | 10 |
10 #include "vm/flags.h" | 11 #include "vm/flags.h" |
11 #include "vm/os.h" | 12 #include "vm/os.h" |
12 #include "vm/signal_handler.h" | 13 #include "vm/signal_handler.h" |
13 #include "vm/thread_interrupter.h" | 14 #include "vm/thread_interrupter.h" |
14 | 15 |
15 namespace dart { | 16 namespace dart { |
16 | 17 |
17 DECLARE_FLAG(bool, thread_interrupter); | 18 DECLARE_FLAG(bool, thread_interrupter); |
18 DECLARE_FLAG(bool, trace_thread_interrupter); | 19 DECLARE_FLAG(bool, trace_thread_interrupter); |
19 | 20 |
20 class ThreadInterrupterAndroid : public AllStatic { | 21 class ThreadInterrupterAndroid : public AllStatic { |
21 public: | 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; |
26 } | 27 } |
27 Thread* thread = Thread::Current(); | 28 Thread* thread = Thread::Current(); |
28 if (thread == NULL) { | 29 if (thread == NULL) { |
29 return; | 30 return; |
30 } | 31 } |
31 ThreadInterruptCallback callback = NULL; | 32 ThreadInterruptCallback callback = NULL; |
32 void* callback_data = NULL; | 33 if (!thread->GetThreadInterruptCallback(&callback)) { |
33 if (!thread->IsThreadInterrupterEnabled(&callback, &callback_data)) { | |
34 return; | 34 return; |
35 } | 35 } |
36 // Extract thread state. | 36 // Extract thread state. |
37 ucontext_t* context = reinterpret_cast<ucontext_t*>(context_); | 37 ucontext_t* context = reinterpret_cast<ucontext_t*>(context_); |
38 mcontext_t mcontext = context->uc_mcontext; | 38 mcontext_t mcontext = context->uc_mcontext; |
39 InterruptedThreadState its; | 39 InterruptedThreadState its; |
40 its.tid = thread->id(); | |
41 its.pc = SignalHandler::GetProgramCounter(mcontext); | 40 its.pc = SignalHandler::GetProgramCounter(mcontext); |
42 its.fp = SignalHandler::GetFramePointer(mcontext); | 41 its.fp = SignalHandler::GetFramePointer(mcontext); |
43 its.csp = SignalHandler::GetCStackPointer(mcontext); | 42 its.csp = SignalHandler::GetCStackPointer(mcontext); |
44 its.dsp = SignalHandler::GetDartStackPointer(mcontext); | 43 its.dsp = SignalHandler::GetDartStackPointer(mcontext); |
45 its.lr = SignalHandler::GetLinkRegister(mcontext); | 44 its.lr = SignalHandler::GetLinkRegister(mcontext); |
46 callback(its, callback_data); | 45 callback(thread, its); |
47 } | 46 } |
48 }; | 47 }; |
49 | 48 |
50 | 49 |
51 void ThreadInterrupter::InterruptThread(Thread* thread) { | 50 void ThreadInterrupter::InterruptThread(Thread* thread) { |
52 if (FLAG_trace_thread_interrupter) { | 51 if (FLAG_trace_thread_interrupter) { |
53 OS::Print("ThreadInterrupter interrupting %p\n", | 52 OS::Print("ThreadInterrupter interrupting %p\n", |
54 reinterpret_cast<void*>(thread->id())); | 53 reinterpret_cast<void*>(thread->id())); |
55 } | 54 } |
56 int result = syscall(__NR_tgkill, getpid(), thread->id(), SIGPROF); | 55 int result = syscall(__NR_tgkill, getpid(), thread->id(), SIGPROF); |
57 ASSERT(result == 0); | 56 ASSERT((result == 0) || (result == ESRCH)); |
58 } | 57 } |
59 | 58 |
60 | 59 |
61 void ThreadInterrupter::InstallSignalHandler() { | 60 void ThreadInterrupter::InstallSignalHandler() { |
62 SignalHandler::Install( | 61 SignalHandler::Install( |
63 ThreadInterrupterAndroid::ThreadInterruptSignalHandler); | 62 ThreadInterrupterAndroid::ThreadInterruptSignalHandler); |
64 } | 63 } |
65 | 64 |
66 | 65 |
67 void ThreadInterrupter::RemoveSignalHandler() { | 66 void ThreadInterrupter::RemoveSignalHandler() { |
68 SignalHandler::Remove(); | 67 SignalHandler::Remove(); |
69 } | 68 } |
70 | 69 |
71 } // namespace dart | 70 } // namespace dart |
72 | 71 |
73 #endif // defined(TARGET_OS_ANDROID) | 72 #endif // defined(TARGET_OS_ANDROID) |
OLD | NEW |