LLDB mainline
ScriptedThread.cpp
Go to the documentation of this file.
1//===-- ScriptedThread.cpp ------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "ScriptedThread.h"
10#include "ScriptedFrame.h"
11
15#include "lldb/Target/Process.h"
18#include "lldb/Target/Unwind.h"
21#include <memory>
22#include <optional>
23
24using namespace lldb;
25using namespace lldb_private;
26
28 lldbassert(m_script_object_sp && "Invalid Script Object.");
29 lldbassert(GetInterface() && "Invalid Scripted Thread Interface.");
30}
31
32llvm::Expected<std::shared_ptr<ScriptedThread>>
34 StructuredData::Generic *script_object) {
35 if (!process.IsValid())
36 return llvm::createStringError(llvm::inconvertibleErrorCode(),
37 "Invalid scripted process.");
38
39 process.CheckScriptedInterface();
40
41 auto scripted_thread_interface =
43 if (!scripted_thread_interface)
44 return llvm::createStringError(
45 llvm::inconvertibleErrorCode(),
46 "Failed to create scripted thread interface.");
47
48 llvm::StringRef thread_class_name;
49 if (!script_object) {
50 std::optional<std::string> class_name =
52 if (!class_name || class_name->empty())
53 return llvm::createStringError(
54 llvm::inconvertibleErrorCode(),
55 "Failed to get scripted thread class name.");
56 thread_class_name = *class_name;
57 }
58
59 ExecutionContext exe_ctx(process);
60 // The legacy thread-spawn path (no script_object) needs to instantiate a
61 // *thread* Python class whose name comes from the process plugin, not the
62 // process's own class name. Build a thread-specific metadata for that case;
63 // when script_object is non-null the class name is unused so we just forward
64 // the process's metadata.
65 ScriptedMetadata thread_metadata =
66 script_object ? process.m_scripted_metadata
67 : ScriptedMetadata(thread_class_name,
69 auto obj_or_err = scripted_thread_interface->CreatePluginObject(
70 thread_metadata, exe_ctx, script_object);
71
72 if (!obj_or_err) {
73 llvm::consumeError(obj_or_err.takeError());
74 return llvm::createStringError(llvm::inconvertibleErrorCode(),
75 "Failed to create script object.");
76 }
77
78 StructuredData::GenericSP owned_script_object_sp = *obj_or_err;
79
80 if (!owned_script_object_sp->IsValid())
81 return llvm::createStringError(llvm::inconvertibleErrorCode(),
82 "Created script object is invalid.");
83
84 lldb::tid_t tid = scripted_thread_interface->GetThreadID();
85
86 return std::make_shared<ScriptedThread>(process, scripted_thread_interface,
87 tid, owned_script_object_sp);
88}
89
91 ScriptedThreadInterfaceSP interface_sp,
92 lldb::tid_t tid,
93 StructuredData::GenericSP script_object_sp)
94 : Thread(process, tid), m_scripted_process(process),
96 m_script_object_sp(script_object_sp) {}
97
99
102 std::optional<std::string> thread_name = GetInterface()->GetName();
103 if (!thread_name)
104 return nullptr;
105 return ConstString(*thread_name).AsCString(nullptr);
106}
107
110 std::optional<std::string> queue_name = GetInterface()->GetQueue();
111 if (!queue_name)
112 return nullptr;
113 return ConstString(*queue_name).AsCString(nullptr);
114}
115
117
119
125
128 const uint32_t concrete_frame_idx =
129 frame ? frame->GetConcreteFrameIndex() : 0;
130
131 if (concrete_frame_idx)
133
134 lldb::RegisterContextSP reg_ctx_sp;
136
137 std::optional<std::string> reg_data = GetInterface()->GetRegisterContext();
138 if (!reg_data)
140 LLVM_PRETTY_FUNCTION, "Failed to get scripted thread registers data.",
142
143 DataBufferSP data_sp(
144 std::make_shared<DataBufferHeap>(reg_data->c_str(), reg_data->size()));
145
146 if (!data_sp->GetByteSize())
148 LLVM_PRETTY_FUNCTION, "Failed to copy raw registers data.", error,
150
151 std::shared_ptr<RegisterContextMemory> reg_ctx_memory =
152 std::make_shared<RegisterContextMemory>(
154 if (!reg_ctx_memory)
156 LLVM_PRETTY_FUNCTION, "Failed to create a register context.", error,
158
159 reg_ctx_memory->SetAllRegisterData(data_sp);
160 m_reg_context_sp = reg_ctx_memory;
161
162 return m_reg_context_sp;
163}
164
166 StructuredData::ArraySP arr_sp = GetInterface()->GetStackFrames();
167
169 if (!arr_sp)
171 LLVM_PRETTY_FUNCTION, "Failed to get scripted thread stackframes.",
173
174 size_t arr_size = arr_sp->GetSize();
175 if (!arr_size)
177 LLVM_PRETTY_FUNCTION, "StackFrame array is empty.", error,
179
180 if (arr_size > std::numeric_limits<uint32_t>::max())
182 LLVM_PRETTY_FUNCTION,
183 llvm::Twine(
184 "StackFrame array size (" + llvm::Twine(arr_size) +
185 ") is greater than maximum authorized for a StackFrameList.")
186 .str(),
188
189 auto create_frame_from_dict =
190 [this, arr_sp](size_t idx,
191 uint32_t frame_list_idx) -> llvm::Expected<StackFrameSP> {
193 std::optional<StructuredData::Dictionary *> maybe_dict =
194 arr_sp->GetItemAtIndexAsDictionary(idx);
195 if (!maybe_dict) {
197 LLVM_PRETTY_FUNCTION,
198 llvm::Twine(
199 "Couldn't get artificial stackframe dictionary at index (" +
200 llvm::Twine(idx) + llvm::Twine(") from stackframe array."))
201 .str(),
203 return error.ToError();
204 }
205 StructuredData::Dictionary *dict = *maybe_dict;
206
208 if (!dict->GetValueForKeyAsInteger("pc", pc)) {
210 LLVM_PRETTY_FUNCTION,
211 "Couldn't find value for key 'pc' in stackframe dictionary.", error,
213 return error.ToError();
214 }
215
216 Address symbol_addr;
217 symbol_addr.SetLoadAddress(pc, &this->GetProcess()->GetTarget());
218
220 bool cfa_is_valid = false;
221 const bool artificial = false;
222 const bool behaves_like_zeroth_frame = (frame_list_idx == 0);
223 SymbolContext sc;
224 symbol_addr.CalculateSymbolContext(&sc);
225
226 return std::make_shared<StackFrame>(shared_from_this(), frame_list_idx, idx,
227 cfa, cfa_is_valid, pc,
228 StackFrame::Kind::Synthetic, artificial,
229 behaves_like_zeroth_frame, &sc);
230 };
231
232 auto create_frame_from_script_object =
233 [this, arr_sp](size_t idx) -> llvm::Expected<StackFrameSP> {
235 StructuredData::ObjectSP object_sp = arr_sp->GetItemAtIndex(idx);
236 if (!object_sp || !object_sp->GetAsGeneric()) {
238 LLVM_PRETTY_FUNCTION,
239 llvm::Twine("Couldn't get artificial stackframe object at index (" +
240 llvm::Twine(idx) +
241 llvm::Twine(") from stackframe array."))
242 .str(),
244 return error.ToError();
245 }
246
247 auto frame_or_error = ScriptedFrame::Create(
248 shared_from_this(), GetInterface(), nullptr, object_sp->GetAsGeneric());
249
250 if (!frame_or_error) {
252 LLVM_PRETTY_FUNCTION, toString(frame_or_error.takeError()), error);
253 return error.ToError();
254 }
255
256 StackFrameSP frame_sp = frame_or_error.get();
257 lldbassert(frame_sp && "Couldn't initialize scripted frame.");
258
259 return frame_sp;
260 };
261
263 uint32_t frame_list_idx = 0;
264
265 for (size_t idx = 0; idx < arr_size; idx++) {
266 StackFrameSP synth_frame_sp = nullptr;
267
268 auto frame_from_dict_or_err = create_frame_from_dict(idx, frame_list_idx);
269 if (!frame_from_dict_or_err) {
270 auto frame_from_script_obj_or_err = create_frame_from_script_object(idx);
271
272 if (!frame_from_script_obj_or_err) {
274 LLVM_PRETTY_FUNCTION,
275 llvm::Twine(
276 "Couldn't add artificial frame (" + llvm::Twine(idx) +
277 llvm::Twine(") to ScriptedThread StackFrameList: ") +
278 llvm::toString(frame_from_script_obj_or_err.takeError()))
279 .str(),
281 } else {
282 llvm::consumeError(frame_from_dict_or_err.takeError());
283 synth_frame_sp = *frame_from_script_obj_or_err;
284 }
285 } else {
286 synth_frame_sp = *frame_from_dict_or_err;
287 }
288
289 if (!frames->SetFrameAtIndex(frame_list_idx, synth_frame_sp))
291 LLVM_PRETTY_FUNCTION,
292 llvm::Twine("Couldn't add frame (" + llvm::Twine(idx) +
293 llvm::Twine(") to ScriptedThread StackFrameList."))
294 .str(),
296 frame_list_idx++;
297
298 // Synthesize inline frames, mirroring StackFrameList::FetchFramesUpTo().
299 frame_list_idx += frames->SynthesizeInlineFrames(
300 synth_frame_sp, /*cfa=*/LLDB_INVALID_ADDRESS);
301 }
302
303 // Mark the stack as fully unwound so the regular unwinder doesn't try to
304 // extend it beyond the artificial frames (e.g. by reading lr/fp from the
305 // register context).
306 frames->SetAllFramesFetched();
307
308 return true;
309}
310
312 StructuredData::DictionarySP dict_sp = GetInterface()->GetStopReason();
313
315 if (!dict_sp)
317 LLVM_PRETTY_FUNCTION, "Failed to get scripted thread stop info.", error,
319
320 // If we're at a BreakpointSite, mark that we stopped there and
321 // need to hit the breakpoint when we resume. This will be cleared
322 // if we CreateStopReasonWithBreakpointSiteID.
323 if (RegisterContextSP reg_ctx_sp = GetRegisterContext()) {
324 addr_t pc = reg_ctx_sp->GetPC();
325 ProcessSP proc = GetProcess();
326 if (BreakpointSiteSP bp_site_sp =
327 proc->GetBreakpointSiteList().FindByAddress(pc))
328 if (proc->IsBreakpointSitePhysicallyEnabled(*bp_site_sp))
330 }
331
332 lldb::StopInfoSP stop_info_sp;
333 lldb::StopReason stop_reason_type;
334
335 if (!dict_sp->GetValueForKeyAsInteger("type", stop_reason_type))
337 LLVM_PRETTY_FUNCTION,
338 "Couldn't find value for key 'type' in stop reason dictionary.", error,
340
342 if (!dict_sp->GetValueForKeyAsDictionary("data", data_dict))
344 LLVM_PRETTY_FUNCTION,
345 "Couldn't find value for key 'data' in stop reason dictionary.", error,
347
348 switch (stop_reason_type) {
350 return true;
352 lldb::break_id_t break_id;
353 data_dict->GetValueForKeyAsInteger("break_id", break_id,
355 stop_info_sp =
357 } break;
359 uint32_t signal;
360 llvm::StringRef description;
361 if (!data_dict->GetValueForKeyAsInteger("signal", signal)) {
363 return false;
364 }
365 data_dict->GetValueForKeyAsString("desc", description);
366 stop_info_sp =
367 StopInfo::CreateStopReasonWithSignal(*this, signal, description.data());
368 } break;
370 stop_info_sp = StopInfo::CreateStopReasonToTrace(*this);
371 } break;
373#if defined(__APPLE__)
374 StructuredData::Dictionary *mach_exception;
375 if (data_dict->GetValueForKeyAsDictionary("mach_exception",
376 mach_exception)) {
377 llvm::StringRef value;
378 mach_exception->GetValueForKeyAsString("type", value);
379 auto exc_type =
380 StopInfoMachException::MachException::ExceptionCode(value.data());
381
382 if (!exc_type)
383 return false;
384
385 uint32_t exc_data_size = 0;
386 llvm::SmallVector<uint64_t, 3> raw_codes;
387
388 StructuredData::Array *exc_rawcodes;
389 mach_exception->GetValueForKeyAsArray("rawCodes", exc_rawcodes);
390 if (exc_rawcodes) {
391 auto fetch_data = [&raw_codes](StructuredData::Object *obj) {
392 if (!obj)
393 return false;
394 raw_codes.push_back(obj->GetUnsignedIntegerValue());
395 return true;
396 };
397
398 exc_rawcodes->ForEach(fetch_data);
399 exc_data_size = raw_codes.size();
400 }
401
403 *this, *exc_type, exc_data_size,
404 exc_data_size >= 1 ? raw_codes[0] : 0,
405 exc_data_size >= 2 ? raw_codes[1] : 0,
406 exc_data_size >= 3 ? raw_codes[2] : 0);
407
408 break;
409 }
410#endif
411 stop_info_sp =
412 StopInfo::CreateStopReasonWithException(*this, "EXC_BAD_ACCESS");
413 } break;
414 default:
416 LLVM_PRETTY_FUNCTION,
417 llvm::Twine("Unsupported stop reason type (" +
418 llvm::Twine(stop_reason_type) + llvm::Twine(")."))
419 .str(),
421 }
422
423 if (!stop_info_sp)
424 return false;
425
426 SetStopInfo(stop_info_sp);
427 return true;
428}
429
431 GetRegisterContext()->InvalidateIfNeeded(/*force=*/false);
433}
434
438
439std::shared_ptr<DynamicRegisterInfo> ScriptedThread::GetDynamicRegisterInfo() {
441
442 if (!m_register_info_sp) {
443 StructuredData::DictionarySP reg_info = GetInterface()->GetRegisterInfo();
444
446 if (!reg_info)
448 std::shared_ptr<DynamicRegisterInfo>>(
449 LLVM_PRETTY_FUNCTION, "Failed to get scripted thread registers info.",
451
453 *reg_info, m_scripted_process.GetTarget().GetArchitecture());
454 }
455
456 return m_register_info_sp;
457}
458
461
463 StructuredData::ArraySP extended_info_sp = GetInterface()->GetExtendedInfo();
464
465 if (!extended_info_sp || !extended_info_sp->GetSize())
467 LLVM_PRETTY_FUNCTION, "No extended information found", error);
468
469 return extended_info_sp;
470}
static llvm::raw_ostream & error(Stream &strm)
#define lldbassert(x)
Definition LLDBAssert.h:16
A section + offset based address class.
Definition Address.h:62
bool SetLoadAddress(lldb::addr_t load_addr, Target *target, bool allow_section_end=false)
Set the address to represent load_addr.
Definition Address.cpp:1034
uint32_t CalculateSymbolContext(SymbolContext *sc, lldb::SymbolContextItem resolve_scope=lldb::eSymbolContextEverything) const
Reconstruct a symbol context from an address.
Definition Address.cpp:819
A uniqued constant string class.
Definition ConstString.h:40
const char * AsCString(const char *value_if_empty) const
Get the string value as a C string.
static std::unique_ptr< DynamicRegisterInfo > Create(const StructuredData::Dictionary &dict, const ArchSpec &arch)
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
bool IsValid() const
Return whether this object is valid (i.e.
Definition Process.h:575
static llvm::Expected< std::shared_ptr< ScriptedFrame > > Create(lldb::ThreadSP thread_sp, lldb::ScriptedThreadInterfaceSP scripted_thread_interface_sp, StructuredData::DictionarySP args_sp, StructuredData::Generic *script_object=nullptr)
Create a ScriptedFrame from a object instanciated in the script interpreter.
static Ret ErrorWithMessage(llvm::StringRef caller_name, llvm::StringRef error_msg, Status &error, LLDBLog log_category=LLDBLog::Process)
StructuredData::DictionarySP GetArgsSP() const
virtual lldb::ScriptedThreadInterfaceSP CreateScriptedThreadInterface()
virtual std::optional< std::string > GetScriptedThreadPluginName()
const ScriptedMetadata m_scripted_metadata
ScriptedProcessInterface & GetInterface() const
void CheckInterpreterAndScriptObject() const
std::shared_ptr< DynamicRegisterInfo > GetDynamicRegisterInfo()
lldb_private::StructuredData::GenericSP m_script_object_sp
const char * GetQueueName() override
Retrieve the Queue name for the queue currently using this Thread.
lldb::RegisterContextSP CreateRegisterContextForFrame(lldb_private::StackFrame *frame) override
std::shared_ptr< DynamicRegisterInfo > m_register_info_sp
lldb::ScriptedThreadInterfaceSP GetInterface() const
StructuredData::ObjectSP FetchThreadExtendedInfo() override
lldb::ScriptedThreadInterfaceSP m_scripted_thread_interface_sp
static llvm::Expected< std::shared_ptr< ScriptedThread > > Create(ScriptedProcess &process, StructuredData::Generic *script_object=nullptr)
void WillResume(lldb::StateType resume_state) override
ScriptedThread(ScriptedProcess &process, lldb::ScriptedThreadInterfaceSP interface_sp, lldb::tid_t tid, StructuredData::GenericSP script_object_sp=nullptr)
const char * GetName() override
bool CalculateStopInfo() override
Ask the thread subclass to set its stop info.
const ScriptedProcess & m_scripted_process
lldb::RegisterContextSP GetRegisterContext() override
virtual uint32_t GetConcreteFrameIndex()
Query this frame to find what frame it is in this Thread's StackFrameList, not counting inlined frame...
Definition StackFrame.h:486
@ Synthetic
An synthetic stack frame (e.g.
Definition StackFrame.h:72
An error handling class.
Definition Status.h:118
static lldb::StopInfoSP CreateStopReasonWithMachException(Thread &thread, uint32_t exc_type, uint32_t exc_data_count, uint64_t exc_code, uint64_t exc_sub_code, uint64_t exc_sub_sub_code, bool pc_already_adjusted=true, bool adjust_pc_if_needed=false)
static lldb::StopInfoSP CreateStopReasonToTrace(Thread &thread)
static lldb::StopInfoSP CreateStopReasonWithSignal(Thread &thread, int signo, const char *description=nullptr, std::optional< int > code=std::nullopt)
static lldb::StopInfoSP CreateStopReasonWithException(Thread &thread, const char *description)
static lldb::StopInfoSP CreateStopReasonWithBreakpointSiteID(Thread &thread, lldb::break_id_t break_id)
bool ForEach(std::function< bool(Object *object)> const &foreach_callback) const
bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const
bool GetValueForKeyAsString(llvm::StringRef key, llvm::StringRef &result) const
bool GetValueForKeyAsArray(llvm::StringRef key, Array *&result) const
bool GetValueForKeyAsDictionary(llvm::StringRef key, Dictionary *&result) const
std::shared_ptr< Generic > GenericSP
std::shared_ptr< Dictionary > DictionarySP
std::shared_ptr< Object > ObjectSP
std::shared_ptr< Array > ArraySP
Defines a symbol context baton that can be handed other debug core functions.
void SetStopInfo(const lldb::StopInfoSP &stop_info_sp)
Definition Thread.cpp:479
virtual void DestroyThread()
Definition Thread.cpp:259
virtual void ClearStackFrames()
Definition Thread.cpp:1749
virtual Unwind & GetUnwinder()
Definition Thread.cpp:2252
Thread(Process &process, lldb::tid_t tid, bool use_invalid_index_id=false)
Constructor.
Definition Thread.cpp:226
void SetThreadStoppedAtUnexecutedBP(lldb::addr_t pc)
When a thread stops at an enabled BreakpointSite that has not executed, the Process plugin should cal...
Definition Thread.h:403
lldb::ProcessSP GetProcess() const
Definition Thread.h:162
friend class StackFrame
Definition Thread.h:1371
lldb::StackFrameListSP GetStackFrameList()
Definition Thread.cpp:1499
lldb::RegisterContextSP m_reg_context_sp
The register context for this thread's current register state.
Definition Thread.h:1436
lldb::RegisterContextSP CreateRegisterContextForFrame(StackFrame *frame)
Definition Unwind.h:56
#define LLDB_INVALID_BREAK_ID
#define LLDB_INVALID_SIGNAL_NUMBER
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
std::string toString(FormatterBytecode::OpCodes op)
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::BreakpointSite > BreakpointSiteSP
StateType
Process and Thread States.
int32_t break_id_t
Definition lldb-types.h:87
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::ScriptedThreadInterface > ScriptedThreadInterfaceSP
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
std::shared_ptr< lldb_private::StopInfo > StopInfoSP
uint64_t addr_t
Definition lldb-types.h:80
StopReason
Thread stop reasons.
@ eStopReasonBreakpoint
@ eStopReasonException
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP
uint64_t tid_t
Definition lldb-types.h:84
std::shared_ptr< lldb_private::StackFrameList > StackFrameListSP