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/Event.h" 21 #include "lldb/Core/Log.h" 22 #include "lldb/Core/Timer.h" 23 #include "lldb/Core/StreamString.h" 24 #include "lldb/Host/Host.h" 25 #include "lldb/lldb-private-log.h" 26 #include "lldb/Symbol/ObjectFile.h" 27 #include "lldb/Target/Process.h" 28 #include "lldb/Core/Debugger.h" 29 30 using namespace lldb; 31 using namespace lldb_private; 32 33 //---------------------------------------------------------------------- 34 // Target constructor 35 //---------------------------------------------------------------------- 36 Target::Target() : 37 Broadcaster("Target"), 38 m_images(), 39 m_breakpoint_list (false), 40 m_internal_breakpoint_list (true), 41 m_process_sp(), 42 m_triple(), 43 m_search_filter_sp(), 44 m_image_search_paths (ImageSearchPathsChanged, this), 45 m_scratch_ast_context_ap(NULL) 46 { 47 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT); 48 if (log) 49 log->Printf ("%p Target::Target()", this); 50 } 51 52 //---------------------------------------------------------------------- 53 // Destructor 54 //---------------------------------------------------------------------- 55 Target::~Target() 56 { 57 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT); 58 if (log) 59 log->Printf ("%p Target::~Target()", this); 60 DeleteCurrentProcess (); 61 } 62 63 void 64 Target::Dump (Stream *s) 65 { 66 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 67 s->Indent(); 68 s->PutCString("Target\n"); 69 s->IndentMore(); 70 m_images.Dump(s); 71 m_breakpoint_list.Dump(s); 72 m_internal_breakpoint_list.Dump(s); 73 // if (m_process_sp.get()) 74 // m_process_sp->Dump(s); 75 s->IndentLess(); 76 } 77 78 void 79 Target::DeleteCurrentProcess () 80 { 81 if (m_process_sp.get()) 82 { 83 if (m_process_sp->IsAlive()) 84 m_process_sp->Destroy(); 85 else 86 m_process_sp->Finalize(); 87 88 // Do any cleanup of the target we need to do between process instances. 89 // NB It is better to do this before destroying the process in case the 90 // clean up needs some help from the process. 91 m_breakpoint_list.ClearAllBreakpointSites(); 92 m_internal_breakpoint_list.ClearAllBreakpointSites(); 93 m_process_sp.reset(); 94 } 95 } 96 97 const lldb::ProcessSP & 98 Target::CreateProcess (Listener &listener, const char *plugin_name) 99 { 100 DeleteCurrentProcess (); 101 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener)); 102 return m_process_sp; 103 } 104 105 const lldb::ProcessSP & 106 Target::GetProcessSP () const 107 { 108 return m_process_sp; 109 } 110 111 lldb::TargetSP 112 Target::GetSP() 113 { 114 return Debugger::GetSharedInstance().GetTargetList().GetTargetSP(this); 115 } 116 117 BreakpointList & 118 Target::GetBreakpointList(bool internal) 119 { 120 if (internal) 121 return m_internal_breakpoint_list; 122 else 123 return m_breakpoint_list; 124 } 125 126 const BreakpointList & 127 Target::GetBreakpointList(bool internal) const 128 { 129 if (internal) 130 return m_internal_breakpoint_list; 131 else 132 return m_breakpoint_list; 133 } 134 135 BreakpointSP 136 Target::GetBreakpointByID (break_id_t break_id) 137 { 138 BreakpointSP bp_sp; 139 140 if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) 141 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); 142 else 143 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); 144 145 return bp_sp; 146 } 147 148 BreakpointSP 149 Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal) 150 { 151 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule)); 152 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines)); 153 return CreateBreakpoint (filter_sp, resolver_sp, internal); 154 } 155 156 157 BreakpointSP 158 Target::CreateBreakpoint (lldb::addr_t load_addr, bool internal) 159 { 160 BreakpointSP bp_sp; 161 Address so_addr; 162 // Attempt to resolve our load address if possible, though it is ok if 163 // it doesn't resolve to section/offset. 164 165 Process *process = GetProcessSP().get(); 166 if (process && process->ResolveLoadAddress(load_addr, so_addr)) 167 bp_sp = CreateBreakpoint(so_addr, internal); 168 return bp_sp; 169 } 170 171 BreakpointSP 172 Target::CreateBreakpoint (Address &addr, bool internal) 173 { 174 TargetSP target_sp = this->GetSP(); 175 SearchFilterSP filter_sp(new SearchFilter (target_sp)); 176 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr)); 177 return CreateBreakpoint (filter_sp, resolver_sp, internal); 178 } 179 180 BreakpointSP 181 Target::CreateBreakpoint (FileSpec *containingModule, const char *func_name, bool internal) 182 { 183 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule)); 184 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL, func_name)); 185 return CreateBreakpoint (filter_sp, resolver_sp, internal); 186 } 187 188 189 SearchFilterSP 190 Target::GetSearchFilterForModule (const FileSpec *containingModule) 191 { 192 SearchFilterSP filter_sp; 193 lldb::TargetSP target_sp = this->GetSP(); 194 if (containingModule != NULL) 195 { 196 // TODO: We should look into sharing module based search filters 197 // across many breakpoints like we do for the simple target based one 198 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule)); 199 } 200 else 201 { 202 if (m_search_filter_sp.get() == NULL) 203 m_search_filter_sp.reset (new SearchFilter (target_sp)); 204 filter_sp = m_search_filter_sp; 205 } 206 return filter_sp; 207 } 208 209 BreakpointSP 210 Target::CreateBreakpoint (FileSpec *containingModule, RegularExpression &func_regex, bool internal) 211 { 212 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule)); 213 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL, func_regex)); 214 215 return CreateBreakpoint (filter_sp, resolver_sp, internal); 216 } 217 218 BreakpointSP 219 Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal) 220 { 221 BreakpointSP bp_sp; 222 if (filter_sp && resolver_sp) 223 { 224 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp)); 225 resolver_sp->SetBreakpoint (bp_sp.get()); 226 227 if (internal) 228 m_internal_breakpoint_list.Add (bp_sp); 229 else 230 m_breakpoint_list.Add (bp_sp); 231 232 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS); 233 if (log) 234 { 235 StreamString s; 236 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose); 237 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData()); 238 } 239 240 // Broadcast the breakpoint creation event. 241 if (!internal && EventTypeHasListeners(eBroadcastBitBreakpointChanged)) 242 { 243 BroadcastEvent (eBroadcastBitBreakpointChanged, 244 new Breakpoint::BreakpointEventData (Breakpoint::BreakpointEventData::eBreakpointAdded, bp_sp)); 245 } 246 247 bp_sp->ResolveBreakpoint(); 248 } 249 return bp_sp; 250 } 251 252 void 253 Target::RemoveAllBreakpoints (bool internal_also) 254 { 255 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS); 256 if (log) 257 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); 258 259 m_breakpoint_list.RemoveAll(); 260 if (internal_also) 261 m_internal_breakpoint_list.RemoveAll(); 262 } 263 264 void 265 Target::DisableAllBreakpoints (bool internal_also) 266 { 267 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS); 268 if (log) 269 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); 270 271 m_breakpoint_list.SetEnabledAll (false); 272 if (internal_also) 273 m_internal_breakpoint_list.SetEnabledAll (false); 274 } 275 276 void 277 Target::EnableAllBreakpoints (bool internal_also) 278 { 279 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS); 280 if (log) 281 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); 282 283 m_breakpoint_list.SetEnabledAll (true); 284 if (internal_also) 285 m_internal_breakpoint_list.SetEnabledAll (true); 286 } 287 288 bool 289 Target::RemoveBreakpointByID (break_id_t break_id) 290 { 291 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS); 292 if (log) 293 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); 294 295 if (DisableBreakpointByID (break_id)) 296 { 297 if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) 298 m_internal_breakpoint_list.Remove(break_id); 299 else 300 m_breakpoint_list.Remove(break_id); 301 return true; 302 } 303 return false; 304 } 305 306 bool 307 Target::DisableBreakpointByID (break_id_t break_id) 308 { 309 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS); 310 if (log) 311 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); 312 313 BreakpointSP bp_sp; 314 315 if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) 316 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); 317 else 318 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); 319 if (bp_sp) 320 { 321 bp_sp->SetEnabled (false); 322 return true; 323 } 324 return false; 325 } 326 327 bool 328 Target::EnableBreakpointByID (break_id_t break_id) 329 { 330 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS); 331 if (log) 332 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", 333 __FUNCTION__, 334 break_id, 335 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); 336 337 BreakpointSP bp_sp; 338 339 if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) 340 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); 341 else 342 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); 343 344 if (bp_sp) 345 { 346 bp_sp->SetEnabled (true); 347 return true; 348 } 349 return false; 350 } 351 352 ModuleSP 353 Target::GetExecutableModule () 354 { 355 ModuleSP executable_sp; 356 if (m_images.GetSize() > 0) 357 executable_sp = m_images.GetModuleAtIndex(0); 358 return executable_sp; 359 } 360 361 void 362 Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files) 363 { 364 m_images.Clear(); 365 m_scratch_ast_context_ap.reset(); 366 367 if (executable_sp.get()) 368 { 369 Timer scoped_timer (__PRETTY_FUNCTION__, 370 "Target::SetExecutableModule (executable = '%s/%s')", 371 executable_sp->GetFileSpec().GetDirectory().AsCString(), 372 executable_sp->GetFileSpec().GetFilename().AsCString()); 373 374 m_images.Append(executable_sp); // The first image is our exectuable file 375 376 ArchSpec exe_arch = executable_sp->GetArchitecture(); 377 FileSpecList dependent_files; 378 ObjectFile * executable_objfile = executable_sp->GetObjectFile(); 379 if (executable_objfile == NULL) 380 { 381 382 FileSpec bundle_executable(executable_sp->GetFileSpec()); 383 if (Host::ResolveExecutableInBundle (&bundle_executable)) 384 { 385 ModuleSP bundle_exe_module_sp(GetSharedModule(bundle_executable, 386 exe_arch)); 387 SetExecutableModule (bundle_exe_module_sp, get_dependent_files); 388 if (bundle_exe_module_sp->GetObjectFile() != NULL) 389 executable_sp = bundle_exe_module_sp; 390 return; 391 } 392 } 393 394 if (executable_objfile) 395 { 396 executable_objfile->GetDependentModules(dependent_files); 397 for (uint32_t i=0; i<dependent_files.GetSize(); i++) 398 { 399 ModuleSP image_module_sp(GetSharedModule(dependent_files.GetFileSpecPointerAtIndex(i), 400 exe_arch)); 401 if (image_module_sp.get()) 402 { 403 //image_module_sp->Dump(&s);// REMOVE THIS, DEBUG ONLY 404 ObjectFile *objfile = image_module_sp->GetObjectFile(); 405 if (objfile) 406 objfile->GetDependentModules(dependent_files); 407 } 408 } 409 } 410 411 // Now see if we know the target triple, and if so, create our scratch AST context: 412 ConstString target_triple; 413 if (GetTargetTriple(target_triple)) 414 { 415 m_scratch_ast_context_ap.reset (new ClangASTContext(target_triple.GetCString())); 416 } 417 } 418 } 419 420 421 ModuleList& 422 Target::GetImages () 423 { 424 return m_images; 425 } 426 427 ArchSpec 428 Target::GetArchitecture () const 429 { 430 ArchSpec arch; 431 if (m_images.GetSize() > 0) 432 { 433 Module *exe_module = m_images.GetModulePointerAtIndex(0); 434 if (exe_module) 435 arch = exe_module->GetArchitecture(); 436 } 437 return arch; 438 } 439 440 441 442 bool 443 Target::GetTargetTriple(ConstString &triple) 444 { 445 triple.Clear(); 446 447 if (m_triple) 448 { 449 triple = m_triple; 450 } 451 else 452 { 453 Module *exe_module = GetExecutableModule().get(); 454 if (exe_module) 455 { 456 ObjectFile *objfile = exe_module->GetObjectFile(); 457 if (objfile) 458 { 459 objfile->GetTargetTriple(m_triple); 460 triple = m_triple; 461 } 462 } 463 } 464 return !triple.IsEmpty(); 465 } 466 467 void 468 Target::ModuleAdded (ModuleSP &module_sp) 469 { 470 // A module is being added to this target for the first time 471 ModuleList module_list; 472 module_list.Append(module_sp); 473 ModulesDidLoad (module_list); 474 } 475 476 void 477 Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp) 478 { 479 // A module is being added to this target for the first time 480 ModuleList module_list; 481 module_list.Append (old_module_sp); 482 ModulesDidUnload (module_list); 483 module_list.Clear (); 484 module_list.Append (new_module_sp); 485 ModulesDidLoad (module_list); 486 } 487 488 void 489 Target::ModulesDidLoad (ModuleList &module_list) 490 { 491 m_breakpoint_list.UpdateBreakpoints (module_list, true); 492 // TODO: make event data that packages up the module_list 493 BroadcastEvent (eBroadcastBitModulesLoaded, NULL); 494 } 495 496 void 497 Target::ModulesDidUnload (ModuleList &module_list) 498 { 499 m_breakpoint_list.UpdateBreakpoints (module_list, false); 500 // TODO: make event data that packages up the module_list 501 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL); 502 } 503 504 size_t 505 Target::ReadMemory 506 ( 507 lldb::AddressType addr_type, 508 lldb::addr_t addr, 509 void *dst, 510 size_t dst_len, 511 Error &error, 512 ObjectFile* objfile 513 ) 514 { 515 size_t bytes_read = 0; 516 error.Clear(); 517 switch (addr_type) 518 { 519 case eAddressTypeFile: 520 if (objfile) 521 { 522 if (m_process_sp.get()) 523 { 524 // If we have an execution context with a process, lets try and 525 // resolve the file address in "objfile" and read it from the 526 // process 527 Address so_addr(addr, objfile->GetSectionList()); 528 lldb::addr_t load_addr = so_addr.GetLoadAddress(m_process_sp.get()); 529 if (load_addr == LLDB_INVALID_ADDRESS) 530 { 531 if (objfile->GetFileSpec()) 532 error.SetErrorStringWithFormat("0x%llx can't be resolved, %s in not currently loaded.\n", addr, objfile->GetFileSpec().GetFilename().AsCString()); 533 else 534 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", addr, objfile->GetFileSpec().GetFilename().AsCString()); 535 } 536 else 537 { 538 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error); 539 if (bytes_read != dst_len) 540 { 541 if (error.Success()) 542 { 543 if (bytes_read == 0) 544 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr); 545 else 546 error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, load_addr); 547 } 548 } 549 } 550 } 551 else 552 { 553 // Try and read the file based address from the object file's 554 // section data. 555 } 556 } 557 break; 558 559 case eAddressTypeLoad: 560 if (m_process_sp.get()) 561 { 562 bytes_read = m_process_sp->ReadMemory(addr, dst, dst_len, error); 563 if (bytes_read != dst_len) 564 { 565 if (error.Success()) 566 { 567 if (bytes_read == 0) 568 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", addr); 569 else 570 error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, addr); 571 } 572 } 573 } 574 else 575 error.SetErrorStringWithFormat("Need valid process to read load address.\n"); 576 break; 577 578 case eAddressTypeHost: 579 // The address is an address in this process, so just copy it 580 ::memcpy (dst, (uint8_t*)NULL + addr, dst_len); 581 break; 582 583 default: 584 error.SetErrorStringWithFormat ("Unsupported lldb::AddressType value (%i).\n", addr_type); 585 break; 586 } 587 return bytes_read; 588 } 589 590 591 ModuleSP 592 Target::GetSharedModule 593 ( 594 const FileSpec& file_spec, 595 const ArchSpec& arch, 596 const UUID *uuid_ptr, 597 const ConstString *object_name, 598 off_t object_offset, 599 Error *error_ptr 600 ) 601 { 602 // Don't pass in the UUID so we can tell if we have a stale value in our list 603 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library 604 bool did_create_module = false; 605 ModuleSP module_sp; 606 607 // If there are image search path entries, try to use them first to acquire a suitable image. 608 609 Error error; 610 611 if (m_image_search_paths.GetSize()) 612 { 613 FileSpec transformed_spec; 614 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory())) 615 { 616 transformed_spec.GetFilename() = file_spec.GetFilename(); 617 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module); 618 } 619 } 620 621 // If a module hasn't been found yet, use the unmodified path. 622 623 if (!module_sp) 624 { 625 error = (ModuleList::GetSharedModule (file_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module)); 626 } 627 628 if (module_sp) 629 { 630 m_images.Append (module_sp); 631 if (did_create_module) 632 { 633 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32) 634 ModuleUpdated(old_module_sp, module_sp); 635 else 636 ModuleAdded(module_sp); 637 } 638 } 639 if (error_ptr) 640 *error_ptr = error; 641 return module_sp; 642 } 643 644 645 Target * 646 Target::CalculateTarget () 647 { 648 return this; 649 } 650 651 Process * 652 Target::CalculateProcess () 653 { 654 return NULL; 655 } 656 657 Thread * 658 Target::CalculateThread () 659 { 660 return NULL; 661 } 662 663 StackFrame * 664 Target::CalculateStackFrame () 665 { 666 return NULL; 667 } 668 669 void 670 Target::Calculate (ExecutionContext &exe_ctx) 671 { 672 exe_ctx.target = this; 673 exe_ctx.process = NULL; // Do NOT fill in process... 674 exe_ctx.thread = NULL; 675 exe_ctx.frame = NULL; 676 } 677 678 PathMappingList & 679 Target::GetImageSearchPathList () 680 { 681 return m_image_search_paths; 682 } 683 684 void 685 Target::ImageSearchPathsChanged 686 ( 687 const PathMappingList &path_list, 688 void *baton 689 ) 690 { 691 Target *target = (Target *)baton; 692 if (target->m_images.GetSize() > 1) 693 { 694 ModuleSP exe_module_sp (target->GetExecutableModule()); 695 if (exe_module_sp) 696 { 697 target->m_images.Clear(); 698 target->SetExecutableModule (exe_module_sp, true); 699 } 700 } 701 } 702 703 ClangASTContext * 704 Target::GetScratchClangASTContext() 705 { 706 return m_scratch_ast_context_ap.get(); 707 } 708