Flutter Engine
The Flutter Engine
thread_interrupter_android.cc
Go to the documentation of this 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
3// BSD-style license that can be found in the LICENSE file.
4
5#include "platform/globals.h"
6#if defined(DART_HOST_OS_ANDROID)
7
8#include <errno.h> // NOLINT
9#include <sys/syscall.h> // NOLINT
10
11#include "vm/flags.h"
12#include "vm/os.h"
13#include "vm/profiler.h"
14#include "vm/signal_handler.h"
16
17namespace dart {
18
19#ifndef PRODUCT
20
21// Old linux kernels on ARM might require a trampoline to
22// work around incorrect Thumb -> ARM transitions.
23// See thread_interrupted_android_arm.S for more details.
24#if defined(HOST_ARCH_ARM) && \
25 (defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_ANDROID)) && \
26 !defined(__thumb__)
27#define USE_SIGNAL_HANDLER_TRAMPOLINE
28#endif
29
30DECLARE_FLAG(bool, trace_thread_interrupter);
31
32namespace {
33#if defined(USE_SIGNAL_HANDLER_TRAMPOLINE)
34extern "C" {
35#endif
36void ThreadInterruptSignalHandler(int signal, siginfo_t* info, void* context_) {
37 if (signal != SIGPROF) {
38 return;
39 }
40 Thread* thread = Thread::Current();
41 if (thread == nullptr) {
42 return;
43 }
44 ThreadInterruptScope signal_handler_scope;
45 // Extract thread state.
46 ucontext_t* context = reinterpret_cast<ucontext_t*>(context_);
47 mcontext_t mcontext = context->uc_mcontext;
48 InterruptedThreadState its;
49 its.pc = SignalHandler::GetProgramCounter(mcontext);
50 its.fp = SignalHandler::GetFramePointer(mcontext);
51 its.csp = SignalHandler::GetCStackPointer(mcontext);
52 its.dsp = SignalHandler::GetDartStackPointer(mcontext);
53 its.lr = SignalHandler::GetLinkRegister(mcontext);
54 Profiler::SampleThread(thread, its);
55}
56#if defined(USE_SIGNAL_HANDLER_TRAMPOLINE)
57} // extern "C"
58#endif
59} // namespace
60
61void ThreadInterrupter::InterruptThread(OSThread* thread) {
62 if (FLAG_trace_thread_interrupter) {
63 OS::PrintErr("ThreadInterrupter interrupting %p\n",
64 reinterpret_cast<void*>(thread->id()));
65 }
66 int result = syscall(__NR_tgkill, getpid(), thread->id(), SIGPROF);
67 ASSERT((result == 0) || (result == ESRCH));
68}
69
70#if defined(USE_SIGNAL_HANDLER_TRAMPOLINE)
71// Defined in thread_interrupted_android_arm.S
72extern "C" void ThreadInterruptSignalHandlerTrampoline(int signal,
73 siginfo_t* info,
74 void* context_);
75#endif
76
77void ThreadInterrupter::InstallSignalHandler() {
78#if defined(USE_SIGNAL_HANDLER_TRAMPOLINE)
79 SignalHandler::Install(&ThreadInterruptSignalHandlerTrampoline);
80#else
81 SignalHandler::Install(&ThreadInterruptSignalHandler);
82#endif
83}
84
85void ThreadInterrupter::RemoveSignalHandler() {
87}
88
90 return SignalHandler::PrepareCurrentThread();
91}
92
94 SignalHandler::CleanupCurrentThreadState(state);
95}
96
97#endif // !PRODUCT
98
99} // namespace dart
100
101#endif // defined(DART_HOST_OS_ANDROID)
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
static void static void PrintErr(const char *format,...) PRINTF_ATTRIBUTE(1
static void SampleThread(Thread *thread, const InterruptedThreadState &state)
Definition: profiler.cc:1354
static void Remove()
static uintptr_t GetDartStackPointer(const mcontext_t &mcontext)
static uintptr_t GetCStackPointer(const mcontext_t &mcontext)
static uintptr_t GetFramePointer(const mcontext_t &mcontext)
static uintptr_t GetProgramCounter(const mcontext_t &mcontext)
static void Install(SignalAction action)
static uintptr_t GetLinkRegister(const mcontext_t &mcontext)
static void CleanupCurrentThreadState(void *state)
static void * PrepareCurrentThread()
static void InterruptThread(OSThread *thread)
static Thread * Current()
Definition: thread.h:362
#define ASSERT(E)
AtkStateType state
GAsyncResult * result
Definition: dart_vm.cc:33
DECLARE_FLAG(bool, show_invisible_frames)