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/InputReader.h" 22 #include "lldb/Core/Module.h" 23 #include "lldb/Core/PluginManager.h" 24 #include "lldb/Core/RegisterValue.h" 25 #include "lldb/Core/State.h" 26 #include "lldb/Core/StreamAsynchronousIO.h" 27 #include "lldb/Core/StreamCallback.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/Host/DynamicLibrary.h" 35 #include "lldb/Host/Terminal.h" 36 #include "lldb/Interpreter/CommandInterpreter.h" 37 #include "lldb/Interpreter/OptionValueSInt64.h" 38 #include "lldb/Interpreter/OptionValueString.h" 39 #include "lldb/Symbol/ClangASTContext.h" 40 #include "lldb/Symbol/CompileUnit.h" 41 #include "lldb/Symbol/Function.h" 42 #include "lldb/Symbol/Symbol.h" 43 #include "lldb/Symbol/VariableList.h" 44 #include "lldb/Target/TargetList.h" 45 #include "lldb/Target/Process.h" 46 #include "lldb/Target/RegisterContext.h" 47 #include "lldb/Target/SectionLoadList.h" 48 #include "lldb/Target/StopInfo.h" 49 #include "lldb/Target/Target.h" 50 #include "lldb/Target/Thread.h" 51 #include "lldb/Utility/AnsiTerminal.h" 52 53 using namespace lldb; 54 using namespace lldb_private; 55 56 57 static uint32_t g_shared_debugger_refcount = 0; 58 static lldb::user_id_t g_unique_id = 1; 59 60 #pragma mark Static Functions 61 62 static Mutex & 63 GetDebuggerListMutex () 64 { 65 static Mutex g_mutex(Mutex::eMutexTypeRecursive); 66 return g_mutex; 67 } 68 69 typedef std::vector<DebuggerSP> DebuggerList; 70 71 static DebuggerList & 72 GetDebuggerList() 73 { 74 // hide the static debugger list inside a singleton accessor to avoid 75 // global init contructors 76 static DebuggerList g_list; 77 return g_list; 78 } 79 80 OptionEnumValueElement 81 g_show_disassembly_enum_values[] = 82 { 83 { Debugger::eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."}, 84 { Debugger::eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."}, 85 { Debugger::eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."}, 86 { 0, NULL, NULL } 87 }; 88 89 OptionEnumValueElement 90 g_language_enumerators[] = 91 { 92 { eScriptLanguageNone, "none", "Disable scripting languages."}, 93 { eScriptLanguagePython, "python", "Select python as the default scripting language."}, 94 { eScriptLanguageDefault, "default", "Select the lldb default as the default scripting language."}, 95 { 0, NULL, NULL } 96 }; 97 98 #define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name-with-args}${function.pc-offset}}}" 99 #define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}" 100 101 #define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id%tid}"\ 102 "{, ${frame.pc}}"\ 103 MODULE_WITH_FUNC\ 104 FILE_AND_LINE\ 105 "{, name = '${thread.name}'}"\ 106 "{, queue = '${thread.queue}'}"\ 107 "{, stop reason = ${thread.stop-reason}}"\ 108 "{\\nReturn value: ${thread.return-value}}"\ 109 "\\n" 110 111 #define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\ 112 MODULE_WITH_FUNC\ 113 FILE_AND_LINE\ 114 "\\n" 115 116 117 118 static PropertyDefinition 119 g_properties[] = 120 { 121 { "auto-confirm", OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true all confirmation prompts will receive their default reply." }, 122 { "frame-format", OptionValue::eTypeString , true, 0 , DEFAULT_FRAME_FORMAT, NULL, "The default frame format string to use when displaying stack frame information for threads." }, 123 { "notify-void", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Notify the user explicitly if an expression returns void (default: false)." }, 124 { "prompt", OptionValue::eTypeString , true, OptionValueString::eOptionEncodeCharacterEscapeSequences, "(lldb) ", NULL, "The debugger command line prompt displayed for the user." }, 125 { "script-lang", OptionValue::eTypeEnum , true, eScriptLanguagePython, NULL, g_language_enumerators, "The script language to be used for evaluating user-written scripts." }, 126 { "stop-disassembly-count", OptionValue::eTypeSInt64 , true, 4 , NULL, NULL, "The number of disassembly lines to show when displaying a stopped context." }, 127 { "stop-disassembly-display", OptionValue::eTypeEnum , true, Debugger::eStopDisassemblyTypeNoSource, NULL, g_show_disassembly_enum_values, "Control when to display disassembly when displaying a stopped context." }, 128 { "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." }, 129 { "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." }, 130 { "term-width", OptionValue::eTypeSInt64 , true, 80 , NULL, NULL, "The maximum number of columns to use for displaying text." }, 131 { "thread-format", OptionValue::eTypeString , true, 0 , DEFAULT_THREAD_FORMAT, NULL, "The default thread format string to use when displaying thread information." }, 132 { "use-external-editor", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Whether to use an external editor or not." }, 133 { "use-color", OptionValue::eTypeBoolean, true, true , NULL, NULL, "Whether to use Ansi color codes or not." }, 134 { "auto-one-line-summaries", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, LLDB will automatically display small structs in one-liner format (default: true)." }, 135 136 { NULL, OptionValue::eTypeInvalid, true, 0 , NULL, NULL, NULL } 137 }; 138 139 enum 140 { 141 ePropertyAutoConfirm = 0, 142 ePropertyFrameFormat, 143 ePropertyNotiftVoid, 144 ePropertyPrompt, 145 ePropertyScriptLanguage, 146 ePropertyStopDisassemblyCount, 147 ePropertyStopDisassemblyDisplay, 148 ePropertyStopLineCountAfter, 149 ePropertyStopLineCountBefore, 150 ePropertyTerminalWidth, 151 ePropertyThreadFormat, 152 ePropertyUseExternalEditor, 153 ePropertyUseColor, 154 ePropertyAutoOneLineSummaries 155 }; 156 157 Debugger::LoadPluginCallbackType Debugger::g_load_plugin_callback = NULL; 158 159 Error 160 Debugger::SetPropertyValue (const ExecutionContext *exe_ctx, 161 VarSetOperationType op, 162 const char *property_path, 163 const char *value) 164 { 165 bool is_load_script = strcmp(property_path,"target.load-script-from-symbol-file") == 0; 166 TargetSP target_sp; 167 LoadScriptFromSymFile load_script_old_value; 168 if (is_load_script && exe_ctx->GetTargetSP()) 169 { 170 target_sp = exe_ctx->GetTargetSP(); 171 load_script_old_value = target_sp->TargetProperties::GetLoadScriptFromSymbolFile(); 172 } 173 Error error (Properties::SetPropertyValue (exe_ctx, op, property_path, value)); 174 if (error.Success()) 175 { 176 // FIXME it would be nice to have "on-change" callbacks for properties 177 if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0) 178 { 179 const char *new_prompt = GetPrompt(); 180 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor()); 181 if (str.length()) 182 new_prompt = str.c_str(); 183 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt))); 184 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp); 185 } 186 else if (strcmp(property_path, g_properties[ePropertyUseColor].name) == 0) 187 { 188 // use-color changed. Ping the prompt so it can reset the ansi terminal codes. 189 SetPrompt (GetPrompt()); 190 } 191 else if (is_load_script && target_sp && load_script_old_value == eLoadScriptFromSymFileWarn) 192 { 193 if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() == eLoadScriptFromSymFileTrue) 194 { 195 std::list<Error> errors; 196 StreamString feedback_stream; 197 if (!target_sp->LoadScriptingResources(errors,&feedback_stream)) 198 { 199 for (auto error : errors) 200 { 201 GetErrorStream().Printf("%s\n",error.AsCString()); 202 } 203 if (feedback_stream.GetSize()) 204 GetErrorStream().Printf("%s",feedback_stream.GetData()); 205 } 206 } 207 } 208 } 209 return error; 210 } 211 212 bool 213 Debugger::GetAutoConfirm () const 214 { 215 const uint32_t idx = ePropertyAutoConfirm; 216 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); 217 } 218 219 const char * 220 Debugger::GetFrameFormat() const 221 { 222 const uint32_t idx = ePropertyFrameFormat; 223 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value); 224 } 225 226 bool 227 Debugger::GetNotifyVoid () const 228 { 229 const uint32_t idx = ePropertyNotiftVoid; 230 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); 231 } 232 233 const char * 234 Debugger::GetPrompt() const 235 { 236 const uint32_t idx = ePropertyPrompt; 237 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value); 238 } 239 240 void 241 Debugger::SetPrompt(const char *p) 242 { 243 const uint32_t idx = ePropertyPrompt; 244 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p); 245 const char *new_prompt = GetPrompt(); 246 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor()); 247 if (str.length()) 248 new_prompt = str.c_str(); 249 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));; 250 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp); 251 } 252 253 const char * 254 Debugger::GetThreadFormat() const 255 { 256 const uint32_t idx = ePropertyThreadFormat; 257 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value); 258 } 259 260 lldb::ScriptLanguage 261 Debugger::GetScriptLanguage() const 262 { 263 const uint32_t idx = ePropertyScriptLanguage; 264 return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value); 265 } 266 267 bool 268 Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang) 269 { 270 const uint32_t idx = ePropertyScriptLanguage; 271 return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang); 272 } 273 274 uint32_t 275 Debugger::GetTerminalWidth () const 276 { 277 const uint32_t idx = ePropertyTerminalWidth; 278 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value); 279 } 280 281 bool 282 Debugger::SetTerminalWidth (uint32_t term_width) 283 { 284 const uint32_t idx = ePropertyTerminalWidth; 285 return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width); 286 } 287 288 bool 289 Debugger::GetUseExternalEditor () const 290 { 291 const uint32_t idx = ePropertyUseExternalEditor; 292 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); 293 } 294 295 bool 296 Debugger::SetUseExternalEditor (bool b) 297 { 298 const uint32_t idx = ePropertyUseExternalEditor; 299 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b); 300 } 301 302 bool 303 Debugger::GetUseColor () const 304 { 305 const uint32_t idx = ePropertyUseColor; 306 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); 307 } 308 309 bool 310 Debugger::SetUseColor (bool b) 311 { 312 const uint32_t idx = ePropertyUseColor; 313 bool ret = m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b); 314 SetPrompt (GetPrompt()); 315 return ret; 316 } 317 318 uint32_t 319 Debugger::GetStopSourceLineCount (bool before) const 320 { 321 const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter; 322 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value); 323 } 324 325 Debugger::StopDisassemblyType 326 Debugger::GetStopDisassemblyDisplay () const 327 { 328 const uint32_t idx = ePropertyStopDisassemblyDisplay; 329 return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value); 330 } 331 332 uint32_t 333 Debugger::GetDisassemblyLineCount () const 334 { 335 const uint32_t idx = ePropertyStopDisassemblyCount; 336 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value); 337 } 338 339 bool 340 Debugger::GetAutoOneLineSummaries () const 341 { 342 const uint32_t idx = ePropertyAutoOneLineSummaries; 343 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true); 344 345 } 346 347 #pragma mark Debugger 348 349 //const DebuggerPropertiesSP & 350 //Debugger::GetSettings() const 351 //{ 352 // return m_properties_sp; 353 //} 354 // 355 356 int 357 Debugger::TestDebuggerRefCount () 358 { 359 return g_shared_debugger_refcount; 360 } 361 362 void 363 Debugger::Initialize (LoadPluginCallbackType load_plugin_callback) 364 { 365 g_load_plugin_callback = load_plugin_callback; 366 if (g_shared_debugger_refcount++ == 0) 367 lldb_private::Initialize(); 368 } 369 370 void 371 Debugger::Terminate () 372 { 373 if (g_shared_debugger_refcount > 0) 374 { 375 g_shared_debugger_refcount--; 376 if (g_shared_debugger_refcount == 0) 377 { 378 lldb_private::WillTerminate(); 379 lldb_private::Terminate(); 380 381 // Clear our master list of debugger objects 382 Mutex::Locker locker (GetDebuggerListMutex ()); 383 GetDebuggerList().clear(); 384 } 385 } 386 } 387 388 void 389 Debugger::SettingsInitialize () 390 { 391 Target::SettingsInitialize (); 392 } 393 394 void 395 Debugger::SettingsTerminate () 396 { 397 Target::SettingsTerminate (); 398 } 399 400 bool 401 Debugger::LoadPlugin (const FileSpec& spec, Error& error) 402 { 403 if (g_load_plugin_callback) 404 { 405 lldb::DynamicLibrarySP dynlib_sp = g_load_plugin_callback (shared_from_this(), spec, error); 406 if (dynlib_sp) 407 { 408 m_loaded_plugins.push_back(dynlib_sp); 409 return true; 410 } 411 } 412 else 413 { 414 // The g_load_plugin_callback is registered in SBDebugger::Initialize() 415 // and if the public API layer isn't available (code is linking against 416 // all of the internal LLDB static libraries), then we can't load plugins 417 error.SetErrorString("Public API layer is not available"); 418 } 419 return false; 420 } 421 422 static FileSpec::EnumerateDirectoryResult 423 LoadPluginCallback 424 ( 425 void *baton, 426 FileSpec::FileType file_type, 427 const FileSpec &file_spec 428 ) 429 { 430 Error error; 431 432 static ConstString g_dylibext("dylib"); 433 static ConstString g_solibext("so"); 434 435 if (!baton) 436 return FileSpec::eEnumerateDirectoryResultQuit; 437 438 Debugger *debugger = (Debugger*)baton; 439 440 // If we have a regular file, a symbolic link or unknown file type, try 441 // and process the file. We must handle unknown as sometimes the directory 442 // enumeration might be enumerating a file system that doesn't have correct 443 // file type information. 444 if (file_type == FileSpec::eFileTypeRegular || 445 file_type == FileSpec::eFileTypeSymbolicLink || 446 file_type == FileSpec::eFileTypeUnknown ) 447 { 448 FileSpec plugin_file_spec (file_spec); 449 plugin_file_spec.ResolvePath (); 450 451 if (plugin_file_spec.GetFileNameExtension() != g_dylibext && 452 plugin_file_spec.GetFileNameExtension() != g_solibext) 453 { 454 return FileSpec::eEnumerateDirectoryResultNext; 455 } 456 457 Error plugin_load_error; 458 debugger->LoadPlugin (plugin_file_spec, plugin_load_error); 459 460 return FileSpec::eEnumerateDirectoryResultNext; 461 } 462 463 else if (file_type == FileSpec::eFileTypeUnknown || 464 file_type == FileSpec::eFileTypeDirectory || 465 file_type == FileSpec::eFileTypeSymbolicLink ) 466 { 467 // Try and recurse into anything that a directory or symbolic link. 468 // We must also do this for unknown as sometimes the directory enumeration 469 // might be enurating a file system that doesn't have correct file type 470 // information. 471 return FileSpec::eEnumerateDirectoryResultEnter; 472 } 473 474 return FileSpec::eEnumerateDirectoryResultNext; 475 } 476 477 void 478 Debugger::InstanceInitialize () 479 { 480 FileSpec dir_spec; 481 const bool find_directories = true; 482 const bool find_files = true; 483 const bool find_other = true; 484 char dir_path[PATH_MAX]; 485 if (Host::GetLLDBPath (ePathTypeLLDBSystemPlugins, dir_spec)) 486 { 487 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path))) 488 { 489 FileSpec::EnumerateDirectory (dir_path, 490 find_directories, 491 find_files, 492 find_other, 493 LoadPluginCallback, 494 this); 495 } 496 } 497 498 if (Host::GetLLDBPath (ePathTypeLLDBUserPlugins, dir_spec)) 499 { 500 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path))) 501 { 502 FileSpec::EnumerateDirectory (dir_path, 503 find_directories, 504 find_files, 505 find_other, 506 LoadPluginCallback, 507 this); 508 } 509 } 510 511 PluginManager::DebuggerInitialize (*this); 512 } 513 514 DebuggerSP 515 Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton) 516 { 517 DebuggerSP debugger_sp (new Debugger(log_callback, baton)); 518 if (g_shared_debugger_refcount > 0) 519 { 520 Mutex::Locker locker (GetDebuggerListMutex ()); 521 GetDebuggerList().push_back(debugger_sp); 522 } 523 debugger_sp->InstanceInitialize (); 524 return debugger_sp; 525 } 526 527 void 528 Debugger::Destroy (DebuggerSP &debugger_sp) 529 { 530 if (debugger_sp.get() == NULL) 531 return; 532 533 debugger_sp->Clear(); 534 535 if (g_shared_debugger_refcount > 0) 536 { 537 Mutex::Locker locker (GetDebuggerListMutex ()); 538 DebuggerList &debugger_list = GetDebuggerList (); 539 DebuggerList::iterator pos, end = debugger_list.end(); 540 for (pos = debugger_list.begin (); pos != end; ++pos) 541 { 542 if ((*pos).get() == debugger_sp.get()) 543 { 544 debugger_list.erase (pos); 545 return; 546 } 547 } 548 } 549 } 550 551 DebuggerSP 552 Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name) 553 { 554 DebuggerSP debugger_sp; 555 if (g_shared_debugger_refcount > 0) 556 { 557 Mutex::Locker locker (GetDebuggerListMutex ()); 558 DebuggerList &debugger_list = GetDebuggerList(); 559 DebuggerList::iterator pos, end = debugger_list.end(); 560 561 for (pos = debugger_list.begin(); pos != end; ++pos) 562 { 563 if ((*pos).get()->m_instance_name == instance_name) 564 { 565 debugger_sp = *pos; 566 break; 567 } 568 } 569 } 570 return debugger_sp; 571 } 572 573 TargetSP 574 Debugger::FindTargetWithProcessID (lldb::pid_t pid) 575 { 576 TargetSP target_sp; 577 if (g_shared_debugger_refcount > 0) 578 { 579 Mutex::Locker locker (GetDebuggerListMutex ()); 580 DebuggerList &debugger_list = GetDebuggerList(); 581 DebuggerList::iterator pos, end = debugger_list.end(); 582 for (pos = debugger_list.begin(); pos != end; ++pos) 583 { 584 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid); 585 if (target_sp) 586 break; 587 } 588 } 589 return target_sp; 590 } 591 592 TargetSP 593 Debugger::FindTargetWithProcess (Process *process) 594 { 595 TargetSP target_sp; 596 if (g_shared_debugger_refcount > 0) 597 { 598 Mutex::Locker locker (GetDebuggerListMutex ()); 599 DebuggerList &debugger_list = GetDebuggerList(); 600 DebuggerList::iterator pos, end = debugger_list.end(); 601 for (pos = debugger_list.begin(); pos != end; ++pos) 602 { 603 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process); 604 if (target_sp) 605 break; 606 } 607 } 608 return target_sp; 609 } 610 611 Debugger::Debugger (lldb::LogOutputCallback log_callback, void *baton) : 612 UserID (g_unique_id++), 613 Properties(OptionValuePropertiesSP(new OptionValueProperties())), 614 m_input_comm("debugger.input"), 615 m_input_file (), 616 m_output_file (), 617 m_error_file (), 618 m_terminal_state (), 619 m_target_list (*this), 620 m_platform_list (), 621 m_listener ("lldb.Debugger"), 622 m_source_manager_ap(), 623 m_source_file_cache(), 624 m_command_interpreter_ap (new CommandInterpreter (*this, eScriptLanguageDefault, false)), 625 m_input_reader_stack (), 626 m_input_reader_data (), 627 m_instance_name() 628 { 629 char instance_cstr[256]; 630 snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID()); 631 m_instance_name.SetCString(instance_cstr); 632 if (log_callback) 633 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton)); 634 m_command_interpreter_ap->Initialize (); 635 // Always add our default platform to the platform list 636 PlatformSP default_platform_sp (Platform::GetDefaultPlatform()); 637 assert (default_platform_sp.get()); 638 m_platform_list.Append (default_platform_sp, true); 639 640 m_collection_sp->Initialize (g_properties); 641 m_collection_sp->AppendProperty (ConstString("target"), 642 ConstString("Settings specify to debugging targets."), 643 true, 644 Target::GetGlobalProperties()->GetValueProperties()); 645 if (m_command_interpreter_ap.get()) 646 { 647 m_collection_sp->AppendProperty (ConstString("interpreter"), 648 ConstString("Settings specify to the debugger's command interpreter."), 649 true, 650 m_command_interpreter_ap->GetValueProperties()); 651 } 652 OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth); 653 term_width->SetMinimumValue(10); 654 term_width->SetMaximumValue(1024); 655 656 // Turn off use-color if this is a dumb terminal. 657 const char *term = getenv ("TERM"); 658 if (term && !strcmp (term, "dumb")) 659 SetUseColor (false); 660 } 661 662 Debugger::~Debugger () 663 { 664 Clear(); 665 } 666 667 void 668 Debugger::Clear() 669 { 670 CleanUpInputReaders(); 671 m_listener.Clear(); 672 int num_targets = m_target_list.GetNumTargets(); 673 for (int i = 0; i < num_targets; i++) 674 { 675 TargetSP target_sp (m_target_list.GetTargetAtIndex (i)); 676 if (target_sp) 677 { 678 ProcessSP process_sp (target_sp->GetProcessSP()); 679 if (process_sp) 680 process_sp->Finalize(); 681 target_sp->Destroy(); 682 } 683 } 684 BroadcasterManager::Clear (); 685 686 // Close the input file _before_ we close the input read communications class 687 // as it does NOT own the input file, our m_input_file does. 688 m_terminal_state.Clear(); 689 GetInputFile().Close (); 690 // Now that we have closed m_input_file, we can now tell our input communication 691 // class to close down. Its read thread should quickly exit after we close 692 // the input file handle above. 693 m_input_comm.Clear (); 694 } 695 696 bool 697 Debugger::GetCloseInputOnEOF () const 698 { 699 return m_input_comm.GetCloseOnEOF(); 700 } 701 702 void 703 Debugger::SetCloseInputOnEOF (bool b) 704 { 705 m_input_comm.SetCloseOnEOF(b); 706 } 707 708 bool 709 Debugger::GetAsyncExecution () 710 { 711 return !m_command_interpreter_ap->GetSynchronous(); 712 } 713 714 void 715 Debugger::SetAsyncExecution (bool async_execution) 716 { 717 m_command_interpreter_ap->SetSynchronous (!async_execution); 718 } 719 720 721 void 722 Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership) 723 { 724 File &in_file = GetInputFile(); 725 in_file.SetStream (fh, tranfer_ownership); 726 if (in_file.IsValid() == false) 727 in_file.SetStream (stdin, true); 728 729 // Disconnect from any old connection if we had one 730 m_input_comm.Disconnect (); 731 // Pass false as the second argument to ConnectionFileDescriptor below because 732 // our "in_file" above will already take ownership if requested and we don't 733 // want to objects trying to own and close a file descriptor. 734 m_input_comm.SetConnection (new ConnectionFileDescriptor (in_file.GetDescriptor(), false)); 735 m_input_comm.SetReadThreadBytesReceivedCallback (Debugger::DispatchInputCallback, this); 736 737 // Save away the terminal state if that is relevant, so that we can restore it in RestoreInputState. 738 SaveInputTerminalState (); 739 740 Error error; 741 if (m_input_comm.StartReadThread (&error) == false) 742 { 743 File &err_file = GetErrorFile(); 744 745 err_file.Printf ("error: failed to main input read thread: %s", error.AsCString() ? error.AsCString() : "unkown error"); 746 exit(1); 747 } 748 } 749 750 void 751 Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership) 752 { 753 File &out_file = GetOutputFile(); 754 out_file.SetStream (fh, tranfer_ownership); 755 if (out_file.IsValid() == false) 756 out_file.SetStream (stdout, false); 757 758 // do not create the ScriptInterpreter just for setting the output file handle 759 // as the constructor will know how to do the right thing on its own 760 const bool can_create = false; 761 ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create); 762 if (script_interpreter) 763 script_interpreter->ResetOutputFileHandle (fh); 764 } 765 766 void 767 Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership) 768 { 769 File &err_file = GetErrorFile(); 770 err_file.SetStream (fh, tranfer_ownership); 771 if (err_file.IsValid() == false) 772 err_file.SetStream (stderr, false); 773 } 774 775 void 776 Debugger::SaveInputTerminalState () 777 { 778 File &in_file = GetInputFile(); 779 if (in_file.GetDescriptor() != File::kInvalidDescriptor) 780 m_terminal_state.Save(in_file.GetDescriptor(), true); 781 } 782 783 void 784 Debugger::RestoreInputTerminalState () 785 { 786 m_terminal_state.Restore(); 787 } 788 789 ExecutionContext 790 Debugger::GetSelectedExecutionContext () 791 { 792 ExecutionContext exe_ctx; 793 TargetSP target_sp(GetSelectedTarget()); 794 exe_ctx.SetTargetSP (target_sp); 795 796 if (target_sp) 797 { 798 ProcessSP process_sp (target_sp->GetProcessSP()); 799 exe_ctx.SetProcessSP (process_sp); 800 if (process_sp && process_sp->IsRunning() == false) 801 { 802 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread()); 803 if (thread_sp) 804 { 805 exe_ctx.SetThreadSP (thread_sp); 806 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame()); 807 if (exe_ctx.GetFramePtr() == NULL) 808 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0)); 809 } 810 } 811 } 812 return exe_ctx; 813 } 814 815 InputReaderSP 816 Debugger::GetCurrentInputReader () 817 { 818 InputReaderSP reader_sp; 819 820 if (!m_input_reader_stack.IsEmpty()) 821 { 822 // Clear any finished readers from the stack 823 while (CheckIfTopInputReaderIsDone()) ; 824 825 if (!m_input_reader_stack.IsEmpty()) 826 reader_sp = m_input_reader_stack.Top(); 827 } 828 829 return reader_sp; 830 } 831 832 void 833 Debugger::DispatchInputCallback (void *baton, const void *bytes, size_t bytes_len) 834 { 835 if (bytes_len > 0) 836 ((Debugger *)baton)->DispatchInput ((char *)bytes, bytes_len); 837 else 838 ((Debugger *)baton)->DispatchInputEndOfFile (); 839 } 840 841 842 void 843 Debugger::DispatchInput (const char *bytes, size_t bytes_len) 844 { 845 if (bytes == NULL || bytes_len == 0) 846 return; 847 848 WriteToDefaultReader (bytes, bytes_len); 849 } 850 851 void 852 Debugger::DispatchInputInterrupt () 853 { 854 m_input_reader_data.clear(); 855 856 InputReaderSP reader_sp (GetCurrentInputReader ()); 857 if (reader_sp) 858 { 859 reader_sp->Notify (eInputReaderInterrupt); 860 861 // If notifying the reader of the interrupt finished the reader, we should pop it off the stack. 862 while (CheckIfTopInputReaderIsDone ()) ; 863 } 864 } 865 866 void 867 Debugger::DispatchInputEndOfFile () 868 { 869 m_input_reader_data.clear(); 870 871 InputReaderSP reader_sp (GetCurrentInputReader ()); 872 if (reader_sp) 873 { 874 reader_sp->Notify (eInputReaderEndOfFile); 875 876 // If notifying the reader of the end-of-file finished the reader, we should pop it off the stack. 877 while (CheckIfTopInputReaderIsDone ()) ; 878 } 879 } 880 881 void 882 Debugger::CleanUpInputReaders () 883 { 884 m_input_reader_data.clear(); 885 886 // The bottom input reader should be the main debugger input reader. We do not want to close that one here. 887 while (m_input_reader_stack.GetSize() > 1) 888 { 889 InputReaderSP reader_sp (GetCurrentInputReader ()); 890 if (reader_sp) 891 { 892 reader_sp->Notify (eInputReaderEndOfFile); 893 reader_sp->SetIsDone (true); 894 } 895 } 896 } 897 898 void 899 Debugger::NotifyTopInputReader (InputReaderAction notification) 900 { 901 InputReaderSP reader_sp (GetCurrentInputReader()); 902 if (reader_sp) 903 { 904 reader_sp->Notify (notification); 905 906 // Flush out any input readers that are done. 907 while (CheckIfTopInputReaderIsDone ()) 908 /* Do nothing. */; 909 } 910 } 911 912 bool 913 Debugger::InputReaderIsTopReader (const InputReaderSP& reader_sp) 914 { 915 InputReaderSP top_reader_sp (GetCurrentInputReader()); 916 917 return (reader_sp.get() == top_reader_sp.get()); 918 } 919 920 921 void 922 Debugger::WriteToDefaultReader (const char *bytes, size_t bytes_len) 923 { 924 if (bytes && bytes_len) 925 m_input_reader_data.append (bytes, bytes_len); 926 927 if (m_input_reader_data.empty()) 928 return; 929 930 while (!m_input_reader_stack.IsEmpty() && !m_input_reader_data.empty()) 931 { 932 // Get the input reader from the top of the stack 933 InputReaderSP reader_sp (GetCurrentInputReader ()); 934 if (!reader_sp) 935 break; 936 937 size_t bytes_handled = reader_sp->HandleRawBytes (m_input_reader_data.c_str(), 938 m_input_reader_data.size()); 939 if (bytes_handled) 940 { 941 m_input_reader_data.erase (0, bytes_handled); 942 } 943 else 944 { 945 // No bytes were handled, we might not have reached our 946 // granularity, just return and wait for more data 947 break; 948 } 949 } 950 951 // Flush out any input readers that are done. 952 while (CheckIfTopInputReaderIsDone ()) 953 /* Do nothing. */; 954 955 } 956 957 void 958 Debugger::PushInputReader (const InputReaderSP& reader_sp) 959 { 960 if (!reader_sp) 961 return; 962 963 // Deactivate the old top reader 964 InputReaderSP top_reader_sp (GetCurrentInputReader ()); 965 966 if (top_reader_sp) 967 top_reader_sp->Notify (eInputReaderDeactivate); 968 969 m_input_reader_stack.Push (reader_sp); 970 reader_sp->Notify (eInputReaderActivate); 971 ActivateInputReader (reader_sp); 972 } 973 974 bool 975 Debugger::PopInputReader (const InputReaderSP& pop_reader_sp) 976 { 977 bool result = false; 978 979 // The reader on the stop of the stack is done, so let the next 980 // read on the stack referesh its prompt and if there is one... 981 if (!m_input_reader_stack.IsEmpty()) 982 { 983 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop. 984 InputReaderSP reader_sp(m_input_reader_stack.Top()); 985 986 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get()) 987 { 988 m_input_reader_stack.Pop (); 989 reader_sp->Notify (eInputReaderDeactivate); 990 reader_sp->Notify (eInputReaderDone); 991 result = true; 992 993 if (!m_input_reader_stack.IsEmpty()) 994 { 995 reader_sp = m_input_reader_stack.Top(); 996 if (reader_sp) 997 { 998 ActivateInputReader (reader_sp); 999 reader_sp->Notify (eInputReaderReactivate); 1000 } 1001 } 1002 } 1003 } 1004 return result; 1005 } 1006 1007 bool 1008 Debugger::CheckIfTopInputReaderIsDone () 1009 { 1010 bool result = false; 1011 if (!m_input_reader_stack.IsEmpty()) 1012 { 1013 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop. 1014 InputReaderSP reader_sp(m_input_reader_stack.Top()); 1015 1016 if (reader_sp && reader_sp->IsDone()) 1017 { 1018 result = true; 1019 PopInputReader (reader_sp); 1020 } 1021 } 1022 return result; 1023 } 1024 1025 void 1026 Debugger::ActivateInputReader (const InputReaderSP &reader_sp) 1027 { 1028 int input_fd = m_input_file.GetFile().GetDescriptor(); 1029 1030 if (input_fd >= 0) 1031 { 1032 Terminal tty(input_fd); 1033 1034 tty.SetEcho(reader_sp->GetEcho()); 1035 1036 switch (reader_sp->GetGranularity()) 1037 { 1038 case eInputReaderGranularityByte: 1039 case eInputReaderGranularityWord: 1040 tty.SetCanonical (false); 1041 break; 1042 1043 case eInputReaderGranularityLine: 1044 case eInputReaderGranularityAll: 1045 tty.SetCanonical (true); 1046 break; 1047 1048 default: 1049 break; 1050 } 1051 } 1052 } 1053 1054 StreamSP 1055 Debugger::GetAsyncOutputStream () 1056 { 1057 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(), 1058 CommandInterpreter::eBroadcastBitAsynchronousOutputData)); 1059 } 1060 1061 StreamSP 1062 Debugger::GetAsyncErrorStream () 1063 { 1064 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(), 1065 CommandInterpreter::eBroadcastBitAsynchronousErrorData)); 1066 } 1067 1068 size_t 1069 Debugger::GetNumDebuggers() 1070 { 1071 if (g_shared_debugger_refcount > 0) 1072 { 1073 Mutex::Locker locker (GetDebuggerListMutex ()); 1074 return GetDebuggerList().size(); 1075 } 1076 return 0; 1077 } 1078 1079 lldb::DebuggerSP 1080 Debugger::GetDebuggerAtIndex (size_t index) 1081 { 1082 DebuggerSP debugger_sp; 1083 1084 if (g_shared_debugger_refcount > 0) 1085 { 1086 Mutex::Locker locker (GetDebuggerListMutex ()); 1087 DebuggerList &debugger_list = GetDebuggerList(); 1088 1089 if (index < debugger_list.size()) 1090 debugger_sp = debugger_list[index]; 1091 } 1092 1093 return debugger_sp; 1094 } 1095 1096 DebuggerSP 1097 Debugger::FindDebuggerWithID (lldb::user_id_t id) 1098 { 1099 DebuggerSP debugger_sp; 1100 1101 if (g_shared_debugger_refcount > 0) 1102 { 1103 Mutex::Locker locker (GetDebuggerListMutex ()); 1104 DebuggerList &debugger_list = GetDebuggerList(); 1105 DebuggerList::iterator pos, end = debugger_list.end(); 1106 for (pos = debugger_list.begin(); pos != end; ++pos) 1107 { 1108 if ((*pos).get()->GetID() == id) 1109 { 1110 debugger_sp = *pos; 1111 break; 1112 } 1113 } 1114 } 1115 return debugger_sp; 1116 } 1117 1118 static void 1119 TestPromptFormats (StackFrame *frame) 1120 { 1121 if (frame == NULL) 1122 return; 1123 1124 StreamString s; 1125 const char *prompt_format = 1126 "{addr = '${addr}'\n}" 1127 "{process.id = '${process.id}'\n}" 1128 "{process.name = '${process.name}'\n}" 1129 "{process.file.basename = '${process.file.basename}'\n}" 1130 "{process.file.fullpath = '${process.file.fullpath}'\n}" 1131 "{thread.id = '${thread.id}'\n}" 1132 "{thread.index = '${thread.index}'\n}" 1133 "{thread.name = '${thread.name}'\n}" 1134 "{thread.queue = '${thread.queue}'\n}" 1135 "{thread.stop-reason = '${thread.stop-reason}'\n}" 1136 "{target.arch = '${target.arch}'\n}" 1137 "{module.file.basename = '${module.file.basename}'\n}" 1138 "{module.file.fullpath = '${module.file.fullpath}'\n}" 1139 "{file.basename = '${file.basename}'\n}" 1140 "{file.fullpath = '${file.fullpath}'\n}" 1141 "{frame.index = '${frame.index}'\n}" 1142 "{frame.pc = '${frame.pc}'\n}" 1143 "{frame.sp = '${frame.sp}'\n}" 1144 "{frame.fp = '${frame.fp}'\n}" 1145 "{frame.flags = '${frame.flags}'\n}" 1146 "{frame.reg.rdi = '${frame.reg.rdi}'\n}" 1147 "{frame.reg.rip = '${frame.reg.rip}'\n}" 1148 "{frame.reg.rsp = '${frame.reg.rsp}'\n}" 1149 "{frame.reg.rbp = '${frame.reg.rbp}'\n}" 1150 "{frame.reg.rflags = '${frame.reg.rflags}'\n}" 1151 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}" 1152 "{frame.reg.carp = '${frame.reg.carp}'\n}" 1153 "{function.id = '${function.id}'\n}" 1154 "{function.name = '${function.name}'\n}" 1155 "{function.name-with-args = '${function.name-with-args}'\n}" 1156 "{function.addr-offset = '${function.addr-offset}'\n}" 1157 "{function.line-offset = '${function.line-offset}'\n}" 1158 "{function.pc-offset = '${function.pc-offset}'\n}" 1159 "{line.file.basename = '${line.file.basename}'\n}" 1160 "{line.file.fullpath = '${line.file.fullpath}'\n}" 1161 "{line.number = '${line.number}'\n}" 1162 "{line.start-addr = '${line.start-addr}'\n}" 1163 "{line.end-addr = '${line.end-addr}'\n}" 1164 ; 1165 1166 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything)); 1167 ExecutionContext exe_ctx; 1168 frame->CalculateExecutionContext(exe_ctx); 1169 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s)) 1170 { 1171 printf("%s\n", s.GetData()); 1172 } 1173 else 1174 { 1175 printf ("what we got: %s\n", s.GetData()); 1176 } 1177 } 1178 1179 static bool 1180 ScanFormatDescriptor (const char* var_name_begin, 1181 const char* var_name_end, 1182 const char** var_name_final, 1183 const char** percent_position, 1184 Format* custom_format, 1185 ValueObject::ValueObjectRepresentationStyle* val_obj_display) 1186 { 1187 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES)); 1188 *percent_position = ::strchr(var_name_begin,'%'); 1189 if (!*percent_position || *percent_position > var_name_end) 1190 { 1191 if (log) 1192 log->Printf("[ScanFormatDescriptor] no format descriptor in string, skipping"); 1193 *var_name_final = var_name_end; 1194 } 1195 else 1196 { 1197 *var_name_final = *percent_position; 1198 std::string format_name(*var_name_final+1, var_name_end-*var_name_final-1); 1199 if (log) 1200 log->Printf("[ScanFormatDescriptor] parsing %s as a format descriptor", format_name.c_str()); 1201 if ( !FormatManager::GetFormatFromCString(format_name.c_str(), 1202 true, 1203 *custom_format) ) 1204 { 1205 if (log) 1206 log->Printf("[ScanFormatDescriptor] %s is an unknown format", format_name.c_str()); 1207 1208 switch (format_name.front()) 1209 { 1210 case '@': // if this is an @ sign, print ObjC description 1211 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLanguageSpecific; 1212 break; 1213 case 'V': // if this is a V, print the value using the default format 1214 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue; 1215 break; 1216 case 'L': // if this is an L, print the location of the value 1217 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLocation; 1218 break; 1219 case 'S': // if this is an S, print the summary after all 1220 *val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary; 1221 break; 1222 case '#': // if this is a '#', print the number of children 1223 *val_obj_display = ValueObject::eValueObjectRepresentationStyleChildrenCount; 1224 break; 1225 case 'T': // if this is a 'T', print the type 1226 *val_obj_display = ValueObject::eValueObjectRepresentationStyleType; 1227 break; 1228 case 'N': // if this is a 'N', print the name 1229 *val_obj_display = ValueObject::eValueObjectRepresentationStyleName; 1230 break; 1231 case '>': // if this is a '>', print the name 1232 *val_obj_display = ValueObject::eValueObjectRepresentationStyleExpressionPath; 1233 break; 1234 default: 1235 if (log) 1236 log->Printf("ScanFormatDescriptor] %s is an error, leaving the previous value alone", format_name.c_str()); 1237 break; 1238 } 1239 } 1240 // a good custom format tells us to print the value using it 1241 else 1242 { 1243 if (log) 1244 log->Printf("[ScanFormatDescriptor] will display value for this VO"); 1245 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue; 1246 } 1247 } 1248 if (log) 1249 log->Printf("[ScanFormatDescriptor] final format description outcome: custom_format = %d, val_obj_display = %d", 1250 *custom_format, 1251 *val_obj_display); 1252 return true; 1253 } 1254 1255 static bool 1256 ScanBracketedRange (const char* var_name_begin, 1257 const char* var_name_end, 1258 const char* var_name_final, 1259 const char** open_bracket_position, 1260 const char** separator_position, 1261 const char** close_bracket_position, 1262 const char** var_name_final_if_array_range, 1263 int64_t* index_lower, 1264 int64_t* index_higher) 1265 { 1266 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES)); 1267 *open_bracket_position = ::strchr(var_name_begin,'['); 1268 if (*open_bracket_position && *open_bracket_position < var_name_final) 1269 { 1270 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield 1271 *close_bracket_position = ::strchr(*open_bracket_position,']'); 1272 // as usual, we assume that [] will come before % 1273 //printf("trying to expand a []\n"); 1274 *var_name_final_if_array_range = *open_bracket_position; 1275 if (*close_bracket_position - *open_bracket_position == 1) 1276 { 1277 if (log) 1278 log->Printf("[ScanBracketedRange] '[]' detected.. going from 0 to end of data"); 1279 *index_lower = 0; 1280 } 1281 else if (*separator_position == NULL || *separator_position > var_name_end) 1282 { 1283 char *end = NULL; 1284 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0); 1285 *index_higher = *index_lower; 1286 if (log) 1287 log->Printf("[ScanBracketedRange] [%" PRId64 "] detected, high index is same", *index_lower); 1288 } 1289 else if (*close_bracket_position && *close_bracket_position < var_name_end) 1290 { 1291 char *end = NULL; 1292 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0); 1293 *index_higher = ::strtoul (*separator_position+1, &end, 0); 1294 if (log) 1295 log->Printf("[ScanBracketedRange] [%" PRId64 "-%" PRId64 "] detected", *index_lower, *index_higher); 1296 } 1297 else 1298 { 1299 if (log) 1300 log->Printf("[ScanBracketedRange] expression is erroneous, cannot extract indices out of it"); 1301 return false; 1302 } 1303 if (*index_lower > *index_higher && *index_higher > 0) 1304 { 1305 if (log) 1306 log->Printf("[ScanBracketedRange] swapping indices"); 1307 int64_t temp = *index_lower; 1308 *index_lower = *index_higher; 1309 *index_higher = temp; 1310 } 1311 } 1312 else if (log) 1313 log->Printf("[ScanBracketedRange] no bracketed range, skipping entirely"); 1314 return true; 1315 } 1316 1317 template <typename T> 1318 static bool RunScriptFormatKeyword(Stream &s, ScriptInterpreter *script_interpreter, T t, const std::string& script_name) 1319 { 1320 if (script_interpreter) 1321 { 1322 Error script_error; 1323 std::string script_output; 1324 1325 if (script_interpreter->RunScriptFormatKeyword(script_name.c_str(), t, script_output, script_error) && script_error.Success()) 1326 { 1327 s.Printf("%s", script_output.c_str()); 1328 return true; 1329 } 1330 else 1331 { 1332 s.Printf("<error: %s>",script_error.AsCString()); 1333 } 1334 } 1335 return false; 1336 } 1337 1338 static ValueObjectSP 1339 ExpandIndexedExpression (ValueObject* valobj, 1340 size_t index, 1341 StackFrame* frame, 1342 bool deref_pointer) 1343 { 1344 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES)); 1345 const char* ptr_deref_format = "[%d]"; 1346 std::string ptr_deref_buffer(10,0); 1347 ::sprintf(&ptr_deref_buffer[0], ptr_deref_format, index); 1348 if (log) 1349 log->Printf("[ExpandIndexedExpression] name to deref: %s",ptr_deref_buffer.c_str()); 1350 const char* first_unparsed; 1351 ValueObject::GetValueForExpressionPathOptions options; 1352 ValueObject::ExpressionPathEndResultType final_value_type; 1353 ValueObject::ExpressionPathScanEndReason reason_to_stop; 1354 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing); 1355 ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.c_str(), 1356 &first_unparsed, 1357 &reason_to_stop, 1358 &final_value_type, 1359 options, 1360 &what_next); 1361 if (!item) 1362 { 1363 if (log) 1364 log->Printf("[ExpandIndexedExpression] ERROR: unparsed portion = %s, why stopping = %d," 1365 " final_value_type %d", 1366 first_unparsed, reason_to_stop, final_value_type); 1367 } 1368 else 1369 { 1370 if (log) 1371 log->Printf("[ExpandIndexedExpression] ALL RIGHT: unparsed portion = %s, why stopping = %d," 1372 " final_value_type %d", 1373 first_unparsed, reason_to_stop, final_value_type); 1374 } 1375 return item; 1376 } 1377 1378 static inline bool 1379 IsToken(const char *var_name_begin, const char *var) 1380 { 1381 return (::strncmp (var_name_begin, var, strlen(var)) == 0); 1382 } 1383 1384 static bool 1385 IsTokenWithFormat(const char *var_name_begin, const char *var, std::string &format, const char *default_format, 1386 const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr) 1387 { 1388 int var_len = strlen(var); 1389 if (::strncmp (var_name_begin, var, var_len) == 0) 1390 { 1391 var_name_begin += var_len; 1392 if (*var_name_begin == '}') 1393 { 1394 format = default_format; 1395 return true; 1396 } 1397 else if (*var_name_begin == '%') 1398 { 1399 // Allow format specifiers: x|X|u with optional width specifiers. 1400 // ${thread.id%x} ; hex 1401 // ${thread.id%X} ; uppercase hex 1402 // ${thread.id%u} ; unsigned decimal 1403 // ${thread.id%8.8X} ; width.precision + specifier 1404 // ${thread.id%tid} ; unsigned on FreeBSD/Linux, otherwise default_format (0x%4.4x for thread.id) 1405 int dot_count = 0; 1406 const char *specifier = NULL; 1407 int width_precision_length = 0; 1408 const char *width_precision = ++var_name_begin; 1409 while (isdigit(*var_name_begin) || *var_name_begin == '.') 1410 { 1411 dot_count += (*var_name_begin == '.'); 1412 if (dot_count > 1) 1413 break; 1414 var_name_begin++; 1415 width_precision_length++; 1416 } 1417 1418 if (IsToken (var_name_begin, "tid}")) 1419 { 1420 Target *target = Target::GetTargetFromContexts (exe_ctx_ptr, sc_ptr); 1421 if (target) 1422 { 1423 ArchSpec arch (target->GetArchitecture ()); 1424 llvm::Triple::OSType ostype = arch.IsValid() ? arch.GetTriple().getOS() : llvm::Triple::UnknownOS; 1425 if ((ostype == llvm::Triple::FreeBSD) || (ostype == llvm::Triple::Linux)) 1426 specifier = PRIu64; 1427 } 1428 if (!specifier) 1429 { 1430 format = default_format; 1431 return true; 1432 } 1433 } 1434 else if (IsToken (var_name_begin, "x}")) 1435 specifier = PRIx64; 1436 else if (IsToken (var_name_begin, "X}")) 1437 specifier = PRIX64; 1438 else if (IsToken (var_name_begin, "u}")) 1439 specifier = PRIu64; 1440 1441 if (specifier) 1442 { 1443 format = "%"; 1444 if (width_precision_length) 1445 format += std::string(width_precision, width_precision_length); 1446 format += specifier; 1447 return true; 1448 } 1449 } 1450 } 1451 return false; 1452 } 1453 1454 static bool 1455 FormatPromptRecurse 1456 ( 1457 const char *format, 1458 const SymbolContext *sc, 1459 const ExecutionContext *exe_ctx, 1460 const Address *addr, 1461 Stream &s, 1462 const char **end, 1463 ValueObject* valobj 1464 ) 1465 { 1466 ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers 1467 bool success = true; 1468 const char *p; 1469 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES)); 1470 1471 for (p = format; *p != '\0'; ++p) 1472 { 1473 if (realvalobj) 1474 { 1475 valobj = realvalobj; 1476 realvalobj = NULL; 1477 } 1478 size_t non_special_chars = ::strcspn (p, "${}\\"); 1479 if (non_special_chars > 0) 1480 { 1481 if (success) 1482 s.Write (p, non_special_chars); 1483 p += non_special_chars; 1484 } 1485 1486 if (*p == '\0') 1487 { 1488 break; 1489 } 1490 else if (*p == '{') 1491 { 1492 // Start a new scope that must have everything it needs if it is to 1493 // to make it into the final output stream "s". If you want to make 1494 // a format that only prints out the function or symbol name if there 1495 // is one in the symbol context you can use: 1496 // "{function =${function.name}}" 1497 // The first '{' starts a new scope that end with the matching '}' at 1498 // the end of the string. The contents "function =${function.name}" 1499 // will then be evaluated and only be output if there is a function 1500 // or symbol with a valid name. 1501 StreamString sub_strm; 1502 1503 ++p; // Skip the '{' 1504 1505 if (FormatPromptRecurse (p, sc, exe_ctx, addr, sub_strm, &p, valobj)) 1506 { 1507 // The stream had all it needed 1508 s.Write(sub_strm.GetData(), sub_strm.GetSize()); 1509 } 1510 if (*p != '}') 1511 { 1512 success = false; 1513 break; 1514 } 1515 } 1516 else if (*p == '}') 1517 { 1518 // End of a enclosing scope 1519 break; 1520 } 1521 else if (*p == '$') 1522 { 1523 // We have a prompt variable to print 1524 ++p; 1525 if (*p == '{') 1526 { 1527 ++p; 1528 const char *var_name_begin = p; 1529 const char *var_name_end = ::strchr (p, '}'); 1530 1531 if (var_name_end && var_name_begin < var_name_end) 1532 { 1533 // if we have already failed to parse, skip this variable 1534 if (success) 1535 { 1536 const char *cstr = NULL; 1537 std::string token_format; 1538 Address format_addr; 1539 bool calculate_format_addr_function_offset = false; 1540 // Set reg_kind and reg_num to invalid values 1541 RegisterKind reg_kind = kNumRegisterKinds; 1542 uint32_t reg_num = LLDB_INVALID_REGNUM; 1543 FileSpec format_file_spec; 1544 const RegisterInfo *reg_info = NULL; 1545 RegisterContext *reg_ctx = NULL; 1546 bool do_deref_pointer = false; 1547 ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString; 1548 ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain; 1549 1550 // Each variable must set success to true below... 1551 bool var_success = false; 1552 switch (var_name_begin[0]) 1553 { 1554 case '*': 1555 case 'v': 1556 case 's': 1557 { 1558 if (!valobj) 1559 break; 1560 1561 if (log) 1562 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin); 1563 1564 // check for *var and *svar 1565 if (*var_name_begin == '*') 1566 { 1567 do_deref_pointer = true; 1568 var_name_begin++; 1569 if (log) 1570 log->Printf("[Debugger::FormatPrompt] found a deref, new string is: %s",var_name_begin); 1571 } 1572 1573 if (*var_name_begin == 's') 1574 { 1575 if (!valobj->IsSynthetic()) 1576 valobj = valobj->GetSyntheticValue().get(); 1577 if (!valobj) 1578 break; 1579 var_name_begin++; 1580 if (log) 1581 log->Printf("[Debugger::FormatPrompt] found a synthetic, new string is: %s",var_name_begin); 1582 } 1583 1584 // should be a 'v' by now 1585 if (*var_name_begin != 'v') 1586 break; 1587 1588 if (log) 1589 log->Printf("[Debugger::FormatPrompt] string I am working with: %s",var_name_begin); 1590 1591 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ? 1592 ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing); 1593 ValueObject::GetValueForExpressionPathOptions options; 1594 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren(); 1595 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary; 1596 ValueObject* target = NULL; 1597 Format custom_format = eFormatInvalid; 1598 const char* var_name_final = NULL; 1599 const char* var_name_final_if_array_range = NULL; 1600 const char* close_bracket_position = NULL; 1601 int64_t index_lower = -1; 1602 int64_t index_higher = -1; 1603 bool is_array_range = false; 1604 const char* first_unparsed; 1605 bool was_plain_var = false; 1606 bool was_var_format = false; 1607 bool was_var_indexed = false; 1608 1609 if (!valobj) break; 1610 // simplest case ${var}, just print valobj's value 1611 if (IsToken (var_name_begin, "var}")) 1612 { 1613 was_plain_var = true; 1614 target = valobj; 1615 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue; 1616 } 1617 else if (IsToken (var_name_begin,"var%")) 1618 { 1619 was_var_format = true; 1620 // this is a variable with some custom format applied to it 1621 const char* percent_position; 1622 target = valobj; 1623 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue; 1624 ScanFormatDescriptor (var_name_begin, 1625 var_name_end, 1626 &var_name_final, 1627 &percent_position, 1628 &custom_format, 1629 &val_obj_display); 1630 } 1631 // this is ${var.something} or multiple .something nested 1632 else if (IsToken (var_name_begin, "var")) 1633 { 1634 if (IsToken (var_name_begin, "var[")) 1635 was_var_indexed = true; 1636 const char* percent_position; 1637 ScanFormatDescriptor (var_name_begin, 1638 var_name_end, 1639 &var_name_final, 1640 &percent_position, 1641 &custom_format, 1642 &val_obj_display); 1643 1644 const char* open_bracket_position; 1645 const char* separator_position; 1646 ScanBracketedRange (var_name_begin, 1647 var_name_end, 1648 var_name_final, 1649 &open_bracket_position, 1650 &separator_position, 1651 &close_bracket_position, 1652 &var_name_final_if_array_range, 1653 &index_lower, 1654 &index_higher); 1655 1656 Error error; 1657 1658 std::string expr_path(var_name_final-var_name_begin-1,0); 1659 memcpy(&expr_path[0], var_name_begin+3,var_name_final-var_name_begin-3); 1660 1661 if (log) 1662 log->Printf("[Debugger::FormatPrompt] symbol to expand: %s",expr_path.c_str()); 1663 1664 target = valobj->GetValueForExpressionPath(expr_path.c_str(), 1665 &first_unparsed, 1666 &reason_to_stop, 1667 &final_value_type, 1668 options, 1669 &what_next).get(); 1670 1671 if (!target) 1672 { 1673 if (log) 1674 log->Printf("[Debugger::FormatPrompt] ERROR: unparsed portion = %s, why stopping = %d," 1675 " final_value_type %d", 1676 first_unparsed, reason_to_stop, final_value_type); 1677 break; 1678 } 1679 else 1680 { 1681 if (log) 1682 log->Printf("[Debugger::FormatPrompt] ALL RIGHT: unparsed portion = %s, why stopping = %d," 1683 " final_value_type %d", 1684 first_unparsed, reason_to_stop, final_value_type); 1685 } 1686 } 1687 else 1688 break; 1689 1690 is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange || 1691 final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange); 1692 1693 do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference); 1694 1695 if (do_deref_pointer && !is_array_range) 1696 { 1697 // I have not deref-ed yet, let's do it 1698 // this happens when we are not going through GetValueForVariableExpressionPath 1699 // to get to the target ValueObject 1700 Error error; 1701 target = target->Dereference(error).get(); 1702 if (error.Fail()) 1703 { 1704 if (log) 1705 log->Printf("[Debugger::FormatPrompt] ERROR: %s\n", error.AsCString("unknown")); \ 1706 break; 1707 } 1708 do_deref_pointer = false; 1709 } 1710 1711 // we do not want to use the summary for a bitfield of type T:n 1712 // if we were originally dealing with just a T - that would get 1713 // us into an endless recursion 1714 if (target->IsBitfield() && was_var_indexed) 1715 { 1716 // TODO: check for a (T:n)-specific summary - we should still obey that 1717 StreamString bitfield_name; 1718 bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), target->GetBitfieldBitSize()); 1719 lldb::TypeNameSpecifierImplSP type_sp(new TypeNameSpecifierImpl(bitfield_name.GetData(),false)); 1720 if (!DataVisualization::GetSummaryForType(type_sp)) 1721 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue; 1722 } 1723 1724 // TODO use flags for these 1725 const uint32_t type_info_flags = target->GetClangType().GetTypeInfo(NULL); 1726 bool is_array = (type_info_flags & ClangASTType::eTypeIsArray) != 0; 1727 bool is_pointer = (type_info_flags & ClangASTType::eTypeIsPointer) != 0; 1728 bool is_aggregate = target->GetClangType().IsAggregateType(); 1729 1730 if ((is_array || is_pointer) && (!is_array_range) && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue) // this should be wrong, but there are some exceptions 1731 { 1732 StreamString str_temp; 1733 if (log) 1734 log->Printf("[Debugger::FormatPrompt] I am into array || pointer && !range"); 1735 1736 if (target->HasSpecialPrintableRepresentation(val_obj_display, custom_format)) 1737 { 1738 // try to use the special cases 1739 var_success = target->DumpPrintableRepresentation(str_temp, 1740 val_obj_display, 1741 custom_format); 1742 if (log) 1743 log->Printf("[Debugger::FormatPrompt] special cases did%s match", var_success ? "" : "n't"); 1744 1745 // should not happen 1746 if (var_success) 1747 s << str_temp.GetData(); 1748 var_success = true; 1749 break; 1750 } 1751 else 1752 { 1753 if (was_plain_var) // if ${var} 1754 { 1755 s << target->GetTypeName() << " @ " << target->GetLocationAsCString(); 1756 } 1757 else if (is_pointer) // if pointer, value is the address stored 1758 { 1759 target->DumpPrintableRepresentation (s, 1760 val_obj_display, 1761 custom_format, 1762 ValueObject::ePrintableRepresentationSpecialCasesDisable); 1763 } 1764 var_success = true; 1765 break; 1766 } 1767 } 1768 1769 // if directly trying to print ${var}, and this is an aggregate, display a nice 1770 // type @ location message 1771 if (is_aggregate && was_plain_var) 1772 { 1773 s << target->GetTypeName() << " @ " << target->GetLocationAsCString(); 1774 var_success = true; 1775 break; 1776 } 1777 1778 // if directly trying to print ${var%V}, and this is an aggregate, do not let the user do it 1779 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue))) 1780 { 1781 s << "<invalid use of aggregate type>"; 1782 var_success = true; 1783 break; 1784 } 1785 1786 if (!is_array_range) 1787 { 1788 if (log) 1789 log->Printf("[Debugger::FormatPrompt] dumping ordinary printable output"); 1790 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format); 1791 } 1792 else 1793 { 1794 if (log) 1795 log->Printf("[Debugger::FormatPrompt] checking if I can handle as array"); 1796 if (!is_array && !is_pointer) 1797 break; 1798 if (log) 1799 log->Printf("[Debugger::FormatPrompt] handle as array"); 1800 const char* special_directions = NULL; 1801 StreamString special_directions_writer; 1802 if (close_bracket_position && (var_name_end-close_bracket_position > 1)) 1803 { 1804 ConstString additional_data; 1805 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1); 1806 special_directions_writer.Printf("${%svar%s}", 1807 do_deref_pointer ? "*" : "", 1808 additional_data.GetCString()); 1809 special_directions = special_directions_writer.GetData(); 1810 } 1811 1812 // let us display items index_lower thru index_higher of this array 1813 s.PutChar('['); 1814 var_success = true; 1815 1816 if (index_higher < 0) 1817 index_higher = valobj->GetNumChildren() - 1; 1818 1819 uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay(); 1820 1821 for (;index_lower<=index_higher;index_lower++) 1822 { 1823 ValueObject* item = ExpandIndexedExpression (target, 1824 index_lower, 1825 exe_ctx->GetFramePtr(), 1826 false).get(); 1827 1828 if (!item) 1829 { 1830 if (log) 1831 log->Printf("[Debugger::FormatPrompt] ERROR in getting child item at index %" PRId64, index_lower); 1832 } 1833 else 1834 { 1835 if (log) 1836 log->Printf("[Debugger::FormatPrompt] special_directions for child item: %s",special_directions); 1837 } 1838 1839 if (!special_directions) 1840 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format); 1841 else 1842 var_success &= FormatPromptRecurse(special_directions, sc, exe_ctx, addr, s, NULL, item); 1843 1844 if (--max_num_children == 0) 1845 { 1846 s.PutCString(", ..."); 1847 break; 1848 } 1849 1850 if (index_lower < index_higher) 1851 s.PutChar(','); 1852 } 1853 s.PutChar(']'); 1854 } 1855 } 1856 break; 1857 case 'a': 1858 if (IsToken (var_name_begin, "addr}")) 1859 { 1860 if (addr && addr->IsValid()) 1861 { 1862 var_success = true; 1863 format_addr = *addr; 1864 } 1865 } 1866 break; 1867 1868 case 'p': 1869 if (IsToken (var_name_begin, "process.")) 1870 { 1871 if (exe_ctx) 1872 { 1873 Process *process = exe_ctx->GetProcessPtr(); 1874 if (process) 1875 { 1876 var_name_begin += ::strlen ("process."); 1877 if (IsTokenWithFormat (var_name_begin, "id", token_format, "%" PRIu64, exe_ctx, sc)) 1878 { 1879 s.Printf(token_format.c_str(), process->GetID()); 1880 var_success = true; 1881 } 1882 else if ((IsToken (var_name_begin, "name}")) || 1883 (IsToken (var_name_begin, "file.basename}")) || 1884 (IsToken (var_name_begin, "file.fullpath}"))) 1885 { 1886 Module *exe_module = process->GetTarget().GetExecutableModulePointer(); 1887 if (exe_module) 1888 { 1889 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f') 1890 { 1891 format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename(); 1892 var_success = (bool)format_file_spec; 1893 } 1894 else 1895 { 1896 format_file_spec = exe_module->GetFileSpec(); 1897 var_success = (bool)format_file_spec; 1898 } 1899 } 1900 } 1901 else if (IsToken (var_name_begin, "script:")) 1902 { 1903 var_name_begin += ::strlen("script:"); 1904 std::string script_name(var_name_begin,var_name_end); 1905 ScriptInterpreter* script_interpreter = process->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter(); 1906 if (RunScriptFormatKeyword (s, script_interpreter, process, script_name)) 1907 var_success = true; 1908 } 1909 } 1910 } 1911 } 1912 break; 1913 1914 case 't': 1915 if (IsToken (var_name_begin, "thread.")) 1916 { 1917 if (exe_ctx) 1918 { 1919 Thread *thread = exe_ctx->GetThreadPtr(); 1920 if (thread) 1921 { 1922 var_name_begin += ::strlen ("thread."); 1923 if (IsTokenWithFormat (var_name_begin, "id", token_format, "0x%4.4" PRIx64, exe_ctx, sc)) 1924 { 1925 s.Printf(token_format.c_str(), thread->GetID()); 1926 var_success = true; 1927 } 1928 else if (IsTokenWithFormat (var_name_begin, "protocol_id", token_format, "0x%4.4" PRIx64, exe_ctx, sc)) 1929 { 1930 s.Printf(token_format.c_str(), thread->GetProtocolID()); 1931 var_success = true; 1932 } 1933 else if (IsTokenWithFormat (var_name_begin, "index", token_format, "%" PRIu64, exe_ctx, sc)) 1934 { 1935 s.Printf(token_format.c_str(), (uint64_t)thread->GetIndexID()); 1936 var_success = true; 1937 } 1938 else if (IsToken (var_name_begin, "name}")) 1939 { 1940 cstr = thread->GetName(); 1941 var_success = cstr && cstr[0]; 1942 if (var_success) 1943 s.PutCString(cstr); 1944 } 1945 else if (IsToken (var_name_begin, "queue}")) 1946 { 1947 cstr = thread->GetQueueName(); 1948 var_success = cstr && cstr[0]; 1949 if (var_success) 1950 s.PutCString(cstr); 1951 } 1952 else if (IsToken (var_name_begin, "stop-reason}")) 1953 { 1954 StopInfoSP stop_info_sp = thread->GetStopInfo (); 1955 if (stop_info_sp && stop_info_sp->IsValid()) 1956 { 1957 cstr = stop_info_sp->GetDescription(); 1958 if (cstr && cstr[0]) 1959 { 1960 s.PutCString(cstr); 1961 var_success = true; 1962 } 1963 } 1964 } 1965 else if (IsToken (var_name_begin, "return-value}")) 1966 { 1967 StopInfoSP stop_info_sp = thread->GetStopInfo (); 1968 if (stop_info_sp && stop_info_sp->IsValid()) 1969 { 1970 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp); 1971 if (return_valobj_sp) 1972 { 1973 return_valobj_sp->Dump(s); 1974 var_success = true; 1975 } 1976 } 1977 } 1978 else if (IsToken (var_name_begin, "script:")) 1979 { 1980 var_name_begin += ::strlen("script:"); 1981 std::string script_name(var_name_begin,var_name_end); 1982 ScriptInterpreter* script_interpreter = thread->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter(); 1983 if (RunScriptFormatKeyword (s, script_interpreter, thread, script_name)) 1984 var_success = true; 1985 } 1986 } 1987 } 1988 } 1989 else if (IsToken (var_name_begin, "target.")) 1990 { 1991 // TODO: hookup properties 1992 // if (!target_properties_sp) 1993 // { 1994 // Target *target = Target::GetTargetFromContexts (exe_ctx, sc); 1995 // if (target) 1996 // target_properties_sp = target->GetProperties(); 1997 // } 1998 // 1999 // if (target_properties_sp) 2000 // { 2001 // var_name_begin += ::strlen ("target."); 2002 // const char *end_property = strchr(var_name_begin, '}'); 2003 // if (end_property) 2004 // { 2005 // ConstString property_name(var_name_begin, end_property - var_name_begin); 2006 // std::string property_value (target_properties_sp->GetPropertyValue(property_name)); 2007 // if (!property_value.empty()) 2008 // { 2009 // s.PutCString (property_value.c_str()); 2010 // var_success = true; 2011 // } 2012 // } 2013 // } 2014 Target *target = Target::GetTargetFromContexts (exe_ctx, sc); 2015 if (target) 2016 { 2017 var_name_begin += ::strlen ("target."); 2018 if (IsToken (var_name_begin, "arch}")) 2019 { 2020 ArchSpec arch (target->GetArchitecture ()); 2021 if (arch.IsValid()) 2022 { 2023 s.PutCString (arch.GetArchitectureName()); 2024 var_success = true; 2025 } 2026 } 2027 else if (IsToken (var_name_begin, "script:")) 2028 { 2029 var_name_begin += ::strlen("script:"); 2030 std::string script_name(var_name_begin,var_name_end); 2031 ScriptInterpreter* script_interpreter = target->GetDebugger().GetCommandInterpreter().GetScriptInterpreter(); 2032 if (RunScriptFormatKeyword (s, script_interpreter, target, script_name)) 2033 var_success = true; 2034 } 2035 } 2036 } 2037 break; 2038 2039 2040 case 'm': 2041 if (IsToken (var_name_begin, "module.")) 2042 { 2043 if (sc && sc->module_sp.get()) 2044 { 2045 Module *module = sc->module_sp.get(); 2046 var_name_begin += ::strlen ("module."); 2047 2048 if (IsToken (var_name_begin, "file.")) 2049 { 2050 if (module->GetFileSpec()) 2051 { 2052 var_name_begin += ::strlen ("file."); 2053 2054 if (IsToken (var_name_begin, "basename}")) 2055 { 2056 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename(); 2057 var_success = (bool)format_file_spec; 2058 } 2059 else if (IsToken (var_name_begin, "fullpath}")) 2060 { 2061 format_file_spec = module->GetFileSpec(); 2062 var_success = (bool)format_file_spec; 2063 } 2064 } 2065 } 2066 } 2067 } 2068 break; 2069 2070 2071 case 'f': 2072 if (IsToken (var_name_begin, "file.")) 2073 { 2074 if (sc && sc->comp_unit != NULL) 2075 { 2076 var_name_begin += ::strlen ("file."); 2077 2078 if (IsToken (var_name_begin, "basename}")) 2079 { 2080 format_file_spec.GetFilename() = sc->comp_unit->GetFilename(); 2081 var_success = (bool)format_file_spec; 2082 } 2083 else if (IsToken (var_name_begin, "fullpath}")) 2084 { 2085 format_file_spec = *sc->comp_unit; 2086 var_success = (bool)format_file_spec; 2087 } 2088 } 2089 } 2090 else if (IsToken (var_name_begin, "frame.")) 2091 { 2092 if (exe_ctx) 2093 { 2094 StackFrame *frame = exe_ctx->GetFramePtr(); 2095 if (frame) 2096 { 2097 var_name_begin += ::strlen ("frame."); 2098 if (IsToken (var_name_begin, "index}")) 2099 { 2100 s.Printf("%u", frame->GetFrameIndex()); 2101 var_success = true; 2102 } 2103 else if (IsToken (var_name_begin, "pc}")) 2104 { 2105 reg_kind = eRegisterKindGeneric; 2106 reg_num = LLDB_REGNUM_GENERIC_PC; 2107 var_success = true; 2108 } 2109 else if (IsToken (var_name_begin, "sp}")) 2110 { 2111 reg_kind = eRegisterKindGeneric; 2112 reg_num = LLDB_REGNUM_GENERIC_SP; 2113 var_success = true; 2114 } 2115 else if (IsToken (var_name_begin, "fp}")) 2116 { 2117 reg_kind = eRegisterKindGeneric; 2118 reg_num = LLDB_REGNUM_GENERIC_FP; 2119 var_success = true; 2120 } 2121 else if (IsToken (var_name_begin, "flags}")) 2122 { 2123 reg_kind = eRegisterKindGeneric; 2124 reg_num = LLDB_REGNUM_GENERIC_FLAGS; 2125 var_success = true; 2126 } 2127 else if (IsToken (var_name_begin, "reg.")) 2128 { 2129 reg_ctx = frame->GetRegisterContext().get(); 2130 if (reg_ctx) 2131 { 2132 var_name_begin += ::strlen ("reg."); 2133 if (var_name_begin < var_name_end) 2134 { 2135 std::string reg_name (var_name_begin, var_name_end); 2136 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str()); 2137 if (reg_info) 2138 var_success = true; 2139 } 2140 } 2141 } 2142 else if (IsToken (var_name_begin, "script:")) 2143 { 2144 var_name_begin += ::strlen("script:"); 2145 std::string script_name(var_name_begin,var_name_end); 2146 ScriptInterpreter* script_interpreter = frame->GetThread()->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter(); 2147 if (RunScriptFormatKeyword (s, script_interpreter, frame, script_name)) 2148 var_success = true; 2149 } 2150 } 2151 } 2152 } 2153 else if (IsToken (var_name_begin, "function.")) 2154 { 2155 if (sc && (sc->function != NULL || sc->symbol != NULL)) 2156 { 2157 var_name_begin += ::strlen ("function."); 2158 if (IsToken (var_name_begin, "id}")) 2159 { 2160 if (sc->function) 2161 s.Printf("function{0x%8.8" PRIx64 "}", sc->function->GetID()); 2162 else 2163 s.Printf("symbol[%u]", sc->symbol->GetID()); 2164 2165 var_success = true; 2166 } 2167 else if (IsToken (var_name_begin, "name}")) 2168 { 2169 if (sc->function) 2170 cstr = sc->function->GetName().AsCString (NULL); 2171 else if (sc->symbol) 2172 cstr = sc->symbol->GetName().AsCString (NULL); 2173 if (cstr) 2174 { 2175 s.PutCString(cstr); 2176 2177 if (sc->block) 2178 { 2179 Block *inline_block = sc->block->GetContainingInlinedBlock (); 2180 if (inline_block) 2181 { 2182 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo(); 2183 if (inline_info) 2184 { 2185 s.PutCString(" [inlined] "); 2186 inline_info->GetName().Dump(&s); 2187 } 2188 } 2189 } 2190 var_success = true; 2191 } 2192 } 2193 else if (IsToken (var_name_begin, "name-with-args}")) 2194 { 2195 // Print the function name with arguments in it 2196 2197 if (sc->function) 2198 { 2199 var_success = true; 2200 ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL; 2201 cstr = sc->function->GetName().AsCString (NULL); 2202 if (cstr) 2203 { 2204 const InlineFunctionInfo *inline_info = NULL; 2205 VariableListSP variable_list_sp; 2206 bool get_function_vars = true; 2207 if (sc->block) 2208 { 2209 Block *inline_block = sc->block->GetContainingInlinedBlock (); 2210 2211 if (inline_block) 2212 { 2213 get_function_vars = false; 2214 inline_info = sc->block->GetInlinedFunctionInfo(); 2215 if (inline_info) 2216 variable_list_sp = inline_block->GetBlockVariableList (true); 2217 } 2218 } 2219 2220 if (get_function_vars) 2221 { 2222 variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true); 2223 } 2224 2225 if (inline_info) 2226 { 2227 s.PutCString (cstr); 2228 s.PutCString (" [inlined] "); 2229 cstr = inline_info->GetName().GetCString(); 2230 } 2231 2232 VariableList args; 2233 if (variable_list_sp) 2234 variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument, args); 2235 if (args.GetSize() > 0) 2236 { 2237 const char *open_paren = strchr (cstr, '('); 2238 const char *close_paren = NULL; 2239 if (open_paren) 2240 { 2241 if (IsToken (open_paren, "(anonymous namespace)")) 2242 { 2243 open_paren = strchr (open_paren + strlen("(anonymous namespace)"), '('); 2244 if (open_paren) 2245 close_paren = strchr (open_paren, ')'); 2246 } 2247 else 2248 close_paren = strchr (open_paren, ')'); 2249 } 2250 2251 if (open_paren) 2252 s.Write(cstr, open_paren - cstr + 1); 2253 else 2254 { 2255 s.PutCString (cstr); 2256 s.PutChar ('('); 2257 } 2258 const size_t num_args = args.GetSize(); 2259 for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx) 2260 { 2261 VariableSP var_sp (args.GetVariableAtIndex (arg_idx)); 2262 ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp)); 2263 const char *var_name = var_value_sp->GetName().GetCString(); 2264 const char *var_value = var_value_sp->GetValueAsCString(); 2265 if (arg_idx > 0) 2266 s.PutCString (", "); 2267 if (var_value_sp->GetError().Success()) 2268 { 2269 if (var_value) 2270 s.Printf ("%s=%s", var_name, var_value); 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.reset(new StreamFile(GetOutputFile().GetDescriptor(), false)); 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