Skip to content

Commit c7b4936

Browse files
committed
[lldb/Plugins] Refactor ScriptedThread register context creation
This patch changes the ScriptedThread class to create the register context when Process::RefreshStateAfterStop is called rather than doing it in the thread constructor. This is required to update the thread state for execution control. Differential Revision: https://reviews.llvm.org/D112167 Signed-off-by: Med Ismail Bennani <[email protected]>
1 parent ff790af commit c7b4936

File tree

3 files changed

+50
-43
lines changed

3 files changed

+50
-43
lines changed

lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "lldb/Interpreter/ScriptInterpreter.h"
2121
#include "lldb/Target/MemoryRegionInfo.h"
2222
#include "lldb/Target/RegisterContext.h"
23-
2423
#include "lldb/Utility/State.h"
2524

2625
#include <mutex>
@@ -300,6 +299,7 @@ bool ScriptedProcess::DoUpdateThreadList(ThreadList &old_thread_list,
300299
// actually new threads will get added to new_thread_list.
301300

302301
CheckInterpreterAndScriptObject();
302+
m_thread_plans.ClearThreadCache();
303303

304304
Status error;
305305
ScriptLanguage language = m_interpreter->GetLanguage();
@@ -325,6 +325,12 @@ bool ScriptedProcess::DoUpdateThreadList(ThreadList &old_thread_list,
325325
return new_thread_list.GetSize(false) > 0;
326326
}
327327

328+
void ScriptedProcess::RefreshStateAfterStop() {
329+
// Let all threads recover from stopping and do any clean up based on the
330+
// previous thread state (if any).
331+
m_thread_list.RefreshStateAfterStop();
332+
}
333+
328334
bool ScriptedProcess::GetProcessInfo(ProcessInstanceInfo &info) {
329335
info.Clear();
330336
info.SetProcessID(GetID());

lldb/source/Plugins/Process/scripted/ScriptedProcess.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class ScriptedProcess : public Process {
7777

7878
Status DoDestroy() override;
7979

80-
void RefreshStateAfterStop() override{};
80+
void RefreshStateAfterStop() override;
8181

8282
bool IsAlive() override;
8383

lldb/source/Plugins/Process/scripted/ScriptedThread.cpp

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -64,32 +64,6 @@ ScriptedThread::ScriptedThread(ScriptedProcess &process, Status &error)
6464
m_script_object_sp = object_sp;
6565

6666
SetID(scripted_thread_interface->GetThreadID());
67-
68-
llvm::Optional<std::string> reg_data =
69-
scripted_thread_interface->GetRegisterContext();
70-
if (!reg_data) {
71-
error.SetErrorString("Failed to get scripted thread registers data.");
72-
return;
73-
}
74-
75-
DataBufferSP data_sp(
76-
std::make_shared<DataBufferHeap>(reg_data->c_str(), reg_data->size()));
77-
78-
if (!data_sp->GetByteSize()) {
79-
error.SetErrorString("Failed to copy raw registers data.");
80-
return;
81-
}
82-
83-
std::shared_ptr<RegisterContextMemory> reg_ctx_memory =
84-
std::make_shared<RegisterContextMemory>(
85-
*this, 0, *GetDynamicRegisterInfo(), LLDB_INVALID_ADDRESS);
86-
if (!reg_ctx_memory) {
87-
error.SetErrorString("Failed to create a register context.");
88-
return;
89-
}
90-
91-
reg_ctx_memory->SetAllRegisterData(data_sp);
92-
m_reg_context_sp = reg_ctx_memory;
9367
}
9468

9569
ScriptedThread::~ScriptedThread() { DestroyThread(); }
@@ -115,21 +89,48 @@ void ScriptedThread::WillResume(StateType resume_state) {}
11589
void ScriptedThread::ClearStackFrames() { Thread::ClearStackFrames(); }
11690

11791
RegisterContextSP ScriptedThread::GetRegisterContext() {
118-
if (!m_reg_context_sp) {
119-
m_reg_context_sp = std::make_shared<RegisterContextThreadMemory>(
120-
*this, LLDB_INVALID_ADDRESS);
121-
GetInterface()->GetRegisterContext();
122-
}
92+
if (!m_reg_context_sp)
93+
m_reg_context_sp = CreateRegisterContextForFrame(nullptr);
12394
return m_reg_context_sp;
12495
}
12596

12697
RegisterContextSP
12798
ScriptedThread::CreateRegisterContextForFrame(StackFrame *frame) {
128-
uint32_t concrete_frame_idx = frame ? frame->GetConcreteFrameIndex() : 0;
99+
const uint32_t concrete_frame_idx =
100+
frame ? frame->GetConcreteFrameIndex() : 0;
101+
102+
if (concrete_frame_idx)
103+
return GetUnwinder().CreateRegisterContextForFrame(frame);
104+
105+
lldb::RegisterContextSP reg_ctx_sp;
106+
Status error;
107+
108+
llvm::Optional<std::string> reg_data = GetInterface()->GetRegisterContext();
109+
if (!reg_data)
110+
return GetInterface()->ErrorWithMessage<lldb::RegisterContextSP>(
111+
LLVM_PRETTY_FUNCTION, "Failed to get scripted thread registers data.",
112+
error, LIBLLDB_LOG_THREAD);
113+
114+
DataBufferSP data_sp(
115+
std::make_shared<DataBufferHeap>(reg_data->c_str(), reg_data->size()));
116+
117+
if (!data_sp->GetByteSize())
118+
return GetInterface()->ErrorWithMessage<lldb::RegisterContextSP>(
119+
LLVM_PRETTY_FUNCTION, "Failed to copy raw registers data.", error,
120+
LIBLLDB_LOG_THREAD);
129121

130-
if (concrete_frame_idx == 0)
131-
return GetRegisterContext();
132-
return GetUnwinder().CreateRegisterContextForFrame(frame);
122+
std::shared_ptr<RegisterContextMemory> reg_ctx_memory =
123+
std::make_shared<RegisterContextMemory>(
124+
*this, 0, *GetDynamicRegisterInfo(), LLDB_INVALID_ADDRESS);
125+
if (!reg_ctx_memory)
126+
return GetInterface()->ErrorWithMessage<lldb::RegisterContextSP>(
127+
LLVM_PRETTY_FUNCTION, "Failed to create a register context.", error,
128+
LIBLLDB_LOG_THREAD);
129+
130+
reg_ctx_memory->SetAllRegisterData(data_sp);
131+
m_reg_context_sp = reg_ctx_memory;
132+
133+
return m_reg_context_sp;
133134
}
134135

135136
bool ScriptedThread::CalculateStopInfo() {
@@ -142,13 +143,15 @@ bool ScriptedThread::CalculateStopInfo() {
142143
if (!dict_sp->GetValueForKeyAsInteger("type", stop_reason_type))
143144
return GetInterface()->ErrorWithMessage<bool>(
144145
LLVM_PRETTY_FUNCTION,
145-
"Couldn't find value for key 'type' in stop reason dictionary.", error);
146+
"Couldn't find value for key 'type' in stop reason dictionary.", error,
147+
LIBLLDB_LOG_THREAD);
146148

147149
StructuredData::Dictionary *data_dict;
148150
if (!dict_sp->GetValueForKeyAsDictionary("data", data_dict))
149151
return GetInterface()->ErrorWithMessage<bool>(
150152
LLVM_PRETTY_FUNCTION,
151-
"Couldn't find value for key 'type' in stop reason dictionary.", error);
153+
"Couldn't find value for key 'type' in stop reason dictionary.", error,
154+
LIBLLDB_LOG_THREAD);
152155

153156
switch (stop_reason_type) {
154157
case lldb::eStopReasonNone:
@@ -175,17 +178,15 @@ bool ScriptedThread::CalculateStopInfo() {
175178
llvm::Twine("Unsupported stop reason type (" +
176179
llvm::Twine(stop_reason_type) + llvm::Twine(")."))
177180
.str(),
178-
error);
181+
error, LIBLLDB_LOG_THREAD);
179182
}
180183

181184
SetStopInfo(stop_info_sp);
182185
return true;
183186
}
184187

185188
void ScriptedThread::RefreshStateAfterStop() {
186-
// TODO: Implement
187-
if (m_reg_context_sp)
188-
m_reg_context_sp->InvalidateAllRegisters();
189+
GetRegisterContext()->InvalidateIfNeeded(/*force=*/false);
189190
}
190191

191192
lldb::ScriptedThreadInterfaceSP ScriptedThread::GetInterface() const {

0 commit comments

Comments
 (0)