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