1 //===-- PlatformLinux.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 "PlatformLinux.h" 11 #include "lldb/Host/Config.h" 12 13 // C Includes 14 #include <stdio.h> 15 #ifndef LLDB_DISABLE_POSIX 16 #include <sys/utsname.h> 17 #endif 18 19 // C++ Includes 20 // Other libraries and framework includes 21 // Project includes 22 #include "lldb/Core/Debugger.h" 23 #include "lldb/Core/Log.h" 24 #include "lldb/Core/PluginManager.h" 25 #include "lldb/Core/State.h" 26 #include "lldb/Host/FileSpec.h" 27 #include "lldb/Host/HostInfo.h" 28 #include "lldb/Target/Process.h" 29 #include "lldb/Target/Target.h" 30 #include "lldb/Utility/Error.h" 31 #include "lldb/Utility/StreamString.h" 32 33 // Define these constants from Linux mman.h for use when targeting 34 // remote linux systems even when host has different values. 35 #define MAP_PRIVATE 2 36 #define MAP_ANON 0x20 37 38 using namespace lldb; 39 using namespace lldb_private; 40 using namespace lldb_private::platform_linux; 41 42 static uint32_t g_initialize_count = 0; 43 44 //------------------------------------------------------------------ 45 46 PlatformSP PlatformLinux::CreateInstance(bool force, const ArchSpec *arch) { 47 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM)); 48 if (log) { 49 const char *arch_name; 50 if (arch && arch->GetArchitectureName()) 51 arch_name = arch->GetArchitectureName(); 52 else 53 arch_name = "<null>"; 54 55 const char *triple_cstr = 56 arch ? arch->GetTriple().getTriple().c_str() : "<null>"; 57 58 log->Printf("PlatformLinux::%s(force=%s, arch={%s,%s})", __FUNCTION__, 59 force ? "true" : "false", arch_name, triple_cstr); 60 } 61 62 bool create = force; 63 if (create == false && arch && arch->IsValid()) { 64 const llvm::Triple &triple = arch->GetTriple(); 65 switch (triple.getOS()) { 66 case llvm::Triple::Linux: 67 create = true; 68 break; 69 70 #if defined(__linux__) 71 // Only accept "unknown" for the OS if the host is linux and 72 // it "unknown" wasn't specified (it was just returned because it 73 // was NOT specified) 74 case llvm::Triple::OSType::UnknownOS: 75 create = !arch->TripleOSWasSpecified(); 76 break; 77 #endif 78 default: 79 break; 80 } 81 } 82 83 if (create) { 84 if (log) 85 log->Printf("PlatformLinux::%s() creating remote-linux platform", 86 __FUNCTION__); 87 return PlatformSP(new PlatformLinux(false)); 88 } 89 90 if (log) 91 log->Printf( 92 "PlatformLinux::%s() aborting creation of remote-linux platform", 93 __FUNCTION__); 94 95 return PlatformSP(); 96 } 97 98 ConstString PlatformLinux::GetPluginNameStatic(bool is_host) { 99 if (is_host) { 100 static ConstString g_host_name(Platform::GetHostPlatformName()); 101 return g_host_name; 102 } else { 103 static ConstString g_remote_name("remote-linux"); 104 return g_remote_name; 105 } 106 } 107 108 const char *PlatformLinux::GetPluginDescriptionStatic(bool is_host) { 109 if (is_host) 110 return "Local Linux user platform plug-in."; 111 else 112 return "Remote Linux user platform plug-in."; 113 } 114 115 ConstString PlatformLinux::GetPluginName() { 116 return GetPluginNameStatic(IsHost()); 117 } 118 119 void PlatformLinux::Initialize() { 120 PlatformPOSIX::Initialize(); 121 122 if (g_initialize_count++ == 0) { 123 #if defined(__linux__) && !defined(__ANDROID__) 124 PlatformSP default_platform_sp(new PlatformLinux(true)); 125 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture()); 126 Platform::SetHostPlatform(default_platform_sp); 127 #endif 128 PluginManager::RegisterPlugin( 129 PlatformLinux::GetPluginNameStatic(false), 130 PlatformLinux::GetPluginDescriptionStatic(false), 131 PlatformLinux::CreateInstance, nullptr); 132 } 133 } 134 135 void PlatformLinux::Terminate() { 136 if (g_initialize_count > 0) { 137 if (--g_initialize_count == 0) { 138 PluginManager::UnregisterPlugin(PlatformLinux::CreateInstance); 139 } 140 } 141 142 PlatformPOSIX::Terminate(); 143 } 144 145 //------------------------------------------------------------------ 146 /// Default Constructor 147 //------------------------------------------------------------------ 148 PlatformLinux::PlatformLinux(bool is_host) 149 : PlatformPOSIX(is_host) // This is the local host platform 150 {} 151 152 PlatformLinux::~PlatformLinux() = default; 153 154 bool PlatformLinux::GetSupportedArchitectureAtIndex(uint32_t idx, 155 ArchSpec &arch) { 156 if (IsHost()) { 157 ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault); 158 if (hostArch.GetTriple().isOSLinux()) { 159 if (idx == 0) { 160 arch = hostArch; 161 return arch.IsValid(); 162 } else if (idx == 1) { 163 // If the default host architecture is 64-bit, look for a 32-bit variant 164 if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) { 165 arch = HostInfo::GetArchitecture(HostInfo::eArchKind32); 166 return arch.IsValid(); 167 } 168 } 169 } 170 } else { 171 if (m_remote_platform_sp) 172 return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch); 173 174 llvm::Triple triple; 175 // Set the OS to linux 176 triple.setOS(llvm::Triple::Linux); 177 // Set the architecture 178 switch (idx) { 179 case 0: 180 triple.setArchName("x86_64"); 181 break; 182 case 1: 183 triple.setArchName("i386"); 184 break; 185 case 2: 186 triple.setArchName("arm"); 187 break; 188 case 3: 189 triple.setArchName("aarch64"); 190 break; 191 case 4: 192 triple.setArchName("mips64"); 193 break; 194 case 5: 195 triple.setArchName("hexagon"); 196 break; 197 case 6: 198 triple.setArchName("mips"); 199 break; 200 case 7: 201 triple.setArchName("mips64el"); 202 break; 203 case 8: 204 triple.setArchName("mipsel"); 205 break; 206 case 9: 207 triple.setArchName("s390x"); 208 break; 209 default: 210 return false; 211 } 212 // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the 213 // vendor by 214 // calling triple.SetVendorName("unknown") so that it is a "unspecified 215 // unknown". 216 // This means when someone calls triple.GetVendorName() it will return an 217 // empty string 218 // which indicates that the vendor can be set when two architectures are 219 // merged 220 221 // Now set the triple into "arch" and return true 222 arch.SetTriple(triple); 223 return true; 224 } 225 return false; 226 } 227 228 void PlatformLinux::GetStatus(Stream &strm) { 229 Platform::GetStatus(strm); 230 231 #ifndef LLDB_DISABLE_POSIX 232 // Display local kernel information only when we are running in host mode. 233 // Otherwise, we would end up printing non-Linux information (when running 234 // on Mac OS for example). 235 if (IsHost()) { 236 struct utsname un; 237 238 if (uname(&un)) 239 return; 240 241 strm.Printf(" Kernel: %s\n", un.sysname); 242 strm.Printf(" Release: %s\n", un.release); 243 strm.Printf(" Version: %s\n", un.version); 244 } 245 #endif 246 } 247 248 int32_t 249 PlatformLinux::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) { 250 int32_t resume_count = 0; 251 252 // Always resume past the initial stop when we use eLaunchFlagDebug 253 if (launch_info.GetFlags().Test(eLaunchFlagDebug)) { 254 // Resume past the stop for the final exec into the true inferior. 255 ++resume_count; 256 } 257 258 // If we're not launching a shell, we're done. 259 const FileSpec &shell = launch_info.GetShell(); 260 if (!shell) 261 return resume_count; 262 263 std::string shell_string = shell.GetPath(); 264 // We're in a shell, so for sure we have to resume past the shell exec. 265 ++resume_count; 266 267 // Figure out what shell we're planning on using. 268 const char *shell_name = strrchr(shell_string.c_str(), '/'); 269 if (shell_name == NULL) 270 shell_name = shell_string.c_str(); 271 else 272 shell_name++; 273 274 if (strcmp(shell_name, "csh") == 0 || strcmp(shell_name, "tcsh") == 0 || 275 strcmp(shell_name, "zsh") == 0 || strcmp(shell_name, "sh") == 0) { 276 // These shells seem to re-exec themselves. Add another resume. 277 ++resume_count; 278 } 279 280 return resume_count; 281 } 282 283 bool PlatformLinux::CanDebugProcess() { 284 if (IsHost()) { 285 return true; 286 } else { 287 // If we're connected, we can debug. 288 return IsConnected(); 289 } 290 } 291 292 // For local debugging, Linux will override the debug logic to use llgs-launch 293 // rather than 294 // lldb-launch, llgs-attach. This differs from current lldb-launch, 295 // debugserver-attach 296 // approach on MacOSX. 297 lldb::ProcessSP 298 PlatformLinux::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger, 299 Target *target, // Can be NULL, if NULL create a new 300 // target, else use existing one 301 Error &error) { 302 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM)); 303 if (log) 304 log->Printf("PlatformLinux::%s entered (target %p)", __FUNCTION__, 305 static_cast<void *>(target)); 306 307 // If we're a remote host, use standard behavior from parent class. 308 if (!IsHost()) 309 return PlatformPOSIX::DebugProcess(launch_info, debugger, target, error); 310 311 // 312 // For local debugging, we'll insist on having ProcessGDBRemote create the 313 // process. 314 // 315 316 ProcessSP process_sp; 317 318 // Make sure we stop at the entry point 319 launch_info.GetFlags().Set(eLaunchFlagDebug); 320 321 // We always launch the process we are going to debug in a separate process 322 // group, since then we can handle ^C interrupts ourselves w/o having to worry 323 // about the target getting them as well. 324 launch_info.SetLaunchInSeparateProcessGroup(true); 325 326 // Ensure we have a target. 327 if (target == nullptr) { 328 if (log) 329 log->Printf("PlatformLinux::%s creating new target", __FUNCTION__); 330 331 TargetSP new_target_sp; 332 error = debugger.GetTargetList().CreateTarget(debugger, "", "", false, 333 nullptr, new_target_sp); 334 if (error.Fail()) { 335 if (log) 336 log->Printf("PlatformLinux::%s failed to create new target: %s", 337 __FUNCTION__, error.AsCString()); 338 return process_sp; 339 } 340 341 target = new_target_sp.get(); 342 if (!target) { 343 error.SetErrorString("CreateTarget() returned nullptr"); 344 if (log) 345 log->Printf("PlatformLinux::%s failed: %s", __FUNCTION__, 346 error.AsCString()); 347 return process_sp; 348 } 349 } else { 350 if (log) 351 log->Printf("PlatformLinux::%s using provided target", __FUNCTION__); 352 } 353 354 // Mark target as currently selected target. 355 debugger.GetTargetList().SetSelectedTarget(target); 356 357 // Now create the gdb-remote process. 358 if (log) 359 log->Printf( 360 "PlatformLinux::%s having target create process with gdb-remote plugin", 361 __FUNCTION__); 362 process_sp = target->CreateProcess( 363 launch_info.GetListenerForProcess(debugger), "gdb-remote", nullptr); 364 365 if (!process_sp) { 366 error.SetErrorString("CreateProcess() failed for gdb-remote process"); 367 if (log) 368 log->Printf("PlatformLinux::%s failed: %s", __FUNCTION__, 369 error.AsCString()); 370 return process_sp; 371 } else { 372 if (log) 373 log->Printf("PlatformLinux::%s successfully created process", 374 __FUNCTION__); 375 } 376 377 // Adjust launch for a hijacker. 378 ListenerSP listener_sp; 379 if (!launch_info.GetHijackListener()) { 380 if (log) 381 log->Printf("PlatformLinux::%s setting up hijacker", __FUNCTION__); 382 383 listener_sp = 384 Listener::MakeListener("lldb.PlatformLinux.DebugProcess.hijack"); 385 launch_info.SetHijackListener(listener_sp); 386 process_sp->HijackProcessEvents(listener_sp); 387 } 388 389 // Log file actions. 390 if (log) { 391 log->Printf( 392 "PlatformLinux::%s launching process with the following file actions:", 393 __FUNCTION__); 394 395 StreamString stream; 396 size_t i = 0; 397 const FileAction *file_action; 398 while ((file_action = launch_info.GetFileActionAtIndex(i++)) != nullptr) { 399 file_action->Dump(stream); 400 log->PutCString(stream.GetData()); 401 stream.Clear(); 402 } 403 } 404 405 // Do the launch. 406 error = process_sp->Launch(launch_info); 407 if (error.Success()) { 408 // Handle the hijacking of process events. 409 if (listener_sp) { 410 const StateType state = process_sp->WaitForProcessToStop( 411 llvm::None, NULL, false, listener_sp); 412 413 if (state == eStateStopped) { 414 if (log) 415 log->Printf("PlatformLinux::%s pid %" PRIu64 " state %s\n", 416 __FUNCTION__, process_sp->GetID(), StateAsCString(state)); 417 } else { 418 if (log) 419 log->Printf("PlatformLinux::%s pid %" PRIu64 420 " state is not stopped - %s\n", 421 __FUNCTION__, process_sp->GetID(), StateAsCString(state)); 422 } 423 } 424 425 // Hook up process PTY if we have one (which we should for local debugging 426 // with llgs). 427 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor(); 428 if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd) { 429 process_sp->SetSTDIOFileDescriptor(pty_fd); 430 if (log) 431 log->Printf("PlatformLinux::%s pid %" PRIu64 432 " hooked up STDIO pty to process", 433 __FUNCTION__, process_sp->GetID()); 434 } else { 435 if (log) 436 log->Printf("PlatformLinux::%s pid %" PRIu64 437 " not using process STDIO pty", 438 __FUNCTION__, process_sp->GetID()); 439 } 440 } else { 441 if (log) 442 log->Printf("PlatformLinux::%s process launch failed: %s", __FUNCTION__, 443 error.AsCString()); 444 // FIXME figure out appropriate cleanup here. Do we delete the target? Do 445 // we delete the process? Does our caller do that? 446 } 447 448 return process_sp; 449 } 450 451 void PlatformLinux::CalculateTrapHandlerSymbolNames() { 452 m_trap_handlers.push_back(ConstString("_sigtramp")); 453 } 454 455 uint64_t PlatformLinux::ConvertMmapFlagsToPlatform(const ArchSpec &arch, 456 unsigned flags) { 457 uint64_t flags_platform = 0; 458 uint64_t map_anon = MAP_ANON; 459 460 // To get correct flags for MIPS Architecture 461 if (arch.GetTriple().getArch() == llvm::Triple::mips64 || 462 arch.GetTriple().getArch() == llvm::Triple::mips64el || 463 arch.GetTriple().getArch() == llvm::Triple::mips || 464 arch.GetTriple().getArch() == llvm::Triple::mipsel) 465 map_anon = 0x800; 466 467 if (flags & eMmapFlagsPrivate) 468 flags_platform |= MAP_PRIVATE; 469 if (flags & eMmapFlagsAnon) 470 flags_platform |= map_anon; 471 return flags_platform; 472 } 473 474