Skip to content

Commit ef99dc4

Browse files
author
Pete Stevenson
authored
Refactor probe specs (struct definitions) into their own sub-tree. (#1631)
Summary: In anticipation of merging the record & replay feature, we move the probe specs out from `bcc_wrapper.h` into a new file `probe_specs.h` in its own sub-tree. This enables other parts of our source code to include these struct definitions without depending on the BCC wrapper itself. Relevant Issues: #1163 Type of change: /kind feature Test Plan: Existing tests. --------- Signed-off-by: Pete Stevenson <jps@pixielabs.ai>
1 parent 5e4a581 commit ef99dc4

File tree

4 files changed

+247
-158
lines changed

4 files changed

+247
-158
lines changed

src/stirling/bpf_tools/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pl_cc_library(
3838
"//src/common/system:cc_library",
3939
"//src/stirling/bpf_tools/bcc_bpf:task_struct_mem_read",
4040
"//src/stirling/bpf_tools/bcc_bpf_intf:cc_library",
41+
"//src/stirling/bpf_tools/probe_specs:cc_library",
4142
"//src/stirling/obj_tools:cc_library",
4243
"//src/stirling/utils:cc_library",
4344
"@com_github_iovisor_bcc//:bcc",

src/stirling/bpf_tools/bcc_wrapper.h

Lines changed: 1 addition & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
#include "src/common/base/base.h"
4646
#include "src/common/json/json.h"
47+
#include "src/stirling/bpf_tools/probe_specs/probe_specs.h"
4748
#include "src/stirling/bpf_tools/task_struct_resolver.h"
4849
#include "src/stirling/obj_tools/elf_reader.h"
4950

@@ -64,164 +65,6 @@ namespace px {
6465
namespace stirling {
6566
namespace bpf_tools {
6667

67-
enum class BPFProbeAttachType {
68-
// Attach to function entry.
69-
kEntry = BPF_PROBE_ENTRY,
70-
// Attach to function return (BCC native way, using stack).
71-
kReturn = BPF_PROBE_RETURN,
72-
// Attach to all function return instructions (required for golang).
73-
kReturnInsts,
74-
};
75-
76-
/**
77-
* Describes a kernel probe (kprobe).
78-
*/
79-
struct KProbeSpec {
80-
// Name of kernel function to probe (currently must be syscall).
81-
std::string_view kernel_fn;
82-
83-
// Whether this is an ENTRY or RETURN probe.
84-
BPFProbeAttachType attach_type = BPFProbeAttachType::kEntry;
85-
86-
// Name of user-provided function to run when event is triggered.
87-
std::string_view probe_fn;
88-
89-
// If true the kernel_fn is the short name of a syscall.
90-
bool is_syscall = true;
91-
92-
// Whether to fail if the kprobe doesn't deploy. Useful in case the symbol may not exist in some
93-
// kernels.
94-
bool is_optional = false;
95-
96-
std::string ToString() const {
97-
return absl::Substitute("[kernel_function=$0 type=$1 probe=$2]", kernel_fn,
98-
magic_enum::enum_name(attach_type), probe_fn);
99-
}
100-
};
101-
102-
/**
103-
* Describes a userspace probe (uprobe).
104-
*/
105-
struct UProbeSpec {
106-
// The canonical path to the binary to which this uprobe is attached.
107-
std::filesystem::path binary_path;
108-
109-
// Exactly one of symbol and address must be specified.
110-
std::string symbol;
111-
uint64_t address = 0;
112-
113-
// Must be identical to the default value of `pid` argument of BPF::{attach,detach}_uprobe().
114-
static constexpr pid_t kDefaultPID = -1;
115-
116-
// Specifies the target process to attach. This still requires setting binary_path, symbol or
117-
// address.
118-
pid_t pid = kDefaultPID;
119-
120-
BPFProbeAttachType attach_type = BPFProbeAttachType::kEntry;
121-
std::string probe_fn;
122-
bool is_optional = false;
123-
124-
std::string ToString() const {
125-
return absl::Substitute(
126-
"[binary=$0 symbol=$1 address=$2 pid=$3 type=$4 probe_fn=$5 optional=$6]",
127-
binary_path.string(), symbol, address, pid, magic_enum::enum_name(attach_type), probe_fn,
128-
is_optional);
129-
}
130-
131-
std::string ToJSON() const {
132-
::px::utils::JSONObjectBuilder builder;
133-
builder.WriteKV("binary", binary_path.string());
134-
builder.WriteKV("symbol", symbol);
135-
builder.WriteKV("address", static_cast<int64_t>(address));
136-
builder.WriteKV("pid", pid);
137-
builder.WriteKV("type", magic_enum::enum_name(attach_type));
138-
builder.WriteKV("probe_fn", probe_fn);
139-
return builder.GetString();
140-
}
141-
};
142-
143-
/**
144-
* Describes a probe on a pre-defined kernel tracepoint.
145-
*/
146-
struct TracepointSpec {
147-
std::string tracepoint;
148-
std::string probe_fn;
149-
150-
std::string ToString() const {
151-
return absl::Substitute("[tracepoint=$0 probe=$1]", tracepoint, probe_fn);
152-
}
153-
};
154-
155-
/**
156-
* Describes a sampling probe that triggers according to a time period.
157-
* This is in contrast to KProbes and UProbes, which trigger based on
158-
* a code event.
159-
*/
160-
struct SamplingProbeSpec {
161-
// Name of user-provided BPF function to run when probe is triggered.
162-
std::string_view probe_fn;
163-
164-
// Sampling period in milliseconds to trigger the probe.
165-
uint64_t period_millis;
166-
};
167-
168-
/**
169-
* PerfBufferSizeCategory specifies which category (currently Data or Control) a perf buffer belongs
170-
* to. This is used for accounting purposes, so that a maximum total size can be set per category.
171-
*/
172-
enum class PerfBufferSizeCategory {
173-
kUncategorized,
174-
kData,
175-
kControl,
176-
};
177-
178-
/**
179-
* Describes a BPF perf buffer, through which data is returned to user-space.
180-
*/
181-
struct PerfBufferSpec {
182-
// Name of the perf buffer.
183-
// Must be the same as the perf buffer name declared in the probe code with BPF_PERF_OUTPUT.
184-
std::string name;
185-
186-
// Function that will be called for every event in the perf buffer,
187-
// when perf buffer read is triggered.
188-
perf_reader_raw_cb probe_output_fn;
189-
190-
// Function that will be called if there are lost/clobbered perf events.
191-
perf_reader_lost_cb probe_loss_fn;
192-
193-
// Size of perf buffer. Will be rounded up to and allocated in a power of 2 number of pages.
194-
int size_bytes = 1024 * 1024;
195-
196-
// We specify a maximum total size per PerfBufferSizeCategory, this specifies which size category
197-
// to count this buffer's size against.
198-
PerfBufferSizeCategory size_category = PerfBufferSizeCategory::kUncategorized;
199-
200-
std::string ToString() const {
201-
return absl::Substitute("name=$0 size_bytes=$1 size_category=$2", name, size_bytes,
202-
magic_enum::enum_name(size_category));
203-
}
204-
};
205-
206-
/**
207-
* Describes a perf event to attach.
208-
* This can be run stand-alone and is not dependent on kProbes.
209-
*/
210-
struct PerfEventSpec {
211-
// The type of perf event (e.g. PERF_TYPE_HARDWARE, PERF_TYPE_SOFTWARE, etc.)
212-
perf_type_id type;
213-
214-
// The actual event to be counted (e.g. PERF_COUNT_HW_CPU_CYCLES).
215-
uint32_t config;
216-
217-
// Name of user-provided function to run when event is triggered.
218-
std::string_view probe_fn;
219-
220-
// Sampling period in number of events.
221-
// Mutually exclusive with sample_freq.
222-
uint64_t sample_period;
223-
};
224-
22568
/**
22669
* Wrapper around BCC, as a convenience.
22770
*/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2018- The Pixie Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
16+
17+
load("//bazel:pl_build_system.bzl", "pl_cc_library")
18+
19+
package(default_visibility = ["//src/stirling:__subpackages__"])
20+
21+
pl_cc_library(
22+
name = "cc_library",
23+
srcs = glob(
24+
["*.cc"],
25+
exclude = [
26+
"**/*_test.cc",
27+
],
28+
),
29+
hdrs = glob(
30+
[
31+
"*.h",
32+
],
33+
),
34+
deps = [
35+
"//src/common/json:cc_library",
36+
"@com_github_iovisor_bcc//:bcc",
37+
],
38+
)

0 commit comments

Comments
 (0)