1 //===-- SBTarget.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/API/SBTarget.h" 11 12 #include "lldb/lldb-include.h" 13 14 #include "lldb/API/SBFileSpec.h" 15 #include "lldb/API/SBModule.h" 16 #include "lldb/API/SBStream.h" 17 #include "lldb/Breakpoint/BreakpointID.h" 18 #include "lldb/Breakpoint/BreakpointIDList.h" 19 #include "lldb/Breakpoint/BreakpointList.h" 20 #include "lldb/Breakpoint/BreakpointLocation.h" 21 #include "lldb/Core/Address.h" 22 #include "lldb/Core/AddressResolver.h" 23 #include "lldb/Core/AddressResolverName.h" 24 #include "lldb/Interpreter/Args.h" 25 #include "lldb/Core/ArchSpec.h" 26 #include "lldb/Core/Debugger.h" 27 #include "lldb/Core/Disassembler.h" 28 #include "lldb/Core/FileSpec.h" 29 #include "lldb/Core/RegularExpression.h" 30 #include "lldb/Core/SearchFilter.h" 31 #include "lldb/Core/STLUtils.h" 32 #include "lldb/Target/Process.h" 33 #include "lldb/Target/Target.h" 34 #include "lldb/Target/TargetList.h" 35 36 #include "lldb/Interpreter/CommandReturnObject.h" 37 #include "../source/Commands/CommandObjectBreakpoint.h" 38 39 #include "lldb/API/SBDebugger.h" 40 #include "lldb/API/SBProcess.h" 41 #include "lldb/API/SBListener.h" 42 #include "lldb/API/SBBreakpoint.h" 43 44 using namespace lldb; 45 using namespace lldb_private; 46 47 #define DEFAULT_DISASM_BYTE_SIZE 32 48 49 //---------------------------------------------------------------------- 50 // SBTarget constructor 51 //---------------------------------------------------------------------- 52 SBTarget::SBTarget () 53 { 54 } 55 56 SBTarget::SBTarget (const SBTarget& rhs) : 57 m_opaque_sp (rhs.m_opaque_sp) 58 { 59 } 60 61 SBTarget::SBTarget(const TargetSP& target_sp) : 62 m_opaque_sp (target_sp) 63 { 64 } 65 66 const SBTarget& 67 SBTarget::Assign (const SBTarget& rhs) 68 { 69 if (this != &rhs) 70 { 71 m_opaque_sp = rhs.m_opaque_sp; 72 } 73 return *this; 74 } 75 76 77 //---------------------------------------------------------------------- 78 // Destructor 79 //---------------------------------------------------------------------- 80 SBTarget::~SBTarget() 81 { 82 } 83 84 bool 85 SBTarget::IsValid () const 86 { 87 return m_opaque_sp.get() != NULL; 88 } 89 90 SBProcess 91 SBTarget::GetProcess () 92 { 93 SBProcess sb_process; 94 if (m_opaque_sp) 95 sb_process.SetProcess (m_opaque_sp->GetProcessSP()); 96 return sb_process; 97 } 98 99 SBDebugger 100 SBTarget::GetDebugger () const 101 { 102 SBDebugger debugger; 103 if (m_opaque_sp) 104 debugger.reset (m_opaque_sp->GetDebugger().GetSP()); 105 return debugger; 106 } 107 108 109 // DEPRECATED 110 SBProcess 111 SBTarget::CreateProcess () 112 { 113 SBProcess sb_process; 114 115 if (m_opaque_sp) 116 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener())); 117 118 return sb_process; 119 } 120 121 122 SBProcess 123 SBTarget::LaunchProcess 124 ( 125 char const **argv, 126 char const **envp, 127 const char *tty, 128 uint32_t launch_flags, 129 bool stop_at_entry 130 ) 131 { 132 SBError sb_error; 133 return Launch (argv, envp, tty, launch_flags, stop_at_entry, sb_error); 134 } 135 136 SBProcess 137 SBTarget::Launch 138 ( 139 char const **argv, 140 char const **envp, 141 const char *tty, 142 uint32_t launch_flags, 143 bool stop_at_entry, 144 SBError &error 145 ) 146 { 147 SBProcess sb_process; 148 if (m_opaque_sp) 149 { 150 // DEPRECATED, this will change when CreateProcess is removed... 151 if (m_opaque_sp->GetProcessSP()) 152 { 153 sb_process.SetProcess(m_opaque_sp->GetProcessSP()); 154 } 155 else 156 { 157 // When launching, we always want to create a new process When 158 // SBTarget::CreateProcess is removed, this will always happen. 159 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener())); 160 } 161 162 if (sb_process.IsValid()) 163 { 164 error.SetError (sb_process->Launch (argv, envp, launch_flags, tty, tty, tty)); 165 if (error.Success()) 166 { 167 // We we are stopping at the entry point, we can return now! 168 if (stop_at_entry) 169 return sb_process; 170 171 // Make sure we are stopped at the entry 172 StateType state = sb_process->WaitForProcessToStop (NULL); 173 if (state == eStateStopped) 174 { 175 // resume the process to skip the entry point 176 error.SetError (sb_process->Resume()); 177 if (error.Success()) 178 { 179 // If we are doing synchronous mode, then wait for the 180 // process to stop yet again! 181 if (m_opaque_sp->GetDebugger().GetAsyncExecution () == false) 182 sb_process->WaitForProcessToStop (NULL); 183 } 184 } 185 } 186 } 187 else 188 { 189 error.SetErrorString ("unable to create lldb_private::Process"); 190 } 191 } 192 else 193 { 194 error.SetErrorString ("SBTarget is invalid"); 195 } 196 return sb_process; 197 } 198 199 200 lldb::SBProcess 201 SBTarget::AttachToProcessWithID 202 ( 203 lldb::pid_t pid,// The process ID to attach to 204 SBError& error // An error explaining what went wrong if attach fails 205 ) 206 { 207 SBProcess sb_process; 208 if (m_opaque_sp) 209 { 210 // DEPRECATED, this will change when CreateProcess is removed... 211 if (m_opaque_sp->GetProcessSP()) 212 { 213 sb_process.SetProcess(m_opaque_sp->GetProcessSP()); 214 } 215 else 216 { 217 // When launching, we always want to create a new process When 218 // SBTarget::CreateProcess is removed, this will always happen. 219 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener())); 220 } 221 222 if (sb_process.IsValid()) 223 { 224 error.SetError (sb_process->Attach (pid)); 225 } 226 else 227 { 228 error.SetErrorString ("unable to create lldb_private::Process"); 229 } 230 } 231 else 232 { 233 error.SetErrorString ("SBTarget is invalid"); 234 } 235 return sb_process; 236 237 } 238 239 lldb::SBProcess 240 SBTarget::AttachToProcessWithName 241 ( 242 const char *name, // basename of process to attach to 243 bool wait_for, // if true wait for a new instance of "name" to be launched 244 SBError& error // An error explaining what went wrong if attach fails 245 ) 246 { 247 SBProcess sb_process; 248 if (m_opaque_sp) 249 { 250 // DEPRECATED, this will change when CreateProcess is removed... 251 if (m_opaque_sp->GetProcessSP()) 252 { 253 sb_process.SetProcess(m_opaque_sp->GetProcessSP()); 254 } 255 else 256 { 257 // When launching, we always want to create a new process When 258 // SBTarget::CreateProcess is removed, this will always happen. 259 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener())); 260 } 261 262 if (sb_process.IsValid()) 263 { 264 error.SetError (sb_process->Attach (name, wait_for)); 265 } 266 else 267 { 268 error.SetErrorString ("unable to create lldb_private::Process"); 269 } 270 } 271 else 272 { 273 error.SetErrorString ("SBTarget is invalid"); 274 } 275 return sb_process; 276 277 } 278 279 SBFileSpec 280 SBTarget::GetExecutable () 281 { 282 SBFileSpec exe_file_spec; 283 if (m_opaque_sp) 284 { 285 ModuleSP exe_module_sp (m_opaque_sp->GetExecutableModule ()); 286 if (exe_module_sp) 287 exe_file_spec.SetFileSpec (exe_module_sp->GetFileSpec()); 288 } 289 return exe_file_spec; 290 } 291 292 293 bool 294 SBTarget::DeleteTargetFromList (TargetList *list) 295 { 296 if (m_opaque_sp) 297 return list->DeleteTarget (m_opaque_sp); 298 else 299 return false; 300 } 301 302 bool 303 SBTarget::operator == (const SBTarget &rhs) const 304 { 305 return m_opaque_sp.get() == rhs.m_opaque_sp.get(); 306 } 307 308 bool 309 SBTarget::operator != (const SBTarget &rhs) const 310 { 311 return m_opaque_sp.get() != rhs.m_opaque_sp.get(); 312 } 313 314 lldb_private::Target * 315 SBTarget::operator ->() const 316 { 317 return m_opaque_sp.get(); 318 } 319 320 lldb_private::Target * 321 SBTarget::get() const 322 { 323 return m_opaque_sp.get(); 324 } 325 326 void 327 SBTarget::reset (const lldb::TargetSP& target_sp) 328 { 329 m_opaque_sp = target_sp; 330 } 331 332 SBBreakpoint 333 SBTarget::BreakpointCreateByLocation (const char *file, uint32_t line) 334 { 335 SBBreakpoint sb_bp; 336 if (file != NULL && line != 0) 337 sb_bp = BreakpointCreateByLocation (SBFileSpec (file), line); 338 return sb_bp; 339 } 340 341 SBBreakpoint 342 SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec, uint32_t line) 343 { 344 SBBreakpoint sb_bp; 345 if (m_opaque_sp.get() && line != 0) 346 *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, *sb_file_spec, line, true, false); 347 return sb_bp; 348 } 349 350 SBBreakpoint 351 SBTarget::BreakpointCreateByName (const char *symbol_name, const char *module_name) 352 { 353 SBBreakpoint sb_bp; 354 if (m_opaque_sp.get() && symbol_name && symbol_name[0]) 355 { 356 if (module_name && module_name[0]) 357 { 358 FileSpec module_file_spec(module_name, false); 359 *sb_bp = m_opaque_sp->CreateBreakpoint (&module_file_spec, symbol_name, eFunctionNameTypeFull | eFunctionNameTypeBase, false); 360 } 361 else 362 { 363 *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, symbol_name, eFunctionNameTypeFull | eFunctionNameTypeBase, false); 364 } 365 } 366 return sb_bp; 367 } 368 369 SBBreakpoint 370 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex, const char *module_name) 371 { 372 SBBreakpoint sb_bp; 373 if (m_opaque_sp.get() && symbol_name_regex && symbol_name_regex[0]) 374 { 375 RegularExpression regexp(symbol_name_regex); 376 377 if (module_name && module_name[0]) 378 { 379 FileSpec module_file_spec(module_name, false); 380 381 *sb_bp = m_opaque_sp->CreateBreakpoint (&module_file_spec, regexp, false); 382 } 383 else 384 { 385 *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, regexp, false); 386 } 387 } 388 return sb_bp; 389 } 390 391 392 393 SBBreakpoint 394 SBTarget::BreakpointCreateByAddress (addr_t address) 395 { 396 SBBreakpoint sb_bp; 397 if (m_opaque_sp.get()) 398 *sb_bp = m_opaque_sp->CreateBreakpoint (address, false); 399 return sb_bp; 400 } 401 402 SBBreakpoint 403 SBTarget::FindBreakpointByID (break_id_t bp_id) 404 { 405 SBBreakpoint sb_breakpoint; 406 if (m_opaque_sp && bp_id != LLDB_INVALID_BREAK_ID) 407 *sb_breakpoint = m_opaque_sp->GetBreakpointByID (bp_id); 408 return sb_breakpoint; 409 } 410 411 uint32_t 412 SBTarget::GetNumBreakpoints () const 413 { 414 if (m_opaque_sp) 415 return m_opaque_sp->GetBreakpointList().GetSize(); 416 return 0; 417 } 418 419 SBBreakpoint 420 SBTarget::GetBreakpointAtIndex (uint32_t idx) const 421 { 422 SBBreakpoint sb_breakpoint; 423 if (m_opaque_sp) 424 *sb_breakpoint = m_opaque_sp->GetBreakpointList().GetBreakpointAtIndex(idx); 425 return sb_breakpoint; 426 } 427 428 bool 429 SBTarget::BreakpointDelete (break_id_t bp_id) 430 { 431 if (m_opaque_sp) 432 return m_opaque_sp->RemoveBreakpointByID (bp_id); 433 return false; 434 } 435 436 bool 437 SBTarget::EnableAllBreakpoints () 438 { 439 if (m_opaque_sp) 440 { 441 m_opaque_sp->EnableAllBreakpoints (); 442 return true; 443 } 444 return false; 445 } 446 447 bool 448 SBTarget::DisableAllBreakpoints () 449 { 450 if (m_opaque_sp) 451 { 452 m_opaque_sp->DisableAllBreakpoints (); 453 return true; 454 } 455 return false; 456 } 457 458 bool 459 SBTarget::DeleteAllBreakpoints () 460 { 461 if (m_opaque_sp) 462 { 463 m_opaque_sp->RemoveAllBreakpoints (); 464 return true; 465 } 466 return false; 467 } 468 469 470 uint32_t 471 SBTarget::GetNumModules () const 472 { 473 if (m_opaque_sp) 474 return m_opaque_sp->GetImages().GetSize(); 475 return 0; 476 } 477 478 void 479 SBTarget::Clear () 480 { 481 m_opaque_sp.reset(); 482 } 483 484 485 SBModule 486 SBTarget::FindModule (const SBFileSpec &sb_file_spec) 487 { 488 SBModule sb_module; 489 if (m_opaque_sp && sb_file_spec.IsValid()) 490 sb_module.SetModule (m_opaque_sp->GetImages().FindFirstModuleForFileSpec (*sb_file_spec, NULL)); 491 return sb_module; 492 } 493 494 SBModule 495 SBTarget::GetModuleAtIndex (uint32_t idx) 496 { 497 SBModule sb_module; 498 if (m_opaque_sp) 499 sb_module.SetModule(m_opaque_sp->GetImages().GetModuleAtIndex(idx)); 500 return sb_module; 501 } 502 503 504 SBBroadcaster 505 SBTarget::GetBroadcaster () const 506 { 507 SBBroadcaster broadcaster(m_opaque_sp.get(), false); 508 return broadcaster; 509 } 510 511 void 512 SBTarget::Disassemble (lldb::addr_t start_addr, lldb::addr_t end_addr, const char *module_name) 513 { 514 if (start_addr == LLDB_INVALID_ADDRESS) 515 return; 516 517 FILE *out = m_opaque_sp->GetDebugger().GetOutputFileHandle(); 518 if (out == NULL) 519 return; 520 521 if (m_opaque_sp) 522 { 523 ModuleSP module_sp; 524 if (module_name != NULL) 525 { 526 FileSpec module_file_spec (module_name, false); 527 module_sp = m_opaque_sp->GetImages().FindFirstModuleForFileSpec (module_file_spec, NULL); 528 } 529 530 AddressRange range; 531 532 // Make sure the process object is alive if we have one (it might be 533 // created but we might not be launched yet). 534 535 Process *sb_process = m_opaque_sp->GetProcessSP().get(); 536 if (sb_process && !sb_process->IsAlive()) 537 sb_process = NULL; 538 539 // If we are given a module, then "start_addr" is a file address in 540 // that module. 541 if (module_sp) 542 { 543 if (!module_sp->ResolveFileAddress (start_addr, range.GetBaseAddress())) 544 range.GetBaseAddress().SetOffset(start_addr); 545 } 546 else if (m_opaque_sp->GetSectionLoadList().IsEmpty() == false) 547 { 548 // We don't have a module, se we need to figure out if "start_addr" 549 // resolves to anything in a running process. 550 if (!m_opaque_sp->GetSectionLoadList().ResolveLoadAddress (start_addr, range.GetBaseAddress())) 551 range.GetBaseAddress().SetOffset(start_addr); 552 } 553 else 554 { 555 if (m_opaque_sp->GetImages().ResolveFileAddress (start_addr, range.GetBaseAddress())) 556 range.GetBaseAddress().SetOffset(start_addr); 557 } 558 559 // For now, we need a process; the disassembly functions insist. If we don't have one already, 560 // make one. 561 562 ExecutionContext exe_ctx; 563 564 if (sb_process) 565 sb_process->CalculateExecutionContext(exe_ctx); 566 else 567 m_opaque_sp->CalculateExecutionContext(exe_ctx); 568 569 if (end_addr == LLDB_INVALID_ADDRESS || end_addr < start_addr) 570 range.SetByteSize( DEFAULT_DISASM_BYTE_SIZE); 571 else 572 range.SetByteSize(end_addr - start_addr); 573 574 StreamFile out_stream (out); 575 576 Disassembler::Disassemble (m_opaque_sp->GetDebugger(), 577 m_opaque_sp->GetArchitecture(), 578 exe_ctx, 579 range, 580 3, 581 false, 582 out_stream); 583 } 584 } 585 586 void 587 SBTarget::Disassemble (const char *function_name, const char *module_name) 588 { 589 if (function_name == NULL) 590 return; 591 592 FILE *out = m_opaque_sp->GetDebugger().GetOutputFileHandle(); 593 if (out == NULL) 594 return; 595 596 if (m_opaque_sp) 597 { 598 Disassembler *disassembler = Disassembler::FindPlugin (m_opaque_sp->GetArchitecture()); 599 if (disassembler == NULL) 600 return; 601 602 ModuleSP module_sp; 603 if (module_name != NULL) 604 { 605 FileSpec module_file_spec (module_name, false); 606 module_sp = m_opaque_sp->GetImages().FindFirstModuleForFileSpec (module_file_spec, NULL); 607 } 608 609 ExecutionContext exe_ctx; 610 611 // Make sure the process object is alive if we have one (it might be 612 // created but we might not be launched yet). 613 Process *sb_process = m_opaque_sp->GetProcessSP().get(); 614 if (sb_process && !sb_process->IsAlive()) 615 sb_process = NULL; 616 617 if (sb_process) 618 sb_process->CalculateExecutionContext(exe_ctx); 619 else 620 m_opaque_sp->CalculateExecutionContext(exe_ctx); 621 622 623 StreamFile out_stream (out); 624 625 Disassembler::Disassemble (m_opaque_sp->GetDebugger(), 626 m_opaque_sp->GetArchitecture(), 627 exe_ctx, 628 ConstString (function_name), 629 module_sp.get(), 630 3, 631 false, 632 out_stream); 633 } 634 } 635 636 bool 637 SBTarget::GetDescription (SBStream &description) 638 { 639 if (m_opaque_sp) 640 { 641 description.ref(); 642 m_opaque_sp->Dump (description.get()); 643 } 644 else 645 description.Printf ("No value"); 646 647 return true; 648 } 649