blob: f94a10442e312b77e3db5f64c7301ef66e84411b [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
Adrian McCarthy3ae74922015-06-01 21:51:50 +000010#include "lldb/Core/Log.h"
11#include "lldb/Core/Logging.h"
12#include "lldb/Core/State.h"
Zachary Turner17f383d2014-11-20 22:47:32 +000013#include "lldb/Host/HostInfo.h"
Zachary Turner119767d2014-11-17 17:46:43 +000014#include "lldb/Host/HostNativeThreadBase.h"
15#include "lldb/Host/windows/HostThreadWindows.h"
16#include "lldb/Host/windows/windows.h"
Zachary Turner17f383d2014-11-20 22:47:32 +000017#include "lldb/Target/RegisterContext.h"
18
19#include "TargetThreadWindows.h"
20#include "ProcessWindows.h"
Adrian McCarthy3ae74922015-06-01 21:51:50 +000021#include "ProcessWindowsLog.h"
Zachary Turner17f383d2014-11-20 22:47:32 +000022#include "UnwindLLDB.h"
Zachary Turner119767d2014-11-17 17:46:43 +000023
Zachary Turner7cc34942015-04-27 22:58:57 +000024#if defined(_WIN64)
25#include "x64/RegisterContextWindows_x64.h"
26#else
Zachary Turner7ae4b6d2014-12-18 18:21:33 +000027#include "x86/RegisterContextWindows_x86.h"
28#endif
29
Zachary Turner119767d2014-11-17 17:46:43 +000030using namespace lldb;
31using namespace lldb_private;
32
33TargetThreadWindows::TargetThreadWindows(ProcessWindows &process, const HostThread &thread)
Zachary Turnerc3018992014-11-17 22:42:57 +000034 : Thread(process, thread.GetNativeThread().GetThreadId())
Zachary Turner119767d2014-11-17 17:46:43 +000035 , m_host_thread(thread)
36{
37}
38
39TargetThreadWindows::~TargetThreadWindows()
40{
41 DestroyThread();
42}
43
44void
45TargetThreadWindows::RefreshStateAfterStop()
46{
Zachary Turner48b475c2015-04-02 20:57:38 +000047 ::SuspendThread(m_host_thread.GetNativeThread().GetSystemHandle());
Adrian McCarthy3ae74922015-06-01 21:51:50 +000048 SetState(eStateStopped);
Zachary Turner17f383d2014-11-20 22:47:32 +000049 GetRegisterContext()->InvalidateIfNeeded(false);
Zachary Turner119767d2014-11-17 17:46:43 +000050}
51
52void
53TargetThreadWindows::WillResume(lldb::StateType resume_state)
54{
Adrian McCarthy3ae74922015-06-01 21:51:50 +000055 Log *log(lldb_private::GetLogIfAllCategoriesSet (WINDOWS_LOG_THREAD));
56 if (log)
57 log->Printf ("TargetThreadWindows::WillResume (tid = %" PRIi64 ") setting thread resume state to %s",
58 GetID(), StateAsCString(resume_state));
Zachary Turner119767d2014-11-17 17:46:43 +000059}
60
61void
62TargetThreadWindows::DidStop()
63{
64}
65
66RegisterContextSP
67TargetThreadWindows::GetRegisterContext()
68{
Zachary Turner17f383d2014-11-20 22:47:32 +000069 if (!m_reg_context_sp)
70 m_reg_context_sp = CreateRegisterContextForFrameIndex(0);
71
72 return m_reg_context_sp;
Zachary Turner119767d2014-11-17 17:46:43 +000073}
74
75RegisterContextSP
76TargetThreadWindows::CreateRegisterContextForFrame(StackFrame *frame)
77{
Zachary Turner17f383d2014-11-20 22:47:32 +000078 return CreateRegisterContextForFrameIndex(frame->GetConcreteFrameIndex());
79}
80
81RegisterContextSP
82TargetThreadWindows::CreateRegisterContextForFrameIndex(uint32_t idx)
83{
84 if (!m_reg_context_sp)
85 {
86 ArchSpec arch = HostInfo::GetArchitecture();
87 switch (arch.GetMachine())
88 {
89 case llvm::Triple::x86:
Zachary Turner7ae4b6d2014-12-18 18:21:33 +000090#if defined(_WIN64)
91 // FIXME: This is a Wow64 process, create a RegisterContextWindows_Wow64
92#else
Zachary Turner17f383d2014-11-20 22:47:32 +000093 m_reg_context_sp.reset(new RegisterContextWindows_x86(*this, idx));
Zachary Turner7ae4b6d2014-12-18 18:21:33 +000094#endif
Zachary Turner17f383d2014-11-20 22:47:32 +000095 break;
Zachary Turner7ae4b6d2014-12-18 18:21:33 +000096 case llvm::Triple::x86_64:
97#if defined(_WIN64)
Zachary Turner7cc34942015-04-27 22:58:57 +000098 m_reg_context_sp.reset(new RegisterContextWindows_x64(*this, idx));
Zachary Turner7ae4b6d2014-12-18 18:21:33 +000099#else
100 // LLDB is 32-bit, but the target process is 64-bit. We probably can't debug this.
101#endif
Zachary Turner17f383d2014-11-20 22:47:32 +0000102 default:
Zachary Turner17f383d2014-11-20 22:47:32 +0000103 break;
104 }
105 }
106 return m_reg_context_sp;
Zachary Turner119767d2014-11-17 17:46:43 +0000107}
108
109bool
110TargetThreadWindows::CalculateStopInfo()
111{
Zachary Turner17f383d2014-11-20 22:47:32 +0000112 SetStopInfo(m_stop_info_sp);
113 return true;
114}
115
116Unwind *
117TargetThreadWindows::GetUnwinder()
118{
119 // FIXME: Implement an unwinder based on the Windows unwinder exposed through DIA SDK.
120 if (m_unwinder_ap.get() == NULL)
121 m_unwinder_ap.reset(new UnwindLLDB(*this));
122 return m_unwinder_ap.get();
Zachary Turner119767d2014-11-17 17:46:43 +0000123}
124
125bool
126TargetThreadWindows::DoResume()
127{
128 StateType resume_state = GetResumeState();
129 StateType current_state = GetState();
130 if (resume_state == current_state)
131 return true;
132
Zachary Turnerf194c502015-01-15 22:54:08 +0000133 if (resume_state == eStateStepping)
Zachary Turner119767d2014-11-17 17:46:43 +0000134 {
Zachary Turnerf194c502015-01-15 22:54:08 +0000135 uint32_t flags_index = GetRegisterContext()->ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS);
136 uint64_t flags_value = GetRegisterContext()->ReadRegisterAsUnsigned(flags_index, 0);
137 flags_value |= 0x100; // Set the trap flag on the CPU
138 GetRegisterContext()->WriteRegisterFromUnsigned(flags_index, flags_value);
Zachary Turner119767d2014-11-17 17:46:43 +0000139 }
Zachary Turnerf194c502015-01-15 22:54:08 +0000140
141 if (resume_state == eStateStepping || resume_state == eStateRunning)
142 {
143 DWORD previous_suspend_count = 0;
144 HANDLE thread_handle = m_host_thread.GetNativeThread().GetSystemHandle();
145 do
146 {
147 previous_suspend_count = ::ResumeThread(thread_handle);
148 } while (previous_suspend_count > 0);
149 }
150 return true;
Zachary Turner119767d2014-11-17 17:46:43 +0000151}