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