libclues
Linux C++ Tracing Library
Loading...
Searching...
No Matches
utils.hxx
1#pragma once
2
3// C++
4#include <array>
5#include <functional>
6#include <optional>
7#include <set>
8#include <vector>
9
10// cosmos
11#include <cosmos/compiler.hxx>
12#include <cosmos/error/errno.hxx>
13#include <cosmos/fs/types.hxx>
14#include <cosmos/proc/ptrace.hxx>
15
16// clues
17#include <clues/fwd.hxx>
18#include <clues/sysnrs/fwd.hxx>
19#include <clues/types.hxx>
20
21namespace clues {
22
24const char* get_errno_label(const cosmos::Errno err);
26const char* get_kernel_errno_label(const KernelErrno err);
28const char* get_ptrace_event_str(const cosmos::ptrace::Event event);
29
31
35std::set<cosmos::FileNum> get_currently_open_fds(const cosmos::ProcessID pid);
36
38
42std::vector<FDInfo> get_fd_infos(const cosmos::ProcessID pid);
43
45CLUES_API std::optional<SystemCallNr> lookup_system_call(
46 const std::string_view name);
47
49
56CLUES_API bool is_default_abi(const ABI abi);
57
59constexpr ABI get_default_abi() {
60 if (cosmos::arch::X32)
61 return ABI::X32;
62 else if (cosmos::arch::X86_64)
63 return ABI::X86_64;
64 else if (cosmos::arch::I386)
65 return ABI::I386;
66 else if (cosmos::arch::AARCH64)
67 return ABI::AARCH64;
68
69 return ABI::UNKNOWN;
70}
71
72#ifdef COSMOS_X86_64
73constexpr inline size_t SUPPORTED_ABIS = 3;
74#elif defined(COSMOS_I386)
75constexpr inline size_t SUPPORTED_ABIS = 1;
76#elif defined(COSMOS_AARCH64)
77constexpr inline size_t SUPPORTED_ABIS = 1;
78#else
79#error "no configuration yet for this platform"
80#endif
81
82CLUES_API const char* get_abi_label(const ABI abi);
83
85CLUES_API std::array<ABI, SUPPORTED_ABIS> get_supported_abis();
86
88inline bool is_supported_abi(const ABI abi) {
89 for (const auto supported: get_supported_abis()) {
90 if (supported == abi)
91 return true;
92 }
93
94 return false;
95}
96
98
107void CLUES_API parse_proc_file(const cosmos::ProcessID pid, const std::string_view subpath, std::function<bool(const std::string&)> parser);
108
110
113void CLUES_API parse_proc_file(const Tracee &tracee, const std::string_view subpath, std::function<bool(const std::string&)> parser);
114
115} // end ns
KernelErrno
Errno values that can appear in tracing context.
Definition types.hxx:49
const char * get_ptrace_event_str(const cosmos::ptrace::Event event)
Returns a string label for the given event.
Definition utils.cxx:47
constexpr ABI get_default_abi()
Returns the default ABI for this system.
Definition utils.hxx:59
ABI
System Call ABI.
Definition types.hxx:62
@ X32
X86_64 with 32-bit pointers.
Definition types.hxx:66
std::vector< FDInfo > get_fd_infos(const cosmos::ProcessID pid)
Obtain detailed information about currently open file descriptors according to /proc/<pid>/fd.
Definition utils.cxx:210
std::array< ABI, SUPPORTED_ABIS > get_supported_abis()
Returns a list of ABIs supported on the current platform.
Definition utils.cxx:299
std::optional< SystemCallNr > lookup_system_call(const std::string_view name)
Returns the SystemCallNr for the given system call name, if it exists.
Definition utils.cxx:271
const char * get_errno_label(const cosmos::Errno err)
Returns a short errno label like ENOENT for the given errno integer.
Definition utils.cxx:29
const char * get_kernel_errno_label(const KernelErrno err)
Returns a short errno label for extended KernelErrno codes.
Definition utils.cxx:37
bool is_supported_abi(const ABI abi)
Returns whether the given ABI is supported on the current platform.
Definition utils.hxx:88
std::set< cosmos::FileNum > get_currently_open_fds(const cosmos::ProcessID pid)
Returns the currently open file descriptors according to /proc/<pid>/fd.
Definition utils.cxx:62
void parse_proc_file(const cosmos::ProcessID pid, const std::string_view subpath, std::function< bool(const std::string &)> parser)
Parse a proc file of the given process using the given functor.
Definition utils.cxx:322
bool is_default_abi(const ABI abi)
Returns whether the given ABI is default ABI for this system.
Definition utils.cxx:280