180814287SRaphael Isemann //===-- PlatformLinux.cpp -------------------------------------------------===//
2e996fd30SGreg Clayton //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e996fd30SGreg Clayton //
7e996fd30SGreg Clayton //===----------------------------------------------------------------------===//
8e996fd30SGreg Clayton 
9e996fd30SGreg Clayton #include "PlatformLinux.h"
10b2f1fb29SVirgile Bello #include "lldb/Host/Config.h"
11e996fd30SGreg Clayton 
1276e47d48SRaphael Isemann #include <cstdio>
133011d55fSJonas Devlieghere #if LLDB_ENABLE_POSIX
14ecc11474SStephen Wilson #include <sys/utsname.h>
15b2f1fb29SVirgile Bello #endif
16ecc11474SStephen Wilson 
1728041352SGreg Clayton #include "lldb/Core/Debugger.h"
18ecc11474SStephen Wilson #include "lldb/Core/PluginManager.h"
1913b18261SZachary Turner #include "lldb/Host/HostInfo.h"
20e996fd30SGreg Clayton #include "lldb/Target/Process.h"
21b9c1b51eSKate Stone #include "lldb/Target/Target.h"
225713a05bSZachary Turner #include "lldb/Utility/FileSpec.h"
236f9e6901SZachary Turner #include "lldb/Utility/Log.h"
24d821c997SPavel Labath #include "lldb/Utility/State.h"
2597206d57SZachary Turner #include "lldb/Utility/Status.h"
26bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
27e996fd30SGreg Clayton 
2805097246SAdrian Prantl // Define these constants from Linux mman.h for use when targeting remote linux
2905097246SAdrian Prantl // systems even when host has different values.
3096ad3de5SRobert Flack #define MAP_PRIVATE 2
3196ad3de5SRobert Flack #define MAP_ANON 0x20
3296ad3de5SRobert Flack 
33e996fd30SGreg Clayton using namespace lldb;
34e996fd30SGreg Clayton using namespace lldb_private;
35db264a6dSTamas Berghammer using namespace lldb_private::platform_linux;
36e996fd30SGreg Clayton 
37bba9ba8dSJonas Devlieghere LLDB_PLUGIN_DEFINE(PlatformLinux)
38fbb4d1e4SJonas Devlieghere 
3928041352SGreg Clayton static uint32_t g_initialize_count = 0;
4028041352SGreg Clayton 
41281961a8STodd Fiala 
42b9c1b51eSKate Stone PlatformSP PlatformLinux::CreateInstance(bool force, const ArchSpec *arch) {
43db264a6dSTamas Berghammer   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
440e92fbb5SPavel Labath   LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force,
450e92fbb5SPavel Labath            arch ? arch->GetArchitectureName() : "<null>",
460e92fbb5SPavel Labath            arch ? arch->GetTriple().getTriple() : "<null>");
47015d818bSTodd Fiala 
48b3a40ba8SGreg Clayton   bool create = force;
49a6682a41SJonas Devlieghere   if (!create && arch && arch->IsValid()) {
50b3a40ba8SGreg Clayton     const llvm::Triple &triple = arch->GetTriple();
51b9c1b51eSKate Stone     switch (triple.getOS()) {
5270512317SGreg Clayton     case llvm::Triple::Linux:
53b1da257aSTed Woodward       create = true;
5470512317SGreg Clayton       break;
5570512317SGreg Clayton 
56dbc6c0bbSGreg Clayton #if defined(__linux__)
5705097246SAdrian Prantl     // Only accept "unknown" for the OS if the host is linux and it "unknown"
5805097246SAdrian Prantl     // wasn't specified (it was just returned because it was NOT specified)
59015d818bSTodd Fiala     case llvm::Triple::OSType::UnknownOS:
6070512317SGreg Clayton       create = !arch->TripleOSWasSpecified();
6170512317SGreg Clayton       break;
62dbc6c0bbSGreg Clayton #endif
6370512317SGreg Clayton     default:
6470512317SGreg Clayton       break;
6570512317SGreg Clayton     }
6670512317SGreg Clayton   }
67015d818bSTodd Fiala 
680e92fbb5SPavel Labath   LLDB_LOG(log, "create = {0}", create);
69b9c1b51eSKate Stone   if (create) {
70615eb7e6SGreg Clayton     return PlatformSP(new PlatformLinux(false));
71015d818bSTodd Fiala   }
72615eb7e6SGreg Clayton   return PlatformSP();
73ecc11474SStephen Wilson }
74ecc11474SStephen Wilson 
75*a458ef4fSPavel Labath llvm::StringRef PlatformLinux::GetPluginDescriptionStatic(bool is_host) {
7628041352SGreg Clayton   if (is_host)
7728041352SGreg Clayton     return "Local Linux user platform plug-in.";
7828041352SGreg Clayton   return "Remote Linux user platform plug-in.";
79ecc11474SStephen Wilson }
80ecc11474SStephen Wilson 
81b9c1b51eSKate Stone void PlatformLinux::Initialize() {
823c4f89d7STamas Berghammer   PlatformPOSIX::Initialize();
833c4f89d7STamas Berghammer 
84b9c1b51eSKate Stone   if (g_initialize_count++ == 0) {
851c6a1ea9STamas Berghammer #if defined(__linux__) && !defined(__ANDROID__)
8628041352SGreg Clayton     PlatformSP default_platform_sp(new PlatformLinux(true));
8713b18261SZachary Turner     default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
88615eb7e6SGreg Clayton     Platform::SetHostPlatform(default_platform_sp);
8928041352SGreg Clayton #endif
90b9c1b51eSKate Stone     PluginManager::RegisterPlugin(
91b9c1b51eSKate Stone         PlatformLinux::GetPluginNameStatic(false),
9228041352SGreg Clayton         PlatformLinux::GetPluginDescriptionStatic(false),
93d65ac229SPavel Labath         PlatformLinux::CreateInstance, nullptr);
94ecc11474SStephen Wilson   }
95e996fd30SGreg Clayton }
96e996fd30SGreg Clayton 
97b9c1b51eSKate Stone void PlatformLinux::Terminate() {
98b9c1b51eSKate Stone   if (g_initialize_count > 0) {
99b9c1b51eSKate Stone     if (--g_initialize_count == 0) {
10028041352SGreg Clayton       PluginManager::UnregisterPlugin(PlatformLinux::CreateInstance);
101e996fd30SGreg Clayton     }
10228041352SGreg Clayton   }
1033c4f89d7STamas Berghammer 
1043c4f89d7STamas Berghammer   PlatformPOSIX::Terminate();
10528041352SGreg Clayton }
106e996fd30SGreg Clayton 
107e996fd30SGreg Clayton /// Default Constructor
108b9c1b51eSKate Stone PlatformLinux::PlatformLinux(bool is_host)
109b9c1b51eSKate Stone     : PlatformPOSIX(is_host) // This is the local host platform
110b9c1b51eSKate Stone {}
111e996fd30SGreg Clayton 
112b9c1b51eSKate Stone bool PlatformLinux::GetSupportedArchitectureAtIndex(uint32_t idx,
113b9c1b51eSKate Stone                                                     ArchSpec &arch) {
114b9c1b51eSKate Stone   if (IsHost()) {
115e49b8e06SRobert Flack     ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
116b9c1b51eSKate Stone     if (hostArch.GetTriple().isOSLinux()) {
117b9c1b51eSKate Stone       if (idx == 0) {
118e49b8e06SRobert Flack         arch = hostArch;
119e49b8e06SRobert Flack         return arch.IsValid();
120b9c1b51eSKate Stone       } else if (idx == 1) {
12105097246SAdrian Prantl         // If the default host architecture is 64-bit, look for a 32-bit
12205097246SAdrian Prantl         // variant
123b9c1b51eSKate Stone         if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) {
124e49b8e06SRobert Flack           arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
125e49b8e06SRobert Flack           return arch.IsValid();
126e49b8e06SRobert Flack         }
127e49b8e06SRobert Flack       }
128e49b8e06SRobert Flack     }
129b9c1b51eSKate Stone   } else {
130e49b8e06SRobert Flack     if (m_remote_platform_sp)
131e49b8e06SRobert Flack       return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
132bb1e283cSTed Woodward 
133bb1e283cSTed Woodward     llvm::Triple triple;
134bb1e283cSTed Woodward     // Set the OS to linux
135bb1e283cSTed Woodward     triple.setOS(llvm::Triple::Linux);
136bb1e283cSTed Woodward     // Set the architecture
137b9c1b51eSKate Stone     switch (idx) {
138b9c1b51eSKate Stone     case 0:
139b9c1b51eSKate Stone       triple.setArchName("x86_64");
140b9c1b51eSKate Stone       break;
141b9c1b51eSKate Stone     case 1:
142b9c1b51eSKate Stone       triple.setArchName("i386");
143b9c1b51eSKate Stone       break;
144b9c1b51eSKate Stone     case 2:
145b9c1b51eSKate Stone       triple.setArchName("arm");
146b9c1b51eSKate Stone       break;
147b9c1b51eSKate Stone     case 3:
148b9c1b51eSKate Stone       triple.setArchName("aarch64");
149b9c1b51eSKate Stone       break;
150b9c1b51eSKate Stone     case 4:
151b9c1b51eSKate Stone       triple.setArchName("mips64");
152b9c1b51eSKate Stone       break;
153b9c1b51eSKate Stone     case 5:
154b9c1b51eSKate Stone       triple.setArchName("hexagon");
155b9c1b51eSKate Stone       break;
156b9c1b51eSKate Stone     case 6:
157b9c1b51eSKate Stone       triple.setArchName("mips");
158b9c1b51eSKate Stone       break;
159b9c1b51eSKate Stone     case 7:
160b9c1b51eSKate Stone       triple.setArchName("mips64el");
161b9c1b51eSKate Stone       break;
162b9c1b51eSKate Stone     case 8:
163b9c1b51eSKate Stone       triple.setArchName("mipsel");
164b9c1b51eSKate Stone       break;
165b9c1b51eSKate Stone     case 9:
166b9c1b51eSKate Stone       triple.setArchName("s390x");
167b9c1b51eSKate Stone       break;
168b9c1b51eSKate Stone     default:
169b9c1b51eSKate Stone       return false;
170bb1e283cSTed Woodward     }
171b9c1b51eSKate Stone     // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the
17205097246SAdrian Prantl     // vendor by calling triple.SetVendorName("unknown") so that it is a
17305097246SAdrian Prantl     // "unspecified unknown". This means when someone calls
17405097246SAdrian Prantl     // triple.GetVendorName() it will return an empty string which indicates
17505097246SAdrian Prantl     // that the vendor can be set when two architectures are merged
176bb1e283cSTed Woodward 
177bb1e283cSTed Woodward     // Now set the triple into "arch" and return true
178bb1e283cSTed Woodward     arch.SetTriple(triple);
179bb1e283cSTed Woodward     return true;
180e49b8e06SRobert Flack   }
181e996fd30SGreg Clayton   return false;
182e996fd30SGreg Clayton }
183ecc11474SStephen Wilson 
184b9c1b51eSKate Stone void PlatformLinux::GetStatus(Stream &strm) {
1853be69dacSDaniel Malea   Platform::GetStatus(strm);
186ecc11474SStephen Wilson 
1873011d55fSJonas Devlieghere #if LLDB_ENABLE_POSIX
188b743a803SStephane Sezer   // Display local kernel information only when we are running in host mode.
18905097246SAdrian Prantl   // Otherwise, we would end up printing non-Linux information (when running on
19005097246SAdrian Prantl   // Mac OS for example).
191b9c1b51eSKate Stone   if (IsHost()) {
192b2f1fb29SVirgile Bello     struct utsname un;
193b2f1fb29SVirgile Bello 
1943be69dacSDaniel Malea     if (uname(&un))
1953be69dacSDaniel Malea       return;
1963be69dacSDaniel Malea 
1973be69dacSDaniel Malea     strm.Printf("    Kernel: %s\n", un.sysname);
1983be69dacSDaniel Malea     strm.Printf("   Release: %s\n", un.release);
1993be69dacSDaniel Malea     strm.Printf("   Version: %s\n", un.version);
200b743a803SStephane Sezer   }
201b2f1fb29SVirgile Bello #endif
202ecc11474SStephen Wilson }
203ecc11474SStephen Wilson 
20445c3fc97SRaphael Isemann uint32_t
205b9c1b51eSKate Stone PlatformLinux::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) {
20645c3fc97SRaphael Isemann   uint32_t resume_count = 0;
20728041352SGreg Clayton 
208348fb385STodd Fiala   // Always resume past the initial stop when we use eLaunchFlagDebug
209b9c1b51eSKate Stone   if (launch_info.GetFlags().Test(eLaunchFlagDebug)) {
210348fb385STodd Fiala     // Resume past the stop for the final exec into the true inferior.
211348fb385STodd Fiala     ++resume_count;
212348fb385STodd Fiala   }
213015d818bSTodd Fiala 
214348fb385STodd Fiala   // If we're not launching a shell, we're done.
21510687b0eSZachary Turner   const FileSpec &shell = launch_info.GetShell();
21610687b0eSZachary Turner   if (!shell)
217348fb385STodd Fiala     return resume_count;
218348fb385STodd Fiala 
21910687b0eSZachary Turner   std::string shell_string = shell.GetPath();
220348fb385STodd Fiala   // We're in a shell, so for sure we have to resume past the shell exec.
221348fb385STodd Fiala   ++resume_count;
222348fb385STodd Fiala 
223348fb385STodd Fiala   // Figure out what shell we're planning on using.
22410687b0eSZachary Turner   const char *shell_name = strrchr(shell_string.c_str(), '/');
225248a1305SKonrad Kleine   if (shell_name == nullptr)
22610687b0eSZachary Turner     shell_name = shell_string.c_str();
22728041352SGreg Clayton   else
228348fb385STodd Fiala     shell_name++;
229348fb385STodd Fiala 
230b9c1b51eSKate Stone   if (strcmp(shell_name, "csh") == 0 || strcmp(shell_name, "tcsh") == 0 ||
231b9c1b51eSKate Stone       strcmp(shell_name, "zsh") == 0 || strcmp(shell_name, "sh") == 0) {
232348fb385STodd Fiala     // These shells seem to re-exec themselves.  Add another resume.
233348fb385STodd Fiala     ++resume_count;
234ecc11474SStephen Wilson   }
23513e8e1c3SJohnny Chen 
236348fb385STodd Fiala   return resume_count;
237348fb385STodd Fiala }
238348fb385STodd Fiala 
239b9c1b51eSKate Stone bool PlatformLinux::CanDebugProcess() {
240b9c1b51eSKate Stone   if (IsHost()) {
241b36f9178SPavel Labath     return true;
242b9c1b51eSKate Stone   } else {
243015d818bSTodd Fiala     // If we're connected, we can debug.
244015d818bSTodd Fiala     return IsConnected();
245015d818bSTodd Fiala   }
246348fb385STodd Fiala }
247015d818bSTodd Fiala 
248b9c1b51eSKate Stone void PlatformLinux::CalculateTrapHandlerSymbolNames() {
2492094dbf4SJason Molenda   m_trap_handlers.push_back(ConstString("_sigtramp"));
2503fd917d8SJoseph Tremoulet   m_trap_handlers.push_back(ConstString("__kernel_rt_sigreturn"));
2513fd917d8SJoseph Tremoulet   m_trap_handlers.push_back(ConstString("__restore_rt"));
2522094dbf4SJason Molenda }
253af245d11STodd Fiala 
25437c40af7SEd Maste MmapArgList PlatformLinux::GetMmapArgumentList(const ArchSpec &arch,
25537c40af7SEd Maste                                                addr_t addr, addr_t length,
25637c40af7SEd Maste                                                unsigned prot, unsigned flags,
25737c40af7SEd Maste                                                addr_t fd, addr_t offset) {
25896ad3de5SRobert Flack   uint64_t flags_platform = 0;
259ddb93b63SFangrui Song   uint64_t map_anon = arch.IsMIPS() ? 0x800 : MAP_ANON;
260e0d8c422SMohit K. Bhakkad 
26196ad3de5SRobert Flack   if (flags & eMmapFlagsPrivate)
26296ad3de5SRobert Flack     flags_platform |= MAP_PRIVATE;
26396ad3de5SRobert Flack   if (flags & eMmapFlagsAnon)
264e0d8c422SMohit K. Bhakkad     flags_platform |= map_anon;
26537c40af7SEd Maste 
26637c40af7SEd Maste   MmapArgList args({addr, length, prot, flags_platform, fd, offset});
26737c40af7SEd Maste   return args;
26896ad3de5SRobert Flack }
2696e25aeeaSEnrico Granata 
270