LLDB mainline
Procfs.cpp
Go to the documentation of this file.
1//===-- Procfs.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 "Procfs.h"
11#include "llvm/Support/Error.h"
12#include "llvm/Support/MemoryBuffer.h"
13#include "llvm/Support/Threading.h"
14#include <optional>
15
16using namespace lldb;
17using namespace lldb_private;
18using namespace process_linux;
19using namespace llvm;
20
21Expected<ArrayRef<uint8_t>> lldb_private::process_linux::GetProcfsCpuInfo() {
22 static ErrorOr<std::unique_ptr<MemoryBuffer>> cpu_info_or_err =
23 getProcFile("cpuinfo");
24
25 if (!*cpu_info_or_err)
26 cpu_info_or_err.getError();
27
28 MemoryBuffer &buffer = **cpu_info_or_err;
29 return arrayRefFromStringRef(buffer.getBuffer());
30}
31
32Expected<std::vector<cpu_id_t>>
34 SmallVector<StringRef, 8> lines;
35 cpuinfo.split(lines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
36 std::vector<cpu_id_t> logical_cores;
37
38 for (StringRef line : lines) {
39 std::pair<StringRef, StringRef> key_value = line.split(':');
40 auto key = key_value.first.trim();
41 auto val = key_value.second.trim();
42 if (key == "processor") {
43 cpu_id_t processor;
44 if (val.getAsInteger(10, processor))
45 return createStringError(
46 inconvertibleErrorCode(),
47 "Failed parsing the /proc/cpuinfo line entry: %s", line.data());
48 logical_cores.push_back(processor);
49 }
50 }
51 return logical_cores;
52}
53
54llvm::Expected<llvm::ArrayRef<cpu_id_t>>
56 static std::optional<std::vector<cpu_id_t>> logical_cores_ids;
57 if (!logical_cores_ids) {
58 // We find the actual list of core ids by parsing /proc/cpuinfo
59 Expected<ArrayRef<uint8_t>> cpuinfo = GetProcfsCpuInfo();
60 if (!cpuinfo)
61 return cpuinfo.takeError();
62
63 Expected<std::vector<cpu_id_t>> cpu_ids = GetAvailableLogicalCoreIDs(
64 StringRef(reinterpret_cast<const char *>(cpuinfo->data())));
65 if (!cpu_ids)
66 return cpu_ids.takeError();
67
68 logical_cores_ids.emplace(std::move(*cpu_ids));
69 }
70 return *logical_cores_ids;
71}
72
74 ErrorOr<std::unique_ptr<MemoryBuffer>> ptrace_scope_file =
75 getProcFile("sys/kernel/yama/ptrace_scope");
76 if (!*ptrace_scope_file)
77 return errorCodeToError(ptrace_scope_file.getError());
78 // The contents should be something like "1\n". Trim it so we get "1".
79 StringRef buffer = (*ptrace_scope_file)->getBuffer().trim();
80 int ptrace_scope_value;
81 if (buffer.getAsInteger(10, ptrace_scope_value)) {
82 return createStringError(inconvertibleErrorCode(),
83 "Invalid ptrace_scope value: '%s'", buffer.data());
84 }
85 return ptrace_scope_value;
86}
llvm::Expected< int > GetPtraceScope()
Definition: Procfs.cpp:73
llvm::Expected< llvm::ArrayRef< lldb::cpu_id_t > > GetAvailableLogicalCoreIDs()
Definition: Procfs.cpp:55
llvm::Expected< llvm::ArrayRef< uint8_t > > GetProcfsCpuInfo()
Definition: Procfs.cpp:21
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getProcFile(::pid_t pid, ::pid_t tid, const llvm::Twine &file)
Definition: Support.cpp:15
Definition: SBAddress.h:15
Definition: Debugger.h:52