| 1 |
/* bpfinterp.h - SystemTap BPF interpreter |
| 2 |
* |
| 3 |
* This program is free software; you can redistribute it and/or modify |
| 4 |
* it under the terms of the GNU General Public License as published by |
| 5 |
* the Free Software Foundation; either version 2 of the License, or |
| 6 |
* (at your option) any later version. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
* |
| 16 |
* Copyright (C) 2016-2021 Red Hat, Inc. |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
#ifndef BPFINTERP_H |
| 21 |
#define BPFINTERP_H 1 |
| 22 |
|
| 23 |
#include <sys/types.h> |
| 24 |
#include <inttypes.h> |
| 25 |
#include <linux/bpf.h> |
| 26 |
|
| 27 |
extern "C" { |
| 28 |
#include "libbpf.h" |
| 29 |
} |
| 30 |
|
| 31 |
// Required constants such as BPF_MAXFORMATLEN: |
| 32 |
#include "../bpf-internal.h" |
| 33 |
|
| 34 |
// Used by the transport layer and interpreter: |
| 35 |
struct bpf_transport_context { |
| 36 |
// XXX: The following two fields are only used for kernel programs. |
| 37 |
// pmu_fd == -1 indicates context for a userspace interpreter. |
| 38 |
unsigned cpu; |
| 39 |
int pmu_fd; |
| 40 |
|
| 41 |
// References to global state: |
| 42 |
unsigned ncpus; |
| 43 |
bpf_map_def *map_attrs; |
| 44 |
std::vector<int> *map_fds; |
| 45 |
FILE *output_f; |
| 46 |
std::vector<std::string> *interned_strings; |
| 47 |
std::unordered_map<bpf::globals::agg_idx, bpf::globals::stats_map> *aggregates; |
| 48 |
std::vector<bpf::globals::foreach_info> *foreach_loop_info; |
| 49 |
// XXX: Could be refactored into a single global struct bpf_global_context. |
| 50 |
|
| 51 |
// Data for procfs probes. Multiple threads will be accessing this variable. |
| 52 |
// However, the procfs_lock should prevent any concurrency issues. |
| 53 |
std::string procfs_msg; |
| 54 |
|
| 55 |
// Data for an in-progress printf request: |
| 56 |
bool in_printf; |
| 57 |
int format_no; // -- index into table of interned strings |
| 58 |
unsigned expected_args; // -- expected number of printf_args |
| 59 |
std::vector<void *> printf_args; |
| 60 |
std::vector<bpf::globals::perf_event_type> printf_arg_types; // either ..ARG_LONG or ..ARG_STR |
| 61 |
|
| 62 |
// Used to communicate if a hard error has occured. |
| 63 |
bool* error; |
| 64 |
|
| 65 |
// Store error messages for printing later on. |
| 66 |
std::queue<std::string> error_message; |
| 67 |
|
| 68 |
// TODO: Takes a lot of arguments. Refactor to be less unwieldy. |
| 69 |
bpf_transport_context(unsigned cpu, int pmu_fd, unsigned ncpus, |
| 70 |
bpf_map_def *map_attrs, |
| 71 |
std::vector<int> *map_fds, |
| 72 |
FILE *output_f, |
| 73 |
std::vector<std::string> *interned_strings, |
| 74 |
std::unordered_map<bpf::globals::agg_idx, |
| 75 |
bpf::globals::stats_map> *aggregates, |
| 76 |
std::vector<bpf::globals::foreach_info> *foreach_loop_info, |
| 77 |
bool* error) |
| 78 |
: cpu(cpu), pmu_fd(pmu_fd), ncpus(ncpus), |
| 79 |
map_attrs(map_attrs), map_fds(map_fds), output_f(output_f), |
| 80 |
interned_strings(interned_strings), aggregates(aggregates), |
| 81 |
foreach_loop_info(foreach_loop_info), |
| 82 |
in_printf(false), format_no(-1), expected_args(0), error(error) {} |
| 83 |
}; |
| 84 |
|
| 85 |
enum bpf_perf_event_ret bpf_handle_transport_msg(void *buf, size_t size, |
| 86 |
bpf_transport_context *ctx); |
| 87 |
|
| 88 |
uint64_t bpf_interpret(size_t ninsns, |
| 89 |
const struct bpf_insn insns[], |
| 90 |
bpf_transport_context *ctx); |
| 91 |
|
| 92 |
extern "C" { |
| 93 |
extern int target_pid; |
| 94 |
}; |
| 95 |
|
| 96 |
#endif /* STAPRUNBPF_H */ |