1 //===-- PlatformWindows.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 "PlatformWindows.h" 11 12 // C Includes 13 #include <stdio.h> 14 #if defined (_WIN32) 15 #include "lldb/Host/windows/windows.h" 16 #include <winsock2.h> 17 #endif 18 19 // C++ Includes 20 // Other libraries and framework includes 21 // Project includes 22 #include "lldb/Core/Error.h" 23 #include "lldb/Core/Debugger.h" 24 #include "lldb/Core/PluginManager.h" 25 #include "lldb/Host/Host.h" 26 #include "lldb/Core/ModuleSpec.h" 27 #include "lldb/Core/Module.h" 28 #include "lldb/Breakpoint/BreakpointLocation.h" 29 30 using namespace lldb; 31 using namespace lldb_private; 32 33 static uint32_t g_initialize_count = 0; 34 35 namespace 36 { 37 class SupportedArchList 38 { 39 public: 40 SupportedArchList() 41 { 42 AddArch(ArchSpec("i686-pc-windows")); 43 AddArch(Host::GetArchitecture(Host::eSystemDefaultArchitecture)); 44 AddArch(Host::GetArchitecture(Host::eSystemDefaultArchitecture32)); 45 AddArch(Host::GetArchitecture(Host::eSystemDefaultArchitecture64)); 46 AddArch(ArchSpec("i386-pc-windows")); 47 } 48 49 size_t Count() const { return m_archs.size(); } 50 51 const ArchSpec& operator[](int idx) { return m_archs[idx]; } 52 53 private: 54 void AddArch(const ArchSpec& spec) 55 { 56 auto iter = std::find_if( 57 m_archs.begin(), m_archs.end(), 58 [spec](const ArchSpec& rhs) { return spec.IsExactMatch(rhs); }); 59 if (iter != m_archs.end()) 60 return; 61 if (spec.IsValid()) 62 m_archs.push_back(spec); 63 } 64 65 std::vector<ArchSpec> m_archs; 66 }; 67 } 68 69 Platform * 70 PlatformWindows::CreateInstance (bool force, const lldb_private::ArchSpec *arch) 71 { 72 // The only time we create an instance is when we are creating a remote 73 // windows platform 74 const bool is_host = false; 75 76 bool create = force; 77 if (create == false && arch && arch->IsValid()) 78 { 79 const llvm::Triple &triple = arch->GetTriple(); 80 switch (triple.getVendor()) 81 { 82 case llvm::Triple::PC: 83 create = true; 84 break; 85 86 case llvm::Triple::UnknownArch: 87 create = !arch->TripleVendorWasSpecified(); 88 break; 89 90 default: 91 break; 92 } 93 94 if (create) 95 { 96 switch (triple.getOS()) 97 { 98 case llvm::Triple::Win32: 99 case llvm::Triple::MinGW32: 100 break; 101 102 case llvm::Triple::UnknownOS: 103 create = arch->TripleOSWasSpecified(); 104 break; 105 106 default: 107 create = false; 108 break; 109 } 110 } 111 } 112 if (create) 113 return new PlatformWindows (is_host); 114 return NULL; 115 116 } 117 118 lldb_private::ConstString 119 PlatformWindows::GetPluginNameStatic(bool is_host) 120 { 121 if (is_host) 122 { 123 static ConstString g_host_name(Platform::GetHostPlatformName ()); 124 return g_host_name; 125 } 126 else 127 { 128 static ConstString g_remote_name("remote-windows"); 129 return g_remote_name; 130 } 131 } 132 133 const char * 134 PlatformWindows::GetPluginDescriptionStatic(bool is_host) 135 { 136 return is_host ? 137 "Local Windows user platform plug-in." : 138 "Remote Windows user platform plug-in."; 139 } 140 141 lldb_private::ConstString 142 PlatformWindows::GetPluginName(void) 143 { 144 return GetPluginNameStatic(IsHost()); 145 } 146 147 void 148 PlatformWindows::Initialize(void) 149 { 150 if (g_initialize_count++ == 0) 151 { 152 #if defined (_WIN32) 153 WSADATA dummy; 154 WSAStartup(MAKEWORD(2,2), &dummy); 155 // Force a host flag to true for the default platform object. 156 PlatformSP default_platform_sp (new PlatformWindows(true)); 157 default_platform_sp->SetSystemArchitecture (Host::GetArchitecture()); 158 Platform::SetDefaultPlatform (default_platform_sp); 159 #endif 160 PluginManager::RegisterPlugin(PlatformWindows::GetPluginNameStatic(false), 161 PlatformWindows::GetPluginDescriptionStatic(false), 162 PlatformWindows::CreateInstance); 163 } 164 } 165 166 void 167 PlatformWindows::Terminate( void ) 168 { 169 if (g_initialize_count > 0) 170 { 171 if (--g_initialize_count == 0) 172 { 173 #ifdef _WIN32 174 WSACleanup(); 175 #endif 176 PluginManager::UnregisterPlugin (PlatformWindows::CreateInstance); 177 } 178 } 179 } 180 181 //------------------------------------------------------------------ 182 /// Default Constructor 183 //------------------------------------------------------------------ 184 PlatformWindows::PlatformWindows (bool is_host) : 185 Platform(is_host) 186 { 187 } 188 189 //------------------------------------------------------------------ 190 /// Destructor. 191 /// 192 /// The destructor is virtual since this class is designed to be 193 /// inherited from by the plug-in instance. 194 //------------------------------------------------------------------ 195 PlatformWindows::~PlatformWindows() 196 { 197 } 198 199 Error 200 PlatformWindows::ResolveExecutable (const FileSpec &exe_file, 201 const ArchSpec &exe_arch, 202 lldb::ModuleSP &exe_module_sp, 203 const FileSpecList *module_search_paths_ptr) 204 { 205 Error error; 206 // Nothing special to do here, just use the actual file and architecture 207 208 char exe_path[PATH_MAX]; 209 FileSpec resolved_exe_file (exe_file); 210 211 if (IsHost()) 212 { 213 // if we cant resolve the executable loation based on the current path variables 214 if (!resolved_exe_file.Exists()) 215 { 216 exe_file.GetPath(exe_path, sizeof(exe_path)); 217 resolved_exe_file.SetFile(exe_path, true); 218 } 219 220 if (!resolved_exe_file.Exists()) 221 resolved_exe_file.ResolveExecutableLocation (); 222 223 if (resolved_exe_file.Exists()) 224 error.Clear(); 225 else 226 { 227 exe_file.GetPath(exe_path, sizeof(exe_path)); 228 error.SetErrorStringWithFormat("unable to find executable for '%s'", exe_path); 229 } 230 } 231 else 232 { 233 if (m_remote_platform_sp) 234 { 235 error = m_remote_platform_sp->ResolveExecutable (exe_file, 236 exe_arch, 237 exe_module_sp, 238 NULL); 239 } 240 else 241 { 242 // We may connect to a process and use the provided executable (Don't use local $PATH). 243 if (resolved_exe_file.Exists()) 244 error.Clear(); 245 else 246 error.SetErrorStringWithFormat("the platform is not currently connected, and '%s' doesn't exist in the system root.", exe_path); 247 } 248 } 249 250 if (error.Success()) 251 { 252 ModuleSpec module_spec (resolved_exe_file, exe_arch); 253 if (exe_arch.IsValid()) 254 { 255 error = ModuleList::GetSharedModule (module_spec, 256 exe_module_sp, 257 NULL, 258 NULL, 259 NULL); 260 261 if (!exe_module_sp || exe_module_sp->GetObjectFile() == NULL) 262 { 263 exe_module_sp.reset(); 264 error.SetErrorStringWithFormat ("'%s' doesn't contain the architecture %s", 265 exe_file.GetPath().c_str(), 266 exe_arch.GetArchitectureName()); 267 } 268 } 269 else 270 { 271 // No valid architecture was specified, ask the platform for 272 // the architectures that we should be using (in the correct order) 273 // and see if we can find a match that way 274 StreamString arch_names; 275 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, module_spec.GetArchitecture()); ++idx) 276 { 277 error = ModuleList::GetSharedModule (module_spec, 278 exe_module_sp, 279 NULL, 280 NULL, 281 NULL); 282 // Did we find an executable using one of the 283 if (error.Success()) 284 { 285 if (exe_module_sp && exe_module_sp->GetObjectFile()) 286 break; 287 else 288 error.SetErrorToGenericError(); 289 } 290 291 if (idx > 0) 292 arch_names.PutCString (", "); 293 arch_names.PutCString (module_spec.GetArchitecture().GetArchitectureName()); 294 } 295 296 if (error.Fail() || !exe_module_sp) 297 { 298 error.SetErrorStringWithFormat ("'%s' doesn't contain any '%s' platform architectures: %s", 299 exe_file.GetPath().c_str(), 300 GetPluginName().GetCString(), 301 arch_names.GetString().c_str()); 302 } 303 } 304 } 305 306 return error; 307 } 308 309 size_t 310 PlatformWindows::GetSoftwareBreakpointTrapOpcode (Target &target, BreakpointSite *bp_site) 311 { 312 ArchSpec arch = target.GetArchitecture(); 313 const uint8_t *trap_opcode = NULL; 314 size_t trap_opcode_size = 0; 315 316 switch (arch.GetMachine()) 317 { 318 case llvm::Triple::x86: 319 case llvm::Triple::x86_64: 320 { 321 static const uint8_t g_i386_opcode[] = { 0xCC }; 322 trap_opcode = g_i386_opcode; 323 trap_opcode_size = sizeof(g_i386_opcode); 324 } 325 break; 326 327 case llvm::Triple::hexagon: 328 { 329 static const uint8_t g_hex_opcode[] = { 0x0c, 0xdb, 0x00, 0x54 }; 330 trap_opcode = g_hex_opcode; 331 trap_opcode_size = sizeof(g_hex_opcode); 332 } 333 break; 334 default: 335 llvm_unreachable("Unhandled architecture in PlatformWindows::GetSoftwareBreakpointTrapOpcode()"); 336 break; 337 } 338 339 if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size)) 340 return trap_opcode_size; 341 342 return 0; 343 } 344 345 bool 346 PlatformWindows::GetRemoteOSVersion () 347 { 348 if (m_remote_platform_sp) 349 return m_remote_platform_sp->GetOSVersion (m_major_os_version, 350 m_minor_os_version, 351 m_update_os_version); 352 return false; 353 } 354 355 bool 356 PlatformWindows::GetRemoteOSBuildString (std::string &s) 357 { 358 if (m_remote_platform_sp) 359 return m_remote_platform_sp->GetRemoteOSBuildString (s); 360 s.clear(); 361 return false; 362 } 363 364 bool 365 PlatformWindows::GetRemoteOSKernelDescription (std::string &s) 366 { 367 if (m_remote_platform_sp) 368 return m_remote_platform_sp->GetRemoteOSKernelDescription (s); 369 s.clear(); 370 return false; 371 } 372 373 // Remote Platform subclasses need to override this function 374 ArchSpec 375 PlatformWindows::GetRemoteSystemArchitecture () 376 { 377 if (m_remote_platform_sp) 378 return m_remote_platform_sp->GetRemoteSystemArchitecture (); 379 return ArchSpec(); 380 } 381 382 const char * 383 PlatformWindows::GetHostname () 384 { 385 if (IsHost()) 386 return Platform::GetHostname(); 387 388 if (m_remote_platform_sp) 389 return m_remote_platform_sp->GetHostname (); 390 return NULL; 391 } 392 393 bool 394 PlatformWindows::IsConnected () const 395 { 396 if (IsHost()) 397 return true; 398 else if (m_remote_platform_sp) 399 return m_remote_platform_sp->IsConnected(); 400 return false; 401 } 402 403 Error 404 PlatformWindows::ConnectRemote (Args& args) 405 { 406 Error error; 407 if (IsHost()) 408 { 409 error.SetErrorStringWithFormat ("can't connect to the host platform '%s', always connected", GetPluginName().AsCString() ); 410 } 411 else 412 { 413 if (!m_remote_platform_sp) 414 m_remote_platform_sp = Platform::Create ("remote-gdb-server", error); 415 416 if (m_remote_platform_sp) 417 { 418 if (error.Success()) 419 { 420 if (m_remote_platform_sp) 421 { 422 error = m_remote_platform_sp->ConnectRemote (args); 423 } 424 else 425 { 426 error.SetErrorString ("\"platform connect\" takes a single argument: <connect-url>"); 427 } 428 } 429 } 430 else 431 error.SetErrorString ("failed to create a 'remote-gdb-server' platform"); 432 433 if (error.Fail()) 434 m_remote_platform_sp.reset(); 435 } 436 437 return error; 438 } 439 440 Error 441 PlatformWindows::DisconnectRemote () 442 { 443 Error error; 444 445 if (IsHost()) 446 { 447 error.SetErrorStringWithFormat ("can't disconnect from the host platform '%s', always connected", GetPluginName().AsCString() ); 448 } 449 else 450 { 451 if (m_remote_platform_sp) 452 error = m_remote_platform_sp->DisconnectRemote (); 453 else 454 error.SetErrorString ("the platform is not currently connected"); 455 } 456 return error; 457 } 458 459 bool 460 PlatformWindows::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) 461 { 462 bool success = false; 463 if (IsHost()) 464 { 465 success = Platform::GetProcessInfo (pid, process_info); 466 } 467 else if (m_remote_platform_sp) 468 { 469 success = m_remote_platform_sp->GetProcessInfo (pid, process_info); 470 } 471 return success; 472 } 473 474 uint32_t 475 PlatformWindows::FindProcesses (const ProcessInstanceInfoMatch &match_info, 476 ProcessInstanceInfoList &process_infos) 477 { 478 uint32_t match_count = 0; 479 if (IsHost()) 480 { 481 // Let the base class figure out the host details 482 match_count = Platform::FindProcesses (match_info, process_infos); 483 } 484 else 485 { 486 // If we are remote, we can only return results if we are connected 487 if (m_remote_platform_sp) 488 match_count = m_remote_platform_sp->FindProcesses (match_info, process_infos); 489 } 490 return match_count; 491 } 492 493 Error 494 PlatformWindows::LaunchProcess (ProcessLaunchInfo &launch_info) 495 { 496 Error error; 497 if (IsHost()) 498 { 499 error = Platform::LaunchProcess (launch_info); 500 } 501 else 502 { 503 if (m_remote_platform_sp) 504 error = m_remote_platform_sp->LaunchProcess (launch_info); 505 else 506 error.SetErrorString ("the platform is not currently connected"); 507 } 508 return error; 509 } 510 511 lldb::ProcessSP 512 PlatformWindows::Attach(ProcessAttachInfo &attach_info, 513 Debugger &debugger, 514 Target *target, 515 Listener &listener, 516 Error &error) 517 { 518 lldb::ProcessSP process_sp; 519 if (IsHost()) 520 { 521 if (target == NULL) 522 { 523 TargetSP new_target_sp; 524 FileSpec emptyFileSpec; 525 ArchSpec emptyArchSpec; 526 527 error = debugger.GetTargetList().CreateTarget (debugger, 528 NULL, 529 NULL, 530 false, 531 NULL, 532 new_target_sp); 533 target = new_target_sp.get(); 534 } 535 else 536 error.Clear(); 537 538 if (target && error.Success()) 539 { 540 debugger.GetTargetList().SetSelectedTarget(target); 541 // The Windows platform always currently uses the GDB remote debugger plug-in 542 // so even when debugging locally we are debugging remotely! 543 // Just like the darwin plugin. 544 process_sp = target->CreateProcess (listener, "gdb-remote", NULL); 545 546 if (process_sp) 547 error = process_sp->Attach (attach_info); 548 } 549 } 550 else 551 { 552 if (m_remote_platform_sp) 553 process_sp = m_remote_platform_sp->Attach (attach_info, debugger, target, listener, error); 554 else 555 error.SetErrorString ("the platform is not currently connected"); 556 } 557 return process_sp; 558 } 559 560 const char * 561 PlatformWindows::GetUserName (uint32_t uid) 562 { 563 // Check the cache in Platform in case we have already looked this uid up 564 const char *user_name = Platform::GetUserName(uid); 565 if (user_name) 566 return user_name; 567 568 if (IsRemote() && m_remote_platform_sp) 569 return m_remote_platform_sp->GetUserName(uid); 570 return NULL; 571 } 572 573 const char * 574 PlatformWindows::GetGroupName (uint32_t gid) 575 { 576 const char *group_name = Platform::GetGroupName(gid); 577 if (group_name) 578 return group_name; 579 580 if (IsRemote() && m_remote_platform_sp) 581 return m_remote_platform_sp->GetGroupName(gid); 582 return NULL; 583 } 584 585 Error 586 PlatformWindows::GetFileWithUUID (const FileSpec &platform_file, 587 const UUID *uuid_ptr, 588 FileSpec &local_file) 589 { 590 if (IsRemote()) 591 { 592 if (m_remote_platform_sp) 593 return m_remote_platform_sp->GetFileWithUUID (platform_file, uuid_ptr, local_file); 594 } 595 596 // Default to the local case 597 local_file = platform_file; 598 return Error(); 599 } 600 601 Error 602 PlatformWindows::GetSharedModule (const ModuleSpec &module_spec, 603 ModuleSP &module_sp, 604 const FileSpecList *module_search_paths_ptr, 605 ModuleSP *old_module_sp_ptr, 606 bool *did_create_ptr) 607 { 608 Error error; 609 module_sp.reset(); 610 611 if (IsRemote()) 612 { 613 // If we have a remote platform always, let it try and locate 614 // the shared module first. 615 if (m_remote_platform_sp) 616 { 617 error = m_remote_platform_sp->GetSharedModule (module_spec, 618 module_sp, 619 module_search_paths_ptr, 620 old_module_sp_ptr, 621 did_create_ptr); 622 } 623 } 624 625 if (!module_sp) 626 { 627 // Fall back to the local platform and find the file locally 628 error = Platform::GetSharedModule (module_spec, 629 module_sp, 630 module_search_paths_ptr, 631 old_module_sp_ptr, 632 did_create_ptr); 633 } 634 if (module_sp) 635 module_sp->SetPlatformFileSpec(module_spec.GetFileSpec()); 636 return error; 637 } 638 639 bool 640 PlatformWindows::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch) 641 { 642 static SupportedArchList architectures; 643 644 if (idx >= architectures.Count()) 645 return false; 646 arch = architectures[idx]; 647 return true; 648 } 649 650 void 651 PlatformWindows::GetStatus (Stream &strm) 652 { 653 Platform::GetStatus(strm); 654 655 #ifdef _WIN32 656 uint32_t major; 657 uint32_t minor; 658 uint32_t update; 659 if (!Host::GetOSVersion(major, minor, update)) 660 { 661 strm << "Windows"; 662 return; 663 } 664 665 strm << "Host: Windows " << major 666 << '.' << minor 667 << " Build: " << update << '\n'; 668 #endif 669 } 670