summaryrefslogtreecommitdiffstats
path: root/lldb/source/Plugins/Process/Windows/TargetThreadWindows.cpp
blob: 6cc60ee04a3ebdac1d3c82844b54367829d0f613 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//===-- TargetThreadWindows.cpp----------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/Host/HostInfo.h"
#include "lldb/Host/HostNativeThreadBase.h"
#include "lldb/Host/windows/HostThreadWindows.h"
#include "lldb/Host/windows/windows.h"
#include "lldb/Target/RegisterContext.h"

#include "TargetThreadWindows.h"
#include "ProcessWindows.h"
#include "UnwindLLDB.h"

#if !defined(_WIN64)
#include "x86/RegisterContextWindows_x86.h"
#endif

using namespace lldb;
using namespace lldb_private;

TargetThreadWindows::TargetThreadWindows(ProcessWindows &process, const HostThread &thread)
    : Thread(process, thread.GetNativeThread().GetThreadId())
    , m_host_thread(thread)
{
}

TargetThreadWindows::~TargetThreadWindows()
{
    DestroyThread();
}

void
TargetThreadWindows::RefreshStateAfterStop()
{
    ::SuspendThread(m_host_thread.GetNativeThread().GetSystemHandle());

    GetRegisterContext()->InvalidateIfNeeded(false);
}

void
TargetThreadWindows::WillResume(lldb::StateType resume_state)
{
    SetResumeState(resume_state);
}

void
TargetThreadWindows::DidStop()
{
}

RegisterContextSP
TargetThreadWindows::GetRegisterContext()
{
    if (!m_reg_context_sp)
        m_reg_context_sp = CreateRegisterContextForFrameIndex(0);

    return m_reg_context_sp;
}

RegisterContextSP
TargetThreadWindows::CreateRegisterContextForFrame(StackFrame *frame)
{
    return CreateRegisterContextForFrameIndex(frame->GetConcreteFrameIndex());
}

RegisterContextSP
TargetThreadWindows::CreateRegisterContextForFrameIndex(uint32_t idx)
{
    if (!m_reg_context_sp)
    {
        ArchSpec arch = HostInfo::GetArchitecture();
        switch (arch.GetMachine())
        {
            case llvm::Triple::x86:
#if defined(_WIN64)
                // FIXME: This is a Wow64 process, create a RegisterContextWindows_Wow64
#else
                m_reg_context_sp.reset(new RegisterContextWindows_x86(*this, idx));
#endif
                break;
            case llvm::Triple::x86_64:
#if defined(_WIN64)
                // FIXME: This is a 64-bit process, create a RegisterContextWindows_x86_64
#else
                // LLDB is 32-bit, but the target process is 64-bit.  We probably can't debug this.
#endif
            default:
                break;
        }
    }
    return m_reg_context_sp;
}

bool
TargetThreadWindows::CalculateStopInfo()
{
    SetStopInfo(m_stop_info_sp);
    return true;
}

Unwind *
TargetThreadWindows::GetUnwinder()
{
    // FIXME: Implement an unwinder based on the Windows unwinder exposed through DIA SDK.
    if (m_unwinder_ap.get() == NULL)
        m_unwinder_ap.reset(new UnwindLLDB(*this));
    return m_unwinder_ap.get();
}

bool
TargetThreadWindows::DoResume()
{
    StateType resume_state = GetResumeState();
    StateType current_state = GetState();
    if (resume_state == current_state)
        return true;

    if (resume_state == eStateStepping)
    {
        uint32_t flags_index = GetRegisterContext()->ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS);
        uint64_t flags_value = GetRegisterContext()->ReadRegisterAsUnsigned(flags_index, 0);
        flags_value |= 0x100; // Set the trap flag on the CPU
        GetRegisterContext()->WriteRegisterFromUnsigned(flags_index, flags_value);
    }

    if (resume_state == eStateStepping || resume_state == eStateRunning)
    {
        DWORD previous_suspend_count = 0;
        HANDLE thread_handle = m_host_thread.GetNativeThread().GetSystemHandle();
        do
        {
            previous_suspend_count = ::ResumeThread(thread_handle);
        } while (previous_suspend_count > 0);
    }
    return true;
}
OpenPOWER on IntegriCloud