| 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_WINDOWS) | 6 #if defined(TARGET_OS_WINDOWS) |
| 7 | 7 |
| 8 #include "vm/thread_interrupter.h" | 8 #include "vm/thread_interrupter.h" |
| 9 | 9 |
| 10 namespace dart { | 10 namespace dart { |
| 11 | 11 |
| 12 DECLARE_FLAG(bool, thread_interrupter); | 12 DECLARE_FLAG(bool, thread_interrupter); |
| 13 DECLARE_FLAG(bool, trace_thread_interrupter); | 13 DECLARE_FLAG(bool, trace_thread_interrupter); |
| 14 | 14 |
| 15 #define kThreadError -1 | 15 #define kThreadError -1 |
| 16 | 16 |
| 17 class ThreadInterrupterWin : public AllStatic { | 17 class ThreadInterrupterWin : public AllStatic { |
| 18 public: | 18 public: |
| 19 static bool GrabRegisters(ThreadId thread, InterruptedThreadState* state) { | 19 static bool GrabRegisters(HANDLE handle, InterruptedThreadState* state) { |
| 20 CONTEXT context; | 20 CONTEXT context; |
| 21 memset(&context, 0, sizeof(context)); | 21 memset(&context, 0, sizeof(context)); |
| 22 context.ContextFlags = CONTEXT_FULL; | 22 context.ContextFlags = CONTEXT_CONTROL; |
| 23 if (GetThreadContext(thread, &context) != 0) { | 23 if (GetThreadContext(handle, &context) != 0) { |
| 24 #if defined(TARGET_ARCH_IA32) | 24 #if defined(TARGET_ARCH_IA32) |
| 25 state->pc = static_cast<uintptr_t>(context.Eip); | 25 state->pc = static_cast<uintptr_t>(context.Eip); |
| 26 state->fp = static_cast<uintptr_t>(context.Ebp); | 26 state->fp = static_cast<uintptr_t>(context.Ebp); |
| 27 state->sp = static_cast<uintptr_t>(context.Esp); | 27 state->sp = static_cast<uintptr_t>(context.Esp); |
| 28 #elif defined(TARGET_ARCH_X64) | 28 #elif defined(TARGET_ARCH_X64) |
| 29 state->pc = reinterpret_cast<uintptr_t>(context.Rip); | 29 state->pc = reinterpret_cast<uintptr_t>(context.Rip); |
| 30 state->fp = reinterpret_cast<uintptr_t>(context.Rbp); | 30 state->fp = reinterpret_cast<uintptr_t>(context.Rbp); |
| 31 state->sp = reinterpret_cast<uintptr_t>(context.Rsp); | 31 state->sp = reinterpret_cast<uintptr_t>(context.Rsp); |
| 32 #else | 32 #else |
| 33 UNIMPLEMENTED(); | 33 UNIMPLEMENTED(); |
| 34 #endif | 34 #endif |
| 35 return true; | 35 return true; |
| 36 } | 36 } |
| 37 return false; | 37 return false; |
| 38 } | 38 } |
| 39 | 39 |
| 40 | 40 |
| 41 static void Interrupt(InterruptableThreadState* state) { | 41 static void Interrupt(InterruptableThreadState* state) { |
| 42 ASSERT(GetCurrentThread() != state->id); | 42 ASSERT(!Thread::Compare(GetCurrentThreadId(), state->id)); |
| 43 DWORD result = SuspendThread(state->id); | 43 HANDLE handle = OpenThread(THREAD_GET_CONTEXT | |
| 44 THREAD_QUERY_INFORMATION | |
| 45 THREAD_SUSPEND_RESUME, |
| 46 false, |
| 47 state->id); |
| 48 ASSERT(handle != NULL); |
| 49 DWORD result = SuspendThread(handle); |
| 44 if (result == kThreadError) { | 50 if (result == kThreadError) { |
| 45 if (FLAG_trace_thread_interrupter) { | 51 if (FLAG_trace_thread_interrupter) { |
| 46 OS::Print("ThreadInterrupted failed to suspend thread %p\n", | 52 OS::Print("ThreadInterrupted failed to suspend thread %p\n", |
| 47 reinterpret_cast<void*>(state->id)); | 53 reinterpret_cast<void*>(state->id)); |
| 48 } | 54 } |
| 55 CloseHandle(handle); |
| 49 return; | 56 return; |
| 50 } | 57 } |
| 51 InterruptedThreadState its; | 58 InterruptedThreadState its; |
| 52 its.tid = state->id; | 59 its.tid = state->id; |
| 53 if (!GrabRegisters(state->id, &its)) { | 60 if (!GrabRegisters(handle, &its)) { |
| 54 // Failed to get thread registers. | 61 // Failed to get thread registers. |
| 55 ResumeThread(state->id); | 62 ResumeThread(handle); |
| 56 if (FLAG_trace_thread_interrupter) { | 63 if (FLAG_trace_thread_interrupter) { |
| 57 OS::Print("ThreadInterrupted failed to get registers for %p\n", | 64 OS::Print("ThreadInterrupted failed to get registers for %p\n", |
| 58 reinterpret_cast<void*>(state->id)); | 65 reinterpret_cast<void*>(state->id)); |
| 59 } | 66 } |
| 67 CloseHandle(handle); |
| 60 return; | 68 return; |
| 61 } | 69 } |
| 62 if (state->callback == NULL) { | 70 if (state->callback == NULL) { |
| 63 // No callback registered. | 71 // No callback registered. |
| 64 ResumeThread(state->id); | 72 ResumeThread(handle); |
| 73 CloseHandle(handle); |
| 65 return; | 74 return; |
| 66 } | 75 } |
| 67 state->callback(its, state->data); | 76 state->callback(its, state->data); |
| 68 ResumeThread(state->id); | 77 ResumeThread(handle); |
| 78 CloseHandle(handle); |
| 69 } | 79 } |
| 70 }; | 80 }; |
| 71 | 81 |
| 72 | 82 |
| 73 void ThreadInterrupter::InterruptThread(InterruptableThreadState* state) { | 83 void ThreadInterrupter::InterruptThread(InterruptableThreadState* state) { |
| 74 if (FLAG_trace_thread_interrupter) { | 84 if (FLAG_trace_thread_interrupter) { |
| 75 OS::Print("ThreadInterrupter suspending %p\n", | 85 OS::Print("ThreadInterrupter suspending %p\n", |
| 76 reinterpret_cast<void*>(state->id)); | 86 reinterpret_cast<void*>(state->id)); |
| 77 } | 87 } |
| 78 ThreadInterrupterWin::Interrupt(state); | 88 ThreadInterrupterWin::Interrupt(state); |
| 79 if (FLAG_trace_thread_interrupter) { | 89 if (FLAG_trace_thread_interrupter) { |
| 80 OS::Print("ThreadInterrupter resuming %p\n", | 90 OS::Print("ThreadInterrupter resuming %p\n", |
| 81 reinterpret_cast<void*>(state->id)); | 91 reinterpret_cast<void*>(state->id)); |
| 82 } | 92 } |
| 83 } | 93 } |
| 84 | 94 |
| 85 | 95 |
| 86 void ThreadInterrupter::InstallSignalHandler() { | 96 void ThreadInterrupter::InstallSignalHandler() { |
| 87 // Nothing to do on Windows. | 97 // Nothing to do on Windows. |
| 88 } | 98 } |
| 89 | 99 |
| 90 | 100 |
| 91 } // namespace dart | 101 } // namespace dart |
| 92 | 102 |
| 93 #endif // defined(TARGET_OS_WINDOWS) | 103 #endif // defined(TARGET_OS_WINDOWS) |
| 94 | 104 |
| OLD | NEW |