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