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