blob: 1c33f218084d5872dc6f904ed6d322fb4e744d1d [file] [log] [blame]
Zachary Turner119767d2014-11-17 17:46:43 +00001//===-- TargetThreadWindows.cpp----------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Zachary Turner17f383d2014-11-20 22:47:32 +000010#include "lldb/Host/HostInfo.h"
Zachary Turner119767d2014-11-17 17:46:43 +000011#include "lldb/Host/HostNativeThreadBase.h"
12#include "lldb/Host/windows/HostThreadWindows.h"
13#include "lldb/Host/windows/windows.h"
Zachary Turner17f383d2014-11-20 22:47:32 +000014#include "lldb/Target/RegisterContext.h"
15
16#include "TargetThreadWindows.h"
17#include "ProcessWindows.h"
18#include "RegisterContextWindows_x86.h"
19#include "UnwindLLDB.h"
Zachary Turner119767d2014-11-17 17:46:43 +000020
21using namespace lldb;
22using namespace lldb_private;
23
24TargetThreadWindows::TargetThreadWindows(ProcessWindows &process, const HostThread &thread)
Zachary Turnerc3018992014-11-17 22:42:57 +000025 : Thread(process, thread.GetNativeThread().GetThreadId())
Zachary Turner119767d2014-11-17 17:46:43 +000026 , m_host_thread(thread)
27{
28}
29
30TargetThreadWindows::~TargetThreadWindows()
31{
32 DestroyThread();
33}
34
35void
36TargetThreadWindows::RefreshStateAfterStop()
37{
Zachary Turner17f383d2014-11-20 22:47:32 +000038 GetRegisterContext()->InvalidateIfNeeded(false);
Zachary Turner119767d2014-11-17 17:46:43 +000039}
40
41void
42TargetThreadWindows::WillResume(lldb::StateType resume_state)
43{
44}
45
46void
47TargetThreadWindows::DidStop()
48{
49}
50
51RegisterContextSP
52TargetThreadWindows::GetRegisterContext()
53{
Zachary Turner17f383d2014-11-20 22:47:32 +000054 if (!m_reg_context_sp)
55 m_reg_context_sp = CreateRegisterContextForFrameIndex(0);
56
57 return m_reg_context_sp;
Zachary Turner119767d2014-11-17 17:46:43 +000058}
59
60RegisterContextSP
61TargetThreadWindows::CreateRegisterContextForFrame(StackFrame *frame)
62{
Zachary Turner17f383d2014-11-20 22:47:32 +000063 return CreateRegisterContextForFrameIndex(frame->GetConcreteFrameIndex());
64}
65
66RegisterContextSP
67TargetThreadWindows::CreateRegisterContextForFrameIndex(uint32_t idx)
68{
69 if (!m_reg_context_sp)
70 {
71 ArchSpec arch = HostInfo::GetArchitecture();
72 switch (arch.GetMachine())
73 {
74 case llvm::Triple::x86:
75 m_reg_context_sp.reset(new RegisterContextWindows_x86(*this, idx));
76 break;
77 default:
78 // FIXME: Support x64 by creating a RegisterContextWindows_x86_64
79 break;
80 }
81 }
82 return m_reg_context_sp;
Zachary Turner119767d2014-11-17 17:46:43 +000083}
84
85bool
86TargetThreadWindows::CalculateStopInfo()
87{
Zachary Turner17f383d2014-11-20 22:47:32 +000088 SetStopInfo(m_stop_info_sp);
89 return true;
90}
91
92Unwind *
93TargetThreadWindows::GetUnwinder()
94{
95 // FIXME: Implement an unwinder based on the Windows unwinder exposed through DIA SDK.
96 if (m_unwinder_ap.get() == NULL)
97 m_unwinder_ap.reset(new UnwindLLDB(*this));
98 return m_unwinder_ap.get();
Zachary Turner119767d2014-11-17 17:46:43 +000099}
100
101bool
102TargetThreadWindows::DoResume()
103{
104 StateType resume_state = GetResumeState();
105 StateType current_state = GetState();
106 if (resume_state == current_state)
107 return true;
108
109 bool success = false;
110 DWORD suspend_count = 0;
111 switch (resume_state)
112 {
113 case eStateRunning:
114 SetState(resume_state);
115 do
116 {
117 suspend_count = ::ResumeThread(m_host_thread.GetNativeThread().GetSystemHandle());
118 } while (suspend_count > 1 && suspend_count != (DWORD)-1);
119 success = (suspend_count != (DWORD)-1);
120 break;
121 case eStateStopped:
122 case eStateSuspended:
123 if (current_state != eStateStopped && current_state != eStateSuspended)
124 {
125 suspend_count = SuspendThread(m_host_thread.GetNativeThread().GetSystemHandle());
126 success = (suspend_count != (DWORD)-1);
127 }
128 break;
129 default:
130 success = false;
131 }
132 return success;
133}