1 //===-- Debugger.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 10 #include "lldb/Core/Debugger.h" 11 12 #include <map> 13 14 #include "clang/AST/DeclCXX.h" 15 #include "clang/AST/Type.h" 16 17 #include "lldb/lldb-private.h" 18 #include "lldb/Core/ConnectionFileDescriptor.h" 19 #include "lldb/Core/FormatManager.h" 20 #include "lldb/Core/InputReader.h" 21 #include "lldb/Core/RegisterValue.h" 22 #include "lldb/Core/State.h" 23 #include "lldb/Core/StreamAsynchronousIO.h" 24 #include "lldb/Core/StreamString.h" 25 #include "lldb/Core/Timer.h" 26 #include "lldb/Core/ValueObject.h" 27 #include "lldb/Core/ValueObjectVariable.h" 28 #include "lldb/Host/Terminal.h" 29 #include "lldb/Interpreter/CommandInterpreter.h" 30 #include "lldb/Symbol/VariableList.h" 31 #include "lldb/Target/TargetList.h" 32 #include "lldb/Target/Process.h" 33 #include "lldb/Target/RegisterContext.h" 34 #include "lldb/Target/StopInfo.h" 35 #include "lldb/Target/Thread.h" 36 #include "lldb/Utility/AnsiTerminal.h" 37 38 using namespace lldb; 39 using namespace lldb_private; 40 41 42 static uint32_t g_shared_debugger_refcount = 0; 43 static lldb::user_id_t g_unique_id = 1; 44 45 #pragma mark Static Functions 46 47 static Mutex & 48 GetDebuggerListMutex () 49 { 50 static Mutex g_mutex(Mutex::eMutexTypeRecursive); 51 return g_mutex; 52 } 53 54 typedef std::vector<DebuggerSP> DebuggerList; 55 56 static DebuggerList & 57 GetDebuggerList() 58 { 59 // hide the static debugger list inside a singleton accessor to avoid 60 // global init contructors 61 static DebuggerList g_list; 62 return g_list; 63 } 64 65 66 static const ConstString & 67 PromptVarName () 68 { 69 static ConstString g_const_string ("prompt"); 70 return g_const_string; 71 } 72 73 static const ConstString & 74 GetFrameFormatName () 75 { 76 static ConstString g_const_string ("frame-format"); 77 return g_const_string; 78 } 79 80 static const ConstString & 81 GetThreadFormatName () 82 { 83 static ConstString g_const_string ("thread-format"); 84 return g_const_string; 85 } 86 87 static const ConstString & 88 ScriptLangVarName () 89 { 90 static ConstString g_const_string ("script-lang"); 91 return g_const_string; 92 } 93 94 static const ConstString & 95 TermWidthVarName () 96 { 97 static ConstString g_const_string ("term-width"); 98 return g_const_string; 99 } 100 101 static const ConstString & 102 UseExternalEditorVarName () 103 { 104 static ConstString g_const_string ("use-external-editor"); 105 return g_const_string; 106 } 107 108 static const ConstString & 109 AutoConfirmName () 110 { 111 static ConstString g_const_string ("auto-confirm"); 112 return g_const_string; 113 } 114 115 static const ConstString & 116 StopSourceContextBeforeName () 117 { 118 static ConstString g_const_string ("stop-line-count-before"); 119 return g_const_string; 120 } 121 122 static const ConstString & 123 StopSourceContextAfterName () 124 { 125 static ConstString g_const_string ("stop-line-count-after"); 126 return g_const_string; 127 } 128 129 static const ConstString & 130 StopDisassemblyCountName () 131 { 132 static ConstString g_const_string ("stop-disassembly-count"); 133 return g_const_string; 134 } 135 136 static const ConstString & 137 StopDisassemblyDisplayName () 138 { 139 static ConstString g_const_string ("stop-disassembly-display"); 140 return g_const_string; 141 } 142 143 OptionEnumValueElement 144 DebuggerInstanceSettings::g_show_disassembly_enum_values[] = 145 { 146 { eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."}, 147 { eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."}, 148 { eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."}, 149 { 0, NULL, NULL } 150 }; 151 152 153 154 #pragma mark Debugger 155 156 UserSettingsControllerSP & 157 Debugger::GetSettingsController () 158 { 159 static UserSettingsControllerSP g_settings_controller; 160 return g_settings_controller; 161 } 162 163 int 164 Debugger::TestDebuggerRefCount () 165 { 166 return g_shared_debugger_refcount; 167 } 168 169 void 170 Debugger::Initialize () 171 { 172 if (g_shared_debugger_refcount == 0) 173 { 174 lldb_private::Initialize(); 175 } 176 g_shared_debugger_refcount++; 177 178 } 179 180 void 181 Debugger::Terminate () 182 { 183 if (g_shared_debugger_refcount > 0) 184 { 185 g_shared_debugger_refcount--; 186 if (g_shared_debugger_refcount == 0) 187 { 188 lldb_private::WillTerminate(); 189 lldb_private::Terminate(); 190 191 // Clear our master list of debugger objects 192 Mutex::Locker locker (GetDebuggerListMutex ()); 193 GetDebuggerList().clear(); 194 } 195 } 196 } 197 198 void 199 Debugger::SettingsInitialize () 200 { 201 static bool g_initialized = false; 202 203 if (!g_initialized) 204 { 205 g_initialized = true; 206 UserSettingsControllerSP &usc = GetSettingsController(); 207 usc.reset (new SettingsController); 208 UserSettingsController::InitializeSettingsController (usc, 209 SettingsController::global_settings_table, 210 SettingsController::instance_settings_table); 211 // Now call SettingsInitialize for each settings 'child' of Debugger 212 Target::SettingsInitialize (); 213 } 214 } 215 216 void 217 Debugger::SettingsTerminate () 218 { 219 220 // Must call SettingsTerminate() for each settings 'child' of Debugger, before terminating the Debugger's 221 // Settings. 222 223 Target::SettingsTerminate (); 224 225 // Now terminate the Debugger Settings. 226 227 UserSettingsControllerSP &usc = GetSettingsController(); 228 UserSettingsController::FinalizeSettingsController (usc); 229 usc.reset(); 230 } 231 232 DebuggerSP 233 Debugger::CreateInstance () 234 { 235 DebuggerSP debugger_sp (new Debugger); 236 // Scope for locker 237 { 238 Mutex::Locker locker (GetDebuggerListMutex ()); 239 GetDebuggerList().push_back(debugger_sp); 240 } 241 return debugger_sp; 242 } 243 244 void 245 Debugger::Destroy (DebuggerSP &debugger_sp) 246 { 247 if (debugger_sp.get() == NULL) 248 return; 249 250 debugger_sp->Clear(); 251 252 Mutex::Locker locker (GetDebuggerListMutex ()); 253 DebuggerList &debugger_list = GetDebuggerList (); 254 DebuggerList::iterator pos, end = debugger_list.end(); 255 for (pos = debugger_list.begin (); pos != end; ++pos) 256 { 257 if ((*pos).get() == debugger_sp.get()) 258 { 259 debugger_list.erase (pos); 260 return; 261 } 262 } 263 } 264 265 DebuggerSP 266 Debugger::GetSP () 267 { 268 // This object contains an instrusive ref count base class so we can 269 // easily make a shared pointer to this object 270 return DebuggerSP (this); 271 } 272 273 DebuggerSP 274 Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name) 275 { 276 DebuggerSP debugger_sp; 277 278 Mutex::Locker locker (GetDebuggerListMutex ()); 279 DebuggerList &debugger_list = GetDebuggerList(); 280 DebuggerList::iterator pos, end = debugger_list.end(); 281 282 for (pos = debugger_list.begin(); pos != end; ++pos) 283 { 284 if ((*pos).get()->m_instance_name == instance_name) 285 { 286 debugger_sp = *pos; 287 break; 288 } 289 } 290 return debugger_sp; 291 } 292 293 TargetSP 294 Debugger::FindTargetWithProcessID (lldb::pid_t pid) 295 { 296 TargetSP target_sp; 297 Mutex::Locker locker (GetDebuggerListMutex ()); 298 DebuggerList &debugger_list = GetDebuggerList(); 299 DebuggerList::iterator pos, end = debugger_list.end(); 300 for (pos = debugger_list.begin(); pos != end; ++pos) 301 { 302 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid); 303 if (target_sp) 304 break; 305 } 306 return target_sp; 307 } 308 309 TargetSP 310 Debugger::FindTargetWithProcess (Process *process) 311 { 312 TargetSP target_sp; 313 Mutex::Locker locker (GetDebuggerListMutex ()); 314 DebuggerList &debugger_list = GetDebuggerList(); 315 DebuggerList::iterator pos, end = debugger_list.end(); 316 for (pos = debugger_list.begin(); pos != end; ++pos) 317 { 318 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process); 319 if (target_sp) 320 break; 321 } 322 return target_sp; 323 } 324 325 326 Debugger::Debugger () : 327 UserID (g_unique_id++), 328 DebuggerInstanceSettings (*GetSettingsController()), 329 m_input_comm("debugger.input"), 330 m_input_file (), 331 m_output_file (), 332 m_error_file (), 333 m_target_list (), 334 m_platform_list (), 335 m_listener ("lldb.Debugger"), 336 m_source_manager(*this), 337 m_source_file_cache(), 338 m_command_interpreter_ap (new CommandInterpreter (*this, eScriptLanguageDefault, false)), 339 m_input_reader_stack (), 340 m_input_reader_data () 341 { 342 m_command_interpreter_ap->Initialize (); 343 // Always add our default platform to the platform list 344 PlatformSP default_platform_sp (Platform::GetDefaultPlatform()); 345 assert (default_platform_sp.get()); 346 m_platform_list.Append (default_platform_sp, true); 347 } 348 349 Debugger::~Debugger () 350 { 351 Clear(); 352 } 353 354 void 355 Debugger::Clear() 356 { 357 CleanUpInputReaders(); 358 m_listener.Clear(); 359 int num_targets = m_target_list.GetNumTargets(); 360 for (int i = 0; i < num_targets; i++) 361 { 362 TargetSP target_sp (m_target_list.GetTargetAtIndex (i)); 363 if (target_sp) 364 { 365 ProcessSP process_sp (target_sp->GetProcessSP()); 366 if (process_sp) 367 { 368 if (process_sp->GetShouldDetach()) 369 process_sp->Detach(); 370 } 371 target_sp->Destroy(); 372 } 373 } 374 DisconnectInput(); 375 376 } 377 378 bool 379 Debugger::GetCloseInputOnEOF () const 380 { 381 return m_input_comm.GetCloseOnEOF(); 382 } 383 384 void 385 Debugger::SetCloseInputOnEOF (bool b) 386 { 387 m_input_comm.SetCloseOnEOF(b); 388 } 389 390 bool 391 Debugger::GetAsyncExecution () 392 { 393 return !m_command_interpreter_ap->GetSynchronous(); 394 } 395 396 void 397 Debugger::SetAsyncExecution (bool async_execution) 398 { 399 m_command_interpreter_ap->SetSynchronous (!async_execution); 400 } 401 402 403 void 404 Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership) 405 { 406 File &in_file = GetInputFile(); 407 in_file.SetStream (fh, tranfer_ownership); 408 if (in_file.IsValid() == false) 409 in_file.SetStream (stdin, true); 410 411 // Disconnect from any old connection if we had one 412 m_input_comm.Disconnect (); 413 // Pass false as the second argument to ConnectionFileDescriptor below because 414 // our "in_file" above will already take ownership if requested and we don't 415 // want to objects trying to own and close a file descriptor. 416 m_input_comm.SetConnection (new ConnectionFileDescriptor (in_file.GetDescriptor(), false)); 417 m_input_comm.SetReadThreadBytesReceivedCallback (Debugger::DispatchInputCallback, this); 418 419 Error error; 420 if (m_input_comm.StartReadThread (&error) == false) 421 { 422 File &err_file = GetErrorFile(); 423 424 err_file.Printf ("error: failed to main input read thread: %s", error.AsCString() ? error.AsCString() : "unkown error"); 425 exit(1); 426 } 427 } 428 429 void 430 Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership) 431 { 432 File &out_file = GetOutputFile(); 433 out_file.SetStream (fh, tranfer_ownership); 434 if (out_file.IsValid() == false) 435 out_file.SetStream (stdout, false); 436 437 GetCommandInterpreter().GetScriptInterpreter()->ResetOutputFileHandle (fh); 438 } 439 440 void 441 Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership) 442 { 443 File &err_file = GetErrorFile(); 444 err_file.SetStream (fh, tranfer_ownership); 445 if (err_file.IsValid() == false) 446 err_file.SetStream (stderr, false); 447 } 448 449 ExecutionContext 450 Debugger::GetSelectedExecutionContext () 451 { 452 ExecutionContext exe_ctx; 453 TargetSP target_sp(GetSelectedTarget()); 454 exe_ctx.SetTargetSP (target_sp); 455 456 if (target_sp) 457 { 458 ProcessSP process_sp (target_sp->GetProcessSP()); 459 exe_ctx.SetProcessSP (process_sp); 460 if (process_sp && process_sp->IsRunning() == false) 461 { 462 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread()); 463 if (thread_sp) 464 { 465 exe_ctx.SetThreadSP (thread_sp); 466 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame()); 467 if (exe_ctx.GetFramePtr() == NULL) 468 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0)); 469 } 470 } 471 } 472 return exe_ctx; 473 474 } 475 476 InputReaderSP 477 Debugger::GetCurrentInputReader () 478 { 479 InputReaderSP reader_sp; 480 481 if (!m_input_reader_stack.IsEmpty()) 482 { 483 // Clear any finished readers from the stack 484 while (CheckIfTopInputReaderIsDone()) ; 485 486 if (!m_input_reader_stack.IsEmpty()) 487 reader_sp = m_input_reader_stack.Top(); 488 } 489 490 return reader_sp; 491 } 492 493 void 494 Debugger::DispatchInputCallback (void *baton, const void *bytes, size_t bytes_len) 495 { 496 if (bytes_len > 0) 497 ((Debugger *)baton)->DispatchInput ((char *)bytes, bytes_len); 498 else 499 ((Debugger *)baton)->DispatchInputEndOfFile (); 500 } 501 502 503 void 504 Debugger::DispatchInput (const char *bytes, size_t bytes_len) 505 { 506 if (bytes == NULL || bytes_len == 0) 507 return; 508 509 WriteToDefaultReader (bytes, bytes_len); 510 } 511 512 void 513 Debugger::DispatchInputInterrupt () 514 { 515 m_input_reader_data.clear(); 516 517 InputReaderSP reader_sp (GetCurrentInputReader ()); 518 if (reader_sp) 519 { 520 reader_sp->Notify (eInputReaderInterrupt); 521 522 // If notifying the reader of the interrupt finished the reader, we should pop it off the stack. 523 while (CheckIfTopInputReaderIsDone ()) ; 524 } 525 } 526 527 void 528 Debugger::DispatchInputEndOfFile () 529 { 530 m_input_reader_data.clear(); 531 532 InputReaderSP reader_sp (GetCurrentInputReader ()); 533 if (reader_sp) 534 { 535 reader_sp->Notify (eInputReaderEndOfFile); 536 537 // If notifying the reader of the end-of-file finished the reader, we should pop it off the stack. 538 while (CheckIfTopInputReaderIsDone ()) ; 539 } 540 } 541 542 void 543 Debugger::CleanUpInputReaders () 544 { 545 m_input_reader_data.clear(); 546 547 // The bottom input reader should be the main debugger input reader. We do not want to close that one here. 548 while (m_input_reader_stack.GetSize() > 1) 549 { 550 InputReaderSP reader_sp (GetCurrentInputReader ()); 551 if (reader_sp) 552 { 553 reader_sp->Notify (eInputReaderEndOfFile); 554 reader_sp->SetIsDone (true); 555 } 556 } 557 } 558 559 void 560 Debugger::NotifyTopInputReader (InputReaderAction notification) 561 { 562 InputReaderSP reader_sp (GetCurrentInputReader()); 563 if (reader_sp) 564 { 565 reader_sp->Notify (notification); 566 567 // Flush out any input readers that are done. 568 while (CheckIfTopInputReaderIsDone ()) 569 /* Do nothing. */; 570 } 571 } 572 573 bool 574 Debugger::InputReaderIsTopReader (const InputReaderSP& reader_sp) 575 { 576 InputReaderSP top_reader_sp (GetCurrentInputReader()); 577 578 return (reader_sp.get() == top_reader_sp.get()); 579 } 580 581 582 void 583 Debugger::WriteToDefaultReader (const char *bytes, size_t bytes_len) 584 { 585 if (bytes && bytes_len) 586 m_input_reader_data.append (bytes, bytes_len); 587 588 if (m_input_reader_data.empty()) 589 return; 590 591 while (!m_input_reader_stack.IsEmpty() && !m_input_reader_data.empty()) 592 { 593 // Get the input reader from the top of the stack 594 InputReaderSP reader_sp (GetCurrentInputReader ()); 595 if (!reader_sp) 596 break; 597 598 size_t bytes_handled = reader_sp->HandleRawBytes (m_input_reader_data.c_str(), 599 m_input_reader_data.size()); 600 if (bytes_handled) 601 { 602 m_input_reader_data.erase (0, bytes_handled); 603 } 604 else 605 { 606 // No bytes were handled, we might not have reached our 607 // granularity, just return and wait for more data 608 break; 609 } 610 } 611 612 // Flush out any input readers that are done. 613 while (CheckIfTopInputReaderIsDone ()) 614 /* Do nothing. */; 615 616 } 617 618 void 619 Debugger::PushInputReader (const InputReaderSP& reader_sp) 620 { 621 if (!reader_sp) 622 return; 623 624 // Deactivate the old top reader 625 InputReaderSP top_reader_sp (GetCurrentInputReader ()); 626 627 if (top_reader_sp) 628 top_reader_sp->Notify (eInputReaderDeactivate); 629 630 m_input_reader_stack.Push (reader_sp); 631 reader_sp->Notify (eInputReaderActivate); 632 ActivateInputReader (reader_sp); 633 } 634 635 bool 636 Debugger::PopInputReader (const InputReaderSP& pop_reader_sp) 637 { 638 bool result = false; 639 640 // The reader on the stop of the stack is done, so let the next 641 // read on the stack referesh its prompt and if there is one... 642 if (!m_input_reader_stack.IsEmpty()) 643 { 644 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop. 645 InputReaderSP reader_sp(m_input_reader_stack.Top()); 646 647 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get()) 648 { 649 m_input_reader_stack.Pop (); 650 reader_sp->Notify (eInputReaderDeactivate); 651 reader_sp->Notify (eInputReaderDone); 652 result = true; 653 654 if (!m_input_reader_stack.IsEmpty()) 655 { 656 reader_sp = m_input_reader_stack.Top(); 657 if (reader_sp) 658 { 659 ActivateInputReader (reader_sp); 660 reader_sp->Notify (eInputReaderReactivate); 661 } 662 } 663 } 664 } 665 return result; 666 } 667 668 bool 669 Debugger::CheckIfTopInputReaderIsDone () 670 { 671 bool result = false; 672 if (!m_input_reader_stack.IsEmpty()) 673 { 674 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop. 675 InputReaderSP reader_sp(m_input_reader_stack.Top()); 676 677 if (reader_sp && reader_sp->IsDone()) 678 { 679 result = true; 680 PopInputReader (reader_sp); 681 } 682 } 683 return result; 684 } 685 686 void 687 Debugger::ActivateInputReader (const InputReaderSP &reader_sp) 688 { 689 int input_fd = m_input_file.GetFile().GetDescriptor(); 690 691 if (input_fd >= 0) 692 { 693 Terminal tty(input_fd); 694 695 tty.SetEcho(reader_sp->GetEcho()); 696 697 switch (reader_sp->GetGranularity()) 698 { 699 case eInputReaderGranularityByte: 700 case eInputReaderGranularityWord: 701 tty.SetCanonical (false); 702 break; 703 704 case eInputReaderGranularityLine: 705 case eInputReaderGranularityAll: 706 tty.SetCanonical (true); 707 break; 708 709 default: 710 break; 711 } 712 } 713 } 714 715 StreamSP 716 Debugger::GetAsyncOutputStream () 717 { 718 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(), 719 CommandInterpreter::eBroadcastBitAsynchronousOutputData)); 720 } 721 722 StreamSP 723 Debugger::GetAsyncErrorStream () 724 { 725 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(), 726 CommandInterpreter::eBroadcastBitAsynchronousErrorData)); 727 } 728 729 DebuggerSP 730 Debugger::FindDebuggerWithID (lldb::user_id_t id) 731 { 732 DebuggerSP debugger_sp; 733 734 Mutex::Locker locker (GetDebuggerListMutex ()); 735 DebuggerList &debugger_list = GetDebuggerList(); 736 DebuggerList::iterator pos, end = debugger_list.end(); 737 for (pos = debugger_list.begin(); pos != end; ++pos) 738 { 739 if ((*pos).get()->GetID() == id) 740 { 741 debugger_sp = *pos; 742 break; 743 } 744 } 745 return debugger_sp; 746 } 747 748 static void 749 TestPromptFormats (StackFrame *frame) 750 { 751 if (frame == NULL) 752 return; 753 754 StreamString s; 755 const char *prompt_format = 756 "{addr = '${addr}'\n}" 757 "{process.id = '${process.id}'\n}" 758 "{process.name = '${process.name}'\n}" 759 "{process.file.basename = '${process.file.basename}'\n}" 760 "{process.file.fullpath = '${process.file.fullpath}'\n}" 761 "{thread.id = '${thread.id}'\n}" 762 "{thread.index = '${thread.index}'\n}" 763 "{thread.name = '${thread.name}'\n}" 764 "{thread.queue = '${thread.queue}'\n}" 765 "{thread.stop-reason = '${thread.stop-reason}'\n}" 766 "{target.arch = '${target.arch}'\n}" 767 "{module.file.basename = '${module.file.basename}'\n}" 768 "{module.file.fullpath = '${module.file.fullpath}'\n}" 769 "{file.basename = '${file.basename}'\n}" 770 "{file.fullpath = '${file.fullpath}'\n}" 771 "{frame.index = '${frame.index}'\n}" 772 "{frame.pc = '${frame.pc}'\n}" 773 "{frame.sp = '${frame.sp}'\n}" 774 "{frame.fp = '${frame.fp}'\n}" 775 "{frame.flags = '${frame.flags}'\n}" 776 "{frame.reg.rdi = '${frame.reg.rdi}'\n}" 777 "{frame.reg.rip = '${frame.reg.rip}'\n}" 778 "{frame.reg.rsp = '${frame.reg.rsp}'\n}" 779 "{frame.reg.rbp = '${frame.reg.rbp}'\n}" 780 "{frame.reg.rflags = '${frame.reg.rflags}'\n}" 781 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}" 782 "{frame.reg.carp = '${frame.reg.carp}'\n}" 783 "{function.id = '${function.id}'\n}" 784 "{function.name = '${function.name}'\n}" 785 "{function.name-with-args = '${function.name-with-args}'\n}" 786 "{function.addr-offset = '${function.addr-offset}'\n}" 787 "{function.line-offset = '${function.line-offset}'\n}" 788 "{function.pc-offset = '${function.pc-offset}'\n}" 789 "{line.file.basename = '${line.file.basename}'\n}" 790 "{line.file.fullpath = '${line.file.fullpath}'\n}" 791 "{line.number = '${line.number}'\n}" 792 "{line.start-addr = '${line.start-addr}'\n}" 793 "{line.end-addr = '${line.end-addr}'\n}" 794 ; 795 796 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything)); 797 ExecutionContext exe_ctx; 798 frame->CalculateExecutionContext(exe_ctx); 799 const char *end = NULL; 800 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s, &end)) 801 { 802 printf("%s\n", s.GetData()); 803 } 804 else 805 { 806 printf ("error: at '%s'\n", end); 807 printf ("what we got: %s\n", s.GetData()); 808 } 809 } 810 811 static bool 812 ScanFormatDescriptor (const char* var_name_begin, 813 const char* var_name_end, 814 const char** var_name_final, 815 const char** percent_position, 816 Format* custom_format, 817 ValueObject::ValueObjectRepresentationStyle* val_obj_display) 818 { 819 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES)); 820 *percent_position = ::strchr(var_name_begin,'%'); 821 if (!*percent_position || *percent_position > var_name_end) 822 { 823 if (log) 824 log->Printf("no format descriptor in string, skipping"); 825 *var_name_final = var_name_end; 826 } 827 else 828 { 829 *var_name_final = *percent_position; 830 char* format_name = new char[var_name_end-*var_name_final]; format_name[var_name_end-*var_name_final-1] = '\0'; 831 memcpy(format_name, *var_name_final+1, var_name_end-*var_name_final-1); 832 if (log) 833 log->Printf("parsing %s as a format descriptor", format_name); 834 if ( !FormatManager::GetFormatFromCString(format_name, 835 true, 836 *custom_format) ) 837 { 838 if (log) 839 log->Printf("%s is an unknown format", format_name); 840 // if this is an @ sign, print ObjC description 841 if (*format_name == '@') 842 *val_obj_display = ValueObject::eDisplayLanguageSpecific; 843 // if this is a V, print the value using the default format 844 else if (*format_name == 'V') 845 *val_obj_display = ValueObject::eDisplayValue; 846 // if this is an L, print the location of the value 847 else if (*format_name == 'L') 848 *val_obj_display = ValueObject::eDisplayLocation; 849 // if this is an S, print the summary after all 850 else if (*format_name == 'S') 851 *val_obj_display = ValueObject::eDisplaySummary; 852 else if (*format_name == '#') 853 *val_obj_display = ValueObject::eDisplayChildrenCount; 854 else if (*format_name == 'T') 855 *val_obj_display = ValueObject::eDisplayType; 856 else if (log) 857 log->Printf("%s is an error, leaving the previous value alone", format_name); 858 } 859 // a good custom format tells us to print the value using it 860 else 861 { 862 if (log) 863 log->Printf("will display value for this VO"); 864 *val_obj_display = ValueObject::eDisplayValue; 865 } 866 delete format_name; 867 } 868 if (log) 869 log->Printf("final format description outcome: custom_format = %d, val_obj_display = %d", 870 *custom_format, 871 *val_obj_display); 872 return true; 873 } 874 875 static bool 876 ScanBracketedRange (const char* var_name_begin, 877 const char* var_name_end, 878 const char* var_name_final, 879 const char** open_bracket_position, 880 const char** separator_position, 881 const char** close_bracket_position, 882 const char** var_name_final_if_array_range, 883 int64_t* index_lower, 884 int64_t* index_higher) 885 { 886 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES)); 887 *open_bracket_position = ::strchr(var_name_begin,'['); 888 if (*open_bracket_position && *open_bracket_position < var_name_final) 889 { 890 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield 891 *close_bracket_position = ::strchr(*open_bracket_position,']'); 892 // as usual, we assume that [] will come before % 893 //printf("trying to expand a []\n"); 894 *var_name_final_if_array_range = *open_bracket_position; 895 if (*close_bracket_position - *open_bracket_position == 1) 896 { 897 if (log) 898 log->Printf("[] detected.. going from 0 to end of data"); 899 *index_lower = 0; 900 } 901 else if (*separator_position == NULL || *separator_position > var_name_end) 902 { 903 char *end = NULL; 904 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0); 905 *index_higher = *index_lower; 906 if (log) 907 log->Printf("[%lld] detected, high index is same", *index_lower); 908 } 909 else if (*close_bracket_position && *close_bracket_position < var_name_end) 910 { 911 char *end = NULL; 912 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0); 913 *index_higher = ::strtoul (*separator_position+1, &end, 0); 914 if (log) 915 log->Printf("[%lld-%lld] detected", *index_lower, *index_higher); 916 } 917 else 918 { 919 if (log) 920 log->Printf("expression is erroneous, cannot extract indices out of it"); 921 return false; 922 } 923 if (*index_lower > *index_higher && *index_higher > 0) 924 { 925 if (log) 926 log->Printf("swapping indices"); 927 int temp = *index_lower; 928 *index_lower = *index_higher; 929 *index_higher = temp; 930 } 931 } 932 else if (log) 933 log->Printf("no bracketed range, skipping entirely"); 934 return true; 935 } 936 937 938 static ValueObjectSP 939 ExpandExpressionPath (ValueObject* valobj, 940 StackFrame* frame, 941 bool* do_deref_pointer, 942 const char* var_name_begin, 943 const char* var_name_final, 944 Error& error) 945 { 946 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES)); 947 StreamString sstring; 948 VariableSP var_sp; 949 950 if (*do_deref_pointer) 951 { 952 if (log) 953 log->Printf("been told to deref_pointer by caller"); 954 sstring.PutChar('*'); 955 } 956 else if (valobj->IsDereferenceOfParent() && ClangASTContext::IsPointerType(valobj->GetParent()->GetClangType()) && !valobj->IsArrayItemForPointer()) 957 { 958 if (log) 959 log->Printf("decided to deref_pointer myself"); 960 sstring.PutChar('*'); 961 *do_deref_pointer = true; 962 } 963 964 valobj->GetExpressionPath(sstring, true, ValueObject::eHonorPointers); 965 if (log) 966 log->Printf("expression path to expand in phase 0: %s",sstring.GetData()); 967 sstring.PutRawBytes(var_name_begin+3, var_name_final-var_name_begin-3); 968 if (log) 969 log->Printf("expression path to expand in phase 1: %s",sstring.GetData()); 970 std::string name = std::string(sstring.GetData()); 971 ValueObjectSP target = frame->GetValueForVariableExpressionPath (name.c_str(), 972 eNoDynamicValues, 973 0, 974 var_sp, 975 error); 976 return target; 977 } 978 979 static ValueObjectSP 980 ExpandIndexedExpression (ValueObject* valobj, 981 uint32_t index, 982 StackFrame* frame, 983 bool deref_pointer) 984 { 985 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES)); 986 const char* ptr_deref_format = "[%d]"; 987 std::auto_ptr<char> ptr_deref_buffer(new char[10]); 988 ::sprintf(ptr_deref_buffer.get(), ptr_deref_format, index); 989 if (log) 990 log->Printf("name to deref: %s",ptr_deref_buffer.get()); 991 const char* first_unparsed; 992 ValueObject::GetValueForExpressionPathOptions options; 993 ValueObject::ExpressionPathEndResultType final_value_type; 994 ValueObject::ExpressionPathScanEndReason reason_to_stop; 995 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eDereference : ValueObject::eNothing); 996 ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.get(), 997 &first_unparsed, 998 &reason_to_stop, 999 &final_value_type, 1000 options, 1001 &what_next); 1002 if (!item) 1003 { 1004 if (log) 1005 log->Printf("ERROR: unparsed portion = %s, why stopping = %d," 1006 " final_value_type %d", 1007 first_unparsed, reason_to_stop, final_value_type); 1008 } 1009 else 1010 { 1011 if (log) 1012 log->Printf("ALL RIGHT: unparsed portion = %s, why stopping = %d," 1013 " final_value_type %d", 1014 first_unparsed, reason_to_stop, final_value_type); 1015 } 1016 return item; 1017 } 1018 1019 bool 1020 Debugger::FormatPrompt 1021 ( 1022 const char *format, 1023 const SymbolContext *sc, 1024 const ExecutionContext *exe_ctx, 1025 const Address *addr, 1026 Stream &s, 1027 const char **end, 1028 ValueObject* valobj 1029 ) 1030 { 1031 ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers 1032 bool success = true; 1033 const char *p; 1034 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES)); 1035 for (p = format; *p != '\0'; ++p) 1036 { 1037 if (realvalobj) 1038 { 1039 valobj = realvalobj; 1040 realvalobj = NULL; 1041 } 1042 size_t non_special_chars = ::strcspn (p, "${}\\"); 1043 if (non_special_chars > 0) 1044 { 1045 if (success) 1046 s.Write (p, non_special_chars); 1047 p += non_special_chars; 1048 } 1049 1050 if (*p == '\0') 1051 { 1052 break; 1053 } 1054 else if (*p == '{') 1055 { 1056 // Start a new scope that must have everything it needs if it is to 1057 // to make it into the final output stream "s". If you want to make 1058 // a format that only prints out the function or symbol name if there 1059 // is one in the symbol context you can use: 1060 // "{function =${function.name}}" 1061 // The first '{' starts a new scope that end with the matching '}' at 1062 // the end of the string. The contents "function =${function.name}" 1063 // will then be evaluated and only be output if there is a function 1064 // or symbol with a valid name. 1065 StreamString sub_strm; 1066 1067 ++p; // Skip the '{' 1068 1069 if (FormatPrompt (p, sc, exe_ctx, addr, sub_strm, &p, valobj)) 1070 { 1071 // The stream had all it needed 1072 s.Write(sub_strm.GetData(), sub_strm.GetSize()); 1073 } 1074 if (*p != '}') 1075 { 1076 success = false; 1077 break; 1078 } 1079 } 1080 else if (*p == '}') 1081 { 1082 // End of a enclosing scope 1083 break; 1084 } 1085 else if (*p == '$') 1086 { 1087 // We have a prompt variable to print 1088 ++p; 1089 if (*p == '{') 1090 { 1091 ++p; 1092 const char *var_name_begin = p; 1093 const char *var_name_end = ::strchr (p, '}'); 1094 1095 if (var_name_end && var_name_begin < var_name_end) 1096 { 1097 // if we have already failed to parse, skip this variable 1098 if (success) 1099 { 1100 const char *cstr = NULL; 1101 Address format_addr; 1102 bool calculate_format_addr_function_offset = false; 1103 // Set reg_kind and reg_num to invalid values 1104 RegisterKind reg_kind = kNumRegisterKinds; 1105 uint32_t reg_num = LLDB_INVALID_REGNUM; 1106 FileSpec format_file_spec; 1107 const RegisterInfo *reg_info = NULL; 1108 RegisterContext *reg_ctx = NULL; 1109 bool do_deref_pointer = false; 1110 ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eEndOfString; 1111 ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::ePlain; 1112 1113 // Each variable must set success to true below... 1114 bool var_success = false; 1115 switch (var_name_begin[0]) 1116 { 1117 case '*': 1118 case 'v': 1119 case 's': 1120 { 1121 if (!valobj) 1122 break; 1123 1124 if (log) 1125 log->Printf("initial string: %s",var_name_begin); 1126 1127 // check for *var and *svar 1128 if (*var_name_begin == '*') 1129 { 1130 do_deref_pointer = true; 1131 var_name_begin++; 1132 } 1133 1134 if (log) 1135 log->Printf("initial string: %s",var_name_begin); 1136 1137 if (*var_name_begin == 's') 1138 { 1139 valobj = valobj->GetSyntheticValue(eUseSyntheticFilter).get(); 1140 var_name_begin++; 1141 } 1142 1143 if (log) 1144 log->Printf("initial string: %s",var_name_begin); 1145 1146 // should be a 'v' by now 1147 if (*var_name_begin != 'v') 1148 break; 1149 1150 if (log) 1151 log->Printf("initial string: %s",var_name_begin); 1152 1153 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ? 1154 ValueObject::eDereference : ValueObject::eNothing); 1155 ValueObject::GetValueForExpressionPathOptions options; 1156 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren(); 1157 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eDisplaySummary; 1158 ValueObject* target = NULL; 1159 Format custom_format = eFormatInvalid; 1160 const char* var_name_final = NULL; 1161 const char* var_name_final_if_array_range = NULL; 1162 const char* close_bracket_position = NULL; 1163 int64_t index_lower = -1; 1164 int64_t index_higher = -1; 1165 bool is_array_range = false; 1166 const char* first_unparsed; 1167 bool was_plain_var = false; 1168 bool was_var_format = false; 1169 1170 if (!valobj) break; 1171 // simplest case ${var}, just print valobj's value 1172 if (::strncmp (var_name_begin, "var}", strlen("var}")) == 0) 1173 { 1174 was_plain_var = true; 1175 target = valobj; 1176 val_obj_display = ValueObject::eDisplayValue; 1177 } 1178 else if (::strncmp(var_name_begin,"var%",strlen("var%")) == 0) 1179 { 1180 was_var_format = true; 1181 // this is a variable with some custom format applied to it 1182 const char* percent_position; 1183 target = valobj; 1184 val_obj_display = ValueObject::eDisplayValue; 1185 ScanFormatDescriptor (var_name_begin, 1186 var_name_end, 1187 &var_name_final, 1188 &percent_position, 1189 &custom_format, 1190 &val_obj_display); 1191 } 1192 // this is ${var.something} or multiple .something nested 1193 else if (::strncmp (var_name_begin, "var", strlen("var")) == 0) 1194 { 1195 1196 const char* percent_position; 1197 ScanFormatDescriptor (var_name_begin, 1198 var_name_end, 1199 &var_name_final, 1200 &percent_position, 1201 &custom_format, 1202 &val_obj_display); 1203 1204 const char* open_bracket_position; 1205 const char* separator_position; 1206 ScanBracketedRange (var_name_begin, 1207 var_name_end, 1208 var_name_final, 1209 &open_bracket_position, 1210 &separator_position, 1211 &close_bracket_position, 1212 &var_name_final_if_array_range, 1213 &index_lower, 1214 &index_higher); 1215 1216 Error error; 1217 1218 std::auto_ptr<char> expr_path(new char[var_name_final-var_name_begin-1]); 1219 ::memset(expr_path.get(), 0, var_name_final-var_name_begin-1); 1220 memcpy(expr_path.get(), var_name_begin+3,var_name_final-var_name_begin-3); 1221 1222 if (log) 1223 log->Printf("symbol to expand: %s",expr_path.get()); 1224 1225 target = valobj->GetValueForExpressionPath(expr_path.get(), 1226 &first_unparsed, 1227 &reason_to_stop, 1228 &final_value_type, 1229 options, 1230 &what_next).get(); 1231 1232 if (!target) 1233 { 1234 if (log) 1235 log->Printf("ERROR: unparsed portion = %s, why stopping = %d," 1236 " final_value_type %d", 1237 first_unparsed, reason_to_stop, final_value_type); 1238 break; 1239 } 1240 else 1241 { 1242 if (log) 1243 log->Printf("ALL RIGHT: unparsed portion = %s, why stopping = %d," 1244 " final_value_type %d", 1245 first_unparsed, reason_to_stop, final_value_type); 1246 } 1247 } 1248 else 1249 break; 1250 1251 is_array_range = (final_value_type == ValueObject::eBoundedRange || 1252 final_value_type == ValueObject::eUnboundedRange); 1253 1254 do_deref_pointer = (what_next == ValueObject::eDereference); 1255 1256 if (do_deref_pointer && !is_array_range) 1257 { 1258 // I have not deref-ed yet, let's do it 1259 // this happens when we are not going through GetValueForVariableExpressionPath 1260 // to get to the target ValueObject 1261 Error error; 1262 target = target->Dereference(error).get(); 1263 if (error.Fail()) 1264 { 1265 if (log) 1266 log->Printf("ERROR: %s\n", error.AsCString("unknown")); \ 1267 break; 1268 } 1269 do_deref_pointer = false; 1270 } 1271 1272 // TODO use flags for these 1273 bool is_array = ClangASTContext::IsArrayType(target->GetClangType()); 1274 bool is_pointer = ClangASTContext::IsPointerType(target->GetClangType()); 1275 bool is_aggregate = ClangASTContext::IsAggregateType(target->GetClangType()); 1276 1277 if ((is_array || is_pointer) && (!is_array_range) && val_obj_display == ValueObject::eDisplayValue) // this should be wrong, but there are some exceptions 1278 { 1279 StreamString str_temp; 1280 if (log) 1281 log->Printf("I am into array || pointer && !range"); 1282 1283 if (target->HasSpecialCasesForPrintableRepresentation(val_obj_display, 1284 custom_format)) 1285 { 1286 // try to use the special cases 1287 var_success = target->DumpPrintableRepresentation(str_temp, 1288 val_obj_display, 1289 custom_format); 1290 if (log) 1291 log->Printf("special cases did%s match", var_success ? "" : "n't"); 1292 1293 // should not happen 1294 if (!var_success) 1295 s << "<invalid usage of pointer value as object>"; 1296 else 1297 s << str_temp.GetData(); 1298 var_success = true; 1299 break; 1300 } 1301 else 1302 { 1303 if (was_plain_var) // if ${var} 1304 { 1305 s << target->GetTypeName() << " @ " << target->GetLocationAsCString(); 1306 } 1307 else if (is_pointer) // if pointer, value is the address stored 1308 { 1309 var_success = target->GetPrintableRepresentation(s, 1310 val_obj_display, 1311 custom_format); 1312 } 1313 else 1314 { 1315 s << "<invalid usage of pointer value as object>"; 1316 } 1317 var_success = true; 1318 break; 1319 } 1320 } 1321 1322 // if directly trying to print ${var}, and this is an aggregate, display a nice 1323 // type @ location message 1324 if (is_aggregate && was_plain_var) 1325 { 1326 s << target->GetTypeName() << " @ " << target->GetLocationAsCString(); 1327 var_success = true; 1328 break; 1329 } 1330 1331 // if directly trying to print ${var%V}, and this is an aggregate, do not let the user do it 1332 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eDisplayValue))) 1333 { 1334 s << "<invalid use of aggregate type>"; 1335 var_success = true; 1336 break; 1337 } 1338 1339 if (!is_array_range) 1340 { 1341 if (log) 1342 log->Printf("dumping ordinary printable output"); 1343 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format); 1344 } 1345 else 1346 { 1347 if (log) 1348 log->Printf("checking if I can handle as array"); 1349 if (!is_array && !is_pointer) 1350 break; 1351 if (log) 1352 log->Printf("handle as array"); 1353 const char* special_directions = NULL; 1354 StreamString special_directions_writer; 1355 if (close_bracket_position && (var_name_end-close_bracket_position > 1)) 1356 { 1357 ConstString additional_data; 1358 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1); 1359 special_directions_writer.Printf("${%svar%s}", 1360 do_deref_pointer ? "*" : "", 1361 additional_data.GetCString()); 1362 special_directions = special_directions_writer.GetData(); 1363 } 1364 1365 // let us display items index_lower thru index_higher of this array 1366 s.PutChar('['); 1367 var_success = true; 1368 1369 if (index_higher < 0) 1370 index_higher = valobj->GetNumChildren() - 1; 1371 1372 uint32_t max_num_children = target->GetUpdatePoint().GetTargetSP()->GetMaximumNumberOfChildrenToDisplay(); 1373 1374 for (;index_lower<=index_higher;index_lower++) 1375 { 1376 ValueObject* item = ExpandIndexedExpression (target, 1377 index_lower, 1378 exe_ctx->GetFramePtr(), 1379 false).get(); 1380 1381 if (!item) 1382 { 1383 if (log) 1384 log->Printf("ERROR in getting child item at index %lld", index_lower); 1385 } 1386 else 1387 { 1388 if (log) 1389 log->Printf("special_directions for child item: %s",special_directions); 1390 } 1391 1392 if (!special_directions) 1393 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format); 1394 else 1395 var_success &= FormatPrompt(special_directions, sc, exe_ctx, addr, s, NULL, item); 1396 1397 if (--max_num_children == 0) 1398 { 1399 s.PutCString(", ..."); 1400 break; 1401 } 1402 1403 if (index_lower < index_higher) 1404 s.PutChar(','); 1405 } 1406 s.PutChar(']'); 1407 } 1408 } 1409 break; 1410 case 'a': 1411 if (::strncmp (var_name_begin, "addr}", strlen("addr}")) == 0) 1412 { 1413 if (addr && addr->IsValid()) 1414 { 1415 var_success = true; 1416 format_addr = *addr; 1417 } 1418 } 1419 else if (::strncmp (var_name_begin, "ansi.", strlen("ansi.")) == 0) 1420 { 1421 var_success = true; 1422 var_name_begin += strlen("ansi."); // Skip the "ansi." 1423 if (::strncmp (var_name_begin, "fg.", strlen("fg.")) == 0) 1424 { 1425 var_name_begin += strlen("fg."); // Skip the "fg." 1426 if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0) 1427 { 1428 s.Printf ("%s%s%s", 1429 lldb_utility::ansi::k_escape_start, 1430 lldb_utility::ansi::k_fg_black, 1431 lldb_utility::ansi::k_escape_end); 1432 } 1433 else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0) 1434 { 1435 s.Printf ("%s%s%s", 1436 lldb_utility::ansi::k_escape_start, 1437 lldb_utility::ansi::k_fg_red, 1438 lldb_utility::ansi::k_escape_end); 1439 } 1440 else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0) 1441 { 1442 s.Printf ("%s%s%s", 1443 lldb_utility::ansi::k_escape_start, 1444 lldb_utility::ansi::k_fg_green, 1445 lldb_utility::ansi::k_escape_end); 1446 } 1447 else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0) 1448 { 1449 s.Printf ("%s%s%s", 1450 lldb_utility::ansi::k_escape_start, 1451 lldb_utility::ansi::k_fg_yellow, 1452 lldb_utility::ansi::k_escape_end); 1453 } 1454 else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0) 1455 { 1456 s.Printf ("%s%s%s", 1457 lldb_utility::ansi::k_escape_start, 1458 lldb_utility::ansi::k_fg_blue, 1459 lldb_utility::ansi::k_escape_end); 1460 } 1461 else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0) 1462 { 1463 s.Printf ("%s%s%s", 1464 lldb_utility::ansi::k_escape_start, 1465 lldb_utility::ansi::k_fg_purple, 1466 lldb_utility::ansi::k_escape_end); 1467 } 1468 else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0) 1469 { 1470 s.Printf ("%s%s%s", 1471 lldb_utility::ansi::k_escape_start, 1472 lldb_utility::ansi::k_fg_cyan, 1473 lldb_utility::ansi::k_escape_end); 1474 } 1475 else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0) 1476 { 1477 s.Printf ("%s%s%s", 1478 lldb_utility::ansi::k_escape_start, 1479 lldb_utility::ansi::k_fg_white, 1480 lldb_utility::ansi::k_escape_end); 1481 } 1482 else 1483 { 1484 var_success = false; 1485 } 1486 } 1487 else if (::strncmp (var_name_begin, "bg.", strlen("bg.")) == 0) 1488 { 1489 var_name_begin += strlen("bg."); // Skip the "bg." 1490 if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0) 1491 { 1492 s.Printf ("%s%s%s", 1493 lldb_utility::ansi::k_escape_start, 1494 lldb_utility::ansi::k_bg_black, 1495 lldb_utility::ansi::k_escape_end); 1496 } 1497 else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0) 1498 { 1499 s.Printf ("%s%s%s", 1500 lldb_utility::ansi::k_escape_start, 1501 lldb_utility::ansi::k_bg_red, 1502 lldb_utility::ansi::k_escape_end); 1503 } 1504 else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0) 1505 { 1506 s.Printf ("%s%s%s", 1507 lldb_utility::ansi::k_escape_start, 1508 lldb_utility::ansi::k_bg_green, 1509 lldb_utility::ansi::k_escape_end); 1510 } 1511 else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0) 1512 { 1513 s.Printf ("%s%s%s", 1514 lldb_utility::ansi::k_escape_start, 1515 lldb_utility::ansi::k_bg_yellow, 1516 lldb_utility::ansi::k_escape_end); 1517 } 1518 else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0) 1519 { 1520 s.Printf ("%s%s%s", 1521 lldb_utility::ansi::k_escape_start, 1522 lldb_utility::ansi::k_bg_blue, 1523 lldb_utility::ansi::k_escape_end); 1524 } 1525 else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0) 1526 { 1527 s.Printf ("%s%s%s", 1528 lldb_utility::ansi::k_escape_start, 1529 lldb_utility::ansi::k_bg_purple, 1530 lldb_utility::ansi::k_escape_end); 1531 } 1532 else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0) 1533 { 1534 s.Printf ("%s%s%s", 1535 lldb_utility::ansi::k_escape_start, 1536 lldb_utility::ansi::k_bg_cyan, 1537 lldb_utility::ansi::k_escape_end); 1538 } 1539 else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0) 1540 { 1541 s.Printf ("%s%s%s", 1542 lldb_utility::ansi::k_escape_start, 1543 lldb_utility::ansi::k_bg_white, 1544 lldb_utility::ansi::k_escape_end); 1545 } 1546 else 1547 { 1548 var_success = false; 1549 } 1550 } 1551 else if (::strncmp (var_name_begin, "normal}", strlen ("normal}")) == 0) 1552 { 1553 s.Printf ("%s%s%s", 1554 lldb_utility::ansi::k_escape_start, 1555 lldb_utility::ansi::k_ctrl_normal, 1556 lldb_utility::ansi::k_escape_end); 1557 } 1558 else if (::strncmp (var_name_begin, "bold}", strlen("bold}")) == 0) 1559 { 1560 s.Printf ("%s%s%s", 1561 lldb_utility::ansi::k_escape_start, 1562 lldb_utility::ansi::k_ctrl_bold, 1563 lldb_utility::ansi::k_escape_end); 1564 } 1565 else if (::strncmp (var_name_begin, "faint}", strlen("faint}")) == 0) 1566 { 1567 s.Printf ("%s%s%s", 1568 lldb_utility::ansi::k_escape_start, 1569 lldb_utility::ansi::k_ctrl_faint, 1570 lldb_utility::ansi::k_escape_end); 1571 } 1572 else if (::strncmp (var_name_begin, "italic}", strlen("italic}")) == 0) 1573 { 1574 s.Printf ("%s%s%s", 1575 lldb_utility::ansi::k_escape_start, 1576 lldb_utility::ansi::k_ctrl_italic, 1577 lldb_utility::ansi::k_escape_end); 1578 } 1579 else if (::strncmp (var_name_begin, "underline}", strlen("underline}")) == 0) 1580 { 1581 s.Printf ("%s%s%s", 1582 lldb_utility::ansi::k_escape_start, 1583 lldb_utility::ansi::k_ctrl_underline, 1584 lldb_utility::ansi::k_escape_end); 1585 } 1586 else if (::strncmp (var_name_begin, "slow-blink}", strlen("slow-blink}")) == 0) 1587 { 1588 s.Printf ("%s%s%s", 1589 lldb_utility::ansi::k_escape_start, 1590 lldb_utility::ansi::k_ctrl_slow_blink, 1591 lldb_utility::ansi::k_escape_end); 1592 } 1593 else if (::strncmp (var_name_begin, "fast-blink}", strlen("fast-blink}")) == 0) 1594 { 1595 s.Printf ("%s%s%s", 1596 lldb_utility::ansi::k_escape_start, 1597 lldb_utility::ansi::k_ctrl_fast_blink, 1598 lldb_utility::ansi::k_escape_end); 1599 } 1600 else if (::strncmp (var_name_begin, "negative}", strlen("negative}")) == 0) 1601 { 1602 s.Printf ("%s%s%s", 1603 lldb_utility::ansi::k_escape_start, 1604 lldb_utility::ansi::k_ctrl_negative, 1605 lldb_utility::ansi::k_escape_end); 1606 } 1607 else if (::strncmp (var_name_begin, "conceal}", strlen("conceal}")) == 0) 1608 { 1609 s.Printf ("%s%s%s", 1610 lldb_utility::ansi::k_escape_start, 1611 lldb_utility::ansi::k_ctrl_conceal, 1612 lldb_utility::ansi::k_escape_end); 1613 1614 } 1615 else if (::strncmp (var_name_begin, "crossed-out}", strlen("crossed-out}")) == 0) 1616 { 1617 s.Printf ("%s%s%s", 1618 lldb_utility::ansi::k_escape_start, 1619 lldb_utility::ansi::k_ctrl_crossed_out, 1620 lldb_utility::ansi::k_escape_end); 1621 } 1622 else 1623 { 1624 var_success = false; 1625 } 1626 } 1627 break; 1628 1629 case 'p': 1630 if (::strncmp (var_name_begin, "process.", strlen("process.")) == 0) 1631 { 1632 if (exe_ctx) 1633 { 1634 Process *process = exe_ctx->GetProcessPtr(); 1635 if (process) 1636 { 1637 var_name_begin += ::strlen ("process."); 1638 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0) 1639 { 1640 s.Printf("%llu", process->GetID()); 1641 var_success = true; 1642 } 1643 else if ((::strncmp (var_name_begin, "name}", strlen("name}")) == 0) || 1644 (::strncmp (var_name_begin, "file.basename}", strlen("file.basename}")) == 0) || 1645 (::strncmp (var_name_begin, "file.fullpath}", strlen("file.fullpath}")) == 0)) 1646 { 1647 Module *exe_module = process->GetTarget().GetExecutableModulePointer(); 1648 if (exe_module) 1649 { 1650 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f') 1651 { 1652 format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename(); 1653 var_success = format_file_spec; 1654 } 1655 else 1656 { 1657 format_file_spec = exe_module->GetFileSpec(); 1658 var_success = format_file_spec; 1659 } 1660 } 1661 } 1662 } 1663 } 1664 } 1665 break; 1666 1667 case 't': 1668 if (::strncmp (var_name_begin, "thread.", strlen("thread.")) == 0) 1669 { 1670 if (exe_ctx) 1671 { 1672 Thread *thread = exe_ctx->GetThreadPtr(); 1673 if (thread) 1674 { 1675 var_name_begin += ::strlen ("thread."); 1676 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0) 1677 { 1678 s.Printf("0x%4.4llx", thread->GetID()); 1679 var_success = true; 1680 } 1681 else if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0) 1682 { 1683 s.Printf("%u", thread->GetIndexID()); 1684 var_success = true; 1685 } 1686 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0) 1687 { 1688 cstr = thread->GetName(); 1689 var_success = cstr && cstr[0]; 1690 if (var_success) 1691 s.PutCString(cstr); 1692 } 1693 else if (::strncmp (var_name_begin, "queue}", strlen("queue}")) == 0) 1694 { 1695 cstr = thread->GetQueueName(); 1696 var_success = cstr && cstr[0]; 1697 if (var_success) 1698 s.PutCString(cstr); 1699 } 1700 else if (::strncmp (var_name_begin, "stop-reason}", strlen("stop-reason}")) == 0) 1701 { 1702 StopInfoSP stop_info_sp = thread->GetStopInfo (); 1703 if (stop_info_sp) 1704 { 1705 cstr = stop_info_sp->GetDescription(); 1706 if (cstr && cstr[0]) 1707 { 1708 s.PutCString(cstr); 1709 var_success = true; 1710 } 1711 } 1712 } 1713 else if (::strncmp (var_name_begin, "return-value}", strlen("return-value}")) == 0) 1714 { 1715 StopInfoSP stop_info_sp = thread->GetStopInfo (); 1716 if (stop_info_sp) 1717 { 1718 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp); 1719 if (return_valobj_sp) 1720 { 1721 ValueObject::DumpValueObjectOptions dump_options; 1722 ValueObject::DumpValueObject (s, return_valobj_sp.get(), dump_options); 1723 var_success = true; 1724 } 1725 } 1726 } 1727 } 1728 } 1729 } 1730 else if (::strncmp (var_name_begin, "target.", strlen("target.")) == 0) 1731 { 1732 Target *target = Target::GetTargetFromContexts (exe_ctx, sc); 1733 if (target) 1734 { 1735 var_name_begin += ::strlen ("target."); 1736 if (::strncmp (var_name_begin, "arch}", strlen("arch}")) == 0) 1737 { 1738 ArchSpec arch (target->GetArchitecture ()); 1739 if (arch.IsValid()) 1740 { 1741 s.PutCString (arch.GetArchitectureName()); 1742 var_success = true; 1743 } 1744 } 1745 } 1746 } 1747 break; 1748 1749 1750 case 'm': 1751 if (::strncmp (var_name_begin, "module.", strlen("module.")) == 0) 1752 { 1753 if (sc && sc->module_sp.get()) 1754 { 1755 Module *module = sc->module_sp.get(); 1756 var_name_begin += ::strlen ("module."); 1757 1758 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0) 1759 { 1760 if (module->GetFileSpec()) 1761 { 1762 var_name_begin += ::strlen ("file."); 1763 1764 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0) 1765 { 1766 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename(); 1767 var_success = format_file_spec; 1768 } 1769 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0) 1770 { 1771 format_file_spec = module->GetFileSpec(); 1772 var_success = format_file_spec; 1773 } 1774 } 1775 } 1776 } 1777 } 1778 break; 1779 1780 1781 case 'f': 1782 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0) 1783 { 1784 if (sc && sc->comp_unit != NULL) 1785 { 1786 var_name_begin += ::strlen ("file."); 1787 1788 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0) 1789 { 1790 format_file_spec.GetFilename() = sc->comp_unit->GetFilename(); 1791 var_success = format_file_spec; 1792 } 1793 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0) 1794 { 1795 format_file_spec = *sc->comp_unit; 1796 var_success = format_file_spec; 1797 } 1798 } 1799 } 1800 else if (::strncmp (var_name_begin, "frame.", strlen("frame.")) == 0) 1801 { 1802 if (exe_ctx) 1803 { 1804 StackFrame *frame = exe_ctx->GetFramePtr(); 1805 if (frame) 1806 { 1807 var_name_begin += ::strlen ("frame."); 1808 if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0) 1809 { 1810 s.Printf("%u", frame->GetFrameIndex()); 1811 var_success = true; 1812 } 1813 else if (::strncmp (var_name_begin, "pc}", strlen("pc}")) == 0) 1814 { 1815 reg_kind = eRegisterKindGeneric; 1816 reg_num = LLDB_REGNUM_GENERIC_PC; 1817 var_success = true; 1818 } 1819 else if (::strncmp (var_name_begin, "sp}", strlen("sp}")) == 0) 1820 { 1821 reg_kind = eRegisterKindGeneric; 1822 reg_num = LLDB_REGNUM_GENERIC_SP; 1823 var_success = true; 1824 } 1825 else if (::strncmp (var_name_begin, "fp}", strlen("fp}")) == 0) 1826 { 1827 reg_kind = eRegisterKindGeneric; 1828 reg_num = LLDB_REGNUM_GENERIC_FP; 1829 var_success = true; 1830 } 1831 else if (::strncmp (var_name_begin, "flags}", strlen("flags}")) == 0) 1832 { 1833 reg_kind = eRegisterKindGeneric; 1834 reg_num = LLDB_REGNUM_GENERIC_FLAGS; 1835 var_success = true; 1836 } 1837 else if (::strncmp (var_name_begin, "reg.", strlen ("reg.")) == 0) 1838 { 1839 reg_ctx = frame->GetRegisterContext().get(); 1840 if (reg_ctx) 1841 { 1842 var_name_begin += ::strlen ("reg."); 1843 if (var_name_begin < var_name_end) 1844 { 1845 std::string reg_name (var_name_begin, var_name_end); 1846 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str()); 1847 if (reg_info) 1848 var_success = true; 1849 } 1850 } 1851 } 1852 } 1853 } 1854 } 1855 else if (::strncmp (var_name_begin, "function.", strlen("function.")) == 0) 1856 { 1857 if (sc && (sc->function != NULL || sc->symbol != NULL)) 1858 { 1859 var_name_begin += ::strlen ("function."); 1860 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0) 1861 { 1862 if (sc->function) 1863 s.Printf("function{0x%8.8llx}", sc->function->GetID()); 1864 else 1865 s.Printf("symbol[%u]", sc->symbol->GetID()); 1866 1867 var_success = true; 1868 } 1869 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0) 1870 { 1871 if (sc->function) 1872 cstr = sc->function->GetName().AsCString (NULL); 1873 else if (sc->symbol) 1874 cstr = sc->symbol->GetName().AsCString (NULL); 1875 if (cstr) 1876 { 1877 s.PutCString(cstr); 1878 1879 if (sc->block) 1880 { 1881 Block *inline_block = sc->block->GetContainingInlinedBlock (); 1882 if (inline_block) 1883 { 1884 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo(); 1885 if (inline_info) 1886 { 1887 s.PutCString(" [inlined] "); 1888 inline_info->GetName().Dump(&s); 1889 } 1890 } 1891 } 1892 var_success = true; 1893 } 1894 } 1895 else if (::strncmp (var_name_begin, "name-with-args}", strlen("name-with-args}")) == 0) 1896 { 1897 // Print the function name with arguments in it 1898 1899 if (sc->function) 1900 { 1901 var_success = true; 1902 ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL; 1903 cstr = sc->function->GetName().AsCString (NULL); 1904 if (cstr) 1905 { 1906 const InlineFunctionInfo *inline_info = NULL; 1907 VariableListSP variable_list_sp; 1908 bool get_function_vars = true; 1909 if (sc->block) 1910 { 1911 Block *inline_block = sc->block->GetContainingInlinedBlock (); 1912 1913 if (inline_block) 1914 { 1915 get_function_vars = false; 1916 inline_info = sc->block->GetInlinedFunctionInfo(); 1917 if (inline_info) 1918 variable_list_sp = inline_block->GetBlockVariableList (true); 1919 } 1920 } 1921 1922 if (get_function_vars) 1923 { 1924 variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true); 1925 } 1926 1927 if (inline_info) 1928 { 1929 s.PutCString (cstr); 1930 s.PutCString (" [inlined] "); 1931 cstr = inline_info->GetName().GetCString(); 1932 } 1933 1934 VariableList args; 1935 if (variable_list_sp) 1936 { 1937 const size_t num_variables = variable_list_sp->GetSize(); 1938 for (size_t var_idx = 0; var_idx < num_variables; ++var_idx) 1939 { 1940 VariableSP var_sp (variable_list_sp->GetVariableAtIndex(var_idx)); 1941 if (var_sp->GetScope() == eValueTypeVariableArgument) 1942 args.AddVariable (var_sp); 1943 } 1944 1945 } 1946 if (args.GetSize() > 0) 1947 { 1948 const char *open_paren = strchr (cstr, '('); 1949 const char *close_paren = NULL; 1950 if (open_paren) 1951 close_paren = strchr (open_paren, ')'); 1952 1953 if (open_paren) 1954 s.Write(cstr, open_paren - cstr + 1); 1955 else 1956 { 1957 s.PutCString (cstr); 1958 s.PutChar ('('); 1959 } 1960 const size_t num_args = variable_list_sp->GetSize(); 1961 for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx) 1962 { 1963 VariableSP var_sp (args.GetVariableAtIndex (arg_idx)); 1964 ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp)); 1965 const char *var_name = var_value_sp->GetName().GetCString(); 1966 const char *var_value = var_value_sp->GetValueAsCString(); 1967 if (var_value_sp->GetError().Success()) 1968 { 1969 if (arg_idx > 0) 1970 s.PutCString (", "); 1971 s.Printf ("%s=%s", var_name, var_value); 1972 } 1973 } 1974 1975 if (close_paren) 1976 s.PutCString (close_paren); 1977 else 1978 s.PutChar(')'); 1979 1980 } 1981 else 1982 { 1983 s.PutCString(cstr); 1984 } 1985 } 1986 } 1987 else if (sc->symbol) 1988 { 1989 cstr = sc->symbol->GetName().AsCString (NULL); 1990 if (cstr) 1991 { 1992 s.PutCString(cstr); 1993 var_success = true; 1994 } 1995 } 1996 } 1997 else if (::strncmp (var_name_begin, "addr-offset}", strlen("addr-offset}")) == 0) 1998 { 1999 var_success = addr != NULL; 2000 if (var_success) 2001 { 2002 format_addr = *addr; 2003 calculate_format_addr_function_offset = true; 2004 } 2005 } 2006 else if (::strncmp (var_name_begin, "line-offset}", strlen("line-offset}")) == 0) 2007 { 2008 var_success = sc->line_entry.range.GetBaseAddress().IsValid(); 2009 if (var_success) 2010 { 2011 format_addr = sc->line_entry.range.GetBaseAddress(); 2012 calculate_format_addr_function_offset = true; 2013 } 2014 } 2015 else if (::strncmp (var_name_begin, "pc-offset}", strlen("pc-offset}")) == 0) 2016 { 2017 StackFrame *frame = exe_ctx->GetFramePtr(); 2018 var_success = frame != NULL; 2019 if (var_success) 2020 { 2021 format_addr = frame->GetFrameCodeAddress(); 2022 calculate_format_addr_function_offset = true; 2023 } 2024 } 2025 } 2026 } 2027 break; 2028 2029 case 'l': 2030 if (::strncmp (var_name_begin, "line.", strlen("line.")) == 0) 2031 { 2032 if (sc && sc->line_entry.IsValid()) 2033 { 2034 var_name_begin += ::strlen ("line."); 2035 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0) 2036 { 2037 var_name_begin += ::strlen ("file."); 2038 2039 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0) 2040 { 2041 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename(); 2042 var_success = format_file_spec; 2043 } 2044 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0) 2045 { 2046 format_file_spec = sc->line_entry.file; 2047 var_success = format_file_spec; 2048 } 2049 } 2050 else if (::strncmp (var_name_begin, "number}", strlen("number}")) == 0) 2051 { 2052 var_success = true; 2053 s.Printf("%u", sc->line_entry.line); 2054 } 2055 else if ((::strncmp (var_name_begin, "start-addr}", strlen("start-addr}")) == 0) || 2056 (::strncmp (var_name_begin, "end-addr}", strlen("end-addr}")) == 0)) 2057 { 2058 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid(); 2059 if (var_success) 2060 { 2061 format_addr = sc->line_entry.range.GetBaseAddress(); 2062 if (var_name_begin[0] == 'e') 2063 format_addr.Slide (sc->line_entry.range.GetByteSize()); 2064 } 2065 } 2066 } 2067 } 2068 break; 2069 } 2070 2071 if (var_success) 2072 { 2073 // If format addr is valid, then we need to print an address 2074 if (reg_num != LLDB_INVALID_REGNUM) 2075 { 2076 StackFrame *frame = exe_ctx->GetFramePtr(); 2077 // We have a register value to display... 2078 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric) 2079 { 2080 format_addr = frame->GetFrameCodeAddress(); 2081 } 2082 else 2083 { 2084 if (reg_ctx == NULL) 2085 reg_ctx = frame->GetRegisterContext().get(); 2086 2087 if (reg_ctx) 2088 { 2089 if (reg_kind != kNumRegisterKinds) 2090 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num); 2091 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num); 2092 var_success = reg_info != NULL; 2093 } 2094 } 2095 } 2096 2097 if (reg_info != NULL) 2098 { 2099 RegisterValue reg_value; 2100 var_success = reg_ctx->ReadRegister (reg_info, reg_value); 2101 if (var_success) 2102 { 2103 reg_value.Dump(&s, reg_info, false, false, eFormatDefault); 2104 } 2105 } 2106 2107 if (format_file_spec) 2108 { 2109 s << format_file_spec; 2110 } 2111 2112 // If format addr is valid, then we need to print an address 2113 if (format_addr.IsValid()) 2114 { 2115 var_success = false; 2116 2117 if (calculate_format_addr_function_offset) 2118 { 2119 Address func_addr; 2120 2121 if (sc) 2122 { 2123 if (sc->function) 2124 { 2125 func_addr = sc->function->GetAddressRange().GetBaseAddress(); 2126 if (sc->block) 2127 { 2128 // Check to make sure we aren't in an inline 2129 // function. If we are, use the inline block 2130 // range that contains "format_addr" since 2131 // blocks can be discontiguous. 2132 Block *inline_block = sc->block->GetContainingInlinedBlock (); 2133 AddressRange inline_range; 2134 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range)) 2135 func_addr = inline_range.GetBaseAddress(); 2136 } 2137 } 2138 else if (sc->symbol && sc->symbol->GetAddressRangePtr()) 2139 func_addr = sc->symbol->GetAddressRangePtr()->GetBaseAddress(); 2140 } 2141 2142 if (func_addr.IsValid()) 2143 { 2144 if (func_addr.GetSection() == format_addr.GetSection()) 2145 { 2146 addr_t func_file_addr = func_addr.GetFileAddress(); 2147 addr_t addr_file_addr = format_addr.GetFileAddress(); 2148 if (addr_file_addr > func_file_addr) 2149 s.Printf(" + %llu", addr_file_addr - func_file_addr); 2150 else if (addr_file_addr < func_file_addr) 2151 s.Printf(" - %llu", func_file_addr - addr_file_addr); 2152 var_success = true; 2153 } 2154 else 2155 { 2156 Target *target = Target::GetTargetFromContexts (exe_ctx, sc); 2157 if (target) 2158 { 2159 addr_t func_load_addr = func_addr.GetLoadAddress (target); 2160 addr_t addr_load_addr = format_addr.GetLoadAddress (target); 2161 if (addr_load_addr > func_load_addr) 2162 s.Printf(" + %llu", addr_load_addr - func_load_addr); 2163 else if (addr_load_addr < func_load_addr) 2164 s.Printf(" - %llu", func_load_addr - addr_load_addr); 2165 var_success = true; 2166 } 2167 } 2168 } 2169 } 2170 else 2171 { 2172 Target *target = Target::GetTargetFromContexts (exe_ctx, sc); 2173 addr_t vaddr = LLDB_INVALID_ADDRESS; 2174 if (exe_ctx && !target->GetSectionLoadList().IsEmpty()) 2175 vaddr = format_addr.GetLoadAddress (target); 2176 if (vaddr == LLDB_INVALID_ADDRESS) 2177 vaddr = format_addr.GetFileAddress (); 2178 2179 if (vaddr != LLDB_INVALID_ADDRESS) 2180 { 2181 int addr_width = target->GetArchitecture().GetAddressByteSize() * 2; 2182 if (addr_width == 0) 2183 addr_width = 16; 2184 s.Printf("0x%*.*llx", addr_width, addr_width, vaddr); 2185 var_success = true; 2186 } 2187 } 2188 } 2189 } 2190 2191 if (var_success == false) 2192 success = false; 2193 } 2194 p = var_name_end; 2195 } 2196 else 2197 break; 2198 } 2199 else 2200 { 2201 // We got a dollar sign with no '{' after it, it must just be a dollar sign 2202 s.PutChar(*p); 2203 } 2204 } 2205 else if (*p == '\\') 2206 { 2207 ++p; // skip the slash 2208 switch (*p) 2209 { 2210 case 'a': s.PutChar ('\a'); break; 2211 case 'b': s.PutChar ('\b'); break; 2212 case 'f': s.PutChar ('\f'); break; 2213 case 'n': s.PutChar ('\n'); break; 2214 case 'r': s.PutChar ('\r'); break; 2215 case 't': s.PutChar ('\t'); break; 2216 case 'v': s.PutChar ('\v'); break; 2217 case '\'': s.PutChar ('\''); break; 2218 case '\\': s.PutChar ('\\'); break; 2219 case '0': 2220 // 1 to 3 octal chars 2221 { 2222 // Make a string that can hold onto the initial zero char, 2223 // up to 3 octal digits, and a terminating NULL. 2224 char oct_str[5] = { 0, 0, 0, 0, 0 }; 2225 2226 int i; 2227 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i) 2228 oct_str[i] = p[i]; 2229 2230 // We don't want to consume the last octal character since 2231 // the main for loop will do this for us, so we advance p by 2232 // one less than i (even if i is zero) 2233 p += i - 1; 2234 unsigned long octal_value = ::strtoul (oct_str, NULL, 8); 2235 if (octal_value <= UINT8_MAX) 2236 { 2237 char octal_char = octal_value; 2238 s.Write (&octal_char, 1); 2239 } 2240 } 2241 break; 2242 2243 case 'x': 2244 // hex number in the format 2245 if (isxdigit(p[1])) 2246 { 2247 ++p; // Skip the 'x' 2248 2249 // Make a string that can hold onto two hex chars plus a 2250 // NULL terminator 2251 char hex_str[3] = { 0,0,0 }; 2252 hex_str[0] = *p; 2253 if (isxdigit(p[1])) 2254 { 2255 ++p; // Skip the first of the two hex chars 2256 hex_str[1] = *p; 2257 } 2258 2259 unsigned long hex_value = strtoul (hex_str, NULL, 16); 2260 if (hex_value <= UINT8_MAX) 2261 s.PutChar (hex_value); 2262 } 2263 else 2264 { 2265 s.PutChar('x'); 2266 } 2267 break; 2268 2269 default: 2270 // Just desensitize any other character by just printing what 2271 // came after the '\' 2272 s << *p; 2273 break; 2274 2275 } 2276 2277 } 2278 } 2279 if (end) 2280 *end = p; 2281 return success; 2282 } 2283 2284 #pragma mark Debugger::SettingsController 2285 2286 //-------------------------------------------------- 2287 // class Debugger::SettingsController 2288 //-------------------------------------------------- 2289 2290 Debugger::SettingsController::SettingsController () : 2291 UserSettingsController ("", UserSettingsControllerSP()) 2292 { 2293 m_default_settings.reset (new DebuggerInstanceSettings (*this, false, 2294 InstanceSettings::GetDefaultName().AsCString())); 2295 } 2296 2297 Debugger::SettingsController::~SettingsController () 2298 { 2299 } 2300 2301 2302 InstanceSettingsSP 2303 Debugger::SettingsController::CreateInstanceSettings (const char *instance_name) 2304 { 2305 DebuggerInstanceSettings *new_settings = new DebuggerInstanceSettings (*GetSettingsController(), 2306 false, instance_name); 2307 InstanceSettingsSP new_settings_sp (new_settings); 2308 return new_settings_sp; 2309 } 2310 2311 #pragma mark DebuggerInstanceSettings 2312 //-------------------------------------------------- 2313 // class DebuggerInstanceSettings 2314 //-------------------------------------------------- 2315 2316 DebuggerInstanceSettings::DebuggerInstanceSettings 2317 ( 2318 UserSettingsController &owner, 2319 bool live_instance, 2320 const char *name 2321 ) : 2322 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance), 2323 m_term_width (80), 2324 m_stop_source_before_count (3), 2325 m_stop_source_after_count (3), 2326 m_stop_disassembly_count (4), 2327 m_stop_disassembly_display (eStopDisassemblyTypeNoSource), 2328 m_prompt (), 2329 m_frame_format (), 2330 m_thread_format (), 2331 m_script_lang (), 2332 m_use_external_editor (false), 2333 m_auto_confirm_on (false) 2334 { 2335 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called 2336 // until the vtables for DebuggerInstanceSettings are properly set up, i.e. AFTER all the initializers. 2337 // For this reason it has to be called here, rather than in the initializer or in the parent constructor. 2338 // The same is true of CreateInstanceName(). 2339 2340 if (GetInstanceName() == InstanceSettings::InvalidName()) 2341 { 2342 ChangeInstanceName (std::string (CreateInstanceName().AsCString())); 2343 m_owner.RegisterInstanceSettings (this); 2344 } 2345 2346 if (live_instance) 2347 { 2348 const InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name); 2349 CopyInstanceSettings (pending_settings, false); 2350 } 2351 } 2352 2353 DebuggerInstanceSettings::DebuggerInstanceSettings (const DebuggerInstanceSettings &rhs) : 2354 InstanceSettings (*Debugger::GetSettingsController(), CreateInstanceName ().AsCString()), 2355 m_prompt (rhs.m_prompt), 2356 m_frame_format (rhs.m_frame_format), 2357 m_thread_format (rhs.m_thread_format), 2358 m_script_lang (rhs.m_script_lang), 2359 m_use_external_editor (rhs.m_use_external_editor), 2360 m_auto_confirm_on(rhs.m_auto_confirm_on) 2361 { 2362 const InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name); 2363 CopyInstanceSettings (pending_settings, false); 2364 m_owner.RemovePendingSettings (m_instance_name); 2365 } 2366 2367 DebuggerInstanceSettings::~DebuggerInstanceSettings () 2368 { 2369 } 2370 2371 DebuggerInstanceSettings& 2372 DebuggerInstanceSettings::operator= (const DebuggerInstanceSettings &rhs) 2373 { 2374 if (this != &rhs) 2375 { 2376 m_term_width = rhs.m_term_width; 2377 m_prompt = rhs.m_prompt; 2378 m_frame_format = rhs.m_frame_format; 2379 m_thread_format = rhs.m_thread_format; 2380 m_script_lang = rhs.m_script_lang; 2381 m_use_external_editor = rhs.m_use_external_editor; 2382 m_auto_confirm_on = rhs.m_auto_confirm_on; 2383 } 2384 2385 return *this; 2386 } 2387 2388 bool 2389 DebuggerInstanceSettings::ValidTermWidthValue (const char *value, Error err) 2390 { 2391 bool valid = false; 2392 2393 // Verify we have a value string. 2394 if (value == NULL || value[0] == '\0') 2395 { 2396 err.SetErrorString ("missing value, can't set terminal width without a value"); 2397 } 2398 else 2399 { 2400 char *end = NULL; 2401 const uint32_t width = ::strtoul (value, &end, 0); 2402 2403 if (end && end[0] == '\0') 2404 { 2405 if (width >= 10 && width <= 1024) 2406 valid = true; 2407 else 2408 err.SetErrorString ("invalid term-width value; value must be between 10 and 1024"); 2409 } 2410 else 2411 err.SetErrorStringWithFormat ("'%s' is not a valid unsigned integer string", value); 2412 } 2413 2414 return valid; 2415 } 2416 2417 2418 void 2419 DebuggerInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name, 2420 const char *index_value, 2421 const char *value, 2422 const ConstString &instance_name, 2423 const SettingEntry &entry, 2424 VarSetOperationType op, 2425 Error &err, 2426 bool pending) 2427 { 2428 2429 if (var_name == TermWidthVarName()) 2430 { 2431 if (ValidTermWidthValue (value, err)) 2432 { 2433 m_term_width = ::strtoul (value, NULL, 0); 2434 } 2435 } 2436 else if (var_name == PromptVarName()) 2437 { 2438 UserSettingsController::UpdateStringVariable (op, m_prompt, value, err); 2439 if (!pending) 2440 { 2441 // 'instance_name' is actually (probably) in the form '[<instance_name>]'; if so, we need to 2442 // strip off the brackets before passing it to BroadcastPromptChange. 2443 2444 std::string tmp_instance_name (instance_name.AsCString()); 2445 if ((tmp_instance_name[0] == '[') 2446 && (tmp_instance_name[instance_name.GetLength() - 1] == ']')) 2447 tmp_instance_name = tmp_instance_name.substr (1, instance_name.GetLength() - 2); 2448 ConstString new_name (tmp_instance_name.c_str()); 2449 2450 BroadcastPromptChange (new_name, m_prompt.c_str()); 2451 } 2452 } 2453 else if (var_name == GetFrameFormatName()) 2454 { 2455 UserSettingsController::UpdateStringVariable (op, m_frame_format, value, err); 2456 } 2457 else if (var_name == GetThreadFormatName()) 2458 { 2459 UserSettingsController::UpdateStringVariable (op, m_thread_format, value, err); 2460 } 2461 else if (var_name == ScriptLangVarName()) 2462 { 2463 bool success; 2464 m_script_lang = Args::StringToScriptLanguage (value, eScriptLanguageDefault, 2465 &success); 2466 } 2467 else if (var_name == UseExternalEditorVarName ()) 2468 { 2469 UserSettingsController::UpdateBooleanVariable (op, m_use_external_editor, value, false, err); 2470 } 2471 else if (var_name == AutoConfirmName ()) 2472 { 2473 UserSettingsController::UpdateBooleanVariable (op, m_auto_confirm_on, value, false, err); 2474 } 2475 else if (var_name == StopSourceContextBeforeName ()) 2476 { 2477 uint32_t new_value = Args::StringToUInt32(value, UINT32_MAX, 10, NULL); 2478 if (new_value != UINT32_MAX) 2479 m_stop_source_before_count = new_value; 2480 else 2481 err.SetErrorStringWithFormat("invalid unsigned string value '%s' for the '%s' setting", value, StopSourceContextAfterName ().GetCString()); 2482 } 2483 else if (var_name == StopSourceContextAfterName ()) 2484 { 2485 uint32_t new_value = Args::StringToUInt32(value, UINT32_MAX, 10, NULL); 2486 if (new_value != UINT32_MAX) 2487 m_stop_source_after_count = new_value; 2488 else 2489 err.SetErrorStringWithFormat("invalid unsigned string value '%s' for the '%s' setting", value, StopSourceContextBeforeName ().GetCString()); 2490 } 2491 else if (var_name == StopDisassemblyCountName ()) 2492 { 2493 uint32_t new_value = Args::StringToUInt32(value, UINT32_MAX, 10, NULL); 2494 if (new_value != UINT32_MAX) 2495 m_stop_disassembly_count = new_value; 2496 else 2497 err.SetErrorStringWithFormat("invalid unsigned string value '%s' for the '%s' setting", value, StopDisassemblyCountName ().GetCString()); 2498 } 2499 else if (var_name == StopDisassemblyDisplayName ()) 2500 { 2501 int new_value; 2502 UserSettingsController::UpdateEnumVariable (g_show_disassembly_enum_values, &new_value, value, err); 2503 if (err.Success()) 2504 m_stop_disassembly_display = (StopDisassemblyType)new_value; 2505 } 2506 } 2507 2508 bool 2509 DebuggerInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry, 2510 const ConstString &var_name, 2511 StringList &value, 2512 Error *err) 2513 { 2514 if (var_name == PromptVarName()) 2515 { 2516 value.AppendString (m_prompt.c_str(), m_prompt.size()); 2517 2518 } 2519 else if (var_name == ScriptLangVarName()) 2520 { 2521 value.AppendString (ScriptInterpreter::LanguageToString (m_script_lang).c_str()); 2522 } 2523 else if (var_name == TermWidthVarName()) 2524 { 2525 StreamString width_str; 2526 width_str.Printf ("%u", m_term_width); 2527 value.AppendString (width_str.GetData()); 2528 } 2529 else if (var_name == GetFrameFormatName ()) 2530 { 2531 value.AppendString(m_frame_format.c_str(), m_frame_format.size()); 2532 } 2533 else if (var_name == GetThreadFormatName ()) 2534 { 2535 value.AppendString(m_thread_format.c_str(), m_thread_format.size()); 2536 } 2537 else if (var_name == UseExternalEditorVarName()) 2538 { 2539 if (m_use_external_editor) 2540 value.AppendString ("true"); 2541 else 2542 value.AppendString ("false"); 2543 } 2544 else if (var_name == AutoConfirmName()) 2545 { 2546 if (m_auto_confirm_on) 2547 value.AppendString ("true"); 2548 else 2549 value.AppendString ("false"); 2550 } 2551 else if (var_name == StopSourceContextAfterName ()) 2552 { 2553 StreamString strm; 2554 strm.Printf ("%u", m_stop_source_before_count); 2555 value.AppendString (strm.GetData()); 2556 } 2557 else if (var_name == StopSourceContextBeforeName ()) 2558 { 2559 StreamString strm; 2560 strm.Printf ("%u", m_stop_source_after_count); 2561 value.AppendString (strm.GetData()); 2562 } 2563 else if (var_name == StopDisassemblyCountName ()) 2564 { 2565 StreamString strm; 2566 strm.Printf ("%u", m_stop_disassembly_count); 2567 value.AppendString (strm.GetData()); 2568 } 2569 else if (var_name == StopDisassemblyDisplayName ()) 2570 { 2571 if (m_stop_disassembly_display >= eStopDisassemblyTypeNever && m_stop_disassembly_display <= eStopDisassemblyTypeAlways) 2572 value.AppendString (g_show_disassembly_enum_values[m_stop_disassembly_display].string_value); 2573 else 2574 value.AppendString ("<invalid>"); 2575 } 2576 else 2577 { 2578 if (err) 2579 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString()); 2580 return false; 2581 } 2582 return true; 2583 } 2584 2585 void 2586 DebuggerInstanceSettings::CopyInstanceSettings (const InstanceSettingsSP &new_settings, 2587 bool pending) 2588 { 2589 if (new_settings.get() == NULL) 2590 return; 2591 2592 DebuggerInstanceSettings *new_debugger_settings = (DebuggerInstanceSettings *) new_settings.get(); 2593 2594 m_prompt = new_debugger_settings->m_prompt; 2595 if (!pending) 2596 { 2597 // 'instance_name' is actually (probably) in the form '[<instance_name>]'; if so, we need to 2598 // strip off the brackets before passing it to BroadcastPromptChange. 2599 2600 std::string tmp_instance_name (m_instance_name.AsCString()); 2601 if ((tmp_instance_name[0] == '[') 2602 && (tmp_instance_name[m_instance_name.GetLength() - 1] == ']')) 2603 tmp_instance_name = tmp_instance_name.substr (1, m_instance_name.GetLength() - 2); 2604 ConstString new_name (tmp_instance_name.c_str()); 2605 2606 BroadcastPromptChange (new_name, m_prompt.c_str()); 2607 } 2608 m_frame_format = new_debugger_settings->m_frame_format; 2609 m_thread_format = new_debugger_settings->m_thread_format; 2610 m_term_width = new_debugger_settings->m_term_width; 2611 m_script_lang = new_debugger_settings->m_script_lang; 2612 m_use_external_editor = new_debugger_settings->m_use_external_editor; 2613 m_auto_confirm_on = new_debugger_settings->m_auto_confirm_on; 2614 } 2615 2616 2617 bool 2618 DebuggerInstanceSettings::BroadcastPromptChange (const ConstString &instance_name, const char *new_prompt) 2619 { 2620 std::string tmp_prompt; 2621 2622 if (new_prompt != NULL) 2623 { 2624 tmp_prompt = new_prompt ; 2625 int len = tmp_prompt.size(); 2626 if (len > 1 2627 && (tmp_prompt[0] == '\'' || tmp_prompt[0] == '"') 2628 && (tmp_prompt[len-1] == tmp_prompt[0])) 2629 { 2630 tmp_prompt = tmp_prompt.substr(1,len-2); 2631 } 2632 len = tmp_prompt.size(); 2633 if (tmp_prompt[len-1] != ' ') 2634 tmp_prompt.append(" "); 2635 } 2636 EventSP new_event_sp; 2637 new_event_sp.reset (new Event(CommandInterpreter::eBroadcastBitResetPrompt, 2638 new EventDataBytes (tmp_prompt.c_str()))); 2639 2640 if (instance_name.GetLength() != 0) 2641 { 2642 // Set prompt for a particular instance. 2643 Debugger *dbg = Debugger::FindDebuggerWithInstanceName (instance_name).get(); 2644 if (dbg != NULL) 2645 { 2646 dbg->GetCommandInterpreter().BroadcastEvent (new_event_sp); 2647 } 2648 } 2649 2650 return true; 2651 } 2652 2653 const ConstString 2654 DebuggerInstanceSettings::CreateInstanceName () 2655 { 2656 static int instance_count = 1; 2657 StreamString sstr; 2658 2659 sstr.Printf ("debugger_%d", instance_count); 2660 ++instance_count; 2661 2662 const ConstString ret_val (sstr.GetData()); 2663 2664 return ret_val; 2665 } 2666 2667 2668 //-------------------------------------------------- 2669 // SettingsController Variable Tables 2670 //-------------------------------------------------- 2671 2672 2673 SettingEntry 2674 Debugger::SettingsController::global_settings_table[] = 2675 { 2676 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"}, 2677 // The Debugger level global table should always be empty; all Debugger settable variables should be instance 2678 // variables. 2679 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL } 2680 }; 2681 2682 #define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name}${function.pc-offset}}}" 2683 #define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}" 2684 2685 #define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id}"\ 2686 "{, ${frame.pc}}"\ 2687 MODULE_WITH_FUNC\ 2688 FILE_AND_LINE\ 2689 "{, stop reason = ${thread.stop-reason}}"\ 2690 "{\\nReturn value: ${thread.return-value}}"\ 2691 "\\n" 2692 2693 //#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id}"\ 2694 // "{, ${frame.pc}}"\ 2695 // MODULE_WITH_FUNC\ 2696 // FILE_AND_LINE\ 2697 // "{, stop reason = ${thread.stop-reason}}"\ 2698 // "{, name = ${thread.name}}"\ 2699 // "{, queue = ${thread.queue}}"\ 2700 // "\\n" 2701 2702 #define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\ 2703 MODULE_WITH_FUNC\ 2704 FILE_AND_LINE\ 2705 "\\n" 2706 2707 SettingEntry 2708 Debugger::SettingsController::instance_settings_table[] = 2709 { 2710 // NAME Setting variable type Default Enum Init'd Hidden Help 2711 // ======================= ======================= ====================== ==== ====== ====== ====================== 2712 { "frame-format", eSetVarTypeString, DEFAULT_FRAME_FORMAT, NULL, false, false, "The default frame format string to use when displaying thread information." }, 2713 { "prompt", eSetVarTypeString, "(lldb) ", NULL, false, false, "The debugger command line prompt displayed for the user." }, 2714 { "script-lang", eSetVarTypeString, "python", NULL, false, false, "The script language to be used for evaluating user-written scripts." }, 2715 { "term-width", eSetVarTypeInt, "80" , NULL, false, false, "The maximum number of columns to use for displaying text." }, 2716 { "thread-format", eSetVarTypeString, DEFAULT_THREAD_FORMAT, NULL, false, false, "The default thread format string to use when displaying thread information." }, 2717 { "use-external-editor", eSetVarTypeBoolean, "false", NULL, false, false, "Whether to use an external editor or not." }, 2718 { "auto-confirm", eSetVarTypeBoolean, "false", NULL, false, false, "If true all confirmation prompts will receive their default reply." }, 2719 { "stop-line-count-before",eSetVarTypeInt, "3", NULL, false, false, "The number of sources lines to display that come before the current source line when displaying a stopped context." }, 2720 { "stop-line-count-after", eSetVarTypeInt, "3", NULL, false, false, "The number of sources lines to display that come after the current source line when displaying a stopped context." }, 2721 { "stop-disassembly-count", eSetVarTypeInt, "0", NULL, false, false, "The number of disassembly lines to show when displaying a stopped context." }, 2722 { "stop-disassembly-display", eSetVarTypeEnum, "no-source", g_show_disassembly_enum_values, false, false, "Control when to display disassembly when displaying a stopped context." }, 2723 { NULL, eSetVarTypeNone, NULL, NULL, false, false, NULL } 2724 }; 2725