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