Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- 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 Friedman | 7a62c8b | 2010-06-09 07:44:37 +0000 | [diff] [blame] | 10 | #include "lldb/API/SBThread.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 11 | |
| 12 | #include "lldb/API/SBSymbolContext.h" |
| 13 | #include "lldb/API/SBFileSpec.h" |
| 14 | #include "lldb/Core/Stream.h" |
| 15 | #include "lldb/Core/StreamFile.h" |
| 16 | #include "lldb/Target/Thread.h" |
| 17 | #include "lldb/Target/Process.h" |
| 18 | #include "lldb/Symbol/SymbolContext.h" |
| 19 | #include "lldb/Symbol/CompileUnit.h" |
| 20 | #include "lldb/Target/Target.h" |
| 21 | #include "lldb/Target/ThreadPlan.h" |
| 22 | #include "lldb/Target/ThreadPlanContinue.h" |
| 23 | #include "lldb/Target/ThreadPlanStepInstruction.h" |
| 24 | #include "lldb/Target/ThreadPlanStepOut.h" |
| 25 | #include "lldb/Target/ThreadPlanStepRange.h" |
| 26 | #include "lldb/Target/ThreadPlanStepInRange.h" |
| 27 | |
| 28 | |
Eli Friedman | 7a62c8b | 2010-06-09 07:44:37 +0000 | [diff] [blame] | 29 | #include "lldb/API/SBAddress.h" |
| 30 | #include "lldb/API/SBFrame.h" |
| 31 | #include "lldb/API/SBSourceManager.h" |
| 32 | #include "lldb/API/SBDebugger.h" |
| 33 | #include "lldb/API/SBProcess.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 34 | |
| 35 | using namespace lldb; |
| 36 | using namespace lldb_private; |
| 37 | |
| 38 | SBThread::SBThread () : |
| 39 | m_lldb_object_sp () |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | //---------------------------------------------------------------------- |
| 44 | // Thread constructor |
| 45 | //---------------------------------------------------------------------- |
| 46 | SBThread::SBThread (const ThreadSP& lldb_object_sp) : |
| 47 | m_lldb_object_sp (lldb_object_sp) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | SBThread::SBThread (const SBThread &rhs) |
| 52 | { |
| 53 | m_lldb_object_sp = rhs.m_lldb_object_sp; |
| 54 | } |
| 55 | |
| 56 | //---------------------------------------------------------------------- |
| 57 | // Destructor |
| 58 | //---------------------------------------------------------------------- |
| 59 | SBThread::~SBThread() |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | bool |
| 64 | SBThread::IsValid() const |
| 65 | { |
| 66 | return m_lldb_object_sp != NULL; |
| 67 | } |
| 68 | |
| 69 | StopReason |
| 70 | SBThread::GetStopReason() |
| 71 | { |
| 72 | if (m_lldb_object_sp) |
| 73 | { |
| 74 | lldb_private::Thread::StopInfo thread_stop_info; |
| 75 | if (m_lldb_object_sp->GetStopInfo(&thread_stop_info)) |
| 76 | return thread_stop_info.GetStopReason(); |
| 77 | } |
| 78 | return eStopReasonInvalid; |
| 79 | } |
| 80 | |
| 81 | size_t |
| 82 | SBThread::GetStopDescription (char *dst, size_t dst_len) |
| 83 | { |
| 84 | if (m_lldb_object_sp) |
| 85 | { |
| 86 | lldb_private::Thread::StopInfo thread_stop_info; |
| 87 | if (m_lldb_object_sp->GetStopInfo(&thread_stop_info)) |
| 88 | { |
| 89 | const char *stop_desc = thread_stop_info.GetStopDescription(); |
| 90 | if (stop_desc) |
| 91 | { |
| 92 | if (dst) |
| 93 | return ::snprintf (dst, dst_len, "%s", stop_desc); |
| 94 | else |
| 95 | { |
| 96 | // NULL dst passed in, return the length needed to contain the description |
| 97 | return ::strlen (stop_desc) + 1; // Include the NULL byte for size |
| 98 | } |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | const char *stop_desc = NULL; |
| 103 | size_t stop_desc_len = 0; |
| 104 | switch (thread_stop_info.GetStopReason()) |
| 105 | { |
| 106 | case eStopReasonTrace: |
| 107 | case eStopReasonPlanComplete: |
| 108 | { |
| 109 | static char trace_desc[] = "step"; |
| 110 | stop_desc = trace_desc; |
| 111 | stop_desc_len = sizeof(trace_desc); // Include the NULL byte for size |
| 112 | } |
| 113 | break; |
| 114 | |
| 115 | case eStopReasonBreakpoint: |
| 116 | { |
| 117 | static char bp_desc[] = "breakpoint hit"; |
| 118 | stop_desc = bp_desc; |
| 119 | stop_desc_len = sizeof(bp_desc); // Include the NULL byte for size |
| 120 | } |
| 121 | break; |
| 122 | |
| 123 | case eStopReasonWatchpoint: |
| 124 | { |
| 125 | static char wp_desc[] = "watchpoint hit"; |
| 126 | stop_desc = wp_desc; |
| 127 | stop_desc_len = sizeof(wp_desc); // Include the NULL byte for size |
| 128 | } |
| 129 | break; |
| 130 | |
| 131 | case eStopReasonSignal: |
| 132 | { |
| 133 | stop_desc = m_lldb_object_sp->GetProcess().GetUnixSignals ().GetSignalAsCString (thread_stop_info.GetSignal()); |
| 134 | if (stop_desc == NULL || stop_desc[0] == '\0') |
| 135 | { |
| 136 | static char signal_desc[] = "signal"; |
| 137 | stop_desc = signal_desc; |
| 138 | stop_desc_len = sizeof(signal_desc); // Include the NULL byte for size |
| 139 | } |
| 140 | } |
| 141 | break; |
| 142 | |
| 143 | case eStopReasonException: |
| 144 | { |
| 145 | char exc_desc[] = "exception"; |
| 146 | stop_desc = exc_desc; |
| 147 | stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size |
| 148 | } |
| 149 | break; |
| 150 | } |
| 151 | |
| 152 | if (stop_desc && stop_desc[0]) |
| 153 | { |
| 154 | if (dst) |
| 155 | return ::snprintf (dst, dst_len, "%s", stop_desc) + 1; // Include the NULL byte |
| 156 | |
| 157 | if (stop_desc_len == 0) |
| 158 | stop_desc_len = ::strlen (stop_desc) + 1; // Include the NULL byte |
| 159 | |
| 160 | return stop_desc_len; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | if (dst) |
| 166 | *dst = 0; |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | void |
| 171 | SBThread::SetThread (const ThreadSP& lldb_object_sp) |
| 172 | { |
| 173 | m_lldb_object_sp = lldb_object_sp; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | lldb::tid_t |
| 178 | SBThread::GetThreadID () const |
| 179 | { |
| 180 | if (m_lldb_object_sp) |
| 181 | return m_lldb_object_sp->GetID(); |
| 182 | else |
| 183 | return LLDB_INVALID_THREAD_ID; |
| 184 | } |
| 185 | |
| 186 | uint32_t |
| 187 | SBThread::GetIndexID () const |
| 188 | { |
| 189 | if (m_lldb_object_sp) |
| 190 | return m_lldb_object_sp->GetIndexID(); |
| 191 | return LLDB_INVALID_INDEX32; |
| 192 | } |
| 193 | const char * |
| 194 | SBThread::GetName () const |
| 195 | { |
| 196 | if (m_lldb_object_sp) |
| 197 | return m_lldb_object_sp->GetName(); |
| 198 | return NULL; |
| 199 | } |
| 200 | |
| 201 | const char * |
| 202 | SBThread::GetQueueName () const |
| 203 | { |
| 204 | if (m_lldb_object_sp) |
| 205 | return m_lldb_object_sp->GetQueueName(); |
| 206 | return NULL; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | void |
| 211 | SBThread::DisplayFramesForCurrentContext (FILE *out, |
| 212 | FILE *err, |
| 213 | uint32_t first_frame, |
| 214 | uint32_t num_frames, |
| 215 | bool show_frame_info, |
| 216 | uint32_t num_frames_with_source, |
| 217 | uint32_t source_lines_before, |
| 218 | uint32_t source_lines_after) |
| 219 | { |
| 220 | if ((out == NULL) || (err == NULL)) |
| 221 | return; |
| 222 | |
| 223 | if (m_lldb_object_sp) |
| 224 | { |
| 225 | uint32_t num_stack_frames = m_lldb_object_sp->GetStackFrameCount (); |
| 226 | StackFrameSP frame_sp; |
| 227 | int frame_idx = 0; |
| 228 | |
| 229 | for (frame_idx = first_frame; frame_idx < first_frame + num_frames; ++frame_idx) |
| 230 | { |
| 231 | if (frame_idx >= num_stack_frames) |
| 232 | break; |
| 233 | |
| 234 | frame_sp = m_lldb_object_sp->GetStackFrameAtIndex (frame_idx); |
| 235 | if (!frame_sp) |
| 236 | break; |
| 237 | |
| 238 | SBFrame sb_frame (frame_sp); |
| 239 | if (DisplaySingleFrameForCurrentContext (out, |
| 240 | err, |
| 241 | sb_frame, |
| 242 | show_frame_info, |
| 243 | num_frames_with_source > first_frame - frame_idx, |
| 244 | source_lines_before, |
| 245 | source_lines_after) == false) |
| 246 | break; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | bool |
| 252 | SBThread::DisplaySingleFrameForCurrentContext (FILE *out, |
| 253 | FILE *err, |
| 254 | SBFrame &frame, |
| 255 | bool show_frame_info, |
| 256 | bool show_source, |
| 257 | uint32_t source_lines_after, |
| 258 | uint32_t source_lines_before) |
| 259 | { |
| 260 | bool success = false; |
| 261 | |
| 262 | if ((out == NULL) || (err == NULL)) |
| 263 | return false; |
| 264 | |
| 265 | if (m_lldb_object_sp && frame.IsValid()) |
| 266 | { |
| 267 | |
| 268 | StreamFile str (out); |
| 269 | |
| 270 | SBSymbolContext sc(frame.GetSymbolContext(eSymbolContextEverything)); |
| 271 | |
| 272 | if (show_frame_info && sc.IsValid()) |
| 273 | { |
| 274 | user_id_t frame_idx = (user_id_t) frame.GetFrameID(); |
| 275 | lldb::addr_t pc = frame.GetPC(); |
| 276 | ::fprintf (out, |
| 277 | " frame #%u: tid = 0x%4.4x, pc = 0x%llx ", |
| 278 | frame_idx, |
| 279 | GetThreadID(), |
| 280 | pc); |
| 281 | sc->DumpStopContext (&str, &m_lldb_object_sp->GetProcess(), *frame.GetPCAddress()); |
| 282 | fprintf (out, "\n"); |
| 283 | success = true; |
| 284 | } |
| 285 | |
| 286 | SBCompileUnit comp_unit(sc.GetCompileUnit()); |
| 287 | if (show_source && comp_unit.IsValid()) |
| 288 | { |
| 289 | success = false; |
| 290 | SBLineEntry line_entry; |
| 291 | if (line_entry.IsValid()) |
| 292 | { |
| 293 | SBSourceManager& source_manager = SBDebugger::GetSourceManager(); |
| 294 | SBFileSpec line_entry_file_spec = line_entry.GetFileSpec(); |
| 295 | |
| 296 | if (line_entry_file_spec.IsValid()) |
| 297 | { |
| 298 | source_manager.DisplaySourceLinesWithLineNumbers (line_entry_file_spec, |
| 299 | line_entry.GetLine(), |
| 300 | source_lines_after, |
| 301 | source_lines_before, "->", |
| 302 | out); |
| 303 | success = true; |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | return success; |
| 309 | } |
| 310 | |
| 311 | void |
| 312 | SBThread::StepOver (lldb::RunMode stop_other_threads) |
| 313 | { |
| 314 | if (m_lldb_object_sp) |
| 315 | { |
| 316 | bool abort_other_plans = true; |
| 317 | StackFrameSP frame_sp(m_lldb_object_sp->GetStackFrameAtIndex (0)); |
| 318 | |
| 319 | if (frame_sp) |
| 320 | { |
| 321 | if (frame_sp->HasDebugInformation ()) |
| 322 | { |
| 323 | SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything)); |
| 324 | m_lldb_object_sp->QueueThreadPlanForStepRange (abort_other_plans, |
| 325 | eStepTypeOver, |
| 326 | sc.line_entry.range, |
| 327 | sc, |
Greg Clayton | 8f5fd6b | 2010-06-12 18:59:55 +0000 | [diff] [blame^] | 328 | stop_other_threads, |
| 329 | false); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 330 | |
| 331 | } |
| 332 | else |
| 333 | { |
| 334 | m_lldb_object_sp->QueueThreadPlanForStepSingleInstruction (true, |
| 335 | abort_other_plans, |
| 336 | stop_other_threads); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | Process &process = m_lldb_object_sp->GetProcess(); |
| 341 | // Why do we need to set the current thread by ID here??? |
| 342 | process.GetThreadList().SetCurrentThreadByID (m_lldb_object_sp->GetID()); |
| 343 | process.Resume(); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | void |
| 348 | SBThread::StepInto (lldb::RunMode stop_other_threads) |
| 349 | { |
| 350 | if (m_lldb_object_sp) |
| 351 | { |
| 352 | bool abort_other_plans = true; |
| 353 | |
| 354 | StackFrameSP frame_sp(m_lldb_object_sp->GetStackFrameAtIndex (0)); |
| 355 | |
| 356 | if (frame_sp && frame_sp->HasDebugInformation ()) |
| 357 | { |
Greg Clayton | 8f5fd6b | 2010-06-12 18:59:55 +0000 | [diff] [blame^] | 358 | bool avoid_code_without_debug_info = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 359 | SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything)); |
Greg Clayton | 8f5fd6b | 2010-06-12 18:59:55 +0000 | [diff] [blame^] | 360 | m_lldb_object_sp->QueueThreadPlanForStepRange (abort_other_plans, |
| 361 | eStepTypeInto, |
| 362 | sc.line_entry.range, |
| 363 | sc, |
| 364 | stop_other_threads, |
| 365 | avoid_code_without_debug_info); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 366 | } |
| 367 | else |
| 368 | { |
| 369 | m_lldb_object_sp->QueueThreadPlanForStepSingleInstruction (false, |
| 370 | abort_other_plans, |
| 371 | stop_other_threads); |
| 372 | } |
| 373 | |
| 374 | Process &process = m_lldb_object_sp->GetProcess(); |
| 375 | // Why do we need to set the current thread by ID here??? |
| 376 | process.GetThreadList().SetCurrentThreadByID (m_lldb_object_sp->GetID()); |
| 377 | process.Resume(); |
| 378 | |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | void |
| 383 | SBThread::StepOut () |
| 384 | { |
| 385 | if (m_lldb_object_sp) |
| 386 | { |
| 387 | bool abort_other_plans = true; |
| 388 | bool stop_other_threads = true; |
| 389 | |
| 390 | m_lldb_object_sp->QueueThreadPlanForStepOut (abort_other_plans, NULL, false, stop_other_threads, eVoteYes, eVoteNoOpinion); |
| 391 | |
| 392 | Process &process = m_lldb_object_sp->GetProcess(); |
| 393 | process.GetThreadList().SetCurrentThreadByID (m_lldb_object_sp->GetID()); |
| 394 | process.Resume(); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | void |
| 399 | SBThread::StepInstruction (bool step_over) |
| 400 | { |
| 401 | if (m_lldb_object_sp) |
| 402 | { |
| 403 | m_lldb_object_sp->QueueThreadPlanForStepSingleInstruction (step_over, true, true); |
| 404 | Process &process = m_lldb_object_sp->GetProcess(); |
| 405 | process.GetThreadList().SetCurrentThreadByID (m_lldb_object_sp->GetID()); |
| 406 | process.Resume(); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | void |
| 411 | SBThread::RunToAddress (lldb::addr_t addr) |
| 412 | { |
| 413 | if (m_lldb_object_sp) |
| 414 | { |
| 415 | bool abort_other_plans = true; |
| 416 | bool stop_other_threads = true; |
| 417 | |
| 418 | Address target_addr (NULL, addr); |
| 419 | |
| 420 | m_lldb_object_sp->QueueThreadPlanForRunToAddress (abort_other_plans, target_addr, stop_other_threads); |
| 421 | Process &process = m_lldb_object_sp->GetProcess(); |
| 422 | process.GetThreadList().SetCurrentThreadByID (m_lldb_object_sp->GetID()); |
| 423 | process.Resume(); |
| 424 | } |
| 425 | |
| 426 | } |
| 427 | |
| 428 | void |
| 429 | SBThread::Backtrace (uint32_t num_frames) |
| 430 | { |
| 431 | bool all_frames = false; |
| 432 | if (num_frames < 1) |
| 433 | all_frames = true; |
| 434 | |
| 435 | FILE *out = SBDebugger::GetOutputFileHandle(); |
| 436 | FILE *err = SBDebugger::GetErrorFileHandle(); |
| 437 | |
| 438 | if ((out == NULL) || (err == NULL)) |
| 439 | return; |
| 440 | |
| 441 | if (m_lldb_object_sp) |
| 442 | { |
| 443 | if (out && err) |
| 444 | { |
| 445 | int max_num_frames = m_lldb_object_sp->GetStackFrameCount(); |
| 446 | int last_frame = max_num_frames; |
| 447 | |
| 448 | if (!all_frames && (num_frames < last_frame)) |
| 449 | last_frame = num_frames; |
| 450 | |
| 451 | StackFrameSP frame_sp; |
| 452 | for (int i = 0; i < last_frame; ++i) |
| 453 | { |
| 454 | frame_sp = m_lldb_object_sp->GetStackFrameAtIndex (i); |
| 455 | if (!frame_sp) |
| 456 | break; |
| 457 | |
| 458 | SBFrame sb_frame (frame_sp); |
| 459 | if (DisplaySingleFrameForCurrentContext ((FILE *) out, (FILE *) err, sb_frame, true, false, 0, 0) == false) |
| 460 | break; |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | SBProcess |
| 467 | SBThread::GetProcess () |
| 468 | { |
| 469 | SBProcess process; |
| 470 | if (m_lldb_object_sp) |
| 471 | { |
| 472 | // Have to go up to the target so we can get a shared pointer to our process... |
| 473 | process.SetProcess(m_lldb_object_sp->GetProcess().GetTarget().GetProcessSP()); |
| 474 | } |
| 475 | return process; |
| 476 | } |
| 477 | |
| 478 | uint32_t |
| 479 | SBThread::GetNumFrames () |
| 480 | { |
| 481 | if (m_lldb_object_sp) |
| 482 | return m_lldb_object_sp->GetStackFrameCount(); |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | SBFrame |
| 487 | SBThread::GetFrameAtIndex (uint32_t idx) |
| 488 | { |
| 489 | SBFrame sb_frame; |
| 490 | if (m_lldb_object_sp) |
| 491 | sb_frame.SetFrame (m_lldb_object_sp->GetStackFrameAtIndex (idx)); |
| 492 | return sb_frame; |
| 493 | } |
| 494 | |
| 495 | const lldb::SBThread & |
| 496 | SBThread::operator = (const lldb::SBThread &rhs) |
| 497 | { |
| 498 | m_lldb_object_sp = rhs.m_lldb_object_sp; |
| 499 | return *this; |
| 500 | } |
| 501 | |
| 502 | bool |
| 503 | SBThread::operator == (const SBThread &rhs) const |
| 504 | { |
| 505 | return m_lldb_object_sp.get() == rhs.m_lldb_object_sp.get(); |
| 506 | } |
| 507 | |
| 508 | bool |
| 509 | SBThread::operator != (const SBThread &rhs) const |
| 510 | { |
| 511 | return m_lldb_object_sp.get() != rhs.m_lldb_object_sp.get(); |
| 512 | } |
| 513 | |
| 514 | lldb_private::Thread * |
| 515 | SBThread::GetLLDBObjectPtr () |
| 516 | { |
| 517 | return m_lldb_object_sp.get(); |
| 518 | } |
| 519 | |
| 520 | const lldb_private::Thread * |
| 521 | SBThread::operator->() const |
| 522 | { |
| 523 | return m_lldb_object_sp.get(); |
| 524 | } |
| 525 | |
| 526 | const lldb_private::Thread & |
| 527 | SBThread::operator*() const |
| 528 | { |
| 529 | return *m_lldb_object_sp; |
| 530 | } |
| 531 | |
| 532 | lldb_private::Thread * |
| 533 | SBThread::operator->() |
| 534 | { |
| 535 | return m_lldb_object_sp.get(); |
| 536 | } |
| 537 | |
| 538 | lldb_private::Thread & |
| 539 | SBThread::operator*() |
| 540 | { |
| 541 | return *m_lldb_object_sp; |
| 542 | } |