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 
179db2541dSDavid Spickett #include "Utility/ARM64_DWARF_Registers.h"
1828041352SGreg Clayton #include "lldb/Core/Debugger.h"
19ecc11474SStephen Wilson #include "lldb/Core/PluginManager.h"
2013b18261SZachary Turner #include "lldb/Host/HostInfo.h"
219db2541dSDavid Spickett #include "lldb/Symbol/UnwindPlan.h"
22e996fd30SGreg Clayton #include "lldb/Target/Process.h"
23b9c1b51eSKate Stone #include "lldb/Target/Target.h"
245713a05bSZachary Turner #include "lldb/Utility/FileSpec.h"
25c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h"
266f9e6901SZachary Turner #include "lldb/Utility/Log.h"
27d821c997SPavel Labath #include "lldb/Utility/State.h"
2897206d57SZachary Turner #include "lldb/Utility/Status.h"
29bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
30e996fd30SGreg Clayton 
3105097246SAdrian Prantl // Define these constants from Linux mman.h for use when targeting remote linux
3205097246SAdrian Prantl // systems even when host has different values.
3396ad3de5SRobert Flack #define MAP_PRIVATE 2
3496ad3de5SRobert Flack #define MAP_ANON 0x20
3596ad3de5SRobert Flack 
36e996fd30SGreg Clayton using namespace lldb;
37e996fd30SGreg Clayton using namespace lldb_private;
38db264a6dSTamas Berghammer using namespace lldb_private::platform_linux;
39e996fd30SGreg Clayton 
40bba9ba8dSJonas Devlieghere LLDB_PLUGIN_DEFINE(PlatformLinux)
41fbb4d1e4SJonas Devlieghere 
4228041352SGreg Clayton static uint32_t g_initialize_count = 0;
4328041352SGreg Clayton 
44281961a8STodd Fiala 
CreateInstance(bool force,const ArchSpec * arch)45b9c1b51eSKate Stone PlatformSP PlatformLinux::CreateInstance(bool force, const ArchSpec *arch) {
46a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::Platform);
470e92fbb5SPavel Labath   LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force,
480e92fbb5SPavel Labath            arch ? arch->GetArchitectureName() : "<null>",
490e92fbb5SPavel Labath            arch ? arch->GetTriple().getTriple() : "<null>");
50015d818bSTodd Fiala 
51b3a40ba8SGreg Clayton   bool create = force;
52a6682a41SJonas Devlieghere   if (!create && arch && arch->IsValid()) {
53b3a40ba8SGreg Clayton     const llvm::Triple &triple = arch->GetTriple();
54b9c1b51eSKate Stone     switch (triple.getOS()) {
5570512317SGreg Clayton     case llvm::Triple::Linux:
56b1da257aSTed Woodward       create = true;
5770512317SGreg Clayton       break;
5870512317SGreg Clayton 
59dbc6c0bbSGreg Clayton #if defined(__linux__)
6005097246SAdrian Prantl     // Only accept "unknown" for the OS if the host is linux and it "unknown"
6105097246SAdrian Prantl     // wasn't specified (it was just returned because it was NOT specified)
62015d818bSTodd Fiala     case llvm::Triple::OSType::UnknownOS:
6370512317SGreg Clayton       create = !arch->TripleOSWasSpecified();
6470512317SGreg Clayton       break;
65dbc6c0bbSGreg Clayton #endif
6670512317SGreg Clayton     default:
6770512317SGreg Clayton       break;
6870512317SGreg Clayton     }
6970512317SGreg Clayton   }
70015d818bSTodd Fiala 
710e92fbb5SPavel Labath   LLDB_LOG(log, "create = {0}", create);
72b9c1b51eSKate Stone   if (create) {
73615eb7e6SGreg Clayton     return PlatformSP(new PlatformLinux(false));
74015d818bSTodd Fiala   }
75615eb7e6SGreg Clayton   return PlatformSP();
76ecc11474SStephen Wilson }
77ecc11474SStephen Wilson 
GetPluginDescriptionStatic(bool is_host)78a458ef4fSPavel Labath llvm::StringRef PlatformLinux::GetPluginDescriptionStatic(bool is_host) {
7928041352SGreg Clayton   if (is_host)
8028041352SGreg Clayton     return "Local Linux user platform plug-in.";
8128041352SGreg Clayton   return "Remote Linux user platform plug-in.";
82ecc11474SStephen Wilson }
83ecc11474SStephen Wilson 
Initialize()84b9c1b51eSKate Stone void PlatformLinux::Initialize() {
853c4f89d7STamas Berghammer   PlatformPOSIX::Initialize();
863c4f89d7STamas Berghammer 
87b9c1b51eSKate Stone   if (g_initialize_count++ == 0) {
881c6a1ea9STamas Berghammer #if defined(__linux__) && !defined(__ANDROID__)
8928041352SGreg Clayton     PlatformSP default_platform_sp(new PlatformLinux(true));
9013b18261SZachary Turner     default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
91615eb7e6SGreg Clayton     Platform::SetHostPlatform(default_platform_sp);
9228041352SGreg Clayton #endif
93b9c1b51eSKate Stone     PluginManager::RegisterPlugin(
94b9c1b51eSKate Stone         PlatformLinux::GetPluginNameStatic(false),
9528041352SGreg Clayton         PlatformLinux::GetPluginDescriptionStatic(false),
96d65ac229SPavel Labath         PlatformLinux::CreateInstance, nullptr);
97ecc11474SStephen Wilson   }
98e996fd30SGreg Clayton }
99e996fd30SGreg Clayton 
Terminate()100b9c1b51eSKate Stone void PlatformLinux::Terminate() {
101b9c1b51eSKate Stone   if (g_initialize_count > 0) {
102b9c1b51eSKate Stone     if (--g_initialize_count == 0) {
10328041352SGreg Clayton       PluginManager::UnregisterPlugin(PlatformLinux::CreateInstance);
104e996fd30SGreg Clayton     }
10528041352SGreg Clayton   }
1063c4f89d7STamas Berghammer 
1073c4f89d7STamas Berghammer   PlatformPOSIX::Terminate();
10828041352SGreg Clayton }
109e996fd30SGreg Clayton 
110e996fd30SGreg Clayton /// Default Constructor
PlatformLinux(bool is_host)111b9c1b51eSKate Stone PlatformLinux::PlatformLinux(bool is_host)
112b9c1b51eSKate Stone     : PlatformPOSIX(is_host) // This is the local host platform
113669e57ebSPavel Labath {
114669e57ebSPavel Labath   if (is_host) {
115e49b8e06SRobert Flack     ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
116669e57ebSPavel Labath     m_supported_architectures.push_back(hostArch);
117669e57ebSPavel Labath     if (hostArch.GetTriple().isArch64Bit()) {
118669e57ebSPavel Labath       m_supported_architectures.push_back(
119669e57ebSPavel Labath           HostInfo::GetArchitecture(HostInfo::eArchKind32));
120e49b8e06SRobert Flack     }
121b9c1b51eSKate Stone   } else {
122669e57ebSPavel Labath     m_supported_architectures = CreateArchList(
123669e57ebSPavel Labath         {llvm::Triple::x86_64, llvm::Triple::x86, llvm::Triple::arm,
124669e57ebSPavel Labath          llvm::Triple::aarch64, llvm::Triple::mips64, llvm::Triple::mips64,
125669e57ebSPavel Labath          llvm::Triple::hexagon, llvm::Triple::mips, llvm::Triple::mips64el,
126669e57ebSPavel Labath          llvm::Triple::mipsel, llvm::Triple::systemz},
127669e57ebSPavel Labath         llvm::Triple::Linux);
128669e57ebSPavel Labath   }
129669e57ebSPavel Labath }
130669e57ebSPavel Labath 
131*dde487e5SJonas Devlieghere std::vector<ArchSpec>
GetSupportedArchitectures(const ArchSpec & process_host_arch)132*dde487e5SJonas Devlieghere PlatformLinux::GetSupportedArchitectures(const ArchSpec &process_host_arch) {
133e49b8e06SRobert Flack   if (m_remote_platform_sp)
134*dde487e5SJonas Devlieghere     return m_remote_platform_sp->GetSupportedArchitectures(process_host_arch);
135669e57ebSPavel Labath   return m_supported_architectures;
136e996fd30SGreg Clayton }
137ecc11474SStephen Wilson 
GetStatus(Stream & strm)138b9c1b51eSKate Stone void PlatformLinux::GetStatus(Stream &strm) {
1393be69dacSDaniel Malea   Platform::GetStatus(strm);
140ecc11474SStephen Wilson 
1413011d55fSJonas Devlieghere #if LLDB_ENABLE_POSIX
142b743a803SStephane Sezer   // Display local kernel information only when we are running in host mode.
14305097246SAdrian Prantl   // Otherwise, we would end up printing non-Linux information (when running on
14405097246SAdrian Prantl   // Mac OS for example).
145b9c1b51eSKate Stone   if (IsHost()) {
146b2f1fb29SVirgile Bello     struct utsname un;
147b2f1fb29SVirgile Bello 
1483be69dacSDaniel Malea     if (uname(&un))
1493be69dacSDaniel Malea       return;
1503be69dacSDaniel Malea 
1513be69dacSDaniel Malea     strm.Printf("    Kernel: %s\n", un.sysname);
1523be69dacSDaniel Malea     strm.Printf("   Release: %s\n", un.release);
1533be69dacSDaniel Malea     strm.Printf("   Version: %s\n", un.version);
154b743a803SStephane Sezer   }
155b2f1fb29SVirgile Bello #endif
156ecc11474SStephen Wilson }
157ecc11474SStephen Wilson 
15845c3fc97SRaphael Isemann uint32_t
GetResumeCountForLaunchInfo(ProcessLaunchInfo & launch_info)159b9c1b51eSKate Stone PlatformLinux::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) {
16045c3fc97SRaphael Isemann   uint32_t resume_count = 0;
16128041352SGreg Clayton 
162348fb385STodd Fiala   // Always resume past the initial stop when we use eLaunchFlagDebug
163b9c1b51eSKate Stone   if (launch_info.GetFlags().Test(eLaunchFlagDebug)) {
164348fb385STodd Fiala     // Resume past the stop for the final exec into the true inferior.
165348fb385STodd Fiala     ++resume_count;
166348fb385STodd Fiala   }
167015d818bSTodd Fiala 
168348fb385STodd Fiala   // If we're not launching a shell, we're done.
16910687b0eSZachary Turner   const FileSpec &shell = launch_info.GetShell();
17010687b0eSZachary Turner   if (!shell)
171348fb385STodd Fiala     return resume_count;
172348fb385STodd Fiala 
17310687b0eSZachary Turner   std::string shell_string = shell.GetPath();
174348fb385STodd Fiala   // We're in a shell, so for sure we have to resume past the shell exec.
175348fb385STodd Fiala   ++resume_count;
176348fb385STodd Fiala 
177348fb385STodd Fiala   // Figure out what shell we're planning on using.
17810687b0eSZachary Turner   const char *shell_name = strrchr(shell_string.c_str(), '/');
179248a1305SKonrad Kleine   if (shell_name == nullptr)
18010687b0eSZachary Turner     shell_name = shell_string.c_str();
18128041352SGreg Clayton   else
182348fb385STodd Fiala     shell_name++;
183348fb385STodd Fiala 
184b9c1b51eSKate Stone   if (strcmp(shell_name, "csh") == 0 || strcmp(shell_name, "tcsh") == 0 ||
185b9c1b51eSKate Stone       strcmp(shell_name, "zsh") == 0 || strcmp(shell_name, "sh") == 0) {
186348fb385STodd Fiala     // These shells seem to re-exec themselves.  Add another resume.
187348fb385STodd Fiala     ++resume_count;
188ecc11474SStephen Wilson   }
18913e8e1c3SJohnny Chen 
190348fb385STodd Fiala   return resume_count;
191348fb385STodd Fiala }
192348fb385STodd Fiala 
CanDebugProcess()193b9c1b51eSKate Stone bool PlatformLinux::CanDebugProcess() {
194b9c1b51eSKate Stone   if (IsHost()) {
195b36f9178SPavel Labath     return true;
196b9c1b51eSKate Stone   } else {
197015d818bSTodd Fiala     // If we're connected, we can debug.
198015d818bSTodd Fiala     return IsConnected();
199015d818bSTodd Fiala   }
200348fb385STodd Fiala }
201015d818bSTodd Fiala 
CalculateTrapHandlerSymbolNames()202b9c1b51eSKate Stone void PlatformLinux::CalculateTrapHandlerSymbolNames() {
2032094dbf4SJason Molenda   m_trap_handlers.push_back(ConstString("_sigtramp"));
2043fd917d8SJoseph Tremoulet   m_trap_handlers.push_back(ConstString("__kernel_rt_sigreturn"));
2053fd917d8SJoseph Tremoulet   m_trap_handlers.push_back(ConstString("__restore_rt"));
2062094dbf4SJason Molenda }
207af245d11STodd Fiala 
GetAArch64TrapHanlderUnwindPlan(ConstString name)2089db2541dSDavid Spickett static lldb::UnwindPlanSP GetAArch64TrapHanlderUnwindPlan(ConstString name) {
2099db2541dSDavid Spickett   UnwindPlanSP unwind_plan_sp;
2109db2541dSDavid Spickett   if (name != "__kernel_rt_sigreturn")
2119db2541dSDavid Spickett     return unwind_plan_sp;
2129db2541dSDavid Spickett 
2139db2541dSDavid Spickett   UnwindPlan::RowSP row = std::make_shared<UnwindPlan::Row>();
2149db2541dSDavid Spickett   row->SetOffset(0);
2159db2541dSDavid Spickett 
2169db2541dSDavid Spickett   // In the signal trampoline frame, sp points to an rt_sigframe[1], which is:
2179db2541dSDavid Spickett   //  - 128-byte siginfo struct
2189db2541dSDavid Spickett   //  - ucontext struct:
2199db2541dSDavid Spickett   //     - 8-byte long (uc_flags)
2209db2541dSDavid Spickett   //     - 8-byte pointer (uc_link)
2219db2541dSDavid Spickett   //     - 24-byte stack_t
2229db2541dSDavid Spickett   //     - 128-byte signal set
2239db2541dSDavid Spickett   //     - 8 bytes of padding because sigcontext has 16-byte alignment
2249db2541dSDavid Spickett   //     - sigcontext/mcontext_t
2259db2541dSDavid Spickett   // [1]
2269db2541dSDavid Spickett   // https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/signal.c
2279db2541dSDavid Spickett   int32_t offset = 128 + 8 + 8 + 24 + 128 + 8;
2289db2541dSDavid Spickett   // Then sigcontext[2] is:
2299db2541dSDavid Spickett   // - 8 byte fault address
2309db2541dSDavid Spickett   // - 31 8 byte registers
2319db2541dSDavid Spickett   // - 8 byte sp
2329db2541dSDavid Spickett   // - 8 byte pc
2339db2541dSDavid Spickett   // [2]
2349db2541dSDavid Spickett   // https://github.com/torvalds/linux/blob/master/arch/arm64/include/uapi/asm/sigcontext.h
2359db2541dSDavid Spickett 
2369db2541dSDavid Spickett   // Skip fault address
2379db2541dSDavid Spickett   offset += 8;
2389db2541dSDavid Spickett   row->GetCFAValue().SetIsRegisterPlusOffset(arm64_dwarf::sp, offset);
2399db2541dSDavid Spickett 
2409db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x0, 0 * 8, false);
2419db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x1, 1 * 8, false);
2429db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x2, 2 * 8, false);
2439db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x3, 3 * 8, false);
2449db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x4, 4 * 8, false);
2459db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x5, 5 * 8, false);
2469db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x6, 6 * 8, false);
2479db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x7, 7 * 8, false);
2489db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x8, 8 * 8, false);
2499db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x9, 9 * 8, false);
2509db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x10, 10 * 8, false);
2519db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x11, 11 * 8, false);
2529db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x12, 12 * 8, false);
2539db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x13, 13 * 8, false);
2549db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x14, 14 * 8, false);
2559db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x15, 15 * 8, false);
2569db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x16, 16 * 8, false);
2579db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x17, 17 * 8, false);
2589db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x18, 18 * 8, false);
2599db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x19, 19 * 8, false);
2609db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x20, 20 * 8, false);
2619db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x21, 21 * 8, false);
2629db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x22, 22 * 8, false);
2639db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x23, 23 * 8, false);
2649db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x24, 24 * 8, false);
2659db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x25, 25 * 8, false);
2669db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x26, 26 * 8, false);
2679db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x27, 27 * 8, false);
2689db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x28, 28 * 8, false);
2699db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::fp, 29 * 8, false);
2709db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x30, 30 * 8, false);
2719db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::sp, 31 * 8, false);
2729db2541dSDavid Spickett   row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::pc, 32 * 8, false);
2739db2541dSDavid Spickett 
2749db2541dSDavid Spickett   // The sigcontext may also contain floating point and SVE registers.
2759db2541dSDavid Spickett   // However this would require a dynamic unwind plan so they are not included
2769db2541dSDavid Spickett   // here.
2779db2541dSDavid Spickett 
2789db2541dSDavid Spickett   unwind_plan_sp = std::make_shared<UnwindPlan>(eRegisterKindDWARF);
2799db2541dSDavid Spickett   unwind_plan_sp->AppendRow(row);
2809db2541dSDavid Spickett   unwind_plan_sp->SetSourceName("AArch64 Linux sigcontext");
2819db2541dSDavid Spickett   unwind_plan_sp->SetSourcedFromCompiler(eLazyBoolYes);
2829db2541dSDavid Spickett   // Because sp is the same throughout the function
2839db2541dSDavid Spickett   unwind_plan_sp->SetUnwindPlanValidAtAllInstructions(eLazyBoolYes);
2849db2541dSDavid Spickett   unwind_plan_sp->SetUnwindPlanForSignalTrap(eLazyBoolYes);
2859db2541dSDavid Spickett 
2869db2541dSDavid Spickett   return unwind_plan_sp;
2879db2541dSDavid Spickett }
2889db2541dSDavid Spickett 
2899db2541dSDavid Spickett lldb::UnwindPlanSP
GetTrapHandlerUnwindPlan(const llvm::Triple & triple,ConstString name)2909db2541dSDavid Spickett PlatformLinux::GetTrapHandlerUnwindPlan(const llvm::Triple &triple,
2919db2541dSDavid Spickett                                         ConstString name) {
2929db2541dSDavid Spickett   if (triple.isAArch64())
2939db2541dSDavid Spickett     return GetAArch64TrapHanlderUnwindPlan(name);
2949db2541dSDavid Spickett 
2959db2541dSDavid Spickett   return {};
2969db2541dSDavid Spickett }
2979db2541dSDavid Spickett 
GetMmapArgumentList(const ArchSpec & arch,addr_t addr,addr_t length,unsigned prot,unsigned flags,addr_t fd,addr_t offset)29837c40af7SEd Maste MmapArgList PlatformLinux::GetMmapArgumentList(const ArchSpec &arch,
29937c40af7SEd Maste                                                addr_t addr, addr_t length,
30037c40af7SEd Maste                                                unsigned prot, unsigned flags,
30137c40af7SEd Maste                                                addr_t fd, addr_t offset) {
30296ad3de5SRobert Flack   uint64_t flags_platform = 0;
303ddb93b63SFangrui Song   uint64_t map_anon = arch.IsMIPS() ? 0x800 : MAP_ANON;
304e0d8c422SMohit K. Bhakkad 
30596ad3de5SRobert Flack   if (flags & eMmapFlagsPrivate)
30696ad3de5SRobert Flack     flags_platform |= MAP_PRIVATE;
30796ad3de5SRobert Flack   if (flags & eMmapFlagsAnon)
308e0d8c422SMohit K. Bhakkad     flags_platform |= map_anon;
30937c40af7SEd Maste 
31037c40af7SEd Maste   MmapArgList args({addr, length, prot, flags_platform, fd, offset});
31137c40af7SEd Maste   return args;
31296ad3de5SRobert Flack }
3136e25aeeaSEnrico Granata 
GetSiginfoType(const llvm::Triple & triple)3143053e143SMichał Górny CompilerType PlatformLinux::GetSiginfoType(const llvm::Triple &triple) {
3153053e143SMichał Górny   if (!m_type_system_up)
3163053e143SMichał Górny     m_type_system_up.reset(new TypeSystemClang("siginfo", triple));
3173053e143SMichał Górny   TypeSystemClang *ast = m_type_system_up.get();
3183053e143SMichał Górny 
3193053e143SMichał Górny   bool si_errno_then_code = true;
3203053e143SMichał Górny 
3213053e143SMichał Górny   switch (triple.getArch()) {
3223053e143SMichał Górny   case llvm::Triple::mips:
3233053e143SMichał Górny   case llvm::Triple::mipsel:
3243053e143SMichał Górny   case llvm::Triple::mips64:
3253053e143SMichał Górny   case llvm::Triple::mips64el:
3263053e143SMichał Górny     // mips has si_code and si_errno swapped
3273053e143SMichał Górny     si_errno_then_code = false;
3283053e143SMichał Górny     break;
3293053e143SMichał Górny   default:
3303053e143SMichał Górny     break;
3313053e143SMichał Górny   }
3323053e143SMichał Górny 
3333053e143SMichał Górny   // generic types
3343053e143SMichał Górny   CompilerType int_type = ast->GetBasicType(eBasicTypeInt);
3353053e143SMichał Górny   CompilerType uint_type = ast->GetBasicType(eBasicTypeUnsignedInt);
3363053e143SMichał Górny   CompilerType short_type = ast->GetBasicType(eBasicTypeShort);
3373053e143SMichał Górny   CompilerType long_type = ast->GetBasicType(eBasicTypeLong);
3383053e143SMichał Górny   CompilerType voidp_type = ast->GetBasicType(eBasicTypeVoid).GetPointerType();
3393053e143SMichał Górny 
3403053e143SMichał Górny   // platform-specific types
3413053e143SMichał Górny   CompilerType &pid_type = int_type;
3423053e143SMichał Górny   CompilerType &uid_type = uint_type;
3433053e143SMichał Górny   CompilerType &clock_type = long_type;
3443053e143SMichał Górny   CompilerType &band_type = long_type;
3453053e143SMichał Górny 
3463053e143SMichał Górny   CompilerType sigval_type = ast->CreateRecordType(
3473053e143SMichał Górny       nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_sigval_t",
3483053e143SMichał Górny       clang::TTK_Union, lldb::eLanguageTypeC);
3493053e143SMichał Górny   ast->StartTagDeclarationDefinition(sigval_type);
3503053e143SMichał Górny   ast->AddFieldToRecordType(sigval_type, "sival_int", int_type,
3513053e143SMichał Górny                             lldb::eAccessPublic, 0);
3523053e143SMichał Górny   ast->AddFieldToRecordType(sigval_type, "sival_ptr", voidp_type,
3533053e143SMichał Górny                             lldb::eAccessPublic, 0);
3543053e143SMichał Górny   ast->CompleteTagDeclarationDefinition(sigval_type);
3553053e143SMichał Górny 
3563053e143SMichał Górny   CompilerType sigfault_bounds_type = ast->CreateRecordType(
3573053e143SMichał Górny       nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "",
3583053e143SMichał Górny       clang::TTK_Union, lldb::eLanguageTypeC);
3593053e143SMichał Górny   ast->StartTagDeclarationDefinition(sigfault_bounds_type);
3603053e143SMichał Górny   ast->AddFieldToRecordType(sigfault_bounds_type, "_addr_bnd",
3613053e143SMichał Górny       ast->CreateStructForIdentifier(ConstString(),
3623053e143SMichał Górny                                      {
3633053e143SMichał Górny                                          {"_lower", voidp_type},
3643053e143SMichał Górny                                          {"_upper", voidp_type},
3653053e143SMichał Górny                                      }),
3663053e143SMichał Górny                             lldb::eAccessPublic, 0);
3673053e143SMichał Górny   ast->AddFieldToRecordType(sigfault_bounds_type, "_pkey", uint_type,
3683053e143SMichał Górny                             lldb::eAccessPublic, 0);
3693053e143SMichał Górny   ast->CompleteTagDeclarationDefinition(sigfault_bounds_type);
3703053e143SMichał Górny 
3713053e143SMichał Górny   // siginfo_t
3723053e143SMichał Górny   CompilerType siginfo_type = ast->CreateRecordType(
3733053e143SMichał Górny       nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_siginfo_t",
3743053e143SMichał Górny       clang::TTK_Struct, lldb::eLanguageTypeC);
3753053e143SMichał Górny   ast->StartTagDeclarationDefinition(siginfo_type);
3763053e143SMichał Górny   ast->AddFieldToRecordType(siginfo_type, "si_signo", int_type,
3773053e143SMichał Górny                             lldb::eAccessPublic, 0);
3783053e143SMichał Górny 
3793053e143SMichał Górny   if (si_errno_then_code) {
3803053e143SMichał Górny     ast->AddFieldToRecordType(siginfo_type, "si_errno", int_type,
3813053e143SMichał Górny                               lldb::eAccessPublic, 0);
3823053e143SMichał Górny     ast->AddFieldToRecordType(siginfo_type, "si_code", int_type,
3833053e143SMichał Górny                               lldb::eAccessPublic, 0);
3843053e143SMichał Górny   } else {
3853053e143SMichał Górny     ast->AddFieldToRecordType(siginfo_type, "si_code", int_type,
3863053e143SMichał Górny                               lldb::eAccessPublic, 0);
3873053e143SMichał Górny     ast->AddFieldToRecordType(siginfo_type, "si_errno", int_type,
3883053e143SMichał Górny                               lldb::eAccessPublic, 0);
3893053e143SMichał Górny   }
3903053e143SMichał Górny 
3913053e143SMichał Górny   // the structure is padded on 64-bit arches to fix alignment
3923053e143SMichał Górny   if (triple.isArch64Bit())
3933053e143SMichał Górny     ast->AddFieldToRecordType(siginfo_type, "__pad0", int_type,
3943053e143SMichał Górny                               lldb::eAccessPublic, 0);
3953053e143SMichał Górny 
3963053e143SMichał Górny   // union used to hold the signal data
3973053e143SMichał Górny   CompilerType union_type = ast->CreateRecordType(
3983053e143SMichał Górny       nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "",
3993053e143SMichał Górny       clang::TTK_Union, lldb::eLanguageTypeC);
4003053e143SMichał Górny   ast->StartTagDeclarationDefinition(union_type);
4013053e143SMichał Górny 
4023053e143SMichał Górny   ast->AddFieldToRecordType(
4033053e143SMichał Górny       union_type, "_kill",
4043053e143SMichał Górny       ast->CreateStructForIdentifier(ConstString(),
4053053e143SMichał Górny                                      {
4063053e143SMichał Górny                                          {"si_pid", pid_type},
4073053e143SMichał Górny                                          {"si_uid", uid_type},
4083053e143SMichał Górny                                      }),
4093053e143SMichał Górny       lldb::eAccessPublic, 0);
4103053e143SMichał Górny 
4113053e143SMichał Górny   ast->AddFieldToRecordType(
4123053e143SMichał Górny       union_type, "_timer",
4133053e143SMichał Górny       ast->CreateStructForIdentifier(ConstString(),
4143053e143SMichał Górny                                      {
4153053e143SMichał Górny                                          {"si_tid", int_type},
4163053e143SMichał Górny                                          {"si_overrun", int_type},
4173053e143SMichał Górny                                          {"si_sigval", sigval_type},
4183053e143SMichał Górny                                      }),
4193053e143SMichał Górny       lldb::eAccessPublic, 0);
4203053e143SMichał Górny 
4213053e143SMichał Górny   ast->AddFieldToRecordType(
4223053e143SMichał Górny       union_type, "_rt",
4233053e143SMichał Górny       ast->CreateStructForIdentifier(ConstString(),
4243053e143SMichał Górny                                      {
4253053e143SMichał Górny                                          {"si_pid", pid_type},
4263053e143SMichał Górny                                          {"si_uid", uid_type},
4273053e143SMichał Górny                                          {"si_sigval", sigval_type},
4283053e143SMichał Górny                                      }),
4293053e143SMichał Górny       lldb::eAccessPublic, 0);
4303053e143SMichał Górny 
4313053e143SMichał Górny   ast->AddFieldToRecordType(
4323053e143SMichał Górny       union_type, "_sigchld",
4333053e143SMichał Górny       ast->CreateStructForIdentifier(ConstString(),
4343053e143SMichał Górny                                      {
4353053e143SMichał Górny                                          {"si_pid", pid_type},
4363053e143SMichał Górny                                          {"si_uid", uid_type},
4373053e143SMichał Górny                                          {"si_status", int_type},
4383053e143SMichał Górny                                          {"si_utime", clock_type},
4393053e143SMichał Górny                                          {"si_stime", clock_type},
4403053e143SMichał Górny                                      }),
4413053e143SMichał Górny       lldb::eAccessPublic, 0);
4423053e143SMichał Górny 
4433053e143SMichał Górny   ast->AddFieldToRecordType(
4443053e143SMichał Górny       union_type, "_sigfault",
4453053e143SMichał Górny       ast->CreateStructForIdentifier(ConstString(),
4463053e143SMichał Górny                                      {
4473053e143SMichał Górny                                          {"si_addr", voidp_type},
4483053e143SMichał Górny                                          {"si_addr_lsb", short_type},
4493053e143SMichał Górny                                          {"_bounds", sigfault_bounds_type},
4503053e143SMichał Górny                                      }),
4513053e143SMichał Górny       lldb::eAccessPublic, 0);
4523053e143SMichał Górny 
4533053e143SMichał Górny   ast->AddFieldToRecordType(
4543053e143SMichał Górny       union_type, "_sigpoll",
4553053e143SMichał Górny       ast->CreateStructForIdentifier(ConstString(),
4563053e143SMichał Górny                                      {
4573053e143SMichał Górny                                          {"si_band", band_type},
4583053e143SMichał Górny                                          {"si_fd", int_type},
4593053e143SMichał Górny                                      }),
4603053e143SMichał Górny       lldb::eAccessPublic, 0);
4613053e143SMichał Górny 
4623053e143SMichał Górny   // NB: SIGSYS is not present on ia64 but we don't seem to support that
4633053e143SMichał Górny   ast->AddFieldToRecordType(
4643053e143SMichał Górny       union_type, "_sigsys",
4653053e143SMichał Górny       ast->CreateStructForIdentifier(ConstString(),
4663053e143SMichał Górny                                      {
4673053e143SMichał Górny                                          {"_call_addr", voidp_type},
4683053e143SMichał Górny                                          {"_syscall", int_type},
4693053e143SMichał Górny                                          {"_arch", uint_type},
4703053e143SMichał Górny                                      }),
4713053e143SMichał Górny       lldb::eAccessPublic, 0);
4723053e143SMichał Górny 
4733053e143SMichał Górny   ast->CompleteTagDeclarationDefinition(union_type);
4743053e143SMichał Górny   ast->AddFieldToRecordType(siginfo_type, "_sifields", union_type,
4753053e143SMichał Górny                             lldb::eAccessPublic, 0);
4763053e143SMichał Górny 
4773053e143SMichał Górny   ast->CompleteTagDeclarationDefinition(siginfo_type);
4783053e143SMichał Górny   return siginfo_type;
4793053e143SMichał Górny }
480