blob: b7af4593add56dcb89b977a4e8490761215b0365 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SBThread.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
Eli Friedman4c5de692010-06-09 07:44:37 +000010#include "lldb/API/SBThread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/API/SBFileSpec.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000013#include "lldb/API/SBStream.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000014#include "lldb/API/SBSymbolContext.h"
Greg Clayton4e78f602010-11-18 18:52:36 +000015#include "lldb/Breakpoint/BreakpointLocation.h"
Greg Clayton66111032010-06-23 01:19:29 +000016#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Core/StreamFile.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000018#include "lldb/Core/ValueObject.h"
Greg Clayton66111032010-06-23 01:19:29 +000019#include "lldb/Interpreter/CommandInterpreter.h"
Zachary Turner93749ab2015-03-03 21:51:25 +000020#include "lldb/Symbol/CompileUnit.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000021#include "lldb/Symbol/SymbolContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Target/Process.h"
Jason Molendab9ffa982014-04-25 00:01:15 +000023#include "lldb/Target/Queue.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000024#include "lldb/Target/StopInfo.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000025#include "lldb/Target/SystemRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Target/Target.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000027#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Target/ThreadPlan.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000029#include "lldb/Target/ThreadPlanStepInRange.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Target/ThreadPlanStepInstruction.h"
31#include "lldb/Target/ThreadPlanStepOut.h"
32#include "lldb/Target/ThreadPlanStepRange.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000033#include "lldb/Target/UnixSignals.h"
Pavel Labathd821c992018-08-07 11:07:21 +000034#include "lldb/Utility/State.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000035#include "lldb/Utility/Stream.h"
Pavel Labathf2a8bcc2017-06-27 10:45:31 +000036#include "lldb/Utility/StructuredData.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037
Eli Friedman4c5de692010-06-09 07:44:37 +000038#include "lldb/API/SBAddress.h"
Eli Friedman4c5de692010-06-09 07:44:37 +000039#include "lldb/API/SBDebugger.h"
Jim Ingham4f465cf2012-10-10 18:32:14 +000040#include "lldb/API/SBEvent.h"
Jim Ingham73ca05a2011-12-17 01:35:57 +000041#include "lldb/API/SBFrame.h"
Eli Friedman4c5de692010-06-09 07:44:37 +000042#include "lldb/API/SBProcess.h"
Kuba Brecka6a831432016-03-23 15:36:22 +000043#include "lldb/API/SBThreadCollection.h"
Jim Ingham2bdbfd52014-09-29 23:17:18 +000044#include "lldb/API/SBThreadPlan.h"
Jim Ingham73ca05a2011-12-17 01:35:57 +000045#include "lldb/API/SBValue.h"
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +000046#include "lldb/lldb-enumerations.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047
48using namespace lldb;
49using namespace lldb_private;
50
Kate Stoneb9c1b512016-09-06 20:57:50 +000051const char *SBThread::GetBroadcasterClassName() {
52 return Thread::GetStaticBroadcasterClass().AsCString();
Jim Ingham4f465cf2012-10-10 18:32:14 +000053}
54
Greg Claytoncfd1ace2010-10-31 03:01:06 +000055//----------------------------------------------------------------------
56// Constructors
57//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000058SBThread::SBThread() : m_opaque_sp(new ExecutionContextRef()) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059
Kate Stoneb9c1b512016-09-06 20:57:50 +000060SBThread::SBThread(const ThreadSP &lldb_object_sp)
61 : m_opaque_sp(new ExecutionContextRef(lldb_object_sp)) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062
Kate Stoneb9c1b512016-09-06 20:57:50 +000063SBThread::SBThread(const SBThread &rhs)
64 : m_opaque_sp(new ExecutionContextRef(*rhs.m_opaque_sp)) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065
66//----------------------------------------------------------------------
Greg Claytoncfd1ace2010-10-31 03:01:06 +000067// Assignment operator
68//----------------------------------------------------------------------
69
Kate Stoneb9c1b512016-09-06 20:57:50 +000070const lldb::SBThread &SBThread::operator=(const SBThread &rhs) {
71 if (this != &rhs)
72 *m_opaque_sp = *rhs.m_opaque_sp;
73 return *this;
Greg Claytoncfd1ace2010-10-31 03:01:06 +000074}
75
76//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077// Destructor
78//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000079SBThread::~SBThread() {}
80
81lldb::SBQueue SBThread::GetQueue() const {
82 SBQueue sb_queue;
83 QueueSP queue_sp;
84 std::unique_lock<std::recursive_mutex> lock;
85 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
86
87 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
88 if (exe_ctx.HasThreadScope()) {
89 Process::StopLocker stop_locker;
90 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) {
91 queue_sp = exe_ctx.GetThreadPtr()->GetQueue();
92 if (queue_sp) {
93 sb_queue.SetQueue(queue_sp);
94 }
95 } else {
96 if (log)
97 log->Printf("SBThread(%p)::GetQueue() => error: process is running",
98 static_cast<void *>(exe_ctx.GetThreadPtr()));
99 }
100 }
101
102 if (log)
103 log->Printf("SBThread(%p)::GetQueue () => SBQueue(%p)",
104 static_cast<void *>(exe_ctx.GetThreadPtr()),
105 static_cast<void *>(queue_sp.get()));
106
107 return sb_queue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108}
109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110bool SBThread::IsValid() const {
111 std::unique_lock<std::recursive_mutex> lock;
112 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Jason Molendab9ffa982014-04-25 00:01:15 +0000113
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114 Target *target = exe_ctx.GetTargetPtr();
115 Process *process = exe_ctx.GetProcessPtr();
116 if (target && process) {
117 Process::StopLocker stop_locker;
118 if (stop_locker.TryLock(&process->GetRunLock()))
119 return m_opaque_sp->GetThreadSP().get() != NULL;
120 }
121 // Without a valid target & process, this thread can't be valid.
122 return false;
123}
124
125void SBThread::Clear() { m_opaque_sp->Clear(); }
126
127StopReason SBThread::GetStopReason() {
128 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
129
130 StopReason reason = eStopReasonInvalid;
131 std::unique_lock<std::recursive_mutex> lock;
132 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
133
134 if (exe_ctx.HasThreadScope()) {
135 Process::StopLocker stop_locker;
136 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) {
137 return exe_ctx.GetThreadPtr()->GetStopReason();
138 } else {
139 if (log)
140 log->Printf(
141 "SBThread(%p)::GetStopReason() => error: process is running",
142 static_cast<void *>(exe_ctx.GetThreadPtr()));
143 }
144 }
145
146 if (log)
147 log->Printf("SBThread(%p)::GetStopReason () => %s",
148 static_cast<void *>(exe_ctx.GetThreadPtr()),
149 Thread::StopReasonAsCString(reason));
150
151 return reason;
152}
153
154size_t SBThread::GetStopReasonDataCount() {
155 std::unique_lock<std::recursive_mutex> lock;
156 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
157
158 if (exe_ctx.HasThreadScope()) {
159 Process::StopLocker stop_locker;
160 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) {
161 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo();
162 if (stop_info_sp) {
163 StopReason reason = stop_info_sp->GetStopReason();
164 switch (reason) {
165 case eStopReasonInvalid:
166 case eStopReasonNone:
167 case eStopReasonTrace:
168 case eStopReasonExec:
169 case eStopReasonPlanComplete:
170 case eStopReasonThreadExiting:
171 case eStopReasonInstrumentation:
172 // There is no data for these stop reasons.
173 return 0;
174
175 case eStopReasonBreakpoint: {
176 break_id_t site_id = stop_info_sp->GetValue();
177 lldb::BreakpointSiteSP bp_site_sp(
178 exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID(
179 site_id));
180 if (bp_site_sp)
181 return bp_site_sp->GetNumberOfOwners() * 2;
182 else
183 return 0; // Breakpoint must have cleared itself...
184 } break;
185
186 case eStopReasonWatchpoint:
187 return 1;
188
189 case eStopReasonSignal:
190 return 1;
191
192 case eStopReasonException:
193 return 1;
194 }
195 }
196 } else {
197 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
198 if (log)
199 log->Printf("SBThread(%p)::GetStopReasonDataCount() => error: process "
200 "is running",
201 static_cast<void *>(exe_ctx.GetThreadPtr()));
202 }
203 }
204 return 0;
205}
206
207uint64_t SBThread::GetStopReasonDataAtIndex(uint32_t idx) {
208 std::unique_lock<std::recursive_mutex> lock;
209 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
210
211 if (exe_ctx.HasThreadScope()) {
212 Process::StopLocker stop_locker;
213 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) {
214 Thread *thread = exe_ctx.GetThreadPtr();
215 StopInfoSP stop_info_sp = thread->GetStopInfo();
216 if (stop_info_sp) {
217 StopReason reason = stop_info_sp->GetStopReason();
218 switch (reason) {
219 case eStopReasonInvalid:
220 case eStopReasonNone:
221 case eStopReasonTrace:
222 case eStopReasonExec:
223 case eStopReasonPlanComplete:
224 case eStopReasonThreadExiting:
225 case eStopReasonInstrumentation:
226 // There is no data for these stop reasons.
227 return 0;
228
229 case eStopReasonBreakpoint: {
230 break_id_t site_id = stop_info_sp->GetValue();
231 lldb::BreakpointSiteSP bp_site_sp(
232 exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID(
233 site_id));
234 if (bp_site_sp) {
235 uint32_t bp_index = idx / 2;
236 BreakpointLocationSP bp_loc_sp(
237 bp_site_sp->GetOwnerAtIndex(bp_index));
238 if (bp_loc_sp) {
239 if (idx & 1) {
240 // Odd idx, return the breakpoint location ID
241 return bp_loc_sp->GetID();
242 } else {
243 // Even idx, return the breakpoint ID
244 return bp_loc_sp->GetBreakpoint().GetID();
245 }
Jason Molendab9ffa982014-04-25 00:01:15 +0000246 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247 }
248 return LLDB_INVALID_BREAK_ID;
249 } break;
250
251 case eStopReasonWatchpoint:
252 return stop_info_sp->GetValue();
253
254 case eStopReasonSignal:
255 return stop_info_sp->GetValue();
256
257 case eStopReasonException:
258 return stop_info_sp->GetValue();
Jason Molendab9ffa982014-04-25 00:01:15 +0000259 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260 }
261 } else {
262 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
263 if (log)
264 log->Printf("SBThread(%p)::GetStopReasonDataAtIndex() => error: "
265 "process is running",
266 static_cast<void *>(exe_ctx.GetThreadPtr()));
Jason Molendab9ffa982014-04-25 00:01:15 +0000267 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268 }
269 return 0;
Jason Molendab9ffa982014-04-25 00:01:15 +0000270}
271
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272bool SBThread::GetStopReasonExtendedInfoAsJSON(lldb::SBStream &stream) {
273 Stream &strm = stream.ref();
Jason Molendab9ffa982014-04-25 00:01:15 +0000274
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275 std::unique_lock<std::recursive_mutex> lock;
276 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Jim Ingham7fa7dc32016-05-07 00:54:56 +0000277
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278 if (!exe_ctx.HasThreadScope())
Jim Ingham7fa7dc32016-05-07 00:54:56 +0000279 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000280
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281 StopInfoSP stop_info = exe_ctx.GetThreadPtr()->GetStopInfo();
282 StructuredData::ObjectSP info = stop_info->GetExtendedInfo();
283 if (!info)
284 return false;
Greg Clayton48e42542010-07-30 20:12:55 +0000285
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286 info->Dump(strm);
Greg Clayton48e42542010-07-30 20:12:55 +0000287
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288 return true;
Kuba Breckaafdf8422014-10-10 23:43:03 +0000289}
290
Kuba Brecka6a831432016-03-23 15:36:22 +0000291SBThreadCollection
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292SBThread::GetStopReasonExtendedBacktraces(InstrumentationRuntimeType type) {
293 ThreadCollectionSP threads;
294 threads.reset(new ThreadCollection());
Jim Inghamb2e7d282016-06-10 17:22:26 +0000295
Kate Stoneb9c1b512016-09-06 20:57:50 +0000296 std::unique_lock<std::recursive_mutex> lock;
297 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
298
299 if (!exe_ctx.HasThreadScope())
300 return threads;
301
302 ProcessSP process_sp = exe_ctx.GetProcessSP();
303
304 StopInfoSP stop_info = exe_ctx.GetThreadPtr()->GetStopInfo();
305 StructuredData::ObjectSP info = stop_info->GetExtendedInfo();
306 if (!info)
307 return threads;
308
309 return process_sp->GetInstrumentationRuntime(type)
310 ->GetBacktracesFromExtendedStopInfo(info);
Kuba Brecka6a831432016-03-23 15:36:22 +0000311}
312
Kate Stoneb9c1b512016-09-06 20:57:50 +0000313size_t SBThread::GetStopDescription(char *dst, size_t dst_len) {
314 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000315
Kate Stoneb9c1b512016-09-06 20:57:50 +0000316 std::unique_lock<std::recursive_mutex> lock;
317 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000318
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319 if (exe_ctx.HasThreadScope()) {
320 Process::StopLocker stop_locker;
321 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000322
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo();
324 if (stop_info_sp) {
325 const char *stop_desc = stop_info_sp->GetDescription();
326 if (stop_desc) {
327 if (log)
328 log->Printf(
329 "SBThread(%p)::GetStopDescription (dst, dst_len) => \"%s\"",
330 static_cast<void *>(exe_ctx.GetThreadPtr()), stop_desc);
331 if (dst)
332 return ::snprintf(dst, dst_len, "%s", stop_desc);
333 else {
334 // NULL dst passed in, return the length needed to contain the
335 // description
336 return ::strlen(stop_desc) + 1; // Include the NULL byte for size
337 }
338 } else {
339 size_t stop_desc_len = 0;
340 switch (stop_info_sp->GetStopReason()) {
341 case eStopReasonTrace:
342 case eStopReasonPlanComplete: {
343 static char trace_desc[] = "step";
344 stop_desc = trace_desc;
345 stop_desc_len =
346 sizeof(trace_desc); // Include the NULL byte for size
347 } break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000348
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349 case eStopReasonBreakpoint: {
350 static char bp_desc[] = "breakpoint hit";
351 stop_desc = bp_desc;
352 stop_desc_len = sizeof(bp_desc); // Include the NULL byte for size
353 } break;
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000354
Kate Stoneb9c1b512016-09-06 20:57:50 +0000355 case eStopReasonWatchpoint: {
356 static char wp_desc[] = "watchpoint hit";
357 stop_desc = wp_desc;
358 stop_desc_len = sizeof(wp_desc); // Include the NULL byte for size
359 } break;
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000360
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361 case eStopReasonSignal: {
362 stop_desc =
363 exe_ctx.GetProcessPtr()->GetUnixSignals()->GetSignalAsCString(
364 stop_info_sp->GetValue());
365 if (stop_desc == NULL || stop_desc[0] == '\0') {
366 static char signal_desc[] = "signal";
367 stop_desc = signal_desc;
368 stop_desc_len =
369 sizeof(signal_desc); // Include the NULL byte for size
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000370 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371 } break;
372
373 case eStopReasonException: {
374 char exc_desc[] = "exception";
375 stop_desc = exc_desc;
376 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
377 } break;
378
379 case eStopReasonExec: {
380 char exc_desc[] = "exec";
381 stop_desc = exc_desc;
382 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
383 } break;
384
385 case eStopReasonThreadExiting: {
386 char limbo_desc[] = "thread exiting";
387 stop_desc = limbo_desc;
388 stop_desc_len = sizeof(limbo_desc);
389 } break;
390 default:
391 break;
392 }
393
394 if (stop_desc && stop_desc[0]) {
Greg Claytonc9858e42012-04-06 02:17:47 +0000395 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000396 log->Printf(
397 "SBThread(%p)::GetStopDescription (dst, dst_len) => '%s'",
398 static_cast<void *>(exe_ctx.GetThreadPtr()), stop_desc);
399
400 if (dst)
401 return ::snprintf(dst, dst_len, "%s", stop_desc) +
402 1; // Include the NULL byte
403
404 if (stop_desc_len == 0)
405 stop_desc_len = ::strlen(stop_desc) + 1; // Include the NULL byte
406
407 return stop_desc_len;
408 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000409 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000410 }
411 } else {
412 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
413 if (log)
414 log->Printf(
415 "SBThread(%p)::GetStopDescription() => error: process is running",
416 static_cast<void *>(exe_ctx.GetThreadPtr()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000418 }
419 if (dst)
420 *dst = 0;
421 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000422}
423
Kate Stoneb9c1b512016-09-06 20:57:50 +0000424SBValue SBThread::GetStopReturnValue() {
425 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
426 ValueObjectSP return_valobj_sp;
427 std::unique_lock<std::recursive_mutex> lock;
428 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000429
Kate Stoneb9c1b512016-09-06 20:57:50 +0000430 if (exe_ctx.HasThreadScope()) {
431 Process::StopLocker stop_locker;
432 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) {
433 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo();
434 if (stop_info_sp) {
435 return_valobj_sp = StopInfo::GetReturnValueObject(stop_info_sp);
436 }
437 } else {
438 if (log)
439 log->Printf(
440 "SBThread(%p)::GetStopReturnValue() => error: process is running",
441 static_cast<void *>(exe_ctx.GetThreadPtr()));
Jim Ingham73ca05a2011-12-17 01:35:57 +0000442 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000443 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000444
Kate Stoneb9c1b512016-09-06 20:57:50 +0000445 if (log)
446 log->Printf("SBThread(%p)::GetStopReturnValue () => %s",
447 static_cast<void *>(exe_ctx.GetThreadPtr()),
448 return_valobj_sp.get() ? return_valobj_sp->GetValueAsCString()
449 : "<no return value>");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000450
Kate Stoneb9c1b512016-09-06 20:57:50 +0000451 return SBValue(return_valobj_sp);
Jim Ingham73ca05a2011-12-17 01:35:57 +0000452}
453
Kate Stoneb9c1b512016-09-06 20:57:50 +0000454void SBThread::SetThread(const ThreadSP &lldb_object_sp) {
455 m_opaque_sp->SetThreadSP(lldb_object_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000456}
457
Kate Stoneb9c1b512016-09-06 20:57:50 +0000458lldb::tid_t SBThread::GetThreadID() const {
459 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
460 if (thread_sp)
461 return thread_sp->GetID();
462 return LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463}
464
Kate Stoneb9c1b512016-09-06 20:57:50 +0000465uint32_t SBThread::GetIndexID() const {
466 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
467 if (thread_sp)
468 return thread_sp->GetIndexID();
469 return LLDB_INVALID_INDEX32;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000470}
Greg Clayton1ac04c32012-02-21 00:09:25 +0000471
Kate Stoneb9c1b512016-09-06 20:57:50 +0000472const char *SBThread::GetName() const {
473 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
474 const char *name = NULL;
475 std::unique_lock<std::recursive_mutex> lock;
476 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000477
Kate Stoneb9c1b512016-09-06 20:57:50 +0000478 if (exe_ctx.HasThreadScope()) {
479 Process::StopLocker stop_locker;
480 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) {
481 name = exe_ctx.GetThreadPtr()->GetName();
482 } else {
483 if (log)
484 log->Printf("SBThread(%p)::GetName() => error: process is running",
485 static_cast<void *>(exe_ctx.GetThreadPtr()));
Greg Claytonaf67cec2010-12-20 20:49:23 +0000486 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000487 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000488
Kate Stoneb9c1b512016-09-06 20:57:50 +0000489 if (log)
490 log->Printf("SBThread(%p)::GetName () => %s",
491 static_cast<void *>(exe_ctx.GetThreadPtr()),
492 name ? name : "NULL");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000493
Kate Stoneb9c1b512016-09-06 20:57:50 +0000494 return name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000495}
496
Kate Stoneb9c1b512016-09-06 20:57:50 +0000497const char *SBThread::GetQueueName() const {
498 const char *name = NULL;
499 std::unique_lock<std::recursive_mutex> lock;
500 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000501
Kate Stoneb9c1b512016-09-06 20:57:50 +0000502 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
503 if (exe_ctx.HasThreadScope()) {
504 Process::StopLocker stop_locker;
505 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) {
506 name = exe_ctx.GetThreadPtr()->GetQueueName();
507 } else {
508 if (log)
509 log->Printf("SBThread(%p)::GetQueueName() => error: process is running",
510 static_cast<void *>(exe_ctx.GetThreadPtr()));
Greg Claytonaf67cec2010-12-20 20:49:23 +0000511 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000512 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000513
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514 if (log)
515 log->Printf("SBThread(%p)::GetQueueName () => %s",
516 static_cast<void *>(exe_ctx.GetThreadPtr()),
517 name ? name : "NULL");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000518
Kate Stoneb9c1b512016-09-06 20:57:50 +0000519 return name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000520}
521
Kate Stoneb9c1b512016-09-06 20:57:50 +0000522lldb::queue_id_t SBThread::GetQueueID() const {
523 queue_id_t id = LLDB_INVALID_QUEUE_ID;
524 std::unique_lock<std::recursive_mutex> lock;
525 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Jason Molenda4fdb5862013-10-21 23:52:54 +0000526
Kate Stoneb9c1b512016-09-06 20:57:50 +0000527 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
528 if (exe_ctx.HasThreadScope()) {
529 Process::StopLocker stop_locker;
530 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) {
531 id = exe_ctx.GetThreadPtr()->GetQueueID();
532 } else {
533 if (log)
534 log->Printf("SBThread(%p)::GetQueueID() => error: process is running",
535 static_cast<void *>(exe_ctx.GetThreadPtr()));
Jason Molenda4fdb5862013-10-21 23:52:54 +0000536 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000537 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000538
Kate Stoneb9c1b512016-09-06 20:57:50 +0000539 if (log)
540 log->Printf("SBThread(%p)::GetQueueID () => 0x%" PRIx64,
541 static_cast<void *>(exe_ctx.GetThreadPtr()), id);
Jason Molenda4fdb5862013-10-21 23:52:54 +0000542
Kate Stoneb9c1b512016-09-06 20:57:50 +0000543 return id;
Jason Molenda4fdb5862013-10-21 23:52:54 +0000544}
545
Kate Stoneb9c1b512016-09-06 20:57:50 +0000546bool SBThread::GetInfoItemByPathAsString(const char *path, SBStream &strm) {
547 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
548 bool success = false;
549 std::unique_lock<std::recursive_mutex> lock;
550 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Jason Molenda705b1802014-06-13 02:37:02 +0000551
Kate Stoneb9c1b512016-09-06 20:57:50 +0000552 if (exe_ctx.HasThreadScope()) {
553 Process::StopLocker stop_locker;
554 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) {
555 Thread *thread = exe_ctx.GetThreadPtr();
556 StructuredData::ObjectSP info_root_sp = thread->GetExtendedInfo();
557 if (info_root_sp) {
558 StructuredData::ObjectSP node =
559 info_root_sp->GetObjectForDotSeparatedPath(path);
560 if (node) {
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +0000561 if (node->GetType() == eStructuredDataTypeString) {
Zachary Turner28333212017-05-12 05:49:54 +0000562 strm.Printf("%s", node->GetAsString()->GetValue().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000563 success = true;
564 }
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +0000565 if (node->GetType() == eStructuredDataTypeInteger) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000566 strm.Printf("0x%" PRIx64, node->GetAsInteger()->GetValue());
567 success = true;
568 }
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +0000569 if (node->GetType() == eStructuredDataTypeFloat) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000570 strm.Printf("0x%f", node->GetAsFloat()->GetValue());
571 success = true;
572 }
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +0000573 if (node->GetType() == eStructuredDataTypeBoolean) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000574 if (node->GetAsBoolean()->GetValue() == true)
575 strm.Printf("true");
576 else
577 strm.Printf("false");
578 success = true;
579 }
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +0000580 if (node->GetType() == eStructuredDataTypeNull) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000581 strm.Printf("null");
582 success = true;
583 }
Jason Molenda705b1802014-06-13 02:37:02 +0000584 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000585 }
586 } else {
587 if (log)
588 log->Printf("SBThread(%p)::GetInfoItemByPathAsString() => error: "
589 "process is running",
590 static_cast<void *>(exe_ctx.GetThreadPtr()));
Jason Molenda705b1802014-06-13 02:37:02 +0000591 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000592 }
Jason Molenda705b1802014-06-13 02:37:02 +0000593
Kate Stoneb9c1b512016-09-06 20:57:50 +0000594 if (log)
Jason Molenda753e13c2017-02-02 03:02:51 +0000595 log->Printf("SBThread(%p)::GetInfoItemByPathAsString (\"%s\") => \"%s\"",
596 static_cast<void *>(exe_ctx.GetThreadPtr()), path, strm.GetData());
Jason Molenda705b1802014-06-13 02:37:02 +0000597
Kate Stoneb9c1b512016-09-06 20:57:50 +0000598 return success;
Jason Molenda705b1802014-06-13 02:37:02 +0000599}
600
Kate Stoneb9c1b512016-09-06 20:57:50 +0000601SBError SBThread::ResumeNewPlan(ExecutionContext &exe_ctx,
602 ThreadPlan *new_plan) {
603 SBError sb_error;
Jason Molenda705b1802014-06-13 02:37:02 +0000604
Kate Stoneb9c1b512016-09-06 20:57:50 +0000605 Process *process = exe_ctx.GetProcessPtr();
606 if (!process) {
607 sb_error.SetErrorString("No process in SBThread::ResumeNewPlan");
Jim Ingham64e7ead2012-05-03 21:19:36 +0000608 return sb_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000609 }
610
611 Thread *thread = exe_ctx.GetThreadPtr();
612 if (!thread) {
613 sb_error.SetErrorString("No thread in SBThread::ResumeNewPlan");
614 return sb_error;
615 }
616
617 // User level plans should be Master Plans so they can be interrupted, other
Adrian Prantl05097242018-04-30 16:49:04 +0000618 // plans executed, and then a "continue" will resume the plan.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000619 if (new_plan != NULL) {
620 new_plan->SetIsMasterPlan(true);
621 new_plan->SetOkayToDiscard(false);
622 }
623
624 // Why do we need to set the current thread by ID here???
625 process->GetThreadList().SetSelectedThreadByID(thread->GetID());
626
627 if (process->GetTarget().GetDebugger().GetAsyncExecution())
628 sb_error.ref() = process->Resume();
629 else
630 sb_error.ref() = process->ResumeSynchronous(NULL);
631
632 return sb_error;
Jim Ingham64e7ead2012-05-03 21:19:36 +0000633}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000634
Kate Stoneb9c1b512016-09-06 20:57:50 +0000635void SBThread::StepOver(lldb::RunMode stop_other_threads) {
Alexander Polyakov859f54b2018-06-20 21:43:16 +0000636 SBError error; // Ignored
637 StepOver(stop_other_threads, error);
638}
639
640void SBThread::StepOver(lldb::RunMode stop_other_threads, SBError &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000641 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000642
Kate Stoneb9c1b512016-09-06 20:57:50 +0000643 std::unique_lock<std::recursive_mutex> lock;
644 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000645
Kate Stoneb9c1b512016-09-06 20:57:50 +0000646 if (log)
647 log->Printf("SBThread(%p)::StepOver (stop_other_threads='%s')",
648 static_cast<void *>(exe_ctx.GetThreadPtr()),
649 Thread::RunModeAsCString(stop_other_threads));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000650
Alexander Polyakov859f54b2018-06-20 21:43:16 +0000651 if (!exe_ctx.HasThreadScope()) {
652 error.SetErrorString("this SBThread object is invalid");
653 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000654 }
Alexander Polyakov859f54b2018-06-20 21:43:16 +0000655
656 Thread *thread = exe_ctx.GetThreadPtr();
657 bool abort_other_plans = false;
658 StackFrameSP frame_sp(thread->GetStackFrameAtIndex(0));
659
660 ThreadPlanSP new_plan_sp;
661 if (frame_sp) {
662 if (frame_sp->HasDebugInformation()) {
663 const LazyBool avoid_no_debug = eLazyBoolCalculate;
664 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
665 new_plan_sp = thread->QueueThreadPlanForStepOverRange(
666 abort_other_plans, sc.line_entry, sc, stop_other_threads,
667 avoid_no_debug);
668 } else {
669 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction(
670 true, abort_other_plans, stop_other_threads);
671 }
672 }
673 error = ResumeNewPlan(exe_ctx, new_plan_sp.get());
Greg Clayton481cef22011-01-21 06:11:58 +0000674}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000675
Kate Stoneb9c1b512016-09-06 20:57:50 +0000676void SBThread::StepInto(lldb::RunMode stop_other_threads) {
677 StepInto(NULL, stop_other_threads);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000678}
679
Kate Stoneb9c1b512016-09-06 20:57:50 +0000680void SBThread::StepInto(const char *target_name,
681 lldb::RunMode stop_other_threads) {
Alexander Polyakov859f54b2018-06-20 21:43:16 +0000682 SBError error; // Ignored
Kate Stoneb9c1b512016-09-06 20:57:50 +0000683 StepInto(target_name, LLDB_INVALID_LINE_NUMBER, error, stop_other_threads);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684}
685
Kate Stoneb9c1b512016-09-06 20:57:50 +0000686void SBThread::StepInto(const char *target_name, uint32_t end_line,
687 SBError &error, lldb::RunMode stop_other_threads) {
688 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000689
Kate Stoneb9c1b512016-09-06 20:57:50 +0000690 std::unique_lock<std::recursive_mutex> lock;
691 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000692
Kate Stoneb9c1b512016-09-06 20:57:50 +0000693 if (log)
694 log->Printf(
695 "SBThread(%p)::StepInto (target_name='%s', stop_other_threads='%s')",
696 static_cast<void *>(exe_ctx.GetThreadPtr()),
697 target_name ? target_name : "<NULL>",
698 Thread::RunModeAsCString(stop_other_threads));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000699
Alexander Polyakov859f54b2018-06-20 21:43:16 +0000700 if (!exe_ctx.HasThreadScope()) {
701 error.SetErrorString("this SBThread object is invalid");
702 return;
703 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000704
Alexander Polyakov859f54b2018-06-20 21:43:16 +0000705 bool abort_other_plans = false;
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000706
Alexander Polyakov859f54b2018-06-20 21:43:16 +0000707 Thread *thread = exe_ctx.GetThreadPtr();
708 StackFrameSP frame_sp(thread->GetStackFrameAtIndex(0));
709 ThreadPlanSP new_plan_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000710
Alexander Polyakov859f54b2018-06-20 21:43:16 +0000711 if (frame_sp && frame_sp->HasDebugInformation()) {
712 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
713 AddressRange range;
714 if (end_line == LLDB_INVALID_LINE_NUMBER)
715 range = sc.line_entry.range;
716 else {
717 if (!sc.GetAddressRangeFromHereToEndLine(end_line, range, error.ref()))
718 return;
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000719 }
720
Alexander Polyakov859f54b2018-06-20 21:43:16 +0000721 const LazyBool step_out_avoids_code_without_debug_info =
722 eLazyBoolCalculate;
723 const LazyBool step_in_avoids_code_without_debug_info =
724 eLazyBoolCalculate;
725 new_plan_sp = thread->QueueThreadPlanForStepInRange(
726 abort_other_plans, range, sc, target_name, stop_other_threads,
727 step_in_avoids_code_without_debug_info,
728 step_out_avoids_code_without_debug_info);
729 } else {
730 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction(
731 false, abort_other_plans, stop_other_threads);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000732 }
Alexander Polyakov859f54b2018-06-20 21:43:16 +0000733 error = ResumeNewPlan(exe_ctx, new_plan_sp.get());
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000734}
735
Kate Stoneb9c1b512016-09-06 20:57:50 +0000736void SBThread::StepOut() {
Alexander Polyakov859f54b2018-06-20 21:43:16 +0000737 SBError error; // Ignored
738 StepOut(error);
739}
740
741void SBThread::StepOut(SBError &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000742 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Richard Mittonf86248d2013-09-12 02:20:34 +0000743
Kate Stoneb9c1b512016-09-06 20:57:50 +0000744 std::unique_lock<std::recursive_mutex> lock;
745 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Richard Mittonf86248d2013-09-12 02:20:34 +0000746
Kate Stoneb9c1b512016-09-06 20:57:50 +0000747 if (log)
748 log->Printf("SBThread(%p)::StepOut ()",
749 static_cast<void *>(exe_ctx.GetThreadPtr()));
Richard Mittonf86248d2013-09-12 02:20:34 +0000750
Alexander Polyakov859f54b2018-06-20 21:43:16 +0000751 if (!exe_ctx.HasThreadScope()) {
752 error.SetErrorString("this SBThread object is invalid");
753 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000754 }
Alexander Polyakov859f54b2018-06-20 21:43:16 +0000755
756 bool abort_other_plans = false;
757 bool stop_other_threads = false;
758
759 Thread *thread = exe_ctx.GetThreadPtr();
760
761 const LazyBool avoid_no_debug = eLazyBoolCalculate;
762 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepOut(
763 abort_other_plans, NULL, false, stop_other_threads, eVoteYes,
764 eVoteNoOpinion, 0, avoid_no_debug));
765
766 error = ResumeNewPlan(exe_ctx, new_plan_sp.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000767}
768
Alexander Polyakov859f54b2018-06-20 21:43:16 +0000769void SBThread::StepOutOfFrame(SBFrame &sb_frame) {
770 SBError error; // Ignored
771 StepOutOfFrame(sb_frame, error);
772}
773
774void SBThread::StepOutOfFrame(SBFrame &sb_frame, SBError &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000775 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
776
777 std::unique_lock<std::recursive_mutex> lock;
778 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
779
780 if (!sb_frame.IsValid()) {
781 if (log)
782 log->Printf(
783 "SBThread(%p)::StepOutOfFrame passed an invalid frame, returning.",
784 static_cast<void *>(exe_ctx.GetThreadPtr()));
Alexander Polyakov859f54b2018-06-20 21:43:16 +0000785 error.SetErrorString("passed invalid SBFrame object");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000786 return;
787 }
788
789 StackFrameSP frame_sp(sb_frame.GetFrameSP());
790 if (log) {
791 SBStream frame_desc_strm;
792 sb_frame.GetDescription(frame_desc_strm);
793 log->Printf("SBThread(%p)::StepOutOfFrame (frame = SBFrame(%p): %s)",
794 static_cast<void *>(exe_ctx.GetThreadPtr()),
795 static_cast<void *>(frame_sp.get()), frame_desc_strm.GetData());
796 }
797
Alexander Polyakov859f54b2018-06-20 21:43:16 +0000798 if (!exe_ctx.HasThreadScope()) {
799 error.SetErrorString("this SBThread object is invalid");
800 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000801 }
Alexander Polyakov859f54b2018-06-20 21:43:16 +0000802
803 bool abort_other_plans = false;
804 bool stop_other_threads = false;
805 Thread *thread = exe_ctx.GetThreadPtr();
806 if (sb_frame.GetThread().GetThreadID() != thread->GetID()) {
807 log->Printf("SBThread(%p)::StepOutOfFrame passed a frame from another "
808 "thread (0x%" PRIx64 " vrs. 0x%" PRIx64 ", returning.",
809 static_cast<void *>(exe_ctx.GetThreadPtr()),
810 sb_frame.GetThread().GetThreadID(), thread->GetID());
811 error.SetErrorString("passed a frame from another thread");
812 return;
813 }
814
815 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepOut(
816 abort_other_plans, NULL, false, stop_other_threads, eVoteYes,
817 eVoteNoOpinion, frame_sp->GetFrameIndex()));
818
819 error = ResumeNewPlan(exe_ctx, new_plan_sp.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000820}
821
822void SBThread::StepInstruction(bool step_over) {
Alexander Polyakov859f54b2018-06-20 21:43:16 +0000823 SBError error; // Ignored
824 StepInstruction(step_over, error);
825}
826
827void SBThread::StepInstruction(bool step_over, SBError &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000828 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
829
830 std::unique_lock<std::recursive_mutex> lock;
831 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
832
833 if (log)
834 log->Printf("SBThread(%p)::StepInstruction (step_over=%i)",
835 static_cast<void *>(exe_ctx.GetThreadPtr()), step_over);
836
Alexander Polyakov859f54b2018-06-20 21:43:16 +0000837 if (!exe_ctx.HasThreadScope()) {
838 error.SetErrorString("this SBThread object is invalid");
839 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000840 }
Alexander Polyakov859f54b2018-06-20 21:43:16 +0000841
842 Thread *thread = exe_ctx.GetThreadPtr();
843 ThreadPlanSP new_plan_sp(
844 thread->QueueThreadPlanForStepSingleInstruction(step_over, true, true));
845
846 error = ResumeNewPlan(exe_ctx, new_plan_sp.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000847}
848
849void SBThread::RunToAddress(lldb::addr_t addr) {
Alexander Polyakov859f54b2018-06-20 21:43:16 +0000850 SBError error; // Ignored
851 RunToAddress(addr, error);
852}
853
854void SBThread::RunToAddress(lldb::addr_t addr, SBError &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000855 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
856
857 std::unique_lock<std::recursive_mutex> lock;
858 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
859
860 if (log)
861 log->Printf("SBThread(%p)::RunToAddress (addr=0x%" PRIx64 ")",
862 static_cast<void *>(exe_ctx.GetThreadPtr()), addr);
863
Alexander Polyakov859f54b2018-06-20 21:43:16 +0000864 if (!exe_ctx.HasThreadScope()) {
865 error.SetErrorString("this SBThread object is invalid");
866 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000867 }
Alexander Polyakov859f54b2018-06-20 21:43:16 +0000868
869 bool abort_other_plans = false;
870 bool stop_other_threads = true;
871
872 Address target_addr(addr);
873
874 Thread *thread = exe_ctx.GetThreadPtr();
875
876 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForRunToAddress(
877 abort_other_plans, target_addr, stop_other_threads));
878
879 error = ResumeNewPlan(exe_ctx, new_plan_sp.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000880}
881
882SBError SBThread::StepOverUntil(lldb::SBFrame &sb_frame,
883 lldb::SBFileSpec &sb_file_spec, uint32_t line) {
884 SBError sb_error;
885 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
886 char path[PATH_MAX];
887
888 std::unique_lock<std::recursive_mutex> lock;
889 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
890
891 StackFrameSP frame_sp(sb_frame.GetFrameSP());
892
893 if (log) {
894 SBStream frame_desc_strm;
895 sb_frame.GetDescription(frame_desc_strm);
896 sb_file_spec->GetPath(path, sizeof(path));
897 log->Printf("SBThread(%p)::StepOverUntil (frame = SBFrame(%p): %s, "
898 "file+line = %s:%u)",
899 static_cast<void *>(exe_ctx.GetThreadPtr()),
900 static_cast<void *>(frame_sp.get()), frame_desc_strm.GetData(),
901 path, line);
902 }
903
904 if (exe_ctx.HasThreadScope()) {
905 Target *target = exe_ctx.GetTargetPtr();
906 Thread *thread = exe_ctx.GetThreadPtr();
907
908 if (line == 0) {
909 sb_error.SetErrorString("invalid line argument");
910 return sb_error;
911 }
912
913 if (!frame_sp) {
914 frame_sp = thread->GetSelectedFrame();
915 if (!frame_sp)
916 frame_sp = thread->GetStackFrameAtIndex(0);
917 }
918
919 SymbolContext frame_sc;
920 if (!frame_sp) {
921 sb_error.SetErrorString("no valid frames in thread to step");
922 return sb_error;
923 }
924
925 // If we have a frame, get its line
926 frame_sc = frame_sp->GetSymbolContext(
927 eSymbolContextCompUnit | eSymbolContextFunction |
928 eSymbolContextLineEntry | eSymbolContextSymbol);
929
930 if (frame_sc.comp_unit == NULL) {
931 sb_error.SetErrorStringWithFormat(
932 "frame %u doesn't have debug information", frame_sp->GetFrameIndex());
933 return sb_error;
934 }
935
936 FileSpec step_file_spec;
937 if (sb_file_spec.IsValid()) {
938 // The file spec passed in was valid, so use it
939 step_file_spec = sb_file_spec.ref();
940 } else {
941 if (frame_sc.line_entry.IsValid())
942 step_file_spec = frame_sc.line_entry.file;
943 else {
944 sb_error.SetErrorString("invalid file argument or no file for frame");
945 return sb_error;
946 }
947 }
948
949 // Grab the current function, then we will make sure the "until" address is
950 // within the function. We discard addresses that are out of the current
951 // function, and then if there are no addresses remaining, give an
Adrian Prantl05097242018-04-30 16:49:04 +0000952 // appropriate error message.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000953
954 bool all_in_function = true;
955 AddressRange fun_range = frame_sc.function->GetAddressRange();
956
957 std::vector<addr_t> step_over_until_addrs;
958 const bool abort_other_plans = false;
959 const bool stop_other_threads = false;
960 const bool check_inlines = true;
961 const bool exact = false;
962
963 SymbolContextList sc_list;
964 const uint32_t num_matches = frame_sc.comp_unit->ResolveSymbolContext(
965 step_file_spec, line, check_inlines, exact, eSymbolContextLineEntry,
966 sc_list);
967 if (num_matches > 0) {
968 SymbolContext sc;
969 for (uint32_t i = 0; i < num_matches; ++i) {
970 if (sc_list.GetContextAtIndex(i, sc)) {
971 addr_t step_addr =
972 sc.line_entry.range.GetBaseAddress().GetLoadAddress(target);
973 if (step_addr != LLDB_INVALID_ADDRESS) {
974 if (fun_range.ContainsLoadAddress(step_addr, target))
975 step_over_until_addrs.push_back(step_addr);
976 else
977 all_in_function = false;
978 }
979 }
980 }
981 }
982
983 if (step_over_until_addrs.empty()) {
984 if (all_in_function) {
985 step_file_spec.GetPath(path, sizeof(path));
986 sb_error.SetErrorStringWithFormat("No line entries for %s:%u", path,
987 line);
988 } else
989 sb_error.SetErrorString("step until target not in current function");
990 } else {
991 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepUntil(
992 abort_other_plans, &step_over_until_addrs[0],
993 step_over_until_addrs.size(), stop_other_threads,
994 frame_sp->GetFrameIndex()));
995
996 sb_error = ResumeNewPlan(exe_ctx, new_plan_sp.get());
997 }
998 } else {
999 sb_error.SetErrorString("this SBThread object is invalid");
1000 }
1001 return sb_error;
1002}
1003
1004SBError SBThread::StepUsingScriptedThreadPlan(const char *script_class_name) {
1005 return StepUsingScriptedThreadPlan(script_class_name, true);
1006}
1007
1008SBError SBThread::StepUsingScriptedThreadPlan(const char *script_class_name,
1009 bool resume_immediately) {
1010 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1011 SBError sb_error;
1012
1013 std::unique_lock<std::recursive_mutex> lock;
1014 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1015
1016 if (log) {
1017 log->Printf("SBThread(%p)::StepUsingScriptedThreadPlan: class name: %s",
1018 static_cast<void *>(exe_ctx.GetThreadPtr()), script_class_name);
1019 }
1020
1021 if (!exe_ctx.HasThreadScope()) {
1022 sb_error.SetErrorString("this SBThread object is invalid");
Richard Mittonf86248d2013-09-12 02:20:34 +00001023 return sb_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001024 }
Richard Mittonf86248d2013-09-12 02:20:34 +00001025
Kate Stoneb9c1b512016-09-06 20:57:50 +00001026 Thread *thread = exe_ctx.GetThreadPtr();
1027 ThreadPlanSP thread_plan_sp =
1028 thread->QueueThreadPlanForStepScripted(false, script_class_name, false);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001029
Kate Stoneb9c1b512016-09-06 20:57:50 +00001030 if (!thread_plan_sp) {
1031 sb_error.SetErrorStringWithFormat(
1032 "Error queueing thread plan for class: %s", script_class_name);
Jim Ingham44137582012-09-12 00:40:39 +00001033 return sb_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001034 }
Jim Ingham44137582012-09-12 00:40:39 +00001035
Kate Stoneb9c1b512016-09-06 20:57:50 +00001036 if (!resume_immediately) {
Jim Ingham4ac8e932016-07-08 02:12:05 +00001037 return sb_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001038 }
Jim Ingham4ac8e932016-07-08 02:12:05 +00001039
Kate Stoneb9c1b512016-09-06 20:57:50 +00001040 if (thread_plan_sp)
1041 sb_error = ResumeNewPlan(exe_ctx, thread_plan_sp.get());
1042 else {
1043 sb_error.SetErrorStringWithFormat(
1044 "Error resuming thread plan for class: %s.", script_class_name);
Greg Claytonc9858e42012-04-06 02:17:47 +00001045 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001046 log->Printf("SBThread(%p)::StepUsingScriptedThreadPlan: Error queuing "
1047 "thread plan for class: %s",
1048 static_cast<void *>(exe_ctx.GetThreadPtr()),
1049 script_class_name);
1050 }
1051
1052 return sb_error;
Greg Clayton722a0cd2011-01-12 02:25:42 +00001053}
1054
Kate Stoneb9c1b512016-09-06 20:57:50 +00001055SBError SBThread::JumpToLine(lldb::SBFileSpec &file_spec, uint32_t line) {
1056 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1057 SBError sb_error;
Jim Inghamb2e7d282016-06-10 17:22:26 +00001058
Kate Stoneb9c1b512016-09-06 20:57:50 +00001059 std::unique_lock<std::recursive_mutex> lock;
1060 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1061
1062 if (log)
1063 log->Printf("SBThread(%p)::JumpToLine (file+line = %s:%u)",
1064 static_cast<void *>(exe_ctx.GetThreadPtr()),
1065 file_spec->GetPath().c_str(), line);
1066
1067 if (!exe_ctx.HasThreadScope()) {
1068 sb_error.SetErrorString("this SBThread object is invalid");
1069 return sb_error;
1070 }
1071
1072 Thread *thread = exe_ctx.GetThreadPtr();
1073
Zachary Turner97206d52017-05-12 04:51:55 +00001074 Status err = thread->JumpToLine(file_spec.get(), line, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001075 sb_error.SetError(err);
1076 return sb_error;
Greg Clayton722a0cd2011-01-12 02:25:42 +00001077}
1078
Kate Stoneb9c1b512016-09-06 20:57:50 +00001079SBError SBThread::ReturnFromFrame(SBFrame &frame, SBValue &return_value) {
1080 SBError sb_error;
Jim Inghamb2e7d282016-06-10 17:22:26 +00001081
Kate Stoneb9c1b512016-09-06 20:57:50 +00001082 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1083
1084 std::unique_lock<std::recursive_mutex> lock;
1085 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1086
1087 if (log)
1088 log->Printf("SBThread(%p)::ReturnFromFrame (frame=%d)",
1089 static_cast<void *>(exe_ctx.GetThreadPtr()),
1090 frame.GetFrameID());
1091
1092 if (exe_ctx.HasThreadScope()) {
1093 Thread *thread = exe_ctx.GetThreadPtr();
1094 sb_error.SetError(
1095 thread->ReturnFromFrame(frame.GetFrameSP(), return_value.GetSP()));
1096 }
1097
1098 return sb_error;
Greg Clayton722a0cd2011-01-12 02:25:42 +00001099}
1100
Kate Stoneb9c1b512016-09-06 20:57:50 +00001101SBError SBThread::UnwindInnermostExpression() {
1102 SBError sb_error;
Jim Inghamb2e7d282016-06-10 17:22:26 +00001103
Kate Stoneb9c1b512016-09-06 20:57:50 +00001104 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1105
1106 std::unique_lock<std::recursive_mutex> lock;
1107 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1108
1109 if (log)
1110 log->Printf("SBThread(%p)::UnwindExpressionEvaluation",
1111 static_cast<void *>(exe_ctx.GetThreadPtr()));
1112
1113 if (exe_ctx.HasThreadScope()) {
1114 Thread *thread = exe_ctx.GetThreadPtr();
1115 sb_error.SetError(thread->UnwindInnermostExpression());
1116 if (sb_error.Success())
1117 thread->SetSelectedFrameByIndex(0, false);
1118 }
1119
1120 return sb_error;
Andrew Kaylora75418d2013-04-15 23:33:53 +00001121}
1122
Kate Stoneb9c1b512016-09-06 20:57:50 +00001123bool SBThread::Suspend() {
Alexander Polyakov859f54b2018-06-20 21:43:16 +00001124 SBError error; // Ignored
1125 return Suspend(error);
1126}
1127
1128bool SBThread::Suspend(SBError &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001129 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1130 std::unique_lock<std::recursive_mutex> lock;
1131 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Jim Inghamb2e7d282016-06-10 17:22:26 +00001132
Kate Stoneb9c1b512016-09-06 20:57:50 +00001133 bool result = false;
1134 if (exe_ctx.HasThreadScope()) {
1135 Process::StopLocker stop_locker;
1136 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) {
1137 exe_ctx.GetThreadPtr()->SetResumeState(eStateSuspended);
1138 result = true;
1139 } else {
Alexander Polyakov859f54b2018-06-20 21:43:16 +00001140 error.SetErrorString("process is running");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001141 if (log)
1142 log->Printf("SBThread(%p)::Suspend() => error: process is running",
1143 static_cast<void *>(exe_ctx.GetThreadPtr()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001144 }
Alexander Polyakov859f54b2018-06-20 21:43:16 +00001145 } else
1146 error.SetErrorString("this SBThread object is invalid");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001147 if (log)
1148 log->Printf("SBThread(%p)::Suspend() => %i",
1149 static_cast<void *>(exe_ctx.GetThreadPtr()), result);
1150 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001151}
1152
Kate Stoneb9c1b512016-09-06 20:57:50 +00001153bool SBThread::Resume() {
Alexander Polyakov859f54b2018-06-20 21:43:16 +00001154 SBError error; // Ignored
1155 return Resume(error);
1156}
1157
1158bool SBThread::Resume(SBError &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001159 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1160 std::unique_lock<std::recursive_mutex> lock;
1161 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001162
Kate Stoneb9c1b512016-09-06 20:57:50 +00001163 bool result = false;
1164 if (exe_ctx.HasThreadScope()) {
1165 Process::StopLocker stop_locker;
1166 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) {
1167 const bool override_suspend = true;
1168 exe_ctx.GetThreadPtr()->SetResumeState(eStateRunning, override_suspend);
1169 result = true;
1170 } else {
Alexander Polyakov859f54b2018-06-20 21:43:16 +00001171 error.SetErrorString("process is running");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001172 if (log)
1173 log->Printf("SBThread(%p)::Resume() => error: process is running",
1174 static_cast<void *>(exe_ctx.GetThreadPtr()));
Greg Claytonaf67cec2010-12-20 20:49:23 +00001175 }
Alexander Polyakov859f54b2018-06-20 21:43:16 +00001176 } else
1177 error.SetErrorString("this SBThread object is invalid");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001178 if (log)
1179 log->Printf("SBThread(%p)::Resume() => %i",
1180 static_cast<void *>(exe_ctx.GetThreadPtr()), result);
1181 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001182}
1183
Kate Stoneb9c1b512016-09-06 20:57:50 +00001184bool SBThread::IsSuspended() {
1185 std::unique_lock<std::recursive_mutex> lock;
1186 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001187
Kate Stoneb9c1b512016-09-06 20:57:50 +00001188 if (exe_ctx.HasThreadScope())
1189 return exe_ctx.GetThreadPtr()->GetResumeState() == eStateSuspended;
1190 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001191}
1192
Kate Stoneb9c1b512016-09-06 20:57:50 +00001193bool SBThread::IsStopped() {
1194 std::unique_lock<std::recursive_mutex> lock;
1195 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Greg Claytonf028a1f2010-12-17 02:26:24 +00001196
Kate Stoneb9c1b512016-09-06 20:57:50 +00001197 if (exe_ctx.HasThreadScope())
1198 return StateIsStoppedState(exe_ctx.GetThreadPtr()->GetState(), true);
1199 return false;
Greg Claytonf028a1f2010-12-17 02:26:24 +00001200}
1201
Kate Stoneb9c1b512016-09-06 20:57:50 +00001202SBProcess SBThread::GetProcess() {
1203 SBProcess sb_process;
1204 std::unique_lock<std::recursive_mutex> lock;
1205 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Greg Claytonf028a1f2010-12-17 02:26:24 +00001206
Kate Stoneb9c1b512016-09-06 20:57:50 +00001207 if (exe_ctx.HasThreadScope()) {
1208 // Have to go up to the target so we can get a shared pointer to our
1209 // process...
1210 sb_process.SetSP(exe_ctx.GetProcessSP());
1211 }
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001212
Kate Stoneb9c1b512016-09-06 20:57:50 +00001213 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1214 if (log) {
1215 SBStream frame_desc_strm;
1216 sb_process.GetDescription(frame_desc_strm);
1217 log->Printf("SBThread(%p)::GetProcess () => SBProcess(%p): %s",
1218 static_cast<void *>(exe_ctx.GetThreadPtr()),
1219 static_cast<void *>(sb_process.GetSP().get()),
1220 frame_desc_strm.GetData());
1221 }
1222
1223 return sb_process;
1224}
1225
1226uint32_t SBThread::GetNumFrames() {
1227 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1228
1229 uint32_t num_frames = 0;
1230 std::unique_lock<std::recursive_mutex> lock;
1231 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1232
1233 if (exe_ctx.HasThreadScope()) {
1234 Process::StopLocker stop_locker;
1235 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) {
1236 num_frames = exe_ctx.GetThreadPtr()->GetStackFrameCount();
1237 } else {
1238 if (log)
1239 log->Printf("SBThread(%p)::GetNumFrames() => error: process is running",
1240 static_cast<void *>(exe_ctx.GetThreadPtr()));
1241 }
1242 }
1243
1244 if (log)
1245 log->Printf("SBThread(%p)::GetNumFrames () => %u",
1246 static_cast<void *>(exe_ctx.GetThreadPtr()), num_frames);
1247
1248 return num_frames;
1249}
1250
1251SBFrame SBThread::GetFrameAtIndex(uint32_t idx) {
1252 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1253
1254 SBFrame sb_frame;
1255 StackFrameSP frame_sp;
1256 std::unique_lock<std::recursive_mutex> lock;
1257 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1258
1259 if (exe_ctx.HasThreadScope()) {
1260 Process::StopLocker stop_locker;
1261 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) {
1262 frame_sp = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(idx);
1263 sb_frame.SetFrameSP(frame_sp);
1264 } else {
1265 if (log)
1266 log->Printf(
1267 "SBThread(%p)::GetFrameAtIndex() => error: process is running",
1268 static_cast<void *>(exe_ctx.GetThreadPtr()));
1269 }
1270 }
1271
1272 if (log) {
1273 SBStream frame_desc_strm;
1274 sb_frame.GetDescription(frame_desc_strm);
1275 log->Printf("SBThread(%p)::GetFrameAtIndex (idx=%d) => SBFrame(%p): %s",
1276 static_cast<void *>(exe_ctx.GetThreadPtr()), idx,
1277 static_cast<void *>(frame_sp.get()), frame_desc_strm.GetData());
1278 }
1279
1280 return sb_frame;
1281}
1282
1283lldb::SBFrame SBThread::GetSelectedFrame() {
1284 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1285
1286 SBFrame sb_frame;
1287 StackFrameSP frame_sp;
1288 std::unique_lock<std::recursive_mutex> lock;
1289 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1290
1291 if (exe_ctx.HasThreadScope()) {
1292 Process::StopLocker stop_locker;
1293 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) {
1294 frame_sp = exe_ctx.GetThreadPtr()->GetSelectedFrame();
1295 sb_frame.SetFrameSP(frame_sp);
1296 } else {
1297 if (log)
1298 log->Printf(
1299 "SBThread(%p)::GetSelectedFrame() => error: process is running",
1300 static_cast<void *>(exe_ctx.GetThreadPtr()));
1301 }
1302 }
1303
1304 if (log) {
1305 SBStream frame_desc_strm;
1306 sb_frame.GetDescription(frame_desc_strm);
1307 log->Printf("SBThread(%p)::GetSelectedFrame () => SBFrame(%p): %s",
1308 static_cast<void *>(exe_ctx.GetThreadPtr()),
1309 static_cast<void *>(frame_sp.get()), frame_desc_strm.GetData());
1310 }
1311
1312 return sb_frame;
1313}
1314
1315lldb::SBFrame SBThread::SetSelectedFrame(uint32_t idx) {
1316 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1317
1318 SBFrame sb_frame;
1319 StackFrameSP frame_sp;
1320 std::unique_lock<std::recursive_mutex> lock;
1321 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1322
1323 if (exe_ctx.HasThreadScope()) {
1324 Process::StopLocker stop_locker;
1325 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) {
1326 Thread *thread = exe_ctx.GetThreadPtr();
1327 frame_sp = thread->GetStackFrameAtIndex(idx);
1328 if (frame_sp) {
1329 thread->SetSelectedFrame(frame_sp.get());
1330 sb_frame.SetFrameSP(frame_sp);
1331 }
1332 } else {
1333 if (log)
1334 log->Printf(
1335 "SBThread(%p)::SetSelectedFrame() => error: process is running",
1336 static_cast<void *>(exe_ctx.GetThreadPtr()));
1337 }
1338 }
1339
1340 if (log) {
1341 SBStream frame_desc_strm;
1342 sb_frame.GetDescription(frame_desc_strm);
1343 log->Printf("SBThread(%p)::SetSelectedFrame (idx=%u) => SBFrame(%p): %s",
1344 static_cast<void *>(exe_ctx.GetThreadPtr()), idx,
1345 static_cast<void *>(frame_sp.get()), frame_desc_strm.GetData());
1346 }
1347 return sb_frame;
1348}
1349
1350bool SBThread::EventIsThreadEvent(const SBEvent &event) {
1351 return Thread::ThreadEventData::GetEventDataFromEvent(event.get()) != NULL;
1352}
1353
1354SBFrame SBThread::GetStackFrameFromEvent(const SBEvent &event) {
1355 return Thread::ThreadEventData::GetStackFrameFromEvent(event.get());
1356}
1357
1358SBThread SBThread::GetThreadFromEvent(const SBEvent &event) {
1359 return Thread::ThreadEventData::GetThreadFromEvent(event.get());
1360}
1361
1362bool SBThread::operator==(const SBThread &rhs) const {
1363 return m_opaque_sp->GetThreadSP().get() ==
1364 rhs.m_opaque_sp->GetThreadSP().get();
1365}
1366
1367bool SBThread::operator!=(const SBThread &rhs) const {
1368 return m_opaque_sp->GetThreadSP().get() !=
1369 rhs.m_opaque_sp->GetThreadSP().get();
1370}
1371
1372bool SBThread::GetStatus(SBStream &status) const {
1373 Stream &strm = status.ref();
1374
1375 std::unique_lock<std::recursive_mutex> lock;
1376 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1377
1378 if (exe_ctx.HasThreadScope()) {
Jim Ingham6a9767c2016-11-08 20:36:40 +00001379 exe_ctx.GetThreadPtr()->GetStatus(strm, 0, 1, 1, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001380 } else
1381 strm.PutCString("No status");
1382
1383 return true;
1384}
1385
1386bool SBThread::GetDescription(SBStream &description) const {
Jim Ingham6a9767c2016-11-08 20:36:40 +00001387 return GetDescription(description, false);
1388}
1389
1390bool SBThread::GetDescription(SBStream &description, bool stop_format) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001391 Stream &strm = description.ref();
1392
1393 std::unique_lock<std::recursive_mutex> lock;
1394 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1395
1396 if (exe_ctx.HasThreadScope()) {
1397 exe_ctx.GetThreadPtr()->DumpUsingSettingsFormat(strm,
Jim Ingham6a9767c2016-11-08 20:36:40 +00001398 LLDB_INVALID_THREAD_ID,
1399 stop_format);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001400 // strm.Printf("SBThread: tid = 0x%4.4" PRIx64,
1401 // exe_ctx.GetThreadPtr()->GetID());
1402 } else
1403 strm.PutCString("No value");
1404
1405 return true;
1406}
1407
1408SBThread SBThread::GetExtendedBacktraceThread(const char *type) {
1409 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1410 std::unique_lock<std::recursive_mutex> lock;
1411 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1412 SBThread sb_origin_thread;
1413
1414 if (exe_ctx.HasThreadScope()) {
1415 Process::StopLocker stop_locker;
1416 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) {
1417 ThreadSP real_thread(exe_ctx.GetThreadSP());
1418 if (real_thread) {
1419 ConstString type_const(type);
1420 Process *process = exe_ctx.GetProcessPtr();
1421 if (process) {
1422 SystemRuntime *runtime = process->GetSystemRuntime();
1423 if (runtime) {
1424 ThreadSP new_thread_sp(
1425 runtime->GetExtendedBacktraceThread(real_thread, type_const));
1426 if (new_thread_sp) {
1427 // Save this in the Process' ExtendedThreadList so a strong
Adrian Prantl05097242018-04-30 16:49:04 +00001428 // pointer retains the object.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001429 process->GetExtendedThreadList().AddThread(new_thread_sp);
1430 sb_origin_thread.SetThread(new_thread_sp);
1431 if (log) {
1432 const char *queue_name = new_thread_sp->GetQueueName();
1433 if (queue_name == NULL)
1434 queue_name = "";
1435 log->Printf("SBThread(%p)::GetExtendedBacktraceThread() => new "
1436 "extended Thread "
1437 "created (%p) with queue_id 0x%" PRIx64
1438 " queue name '%s'",
1439 static_cast<void *>(exe_ctx.GetThreadPtr()),
1440 static_cast<void *>(new_thread_sp.get()),
1441 new_thread_sp->GetQueueID(), queue_name);
1442 }
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001443 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001444 }
Greg Claytonf028a1f2010-12-17 02:26:24 +00001445 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001446 }
1447 } else {
1448 if (log)
1449 log->Printf("SBThread(%p)::GetExtendedBacktraceThread() => error: "
1450 "process is running",
1451 static_cast<void *>(exe_ctx.GetThreadPtr()));
Greg Claytonf028a1f2010-12-17 02:26:24 +00001452 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001453 }
Greg Claytonf028a1f2010-12-17 02:26:24 +00001454
Kate Stoneb9c1b512016-09-06 20:57:50 +00001455 if (log && sb_origin_thread.IsValid() == false)
1456 log->Printf("SBThread(%p)::GetExtendedBacktraceThread() is not returning a "
1457 "Valid thread",
1458 static_cast<void *>(exe_ctx.GetThreadPtr()));
1459 return sb_origin_thread;
Greg Claytonf028a1f2010-12-17 02:26:24 +00001460}
1461
Kate Stoneb9c1b512016-09-06 20:57:50 +00001462uint32_t SBThread::GetExtendedBacktraceOriginatingIndexID() {
1463 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1464 if (thread_sp)
1465 return thread_sp->GetExtendedBacktraceOriginatingIndexID();
1466 return LLDB_INVALID_INDEX32;
Jim Ingham4f465cf2012-10-10 18:32:14 +00001467}
1468
Kate Stoneb9c1b512016-09-06 20:57:50 +00001469bool SBThread::SafeToCallFunctions() {
1470 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1471 if (thread_sp)
1472 return thread_sp->SafeToCallFunctions();
1473 return true;
Jim Ingham4f465cf2012-10-10 18:32:14 +00001474}
1475
Kate Stoneb9c1b512016-09-06 20:57:50 +00001476lldb_private::Thread *SBThread::operator->() {
1477 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1478 if (thread_sp)
1479 return thread_sp.get();
1480 else
1481 return NULL;
Jim Ingham4f465cf2012-10-10 18:32:14 +00001482}
Greg Claytonf028a1f2010-12-17 02:26:24 +00001483
Kate Stoneb9c1b512016-09-06 20:57:50 +00001484lldb_private::Thread *SBThread::get() {
1485 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1486 if (thread_sp)
1487 return thread_sp.get();
1488 else
1489 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001490}