1 //===-- Target.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/Target/Target.h" 11 12 // C Includes 13 // C++ Includes 14 // Other libraries and framework includes 15 // Project includes 16 #include "lldb/Breakpoint/BreakpointResolver.h" 17 #include "lldb/Breakpoint/BreakpointResolverAddress.h" 18 #include "lldb/Breakpoint/BreakpointResolverFileLine.h" 19 #include "lldb/Breakpoint/BreakpointResolverName.h" 20 #include "lldb/Core/Debugger.h" 21 #include "lldb/Core/Event.h" 22 #include "lldb/Core/Log.h" 23 #include "lldb/Core/StreamAsynchronousIO.h" 24 #include "lldb/Core/StreamString.h" 25 #include "lldb/Core/Timer.h" 26 #include "lldb/Core/ValueObject.h" 27 #include "lldb/Expression/ClangUserExpression.h" 28 #include "lldb/Host/Host.h" 29 #include "lldb/Interpreter/CommandInterpreter.h" 30 #include "lldb/Interpreter/CommandReturnObject.h" 31 #include "lldb/lldb-private-log.h" 32 #include "lldb/Symbol/ObjectFile.h" 33 #include "lldb/Target/Process.h" 34 #include "lldb/Target/StackFrame.h" 35 #include "lldb/Target/Thread.h" 36 #include "lldb/Target/ThreadSpec.h" 37 38 using namespace lldb; 39 using namespace lldb_private; 40 41 //---------------------------------------------------------------------- 42 // Target constructor 43 //---------------------------------------------------------------------- 44 Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) : 45 Broadcaster ("lldb.target"), 46 ExecutionContextScope (), 47 TargetInstanceSettings (*GetSettingsController()), 48 m_debugger (debugger), 49 m_platform_sp (platform_sp), 50 m_mutex (Mutex::eMutexTypeRecursive), 51 m_arch (target_arch), 52 m_images (), 53 m_section_load_list (), 54 m_breakpoint_list (false), 55 m_internal_breakpoint_list (true), 56 m_process_sp (), 57 m_search_filter_sp (), 58 m_image_search_paths (ImageSearchPathsChanged, this), 59 m_scratch_ast_context_ap (NULL), 60 m_persistent_variables (), 61 m_stop_hooks (), 62 m_stop_hook_next_id (0), 63 m_suppress_stop_hooks (false) 64 { 65 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed"); 66 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded"); 67 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded"); 68 69 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 70 if (log) 71 log->Printf ("%p Target::Target()", this); 72 } 73 74 //---------------------------------------------------------------------- 75 // Destructor 76 //---------------------------------------------------------------------- 77 Target::~Target() 78 { 79 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 80 if (log) 81 log->Printf ("%p Target::~Target()", this); 82 DeleteCurrentProcess (); 83 } 84 85 void 86 Target::Dump (Stream *s, lldb::DescriptionLevel description_level) 87 { 88 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 89 if (description_level != lldb::eDescriptionLevelBrief) 90 { 91 s->Indent(); 92 s->PutCString("Target\n"); 93 s->IndentMore(); 94 m_images.Dump(s); 95 m_breakpoint_list.Dump(s); 96 m_internal_breakpoint_list.Dump(s); 97 s->IndentLess(); 98 } 99 else 100 { 101 if (GetExecutableModule()) 102 s->PutCString (GetExecutableModule()->GetFileSpec().GetFilename().GetCString()); 103 else 104 s->PutCString ("No executable module."); 105 } 106 } 107 108 void 109 Target::DeleteCurrentProcess () 110 { 111 if (m_process_sp.get()) 112 { 113 m_section_load_list.Clear(); 114 if (m_process_sp->IsAlive()) 115 m_process_sp->Destroy(); 116 117 m_process_sp->Finalize(); 118 119 // Do any cleanup of the target we need to do between process instances. 120 // NB It is better to do this before destroying the process in case the 121 // clean up needs some help from the process. 122 m_breakpoint_list.ClearAllBreakpointSites(); 123 m_internal_breakpoint_list.ClearAllBreakpointSites(); 124 m_process_sp.reset(); 125 } 126 } 127 128 const lldb::ProcessSP & 129 Target::CreateProcess (Listener &listener, const char *plugin_name) 130 { 131 DeleteCurrentProcess (); 132 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener)); 133 return m_process_sp; 134 } 135 136 const lldb::ProcessSP & 137 Target::GetProcessSP () const 138 { 139 return m_process_sp; 140 } 141 142 lldb::TargetSP 143 Target::GetSP() 144 { 145 return m_debugger.GetTargetList().GetTargetSP(this); 146 } 147 148 BreakpointList & 149 Target::GetBreakpointList(bool internal) 150 { 151 if (internal) 152 return m_internal_breakpoint_list; 153 else 154 return m_breakpoint_list; 155 } 156 157 const BreakpointList & 158 Target::GetBreakpointList(bool internal) const 159 { 160 if (internal) 161 return m_internal_breakpoint_list; 162 else 163 return m_breakpoint_list; 164 } 165 166 BreakpointSP 167 Target::GetBreakpointByID (break_id_t break_id) 168 { 169 BreakpointSP bp_sp; 170 171 if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) 172 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); 173 else 174 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); 175 176 return bp_sp; 177 } 178 179 BreakpointSP 180 Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal) 181 { 182 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule)); 183 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines)); 184 return CreateBreakpoint (filter_sp, resolver_sp, internal); 185 } 186 187 188 BreakpointSP 189 Target::CreateBreakpoint (lldb::addr_t addr, bool internal) 190 { 191 Address so_addr; 192 // Attempt to resolve our load address if possible, though it is ok if 193 // it doesn't resolve to section/offset. 194 195 // Try and resolve as a load address if possible 196 m_section_load_list.ResolveLoadAddress(addr, so_addr); 197 if (!so_addr.IsValid()) 198 { 199 // The address didn't resolve, so just set this as an absolute address 200 so_addr.SetOffset (addr); 201 } 202 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal)); 203 return bp_sp; 204 } 205 206 BreakpointSP 207 Target::CreateBreakpoint (Address &addr, bool internal) 208 { 209 TargetSP target_sp = this->GetSP(); 210 SearchFilterSP filter_sp(new SearchFilter (target_sp)); 211 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr)); 212 return CreateBreakpoint (filter_sp, resolver_sp, internal); 213 } 214 215 BreakpointSP 216 Target::CreateBreakpoint (FileSpec *containingModule, const char *func_name, uint32_t func_name_type_mask, bool internal) 217 { 218 BreakpointSP bp_sp; 219 if (func_name) 220 { 221 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule)); 222 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL, func_name, func_name_type_mask, Breakpoint::Exact)); 223 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal); 224 } 225 return bp_sp; 226 } 227 228 229 SearchFilterSP 230 Target::GetSearchFilterForModule (const FileSpec *containingModule) 231 { 232 SearchFilterSP filter_sp; 233 lldb::TargetSP target_sp = this->GetSP(); 234 if (containingModule != NULL) 235 { 236 // TODO: We should look into sharing module based search filters 237 // across many breakpoints like we do for the simple target based one 238 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule)); 239 } 240 else 241 { 242 if (m_search_filter_sp.get() == NULL) 243 m_search_filter_sp.reset (new SearchFilter (target_sp)); 244 filter_sp = m_search_filter_sp; 245 } 246 return filter_sp; 247 } 248 249 BreakpointSP 250 Target::CreateBreakpoint (FileSpec *containingModule, RegularExpression &func_regex, bool internal) 251 { 252 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule)); 253 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL, func_regex)); 254 255 return CreateBreakpoint (filter_sp, resolver_sp, internal); 256 } 257 258 BreakpointSP 259 Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal) 260 { 261 BreakpointSP bp_sp; 262 if (filter_sp && resolver_sp) 263 { 264 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp)); 265 resolver_sp->SetBreakpoint (bp_sp.get()); 266 267 if (internal) 268 m_internal_breakpoint_list.Add (bp_sp, false); 269 else 270 m_breakpoint_list.Add (bp_sp, true); 271 272 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 273 if (log) 274 { 275 StreamString s; 276 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose); 277 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData()); 278 } 279 280 bp_sp->ResolveBreakpoint(); 281 } 282 283 if (!internal && bp_sp) 284 { 285 m_last_created_breakpoint = bp_sp; 286 } 287 288 return bp_sp; 289 } 290 291 void 292 Target::RemoveAllBreakpoints (bool internal_also) 293 { 294 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 295 if (log) 296 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); 297 298 m_breakpoint_list.RemoveAll (true); 299 if (internal_also) 300 m_internal_breakpoint_list.RemoveAll (false); 301 302 m_last_created_breakpoint.reset(); 303 } 304 305 void 306 Target::DisableAllBreakpoints (bool internal_also) 307 { 308 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 309 if (log) 310 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); 311 312 m_breakpoint_list.SetEnabledAll (false); 313 if (internal_also) 314 m_internal_breakpoint_list.SetEnabledAll (false); 315 } 316 317 void 318 Target::EnableAllBreakpoints (bool internal_also) 319 { 320 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 321 if (log) 322 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); 323 324 m_breakpoint_list.SetEnabledAll (true); 325 if (internal_also) 326 m_internal_breakpoint_list.SetEnabledAll (true); 327 } 328 329 bool 330 Target::RemoveBreakpointByID (break_id_t break_id) 331 { 332 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 333 if (log) 334 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); 335 336 if (DisableBreakpointByID (break_id)) 337 { 338 if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) 339 m_internal_breakpoint_list.Remove(break_id, false); 340 else 341 { 342 if (m_last_created_breakpoint) 343 { 344 if (m_last_created_breakpoint->GetID() == break_id) 345 m_last_created_breakpoint.reset(); 346 } 347 m_breakpoint_list.Remove(break_id, true); 348 } 349 return true; 350 } 351 return false; 352 } 353 354 bool 355 Target::DisableBreakpointByID (break_id_t break_id) 356 { 357 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 358 if (log) 359 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); 360 361 BreakpointSP bp_sp; 362 363 if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) 364 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); 365 else 366 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); 367 if (bp_sp) 368 { 369 bp_sp->SetEnabled (false); 370 return true; 371 } 372 return false; 373 } 374 375 bool 376 Target::EnableBreakpointByID (break_id_t break_id) 377 { 378 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 379 if (log) 380 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", 381 __FUNCTION__, 382 break_id, 383 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); 384 385 BreakpointSP bp_sp; 386 387 if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) 388 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); 389 else 390 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); 391 392 if (bp_sp) 393 { 394 bp_sp->SetEnabled (true); 395 return true; 396 } 397 return false; 398 } 399 400 ModuleSP 401 Target::GetExecutableModule () 402 { 403 ModuleSP executable_sp; 404 if (m_images.GetSize() > 0) 405 executable_sp = m_images.GetModuleAtIndex(0); 406 return executable_sp; 407 } 408 409 void 410 Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files) 411 { 412 m_images.Clear(); 413 m_scratch_ast_context_ap.reset(); 414 415 if (executable_sp.get()) 416 { 417 Timer scoped_timer (__PRETTY_FUNCTION__, 418 "Target::SetExecutableModule (executable = '%s/%s')", 419 executable_sp->GetFileSpec().GetDirectory().AsCString(), 420 executable_sp->GetFileSpec().GetFilename().AsCString()); 421 422 m_images.Append(executable_sp); // The first image is our exectuable file 423 424 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module. 425 if (!m_arch.IsValid()) 426 m_arch = executable_sp->GetArchitecture(); 427 428 FileSpecList dependent_files; 429 ObjectFile *executable_objfile = executable_sp->GetObjectFile(); 430 431 if (executable_objfile) 432 { 433 executable_objfile->GetDependentModules(dependent_files); 434 for (uint32_t i=0; i<dependent_files.GetSize(); i++) 435 { 436 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i)); 437 FileSpec platform_dependent_file_spec; 438 if (m_platform_sp) 439 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec); 440 else 441 platform_dependent_file_spec = dependent_file_spec; 442 443 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec, 444 m_arch)); 445 if (image_module_sp.get()) 446 { 447 //image_module_sp->Dump(&s);// REMOVE THIS, DEBUG ONLY 448 ObjectFile *objfile = image_module_sp->GetObjectFile(); 449 if (objfile) 450 objfile->GetDependentModules(dependent_files); 451 } 452 } 453 } 454 455 // Now see if we know the target triple, and if so, create our scratch AST context: 456 if (m_arch.IsValid()) 457 { 458 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str())); 459 } 460 } 461 462 UpdateInstanceName(); 463 } 464 465 466 bool 467 Target::SetArchitecture (const ArchSpec &arch_spec) 468 { 469 if (m_arch == arch_spec) 470 { 471 // If we're setting the architecture to our current architecture, we 472 // don't need to do anything. 473 return true; 474 } 475 else if (!m_arch.IsValid()) 476 { 477 // If we haven't got a valid arch spec, then we just need to set it. 478 m_arch = arch_spec; 479 return true; 480 } 481 else 482 { 483 // If we have an executable file, try to reset the executable to the desired architecture 484 m_arch = arch_spec; 485 ModuleSP executable_sp = GetExecutableModule (); 486 m_images.Clear(); 487 m_scratch_ast_context_ap.reset(); 488 // Need to do something about unsetting breakpoints. 489 490 if (executable_sp) 491 { 492 FileSpec exec_file_spec = executable_sp->GetFileSpec(); 493 Error error = ModuleList::GetSharedModule(exec_file_spec, 494 arch_spec, 495 NULL, 496 NULL, 497 0, 498 executable_sp, 499 NULL, 500 NULL); 501 502 if (!error.Fail() && executable_sp) 503 { 504 SetExecutableModule (executable_sp, true); 505 return true; 506 } 507 else 508 { 509 return false; 510 } 511 } 512 else 513 { 514 return false; 515 } 516 } 517 } 518 519 void 520 Target::ModuleAdded (ModuleSP &module_sp) 521 { 522 // A module is being added to this target for the first time 523 ModuleList module_list; 524 module_list.Append(module_sp); 525 ModulesDidLoad (module_list); 526 } 527 528 void 529 Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp) 530 { 531 // A module is being added to this target for the first time 532 ModuleList module_list; 533 module_list.Append (old_module_sp); 534 ModulesDidUnload (module_list); 535 module_list.Clear (); 536 module_list.Append (new_module_sp); 537 ModulesDidLoad (module_list); 538 } 539 540 void 541 Target::ModulesDidLoad (ModuleList &module_list) 542 { 543 m_breakpoint_list.UpdateBreakpoints (module_list, true); 544 // TODO: make event data that packages up the module_list 545 BroadcastEvent (eBroadcastBitModulesLoaded, NULL); 546 } 547 548 void 549 Target::ModulesDidUnload (ModuleList &module_list) 550 { 551 m_breakpoint_list.UpdateBreakpoints (module_list, false); 552 553 // Remove the images from the target image list 554 m_images.Remove(module_list); 555 556 // TODO: make event data that packages up the module_list 557 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL); 558 } 559 560 size_t 561 Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error) 562 { 563 const Section *section = addr.GetSection(); 564 if (section && section->GetModule()) 565 { 566 ObjectFile *objfile = section->GetModule()->GetObjectFile(); 567 if (objfile) 568 { 569 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile, 570 addr.GetOffset(), 571 dst, 572 dst_len); 573 if (bytes_read > 0) 574 return bytes_read; 575 else 576 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString()); 577 } 578 else 579 { 580 error.SetErrorString("address isn't from a object file"); 581 } 582 } 583 else 584 { 585 error.SetErrorString("address doesn't contain a section that points to a section in a object file"); 586 } 587 return 0; 588 } 589 590 size_t 591 Target::ReadMemory (const Address& addr, bool prefer_file_cache, void *dst, size_t dst_len, Error &error) 592 { 593 error.Clear(); 594 595 bool process_is_valid = m_process_sp && m_process_sp->IsAlive(); 596 597 size_t bytes_read = 0; 598 Address resolved_addr; 599 if (!addr.IsSectionOffset()) 600 { 601 if (process_is_valid) 602 m_section_load_list.ResolveLoadAddress (addr.GetOffset(), resolved_addr); 603 else 604 m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr); 605 } 606 if (!resolved_addr.IsValid()) 607 resolved_addr = addr; 608 609 if (prefer_file_cache) 610 { 611 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error); 612 if (bytes_read > 0) 613 return bytes_read; 614 } 615 616 if (process_is_valid) 617 { 618 lldb::addr_t load_addr = resolved_addr.GetLoadAddress (this); 619 if (load_addr == LLDB_INVALID_ADDRESS) 620 { 621 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec()) 622 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n", 623 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(), 624 resolved_addr.GetFileAddress()); 625 else 626 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress()); 627 } 628 else 629 { 630 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error); 631 if (bytes_read != dst_len) 632 { 633 if (error.Success()) 634 { 635 if (bytes_read == 0) 636 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr); 637 else 638 error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, load_addr); 639 } 640 } 641 if (bytes_read) 642 return bytes_read; 643 // If the address is not section offset we have an address that 644 // doesn't resolve to any address in any currently loaded shared 645 // libaries and we failed to read memory so there isn't anything 646 // more we can do. If it is section offset, we might be able to 647 // read cached memory from the object file. 648 if (!resolved_addr.IsSectionOffset()) 649 return 0; 650 } 651 } 652 653 if (!prefer_file_cache) 654 { 655 // If we didn't already try and read from the object file cache, then 656 // try it after failing to read from the process. 657 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error); 658 } 659 return 0; 660 } 661 662 663 ModuleSP 664 Target::GetSharedModule 665 ( 666 const FileSpec& file_spec, 667 const ArchSpec& arch, 668 const lldb_private::UUID *uuid_ptr, 669 const ConstString *object_name, 670 off_t object_offset, 671 Error *error_ptr 672 ) 673 { 674 // Don't pass in the UUID so we can tell if we have a stale value in our list 675 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library 676 bool did_create_module = false; 677 ModuleSP module_sp; 678 679 Error error; 680 681 // If there are image search path entries, try to use them first to acquire a suitable image. 682 if (m_image_search_paths.GetSize()) 683 { 684 FileSpec transformed_spec; 685 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory())) 686 { 687 transformed_spec.GetFilename() = file_spec.GetFilename(); 688 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module); 689 } 690 } 691 692 // The platform is responsible for finding and caching an appropriate 693 // module in the shared module cache. 694 if (m_platform_sp) 695 { 696 FileSpec platform_file_spec; 697 error = m_platform_sp->GetSharedModule (file_spec, 698 arch, 699 uuid_ptr, 700 object_name, 701 object_offset, 702 module_sp, 703 &old_module_sp, 704 &did_create_module); 705 } 706 else 707 { 708 error.SetErrorString("no platform is currently set"); 709 } 710 711 // If a module hasn't been found yet, use the unmodified path. 712 if (module_sp) 713 { 714 m_images.Append (module_sp); 715 if (did_create_module) 716 { 717 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32) 718 ModuleUpdated(old_module_sp, module_sp); 719 else 720 ModuleAdded(module_sp); 721 } 722 } 723 if (error_ptr) 724 *error_ptr = error; 725 return module_sp; 726 } 727 728 729 Target * 730 Target::CalculateTarget () 731 { 732 return this; 733 } 734 735 Process * 736 Target::CalculateProcess () 737 { 738 return NULL; 739 } 740 741 Thread * 742 Target::CalculateThread () 743 { 744 return NULL; 745 } 746 747 StackFrame * 748 Target::CalculateStackFrame () 749 { 750 return NULL; 751 } 752 753 void 754 Target::CalculateExecutionContext (ExecutionContext &exe_ctx) 755 { 756 exe_ctx.target = this; 757 exe_ctx.process = NULL; // Do NOT fill in process... 758 exe_ctx.thread = NULL; 759 exe_ctx.frame = NULL; 760 } 761 762 PathMappingList & 763 Target::GetImageSearchPathList () 764 { 765 return m_image_search_paths; 766 } 767 768 void 769 Target::ImageSearchPathsChanged 770 ( 771 const PathMappingList &path_list, 772 void *baton 773 ) 774 { 775 Target *target = (Target *)baton; 776 if (target->m_images.GetSize() > 1) 777 { 778 ModuleSP exe_module_sp (target->GetExecutableModule()); 779 if (exe_module_sp) 780 { 781 target->m_images.Clear(); 782 target->SetExecutableModule (exe_module_sp, true); 783 } 784 } 785 } 786 787 ClangASTContext * 788 Target::GetScratchClangASTContext() 789 { 790 return m_scratch_ast_context_ap.get(); 791 } 792 793 void 794 Target::SettingsInitialize () 795 { 796 UserSettingsControllerSP &usc = GetSettingsController(); 797 usc.reset (new SettingsController); 798 UserSettingsController::InitializeSettingsController (usc, 799 SettingsController::global_settings_table, 800 SettingsController::instance_settings_table); 801 802 // Now call SettingsInitialize() on each 'child' setting of Target 803 Process::SettingsInitialize (); 804 } 805 806 void 807 Target::SettingsTerminate () 808 { 809 810 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings. 811 812 Process::SettingsTerminate (); 813 814 // Now terminate Target Settings. 815 816 UserSettingsControllerSP &usc = GetSettingsController(); 817 UserSettingsController::FinalizeSettingsController (usc); 818 usc.reset(); 819 } 820 821 UserSettingsControllerSP & 822 Target::GetSettingsController () 823 { 824 static UserSettingsControllerSP g_settings_controller; 825 return g_settings_controller; 826 } 827 828 ArchSpec 829 Target::GetDefaultArchitecture () 830 { 831 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController()); 832 833 if (settings_controller_sp) 834 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture (); 835 return ArchSpec(); 836 } 837 838 void 839 Target::SetDefaultArchitecture (const ArchSpec& arch) 840 { 841 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController()); 842 843 if (settings_controller_sp) 844 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch; 845 } 846 847 Target * 848 Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr) 849 { 850 // The target can either exist in the "process" of ExecutionContext, or in 851 // the "target_sp" member of SymbolContext. This accessor helper function 852 // will get the target from one of these locations. 853 854 Target *target = NULL; 855 if (sc_ptr != NULL) 856 target = sc_ptr->target_sp.get(); 857 if (target == NULL) 858 { 859 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL) 860 target = &exe_ctx_ptr->process->GetTarget(); 861 } 862 return target; 863 } 864 865 866 void 867 Target::UpdateInstanceName () 868 { 869 StreamString sstr; 870 871 ModuleSP module_sp = GetExecutableModule(); 872 if (module_sp) 873 { 874 sstr.Printf ("%s_%s", 875 module_sp->GetFileSpec().GetFilename().AsCString(), 876 module_sp->GetArchitecture().GetArchitectureName()); 877 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), 878 sstr.GetData()); 879 } 880 } 881 882 const char * 883 Target::GetExpressionPrefixContentsAsCString () 884 { 885 if (m_expr_prefix_contents_sp) 886 return (const char *)m_expr_prefix_contents_sp->GetBytes(); 887 return NULL; 888 } 889 890 ExecutionResults 891 Target::EvaluateExpression 892 ( 893 const char *expr_cstr, 894 StackFrame *frame, 895 bool unwind_on_error, 896 bool keep_in_memory, 897 lldb::DynamicValueType use_dynamic, 898 lldb::ValueObjectSP &result_valobj_sp 899 ) 900 { 901 ExecutionResults execution_results = eExecutionSetupError; 902 903 result_valobj_sp.reset(); 904 905 // We shouldn't run stop hooks in expressions. 906 // Be sure to reset this if you return anywhere within this function. 907 bool old_suppress_value = m_suppress_stop_hooks; 908 m_suppress_stop_hooks = true; 909 910 ExecutionContext exe_ctx; 911 if (frame) 912 { 913 frame->CalculateExecutionContext(exe_ctx); 914 Error error; 915 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember | 916 StackFrame::eExpressionPathOptionsNoFragileObjcIvar; 917 lldb::VariableSP var_sp; 918 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr, 919 use_dynamic, 920 expr_path_options, 921 var_sp, 922 error); 923 } 924 else if (m_process_sp) 925 { 926 m_process_sp->CalculateExecutionContext(exe_ctx); 927 } 928 else 929 { 930 CalculateExecutionContext(exe_ctx); 931 } 932 933 if (result_valobj_sp) 934 { 935 execution_results = eExecutionCompleted; 936 // We got a result from the frame variable expression path above... 937 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName()); 938 939 lldb::ValueObjectSP const_valobj_sp; 940 941 // Check in case our value is already a constant value 942 if (result_valobj_sp->GetIsConstant()) 943 { 944 const_valobj_sp = result_valobj_sp; 945 const_valobj_sp->SetName (persistent_variable_name); 946 } 947 else 948 { 949 if (use_dynamic != lldb::eNoDynamicValues) 950 { 951 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic); 952 if (dynamic_sp) 953 result_valobj_sp = dynamic_sp; 954 } 955 956 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name); 957 } 958 959 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp; 960 961 result_valobj_sp = const_valobj_sp; 962 963 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp)); 964 assert (clang_expr_variable_sp.get()); 965 966 // Set flags and live data as appropriate 967 968 const Value &result_value = live_valobj_sp->GetValue(); 969 970 switch (result_value.GetValueType()) 971 { 972 case Value::eValueTypeHostAddress: 973 case Value::eValueTypeFileAddress: 974 // we don't do anything with these for now 975 break; 976 case Value::eValueTypeScalar: 977 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated; 978 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation; 979 break; 980 case Value::eValueTypeLoadAddress: 981 clang_expr_variable_sp->m_live_sp = live_valobj_sp; 982 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference; 983 break; 984 } 985 } 986 else 987 { 988 // Make sure we aren't just trying to see the value of a persistent 989 // variable (something like "$0") 990 lldb::ClangExpressionVariableSP persistent_var_sp; 991 // Only check for persistent variables the expression starts with a '$' 992 if (expr_cstr[0] == '$') 993 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr); 994 995 if (persistent_var_sp) 996 { 997 result_valobj_sp = persistent_var_sp->GetValueObject (); 998 execution_results = eExecutionCompleted; 999 } 1000 else 1001 { 1002 const char *prefix = GetExpressionPrefixContentsAsCString(); 1003 1004 execution_results = ClangUserExpression::Evaluate (exe_ctx, 1005 unwind_on_error, 1006 expr_cstr, 1007 prefix, 1008 result_valobj_sp); 1009 } 1010 } 1011 1012 m_suppress_stop_hooks = old_suppress_value; 1013 1014 return execution_results; 1015 } 1016 1017 lldb::addr_t 1018 Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const 1019 { 1020 addr_t code_addr = load_addr; 1021 switch (m_arch.GetMachine()) 1022 { 1023 case llvm::Triple::arm: 1024 case llvm::Triple::thumb: 1025 switch (addr_class) 1026 { 1027 case eAddressClassData: 1028 case eAddressClassDebug: 1029 return LLDB_INVALID_ADDRESS; 1030 1031 case eAddressClassUnknown: 1032 case eAddressClassInvalid: 1033 case eAddressClassCode: 1034 case eAddressClassCodeAlternateISA: 1035 case eAddressClassRuntime: 1036 // Check if bit zero it no set? 1037 if ((code_addr & 1ull) == 0) 1038 { 1039 // Bit zero isn't set, check if the address is a multiple of 2? 1040 if (code_addr & 2ull) 1041 { 1042 // The address is a multiple of 2 so it must be thumb, set bit zero 1043 code_addr |= 1ull; 1044 } 1045 else if (addr_class == eAddressClassCodeAlternateISA) 1046 { 1047 // We checked the address and the address claims to be the alternate ISA 1048 // which means thumb, so set bit zero. 1049 code_addr |= 1ull; 1050 } 1051 } 1052 break; 1053 } 1054 break; 1055 1056 default: 1057 break; 1058 } 1059 return code_addr; 1060 } 1061 1062 lldb::addr_t 1063 Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const 1064 { 1065 addr_t opcode_addr = load_addr; 1066 switch (m_arch.GetMachine()) 1067 { 1068 case llvm::Triple::arm: 1069 case llvm::Triple::thumb: 1070 switch (addr_class) 1071 { 1072 case eAddressClassData: 1073 case eAddressClassDebug: 1074 return LLDB_INVALID_ADDRESS; 1075 1076 case eAddressClassInvalid: 1077 case eAddressClassUnknown: 1078 case eAddressClassCode: 1079 case eAddressClassCodeAlternateISA: 1080 case eAddressClassRuntime: 1081 opcode_addr &= ~(1ull); 1082 break; 1083 } 1084 break; 1085 1086 default: 1087 break; 1088 } 1089 return opcode_addr; 1090 } 1091 1092 lldb::user_id_t 1093 Target::AddStopHook (Target::StopHookSP &new_hook_sp) 1094 { 1095 lldb::user_id_t new_uid = ++m_stop_hook_next_id; 1096 new_hook_sp.reset (new StopHook(GetSP(), new_uid)); 1097 m_stop_hooks[new_uid] = new_hook_sp; 1098 return new_uid; 1099 } 1100 1101 bool 1102 Target::RemoveStopHookByID (lldb::user_id_t user_id) 1103 { 1104 size_t num_removed; 1105 num_removed = m_stop_hooks.erase (user_id); 1106 if (num_removed == 0) 1107 return false; 1108 else 1109 return true; 1110 } 1111 1112 void 1113 Target::RemoveAllStopHooks () 1114 { 1115 m_stop_hooks.clear(); 1116 } 1117 1118 Target::StopHookSP 1119 Target::GetStopHookByID (lldb::user_id_t user_id) 1120 { 1121 StopHookSP found_hook; 1122 1123 StopHookCollection::iterator specified_hook_iter; 1124 specified_hook_iter = m_stop_hooks.find (user_id); 1125 if (specified_hook_iter != m_stop_hooks.end()) 1126 found_hook = (*specified_hook_iter).second; 1127 return found_hook; 1128 } 1129 1130 bool 1131 Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state) 1132 { 1133 StopHookCollection::iterator specified_hook_iter; 1134 specified_hook_iter = m_stop_hooks.find (user_id); 1135 if (specified_hook_iter == m_stop_hooks.end()) 1136 return false; 1137 1138 (*specified_hook_iter).second->SetIsActive (active_state); 1139 return true; 1140 } 1141 1142 void 1143 Target::SetAllStopHooksActiveState (bool active_state) 1144 { 1145 StopHookCollection::iterator pos, end = m_stop_hooks.end(); 1146 for (pos = m_stop_hooks.begin(); pos != end; pos++) 1147 { 1148 (*pos).second->SetIsActive (active_state); 1149 } 1150 } 1151 1152 void 1153 Target::RunStopHooks () 1154 { 1155 if (m_suppress_stop_hooks) 1156 return; 1157 1158 if (!m_process_sp) 1159 return; 1160 1161 if (m_stop_hooks.empty()) 1162 return; 1163 1164 StopHookCollection::iterator pos, end = m_stop_hooks.end(); 1165 1166 // If there aren't any active stop hooks, don't bother either: 1167 bool any_active_hooks = false; 1168 for (pos = m_stop_hooks.begin(); pos != end; pos++) 1169 { 1170 if ((*pos).second->IsActive()) 1171 { 1172 any_active_hooks = true; 1173 break; 1174 } 1175 } 1176 if (!any_active_hooks) 1177 return; 1178 1179 CommandReturnObject result; 1180 1181 std::vector<ExecutionContext> exc_ctx_with_reasons; 1182 std::vector<SymbolContext> sym_ctx_with_reasons; 1183 1184 ThreadList &cur_threadlist = m_process_sp->GetThreadList(); 1185 size_t num_threads = cur_threadlist.GetSize(); 1186 for (size_t i = 0; i < num_threads; i++) 1187 { 1188 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i); 1189 if (cur_thread_sp->ThreadStoppedForAReason()) 1190 { 1191 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0); 1192 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get())); 1193 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything)); 1194 } 1195 } 1196 1197 // If no threads stopped for a reason, don't run the stop-hooks. 1198 size_t num_exe_ctx = exc_ctx_with_reasons.size(); 1199 if (num_exe_ctx == 0) 1200 return; 1201 1202 StreamSP output_stream (new StreamAsynchronousIO (m_debugger.GetCommandInterpreter(), 1203 CommandInterpreter::eBroadcastBitAsynchronousOutputData)); 1204 StreamSP error_stream (new StreamAsynchronousIO (m_debugger.GetCommandInterpreter(), 1205 CommandInterpreter::eBroadcastBitAsynchronousErrorData)); 1206 result.SetImmediateOutputStream (output_stream); 1207 result.SetImmediateErrorStream (error_stream); 1208 1209 bool keep_going = true; 1210 bool hooks_ran = false; 1211 bool print_hook_header; 1212 bool print_thread_header; 1213 1214 if (num_exe_ctx == 1) 1215 print_thread_header = false; 1216 else 1217 print_thread_header = true; 1218 1219 if (m_stop_hooks.size() == 1) 1220 print_hook_header = false; 1221 else 1222 print_hook_header = true; 1223 1224 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++) 1225 { 1226 // result.Clear(); 1227 StopHookSP cur_hook_sp = (*pos).second; 1228 if (!cur_hook_sp->IsActive()) 1229 continue; 1230 1231 bool any_thread_matched = false; 1232 for (size_t i = 0; keep_going && i < num_exe_ctx; i++) 1233 { 1234 if ((cur_hook_sp->GetSpecifier () == NULL 1235 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i])) 1236 && (cur_hook_sp->GetThreadSpecifier() == NULL 1237 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].thread))) 1238 { 1239 if (!hooks_ran) 1240 { 1241 result.AppendMessage("\n** Stop Hooks **"); 1242 hooks_ran = true; 1243 } 1244 if (print_hook_header && !any_thread_matched) 1245 { 1246 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID()); 1247 any_thread_matched = true; 1248 } 1249 1250 if (print_thread_header) 1251 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].thread->GetIndexID()); 1252 1253 bool stop_on_continue = true; 1254 bool stop_on_error = true; 1255 bool echo_commands = false; 1256 bool print_results = true; 1257 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(), 1258 &exc_ctx_with_reasons[i], 1259 stop_on_continue, 1260 stop_on_error, 1261 echo_commands, 1262 print_results, 1263 result); 1264 1265 // If the command started the target going again, we should bag out of 1266 // running the stop hooks. 1267 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) || 1268 (result.GetStatus() == eReturnStatusSuccessContinuingResult)) 1269 { 1270 result.AppendMessageWithFormat ("Aborting stop hooks, hook %d set the program running.", cur_hook_sp->GetID()); 1271 keep_going = false; 1272 } 1273 } 1274 } 1275 } 1276 if (hooks_ran) 1277 result.AppendMessage ("\n** End Stop Hooks **\n"); 1278 1279 result.GetImmediateOutputStream()->Flush(); 1280 result.GetImmediateErrorStream()->Flush(); 1281 } 1282 1283 //-------------------------------------------------------------- 1284 // class Target::StopHook 1285 //-------------------------------------------------------------- 1286 1287 1288 Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) : 1289 UserID (uid), 1290 m_target_sp (target_sp), 1291 m_commands (), 1292 m_specifier_sp (), 1293 m_thread_spec_ap(NULL), 1294 m_active (true) 1295 { 1296 } 1297 1298 Target::StopHook::StopHook (const StopHook &rhs) : 1299 UserID (rhs.GetID()), 1300 m_target_sp (rhs.m_target_sp), 1301 m_commands (rhs.m_commands), 1302 m_specifier_sp (rhs.m_specifier_sp), 1303 m_thread_spec_ap (NULL), 1304 m_active (rhs.m_active) 1305 { 1306 if (rhs.m_thread_spec_ap.get() != NULL) 1307 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get())); 1308 } 1309 1310 1311 Target::StopHook::~StopHook () 1312 { 1313 } 1314 1315 void 1316 Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier) 1317 { 1318 m_thread_spec_ap.reset (specifier); 1319 } 1320 1321 1322 void 1323 Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const 1324 { 1325 int indent_level = s->GetIndentLevel(); 1326 1327 s->SetIndentLevel(indent_level + 2); 1328 1329 s->Printf ("Hook: %d\n", GetID()); 1330 if (m_active) 1331 s->Indent ("State: enabled\n"); 1332 else 1333 s->Indent ("State: disabled\n"); 1334 1335 if (m_specifier_sp) 1336 { 1337 s->Indent(); 1338 s->PutCString ("Specifier:\n"); 1339 s->SetIndentLevel (indent_level + 4); 1340 m_specifier_sp->GetDescription (s, level); 1341 s->SetIndentLevel (indent_level + 2); 1342 } 1343 1344 if (m_thread_spec_ap.get() != NULL) 1345 { 1346 StreamString tmp; 1347 s->Indent("Thread:\n"); 1348 m_thread_spec_ap->GetDescription (&tmp, level); 1349 s->SetIndentLevel (indent_level + 4); 1350 s->Indent (tmp.GetData()); 1351 s->PutCString ("\n"); 1352 s->SetIndentLevel (indent_level + 2); 1353 } 1354 1355 s->Indent ("Commands: \n"); 1356 s->SetIndentLevel (indent_level + 4); 1357 uint32_t num_commands = m_commands.GetSize(); 1358 for (uint32_t i = 0; i < num_commands; i++) 1359 { 1360 s->Indent(m_commands.GetStringAtIndex(i)); 1361 s->PutCString ("\n"); 1362 } 1363 s->SetIndentLevel (indent_level); 1364 } 1365 1366 1367 //-------------------------------------------------------------- 1368 // class Target::SettingsController 1369 //-------------------------------------------------------------- 1370 1371 Target::SettingsController::SettingsController () : 1372 UserSettingsController ("target", Debugger::GetSettingsController()), 1373 m_default_architecture () 1374 { 1375 m_default_settings.reset (new TargetInstanceSettings (*this, false, 1376 InstanceSettings::GetDefaultName().AsCString())); 1377 } 1378 1379 Target::SettingsController::~SettingsController () 1380 { 1381 } 1382 1383 lldb::InstanceSettingsSP 1384 Target::SettingsController::CreateInstanceSettings (const char *instance_name) 1385 { 1386 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(), 1387 false, 1388 instance_name); 1389 lldb::InstanceSettingsSP new_settings_sp (new_settings); 1390 return new_settings_sp; 1391 } 1392 1393 1394 #define TSC_DEFAULT_ARCH "default-arch" 1395 #define TSC_EXPR_PREFIX "expr-prefix" 1396 #define TSC_PREFER_DYNAMIC "prefer-dynamic-value" 1397 #define TSC_SKIP_PROLOGUE "skip-prologue" 1398 #define TSC_SOURCE_MAP "source-map" 1399 1400 1401 static const ConstString & 1402 GetSettingNameForDefaultArch () 1403 { 1404 static ConstString g_const_string (TSC_DEFAULT_ARCH); 1405 return g_const_string; 1406 } 1407 1408 static const ConstString & 1409 GetSettingNameForExpressionPrefix () 1410 { 1411 static ConstString g_const_string (TSC_EXPR_PREFIX); 1412 return g_const_string; 1413 } 1414 1415 static const ConstString & 1416 GetSettingNameForPreferDynamicValue () 1417 { 1418 static ConstString g_const_string (TSC_PREFER_DYNAMIC); 1419 return g_const_string; 1420 } 1421 1422 static const ConstString & 1423 GetSettingNameForSourcePathMap () 1424 { 1425 static ConstString g_const_string (TSC_SOURCE_MAP); 1426 return g_const_string; 1427 } 1428 1429 static const ConstString & 1430 GetSettingNameForSkipPrologue () 1431 { 1432 static ConstString g_const_string (TSC_SKIP_PROLOGUE); 1433 return g_const_string; 1434 } 1435 1436 1437 1438 bool 1439 Target::SettingsController::SetGlobalVariable (const ConstString &var_name, 1440 const char *index_value, 1441 const char *value, 1442 const SettingEntry &entry, 1443 const VarSetOperationType op, 1444 Error&err) 1445 { 1446 if (var_name == GetSettingNameForDefaultArch()) 1447 { 1448 m_default_architecture.SetTriple (value, NULL); 1449 if (!m_default_architecture.IsValid()) 1450 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value); 1451 } 1452 return true; 1453 } 1454 1455 1456 bool 1457 Target::SettingsController::GetGlobalVariable (const ConstString &var_name, 1458 StringList &value, 1459 Error &err) 1460 { 1461 if (var_name == GetSettingNameForDefaultArch()) 1462 { 1463 // If the arch is invalid (the default), don't show a string for it 1464 if (m_default_architecture.IsValid()) 1465 value.AppendString (m_default_architecture.GetArchitectureName()); 1466 return true; 1467 } 1468 else 1469 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString()); 1470 1471 return false; 1472 } 1473 1474 //-------------------------------------------------------------- 1475 // class TargetInstanceSettings 1476 //-------------------------------------------------------------- 1477 1478 TargetInstanceSettings::TargetInstanceSettings 1479 ( 1480 UserSettingsController &owner, 1481 bool live_instance, 1482 const char *name 1483 ) : 1484 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance), 1485 m_expr_prefix_file (), 1486 m_expr_prefix_contents_sp (), 1487 m_prefer_dynamic_value (2), 1488 m_skip_prologue (true, true), 1489 m_source_map (NULL, NULL) 1490 { 1491 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called 1492 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers. 1493 // For this reason it has to be called here, rather than in the initializer or in the parent constructor. 1494 // This is true for CreateInstanceName() too. 1495 1496 if (GetInstanceName () == InstanceSettings::InvalidName()) 1497 { 1498 ChangeInstanceName (std::string (CreateInstanceName().AsCString())); 1499 m_owner.RegisterInstanceSettings (this); 1500 } 1501 1502 if (live_instance) 1503 { 1504 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name); 1505 CopyInstanceSettings (pending_settings,false); 1506 } 1507 } 1508 1509 TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) : 1510 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()), 1511 m_expr_prefix_file (rhs.m_expr_prefix_file), 1512 m_expr_prefix_contents_sp (rhs.m_expr_prefix_contents_sp), 1513 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value), 1514 m_skip_prologue (rhs.m_skip_prologue), 1515 m_source_map (rhs.m_source_map) 1516 { 1517 if (m_instance_name != InstanceSettings::GetDefaultName()) 1518 { 1519 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name); 1520 CopyInstanceSettings (pending_settings,false); 1521 } 1522 } 1523 1524 TargetInstanceSettings::~TargetInstanceSettings () 1525 { 1526 } 1527 1528 TargetInstanceSettings& 1529 TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs) 1530 { 1531 if (this != &rhs) 1532 { 1533 } 1534 1535 return *this; 1536 } 1537 1538 void 1539 TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name, 1540 const char *index_value, 1541 const char *value, 1542 const ConstString &instance_name, 1543 const SettingEntry &entry, 1544 VarSetOperationType op, 1545 Error &err, 1546 bool pending) 1547 { 1548 if (var_name == GetSettingNameForExpressionPrefix ()) 1549 { 1550 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file); 1551 if (err.Success()) 1552 { 1553 switch (op) 1554 { 1555 default: 1556 break; 1557 case eVarSetOperationAssign: 1558 case eVarSetOperationAppend: 1559 { 1560 if (!m_expr_prefix_file.GetCurrentValue().Exists()) 1561 { 1562 err.SetErrorToGenericError (); 1563 err.SetErrorStringWithFormat ("%s does not exist.\n", value); 1564 return; 1565 } 1566 1567 m_expr_prefix_contents_sp = m_expr_prefix_file.GetCurrentValue().ReadFileContents(); 1568 1569 if (!m_expr_prefix_contents_sp && m_expr_prefix_contents_sp->GetByteSize() == 0) 1570 { 1571 err.SetErrorStringWithFormat ("Couldn't read data from '%s'\n", value); 1572 m_expr_prefix_contents_sp.reset(); 1573 } 1574 } 1575 break; 1576 case eVarSetOperationClear: 1577 m_expr_prefix_contents_sp.reset(); 1578 } 1579 } 1580 } 1581 else if (var_name == GetSettingNameForPreferDynamicValue()) 1582 { 1583 int new_value; 1584 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err); 1585 if (err.Success()) 1586 m_prefer_dynamic_value = new_value; 1587 } 1588 else if (var_name == GetSettingNameForSkipPrologue()) 1589 { 1590 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue); 1591 } 1592 else if (var_name == GetSettingNameForSourcePathMap ()) 1593 { 1594 switch (op) 1595 { 1596 case eVarSetOperationReplace: 1597 case eVarSetOperationInsertBefore: 1598 case eVarSetOperationInsertAfter: 1599 case eVarSetOperationRemove: 1600 default: 1601 break; 1602 case eVarSetOperationAssign: 1603 m_source_map.Clear(true); 1604 // Fall through to append.... 1605 case eVarSetOperationAppend: 1606 { 1607 Args args(value); 1608 const uint32_t argc = args.GetArgumentCount(); 1609 if (argc & 1 || argc == 0) 1610 { 1611 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc); 1612 } 1613 else 1614 { 1615 char resolved_new_path[PATH_MAX]; 1616 FileSpec file_spec; 1617 const char *old_path; 1618 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2) 1619 { 1620 const char *new_path = args.GetArgumentAtIndex(idx+1); 1621 assert (new_path); // We have an even number of paths, this shouldn't happen! 1622 1623 file_spec.SetFile(new_path, true); 1624 if (file_spec.Exists()) 1625 { 1626 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path)) 1627 { 1628 err.SetErrorStringWithFormat("new path '%s' is too long", new_path); 1629 return; 1630 } 1631 } 1632 else 1633 { 1634 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path); 1635 return; 1636 } 1637 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true); 1638 } 1639 } 1640 } 1641 break; 1642 1643 case eVarSetOperationClear: 1644 m_source_map.Clear(true); 1645 break; 1646 } 1647 } 1648 } 1649 1650 void 1651 TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending) 1652 { 1653 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get()); 1654 1655 if (!new_settings_ptr) 1656 return; 1657 1658 m_expr_prefix_file = new_settings_ptr->m_expr_prefix_file; 1659 m_expr_prefix_contents_sp = new_settings_ptr->m_expr_prefix_contents_sp; 1660 m_prefer_dynamic_value = new_settings_ptr->m_prefer_dynamic_value; 1661 m_skip_prologue = new_settings_ptr->m_skip_prologue; 1662 } 1663 1664 bool 1665 TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry, 1666 const ConstString &var_name, 1667 StringList &value, 1668 Error *err) 1669 { 1670 if (var_name == GetSettingNameForExpressionPrefix ()) 1671 { 1672 char path[PATH_MAX]; 1673 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path)); 1674 if (path_len > 0) 1675 value.AppendString (path, path_len); 1676 } 1677 else if (var_name == GetSettingNameForPreferDynamicValue()) 1678 { 1679 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value); 1680 } 1681 else if (var_name == GetSettingNameForSkipPrologue()) 1682 { 1683 if (m_skip_prologue) 1684 value.AppendString ("true"); 1685 else 1686 value.AppendString ("false"); 1687 } 1688 else if (var_name == GetSettingNameForSourcePathMap ()) 1689 { 1690 } 1691 else 1692 { 1693 if (err) 1694 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString()); 1695 return false; 1696 } 1697 1698 return true; 1699 } 1700 1701 const ConstString 1702 TargetInstanceSettings::CreateInstanceName () 1703 { 1704 StreamString sstr; 1705 static int instance_count = 1; 1706 1707 sstr.Printf ("target_%d", instance_count); 1708 ++instance_count; 1709 1710 const ConstString ret_val (sstr.GetData()); 1711 return ret_val; 1712 } 1713 1714 //-------------------------------------------------- 1715 // Target::SettingsController Variable Tables 1716 //-------------------------------------------------- 1717 OptionEnumValueElement 1718 TargetInstanceSettings::g_dynamic_value_types[] = 1719 { 1720 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"}, 1721 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."}, 1722 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."}, 1723 { 0, NULL, NULL } 1724 }; 1725 1726 SettingEntry 1727 Target::SettingsController::global_settings_table[] = 1728 { 1729 // var-name var-type default enum init'd hidden help-text 1730 // ================= ================== =========== ==== ====== ====== ========================================================================= 1731 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." }, 1732 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL } 1733 }; 1734 1735 SettingEntry 1736 Target::SettingsController::instance_settings_table[] = 1737 { 1738 // var-name var-type default enum init'd hidden help-text 1739 // ================= ================== =============== ======================= ====== ====== ========================================================================= 1740 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." }, 1741 { TSC_PREFER_DYNAMIC, eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." }, 1742 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." }, 1743 { TSC_SOURCE_MAP , eSetVarTypeArray , NULL , NULL, false, false, "Source path remappings to use when locating source files from debug information." }, 1744 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL } 1745 }; 1746