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/DataBufferMemoryMap.h" 21 #include "lldb/Core/Debugger.h" 22 #include "lldb/Core/Event.h" 23 #include "lldb/Core/Log.h" 24 #include "lldb/Core/StreamString.h" 25 #include "lldb/Core/Timer.h" 26 #include "lldb/Core/ValueObject.h" 27 #include "lldb/Host/Host.h" 28 #include "lldb/lldb-private-log.h" 29 #include "lldb/Symbol/ObjectFile.h" 30 #include "lldb/Target/Process.h" 31 #include "lldb/Target/StackFrame.h" 32 33 using namespace lldb; 34 using namespace lldb_private; 35 36 //---------------------------------------------------------------------- 37 // Target constructor 38 //---------------------------------------------------------------------- 39 Target::Target(Debugger &debugger) : 40 Broadcaster("lldb.target"), 41 TargetInstanceSettings (*GetSettingsController()), 42 m_debugger (debugger), 43 m_mutex (Mutex::eMutexTypeRecursive), 44 m_images(), 45 m_section_load_list (), 46 m_breakpoint_list (false), 47 m_internal_breakpoint_list (true), 48 m_process_sp(), 49 m_triple(), 50 m_search_filter_sp(), 51 m_image_search_paths (ImageSearchPathsChanged, this), 52 m_scratch_ast_context_ap (NULL), 53 m_persistent_variables () 54 { 55 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed"); 56 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded"); 57 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded"); 58 59 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 60 if (log) 61 log->Printf ("%p Target::Target()", this); 62 } 63 64 //---------------------------------------------------------------------- 65 // Destructor 66 //---------------------------------------------------------------------- 67 Target::~Target() 68 { 69 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 70 if (log) 71 log->Printf ("%p Target::~Target()", this); 72 DeleteCurrentProcess (); 73 } 74 75 void 76 Target::Dump (Stream *s, lldb::DescriptionLevel description_level) 77 { 78 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 79 if (description_level != lldb::eDescriptionLevelBrief) 80 { 81 s->Indent(); 82 s->PutCString("Target\n"); 83 s->IndentMore(); 84 m_images.Dump(s); 85 m_breakpoint_list.Dump(s); 86 m_internal_breakpoint_list.Dump(s); 87 s->IndentLess(); 88 } 89 else 90 { 91 s->PutCString (GetExecutableModule()->GetFileSpec().GetFilename().GetCString()); 92 } 93 } 94 95 void 96 Target::DeleteCurrentProcess () 97 { 98 if (m_process_sp.get()) 99 { 100 m_section_load_list.Clear(); 101 if (m_process_sp->IsAlive()) 102 m_process_sp->Destroy(); 103 else 104 m_process_sp->Finalize(); 105 106 // Do any cleanup of the target we need to do between process instances. 107 // NB It is better to do this before destroying the process in case the 108 // clean up needs some help from the process. 109 m_breakpoint_list.ClearAllBreakpointSites(); 110 m_internal_breakpoint_list.ClearAllBreakpointSites(); 111 m_process_sp.reset(); 112 } 113 } 114 115 const lldb::ProcessSP & 116 Target::CreateProcess (Listener &listener, const char *plugin_name) 117 { 118 DeleteCurrentProcess (); 119 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener)); 120 return m_process_sp; 121 } 122 123 const lldb::ProcessSP & 124 Target::GetProcessSP () const 125 { 126 return m_process_sp; 127 } 128 129 lldb::TargetSP 130 Target::GetSP() 131 { 132 return m_debugger.GetTargetList().GetTargetSP(this); 133 } 134 135 BreakpointList & 136 Target::GetBreakpointList(bool internal) 137 { 138 if (internal) 139 return m_internal_breakpoint_list; 140 else 141 return m_breakpoint_list; 142 } 143 144 const BreakpointList & 145 Target::GetBreakpointList(bool internal) const 146 { 147 if (internal) 148 return m_internal_breakpoint_list; 149 else 150 return m_breakpoint_list; 151 } 152 153 BreakpointSP 154 Target::GetBreakpointByID (break_id_t break_id) 155 { 156 BreakpointSP bp_sp; 157 158 if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) 159 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); 160 else 161 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); 162 163 return bp_sp; 164 } 165 166 BreakpointSP 167 Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal) 168 { 169 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule)); 170 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines)); 171 return CreateBreakpoint (filter_sp, resolver_sp, internal); 172 } 173 174 175 BreakpointSP 176 Target::CreateBreakpoint (lldb::addr_t addr, bool internal) 177 { 178 Address so_addr; 179 // Attempt to resolve our load address if possible, though it is ok if 180 // it doesn't resolve to section/offset. 181 182 // Try and resolve as a load address if possible 183 m_section_load_list.ResolveLoadAddress(addr, so_addr); 184 if (!so_addr.IsValid()) 185 { 186 // The address didn't resolve, so just set this as an absolute address 187 so_addr.SetOffset (addr); 188 } 189 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal)); 190 return bp_sp; 191 } 192 193 BreakpointSP 194 Target::CreateBreakpoint (Address &addr, bool internal) 195 { 196 TargetSP target_sp = this->GetSP(); 197 SearchFilterSP filter_sp(new SearchFilter (target_sp)); 198 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr)); 199 return CreateBreakpoint (filter_sp, resolver_sp, internal); 200 } 201 202 BreakpointSP 203 Target::CreateBreakpoint (FileSpec *containingModule, const char *func_name, uint32_t func_name_type_mask, bool internal) 204 { 205 BreakpointSP bp_sp; 206 if (func_name) 207 { 208 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule)); 209 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL, func_name, func_name_type_mask, Breakpoint::Exact)); 210 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal); 211 } 212 return bp_sp; 213 } 214 215 216 SearchFilterSP 217 Target::GetSearchFilterForModule (const FileSpec *containingModule) 218 { 219 SearchFilterSP filter_sp; 220 lldb::TargetSP target_sp = this->GetSP(); 221 if (containingModule != NULL) 222 { 223 // TODO: We should look into sharing module based search filters 224 // across many breakpoints like we do for the simple target based one 225 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule)); 226 } 227 else 228 { 229 if (m_search_filter_sp.get() == NULL) 230 m_search_filter_sp.reset (new SearchFilter (target_sp)); 231 filter_sp = m_search_filter_sp; 232 } 233 return filter_sp; 234 } 235 236 BreakpointSP 237 Target::CreateBreakpoint (FileSpec *containingModule, RegularExpression &func_regex, bool internal) 238 { 239 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule)); 240 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL, func_regex)); 241 242 return CreateBreakpoint (filter_sp, resolver_sp, internal); 243 } 244 245 BreakpointSP 246 Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal) 247 { 248 BreakpointSP bp_sp; 249 if (filter_sp && resolver_sp) 250 { 251 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp)); 252 resolver_sp->SetBreakpoint (bp_sp.get()); 253 254 if (internal) 255 m_internal_breakpoint_list.Add (bp_sp, false); 256 else 257 m_breakpoint_list.Add (bp_sp, true); 258 259 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 260 if (log) 261 { 262 StreamString s; 263 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose); 264 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData()); 265 } 266 267 bp_sp->ResolveBreakpoint(); 268 } 269 270 if (!internal && bp_sp) 271 { 272 m_last_created_breakpoint = bp_sp; 273 } 274 275 return bp_sp; 276 } 277 278 void 279 Target::RemoveAllBreakpoints (bool internal_also) 280 { 281 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 282 if (log) 283 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); 284 285 m_breakpoint_list.RemoveAll (true); 286 if (internal_also) 287 m_internal_breakpoint_list.RemoveAll (false); 288 289 m_last_created_breakpoint.reset(); 290 } 291 292 void 293 Target::DisableAllBreakpoints (bool internal_also) 294 { 295 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 296 if (log) 297 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); 298 299 m_breakpoint_list.SetEnabledAll (false); 300 if (internal_also) 301 m_internal_breakpoint_list.SetEnabledAll (false); 302 } 303 304 void 305 Target::EnableAllBreakpoints (bool internal_also) 306 { 307 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 308 if (log) 309 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); 310 311 m_breakpoint_list.SetEnabledAll (true); 312 if (internal_also) 313 m_internal_breakpoint_list.SetEnabledAll (true); 314 } 315 316 bool 317 Target::RemoveBreakpointByID (break_id_t break_id) 318 { 319 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 320 if (log) 321 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); 322 323 if (DisableBreakpointByID (break_id)) 324 { 325 if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) 326 m_internal_breakpoint_list.Remove(break_id, false); 327 else 328 { 329 if (m_last_created_breakpoint->GetID() == break_id) 330 m_last_created_breakpoint.reset(); 331 m_breakpoint_list.Remove(break_id, true); 332 } 333 return true; 334 } 335 return false; 336 } 337 338 bool 339 Target::DisableBreakpointByID (break_id_t break_id) 340 { 341 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 342 if (log) 343 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); 344 345 BreakpointSP bp_sp; 346 347 if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) 348 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); 349 else 350 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); 351 if (bp_sp) 352 { 353 bp_sp->SetEnabled (false); 354 return true; 355 } 356 return false; 357 } 358 359 bool 360 Target::EnableBreakpointByID (break_id_t break_id) 361 { 362 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 363 if (log) 364 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", 365 __FUNCTION__, 366 break_id, 367 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); 368 369 BreakpointSP bp_sp; 370 371 if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) 372 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); 373 else 374 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); 375 376 if (bp_sp) 377 { 378 bp_sp->SetEnabled (true); 379 return true; 380 } 381 return false; 382 } 383 384 ModuleSP 385 Target::GetExecutableModule () 386 { 387 ModuleSP executable_sp; 388 if (m_images.GetSize() > 0) 389 executable_sp = m_images.GetModuleAtIndex(0); 390 return executable_sp; 391 } 392 393 void 394 Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files) 395 { 396 m_images.Clear(); 397 m_scratch_ast_context_ap.reset(); 398 399 if (executable_sp.get()) 400 { 401 Timer scoped_timer (__PRETTY_FUNCTION__, 402 "Target::SetExecutableModule (executable = '%s/%s')", 403 executable_sp->GetFileSpec().GetDirectory().AsCString(), 404 executable_sp->GetFileSpec().GetFilename().AsCString()); 405 406 m_images.Append(executable_sp); // The first image is our exectuable file 407 408 ArchSpec exe_arch = executable_sp->GetArchitecture(); 409 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module. 410 if (!m_arch_spec.IsValid()) 411 m_arch_spec = exe_arch; 412 413 FileSpecList dependent_files; 414 ObjectFile * executable_objfile = executable_sp->GetObjectFile(); 415 if (executable_objfile == NULL) 416 { 417 418 FileSpec bundle_executable(executable_sp->GetFileSpec()); 419 if (Host::ResolveExecutableInBundle (bundle_executable)) 420 { 421 ModuleSP bundle_exe_module_sp(GetSharedModule(bundle_executable, 422 exe_arch)); 423 SetExecutableModule (bundle_exe_module_sp, get_dependent_files); 424 if (bundle_exe_module_sp->GetObjectFile() != NULL) 425 executable_sp = bundle_exe_module_sp; 426 return; 427 } 428 } 429 430 if (executable_objfile) 431 { 432 executable_objfile->GetDependentModules(dependent_files); 433 for (uint32_t i=0; i<dependent_files.GetSize(); i++) 434 { 435 ModuleSP image_module_sp(GetSharedModule(dependent_files.GetFileSpecPointerAtIndex(i), 436 exe_arch)); 437 if (image_module_sp.get()) 438 { 439 //image_module_sp->Dump(&s);// REMOVE THIS, DEBUG ONLY 440 ObjectFile *objfile = image_module_sp->GetObjectFile(); 441 if (objfile) 442 objfile->GetDependentModules(dependent_files); 443 } 444 } 445 } 446 447 // Now see if we know the target triple, and if so, create our scratch AST context: 448 ConstString target_triple; 449 if (GetTargetTriple(target_triple)) 450 { 451 m_scratch_ast_context_ap.reset (new ClangASTContext(target_triple.GetCString())); 452 } 453 } 454 455 UpdateInstanceName(); 456 } 457 458 459 ModuleList& 460 Target::GetImages () 461 { 462 return m_images; 463 } 464 465 ArchSpec 466 Target::GetArchitecture () const 467 { 468 return m_arch_spec; 469 } 470 471 bool 472 Target::SetArchitecture (const ArchSpec &arch_spec) 473 { 474 if (m_arch_spec == arch_spec) 475 { 476 // If we're setting the architecture to our current architecture, we 477 // don't need to do anything. 478 return true; 479 } 480 else if (!m_arch_spec.IsValid()) 481 { 482 // If we haven't got a valid arch spec, then we just need to set it. 483 m_arch_spec = arch_spec; 484 return true; 485 } 486 else 487 { 488 // If we have an executable file, try to reset the executable to the desired architecture 489 m_arch_spec = arch_spec; 490 ModuleSP executable_sp = GetExecutableModule (); 491 m_images.Clear(); 492 m_scratch_ast_context_ap.reset(); 493 m_triple.Clear(); 494 // Need to do something about unsetting breakpoints. 495 496 if (executable_sp) 497 { 498 FileSpec exec_file_spec = executable_sp->GetFileSpec(); 499 Error error = ModuleList::GetSharedModule(exec_file_spec, 500 arch_spec, 501 NULL, 502 NULL, 503 0, 504 executable_sp, 505 NULL, 506 NULL); 507 508 if (!error.Fail() && executable_sp) 509 { 510 SetExecutableModule (executable_sp, true); 511 return true; 512 } 513 else 514 { 515 return false; 516 } 517 } 518 else 519 { 520 return false; 521 } 522 } 523 } 524 525 bool 526 Target::GetTargetTriple(ConstString &triple) 527 { 528 triple.Clear(); 529 530 if (m_triple) 531 { 532 triple = m_triple; 533 } 534 else 535 { 536 Module *exe_module = GetExecutableModule().get(); 537 if (exe_module) 538 { 539 ObjectFile *objfile = exe_module->GetObjectFile(); 540 if (objfile) 541 { 542 objfile->GetTargetTriple(m_triple); 543 triple = m_triple; 544 } 545 } 546 } 547 return !triple.IsEmpty(); 548 } 549 550 void 551 Target::ModuleAdded (ModuleSP &module_sp) 552 { 553 // A module is being added to this target for the first time 554 ModuleList module_list; 555 module_list.Append(module_sp); 556 ModulesDidLoad (module_list); 557 } 558 559 void 560 Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp) 561 { 562 // A module is being added to this target for the first time 563 ModuleList module_list; 564 module_list.Append (old_module_sp); 565 ModulesDidUnload (module_list); 566 module_list.Clear (); 567 module_list.Append (new_module_sp); 568 ModulesDidLoad (module_list); 569 } 570 571 void 572 Target::ModulesDidLoad (ModuleList &module_list) 573 { 574 m_breakpoint_list.UpdateBreakpoints (module_list, true); 575 // TODO: make event data that packages up the module_list 576 BroadcastEvent (eBroadcastBitModulesLoaded, NULL); 577 } 578 579 void 580 Target::ModulesDidUnload (ModuleList &module_list) 581 { 582 m_breakpoint_list.UpdateBreakpoints (module_list, false); 583 584 // Remove the images from the target image list 585 m_images.Remove(module_list); 586 587 // TODO: make event data that packages up the module_list 588 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL); 589 } 590 591 size_t 592 Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error) 593 { 594 const Section *section = addr.GetSection(); 595 if (section && section->GetModule()) 596 { 597 ObjectFile *objfile = section->GetModule()->GetObjectFile(); 598 if (objfile) 599 { 600 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile, 601 addr.GetOffset(), 602 dst, 603 dst_len); 604 if (bytes_read > 0) 605 return bytes_read; 606 else 607 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString()); 608 } 609 else 610 { 611 error.SetErrorString("address isn't from a object file"); 612 } 613 } 614 else 615 { 616 error.SetErrorString("address doesn't contain a section that points to a section in a object file"); 617 } 618 return 0; 619 } 620 621 size_t 622 Target::ReadMemory (const Address& addr, bool prefer_file_cache, void *dst, size_t dst_len, Error &error) 623 { 624 error.Clear(); 625 626 bool process_is_valid = m_process_sp && m_process_sp->IsAlive(); 627 628 size_t bytes_read = 0; 629 Address resolved_addr(addr); 630 if (!resolved_addr.IsSectionOffset()) 631 { 632 if (process_is_valid) 633 { 634 m_section_load_list.ResolveLoadAddress (addr.GetOffset(), resolved_addr); 635 } 636 else 637 { 638 m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr); 639 } 640 } 641 642 if (prefer_file_cache) 643 { 644 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error); 645 if (bytes_read > 0) 646 return bytes_read; 647 } 648 649 if (process_is_valid) 650 { 651 lldb::addr_t load_addr = resolved_addr.GetLoadAddress (this); 652 if (load_addr == LLDB_INVALID_ADDRESS) 653 { 654 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec()) 655 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n", 656 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(), 657 resolved_addr.GetFileAddress()); 658 else 659 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress()); 660 } 661 else 662 { 663 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error); 664 if (bytes_read != dst_len) 665 { 666 if (error.Success()) 667 { 668 if (bytes_read == 0) 669 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr); 670 else 671 error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, load_addr); 672 } 673 } 674 if (bytes_read) 675 return bytes_read; 676 // If the address is not section offset we have an address that 677 // doesn't resolve to any address in any currently loaded shared 678 // libaries and we failed to read memory so there isn't anything 679 // more we can do. If it is section offset, we might be able to 680 // read cached memory from the object file. 681 if (!resolved_addr.IsSectionOffset()) 682 return 0; 683 } 684 } 685 686 if (!prefer_file_cache) 687 { 688 // If we didn't already try and read from the object file cache, then 689 // try it after failing to read from the process. 690 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error); 691 } 692 return 0; 693 } 694 695 696 ModuleSP 697 Target::GetSharedModule 698 ( 699 const FileSpec& file_spec, 700 const ArchSpec& arch, 701 const UUID *uuid_ptr, 702 const ConstString *object_name, 703 off_t object_offset, 704 Error *error_ptr 705 ) 706 { 707 // Don't pass in the UUID so we can tell if we have a stale value in our list 708 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library 709 bool did_create_module = false; 710 ModuleSP module_sp; 711 712 // If there are image search path entries, try to use them first to acquire a suitable image. 713 714 Error error; 715 716 if (m_image_search_paths.GetSize()) 717 { 718 FileSpec transformed_spec; 719 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory())) 720 { 721 transformed_spec.GetFilename() = file_spec.GetFilename(); 722 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module); 723 } 724 } 725 726 // If a module hasn't been found yet, use the unmodified path. 727 728 if (!module_sp) 729 { 730 error = (ModuleList::GetSharedModule (file_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module)); 731 } 732 733 if (module_sp) 734 { 735 m_images.Append (module_sp); 736 if (did_create_module) 737 { 738 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32) 739 ModuleUpdated(old_module_sp, module_sp); 740 else 741 ModuleAdded(module_sp); 742 } 743 } 744 if (error_ptr) 745 *error_ptr = error; 746 return module_sp; 747 } 748 749 750 Target * 751 Target::CalculateTarget () 752 { 753 return this; 754 } 755 756 Process * 757 Target::CalculateProcess () 758 { 759 return NULL; 760 } 761 762 Thread * 763 Target::CalculateThread () 764 { 765 return NULL; 766 } 767 768 StackFrame * 769 Target::CalculateStackFrame () 770 { 771 return NULL; 772 } 773 774 void 775 Target::CalculateExecutionContext (ExecutionContext &exe_ctx) 776 { 777 exe_ctx.target = this; 778 exe_ctx.process = NULL; // Do NOT fill in process... 779 exe_ctx.thread = NULL; 780 exe_ctx.frame = NULL; 781 } 782 783 PathMappingList & 784 Target::GetImageSearchPathList () 785 { 786 return m_image_search_paths; 787 } 788 789 void 790 Target::ImageSearchPathsChanged 791 ( 792 const PathMappingList &path_list, 793 void *baton 794 ) 795 { 796 Target *target = (Target *)baton; 797 if (target->m_images.GetSize() > 1) 798 { 799 ModuleSP exe_module_sp (target->GetExecutableModule()); 800 if (exe_module_sp) 801 { 802 target->m_images.Clear(); 803 target->SetExecutableModule (exe_module_sp, true); 804 } 805 } 806 } 807 808 ClangASTContext * 809 Target::GetScratchClangASTContext() 810 { 811 return m_scratch_ast_context_ap.get(); 812 } 813 814 void 815 Target::Initialize () 816 { 817 UserSettingsControllerSP &usc = GetSettingsController(); 818 usc.reset (new SettingsController); 819 UserSettingsController::InitializeSettingsController (usc, 820 SettingsController::global_settings_table, 821 SettingsController::instance_settings_table); 822 } 823 824 void 825 Target::Terminate () 826 { 827 UserSettingsControllerSP &usc = GetSettingsController(); 828 UserSettingsController::FinalizeSettingsController (usc); 829 usc.reset(); 830 } 831 832 UserSettingsControllerSP & 833 Target::GetSettingsController () 834 { 835 static UserSettingsControllerSP g_settings_controller; 836 return g_settings_controller; 837 } 838 839 ArchSpec 840 Target::GetDefaultArchitecture () 841 { 842 lldb::UserSettingsControllerSP &settings_controller = GetSettingsController(); 843 lldb::SettableVariableType var_type; 844 Error err; 845 StringList result = settings_controller->GetVariable ("target.default-arch", var_type, "[]", err); 846 847 const char *default_name = ""; 848 if (result.GetSize() == 1 && err.Success()) 849 default_name = result.GetStringAtIndex (0); 850 851 ArchSpec default_arch (default_name); 852 return default_arch; 853 } 854 855 void 856 Target::SetDefaultArchitecture (ArchSpec new_arch) 857 { 858 if (new_arch.IsValid()) 859 GetSettingsController ()->SetVariable ("target.default-arch", 860 new_arch.AsCString(), 861 lldb::eVarSetOperationAssign, 862 false, 863 "[]"); 864 } 865 866 Target * 867 Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr) 868 { 869 // The target can either exist in the "process" of ExecutionContext, or in 870 // the "target_sp" member of SymbolContext. This accessor helper function 871 // will get the target from one of these locations. 872 873 Target *target = NULL; 874 if (sc_ptr != NULL) 875 target = sc_ptr->target_sp.get(); 876 if (target == NULL) 877 { 878 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL) 879 target = &exe_ctx_ptr->process->GetTarget(); 880 } 881 return target; 882 } 883 884 885 void 886 Target::UpdateInstanceName () 887 { 888 StreamString sstr; 889 890 ModuleSP module_sp = GetExecutableModule(); 891 if (module_sp) 892 { 893 sstr.Printf ("%s_%s", 894 module_sp->GetFileSpec().GetFilename().AsCString(), 895 module_sp->GetArchitecture().AsCString()); 896 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), 897 sstr.GetData()); 898 } 899 } 900 901 const char * 902 Target::GetExpressionPrefixContentsAsCString () 903 { 904 return m_expr_prefix_contents.c_str(); 905 } 906 907 ExecutionResults 908 Target::EvaluateExpression 909 ( 910 const char *expr_cstr, 911 StackFrame *frame, 912 bool unwind_on_error, 913 lldb::ValueObjectSP &result_valobj_sp 914 ) 915 { 916 ExecutionResults execution_results = eExecutionSetupError; 917 918 result_valobj_sp.reset(); 919 920 ExecutionContext exe_ctx; 921 if (frame) 922 { 923 frame->CalculateExecutionContext(exe_ctx); 924 Error error; 925 const bool check_ptr_vs_member = true; 926 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr, check_ptr_vs_member, error); 927 } 928 else if (m_process_sp) 929 { 930 m_process_sp->CalculateExecutionContext(exe_ctx); 931 } 932 else 933 { 934 CalculateExecutionContext(exe_ctx); 935 } 936 937 if (result_valobj_sp) 938 { 939 execution_results = eExecutionCompleted; 940 // We got a result from the frame variable expression path above... 941 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName()); 942 943 lldb::ValueObjectSP const_valobj_sp; 944 945 // Check in case our value is already a constant value 946 if (result_valobj_sp->GetIsConstant()) 947 { 948 const_valobj_sp = result_valobj_sp; 949 const_valobj_sp->SetName (persistent_variable_name); 950 } 951 else 952 const_valobj_sp = result_valobj_sp->CreateConstantValue (exe_ctx.GetBestExecutionContextScope(), 953 persistent_variable_name); 954 955 result_valobj_sp = const_valobj_sp; 956 957 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp)); 958 assert (clang_expr_variable_sp.get()); 959 } 960 else 961 { 962 // Make sure we aren't just trying to see the value of a persistent 963 // variable (something like "$0") 964 lldb::ClangExpressionVariableSP persistent_var_sp; 965 // Only check for persistent variables the expression starts with a '$' 966 if (expr_cstr[0] == '$') 967 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr); 968 969 if (persistent_var_sp) 970 { 971 result_valobj_sp = persistent_var_sp->GetValueObject (); 972 execution_results = eExecutionCompleted; 973 } 974 else 975 { 976 const char *prefix = GetExpressionPrefixContentsAsCString(); 977 978 execution_results = ClangUserExpression::Evaluate (exe_ctx, 979 unwind_on_error, 980 expr_cstr, 981 prefix, 982 result_valobj_sp); 983 } 984 } 985 return execution_results; 986 } 987 988 //-------------------------------------------------------------- 989 // class Target::SettingsController 990 //-------------------------------------------------------------- 991 992 Target::SettingsController::SettingsController () : 993 UserSettingsController ("target", Debugger::GetSettingsController()), 994 m_default_architecture () 995 { 996 m_default_settings.reset (new TargetInstanceSettings (*this, false, 997 InstanceSettings::GetDefaultName().AsCString())); 998 } 999 1000 Target::SettingsController::~SettingsController () 1001 { 1002 } 1003 1004 lldb::InstanceSettingsSP 1005 Target::SettingsController::CreateInstanceSettings (const char *instance_name) 1006 { 1007 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(), 1008 false, 1009 instance_name); 1010 lldb::InstanceSettingsSP new_settings_sp (new_settings); 1011 return new_settings_sp; 1012 } 1013 1014 const ConstString & 1015 Target::SettingsController::DefArchVarName () 1016 { 1017 static ConstString def_arch_var_name ("default-arch"); 1018 1019 return def_arch_var_name; 1020 } 1021 1022 bool 1023 Target::SettingsController::SetGlobalVariable (const ConstString &var_name, 1024 const char *index_value, 1025 const char *value, 1026 const SettingEntry &entry, 1027 const lldb::VarSetOperationType op, 1028 Error&err) 1029 { 1030 if (var_name == DefArchVarName()) 1031 { 1032 ArchSpec tmp_spec (value); 1033 if (tmp_spec.IsValid()) 1034 m_default_architecture = tmp_spec; 1035 else 1036 err.SetErrorStringWithFormat ("'%s' is not a valid architecture.", value); 1037 } 1038 return true; 1039 } 1040 1041 1042 bool 1043 Target::SettingsController::GetGlobalVariable (const ConstString &var_name, 1044 StringList &value, 1045 Error &err) 1046 { 1047 if (var_name == DefArchVarName()) 1048 { 1049 // If the arch is invalid (the default), don't show a string for it 1050 if (m_default_architecture.IsValid()) 1051 value.AppendString (m_default_architecture.AsCString()); 1052 return true; 1053 } 1054 else 1055 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString()); 1056 1057 return false; 1058 } 1059 1060 //-------------------------------------------------------------- 1061 // class TargetInstanceSettings 1062 //-------------------------------------------------------------- 1063 1064 TargetInstanceSettings::TargetInstanceSettings 1065 ( 1066 UserSettingsController &owner, 1067 bool live_instance, 1068 const char *name 1069 ) : 1070 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance) 1071 { 1072 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called 1073 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers. 1074 // For this reason it has to be called here, rather than in the initializer or in the parent constructor. 1075 // This is true for CreateInstanceName() too. 1076 1077 if (GetInstanceName () == InstanceSettings::InvalidName()) 1078 { 1079 ChangeInstanceName (std::string (CreateInstanceName().AsCString())); 1080 m_owner.RegisterInstanceSettings (this); 1081 } 1082 1083 if (live_instance) 1084 { 1085 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name); 1086 CopyInstanceSettings (pending_settings,false); 1087 //m_owner.RemovePendingSettings (m_instance_name); 1088 } 1089 } 1090 1091 TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) : 1092 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()) 1093 { 1094 if (m_instance_name != InstanceSettings::GetDefaultName()) 1095 { 1096 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name); 1097 CopyInstanceSettings (pending_settings,false); 1098 //m_owner.RemovePendingSettings (m_instance_name); 1099 } 1100 } 1101 1102 TargetInstanceSettings::~TargetInstanceSettings () 1103 { 1104 } 1105 1106 TargetInstanceSettings& 1107 TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs) 1108 { 1109 if (this != &rhs) 1110 { 1111 } 1112 1113 return *this; 1114 } 1115 1116 #define EXPR_PREFIX_STRING "expr-prefix" 1117 1118 void 1119 TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name, 1120 const char *index_value, 1121 const char *value, 1122 const ConstString &instance_name, 1123 const SettingEntry &entry, 1124 lldb::VarSetOperationType op, 1125 Error &err, 1126 bool pending) 1127 { 1128 static ConstString expr_prefix_str (EXPR_PREFIX_STRING); 1129 1130 if (var_name == expr_prefix_str) 1131 { 1132 switch (op) 1133 { 1134 default: 1135 err.SetErrorToGenericError (); 1136 err.SetErrorString ("Unrecognized operation. Cannot update value.\n"); 1137 return; 1138 case lldb::eVarSetOperationAssign: 1139 { 1140 FileSpec file_spec(value, true); 1141 1142 if (!file_spec.Exists()) 1143 { 1144 err.SetErrorToGenericError (); 1145 err.SetErrorStringWithFormat ("%s does not exist.\n", value); 1146 return; 1147 } 1148 1149 DataBufferMemoryMap buf; 1150 1151 if (!buf.MemoryMapFromFileSpec(&file_spec) && 1152 buf.GetError().Fail()) 1153 { 1154 err.SetErrorToGenericError (); 1155 err.SetErrorStringWithFormat ("Couldn't read from %s: %s\n", value, buf.GetError().AsCString()); 1156 return; 1157 } 1158 1159 m_expr_prefix_path = value; 1160 m_expr_prefix_contents.assign(reinterpret_cast<const char *>(buf.GetBytes()), buf.GetByteSize()); 1161 } 1162 return; 1163 case lldb::eVarSetOperationAppend: 1164 err.SetErrorToGenericError (); 1165 err.SetErrorString ("Cannot append to a path.\n"); 1166 return; 1167 case lldb::eVarSetOperationClear: 1168 m_expr_prefix_path.clear (); 1169 m_expr_prefix_contents.clear (); 1170 return; 1171 } 1172 } 1173 } 1174 1175 void 1176 TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, 1177 bool pending) 1178 { 1179 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get()); 1180 1181 if (!new_settings_ptr) 1182 return; 1183 1184 m_expr_prefix_path = new_settings_ptr->m_expr_prefix_path; 1185 m_expr_prefix_contents = new_settings_ptr->m_expr_prefix_contents; 1186 } 1187 1188 bool 1189 TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry, 1190 const ConstString &var_name, 1191 StringList &value, 1192 Error *err) 1193 { 1194 static ConstString expr_prefix_str (EXPR_PREFIX_STRING); 1195 1196 if (var_name == expr_prefix_str) 1197 { 1198 value.AppendString (m_expr_prefix_path.c_str(), m_expr_prefix_path.size()); 1199 } 1200 else 1201 { 1202 if (err) 1203 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString()); 1204 return false; 1205 } 1206 1207 return true; 1208 } 1209 1210 const ConstString 1211 TargetInstanceSettings::CreateInstanceName () 1212 { 1213 StreamString sstr; 1214 static int instance_count = 1; 1215 1216 sstr.Printf ("target_%d", instance_count); 1217 ++instance_count; 1218 1219 const ConstString ret_val (sstr.GetData()); 1220 return ret_val; 1221 } 1222 1223 //-------------------------------------------------- 1224 // Target::SettingsController Variable Tables 1225 //-------------------------------------------------- 1226 1227 SettingEntry 1228 Target::SettingsController::global_settings_table[] = 1229 { 1230 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"}, 1231 { "default-arch", eSetVarTypeString, NULL, NULL, false, false, "Default architecture to choose, when there's a choice." }, 1232 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL } 1233 }; 1234 1235 SettingEntry 1236 Target::SettingsController::instance_settings_table[] = 1237 { 1238 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"}, 1239 { EXPR_PREFIX_STRING, eSetVarTypeString, NULL, NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." }, 1240 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL } 1241 }; 1242