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/lldb-python.h" 11 12 #include "lldb/Core/Debugger.h" 13 14 #include <map> 15 16 #include "clang/AST/DeclCXX.h" 17 #include "clang/AST/Type.h" 18 #include "llvm/ADT/StringRef.h" 19 20 #include "lldb/lldb-private.h" 21 #include "lldb/Core/Module.h" 22 #include "lldb/Core/PluginManager.h" 23 #include "lldb/Core/RegisterValue.h" 24 #include "lldb/Core/State.h" 25 #include "lldb/Core/StreamAsynchronousIO.h" 26 #include "lldb/Core/StreamCallback.h" 27 #include "lldb/Core/StreamFile.h" 28 #include "lldb/Core/StreamString.h" 29 #include "lldb/Core/StructuredData.h" 30 #include "lldb/Core/Timer.h" 31 #include "lldb/Core/ValueObject.h" 32 #include "lldb/Core/ValueObjectVariable.h" 33 #include "lldb/DataFormatters/DataVisualization.h" 34 #include "lldb/DataFormatters/FormatManager.h" 35 #include "lldb/DataFormatters/TypeSummary.h" 36 #include "lldb/Host/ConnectionFileDescriptor.h" 37 #include "lldb/Host/HostInfo.h" 38 #include "lldb/Host/Terminal.h" 39 #include "lldb/Host/ThreadLauncher.h" 40 #include "lldb/Interpreter/CommandInterpreter.h" 41 #include "lldb/Interpreter/OptionValueSInt64.h" 42 #include "lldb/Interpreter/OptionValueString.h" 43 #include "lldb/Symbol/ClangASTContext.h" 44 #include "lldb/Symbol/CompileUnit.h" 45 #include "lldb/Symbol/Function.h" 46 #include "lldb/Symbol/Symbol.h" 47 #include "lldb/Symbol/VariableList.h" 48 #include "lldb/Target/CPPLanguageRuntime.h" 49 #include "lldb/Target/ObjCLanguageRuntime.h" 50 #include "lldb/Target/TargetList.h" 51 #include "lldb/Target/Process.h" 52 #include "lldb/Target/RegisterContext.h" 53 #include "lldb/Target/SectionLoadList.h" 54 #include "lldb/Target/StopInfo.h" 55 #include "lldb/Target/Target.h" 56 #include "lldb/Target/Thread.h" 57 #include "lldb/Utility/AnsiTerminal.h" 58 59 #include "llvm/Support/DynamicLibrary.h" 60 61 using namespace lldb; 62 using namespace lldb_private; 63 64 65 static uint32_t g_shared_debugger_refcount = 0; 66 static lldb::user_id_t g_unique_id = 1; 67 68 #pragma mark Static Functions 69 70 static Mutex & 71 GetDebuggerListMutex () 72 { 73 static Mutex g_mutex(Mutex::eMutexTypeRecursive); 74 return g_mutex; 75 } 76 77 typedef std::vector<DebuggerSP> DebuggerList; 78 79 static DebuggerList & 80 GetDebuggerList() 81 { 82 // hide the static debugger list inside a singleton accessor to avoid 83 // global init constructors 84 static DebuggerList g_list; 85 return g_list; 86 } 87 88 OptionEnumValueElement 89 g_show_disassembly_enum_values[] = 90 { 91 { Debugger::eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."}, 92 { Debugger::eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."}, 93 { Debugger::eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."}, 94 { 0, NULL, NULL } 95 }; 96 97 OptionEnumValueElement 98 g_language_enumerators[] = 99 { 100 { eScriptLanguageNone, "none", "Disable scripting languages."}, 101 { eScriptLanguagePython, "python", "Select python as the default scripting language."}, 102 { eScriptLanguageDefault, "default", "Select the lldb default as the default scripting language."}, 103 { 0, NULL, NULL } 104 }; 105 106 #define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name-with-args}${function.pc-offset}}}" 107 #define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}" 108 109 #define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id%tid}"\ 110 "{, ${frame.pc}}"\ 111 MODULE_WITH_FUNC\ 112 FILE_AND_LINE\ 113 "{, name = '${thread.name}'}"\ 114 "{, queue = '${thread.queue}'}"\ 115 "{, activity = '${thread.info.activity.name}'}" \ 116 "{, ${thread.info.trace_messages} messages}" \ 117 "{, stop reason = ${thread.stop-reason}}"\ 118 "{\\nReturn value: ${thread.return-value}}"\ 119 "{\\nCompleted expression: ${thread.completed-expression}}"\ 120 "\\n" 121 122 #define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\ 123 MODULE_WITH_FUNC\ 124 FILE_AND_LINE\ 125 "\\n" 126 127 #define DEFAULT_DISASSEMBLY_FORMAT "${addr-file-or-load} <${function.name-without-args}${function.concrete-only-addr-offset-no-padding}>: " 128 129 130 static PropertyDefinition 131 g_properties[] = 132 { 133 { "auto-confirm", OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true all confirmation prompts will receive their default reply." }, 134 { "disassembly-format", OptionValue::eTypeString , true, 0 , DEFAULT_DISASSEMBLY_FORMAT, NULL, "The default disassembly format string to use when disassembling instruction sequences." }, 135 { "frame-format", OptionValue::eTypeString , true, 0 , DEFAULT_FRAME_FORMAT, NULL, "The default frame format string to use when displaying stack frame information for threads." }, 136 { "notify-void", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Notify the user explicitly if an expression returns void (default: false)." }, 137 { "prompt", OptionValue::eTypeString , true, OptionValueString::eOptionEncodeCharacterEscapeSequences, "(lldb) ", NULL, "The debugger command line prompt displayed for the user." }, 138 { "script-lang", OptionValue::eTypeEnum , true, eScriptLanguagePython, NULL, g_language_enumerators, "The script language to be used for evaluating user-written scripts." }, 139 { "stop-disassembly-count", OptionValue::eTypeSInt64 , true, 4 , NULL, NULL, "The number of disassembly lines to show when displaying a stopped context." }, 140 { "stop-disassembly-display", OptionValue::eTypeEnum , true, Debugger::eStopDisassemblyTypeNoSource, NULL, g_show_disassembly_enum_values, "Control when to display disassembly when displaying a stopped context." }, 141 { "stop-line-count-after", OptionValue::eTypeSInt64 , true, 3 , NULL, NULL, "The number of sources lines to display that come after the current source line when displaying a stopped context." }, 142 { "stop-line-count-before", OptionValue::eTypeSInt64 , true, 3 , NULL, NULL, "The number of sources lines to display that come before the current source line when displaying a stopped context." }, 143 { "term-width", OptionValue::eTypeSInt64 , true, 80 , NULL, NULL, "The maximum number of columns to use for displaying text." }, 144 { "thread-format", OptionValue::eTypeString , true, 0 , DEFAULT_THREAD_FORMAT, NULL, "The default thread format string to use when displaying thread information." }, 145 { "use-external-editor", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Whether to use an external editor or not." }, 146 { "use-color", OptionValue::eTypeBoolean, true, true , NULL, NULL, "Whether to use Ansi color codes or not." }, 147 { "auto-one-line-summaries", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, LLDB will automatically display small structs in one-liner format (default: true)." }, 148 149 { NULL, OptionValue::eTypeInvalid, true, 0 , NULL, NULL, NULL } 150 }; 151 152 enum 153 { 154 ePropertyAutoConfirm = 0, 155 ePropertyDisassemblyFormat, 156 ePropertyFrameFormat, 157 ePropertyNotiftVoid, 158 ePropertyPrompt, 159 ePropertyScriptLanguage, 160 ePropertyStopDisassemblyCount, 161 ePropertyStopDisassemblyDisplay, 162 ePropertyStopLineCountAfter, 163 ePropertyStopLineCountBefore, 164 ePropertyTerminalWidth, 165 ePropertyThreadFormat, 166 ePropertyUseExternalEditor, 167 ePropertyUseColor, 168 ePropertyAutoOneLineSummaries 169 }; 170 171 Debugger::LoadPluginCallbackType Debugger::g_load_plugin_callback = NULL; 172 173 Error 174 Debugger::SetPropertyValue (const ExecutionContext *exe_ctx, 175 VarSetOperationType op, 176 const char *property_path, 177 const char *value) 178 { 179 bool is_load_script = strcmp(property_path,"target.load-script-from-symbol-file") == 0; 180 TargetSP target_sp; 181 LoadScriptFromSymFile load_script_old_value; 182 if (is_load_script && exe_ctx->GetTargetSP()) 183 { 184 target_sp = exe_ctx->GetTargetSP(); 185 load_script_old_value = target_sp->TargetProperties::GetLoadScriptFromSymbolFile(); 186 } 187 Error error (Properties::SetPropertyValue (exe_ctx, op, property_path, value)); 188 if (error.Success()) 189 { 190 // FIXME it would be nice to have "on-change" callbacks for properties 191 if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0) 192 { 193 const char *new_prompt = GetPrompt(); 194 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor()); 195 if (str.length()) 196 new_prompt = str.c_str(); 197 GetCommandInterpreter().UpdatePrompt(new_prompt); 198 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt))); 199 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp); 200 } 201 else if (strcmp(property_path, g_properties[ePropertyUseColor].name) == 0) 202 { 203 // use-color changed. Ping the prompt so it can reset the ansi terminal codes. 204 SetPrompt (GetPrompt()); 205 } 206 else if (is_load_script && target_sp && load_script_old_value == eLoadScriptFromSymFileWarn) 207 { 208 if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() == eLoadScriptFromSymFileTrue) 209 { 210 std::list<Error> errors; 211 StreamString feedback_stream; 212 if (!target_sp->LoadScriptingResources(errors,&feedback_stream)) 213 { 214 StreamFileSP stream_sp (GetErrorFile()); 215 if (stream_sp) 216 { 217 for (auto error : errors) 218 { 219 stream_sp->Printf("%s\n",error.AsCString()); 220 } 221 if (feedback_stream.GetSize()) 222 stream_sp->Printf("%s",feedback_stream.GetData()); 223 } 224 } 225 } 226 } 227 } 228 return error; 229 } 230 231 bool 232 Debugger::GetAutoConfirm () const 233 { 234 const uint32_t idx = ePropertyAutoConfirm; 235 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); 236 } 237 238 const char * 239 Debugger::GetDisassemblyFormat() const 240 { 241 const uint32_t idx = ePropertyDisassemblyFormat; 242 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value); 243 } 244 245 const char * 246 Debugger::GetFrameFormat() const 247 { 248 const uint32_t idx = ePropertyFrameFormat; 249 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value); 250 } 251 252 bool 253 Debugger::GetNotifyVoid () const 254 { 255 const uint32_t idx = ePropertyNotiftVoid; 256 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); 257 } 258 259 const char * 260 Debugger::GetPrompt() const 261 { 262 const uint32_t idx = ePropertyPrompt; 263 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value); 264 } 265 266 void 267 Debugger::SetPrompt(const char *p) 268 { 269 const uint32_t idx = ePropertyPrompt; 270 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p); 271 const char *new_prompt = GetPrompt(); 272 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor()); 273 if (str.length()) 274 new_prompt = str.c_str(); 275 GetCommandInterpreter().UpdatePrompt(new_prompt); 276 } 277 278 const char * 279 Debugger::GetThreadFormat() const 280 { 281 const uint32_t idx = ePropertyThreadFormat; 282 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value); 283 } 284 285 lldb::ScriptLanguage 286 Debugger::GetScriptLanguage() const 287 { 288 const uint32_t idx = ePropertyScriptLanguage; 289 return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value); 290 } 291 292 bool 293 Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang) 294 { 295 const uint32_t idx = ePropertyScriptLanguage; 296 return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang); 297 } 298 299 uint32_t 300 Debugger::GetTerminalWidth () const 301 { 302 const uint32_t idx = ePropertyTerminalWidth; 303 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value); 304 } 305 306 bool 307 Debugger::SetTerminalWidth (uint32_t term_width) 308 { 309 const uint32_t idx = ePropertyTerminalWidth; 310 return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width); 311 } 312 313 bool 314 Debugger::GetUseExternalEditor () const 315 { 316 const uint32_t idx = ePropertyUseExternalEditor; 317 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); 318 } 319 320 bool 321 Debugger::SetUseExternalEditor (bool b) 322 { 323 const uint32_t idx = ePropertyUseExternalEditor; 324 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b); 325 } 326 327 bool 328 Debugger::GetUseColor () const 329 { 330 const uint32_t idx = ePropertyUseColor; 331 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); 332 } 333 334 bool 335 Debugger::SetUseColor (bool b) 336 { 337 const uint32_t idx = ePropertyUseColor; 338 bool ret = m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b); 339 SetPrompt (GetPrompt()); 340 return ret; 341 } 342 343 uint32_t 344 Debugger::GetStopSourceLineCount (bool before) const 345 { 346 const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter; 347 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value); 348 } 349 350 Debugger::StopDisassemblyType 351 Debugger::GetStopDisassemblyDisplay () const 352 { 353 const uint32_t idx = ePropertyStopDisassemblyDisplay; 354 return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value); 355 } 356 357 uint32_t 358 Debugger::GetDisassemblyLineCount () const 359 { 360 const uint32_t idx = ePropertyStopDisassemblyCount; 361 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value); 362 } 363 364 bool 365 Debugger::GetAutoOneLineSummaries () const 366 { 367 const uint32_t idx = ePropertyAutoOneLineSummaries; 368 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true); 369 370 } 371 372 #pragma mark Debugger 373 374 //const DebuggerPropertiesSP & 375 //Debugger::GetSettings() const 376 //{ 377 // return m_properties_sp; 378 //} 379 // 380 381 int 382 Debugger::TestDebuggerRefCount () 383 { 384 return g_shared_debugger_refcount; 385 } 386 387 void 388 Debugger::Initialize (LoadPluginCallbackType load_plugin_callback) 389 { 390 g_load_plugin_callback = load_plugin_callback; 391 if (g_shared_debugger_refcount++ == 0) 392 lldb_private::Initialize(); 393 } 394 395 void 396 Debugger::Terminate () 397 { 398 if (g_shared_debugger_refcount > 0) 399 { 400 g_shared_debugger_refcount--; 401 if (g_shared_debugger_refcount == 0) 402 { 403 lldb_private::WillTerminate(); 404 lldb_private::Terminate(); 405 406 // Clear our master list of debugger objects 407 Mutex::Locker locker (GetDebuggerListMutex ()); 408 GetDebuggerList().clear(); 409 } 410 } 411 } 412 413 void 414 Debugger::SettingsInitialize () 415 { 416 Target::SettingsInitialize (); 417 } 418 419 void 420 Debugger::SettingsTerminate () 421 { 422 Target::SettingsTerminate (); 423 } 424 425 bool 426 Debugger::LoadPlugin (const FileSpec& spec, Error& error) 427 { 428 if (g_load_plugin_callback) 429 { 430 llvm::sys::DynamicLibrary dynlib = g_load_plugin_callback (shared_from_this(), spec, error); 431 if (dynlib.isValid()) 432 { 433 m_loaded_plugins.push_back(dynlib); 434 return true; 435 } 436 } 437 else 438 { 439 // The g_load_plugin_callback is registered in SBDebugger::Initialize() 440 // and if the public API layer isn't available (code is linking against 441 // all of the internal LLDB static libraries), then we can't load plugins 442 error.SetErrorString("Public API layer is not available"); 443 } 444 return false; 445 } 446 447 static FileSpec::EnumerateDirectoryResult 448 LoadPluginCallback 449 ( 450 void *baton, 451 FileSpec::FileType file_type, 452 const FileSpec &file_spec 453 ) 454 { 455 Error error; 456 457 static ConstString g_dylibext("dylib"); 458 static ConstString g_solibext("so"); 459 460 if (!baton) 461 return FileSpec::eEnumerateDirectoryResultQuit; 462 463 Debugger *debugger = (Debugger*)baton; 464 465 // If we have a regular file, a symbolic link or unknown file type, try 466 // and process the file. We must handle unknown as sometimes the directory 467 // enumeration might be enumerating a file system that doesn't have correct 468 // file type information. 469 if (file_type == FileSpec::eFileTypeRegular || 470 file_type == FileSpec::eFileTypeSymbolicLink || 471 file_type == FileSpec::eFileTypeUnknown ) 472 { 473 FileSpec plugin_file_spec (file_spec); 474 plugin_file_spec.ResolvePath (); 475 476 if (plugin_file_spec.GetFileNameExtension() != g_dylibext && 477 plugin_file_spec.GetFileNameExtension() != g_solibext) 478 { 479 return FileSpec::eEnumerateDirectoryResultNext; 480 } 481 482 Error plugin_load_error; 483 debugger->LoadPlugin (plugin_file_spec, plugin_load_error); 484 485 return FileSpec::eEnumerateDirectoryResultNext; 486 } 487 488 else if (file_type == FileSpec::eFileTypeUnknown || 489 file_type == FileSpec::eFileTypeDirectory || 490 file_type == FileSpec::eFileTypeSymbolicLink ) 491 { 492 // Try and recurse into anything that a directory or symbolic link. 493 // We must also do this for unknown as sometimes the directory enumeration 494 // might be enumerating a file system that doesn't have correct file type 495 // information. 496 return FileSpec::eEnumerateDirectoryResultEnter; 497 } 498 499 return FileSpec::eEnumerateDirectoryResultNext; 500 } 501 502 void 503 Debugger::InstanceInitialize () 504 { 505 FileSpec dir_spec; 506 const bool find_directories = true; 507 const bool find_files = true; 508 const bool find_other = true; 509 char dir_path[PATH_MAX]; 510 if (HostInfo::GetLLDBPath(ePathTypeLLDBSystemPlugins, dir_spec)) 511 { 512 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path))) 513 { 514 FileSpec::EnumerateDirectory (dir_path, 515 find_directories, 516 find_files, 517 find_other, 518 LoadPluginCallback, 519 this); 520 } 521 } 522 523 if (HostInfo::GetLLDBPath(ePathTypeLLDBUserPlugins, dir_spec)) 524 { 525 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path))) 526 { 527 FileSpec::EnumerateDirectory (dir_path, 528 find_directories, 529 find_files, 530 find_other, 531 LoadPluginCallback, 532 this); 533 } 534 } 535 536 PluginManager::DebuggerInitialize (*this); 537 } 538 539 DebuggerSP 540 Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton) 541 { 542 DebuggerSP debugger_sp (new Debugger(log_callback, baton)); 543 if (g_shared_debugger_refcount > 0) 544 { 545 Mutex::Locker locker (GetDebuggerListMutex ()); 546 GetDebuggerList().push_back(debugger_sp); 547 } 548 debugger_sp->InstanceInitialize (); 549 return debugger_sp; 550 } 551 552 void 553 Debugger::Destroy (DebuggerSP &debugger_sp) 554 { 555 if (debugger_sp.get() == NULL) 556 return; 557 558 debugger_sp->Clear(); 559 560 if (g_shared_debugger_refcount > 0) 561 { 562 Mutex::Locker locker (GetDebuggerListMutex ()); 563 DebuggerList &debugger_list = GetDebuggerList (); 564 DebuggerList::iterator pos, end = debugger_list.end(); 565 for (pos = debugger_list.begin (); pos != end; ++pos) 566 { 567 if ((*pos).get() == debugger_sp.get()) 568 { 569 debugger_list.erase (pos); 570 return; 571 } 572 } 573 } 574 } 575 576 DebuggerSP 577 Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name) 578 { 579 DebuggerSP debugger_sp; 580 if (g_shared_debugger_refcount > 0) 581 { 582 Mutex::Locker locker (GetDebuggerListMutex ()); 583 DebuggerList &debugger_list = GetDebuggerList(); 584 DebuggerList::iterator pos, end = debugger_list.end(); 585 586 for (pos = debugger_list.begin(); pos != end; ++pos) 587 { 588 if ((*pos).get()->m_instance_name == instance_name) 589 { 590 debugger_sp = *pos; 591 break; 592 } 593 } 594 } 595 return debugger_sp; 596 } 597 598 TargetSP 599 Debugger::FindTargetWithProcessID (lldb::pid_t pid) 600 { 601 TargetSP target_sp; 602 if (g_shared_debugger_refcount > 0) 603 { 604 Mutex::Locker locker (GetDebuggerListMutex ()); 605 DebuggerList &debugger_list = GetDebuggerList(); 606 DebuggerList::iterator pos, end = debugger_list.end(); 607 for (pos = debugger_list.begin(); pos != end; ++pos) 608 { 609 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid); 610 if (target_sp) 611 break; 612 } 613 } 614 return target_sp; 615 } 616 617 TargetSP 618 Debugger::FindTargetWithProcess (Process *process) 619 { 620 TargetSP target_sp; 621 if (g_shared_debugger_refcount > 0) 622 { 623 Mutex::Locker locker (GetDebuggerListMutex ()); 624 DebuggerList &debugger_list = GetDebuggerList(); 625 DebuggerList::iterator pos, end = debugger_list.end(); 626 for (pos = debugger_list.begin(); pos != end; ++pos) 627 { 628 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process); 629 if (target_sp) 630 break; 631 } 632 } 633 return target_sp; 634 } 635 636 Debugger::Debugger(lldb::LogOutputCallback log_callback, void *baton) : 637 UserID(g_unique_id++), 638 Properties(OptionValuePropertiesSP(new OptionValueProperties())), 639 m_input_file_sp(new StreamFile(stdin, false)), 640 m_output_file_sp(new StreamFile(stdout, false)), 641 m_error_file_sp(new StreamFile(stderr, false)), 642 m_terminal_state(), 643 m_target_list(*this), 644 m_platform_list(), 645 m_listener("lldb.Debugger"), 646 m_source_manager_ap(), 647 m_source_file_cache(), 648 m_command_interpreter_ap(new CommandInterpreter(*this, eScriptLanguageDefault, false)), 649 m_input_reader_stack(), 650 m_instance_name(), 651 m_loaded_plugins() 652 { 653 char instance_cstr[256]; 654 snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID()); 655 m_instance_name.SetCString(instance_cstr); 656 if (log_callback) 657 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton)); 658 m_command_interpreter_ap->Initialize (); 659 // Always add our default platform to the platform list 660 PlatformSP default_platform_sp (Platform::GetHostPlatform()); 661 assert (default_platform_sp.get()); 662 m_platform_list.Append (default_platform_sp, true); 663 664 m_collection_sp->Initialize (g_properties); 665 m_collection_sp->AppendProperty (ConstString("target"), 666 ConstString("Settings specify to debugging targets."), 667 true, 668 Target::GetGlobalProperties()->GetValueProperties()); 669 if (m_command_interpreter_ap.get()) 670 { 671 m_collection_sp->AppendProperty (ConstString("interpreter"), 672 ConstString("Settings specify to the debugger's command interpreter."), 673 true, 674 m_command_interpreter_ap->GetValueProperties()); 675 } 676 OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth); 677 term_width->SetMinimumValue(10); 678 term_width->SetMaximumValue(1024); 679 680 // Turn off use-color if this is a dumb terminal. 681 const char *term = getenv ("TERM"); 682 if (term && !strcmp (term, "dumb")) 683 SetUseColor (false); 684 } 685 686 Debugger::~Debugger () 687 { 688 Clear(); 689 } 690 691 void 692 Debugger::Clear() 693 { 694 ClearIOHandlers(); 695 StopIOHandlerThread(); 696 StopEventHandlerThread(); 697 m_listener.Clear(); 698 int num_targets = m_target_list.GetNumTargets(); 699 for (int i = 0; i < num_targets; i++) 700 { 701 TargetSP target_sp (m_target_list.GetTargetAtIndex (i)); 702 if (target_sp) 703 { 704 ProcessSP process_sp (target_sp->GetProcessSP()); 705 if (process_sp) 706 process_sp->Finalize(); 707 target_sp->Destroy(); 708 } 709 } 710 BroadcasterManager::Clear (); 711 712 // Close the input file _before_ we close the input read communications class 713 // as it does NOT own the input file, our m_input_file does. 714 m_terminal_state.Clear(); 715 if (m_input_file_sp) 716 m_input_file_sp->GetFile().Close (); 717 718 m_command_interpreter_ap->Clear(); 719 } 720 721 bool 722 Debugger::GetCloseInputOnEOF () const 723 { 724 // return m_input_comm.GetCloseOnEOF(); 725 return false; 726 } 727 728 void 729 Debugger::SetCloseInputOnEOF (bool b) 730 { 731 // m_input_comm.SetCloseOnEOF(b); 732 } 733 734 bool 735 Debugger::GetAsyncExecution () 736 { 737 return !m_command_interpreter_ap->GetSynchronous(); 738 } 739 740 void 741 Debugger::SetAsyncExecution (bool async_execution) 742 { 743 m_command_interpreter_ap->SetSynchronous (!async_execution); 744 } 745 746 747 void 748 Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership) 749 { 750 if (m_input_file_sp) 751 m_input_file_sp->GetFile().SetStream (fh, tranfer_ownership); 752 else 753 m_input_file_sp.reset (new StreamFile (fh, tranfer_ownership)); 754 755 File &in_file = m_input_file_sp->GetFile(); 756 if (in_file.IsValid() == false) 757 in_file.SetStream (stdin, true); 758 759 // Save away the terminal state if that is relevant, so that we can restore it in RestoreInputState. 760 SaveInputTerminalState (); 761 } 762 763 void 764 Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership) 765 { 766 if (m_output_file_sp) 767 m_output_file_sp->GetFile().SetStream (fh, tranfer_ownership); 768 else 769 m_output_file_sp.reset (new StreamFile (fh, tranfer_ownership)); 770 771 File &out_file = m_output_file_sp->GetFile(); 772 if (out_file.IsValid() == false) 773 out_file.SetStream (stdout, false); 774 775 // do not create the ScriptInterpreter just for setting the output file handle 776 // as the constructor will know how to do the right thing on its own 777 const bool can_create = false; 778 ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create); 779 if (script_interpreter) 780 script_interpreter->ResetOutputFileHandle (fh); 781 } 782 783 void 784 Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership) 785 { 786 if (m_error_file_sp) 787 m_error_file_sp->GetFile().SetStream (fh, tranfer_ownership); 788 else 789 m_error_file_sp.reset (new StreamFile (fh, tranfer_ownership)); 790 791 File &err_file = m_error_file_sp->GetFile(); 792 if (err_file.IsValid() == false) 793 err_file.SetStream (stderr, false); 794 } 795 796 void 797 Debugger::SaveInputTerminalState () 798 { 799 if (m_input_file_sp) 800 { 801 File &in_file = m_input_file_sp->GetFile(); 802 if (in_file.GetDescriptor() != File::kInvalidDescriptor) 803 m_terminal_state.Save(in_file.GetDescriptor(), true); 804 } 805 } 806 807 void 808 Debugger::RestoreInputTerminalState () 809 { 810 m_terminal_state.Restore(); 811 } 812 813 ExecutionContext 814 Debugger::GetSelectedExecutionContext () 815 { 816 ExecutionContext exe_ctx; 817 TargetSP target_sp(GetSelectedTarget()); 818 exe_ctx.SetTargetSP (target_sp); 819 820 if (target_sp) 821 { 822 ProcessSP process_sp (target_sp->GetProcessSP()); 823 exe_ctx.SetProcessSP (process_sp); 824 if (process_sp && process_sp->IsRunning() == false) 825 { 826 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread()); 827 if (thread_sp) 828 { 829 exe_ctx.SetThreadSP (thread_sp); 830 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame()); 831 if (exe_ctx.GetFramePtr() == NULL) 832 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0)); 833 } 834 } 835 } 836 return exe_ctx; 837 } 838 839 void 840 Debugger::DispatchInputInterrupt () 841 { 842 Mutex::Locker locker (m_input_reader_stack.GetMutex()); 843 IOHandlerSP reader_sp (m_input_reader_stack.Top()); 844 if (reader_sp) 845 reader_sp->Interrupt(); 846 } 847 848 void 849 Debugger::DispatchInputEndOfFile () 850 { 851 Mutex::Locker locker (m_input_reader_stack.GetMutex()); 852 IOHandlerSP reader_sp (m_input_reader_stack.Top()); 853 if (reader_sp) 854 reader_sp->GotEOF(); 855 } 856 857 void 858 Debugger::ClearIOHandlers () 859 { 860 // The bottom input reader should be the main debugger input reader. We do not want to close that one here. 861 Mutex::Locker locker (m_input_reader_stack.GetMutex()); 862 while (m_input_reader_stack.GetSize() > 1) 863 { 864 IOHandlerSP reader_sp (m_input_reader_stack.Top()); 865 if (reader_sp) 866 { 867 m_input_reader_stack.Pop(); 868 reader_sp->SetIsDone(true); 869 reader_sp->Cancel(); 870 } 871 } 872 } 873 874 void 875 Debugger::ExecuteIOHanders() 876 { 877 878 while (1) 879 { 880 IOHandlerSP reader_sp(m_input_reader_stack.Top()); 881 if (!reader_sp) 882 break; 883 884 reader_sp->Activate(); 885 reader_sp->Run(); 886 reader_sp->Deactivate(); 887 888 // Remove all input readers that are done from the top of the stack 889 while (1) 890 { 891 IOHandlerSP top_reader_sp = m_input_reader_stack.Top(); 892 if (top_reader_sp && top_reader_sp->GetIsDone()) 893 m_input_reader_stack.Pop(); 894 else 895 break; 896 } 897 } 898 ClearIOHandlers(); 899 } 900 901 bool 902 Debugger::IsTopIOHandler (const lldb::IOHandlerSP& reader_sp) 903 { 904 return m_input_reader_stack.IsTop (reader_sp); 905 } 906 907 908 ConstString 909 Debugger::GetTopIOHandlerControlSequence(char ch) 910 { 911 return m_input_reader_stack.GetTopIOHandlerControlSequence (ch); 912 } 913 914 void 915 Debugger::RunIOHandler (const IOHandlerSP& reader_sp) 916 { 917 PushIOHandler (reader_sp); 918 919 IOHandlerSP top_reader_sp = reader_sp; 920 while (top_reader_sp) 921 { 922 top_reader_sp->Activate(); 923 top_reader_sp->Run(); 924 top_reader_sp->Deactivate(); 925 926 if (top_reader_sp.get() == reader_sp.get()) 927 { 928 if (PopIOHandler (reader_sp)) 929 break; 930 } 931 932 while (1) 933 { 934 top_reader_sp = m_input_reader_stack.Top(); 935 if (top_reader_sp && top_reader_sp->GetIsDone()) 936 m_input_reader_stack.Pop(); 937 else 938 break; 939 } 940 } 941 } 942 943 void 944 Debugger::AdoptTopIOHandlerFilesIfInvalid (StreamFileSP &in, StreamFileSP &out, StreamFileSP &err) 945 { 946 // Before an IOHandler runs, it must have in/out/err streams. 947 // This function is called when one ore more of the streams 948 // are NULL. We use the top input reader's in/out/err streams, 949 // or fall back to the debugger file handles, or we fall back 950 // onto stdin/stdout/stderr as a last resort. 951 952 Mutex::Locker locker (m_input_reader_stack.GetMutex()); 953 IOHandlerSP top_reader_sp (m_input_reader_stack.Top()); 954 // If no STDIN has been set, then set it appropriately 955 if (!in) 956 { 957 if (top_reader_sp) 958 in = top_reader_sp->GetInputStreamFile(); 959 else 960 in = GetInputFile(); 961 962 // If there is nothing, use stdin 963 if (!in) 964 in = StreamFileSP(new StreamFile(stdin, false)); 965 } 966 // If no STDOUT has been set, then set it appropriately 967 if (!out) 968 { 969 if (top_reader_sp) 970 out = top_reader_sp->GetOutputStreamFile(); 971 else 972 out = GetOutputFile(); 973 974 // If there is nothing, use stdout 975 if (!out) 976 out = StreamFileSP(new StreamFile(stdout, false)); 977 } 978 // If no STDERR has been set, then set it appropriately 979 if (!err) 980 { 981 if (top_reader_sp) 982 err = top_reader_sp->GetErrorStreamFile(); 983 else 984 err = GetErrorFile(); 985 986 // If there is nothing, use stderr 987 if (!err) 988 err = StreamFileSP(new StreamFile(stdout, false)); 989 990 } 991 } 992 993 void 994 Debugger::PushIOHandler (const IOHandlerSP& reader_sp) 995 { 996 if (!reader_sp) 997 return; 998 999 // Got the current top input reader... 1000 IOHandlerSP top_reader_sp (m_input_reader_stack.Top()); 1001 1002 // Don't push the same IO handler twice... 1003 if (reader_sp.get() != top_reader_sp.get()) 1004 { 1005 // Push our new input reader 1006 m_input_reader_stack.Push (reader_sp); 1007 1008 // Interrupt the top input reader to it will exit its Run() function 1009 // and let this new input reader take over 1010 if (top_reader_sp) 1011 top_reader_sp->Deactivate(); 1012 } 1013 } 1014 1015 bool 1016 Debugger::PopIOHandler (const IOHandlerSP& pop_reader_sp) 1017 { 1018 bool result = false; 1019 1020 Mutex::Locker locker (m_input_reader_stack.GetMutex()); 1021 1022 // The reader on the stop of the stack is done, so let the next 1023 // read on the stack refresh its prompt and if there is one... 1024 if (!m_input_reader_stack.IsEmpty()) 1025 { 1026 IOHandlerSP reader_sp(m_input_reader_stack.Top()); 1027 1028 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get()) 1029 { 1030 reader_sp->Deactivate(); 1031 reader_sp->Cancel(); 1032 m_input_reader_stack.Pop (); 1033 1034 reader_sp = m_input_reader_stack.Top(); 1035 if (reader_sp) 1036 reader_sp->Activate(); 1037 1038 result = true; 1039 } 1040 } 1041 return result; 1042 } 1043 1044 bool 1045 Debugger::HideTopIOHandler() 1046 { 1047 Mutex::Locker locker; 1048 1049 if (locker.TryLock(m_input_reader_stack.GetMutex())) 1050 { 1051 IOHandlerSP reader_sp(m_input_reader_stack.Top()); 1052 if (reader_sp) 1053 reader_sp->Hide(); 1054 return true; 1055 } 1056 return false; 1057 } 1058 1059 void 1060 Debugger::RefreshTopIOHandler() 1061 { 1062 IOHandlerSP reader_sp(m_input_reader_stack.Top()); 1063 if (reader_sp) 1064 reader_sp->Refresh(); 1065 } 1066 1067 1068 StreamSP 1069 Debugger::GetAsyncOutputStream () 1070 { 1071 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(), 1072 CommandInterpreter::eBroadcastBitAsynchronousOutputData)); 1073 } 1074 1075 StreamSP 1076 Debugger::GetAsyncErrorStream () 1077 { 1078 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(), 1079 CommandInterpreter::eBroadcastBitAsynchronousErrorData)); 1080 } 1081 1082 size_t 1083 Debugger::GetNumDebuggers() 1084 { 1085 if (g_shared_debugger_refcount > 0) 1086 { 1087 Mutex::Locker locker (GetDebuggerListMutex ()); 1088 return GetDebuggerList().size(); 1089 } 1090 return 0; 1091 } 1092 1093 lldb::DebuggerSP 1094 Debugger::GetDebuggerAtIndex (size_t index) 1095 { 1096 DebuggerSP debugger_sp; 1097 1098 if (g_shared_debugger_refcount > 0) 1099 { 1100 Mutex::Locker locker (GetDebuggerListMutex ()); 1101 DebuggerList &debugger_list = GetDebuggerList(); 1102 1103 if (index < debugger_list.size()) 1104 debugger_sp = debugger_list[index]; 1105 } 1106 1107 return debugger_sp; 1108 } 1109 1110 DebuggerSP 1111 Debugger::FindDebuggerWithID (lldb::user_id_t id) 1112 { 1113 DebuggerSP debugger_sp; 1114 1115 if (g_shared_debugger_refcount > 0) 1116 { 1117 Mutex::Locker locker (GetDebuggerListMutex ()); 1118 DebuggerList &debugger_list = GetDebuggerList(); 1119 DebuggerList::iterator pos, end = debugger_list.end(); 1120 for (pos = debugger_list.begin(); pos != end; ++pos) 1121 { 1122 if ((*pos).get()->GetID() == id) 1123 { 1124 debugger_sp = *pos; 1125 break; 1126 } 1127 } 1128 } 1129 return debugger_sp; 1130 } 1131 1132 #if 0 1133 static void 1134 TestPromptFormats (StackFrame *frame) 1135 { 1136 if (frame == NULL) 1137 return; 1138 1139 StreamString s; 1140 const char *prompt_format = 1141 "{addr = '${addr}'\n}" 1142 "{addr-file-or-load = '${addr-file-or-load}'\n}" 1143 "{current-pc-arrow = '${current-pc-arrow}'\n}" 1144 "{process.id = '${process.id}'\n}" 1145 "{process.name = '${process.name}'\n}" 1146 "{process.file.basename = '${process.file.basename}'\n}" 1147 "{process.file.fullpath = '${process.file.fullpath}'\n}" 1148 "{thread.id = '${thread.id}'\n}" 1149 "{thread.index = '${thread.index}'\n}" 1150 "{thread.name = '${thread.name}'\n}" 1151 "{thread.queue = '${thread.queue}'\n}" 1152 "{thread.stop-reason = '${thread.stop-reason}'\n}" 1153 "{target.arch = '${target.arch}'\n}" 1154 "{module.file.basename = '${module.file.basename}'\n}" 1155 "{module.file.fullpath = '${module.file.fullpath}'\n}" 1156 "{file.basename = '${file.basename}'\n}" 1157 "{file.fullpath = '${file.fullpath}'\n}" 1158 "{frame.index = '${frame.index}'\n}" 1159 "{frame.pc = '${frame.pc}'\n}" 1160 "{frame.sp = '${frame.sp}'\n}" 1161 "{frame.fp = '${frame.fp}'\n}" 1162 "{frame.flags = '${frame.flags}'\n}" 1163 "{frame.reg.rdi = '${frame.reg.rdi}'\n}" 1164 "{frame.reg.rip = '${frame.reg.rip}'\n}" 1165 "{frame.reg.rsp = '${frame.reg.rsp}'\n}" 1166 "{frame.reg.rbp = '${frame.reg.rbp}'\n}" 1167 "{frame.reg.rflags = '${frame.reg.rflags}'\n}" 1168 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}" 1169 "{frame.reg.carp = '${frame.reg.carp}'\n}" 1170 "{function.id = '${function.id}'\n}" 1171 "{function.changed = '${function.changed}'\n}" 1172 "{function.initial-function = '${function.initial-function}'\n}" 1173 "{function.name = '${function.name}'\n}" 1174 "{function.name-without-args = '${function.name-without-args}'\n}" 1175 "{function.name-with-args = '${function.name-with-args}'\n}" 1176 "{function.addr-offset = '${function.addr-offset}'\n}" 1177 "{function.concrete-only-addr-offset-no-padding = '${function.concrete-only-addr-offset-no-padding}'\n}" 1178 "{function.line-offset = '${function.line-offset}'\n}" 1179 "{function.pc-offset = '${function.pc-offset}'\n}" 1180 "{line.file.basename = '${line.file.basename}'\n}" 1181 "{line.file.fullpath = '${line.file.fullpath}'\n}" 1182 "{line.number = '${line.number}'\n}" 1183 "{line.start-addr = '${line.start-addr}'\n}" 1184 "{line.end-addr = '${line.end-addr}'\n}" 1185 ; 1186 1187 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything)); 1188 ExecutionContext exe_ctx; 1189 frame->CalculateExecutionContext(exe_ctx); 1190 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s)) 1191 { 1192 printf("%s\n", s.GetData()); 1193 } 1194 else 1195 { 1196 printf ("what we got: %s\n", s.GetData()); 1197 } 1198 } 1199 #endif 1200 1201 static bool 1202 ScanFormatDescriptor (const char* var_name_begin, 1203 const char* var_name_end, 1204 const char** var_name_final, 1205 const char** percent_position, 1206 Format* custom_format, 1207 ValueObject::ValueObjectRepresentationStyle* val_obj_display) 1208 { 1209 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES)); 1210 *percent_position = ::strchr(var_name_begin,'%'); 1211 if (!*percent_position || *percent_position > var_name_end) 1212 { 1213 if (log) 1214 log->Printf("[ScanFormatDescriptor] no format descriptor in string, skipping"); 1215 *var_name_final = var_name_end; 1216 } 1217 else 1218 { 1219 *var_name_final = *percent_position; 1220 std::string format_name(*var_name_final+1, var_name_end-*var_name_final-1); 1221 if (log) 1222 log->Printf("[ScanFormatDescriptor] parsing %s as a format descriptor", format_name.c_str()); 1223 if ( !FormatManager::GetFormatFromCString(format_name.c_str(), 1224 true, 1225 *custom_format) ) 1226 { 1227 if (log) 1228 log->Printf("[ScanFormatDescriptor] %s is an unknown format", format_name.c_str()); 1229 1230 switch (format_name.front()) 1231 { 1232 case '@': // if this is an @ sign, print ObjC description 1233 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLanguageSpecific; 1234 break; 1235 case 'V': // if this is a V, print the value using the default format 1236 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue; 1237 break; 1238 case 'L': // if this is an L, print the location of the value 1239 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLocation; 1240 break; 1241 case 'S': // if this is an S, print the summary after all 1242 *val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary; 1243 break; 1244 case '#': // if this is a '#', print the number of children 1245 *val_obj_display = ValueObject::eValueObjectRepresentationStyleChildrenCount; 1246 break; 1247 case 'T': // if this is a 'T', print the type 1248 *val_obj_display = ValueObject::eValueObjectRepresentationStyleType; 1249 break; 1250 case 'N': // if this is a 'N', print the name 1251 *val_obj_display = ValueObject::eValueObjectRepresentationStyleName; 1252 break; 1253 case '>': // if this is a '>', print the name 1254 *val_obj_display = ValueObject::eValueObjectRepresentationStyleExpressionPath; 1255 break; 1256 default: 1257 if (log) 1258 log->Printf("ScanFormatDescriptor] %s is an error, leaving the previous value alone", format_name.c_str()); 1259 break; 1260 } 1261 } 1262 // a good custom format tells us to print the value using it 1263 else 1264 { 1265 if (log) 1266 log->Printf("[ScanFormatDescriptor] will display value for this VO"); 1267 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue; 1268 } 1269 } 1270 if (log) 1271 log->Printf("[ScanFormatDescriptor] final format description outcome: custom_format = %d, val_obj_display = %d", 1272 *custom_format, 1273 *val_obj_display); 1274 return true; 1275 } 1276 1277 static bool 1278 ScanBracketedRange (const char* var_name_begin, 1279 const char* var_name_end, 1280 const char* var_name_final, 1281 const char** open_bracket_position, 1282 const char** separator_position, 1283 const char** close_bracket_position, 1284 const char** var_name_final_if_array_range, 1285 int64_t* index_lower, 1286 int64_t* index_higher) 1287 { 1288 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES)); 1289 *open_bracket_position = ::strchr(var_name_begin,'['); 1290 if (*open_bracket_position && *open_bracket_position < var_name_final) 1291 { 1292 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield 1293 *close_bracket_position = ::strchr(*open_bracket_position,']'); 1294 // as usual, we assume that [] will come before % 1295 //printf("trying to expand a []\n"); 1296 *var_name_final_if_array_range = *open_bracket_position; 1297 if (*close_bracket_position - *open_bracket_position == 1) 1298 { 1299 if (log) 1300 log->Printf("[ScanBracketedRange] '[]' detected.. going from 0 to end of data"); 1301 *index_lower = 0; 1302 } 1303 else if (*separator_position == NULL || *separator_position > var_name_end) 1304 { 1305 char *end = NULL; 1306 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0); 1307 *index_higher = *index_lower; 1308 if (log) 1309 log->Printf("[ScanBracketedRange] [%" PRId64 "] detected, high index is same", *index_lower); 1310 } 1311 else if (*close_bracket_position && *close_bracket_position < var_name_end) 1312 { 1313 char *end = NULL; 1314 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0); 1315 *index_higher = ::strtoul (*separator_position+1, &end, 0); 1316 if (log) 1317 log->Printf("[ScanBracketedRange] [%" PRId64 "-%" PRId64 "] detected", *index_lower, *index_higher); 1318 } 1319 else 1320 { 1321 if (log) 1322 log->Printf("[ScanBracketedRange] expression is erroneous, cannot extract indices out of it"); 1323 return false; 1324 } 1325 if (*index_lower > *index_higher && *index_higher > 0) 1326 { 1327 if (log) 1328 log->Printf("[ScanBracketedRange] swapping indices"); 1329 int64_t temp = *index_lower; 1330 *index_lower = *index_higher; 1331 *index_higher = temp; 1332 } 1333 } 1334 else if (log) 1335 log->Printf("[ScanBracketedRange] no bracketed range, skipping entirely"); 1336 return true; 1337 } 1338 1339 template <typename T> 1340 static bool RunScriptFormatKeyword(Stream &s, ScriptInterpreter *script_interpreter, T t, const std::string& script_name) 1341 { 1342 if (script_interpreter) 1343 { 1344 Error script_error; 1345 std::string script_output; 1346 1347 if (script_interpreter->RunScriptFormatKeyword(script_name.c_str(), t, script_output, script_error) && script_error.Success()) 1348 { 1349 s.Printf("%s", script_output.c_str()); 1350 return true; 1351 } 1352 else 1353 { 1354 s.Printf("<error: %s>",script_error.AsCString()); 1355 } 1356 } 1357 return false; 1358 } 1359 1360 static ValueObjectSP 1361 ExpandIndexedExpression (ValueObject* valobj, 1362 size_t index, 1363 StackFrame* frame, 1364 bool deref_pointer) 1365 { 1366 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES)); 1367 const char* ptr_deref_format = "[%d]"; 1368 std::string ptr_deref_buffer(10,0); 1369 ::sprintf(&ptr_deref_buffer[0], ptr_deref_format, index); 1370 if (log) 1371 log->Printf("[ExpandIndexedExpression] name to deref: %s",ptr_deref_buffer.c_str()); 1372 const char* first_unparsed; 1373 ValueObject::GetValueForExpressionPathOptions options; 1374 ValueObject::ExpressionPathEndResultType final_value_type; 1375 ValueObject::ExpressionPathScanEndReason reason_to_stop; 1376 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing); 1377 ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.c_str(), 1378 &first_unparsed, 1379 &reason_to_stop, 1380 &final_value_type, 1381 options, 1382 &what_next); 1383 if (!item) 1384 { 1385 if (log) 1386 log->Printf("[ExpandIndexedExpression] ERROR: unparsed portion = %s, why stopping = %d," 1387 " final_value_type %d", 1388 first_unparsed, reason_to_stop, final_value_type); 1389 } 1390 else 1391 { 1392 if (log) 1393 log->Printf("[ExpandIndexedExpression] ALL RIGHT: unparsed portion = %s, why stopping = %d," 1394 " final_value_type %d", 1395 first_unparsed, reason_to_stop, final_value_type); 1396 } 1397 return item; 1398 } 1399 1400 static inline bool 1401 IsToken(const char *var_name_begin, const char *var) 1402 { 1403 return (::strncmp (var_name_begin, var, strlen(var)) == 0); 1404 } 1405 1406 static bool 1407 IsTokenWithFormat(const char *var_name_begin, const char *var, std::string &format, const char *default_format, 1408 const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr) 1409 { 1410 int var_len = strlen(var); 1411 if (::strncmp (var_name_begin, var, var_len) == 0) 1412 { 1413 var_name_begin += var_len; 1414 if (*var_name_begin == '}') 1415 { 1416 format = default_format; 1417 return true; 1418 } 1419 else if (*var_name_begin == '%') 1420 { 1421 // Allow format specifiers: x|X|u with optional width specifiers. 1422 // ${thread.id%x} ; hex 1423 // ${thread.id%X} ; uppercase hex 1424 // ${thread.id%u} ; unsigned decimal 1425 // ${thread.id%8.8X} ; width.precision + specifier 1426 // ${thread.id%tid} ; unsigned on FreeBSD/Linux, otherwise default_format (0x%4.4x for thread.id) 1427 int dot_count = 0; 1428 const char *specifier = NULL; 1429 int width_precision_length = 0; 1430 const char *width_precision = ++var_name_begin; 1431 while (isdigit(*var_name_begin) || *var_name_begin == '.') 1432 { 1433 dot_count += (*var_name_begin == '.'); 1434 if (dot_count > 1) 1435 break; 1436 var_name_begin++; 1437 width_precision_length++; 1438 } 1439 1440 if (IsToken (var_name_begin, "tid}")) 1441 { 1442 Target *target = Target::GetTargetFromContexts (exe_ctx_ptr, sc_ptr); 1443 if (target) 1444 { 1445 ArchSpec arch (target->GetArchitecture ()); 1446 llvm::Triple::OSType ostype = arch.IsValid() ? arch.GetTriple().getOS() : llvm::Triple::UnknownOS; 1447 if ((ostype == llvm::Triple::FreeBSD) || (ostype == llvm::Triple::Linux)) 1448 specifier = PRIu64; 1449 } 1450 if (!specifier) 1451 { 1452 format = default_format; 1453 return true; 1454 } 1455 } 1456 else if (IsToken (var_name_begin, "x}")) 1457 specifier = PRIx64; 1458 else if (IsToken (var_name_begin, "X}")) 1459 specifier = PRIX64; 1460 else if (IsToken (var_name_begin, "u}")) 1461 specifier = PRIu64; 1462 1463 if (specifier) 1464 { 1465 format = "%"; 1466 if (width_precision_length) 1467 format += std::string(width_precision, width_precision_length); 1468 format += specifier; 1469 return true; 1470 } 1471 } 1472 } 1473 return false; 1474 } 1475 1476 // Find information for the "thread.info.*" specifiers in a format string 1477 static bool 1478 FormatThreadExtendedInfoRecurse 1479 ( 1480 const char *var_name_begin, 1481 StructuredData::ObjectSP thread_info_dictionary, 1482 const SymbolContext *sc, 1483 const ExecutionContext *exe_ctx, 1484 Stream &s 1485 ) 1486 { 1487 bool var_success = false; 1488 std::string token_format; 1489 1490 llvm::StringRef var_name(var_name_begin); 1491 size_t percent_idx = var_name.find('%'); 1492 size_t close_curly_idx = var_name.find('}'); 1493 llvm::StringRef path = var_name; 1494 llvm::StringRef formatter = var_name; 1495 1496 // 'path' will be the dot separated list of objects to transverse up until we hit 1497 // a close curly brace, a percent sign, or an end of string. 1498 if (percent_idx != llvm::StringRef::npos || close_curly_idx != llvm::StringRef::npos) 1499 { 1500 if (percent_idx != llvm::StringRef::npos && close_curly_idx != llvm::StringRef::npos) 1501 { 1502 if (percent_idx < close_curly_idx) 1503 { 1504 path = var_name.slice(0, percent_idx); 1505 formatter = var_name.substr (percent_idx); 1506 } 1507 else 1508 { 1509 path = var_name.slice(0, close_curly_idx); 1510 formatter = var_name.substr (close_curly_idx); 1511 } 1512 } 1513 else if (percent_idx != llvm::StringRef::npos) 1514 { 1515 path = var_name.slice(0, percent_idx); 1516 formatter = var_name.substr (percent_idx); 1517 } 1518 else if (close_curly_idx != llvm::StringRef::npos) 1519 { 1520 path = var_name.slice(0, close_curly_idx); 1521 formatter = var_name.substr (close_curly_idx); 1522 } 1523 } 1524 1525 StructuredData::ObjectSP value = thread_info_dictionary->GetObjectForDotSeparatedPath (path); 1526 1527 if (value.get()) 1528 { 1529 if (value->GetType() == StructuredData::Type::eTypeInteger) 1530 { 1531 if (IsTokenWithFormat (formatter.str().c_str(), "", token_format, "0x%4.4" PRIx64, exe_ctx, sc)) 1532 { 1533 s.Printf(token_format.c_str(), value->GetAsInteger()->GetValue()); 1534 var_success = true; 1535 } 1536 } 1537 else if (value->GetType() == StructuredData::Type::eTypeFloat) 1538 { 1539 s.Printf ("%f", value->GetAsFloat()->GetValue()); 1540 var_success = true; 1541 } 1542 else if (value->GetType() == StructuredData::Type::eTypeString) 1543 { 1544 s.Printf("%s", value->GetAsString()->GetValue().c_str()); 1545 var_success = true; 1546 } 1547 else if (value->GetType() == StructuredData::Type::eTypeArray) 1548 { 1549 if (value->GetAsArray()->GetSize() > 0) 1550 { 1551 s.Printf ("%zu", value->GetAsArray()->GetSize()); 1552 var_success = true; 1553 } 1554 } 1555 else if (value->GetType() == StructuredData::Type::eTypeDictionary) 1556 { 1557 s.Printf ("%zu", value->GetAsDictionary()->GetKeys()->GetAsArray()->GetSize()); 1558 var_success = true; 1559 } 1560 } 1561 1562 return var_success; 1563 } 1564 1565 1566 static bool 1567 FormatPromptRecurse 1568 ( 1569 const char *format, 1570 const SymbolContext *sc, 1571 const ExecutionContext *exe_ctx, 1572 const Address *addr, 1573 Stream &s, 1574 const char **end, 1575 ValueObject* valobj, 1576 bool function_changed, 1577 bool initial_function 1578 ) 1579 { 1580 ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers 1581 bool success = true; 1582 const char *p; 1583 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES)); 1584 1585 for (p = format; *p != '\0'; ++p) 1586 { 1587 if (realvalobj) 1588 { 1589 valobj = realvalobj; 1590 realvalobj = NULL; 1591 } 1592 size_t non_special_chars = ::strcspn (p, "${}\\"); 1593 if (non_special_chars > 0) 1594 { 1595 if (success) 1596 s.Write (p, non_special_chars); 1597 p += non_special_chars; 1598 } 1599 1600 if (*p == '\0') 1601 { 1602 break; 1603 } 1604 else if (*p == '{') 1605 { 1606 // Start a new scope that must have everything it needs if it is to 1607 // to make it into the final output stream "s". If you want to make 1608 // a format that only prints out the function or symbol name if there 1609 // is one in the symbol context you can use: 1610 // "{function =${function.name}}" 1611 // The first '{' starts a new scope that end with the matching '}' at 1612 // the end of the string. The contents "function =${function.name}" 1613 // will then be evaluated and only be output if there is a function 1614 // or symbol with a valid name. 1615 StreamString sub_strm; 1616 1617 ++p; // Skip the '{' 1618 1619 if (FormatPromptRecurse (p, sc, exe_ctx, addr, sub_strm, &p, valobj, function_changed, initial_function)) 1620 { 1621 // The stream had all it needed 1622 s.Write(sub_strm.GetData(), sub_strm.GetSize()); 1623 } 1624 if (*p != '}') 1625 { 1626 success = false; 1627 break; 1628 } 1629 } 1630 else if (*p == '}') 1631 { 1632 // End of a enclosing scope 1633 break; 1634 } 1635 else if (*p == '$') 1636 { 1637 // We have a prompt variable to print 1638 ++p; 1639 if (*p == '{') 1640 { 1641 ++p; 1642 const char *var_name_begin = p; 1643 const char *var_name_end = ::strchr (p, '}'); 1644 1645 if (var_name_end && var_name_begin < var_name_end) 1646 { 1647 // if we have already failed to parse, skip this variable 1648 if (success) 1649 { 1650 const char *cstr = NULL; 1651 std::string token_format; 1652 Address format_addr; 1653 1654 // normally "addr" means print a raw address but 1655 // "file-addr-or-load-addr" means print a module + file addr if there's no load addr 1656 bool print_file_addr_or_load_addr = false; 1657 bool addr_offset_concrete_func_only = false; 1658 bool addr_offset_print_with_no_padding = false; 1659 bool calculate_format_addr_function_offset = false; 1660 // Set reg_kind and reg_num to invalid values 1661 RegisterKind reg_kind = kNumRegisterKinds; 1662 uint32_t reg_num = LLDB_INVALID_REGNUM; 1663 FileSpec format_file_spec; 1664 const RegisterInfo *reg_info = NULL; 1665 RegisterContext *reg_ctx = NULL; 1666 bool do_deref_pointer = false; 1667 ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString; 1668 ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain; 1669 1670 // Each variable must set success to true below... 1671 bool var_success = false; 1672 switch (var_name_begin[0]) 1673 { 1674 case '*': 1675 case 'v': 1676 case 's': 1677 { 1678 if (!valobj) 1679 break; 1680 1681 if (log) 1682 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin); 1683 1684 // check for *var and *svar 1685 if (*var_name_begin == '*') 1686 { 1687 do_deref_pointer = true; 1688 var_name_begin++; 1689 if (log) 1690 log->Printf("[Debugger::FormatPrompt] found a deref, new string is: %s",var_name_begin); 1691 } 1692 1693 if (*var_name_begin == 's') 1694 { 1695 if (!valobj->IsSynthetic()) 1696 valobj = valobj->GetSyntheticValue().get(); 1697 if (!valobj) 1698 break; 1699 var_name_begin++; 1700 if (log) 1701 log->Printf("[Debugger::FormatPrompt] found a synthetic, new string is: %s",var_name_begin); 1702 } 1703 1704 // should be a 'v' by now 1705 if (*var_name_begin != 'v') 1706 break; 1707 1708 if (log) 1709 log->Printf("[Debugger::FormatPrompt] string I am working with: %s",var_name_begin); 1710 1711 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ? 1712 ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing); 1713 ValueObject::GetValueForExpressionPathOptions options; 1714 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren(); 1715 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary; 1716 ValueObject* target = NULL; 1717 Format custom_format = eFormatInvalid; 1718 const char* var_name_final = NULL; 1719 const char* var_name_final_if_array_range = NULL; 1720 const char* close_bracket_position = NULL; 1721 int64_t index_lower = -1; 1722 int64_t index_higher = -1; 1723 bool is_array_range = false; 1724 const char* first_unparsed; 1725 bool was_plain_var = false; 1726 bool was_var_format = false; 1727 bool was_var_indexed = false; 1728 1729 if (!valobj) break; 1730 // simplest case ${var}, just print valobj's value 1731 if (IsToken (var_name_begin, "var}")) 1732 { 1733 was_plain_var = true; 1734 target = valobj; 1735 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue; 1736 } 1737 else if (IsToken (var_name_begin,"var%")) 1738 { 1739 was_var_format = true; 1740 // this is a variable with some custom format applied to it 1741 const char* percent_position; 1742 target = valobj; 1743 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue; 1744 ScanFormatDescriptor (var_name_begin, 1745 var_name_end, 1746 &var_name_final, 1747 &percent_position, 1748 &custom_format, 1749 &val_obj_display); 1750 } 1751 // this is ${var.something} or multiple .something nested 1752 else if (IsToken (var_name_begin, "var")) 1753 { 1754 if (IsToken (var_name_begin, "var[")) 1755 was_var_indexed = true; 1756 const char* percent_position; 1757 ScanFormatDescriptor (var_name_begin, 1758 var_name_end, 1759 &var_name_final, 1760 &percent_position, 1761 &custom_format, 1762 &val_obj_display); 1763 1764 const char* open_bracket_position; 1765 const char* separator_position; 1766 ScanBracketedRange (var_name_begin, 1767 var_name_end, 1768 var_name_final, 1769 &open_bracket_position, 1770 &separator_position, 1771 &close_bracket_position, 1772 &var_name_final_if_array_range, 1773 &index_lower, 1774 &index_higher); 1775 1776 Error error; 1777 1778 std::string expr_path(var_name_final-var_name_begin-1,0); 1779 memcpy(&expr_path[0], var_name_begin+3,var_name_final-var_name_begin-3); 1780 1781 if (log) 1782 log->Printf("[Debugger::FormatPrompt] symbol to expand: %s",expr_path.c_str()); 1783 1784 target = valobj->GetValueForExpressionPath(expr_path.c_str(), 1785 &first_unparsed, 1786 &reason_to_stop, 1787 &final_value_type, 1788 options, 1789 &what_next).get(); 1790 1791 if (!target) 1792 { 1793 if (log) 1794 log->Printf("[Debugger::FormatPrompt] ERROR: unparsed portion = %s, why stopping = %d," 1795 " final_value_type %d", 1796 first_unparsed, reason_to_stop, final_value_type); 1797 break; 1798 } 1799 else 1800 { 1801 if (log) 1802 log->Printf("[Debugger::FormatPrompt] ALL RIGHT: unparsed portion = %s, why stopping = %d," 1803 " final_value_type %d", 1804 first_unparsed, reason_to_stop, final_value_type); 1805 } 1806 } 1807 else 1808 break; 1809 1810 is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange || 1811 final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange); 1812 1813 do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference); 1814 1815 if (do_deref_pointer && !is_array_range) 1816 { 1817 // I have not deref-ed yet, let's do it 1818 // this happens when we are not going through GetValueForVariableExpressionPath 1819 // to get to the target ValueObject 1820 Error error; 1821 target = target->Dereference(error).get(); 1822 if (error.Fail()) 1823 { 1824 if (log) 1825 log->Printf("[Debugger::FormatPrompt] ERROR: %s\n", error.AsCString("unknown")); \ 1826 break; 1827 } 1828 do_deref_pointer = false; 1829 } 1830 1831 if (!target) 1832 { 1833 if (log) 1834 log->Printf("[Debugger::FormatPrompt] could not calculate target for prompt expression"); 1835 break; 1836 } 1837 1838 // we do not want to use the summary for a bitfield of type T:n 1839 // if we were originally dealing with just a T - that would get 1840 // us into an endless recursion 1841 if (target->IsBitfield() && was_var_indexed) 1842 { 1843 // TODO: check for a (T:n)-specific summary - we should still obey that 1844 StreamString bitfield_name; 1845 bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), target->GetBitfieldBitSize()); 1846 lldb::TypeNameSpecifierImplSP type_sp(new TypeNameSpecifierImpl(bitfield_name.GetData(),false)); 1847 if (!DataVisualization::GetSummaryForType(type_sp)) 1848 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue; 1849 } 1850 1851 // TODO use flags for these 1852 const uint32_t type_info_flags = target->GetClangType().GetTypeInfo(NULL); 1853 bool is_array = (type_info_flags & ClangASTType::eTypeIsArray) != 0; 1854 bool is_pointer = (type_info_flags & ClangASTType::eTypeIsPointer) != 0; 1855 bool is_aggregate = target->GetClangType().IsAggregateType(); 1856 1857 if ((is_array || is_pointer) && (!is_array_range) && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue) // this should be wrong, but there are some exceptions 1858 { 1859 StreamString str_temp; 1860 if (log) 1861 log->Printf("[Debugger::FormatPrompt] I am into array || pointer && !range"); 1862 1863 if (target->HasSpecialPrintableRepresentation(val_obj_display, custom_format)) 1864 { 1865 // try to use the special cases 1866 var_success = target->DumpPrintableRepresentation(str_temp, 1867 val_obj_display, 1868 custom_format); 1869 if (log) 1870 log->Printf("[Debugger::FormatPrompt] special cases did%s match", var_success ? "" : "n't"); 1871 1872 // should not happen 1873 if (var_success) 1874 s << str_temp.GetData(); 1875 var_success = true; 1876 break; 1877 } 1878 else 1879 { 1880 if (was_plain_var) // if ${var} 1881 { 1882 s << target->GetTypeName() << " @ " << target->GetLocationAsCString(); 1883 } 1884 else if (is_pointer) // if pointer, value is the address stored 1885 { 1886 target->DumpPrintableRepresentation (s, 1887 val_obj_display, 1888 custom_format, 1889 ValueObject::ePrintableRepresentationSpecialCasesDisable); 1890 } 1891 var_success = true; 1892 break; 1893 } 1894 } 1895 1896 // if directly trying to print ${var}, and this is an aggregate, display a nice 1897 // type @ location message 1898 if (is_aggregate && was_plain_var) 1899 { 1900 s << target->GetTypeName() << " @ " << target->GetLocationAsCString(); 1901 var_success = true; 1902 break; 1903 } 1904 1905 // if directly trying to print ${var%V}, and this is an aggregate, do not let the user do it 1906 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue))) 1907 { 1908 s << "<invalid use of aggregate type>"; 1909 var_success = true; 1910 break; 1911 } 1912 1913 if (!is_array_range) 1914 { 1915 if (log) 1916 log->Printf("[Debugger::FormatPrompt] dumping ordinary printable output"); 1917 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format); 1918 } 1919 else 1920 { 1921 if (log) 1922 log->Printf("[Debugger::FormatPrompt] checking if I can handle as array"); 1923 if (!is_array && !is_pointer) 1924 break; 1925 if (log) 1926 log->Printf("[Debugger::FormatPrompt] handle as array"); 1927 const char* special_directions = NULL; 1928 StreamString special_directions_writer; 1929 if (close_bracket_position && (var_name_end-close_bracket_position > 1)) 1930 { 1931 ConstString additional_data; 1932 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1); 1933 special_directions_writer.Printf("${%svar%s}", 1934 do_deref_pointer ? "*" : "", 1935 additional_data.GetCString()); 1936 special_directions = special_directions_writer.GetData(); 1937 } 1938 1939 // let us display items index_lower thru index_higher of this array 1940 s.PutChar('['); 1941 var_success = true; 1942 1943 if (index_higher < 0) 1944 index_higher = valobj->GetNumChildren() - 1; 1945 1946 uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay(); 1947 1948 for (;index_lower<=index_higher;index_lower++) 1949 { 1950 ValueObject* item = ExpandIndexedExpression (target, 1951 index_lower, 1952 exe_ctx->GetFramePtr(), 1953 false).get(); 1954 1955 if (!item) 1956 { 1957 if (log) 1958 log->Printf("[Debugger::FormatPrompt] ERROR in getting child item at index %" PRId64, index_lower); 1959 } 1960 else 1961 { 1962 if (log) 1963 log->Printf("[Debugger::FormatPrompt] special_directions for child item: %s",special_directions); 1964 } 1965 1966 if (!special_directions) 1967 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format); 1968 else 1969 var_success &= FormatPromptRecurse(special_directions, sc, exe_ctx, addr, s, NULL, item, function_changed, initial_function); 1970 1971 if (--max_num_children == 0) 1972 { 1973 s.PutCString(", ..."); 1974 break; 1975 } 1976 1977 if (index_lower < index_higher) 1978 s.PutChar(','); 1979 } 1980 s.PutChar(']'); 1981 } 1982 } 1983 break; 1984 case 'a': 1985 if (IsToken (var_name_begin, "addr-file-or-load}")) 1986 { 1987 print_file_addr_or_load_addr = true; 1988 } 1989 if (IsToken (var_name_begin, "addr}") 1990 || IsToken (var_name_begin, "addr-file-or-load}")) 1991 { 1992 if (addr && addr->IsValid()) 1993 { 1994 var_success = true; 1995 format_addr = *addr; 1996 } 1997 } 1998 break; 1999 2000 case 'p': 2001 if (IsToken (var_name_begin, "process.")) 2002 { 2003 if (exe_ctx) 2004 { 2005 Process *process = exe_ctx->GetProcessPtr(); 2006 if (process) 2007 { 2008 var_name_begin += ::strlen ("process."); 2009 if (IsTokenWithFormat (var_name_begin, "id", token_format, "%" PRIu64, exe_ctx, sc)) 2010 { 2011 s.Printf(token_format.c_str(), process->GetID()); 2012 var_success = true; 2013 } 2014 else if ((IsToken (var_name_begin, "name}")) || 2015 (IsToken (var_name_begin, "file.basename}")) || 2016 (IsToken (var_name_begin, "file.fullpath}"))) 2017 { 2018 Module *exe_module = process->GetTarget().GetExecutableModulePointer(); 2019 if (exe_module) 2020 { 2021 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f') 2022 { 2023 format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename(); 2024 var_success = (bool)format_file_spec; 2025 } 2026 else 2027 { 2028 format_file_spec = exe_module->GetFileSpec(); 2029 var_success = (bool)format_file_spec; 2030 } 2031 } 2032 } 2033 else if (IsToken (var_name_begin, "script:")) 2034 { 2035 var_name_begin += ::strlen("script:"); 2036 std::string script_name(var_name_begin,var_name_end); 2037 ScriptInterpreter* script_interpreter = process->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter(); 2038 if (RunScriptFormatKeyword (s, script_interpreter, process, script_name)) 2039 var_success = true; 2040 } 2041 } 2042 } 2043 } 2044 break; 2045 2046 case 't': 2047 if (IsToken (var_name_begin, "thread.")) 2048 { 2049 if (exe_ctx) 2050 { 2051 Thread *thread = exe_ctx->GetThreadPtr(); 2052 if (thread) 2053 { 2054 var_name_begin += ::strlen ("thread."); 2055 if (IsTokenWithFormat (var_name_begin, "id", token_format, "0x%4.4" PRIx64, exe_ctx, sc)) 2056 { 2057 s.Printf(token_format.c_str(), thread->GetID()); 2058 var_success = true; 2059 } 2060 else if (IsTokenWithFormat (var_name_begin, "protocol_id", token_format, "0x%4.4" PRIx64, exe_ctx, sc)) 2061 { 2062 s.Printf(token_format.c_str(), thread->GetProtocolID()); 2063 var_success = true; 2064 } 2065 else if (IsTokenWithFormat (var_name_begin, "index", token_format, "%" PRIu64, exe_ctx, sc)) 2066 { 2067 s.Printf(token_format.c_str(), (uint64_t)thread->GetIndexID()); 2068 var_success = true; 2069 } 2070 else if (IsToken (var_name_begin, "name}")) 2071 { 2072 cstr = thread->GetName(); 2073 var_success = cstr && cstr[0]; 2074 if (var_success) 2075 s.PutCString(cstr); 2076 } 2077 else if (IsToken (var_name_begin, "queue}")) 2078 { 2079 cstr = thread->GetQueueName(); 2080 var_success = cstr && cstr[0]; 2081 if (var_success) 2082 s.PutCString(cstr); 2083 } 2084 else if (IsToken (var_name_begin, "stop-reason}")) 2085 { 2086 StopInfoSP stop_info_sp = thread->GetStopInfo (); 2087 if (stop_info_sp && stop_info_sp->IsValid()) 2088 { 2089 cstr = stop_info_sp->GetDescription(); 2090 if (cstr && cstr[0]) 2091 { 2092 s.PutCString(cstr); 2093 var_success = true; 2094 } 2095 } 2096 } 2097 else if (IsToken (var_name_begin, "return-value}")) 2098 { 2099 StopInfoSP stop_info_sp = thread->GetStopInfo (); 2100 if (stop_info_sp && stop_info_sp->IsValid()) 2101 { 2102 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp); 2103 if (return_valobj_sp) 2104 { 2105 return_valobj_sp->Dump(s); 2106 var_success = true; 2107 } 2108 } 2109 } 2110 else if (IsToken (var_name_begin, "completed-expression}")) 2111 { 2112 StopInfoSP stop_info_sp = thread->GetStopInfo (); 2113 if (stop_info_sp && stop_info_sp->IsValid()) 2114 { 2115 ClangExpressionVariableSP expression_var_sp = StopInfo::GetExpressionVariable (stop_info_sp); 2116 if (expression_var_sp && expression_var_sp->GetValueObject()) 2117 { 2118 expression_var_sp->GetValueObject()->Dump(s); 2119 var_success = true; 2120 } 2121 } 2122 } 2123 else if (IsToken (var_name_begin, "script:")) 2124 { 2125 var_name_begin += ::strlen("script:"); 2126 std::string script_name(var_name_begin,var_name_end); 2127 ScriptInterpreter* script_interpreter = thread->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter(); 2128 if (RunScriptFormatKeyword (s, script_interpreter, thread, script_name)) 2129 var_success = true; 2130 } 2131 else if (IsToken (var_name_begin, "info.")) 2132 { 2133 var_name_begin += ::strlen("info."); 2134 StructuredData::ObjectSP object_sp = thread->GetExtendedInfo(); 2135 if (object_sp && object_sp->GetType() == StructuredData::Type::eTypeDictionary) 2136 { 2137 var_success = FormatThreadExtendedInfoRecurse (var_name_begin, object_sp, sc, exe_ctx, s); 2138 } 2139 } 2140 } 2141 } 2142 } 2143 else if (IsToken (var_name_begin, "target.")) 2144 { 2145 // TODO: hookup properties 2146 // if (!target_properties_sp) 2147 // { 2148 // Target *target = Target::GetTargetFromContexts (exe_ctx, sc); 2149 // if (target) 2150 // target_properties_sp = target->GetProperties(); 2151 // } 2152 // 2153 // if (target_properties_sp) 2154 // { 2155 // var_name_begin += ::strlen ("target."); 2156 // const char *end_property = strchr(var_name_begin, '}'); 2157 // if (end_property) 2158 // { 2159 // ConstString property_name(var_name_begin, end_property - var_name_begin); 2160 // std::string property_value (target_properties_sp->GetPropertyValue(property_name)); 2161 // if (!property_value.empty()) 2162 // { 2163 // s.PutCString (property_value.c_str()); 2164 // var_success = true; 2165 // } 2166 // } 2167 // } 2168 Target *target = Target::GetTargetFromContexts (exe_ctx, sc); 2169 if (target) 2170 { 2171 var_name_begin += ::strlen ("target."); 2172 if (IsToken (var_name_begin, "arch}")) 2173 { 2174 ArchSpec arch (target->GetArchitecture ()); 2175 if (arch.IsValid()) 2176 { 2177 s.PutCString (arch.GetArchitectureName()); 2178 var_success = true; 2179 } 2180 } 2181 else if (IsToken (var_name_begin, "script:")) 2182 { 2183 var_name_begin += ::strlen("script:"); 2184 std::string script_name(var_name_begin,var_name_end); 2185 ScriptInterpreter* script_interpreter = target->GetDebugger().GetCommandInterpreter().GetScriptInterpreter(); 2186 if (RunScriptFormatKeyword (s, script_interpreter, target, script_name)) 2187 var_success = true; 2188 } 2189 } 2190 } 2191 break; 2192 2193 case 'm': 2194 if (IsToken (var_name_begin, "module.")) 2195 { 2196 if (sc && sc->module_sp.get()) 2197 { 2198 Module *module = sc->module_sp.get(); 2199 var_name_begin += ::strlen ("module."); 2200 2201 if (IsToken (var_name_begin, "file.")) 2202 { 2203 if (module->GetFileSpec()) 2204 { 2205 var_name_begin += ::strlen ("file."); 2206 2207 if (IsToken (var_name_begin, "basename}")) 2208 { 2209 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename(); 2210 var_success = (bool)format_file_spec; 2211 } 2212 else if (IsToken (var_name_begin, "fullpath}")) 2213 { 2214 format_file_spec = module->GetFileSpec(); 2215 var_success = (bool)format_file_spec; 2216 } 2217 } 2218 } 2219 } 2220 } 2221 break; 2222 2223 2224 case 'f': 2225 if (IsToken (var_name_begin, "file.")) 2226 { 2227 if (sc && sc->comp_unit != NULL) 2228 { 2229 var_name_begin += ::strlen ("file."); 2230 2231 if (IsToken (var_name_begin, "basename}")) 2232 { 2233 format_file_spec.GetFilename() = sc->comp_unit->GetFilename(); 2234 var_success = (bool)format_file_spec; 2235 } 2236 else if (IsToken (var_name_begin, "fullpath}")) 2237 { 2238 format_file_spec = *sc->comp_unit; 2239 var_success = (bool)format_file_spec; 2240 } 2241 } 2242 } 2243 else if (IsToken (var_name_begin, "frame.")) 2244 { 2245 if (exe_ctx) 2246 { 2247 StackFrame *frame = exe_ctx->GetFramePtr(); 2248 if (frame) 2249 { 2250 var_name_begin += ::strlen ("frame."); 2251 if (IsToken (var_name_begin, "index}")) 2252 { 2253 s.Printf("%u", frame->GetFrameIndex()); 2254 var_success = true; 2255 } 2256 else if (IsToken (var_name_begin, "pc}")) 2257 { 2258 reg_kind = eRegisterKindGeneric; 2259 reg_num = LLDB_REGNUM_GENERIC_PC; 2260 var_success = true; 2261 } 2262 else if (IsToken (var_name_begin, "sp}")) 2263 { 2264 reg_kind = eRegisterKindGeneric; 2265 reg_num = LLDB_REGNUM_GENERIC_SP; 2266 var_success = true; 2267 } 2268 else if (IsToken (var_name_begin, "fp}")) 2269 { 2270 reg_kind = eRegisterKindGeneric; 2271 reg_num = LLDB_REGNUM_GENERIC_FP; 2272 var_success = true; 2273 } 2274 else if (IsToken (var_name_begin, "flags}")) 2275 { 2276 reg_kind = eRegisterKindGeneric; 2277 reg_num = LLDB_REGNUM_GENERIC_FLAGS; 2278 var_success = true; 2279 } 2280 else if (IsToken (var_name_begin, "reg.")) 2281 { 2282 reg_ctx = frame->GetRegisterContext().get(); 2283 if (reg_ctx) 2284 { 2285 var_name_begin += ::strlen ("reg."); 2286 if (var_name_begin < var_name_end) 2287 { 2288 std::string reg_name (var_name_begin, var_name_end); 2289 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str()); 2290 if (reg_info) 2291 var_success = true; 2292 } 2293 } 2294 } 2295 else if (IsToken (var_name_begin, "script:")) 2296 { 2297 var_name_begin += ::strlen("script:"); 2298 std::string script_name(var_name_begin,var_name_end); 2299 ScriptInterpreter* script_interpreter = frame->GetThread()->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter(); 2300 if (RunScriptFormatKeyword (s, script_interpreter, frame, script_name)) 2301 var_success = true; 2302 } 2303 } 2304 } 2305 } 2306 else if (IsToken (var_name_begin, "function.")) 2307 { 2308 if (sc && (sc->function != NULL || sc->symbol != NULL)) 2309 { 2310 var_name_begin += ::strlen ("function."); 2311 if (IsToken (var_name_begin, "id}")) 2312 { 2313 if (sc->function) 2314 s.Printf("function{0x%8.8" PRIx64 "}", sc->function->GetID()); 2315 else 2316 s.Printf("symbol[%u]", sc->symbol->GetID()); 2317 2318 var_success = true; 2319 } 2320 if (IsToken (var_name_begin, "changed}") && function_changed) 2321 { 2322 var_success = true; 2323 } 2324 if (IsToken (var_name_begin, "initial-function}") && initial_function) 2325 { 2326 var_success = true; 2327 } 2328 else if (IsToken (var_name_begin, "name}")) 2329 { 2330 if (sc->function) 2331 cstr = sc->function->GetName().AsCString (NULL); 2332 else if (sc->symbol) 2333 cstr = sc->symbol->GetName().AsCString (NULL); 2334 if (cstr) 2335 { 2336 s.PutCString(cstr); 2337 2338 if (sc->block) 2339 { 2340 Block *inline_block = sc->block->GetContainingInlinedBlock (); 2341 if (inline_block) 2342 { 2343 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo(); 2344 if (inline_info) 2345 { 2346 s.PutCString(" [inlined] "); 2347 inline_info->GetName().Dump(&s); 2348 } 2349 } 2350 } 2351 var_success = true; 2352 } 2353 } 2354 else if (IsToken (var_name_begin, "name-without-args}")) 2355 { 2356 ConstString name; 2357 if (sc->function) 2358 name = sc->function->GetMangled().GetName (Mangled::ePreferDemangledWithoutArguments); 2359 else if (sc->symbol) 2360 name = sc->symbol->GetMangled().GetName (Mangled::ePreferDemangledWithoutArguments); 2361 if (name) 2362 { 2363 s.PutCString(name.GetCString()); 2364 var_success = true; 2365 } 2366 } 2367 else if (IsToken (var_name_begin, "name-with-args}")) 2368 { 2369 // Print the function name with arguments in it 2370 2371 if (sc->function) 2372 { 2373 var_success = true; 2374 ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL; 2375 cstr = sc->function->GetName().AsCString (NULL); 2376 if (cstr) 2377 { 2378 const InlineFunctionInfo *inline_info = NULL; 2379 VariableListSP variable_list_sp; 2380 bool get_function_vars = true; 2381 if (sc->block) 2382 { 2383 Block *inline_block = sc->block->GetContainingInlinedBlock (); 2384 2385 if (inline_block) 2386 { 2387 get_function_vars = false; 2388 inline_info = sc->block->GetInlinedFunctionInfo(); 2389 if (inline_info) 2390 variable_list_sp = inline_block->GetBlockVariableList (true); 2391 } 2392 } 2393 2394 if (get_function_vars) 2395 { 2396 variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true); 2397 } 2398 2399 if (inline_info) 2400 { 2401 s.PutCString (cstr); 2402 s.PutCString (" [inlined] "); 2403 cstr = inline_info->GetName().GetCString(); 2404 } 2405 2406 VariableList args; 2407 if (variable_list_sp) 2408 variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument, args); 2409 if (args.GetSize() > 0) 2410 { 2411 const char *open_paren = strchr (cstr, '('); 2412 const char *close_paren = nullptr; 2413 const char *generic = strchr(cstr, '<'); 2414 // if before the arguments list begins there is a template sign 2415 // then scan to the end of the generic args before you try to find 2416 // the arguments list 2417 if (generic && open_paren && generic < open_paren) 2418 { 2419 int generic_depth = 1; 2420 ++generic; 2421 for (; 2422 *generic && generic_depth > 0; 2423 generic++) 2424 { 2425 if (*generic == '<') 2426 generic_depth++; 2427 if (*generic == '>') 2428 generic_depth--; 2429 } 2430 if (*generic) 2431 open_paren = strchr(generic, '('); 2432 else 2433 open_paren = nullptr; 2434 } 2435 if (open_paren) 2436 { 2437 if (IsToken (open_paren, "(anonymous namespace)")) 2438 { 2439 open_paren = strchr (open_paren + strlen("(anonymous namespace)"), '('); 2440 if (open_paren) 2441 close_paren = strchr (open_paren, ')'); 2442 } 2443 else 2444 close_paren = strchr (open_paren, ')'); 2445 } 2446 2447 if (open_paren) 2448 s.Write(cstr, open_paren - cstr + 1); 2449 else 2450 { 2451 s.PutCString (cstr); 2452 s.PutChar ('('); 2453 } 2454 const size_t num_args = args.GetSize(); 2455 for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx) 2456 { 2457 std::string buffer; 2458 2459 VariableSP var_sp (args.GetVariableAtIndex (arg_idx)); 2460 ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp)); 2461 const char *var_representation = nullptr; 2462 const char *var_name = var_value_sp->GetName().GetCString(); 2463 if (var_value_sp->GetClangType().IsAggregateType() && 2464 DataVisualization::ShouldPrintAsOneLiner(*var_value_sp.get())) 2465 { 2466 static StringSummaryFormat format(TypeSummaryImpl::Flags() 2467 .SetHideItemNames(false) 2468 .SetShowMembersOneLiner(true), 2469 ""); 2470 format.FormatObject(var_value_sp.get(), buffer); 2471 var_representation = buffer.c_str(); 2472 } 2473 else 2474 var_representation = var_value_sp->GetValueAsCString(); 2475 if (arg_idx > 0) 2476 s.PutCString (", "); 2477 if (var_value_sp->GetError().Success()) 2478 { 2479 if (var_representation) 2480 s.Printf ("%s=%s", var_name, var_representation); 2481 else 2482 s.Printf ("%s=%s at %s", var_name, var_value_sp->GetTypeName().GetCString(), var_value_sp->GetLocationAsCString()); 2483 } 2484 else 2485 s.Printf ("%s=<unavailable>", var_name); 2486 } 2487 2488 if (close_paren) 2489 s.PutCString (close_paren); 2490 else 2491 s.PutChar(')'); 2492 2493 } 2494 else 2495 { 2496 s.PutCString(cstr); 2497 } 2498 } 2499 } 2500 else if (sc->symbol) 2501 { 2502 cstr = sc->symbol->GetName().AsCString (NULL); 2503 if (cstr) 2504 { 2505 s.PutCString(cstr); 2506 var_success = true; 2507 } 2508 } 2509 } 2510 else if (IsToken (var_name_begin, "addr-offset}") 2511 || IsToken (var_name_begin, "concrete-only-addr-offset-no-padding}")) 2512 { 2513 if (IsToken (var_name_begin, "concrete-only-addr-offset-no-padding}")) 2514 { 2515 addr_offset_print_with_no_padding = true; 2516 addr_offset_concrete_func_only = true; 2517 } 2518 var_success = addr != NULL; 2519 if (var_success) 2520 { 2521 format_addr = *addr; 2522 calculate_format_addr_function_offset = true; 2523 } 2524 } 2525 else if (IsToken (var_name_begin, "line-offset}")) 2526 { 2527 var_success = sc->line_entry.range.GetBaseAddress().IsValid(); 2528 if (var_success) 2529 { 2530 format_addr = sc->line_entry.range.GetBaseAddress(); 2531 calculate_format_addr_function_offset = true; 2532 } 2533 } 2534 else if (IsToken (var_name_begin, "pc-offset}")) 2535 { 2536 StackFrame *frame = exe_ctx->GetFramePtr(); 2537 var_success = frame != NULL; 2538 if (var_success) 2539 { 2540 format_addr = frame->GetFrameCodeAddress(); 2541 calculate_format_addr_function_offset = true; 2542 } 2543 } 2544 } 2545 } 2546 break; 2547 2548 case 'l': 2549 if (IsToken (var_name_begin, "line.")) 2550 { 2551 if (sc && sc->line_entry.IsValid()) 2552 { 2553 var_name_begin += ::strlen ("line."); 2554 if (IsToken (var_name_begin, "file.")) 2555 { 2556 var_name_begin += ::strlen ("file."); 2557 2558 if (IsToken (var_name_begin, "basename}")) 2559 { 2560 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename(); 2561 var_success = (bool)format_file_spec; 2562 } 2563 else if (IsToken (var_name_begin, "fullpath}")) 2564 { 2565 format_file_spec = sc->line_entry.file; 2566 var_success = (bool)format_file_spec; 2567 } 2568 } 2569 else if (IsTokenWithFormat (var_name_begin, "number", token_format, "%" PRIu64, exe_ctx, sc)) 2570 { 2571 var_success = true; 2572 s.Printf(token_format.c_str(), (uint64_t)sc->line_entry.line); 2573 } 2574 else if ((IsToken (var_name_begin, "start-addr}")) || 2575 (IsToken (var_name_begin, "end-addr}"))) 2576 { 2577 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid(); 2578 if (var_success) 2579 { 2580 format_addr = sc->line_entry.range.GetBaseAddress(); 2581 if (var_name_begin[0] == 'e') 2582 format_addr.Slide (sc->line_entry.range.GetByteSize()); 2583 } 2584 } 2585 } 2586 } 2587 break; 2588 case 'c': 2589 if (IsToken (var_name_begin, "current-pc-arrow")) 2590 { 2591 if (addr && exe_ctx && exe_ctx->GetFramePtr()) 2592 { 2593 RegisterContextSP reg_ctx = exe_ctx->GetFramePtr()->GetRegisterContextSP(); 2594 if (reg_ctx.get()) 2595 { 2596 addr_t pc_loadaddr = reg_ctx->GetPC(); 2597 if (pc_loadaddr != LLDB_INVALID_ADDRESS) 2598 { 2599 Address pc; 2600 pc.SetLoadAddress (pc_loadaddr, exe_ctx->GetTargetPtr()); 2601 if (pc == *addr) 2602 { 2603 s.Printf ("->"); 2604 var_success = true; 2605 } 2606 else 2607 { 2608 s.Printf(" "); 2609 var_success = true; 2610 } 2611 } 2612 } 2613 } 2614 } 2615 break; 2616 } 2617 2618 if (var_success) 2619 { 2620 // If format addr is valid, then we need to print an address 2621 if (reg_num != LLDB_INVALID_REGNUM) 2622 { 2623 StackFrame *frame = exe_ctx->GetFramePtr(); 2624 // We have a register value to display... 2625 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric) 2626 { 2627 format_addr = frame->GetFrameCodeAddress(); 2628 } 2629 else 2630 { 2631 if (reg_ctx == NULL) 2632 reg_ctx = frame->GetRegisterContext().get(); 2633 2634 if (reg_ctx) 2635 { 2636 if (reg_kind != kNumRegisterKinds) 2637 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num); 2638 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num); 2639 var_success = reg_info != NULL; 2640 } 2641 } 2642 } 2643 2644 if (reg_info != NULL) 2645 { 2646 RegisterValue reg_value; 2647 var_success = reg_ctx->ReadRegister (reg_info, reg_value); 2648 if (var_success) 2649 { 2650 reg_value.Dump(&s, reg_info, false, false, eFormatDefault); 2651 } 2652 } 2653 2654 if (format_file_spec) 2655 { 2656 s << format_file_spec; 2657 } 2658 2659 // If format addr is valid, then we need to print an address 2660 if (format_addr.IsValid()) 2661 { 2662 var_success = false; 2663 2664 if (calculate_format_addr_function_offset) 2665 { 2666 Address func_addr; 2667 2668 if (sc) 2669 { 2670 if (sc->function) 2671 { 2672 func_addr = sc->function->GetAddressRange().GetBaseAddress(); 2673 if (sc->block && addr_offset_concrete_func_only == false) 2674 { 2675 // Check to make sure we aren't in an inline 2676 // function. If we are, use the inline block 2677 // range that contains "format_addr" since 2678 // blocks can be discontiguous. 2679 Block *inline_block = sc->block->GetContainingInlinedBlock (); 2680 AddressRange inline_range; 2681 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range)) 2682 func_addr = inline_range.GetBaseAddress(); 2683 } 2684 } 2685 else if (sc->symbol && sc->symbol->ValueIsAddress()) 2686 func_addr = sc->symbol->GetAddress(); 2687 } 2688 2689 if (func_addr.IsValid()) 2690 { 2691 const char *addr_offset_padding = " "; 2692 if (addr_offset_print_with_no_padding) 2693 { 2694 addr_offset_padding = ""; 2695 } 2696 if (func_addr.GetSection() == format_addr.GetSection()) 2697 { 2698 addr_t func_file_addr = func_addr.GetFileAddress(); 2699 addr_t addr_file_addr = format_addr.GetFileAddress(); 2700 if (addr_file_addr > func_file_addr) 2701 s.Printf("%s+%s%" PRIu64, addr_offset_padding, addr_offset_padding, addr_file_addr - func_file_addr); 2702 else if (addr_file_addr < func_file_addr) 2703 s.Printf("%s-%s%" PRIu64, addr_offset_padding, addr_offset_padding, func_file_addr - addr_file_addr); 2704 var_success = true; 2705 } 2706 else 2707 { 2708 Target *target = Target::GetTargetFromContexts (exe_ctx, sc); 2709 if (target) 2710 { 2711 addr_t func_load_addr = func_addr.GetLoadAddress (target); 2712 addr_t addr_load_addr = format_addr.GetLoadAddress (target); 2713 if (addr_load_addr > func_load_addr) 2714 s.Printf("%s+%s%" PRIu64, addr_offset_padding, addr_offset_padding, addr_load_addr - func_load_addr); 2715 else if (addr_load_addr < func_load_addr) 2716 s.Printf("%s-%s%" PRIu64, addr_offset_padding, addr_offset_padding, func_load_addr - addr_load_addr); 2717 var_success = true; 2718 } 2719 } 2720 } 2721 } 2722 else 2723 { 2724 Target *target = Target::GetTargetFromContexts (exe_ctx, sc); 2725 addr_t vaddr = LLDB_INVALID_ADDRESS; 2726 if (exe_ctx && !target->GetSectionLoadList().IsEmpty()) 2727 vaddr = format_addr.GetLoadAddress (target); 2728 if (vaddr == LLDB_INVALID_ADDRESS) 2729 vaddr = format_addr.GetFileAddress (); 2730 2731 if (vaddr != LLDB_INVALID_ADDRESS) 2732 { 2733 int addr_width = 0; 2734 if (exe_ctx && target) 2735 { 2736 addr_width = target->GetArchitecture().GetAddressByteSize() * 2; 2737 } 2738 if (addr_width == 0) 2739 addr_width = 16; 2740 if (print_file_addr_or_load_addr) 2741 { 2742 format_addr.Dump (&s, exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL, Address::DumpStyleLoadAddress, Address::DumpStyleModuleWithFileAddress, 0); 2743 } 2744 else 2745 { 2746 s.Printf("0x%*.*" PRIx64, addr_width, addr_width, vaddr); 2747 } 2748 var_success = true; 2749 } 2750 } 2751 } 2752 } 2753 2754 if (var_success == false) 2755 success = false; 2756 } 2757 p = var_name_end; 2758 } 2759 else 2760 break; 2761 } 2762 else 2763 { 2764 // We got a dollar sign with no '{' after it, it must just be a dollar sign 2765 s.PutChar(*p); 2766 } 2767 } 2768 else if (*p == '\\') 2769 { 2770 ++p; // skip the slash 2771 switch (*p) 2772 { 2773 case 'a': s.PutChar ('\a'); break; 2774 case 'b': s.PutChar ('\b'); break; 2775 case 'f': s.PutChar ('\f'); break; 2776 case 'n': s.PutChar ('\n'); break; 2777 case 'r': s.PutChar ('\r'); break; 2778 case 't': s.PutChar ('\t'); break; 2779 case 'v': s.PutChar ('\v'); break; 2780 case '\'': s.PutChar ('\''); break; 2781 case '\\': s.PutChar ('\\'); break; 2782 case '0': 2783 // 1 to 3 octal chars 2784 { 2785 // Make a string that can hold onto the initial zero char, 2786 // up to 3 octal digits, and a terminating NULL. 2787 char oct_str[5] = { 0, 0, 0, 0, 0 }; 2788 2789 int i; 2790 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i) 2791 oct_str[i] = p[i]; 2792 2793 // We don't want to consume the last octal character since 2794 // the main for loop will do this for us, so we advance p by 2795 // one less than i (even if i is zero) 2796 p += i - 1; 2797 unsigned long octal_value = ::strtoul (oct_str, NULL, 8); 2798 if (octal_value <= UINT8_MAX) 2799 { 2800 s.PutChar((char)octal_value); 2801 } 2802 } 2803 break; 2804 2805 case 'x': 2806 // hex number in the format 2807 if (isxdigit(p[1])) 2808 { 2809 ++p; // Skip the 'x' 2810 2811 // Make a string that can hold onto two hex chars plus a 2812 // NULL terminator 2813 char hex_str[3] = { 0,0,0 }; 2814 hex_str[0] = *p; 2815 if (isxdigit(p[1])) 2816 { 2817 ++p; // Skip the first of the two hex chars 2818 hex_str[1] = *p; 2819 } 2820 2821 unsigned long hex_value = strtoul (hex_str, NULL, 16); 2822 if (hex_value <= UINT8_MAX) 2823 s.PutChar ((char)hex_value); 2824 } 2825 else 2826 { 2827 s.PutChar('x'); 2828 } 2829 break; 2830 2831 default: 2832 // Just desensitize any other character by just printing what 2833 // came after the '\' 2834 s << *p; 2835 break; 2836 2837 } 2838 2839 } 2840 } 2841 if (end) 2842 *end = p; 2843 return success; 2844 } 2845 2846 bool 2847 Debugger::FormatPrompt 2848 ( 2849 const char *format, 2850 const SymbolContext *sc, 2851 const ExecutionContext *exe_ctx, 2852 const Address *addr, 2853 Stream &s, 2854 ValueObject* valobj 2855 ) 2856 { 2857 bool use_color = exe_ctx ? exe_ctx->GetTargetRef().GetDebugger().GetUseColor() : true; 2858 std::string format_str = lldb_utility::ansi::FormatAnsiTerminalCodes (format, use_color); 2859 if (format_str.length()) 2860 format = format_str.c_str(); 2861 return FormatPromptRecurse (format, sc, exe_ctx, addr, s, NULL, valobj, false, false); 2862 } 2863 2864 bool 2865 Debugger::FormatDisassemblerAddress (const char *format, 2866 const SymbolContext *sc, 2867 const SymbolContext *prev_sc, 2868 const ExecutionContext *exe_ctx, 2869 const Address *addr, 2870 Stream &s) 2871 { 2872 if (format == NULL && exe_ctx != NULL && exe_ctx->HasTargetScope()) 2873 { 2874 format = exe_ctx->GetTargetRef().GetDebugger().GetDisassemblyFormat(); 2875 } 2876 bool function_changed = false; 2877 bool initial_function = false; 2878 if (prev_sc && (prev_sc->function || prev_sc->symbol)) 2879 { 2880 if (sc && (sc->function || sc->symbol)) 2881 { 2882 if (prev_sc->symbol && sc->symbol) 2883 { 2884 if (!sc->symbol->Compare (prev_sc->symbol->GetName(), prev_sc->symbol->GetType())) 2885 { 2886 function_changed = true; 2887 } 2888 } 2889 else if (prev_sc->function && sc->function) 2890 { 2891 if (prev_sc->function->GetMangled() != sc->function->GetMangled()) 2892 { 2893 function_changed = true; 2894 } 2895 } 2896 } 2897 } 2898 // The first context on a list of instructions will have a prev_sc that 2899 // has no Function or Symbol -- if SymbolContext had an IsValid() method, it 2900 // would return false. But we do get a prev_sc pointer. 2901 if ((sc && (sc->function || sc->symbol)) 2902 && prev_sc && (prev_sc->function == NULL && prev_sc->symbol == NULL)) 2903 { 2904 initial_function = true; 2905 } 2906 return FormatPromptRecurse (format, sc, exe_ctx, addr, s, NULL, NULL, function_changed, initial_function); 2907 } 2908 2909 2910 void 2911 Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton) 2912 { 2913 // For simplicity's sake, I am not going to deal with how to close down any 2914 // open logging streams, I just redirect everything from here on out to the 2915 // callback. 2916 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton)); 2917 } 2918 2919 bool 2920 Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream) 2921 { 2922 Log::Callbacks log_callbacks; 2923 2924 StreamSP log_stream_sp; 2925 if (m_log_callback_stream_sp) 2926 { 2927 log_stream_sp = m_log_callback_stream_sp; 2928 // For now when using the callback mode you always get thread & timestamp. 2929 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME; 2930 } 2931 else if (log_file == NULL || *log_file == '\0') 2932 { 2933 log_stream_sp = GetOutputFile(); 2934 } 2935 else 2936 { 2937 LogStreamMap::iterator pos = m_log_streams.find(log_file); 2938 if (pos != m_log_streams.end()) 2939 log_stream_sp = pos->second.lock(); 2940 if (!log_stream_sp) 2941 { 2942 log_stream_sp.reset (new StreamFile (log_file)); 2943 m_log_streams[log_file] = log_stream_sp; 2944 } 2945 } 2946 assert (log_stream_sp.get()); 2947 2948 if (log_options == 0) 2949 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE; 2950 2951 if (Log::GetLogChannelCallbacks (ConstString(channel), log_callbacks)) 2952 { 2953 log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream); 2954 return true; 2955 } 2956 else 2957 { 2958 LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel)); 2959 if (log_channel_sp) 2960 { 2961 if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories)) 2962 { 2963 return true; 2964 } 2965 else 2966 { 2967 error_stream.Printf ("Invalid log channel '%s'.\n", channel); 2968 return false; 2969 } 2970 } 2971 else 2972 { 2973 error_stream.Printf ("Invalid log channel '%s'.\n", channel); 2974 return false; 2975 } 2976 } 2977 return false; 2978 } 2979 2980 SourceManager & 2981 Debugger::GetSourceManager () 2982 { 2983 if (m_source_manager_ap.get() == NULL) 2984 m_source_manager_ap.reset (new SourceManager (shared_from_this())); 2985 return *m_source_manager_ap; 2986 } 2987 2988 2989 2990 // This function handles events that were broadcast by the process. 2991 void 2992 Debugger::HandleBreakpointEvent (const EventSP &event_sp) 2993 { 2994 using namespace lldb; 2995 const uint32_t event_type = Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent (event_sp); 2996 2997 // if (event_type & eBreakpointEventTypeAdded 2998 // || event_type & eBreakpointEventTypeRemoved 2999 // || event_type & eBreakpointEventTypeEnabled 3000 // || event_type & eBreakpointEventTypeDisabled 3001 // || event_type & eBreakpointEventTypeCommandChanged 3002 // || event_type & eBreakpointEventTypeConditionChanged 3003 // || event_type & eBreakpointEventTypeIgnoreChanged 3004 // || event_type & eBreakpointEventTypeLocationsResolved) 3005 // { 3006 // // Don't do anything about these events, since the breakpoint commands already echo these actions. 3007 // } 3008 // 3009 if (event_type & eBreakpointEventTypeLocationsAdded) 3010 { 3011 uint32_t num_new_locations = Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent(event_sp); 3012 if (num_new_locations > 0) 3013 { 3014 BreakpointSP breakpoint = Breakpoint::BreakpointEventData::GetBreakpointFromEvent(event_sp); 3015 StreamFileSP output_sp (GetOutputFile()); 3016 if (output_sp) 3017 { 3018 output_sp->Printf("%d location%s added to breakpoint %d\n", 3019 num_new_locations, 3020 num_new_locations == 1 ? "" : "s", 3021 breakpoint->GetID()); 3022 RefreshTopIOHandler(); 3023 } 3024 } 3025 } 3026 // else if (event_type & eBreakpointEventTypeLocationsRemoved) 3027 // { 3028 // // These locations just get disabled, not sure it is worth spamming folks about this on the command line. 3029 // } 3030 // else if (event_type & eBreakpointEventTypeLocationsResolved) 3031 // { 3032 // // This might be an interesting thing to note, but I'm going to leave it quiet for now, it just looked noisy. 3033 // } 3034 } 3035 3036 size_t 3037 Debugger::GetProcessSTDOUT (Process *process, Stream *stream) 3038 { 3039 size_t total_bytes = 0; 3040 if (stream == NULL) 3041 stream = GetOutputFile().get(); 3042 3043 if (stream) 3044 { 3045 // The process has stuff waiting for stdout; get it and write it out to the appropriate place. 3046 if (process == NULL) 3047 { 3048 TargetSP target_sp = GetTargetList().GetSelectedTarget(); 3049 if (target_sp) 3050 process = target_sp->GetProcessSP().get(); 3051 } 3052 if (process) 3053 { 3054 Error error; 3055 size_t len; 3056 char stdio_buffer[1024]; 3057 while ((len = process->GetSTDOUT (stdio_buffer, sizeof (stdio_buffer), error)) > 0) 3058 { 3059 stream->Write(stdio_buffer, len); 3060 total_bytes += len; 3061 } 3062 } 3063 stream->Flush(); 3064 } 3065 return total_bytes; 3066 } 3067 3068 size_t 3069 Debugger::GetProcessSTDERR (Process *process, Stream *stream) 3070 { 3071 size_t total_bytes = 0; 3072 if (stream == NULL) 3073 stream = GetOutputFile().get(); 3074 3075 if (stream) 3076 { 3077 // The process has stuff waiting for stderr; get it and write it out to the appropriate place. 3078 if (process == NULL) 3079 { 3080 TargetSP target_sp = GetTargetList().GetSelectedTarget(); 3081 if (target_sp) 3082 process = target_sp->GetProcessSP().get(); 3083 } 3084 if (process) 3085 { 3086 Error error; 3087 size_t len; 3088 char stdio_buffer[1024]; 3089 while ((len = process->GetSTDERR (stdio_buffer, sizeof (stdio_buffer), error)) > 0) 3090 { 3091 stream->Write(stdio_buffer, len); 3092 total_bytes += len; 3093 } 3094 } 3095 stream->Flush(); 3096 } 3097 return total_bytes; 3098 } 3099 3100 // This function handles events that were broadcast by the process. 3101 void 3102 Debugger::HandleProcessEvent (const EventSP &event_sp) 3103 { 3104 using namespace lldb; 3105 const uint32_t event_type = event_sp->GetType(); 3106 ProcessSP process_sp = Process::ProcessEventData::GetProcessFromEvent(event_sp.get()); 3107 3108 StreamString output_stream; 3109 StreamString error_stream; 3110 const bool gui_enabled = IsForwardingEvents(); 3111 3112 if (!gui_enabled) 3113 { 3114 bool pop_process_io_handler = false; 3115 assert (process_sp); 3116 3117 if (event_type & Process::eBroadcastBitSTDOUT || event_type & Process::eBroadcastBitStateChanged) 3118 { 3119 GetProcessSTDOUT (process_sp.get(), &output_stream); 3120 } 3121 3122 if (event_type & Process::eBroadcastBitSTDERR || event_type & Process::eBroadcastBitStateChanged) 3123 { 3124 GetProcessSTDERR (process_sp.get(), &error_stream); 3125 } 3126 3127 if (event_type & Process::eBroadcastBitStateChanged) 3128 { 3129 3130 // Drain all stout and stderr so we don't see any output come after 3131 // we print our prompts 3132 // Something changed in the process; get the event and report the process's current status and location to 3133 // the user. 3134 StateType event_state = Process::ProcessEventData::GetStateFromEvent (event_sp.get()); 3135 if (event_state == eStateInvalid) 3136 return; 3137 3138 switch (event_state) 3139 { 3140 case eStateInvalid: 3141 case eStateUnloaded: 3142 case eStateConnected: 3143 case eStateAttaching: 3144 case eStateLaunching: 3145 case eStateStepping: 3146 case eStateDetached: 3147 { 3148 output_stream.Printf("Process %" PRIu64 " %s\n", 3149 process_sp->GetID(), 3150 StateAsCString (event_state)); 3151 3152 if (event_state == eStateDetached) 3153 pop_process_io_handler = true; 3154 } 3155 break; 3156 3157 case eStateRunning: 3158 // Don't be chatty when we run... 3159 break; 3160 3161 case eStateExited: 3162 process_sp->GetStatus(output_stream); 3163 pop_process_io_handler = true; 3164 break; 3165 3166 case eStateStopped: 3167 case eStateCrashed: 3168 case eStateSuspended: 3169 // Make sure the program hasn't been auto-restarted: 3170 if (Process::ProcessEventData::GetRestartedFromEvent (event_sp.get())) 3171 { 3172 size_t num_reasons = Process::ProcessEventData::GetNumRestartedReasons(event_sp.get()); 3173 if (num_reasons > 0) 3174 { 3175 // FIXME: Do we want to report this, or would that just be annoyingly chatty? 3176 if (num_reasons == 1) 3177 { 3178 const char *reason = Process::ProcessEventData::GetRestartedReasonAtIndex (event_sp.get(), 0); 3179 output_stream.Printf("Process %" PRIu64 " stopped and restarted: %s\n", 3180 process_sp->GetID(), 3181 reason ? reason : "<UNKNOWN REASON>"); 3182 } 3183 else 3184 { 3185 output_stream.Printf("Process %" PRIu64 " stopped and restarted, reasons:\n", 3186 process_sp->GetID()); 3187 3188 3189 for (size_t i = 0; i < num_reasons; i++) 3190 { 3191 const char *reason = Process::ProcessEventData::GetRestartedReasonAtIndex (event_sp.get(), i); 3192 output_stream.Printf("\t%s\n", reason ? reason : "<UNKNOWN REASON>"); 3193 } 3194 } 3195 } 3196 } 3197 else 3198 { 3199 // Lock the thread list so it doesn't change on us, this is the scope for the locker: 3200 { 3201 ThreadList &thread_list = process_sp->GetThreadList(); 3202 Mutex::Locker locker (thread_list.GetMutex()); 3203 3204 ThreadSP curr_thread (thread_list.GetSelectedThread()); 3205 ThreadSP thread; 3206 StopReason curr_thread_stop_reason = eStopReasonInvalid; 3207 if (curr_thread) 3208 curr_thread_stop_reason = curr_thread->GetStopReason(); 3209 if (!curr_thread || 3210 !curr_thread->IsValid() || 3211 curr_thread_stop_reason == eStopReasonInvalid || 3212 curr_thread_stop_reason == eStopReasonNone) 3213 { 3214 // Prefer a thread that has just completed its plan over another thread as current thread. 3215 ThreadSP plan_thread; 3216 ThreadSP other_thread; 3217 const size_t num_threads = thread_list.GetSize(); 3218 size_t i; 3219 for (i = 0; i < num_threads; ++i) 3220 { 3221 thread = thread_list.GetThreadAtIndex(i); 3222 StopReason thread_stop_reason = thread->GetStopReason(); 3223 switch (thread_stop_reason) 3224 { 3225 case eStopReasonInvalid: 3226 case eStopReasonNone: 3227 break; 3228 3229 case eStopReasonTrace: 3230 case eStopReasonBreakpoint: 3231 case eStopReasonWatchpoint: 3232 case eStopReasonSignal: 3233 case eStopReasonException: 3234 case eStopReasonExec: 3235 case eStopReasonThreadExiting: 3236 case eStopReasonInstrumentation: 3237 if (!other_thread) 3238 other_thread = thread; 3239 break; 3240 case eStopReasonPlanComplete: 3241 if (!plan_thread) 3242 plan_thread = thread; 3243 break; 3244 } 3245 } 3246 if (plan_thread) 3247 thread_list.SetSelectedThreadByID (plan_thread->GetID()); 3248 else if (other_thread) 3249 thread_list.SetSelectedThreadByID (other_thread->GetID()); 3250 else 3251 { 3252 if (curr_thread && curr_thread->IsValid()) 3253 thread = curr_thread; 3254 else 3255 thread = thread_list.GetThreadAtIndex(0); 3256 3257 if (thread) 3258 thread_list.SetSelectedThreadByID (thread->GetID()); 3259 } 3260 } 3261 } 3262 // Drop the ThreadList mutex by here, since GetThreadStatus below might have to run code, 3263 // e.g. for Data formatters, and if we hold the ThreadList mutex, then the process is going to 3264 // have a hard time restarting the process. 3265 3266 if (GetTargetList().GetSelectedTarget().get() == &process_sp->GetTarget()) 3267 { 3268 const bool only_threads_with_stop_reason = true; 3269 const uint32_t start_frame = 0; 3270 const uint32_t num_frames = 1; 3271 const uint32_t num_frames_with_source = 1; 3272 process_sp->GetStatus(output_stream); 3273 process_sp->GetThreadStatus (output_stream, 3274 only_threads_with_stop_reason, 3275 start_frame, 3276 num_frames, 3277 num_frames_with_source); 3278 } 3279 else 3280 { 3281 uint32_t target_idx = GetTargetList().GetIndexOfTarget(process_sp->GetTarget().shared_from_this()); 3282 if (target_idx != UINT32_MAX) 3283 output_stream.Printf ("Target %d: (", target_idx); 3284 else 3285 output_stream.Printf ("Target <unknown index>: ("); 3286 process_sp->GetTarget().Dump (&output_stream, eDescriptionLevelBrief); 3287 output_stream.Printf (") stopped.\n"); 3288 } 3289 3290 // Pop the process IO handler 3291 pop_process_io_handler = true; 3292 } 3293 break; 3294 } 3295 } 3296 3297 if (output_stream.GetSize() || error_stream.GetSize()) 3298 { 3299 StreamFileSP error_stream_sp (GetOutputFile()); 3300 bool top_io_handler_hid = false; 3301 3302 if (process_sp->ProcessIOHandlerIsActive() == false) 3303 top_io_handler_hid = HideTopIOHandler(); 3304 3305 if (output_stream.GetSize()) 3306 { 3307 StreamFileSP output_stream_sp (GetOutputFile()); 3308 if (output_stream_sp) 3309 output_stream_sp->Write (output_stream.GetData(), output_stream.GetSize()); 3310 } 3311 3312 if (error_stream.GetSize()) 3313 { 3314 StreamFileSP error_stream_sp (GetErrorFile()); 3315 if (error_stream_sp) 3316 error_stream_sp->Write (error_stream.GetData(), error_stream.GetSize()); 3317 } 3318 3319 if (top_io_handler_hid) 3320 RefreshTopIOHandler(); 3321 } 3322 3323 if (pop_process_io_handler) 3324 process_sp->PopProcessIOHandler(); 3325 } 3326 } 3327 3328 void 3329 Debugger::HandleThreadEvent (const EventSP &event_sp) 3330 { 3331 // At present the only thread event we handle is the Frame Changed event, 3332 // and all we do for that is just reprint the thread status for that thread. 3333 using namespace lldb; 3334 const uint32_t event_type = event_sp->GetType(); 3335 if (event_type == Thread::eBroadcastBitStackChanged || 3336 event_type == Thread::eBroadcastBitThreadSelected ) 3337 { 3338 ThreadSP thread_sp (Thread::ThreadEventData::GetThreadFromEvent (event_sp.get())); 3339 if (thread_sp) 3340 { 3341 HideTopIOHandler(); 3342 StreamFileSP stream_sp (GetOutputFile()); 3343 thread_sp->GetStatus(*stream_sp, 0, 1, 1); 3344 RefreshTopIOHandler(); 3345 } 3346 } 3347 } 3348 3349 bool 3350 Debugger::IsForwardingEvents () 3351 { 3352 return (bool)m_forward_listener_sp; 3353 } 3354 3355 void 3356 Debugger::EnableForwardEvents (const ListenerSP &listener_sp) 3357 { 3358 m_forward_listener_sp = listener_sp; 3359 } 3360 3361 void 3362 Debugger::CancelForwardEvents (const ListenerSP &listener_sp) 3363 { 3364 m_forward_listener_sp.reset(); 3365 } 3366 3367 3368 void 3369 Debugger::DefaultEventHandler() 3370 { 3371 Listener& listener(GetListener()); 3372 ConstString broadcaster_class_target(Target::GetStaticBroadcasterClass()); 3373 ConstString broadcaster_class_process(Process::GetStaticBroadcasterClass()); 3374 ConstString broadcaster_class_thread(Thread::GetStaticBroadcasterClass()); 3375 BroadcastEventSpec target_event_spec (broadcaster_class_target, 3376 Target::eBroadcastBitBreakpointChanged); 3377 3378 BroadcastEventSpec process_event_spec (broadcaster_class_process, 3379 Process::eBroadcastBitStateChanged | 3380 Process::eBroadcastBitSTDOUT | 3381 Process::eBroadcastBitSTDERR); 3382 3383 BroadcastEventSpec thread_event_spec (broadcaster_class_thread, 3384 Thread::eBroadcastBitStackChanged | 3385 Thread::eBroadcastBitThreadSelected ); 3386 3387 listener.StartListeningForEventSpec (*this, target_event_spec); 3388 listener.StartListeningForEventSpec (*this, process_event_spec); 3389 listener.StartListeningForEventSpec (*this, thread_event_spec); 3390 listener.StartListeningForEvents (m_command_interpreter_ap.get(), 3391 CommandInterpreter::eBroadcastBitQuitCommandReceived | 3392 CommandInterpreter::eBroadcastBitAsynchronousOutputData | 3393 CommandInterpreter::eBroadcastBitAsynchronousErrorData ); 3394 3395 bool done = false; 3396 while (!done) 3397 { 3398 // Mutex::Locker locker; 3399 // if (locker.TryLock(m_input_reader_stack.GetMutex())) 3400 // { 3401 // if (m_input_reader_stack.IsEmpty()) 3402 // break; 3403 // } 3404 // 3405 EventSP event_sp; 3406 if (listener.WaitForEvent(NULL, event_sp)) 3407 { 3408 if (event_sp) 3409 { 3410 Broadcaster *broadcaster = event_sp->GetBroadcaster(); 3411 if (broadcaster) 3412 { 3413 uint32_t event_type = event_sp->GetType(); 3414 ConstString broadcaster_class (broadcaster->GetBroadcasterClass()); 3415 if (broadcaster_class == broadcaster_class_process) 3416 { 3417 HandleProcessEvent (event_sp); 3418 } 3419 else if (broadcaster_class == broadcaster_class_target) 3420 { 3421 if (Breakpoint::BreakpointEventData::GetEventDataFromEvent(event_sp.get())) 3422 { 3423 HandleBreakpointEvent (event_sp); 3424 } 3425 } 3426 else if (broadcaster_class == broadcaster_class_thread) 3427 { 3428 HandleThreadEvent (event_sp); 3429 } 3430 else if (broadcaster == m_command_interpreter_ap.get()) 3431 { 3432 if (event_type & CommandInterpreter::eBroadcastBitQuitCommandReceived) 3433 { 3434 done = true; 3435 } 3436 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousErrorData) 3437 { 3438 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get())); 3439 if (data && data[0]) 3440 { 3441 StreamFileSP error_sp (GetErrorFile()); 3442 if (error_sp) 3443 { 3444 HideTopIOHandler(); 3445 error_sp->PutCString(data); 3446 error_sp->Flush(); 3447 RefreshTopIOHandler(); 3448 } 3449 } 3450 } 3451 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousOutputData) 3452 { 3453 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get())); 3454 if (data && data[0]) 3455 { 3456 StreamFileSP output_sp (GetOutputFile()); 3457 if (output_sp) 3458 { 3459 HideTopIOHandler(); 3460 output_sp->PutCString(data); 3461 output_sp->Flush(); 3462 RefreshTopIOHandler(); 3463 } 3464 } 3465 } 3466 } 3467 } 3468 3469 if (m_forward_listener_sp) 3470 m_forward_listener_sp->AddEvent(event_sp); 3471 } 3472 } 3473 } 3474 } 3475 3476 lldb::thread_result_t 3477 Debugger::EventHandlerThread (lldb::thread_arg_t arg) 3478 { 3479 ((Debugger *)arg)->DefaultEventHandler(); 3480 return NULL; 3481 } 3482 3483 bool 3484 Debugger::StartEventHandlerThread() 3485 { 3486 if (!m_event_handler_thread.IsJoinable()) 3487 { 3488 m_event_handler_thread = ThreadLauncher::LaunchThread ("lldb.debugger.event-handler", 3489 EventHandlerThread, 3490 this, 3491 NULL, 3492 8*1024*1024); // Use larger 8MB stack for this thread 3493 } 3494 return m_event_handler_thread.IsJoinable(); 3495 } 3496 3497 void 3498 Debugger::StopEventHandlerThread() 3499 { 3500 if (m_event_handler_thread.IsJoinable()) 3501 { 3502 GetCommandInterpreter().BroadcastEvent(CommandInterpreter::eBroadcastBitQuitCommandReceived); 3503 m_event_handler_thread.Join(nullptr); 3504 } 3505 } 3506 3507 3508 lldb::thread_result_t 3509 Debugger::IOHandlerThread (lldb::thread_arg_t arg) 3510 { 3511 Debugger *debugger = (Debugger *)arg; 3512 debugger->ExecuteIOHanders(); 3513 debugger->StopEventHandlerThread(); 3514 return NULL; 3515 } 3516 3517 bool 3518 Debugger::StartIOHandlerThread() 3519 { 3520 if (!m_io_handler_thread.IsJoinable()) 3521 m_io_handler_thread = ThreadLauncher::LaunchThread ("lldb.debugger.io-handler", 3522 IOHandlerThread, 3523 this, 3524 NULL, 3525 8*1024*1024); // Use larger 8MB stack for this thread 3526 return m_io_handler_thread.IsJoinable(); 3527 } 3528 3529 void 3530 Debugger::StopIOHandlerThread() 3531 { 3532 if (m_io_handler_thread.IsJoinable()) 3533 { 3534 if (m_input_file_sp) 3535 m_input_file_sp->GetFile().Close(); 3536 m_io_handler_thread.Join(nullptr); 3537 } 3538 } 3539 3540 3541