Coverage Report

Created: 2023-09-30 09:22

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/Platform/Linux/PlatformLinux.cpp
Line
Count
Source (jump to first uncovered line)
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 <cstdio>
13
#if LLDB_ENABLE_POSIX
14
#include <sys/utsname.h>
15
#endif
16
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"
30
31
// Define these constants from Linux mman.h for use when targeting remote linux
32
// systems even when host has different values.
33
0
#define MAP_PRIVATE 2
34
0
#define MAP_ANON 0x20
35
36
using namespace lldb;
37
using namespace lldb_private;
38
using namespace lldb_private::platform_linux;
39
40
LLDB_PLUGIN_DEFINE(PlatformLinux)
41
42
static uint32_t g_initialize_count = 0;
43
44
45
400
PlatformSP PlatformLinux::CreateInstance(bool force, const ArchSpec *arch) {
46
400
  Log *log = GetLog(LLDBLog::Platform);
47
400
  LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force,
48
400
           arch ? arch->GetArchitectureName() : "<null>",
49
400
           arch ? arch->GetTriple().getTriple() : "<null>");
50
51
400
  bool create = force;
52
400
  if (!create && 
arch339
&&
arch->IsValid()339
) {
53
339
    const llvm::Triple &triple = arch->GetTriple();
54
339
    switch (triple.getOS()) {
55
102
    case llvm::Triple::Linux:
56
102
      create = true;
57
102
      break;
58
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
237
    default:
67
237
      break;
68
339
    }
69
339
  }
70
71
400
  LLDB_LOG(log, "create = {0}", create);
72
400
  if (create) {
73
163
    return PlatformSP(new PlatformLinux(false));
74
163
  }
75
237
  return PlatformSP();
76
400
}
77
78
3.96k
llvm::StringRef PlatformLinux::GetPluginDescriptionStatic(bool is_host) {
79
3.96k
  if (is_host)
80
0
    return "Local Linux user platform plug-in.";
81
3.96k
  return "Remote Linux user platform plug-in.";
82
3.96k
}
83
84
7.91k
void PlatformLinux::Initialize() {
85
7.91k
  PlatformPOSIX::Initialize();
86
87
7.91k
  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
3.96k
    PluginManager::RegisterPlugin(
94
3.96k
        PlatformLinux::GetPluginNameStatic(false),
95
3.96k
        PlatformLinux::GetPluginDescriptionStatic(false),
96
3.96k
        PlatformLinux::CreateInstance, nullptr);
97
3.96k
  }
98
7.91k
}
99
100
7.90k
void PlatformLinux::Terminate() {
101
7.90k
  if (g_initialize_count > 0) {
102
7.90k
    if (--g_initialize_count == 0) {
103
3.95k
      PluginManager::UnregisterPlugin(PlatformLinux::CreateInstance);
104
3.95k
    }
105
7.90k
  }
106
107
7.90k
  PlatformPOSIX::Terminate();
108
7.90k
}
109
110
/// Default Constructor
111
PlatformLinux::PlatformLinux(bool is_host)
112
197
    : PlatformPOSIX(is_host) // This is the local host platform
113
197
{
114
197
  if (is_host) {
115
0
    ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
116
0
    m_supported_architectures.push_back(hostArch);
117
0
    if (hostArch.GetTriple().isArch64Bit()) {
118
0
      m_supported_architectures.push_back(
119
0
          HostInfo::GetArchitecture(HostInfo::eArchKind32));
120
0
    }
121
197
  } else {
122
197
    m_supported_architectures = CreateArchList(
123
197
        {llvm::Triple::x86_64, llvm::Triple::x86, llvm::Triple::arm,
124
197
         llvm::Triple::aarch64, llvm::Triple::mips64, llvm::Triple::mips64,
125
197
         llvm::Triple::hexagon, llvm::Triple::mips, llvm::Triple::mips64el,
126
197
         llvm::Triple::mipsel, llvm::Triple::msp430, llvm::Triple::systemz},
127
197
        llvm::Triple::Linux);
128
197
  }
129
197
}
130
131
std::vector<ArchSpec>
132
202
PlatformLinux::GetSupportedArchitectures(const ArchSpec &process_host_arch) {
133
202
  if (m_remote_platform_sp)
134
0
    return m_remote_platform_sp->GetSupportedArchitectures(process_host_arch);
135
202
  return m_supported_architectures;
136
202
}
137
138
17
void PlatformLinux::GetStatus(Stream &strm) {
139
17
  Platform::GetStatus(strm);
140
141
17
#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
17
  if (IsHost()) {
146
0
    struct utsname un;
147
148
0
    if (uname(&un))
149
0
      return;
150
151
0
    strm.Printf("    Kernel: %s\n", un.sysname);
152
0
    strm.Printf("   Release: %s\n", un.release);
153
0
    strm.Printf("   Version: %s\n", un.version);
154
0
  }
155
17
#endif
156
17
}
157
158
uint32_t
159
0
PlatformLinux::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) {
160
0
  uint32_t resume_count = 0;
161
162
  // Always resume past the initial stop when we use eLaunchFlagDebug
163
0
  if (launch_info.GetFlags().Test(eLaunchFlagDebug)) {
164
    // Resume past the stop for the final exec into the true inferior.
165
0
    ++resume_count;
166
0
  }
167
168
  // If we're not launching a shell, we're done.
169
0
  const FileSpec &shell = launch_info.GetShell();
170
0
  if (!shell)
171
0
    return resume_count;
172
173
0
  std::string shell_string = shell.GetPath();
174
  // We're in a shell, so for sure we have to resume past the shell exec.
175
0
  ++resume_count;
176
177
  // Figure out what shell we're planning on using.
178
0
  const char *shell_name = strrchr(shell_string.c_str(), '/');
179
0
  if (shell_name == nullptr)
180
0
    shell_name = shell_string.c_str();
181
0
  else
182
0
    shell_name++;
183
184
0
  if (strcmp(shell_name, "csh") == 0 || strcmp(shell_name, "tcsh") == 0 ||
185
0
      strcmp(shell_name, "zsh") == 0 || strcmp(shell_name, "sh") == 0) {
186
    // These shells seem to re-exec themselves.  Add another resume.
187
0
    ++resume_count;
188
0
  }
189
190
0
  return resume_count;
191
0
}
192
193
0
bool PlatformLinux::CanDebugProcess() {
194
0
  if (IsHost()) {
195
0
    return true;
196
0
  } else {
197
    // If we're connected, we can debug.
198
0
    return IsConnected();
199
0
  }
200
0
}
201
202
23
void PlatformLinux::CalculateTrapHandlerSymbolNames() {
203
23
  m_trap_handlers.push_back(ConstString("_sigtramp"));
204
23
  m_trap_handlers.push_back(ConstString("__kernel_rt_sigreturn"));
205
23
  m_trap_handlers.push_back(ConstString("__restore_rt"));
206
23
}
207
208
0
static lldb::UnwindPlanSP GetAArch64TrapHanlderUnwindPlan(ConstString name) {
209
0
  UnwindPlanSP unwind_plan_sp;
210
0
  if (name != "__kernel_rt_sigreturn")
211
0
    return unwind_plan_sp;
212
213
0
  UnwindPlan::RowSP row = std::make_shared<UnwindPlan::Row>();
214
0
  row->SetOffset(0);
215
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
0
  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
235
236
  // Skip fault address
237
0
  offset += 8;
238
0
  row->GetCFAValue().SetIsRegisterPlusOffset(arm64_dwarf::sp, offset);
239
240
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x0, 0 * 8, false);
241
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x1, 1 * 8, false);
242
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x2, 2 * 8, false);
243
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x3, 3 * 8, false);
244
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x4, 4 * 8, false);
245
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x5, 5 * 8, false);
246
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x6, 6 * 8, false);
247
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x7, 7 * 8, false);
248
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x8, 8 * 8, false);
249
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x9, 9 * 8, false);
250
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x10, 10 * 8, false);
251
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x11, 11 * 8, false);
252
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x12, 12 * 8, false);
253
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x13, 13 * 8, false);
254
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x14, 14 * 8, false);
255
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x15, 15 * 8, false);
256
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x16, 16 * 8, false);
257
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x17, 17 * 8, false);
258
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x18, 18 * 8, false);
259
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x19, 19 * 8, false);
260
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x20, 20 * 8, false);
261
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x21, 21 * 8, false);
262
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x22, 22 * 8, false);
263
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x23, 23 * 8, false);
264
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x24, 24 * 8, false);
265
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x25, 25 * 8, false);
266
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x26, 26 * 8, false);
267
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x27, 27 * 8, false);
268
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x28, 28 * 8, false);
269
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::fp, 29 * 8, false);
270
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x30, 30 * 8, false);
271
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::sp, 31 * 8, false);
272
0
  row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::pc, 32 * 8, false);
273
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.
277
278
0
  unwind_plan_sp = std::make_shared<UnwindPlan>(eRegisterKindDWARF);
279
0
  unwind_plan_sp->AppendRow(row);
280
0
  unwind_plan_sp->SetSourceName("AArch64 Linux sigcontext");
281
0
  unwind_plan_sp->SetSourcedFromCompiler(eLazyBoolYes);
282
  // Because sp is the same throughout the function
283
0
  unwind_plan_sp->SetUnwindPlanValidAtAllInstructions(eLazyBoolYes);
284
0
  unwind_plan_sp->SetUnwindPlanForSignalTrap(eLazyBoolYes);
285
286
0
  return unwind_plan_sp;
287
0
}
288
289
lldb::UnwindPlanSP
290
PlatformLinux::GetTrapHandlerUnwindPlan(const llvm::Triple &triple,
291
0
                                        ConstString name) {
292
0
  if (triple.isAArch64())
293
0
    return GetAArch64TrapHanlderUnwindPlan(name);
294
295
0
  return {};
296
0
}
297
298
MmapArgList PlatformLinux::GetMmapArgumentList(const ArchSpec &arch,
299
                                               addr_t addr, addr_t length,
300
                                               unsigned prot, unsigned flags,
301
0
                                               addr_t fd, addr_t offset) {
302
0
  uint64_t flags_platform = 0;
303
0
  uint64_t map_anon = arch.IsMIPS() ? 0x800 : MAP_ANON;
304
305
0
  if (flags & eMmapFlagsPrivate)
306
0
    flags_platform |= MAP_PRIVATE;
307
0
  if (flags & eMmapFlagsAnon)
308
0
    flags_platform |= map_anon;
309
310
0
  MmapArgList args({addr, length, prot, flags_platform, fd, offset});
311
0
  return args;
312
0
}
313
314
7
CompilerType PlatformLinux::GetSiginfoType(const llvm::Triple &triple) {
315
7
  {
316
7
    std::lock_guard<std::mutex> guard(m_mutex);
317
7
    if (!m_type_system)
318
7
      m_type_system = std::make_shared<TypeSystemClang>("siginfo", triple);
319
7
  }
320
7
  TypeSystemClang *ast = m_type_system.get();
321
322
7
  bool si_errno_then_code = true;
323
324
7
  switch (triple.getArch()) {
325
0
  case llvm::Triple::mips:
326
0
  case llvm::Triple::mipsel:
327
0
  case llvm::Triple::mips64:
328
0
  case llvm::Triple::mips64el:
329
    // mips has si_code and si_errno swapped
330
0
    si_errno_then_code = false;
331
0
    break;
332
7
  default:
333
7
    break;
334
7
  }
335
336
  // generic types
337
7
  CompilerType int_type = ast->GetBasicType(eBasicTypeInt);
338
7
  CompilerType uint_type = ast->GetBasicType(eBasicTypeUnsignedInt);
339
7
  CompilerType short_type = ast->GetBasicType(eBasicTypeShort);
340
7
  CompilerType long_type = ast->GetBasicType(eBasicTypeLong);
341
7
  CompilerType voidp_type = ast->GetBasicType(eBasicTypeVoid).GetPointerType();
342
343
  // platform-specific types
344
7
  CompilerType &pid_type = int_type;
345
7
  CompilerType &uid_type = uint_type;
346
7
  CompilerType &clock_type = long_type;
347
7
  CompilerType &band_type = long_type;
348
349
7
  CompilerType sigval_type = ast->CreateRecordType(
350
7
      nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_sigval_t",
351
7
      clang::TTK_Union, lldb::eLanguageTypeC);
352
7
  ast->StartTagDeclarationDefinition(sigval_type);
353
7
  ast->AddFieldToRecordType(sigval_type, "sival_int", int_type,
354
7
                            lldb::eAccessPublic, 0);
355
7
  ast->AddFieldToRecordType(sigval_type, "sival_ptr", voidp_type,
356
7
                            lldb::eAccessPublic, 0);
357
7
  ast->CompleteTagDeclarationDefinition(sigval_type);
358
359
7
  CompilerType sigfault_bounds_type = ast->CreateRecordType(
360
7
      nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "",
361
7
      clang::TTK_Union, lldb::eLanguageTypeC);
362
7
  ast->StartTagDeclarationDefinition(sigfault_bounds_type);
363
7
  ast->AddFieldToRecordType(
364
7
      sigfault_bounds_type, "_addr_bnd",
365
7
      ast->CreateStructForIdentifier(llvm::StringRef(),
366
7
                                     {
367
7
                                         {"_lower", voidp_type},
368
7
                                         {"_upper", voidp_type},
369
7
                                     }),
370
7
      lldb::eAccessPublic, 0);
371
7
  ast->AddFieldToRecordType(sigfault_bounds_type, "_pkey", uint_type,
372
7
                            lldb::eAccessPublic, 0);
373
7
  ast->CompleteTagDeclarationDefinition(sigfault_bounds_type);
374
375
  // siginfo_t
376
7
  CompilerType siginfo_type = ast->CreateRecordType(
377
7
      nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_siginfo_t",
378
7
      clang::TTK_Struct, lldb::eLanguageTypeC);
379
7
  ast->StartTagDeclarationDefinition(siginfo_type);
380
7
  ast->AddFieldToRecordType(siginfo_type, "si_signo", int_type,
381
7
                            lldb::eAccessPublic, 0);
382
383
7
  if (si_errno_then_code) {
384
7
    ast->AddFieldToRecordType(siginfo_type, "si_errno", int_type,
385
7
                              lldb::eAccessPublic, 0);
386
7
    ast->AddFieldToRecordType(siginfo_type, "si_code", int_type,
387
7
                              lldb::eAccessPublic, 0);
388
7
  } else {
389
0
    ast->AddFieldToRecordType(siginfo_type, "si_code", int_type,
390
0
                              lldb::eAccessPublic, 0);
391
0
    ast->AddFieldToRecordType(siginfo_type, "si_errno", int_type,
392
0
                              lldb::eAccessPublic, 0);
393
0
  }
394
395
  // the structure is padded on 64-bit arches to fix alignment
396
7
  if (triple.isArch64Bit())
397
4
    ast->AddFieldToRecordType(siginfo_type, "__pad0", int_type,
398
4
                              lldb::eAccessPublic, 0);
399
400
  // union used to hold the signal data
401
7
  CompilerType union_type = ast->CreateRecordType(
402
7
      nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "",
403
7
      clang::TTK_Union, lldb::eLanguageTypeC);
404
7
  ast->StartTagDeclarationDefinition(union_type);
405
406
7
  ast->AddFieldToRecordType(
407
7
      union_type, "_kill",
408
7
      ast->CreateStructForIdentifier(llvm::StringRef(),
409
7
                                     {
410
7
                                         {"si_pid", pid_type},
411
7
                                         {"si_uid", uid_type},
412
7
                                     }),
413
7
      lldb::eAccessPublic, 0);
414
415
7
  ast->AddFieldToRecordType(
416
7
      union_type, "_timer",
417
7
      ast->CreateStructForIdentifier(llvm::StringRef(),
418
7
                                     {
419
7
                                         {"si_tid", int_type},
420
7
                                         {"si_overrun", int_type},
421
7
                                         {"si_sigval", sigval_type},
422
7
                                     }),
423
7
      lldb::eAccessPublic, 0);
424
425
7
  ast->AddFieldToRecordType(
426
7
      union_type, "_rt",
427
7
      ast->CreateStructForIdentifier(llvm::StringRef(),
428
7
                                     {
429
7
                                         {"si_pid", pid_type},
430
7
                                         {"si_uid", uid_type},
431
7
                                         {"si_sigval", sigval_type},
432
7
                                     }),
433
7
      lldb::eAccessPublic, 0);
434
435
7
  ast->AddFieldToRecordType(
436
7
      union_type, "_sigchld",
437
7
      ast->CreateStructForIdentifier(llvm::StringRef(),
438
7
                                     {
439
7
                                         {"si_pid", pid_type},
440
7
                                         {"si_uid", uid_type},
441
7
                                         {"si_status", int_type},
442
7
                                         {"si_utime", clock_type},
443
7
                                         {"si_stime", clock_type},
444
7
                                     }),
445
7
      lldb::eAccessPublic, 0);
446
447
7
  ast->AddFieldToRecordType(
448
7
      union_type, "_sigfault",
449
7
      ast->CreateStructForIdentifier(llvm::StringRef(),
450
7
                                     {
451
7
                                         {"si_addr", voidp_type},
452
7
                                         {"si_addr_lsb", short_type},
453
7
                                         {"_bounds", sigfault_bounds_type},
454
7
                                     }),
455
7
      lldb::eAccessPublic, 0);
456
457
7
  ast->AddFieldToRecordType(
458
7
      union_type, "_sigpoll",
459
7
      ast->CreateStructForIdentifier(llvm::StringRef(),
460
7
                                     {
461
7
                                         {"si_band", band_type},
462
7
                                         {"si_fd", int_type},
463
7
                                     }),
464
7
      lldb::eAccessPublic, 0);
465
466
  // NB: SIGSYS is not present on ia64 but we don't seem to support that
467
7
  ast->AddFieldToRecordType(
468
7
      union_type, "_sigsys",
469
7
      ast->CreateStructForIdentifier(llvm::StringRef(),
470
7
                                     {
471
7
                                         {"_call_addr", voidp_type},
472
7
                                         {"_syscall", int_type},
473
7
                                         {"_arch", uint_type},
474
7
                                     }),
475
7
      lldb::eAccessPublic, 0);
476
477
7
  ast->CompleteTagDeclarationDefinition(union_type);
478
7
  ast->AddFieldToRecordType(siginfo_type, "_sifields", union_type,
479
7
                            lldb::eAccessPublic, 0);
480
481
7
  ast->CompleteTagDeclarationDefinition(siginfo_type);
482
7
  return siginfo_type;
483
7
}