[clang] Add tracking source deduction guide for the explicitly-written
[llvm-project.git] / lldb / source / Plugins / Platform / Linux / PlatformLinux.cpp
blob13465986f49c53dee693873026b81536149c3259
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 //===----------------------------------------------------------------------===//
9 #include "PlatformLinux.h"
10 #include "lldb/Host/Config.h"
12 #include <cstdio>
13 #if LLDB_ENABLE_POSIX
14 #include <sys/utsname.h>
15 #endif
17 #include "Utility/ARM64_DWARF_Registers.h"
18 #include "lldb/Core/Debugger.h"
19 #include "lldb/Core/PluginManager.h"
20 #include "lldb/Host/HostInfo.h"
21 #include "lldb/Symbol/UnwindPlan.h"
22 #include "lldb/Target/Process.h"
23 #include "lldb/Target/Target.h"
24 #include "lldb/Utility/FileSpec.h"
25 #include "lldb/Utility/LLDBLog.h"
26 #include "lldb/Utility/Log.h"
27 #include "lldb/Utility/State.h"
28 #include "lldb/Utility/Status.h"
29 #include "lldb/Utility/StreamString.h"
31 // Define these constants from Linux mman.h for use when targeting remote linux
32 // systems even when host has different values.
33 #define MAP_PRIVATE 2
34 #define MAP_ANON 0x20
36 using namespace lldb;
37 using namespace lldb_private;
38 using namespace lldb_private::platform_linux;
40 LLDB_PLUGIN_DEFINE(PlatformLinux)
42 static uint32_t g_initialize_count = 0;
45 PlatformSP PlatformLinux::CreateInstance(bool force, const ArchSpec *arch) {
46 Log *log = GetLog(LLDBLog::Platform);
47 LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force,
48 arch ? arch->GetArchitectureName() : "<null>",
49 arch ? arch->GetTriple().getTriple() : "<null>");
51 bool create = force;
52 if (!create && arch && arch->IsValid()) {
53 const llvm::Triple &triple = arch->GetTriple();
54 switch (triple.getOS()) {
55 case llvm::Triple::Linux:
56 create = true;
57 break;
59 #if defined(__linux__)
60 // Only accept "unknown" for the OS if the host is linux and it "unknown"
61 // wasn't specified (it was just returned because it was NOT specified)
62 case llvm::Triple::OSType::UnknownOS:
63 create = !arch->TripleOSWasSpecified();
64 break;
65 #endif
66 default:
67 break;
71 LLDB_LOG(log, "create = {0}", create);
72 if (create) {
73 return PlatformSP(new PlatformLinux(false));
75 return PlatformSP();
78 llvm::StringRef PlatformLinux::GetPluginDescriptionStatic(bool is_host) {
79 if (is_host)
80 return "Local Linux user platform plug-in.";
81 return "Remote Linux user platform plug-in.";
84 void PlatformLinux::Initialize() {
85 PlatformPOSIX::Initialize();
87 if (g_initialize_count++ == 0) {
88 #if defined(__linux__) && !defined(__ANDROID__)
89 PlatformSP default_platform_sp(new PlatformLinux(true));
90 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
91 Platform::SetHostPlatform(default_platform_sp);
92 #endif
93 PluginManager::RegisterPlugin(
94 PlatformLinux::GetPluginNameStatic(false),
95 PlatformLinux::GetPluginDescriptionStatic(false),
96 PlatformLinux::CreateInstance, nullptr);
100 void PlatformLinux::Terminate() {
101 if (g_initialize_count > 0) {
102 if (--g_initialize_count == 0) {
103 PluginManager::UnregisterPlugin(PlatformLinux::CreateInstance);
107 PlatformPOSIX::Terminate();
110 /// Default Constructor
111 PlatformLinux::PlatformLinux(bool is_host)
112 : PlatformPOSIX(is_host) // This is the local host platform
114 if (is_host) {
115 ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
116 m_supported_architectures.push_back(hostArch);
117 if (hostArch.GetTriple().isArch64Bit()) {
118 m_supported_architectures.push_back(
119 HostInfo::GetArchitecture(HostInfo::eArchKind32));
121 } else {
122 m_supported_architectures = CreateArchList(
123 {llvm::Triple::x86_64, llvm::Triple::x86, llvm::Triple::arm,
124 llvm::Triple::aarch64, llvm::Triple::mips64, llvm::Triple::mips64,
125 llvm::Triple::hexagon, llvm::Triple::mips, llvm::Triple::mips64el,
126 llvm::Triple::mipsel, llvm::Triple::msp430, llvm::Triple::systemz},
127 llvm::Triple::Linux);
131 std::vector<ArchSpec>
132 PlatformLinux::GetSupportedArchitectures(const ArchSpec &process_host_arch) {
133 if (m_remote_platform_sp)
134 return m_remote_platform_sp->GetSupportedArchitectures(process_host_arch);
135 return m_supported_architectures;
138 void PlatformLinux::GetStatus(Stream &strm) {
139 Platform::GetStatus(strm);
141 #if LLDB_ENABLE_POSIX
142 // Display local kernel information only when we are running in host mode.
143 // Otherwise, we would end up printing non-Linux information (when running on
144 // Mac OS for example).
145 if (IsHost()) {
146 struct utsname un;
148 if (uname(&un))
149 return;
151 strm.Printf(" Kernel: %s\n", un.sysname);
152 strm.Printf(" Release: %s\n", un.release);
153 strm.Printf(" Version: %s\n", un.version);
155 #endif
158 uint32_t
159 PlatformLinux::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) {
160 uint32_t resume_count = 0;
162 // Always resume past the initial stop when we use eLaunchFlagDebug
163 if (launch_info.GetFlags().Test(eLaunchFlagDebug)) {
164 // Resume past the stop for the final exec into the true inferior.
165 ++resume_count;
168 // If we're not launching a shell, we're done.
169 const FileSpec &shell = launch_info.GetShell();
170 if (!shell)
171 return resume_count;
173 std::string shell_string = shell.GetPath();
174 // We're in a shell, so for sure we have to resume past the shell exec.
175 ++resume_count;
177 // Figure out what shell we're planning on using.
178 const char *shell_name = strrchr(shell_string.c_str(), '/');
179 if (shell_name == nullptr)
180 shell_name = shell_string.c_str();
181 else
182 shell_name++;
184 if (strcmp(shell_name, "csh") == 0 || strcmp(shell_name, "tcsh") == 0 ||
185 strcmp(shell_name, "zsh") == 0 || strcmp(shell_name, "sh") == 0) {
186 // These shells seem to re-exec themselves. Add another resume.
187 ++resume_count;
190 return resume_count;
193 bool PlatformLinux::CanDebugProcess() {
194 if (IsHost()) {
195 return true;
196 } else {
197 // If we're connected, we can debug.
198 return IsConnected();
202 void PlatformLinux::CalculateTrapHandlerSymbolNames() {
203 m_trap_handlers.push_back(ConstString("_sigtramp"));
204 m_trap_handlers.push_back(ConstString("__kernel_rt_sigreturn"));
205 m_trap_handlers.push_back(ConstString("__restore_rt"));
208 static lldb::UnwindPlanSP GetAArch64TrapHandlerUnwindPlan(ConstString name) {
209 UnwindPlanSP unwind_plan_sp;
210 if (name != "__kernel_rt_sigreturn")
211 return unwind_plan_sp;
213 UnwindPlan::RowSP row = std::make_shared<UnwindPlan::Row>();
214 row->SetOffset(0);
216 // In the signal trampoline frame, sp points to an rt_sigframe[1], which is:
217 // - 128-byte siginfo struct
218 // - ucontext struct:
219 // - 8-byte long (uc_flags)
220 // - 8-byte pointer (uc_link)
221 // - 24-byte stack_t
222 // - 128-byte signal set
223 // - 8 bytes of padding because sigcontext has 16-byte alignment
224 // - sigcontext/mcontext_t
225 // [1]
226 // https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/signal.c
227 int32_t offset = 128 + 8 + 8 + 24 + 128 + 8;
228 // Then sigcontext[2] is:
229 // - 8 byte fault address
230 // - 31 8 byte registers
231 // - 8 byte sp
232 // - 8 byte pc
233 // [2]
234 // https://github.com/torvalds/linux/blob/master/arch/arm64/include/uapi/asm/sigcontext.h
236 // Skip fault address
237 offset += 8;
238 row->GetCFAValue().SetIsRegisterPlusOffset(arm64_dwarf::sp, offset);
240 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x0, 0 * 8, false);
241 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x1, 1 * 8, false);
242 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x2, 2 * 8, false);
243 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x3, 3 * 8, false);
244 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x4, 4 * 8, false);
245 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x5, 5 * 8, false);
246 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x6, 6 * 8, false);
247 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x7, 7 * 8, false);
248 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x8, 8 * 8, false);
249 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x9, 9 * 8, false);
250 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x10, 10 * 8, false);
251 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x11, 11 * 8, false);
252 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x12, 12 * 8, false);
253 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x13, 13 * 8, false);
254 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x14, 14 * 8, false);
255 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x15, 15 * 8, false);
256 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x16, 16 * 8, false);
257 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x17, 17 * 8, false);
258 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x18, 18 * 8, false);
259 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x19, 19 * 8, false);
260 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x20, 20 * 8, false);
261 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x21, 21 * 8, false);
262 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x22, 22 * 8, false);
263 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x23, 23 * 8, false);
264 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x24, 24 * 8, false);
265 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x25, 25 * 8, false);
266 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x26, 26 * 8, false);
267 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x27, 27 * 8, false);
268 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x28, 28 * 8, false);
269 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::fp, 29 * 8, false);
270 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x30, 30 * 8, false);
271 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::sp, 31 * 8, false);
272 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::pc, 32 * 8, false);
274 // The sigcontext may also contain floating point and SVE registers.
275 // However this would require a dynamic unwind plan so they are not included
276 // here.
278 unwind_plan_sp = std::make_shared<UnwindPlan>(eRegisterKindDWARF);
279 unwind_plan_sp->AppendRow(row);
280 unwind_plan_sp->SetSourceName("AArch64 Linux sigcontext");
281 unwind_plan_sp->SetSourcedFromCompiler(eLazyBoolYes);
282 // Because sp is the same throughout the function
283 unwind_plan_sp->SetUnwindPlanValidAtAllInstructions(eLazyBoolYes);
284 unwind_plan_sp->SetUnwindPlanForSignalTrap(eLazyBoolYes);
286 return unwind_plan_sp;
289 lldb::UnwindPlanSP
290 PlatformLinux::GetTrapHandlerUnwindPlan(const llvm::Triple &triple,
291 ConstString name) {
292 if (triple.isAArch64())
293 return GetAArch64TrapHandlerUnwindPlan(name);
295 return {};
298 MmapArgList PlatformLinux::GetMmapArgumentList(const ArchSpec &arch,
299 addr_t addr, addr_t length,
300 unsigned prot, unsigned flags,
301 addr_t fd, addr_t offset) {
302 uint64_t flags_platform = 0;
303 uint64_t map_anon = arch.IsMIPS() ? 0x800 : MAP_ANON;
305 if (flags & eMmapFlagsPrivate)
306 flags_platform |= MAP_PRIVATE;
307 if (flags & eMmapFlagsAnon)
308 flags_platform |= map_anon;
310 MmapArgList args({addr, length, prot, flags_platform, fd, offset});
311 return args;
314 CompilerType PlatformLinux::GetSiginfoType(const llvm::Triple &triple) {
316 std::lock_guard<std::mutex> guard(m_mutex);
317 if (!m_type_system)
318 m_type_system = std::make_shared<TypeSystemClang>("siginfo", triple);
320 TypeSystemClang *ast = m_type_system.get();
322 bool si_errno_then_code = true;
324 switch (triple.getArch()) {
325 case llvm::Triple::mips:
326 case llvm::Triple::mipsel:
327 case llvm::Triple::mips64:
328 case llvm::Triple::mips64el:
329 // mips has si_code and si_errno swapped
330 si_errno_then_code = false;
331 break;
332 default:
333 break;
336 // generic types
337 CompilerType int_type = ast->GetBasicType(eBasicTypeInt);
338 CompilerType uint_type = ast->GetBasicType(eBasicTypeUnsignedInt);
339 CompilerType short_type = ast->GetBasicType(eBasicTypeShort);
340 CompilerType long_type = ast->GetBasicType(eBasicTypeLong);
341 CompilerType voidp_type = ast->GetBasicType(eBasicTypeVoid).GetPointerType();
343 // platform-specific types
344 CompilerType &pid_type = int_type;
345 CompilerType &uid_type = uint_type;
346 CompilerType &clock_type = long_type;
347 CompilerType &band_type = long_type;
349 CompilerType sigval_type = ast->CreateRecordType(
350 nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_sigval_t",
351 llvm::to_underlying(clang::TagTypeKind::Union), lldb::eLanguageTypeC);
352 ast->StartTagDeclarationDefinition(sigval_type);
353 ast->AddFieldToRecordType(sigval_type, "sival_int", int_type,
354 lldb::eAccessPublic, 0);
355 ast->AddFieldToRecordType(sigval_type, "sival_ptr", voidp_type,
356 lldb::eAccessPublic, 0);
357 ast->CompleteTagDeclarationDefinition(sigval_type);
359 CompilerType sigfault_bounds_type = ast->CreateRecordType(
360 nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "",
361 llvm::to_underlying(clang::TagTypeKind::Union), lldb::eLanguageTypeC);
362 ast->StartTagDeclarationDefinition(sigfault_bounds_type);
363 ast->AddFieldToRecordType(
364 sigfault_bounds_type, "_addr_bnd",
365 ast->CreateStructForIdentifier(llvm::StringRef(),
367 {"_lower", voidp_type},
368 {"_upper", voidp_type},
370 lldb::eAccessPublic, 0);
371 ast->AddFieldToRecordType(sigfault_bounds_type, "_pkey", uint_type,
372 lldb::eAccessPublic, 0);
373 ast->CompleteTagDeclarationDefinition(sigfault_bounds_type);
375 // siginfo_t
376 CompilerType siginfo_type = ast->CreateRecordType(
377 nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_siginfo_t",
378 llvm::to_underlying(clang::TagTypeKind::Struct), lldb::eLanguageTypeC);
379 ast->StartTagDeclarationDefinition(siginfo_type);
380 ast->AddFieldToRecordType(siginfo_type, "si_signo", int_type,
381 lldb::eAccessPublic, 0);
383 if (si_errno_then_code) {
384 ast->AddFieldToRecordType(siginfo_type, "si_errno", int_type,
385 lldb::eAccessPublic, 0);
386 ast->AddFieldToRecordType(siginfo_type, "si_code", int_type,
387 lldb::eAccessPublic, 0);
388 } else {
389 ast->AddFieldToRecordType(siginfo_type, "si_code", int_type,
390 lldb::eAccessPublic, 0);
391 ast->AddFieldToRecordType(siginfo_type, "si_errno", int_type,
392 lldb::eAccessPublic, 0);
395 // the structure is padded on 64-bit arches to fix alignment
396 if (triple.isArch64Bit())
397 ast->AddFieldToRecordType(siginfo_type, "__pad0", int_type,
398 lldb::eAccessPublic, 0);
400 // union used to hold the signal data
401 CompilerType union_type = ast->CreateRecordType(
402 nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "",
403 llvm::to_underlying(clang::TagTypeKind::Union), lldb::eLanguageTypeC);
404 ast->StartTagDeclarationDefinition(union_type);
406 ast->AddFieldToRecordType(
407 union_type, "_kill",
408 ast->CreateStructForIdentifier(llvm::StringRef(),
410 {"si_pid", pid_type},
411 {"si_uid", uid_type},
413 lldb::eAccessPublic, 0);
415 ast->AddFieldToRecordType(
416 union_type, "_timer",
417 ast->CreateStructForIdentifier(llvm::StringRef(),
419 {"si_tid", int_type},
420 {"si_overrun", int_type},
421 {"si_sigval", sigval_type},
423 lldb::eAccessPublic, 0);
425 ast->AddFieldToRecordType(
426 union_type, "_rt",
427 ast->CreateStructForIdentifier(llvm::StringRef(),
429 {"si_pid", pid_type},
430 {"si_uid", uid_type},
431 {"si_sigval", sigval_type},
433 lldb::eAccessPublic, 0);
435 ast->AddFieldToRecordType(
436 union_type, "_sigchld",
437 ast->CreateStructForIdentifier(llvm::StringRef(),
439 {"si_pid", pid_type},
440 {"si_uid", uid_type},
441 {"si_status", int_type},
442 {"si_utime", clock_type},
443 {"si_stime", clock_type},
445 lldb::eAccessPublic, 0);
447 ast->AddFieldToRecordType(
448 union_type, "_sigfault",
449 ast->CreateStructForIdentifier(llvm::StringRef(),
451 {"si_addr", voidp_type},
452 {"si_addr_lsb", short_type},
453 {"_bounds", sigfault_bounds_type},
455 lldb::eAccessPublic, 0);
457 ast->AddFieldToRecordType(
458 union_type, "_sigpoll",
459 ast->CreateStructForIdentifier(llvm::StringRef(),
461 {"si_band", band_type},
462 {"si_fd", int_type},
464 lldb::eAccessPublic, 0);
466 // NB: SIGSYS is not present on ia64 but we don't seem to support that
467 ast->AddFieldToRecordType(
468 union_type, "_sigsys",
469 ast->CreateStructForIdentifier(llvm::StringRef(),
471 {"_call_addr", voidp_type},
472 {"_syscall", int_type},
473 {"_arch", uint_type},
475 lldb::eAccessPublic, 0);
477 ast->CompleteTagDeclarationDefinition(union_type);
478 ast->AddFieldToRecordType(siginfo_type, "_sifields", union_type,
479 lldb::eAccessPublic, 0);
481 ast->CompleteTagDeclarationDefinition(siginfo_type);
482 return siginfo_type;