Skip to content

Commit 7b9b4e9

Browse files
Jordan Romeviktormalik
authored andcommitted
Standardize async id creation and setting
This will make sure we don't forget to reset async ids in 'create_reset_ids' and also makes sure we're incrementing the id whenever we get it (instead of doing it at some later, random instruction). This is follow up to a conversation here: bpftrace#3249 (comment)
1 parent 8b0a62a commit 7b9b4e9

File tree

5 files changed

+102
-97
lines changed

5 files changed

+102
-97
lines changed

src/ast/async_ids.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#pragma once
2+
3+
namespace bpftrace {
4+
namespace ast {
5+
6+
// Add new ids here
7+
#define FOR_LIST_OF_ASYNC_IDS(DO) \
8+
DO(cat) \
9+
DO(cgroup_path) \
10+
DO(helper_error) \
11+
DO(join) \
12+
DO(mapped_printf) \
13+
DO(non_map_print) \
14+
DO(printf) \
15+
DO(skb_output) \
16+
DO(strftime) \
17+
DO(str) \
18+
DO(system) \
19+
DO(time) \
20+
DO(watchpoint)
21+
22+
#define DEFINE_MEMBER_VAR(id, ...) int _##id = 0;
23+
#define DEFINE_ACCESS_METHOD(id, ...) \
24+
int id() \
25+
{ \
26+
return _##id++; \
27+
}
28+
#define LOCAL_SAVE(id, ...) local_##id = _##id,
29+
#define LOCAL_RESTORE(id, ...) this->_##id = local_##id;
30+
31+
class AsyncIds {
32+
public:
33+
explicit AsyncIds() = default;
34+
35+
FOR_LIST_OF_ASYNC_IDS(DEFINE_ACCESS_METHOD)
36+
37+
/*
38+
* For 'create_reset_ids' return a lambda that has captured-by-value
39+
* CodegenLLVM's async id state. Running the returned lambda will restore
40+
* `CodegenLLVM`s async id state back to when this function was first called.
41+
*/
42+
std::function<void()> create_reset_ids()
43+
{
44+
return [FOR_LIST_OF_ASYNC_IDS(LOCAL_SAVE) this] {
45+
FOR_LIST_OF_ASYNC_IDS(LOCAL_RESTORE)
46+
};
47+
}
48+
49+
private:
50+
FOR_LIST_OF_ASYNC_IDS(DEFINE_MEMBER_VAR)
51+
};
52+
53+
} // namespace ast
54+
} // namespace bpftrace

src/ast/irbuilderbpf.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,12 @@ StructType *IRBuilderBPF::GetStructType(
124124

125125
IRBuilderBPF::IRBuilderBPF(LLVMContext &context,
126126
Module &module,
127-
BPFtrace &bpftrace)
128-
: IRBuilder<>(context), module_(module), bpftrace_(bpftrace)
127+
BPFtrace &bpftrace,
128+
AsyncIds &async_ids)
129+
: IRBuilder<>(context),
130+
module_(module),
131+
bpftrace_(bpftrace),
132+
async_ids_(async_ids)
129133
{
130134
// Declare external LLVM function
131135
FunctionType *pseudo_func_type = FunctionType::get(
@@ -2246,7 +2250,7 @@ void IRBuilderBPF::CreateHelperError(Value *ctx,
22462250
(bpftrace_.helper_check_level_ == 1 && return_zero_if_err(func_id)))
22472251
return;
22482252

2249-
int error_id = helper_error_id_++;
2253+
int error_id = async_ids_.helper_error();
22502254
bpftrace_.resources.helper_error_info[error_id] = { .func_id = func_id,
22512255
.loc = loc };
22522256

src/ast/irbuilderbpf.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <optional>
99

1010
#include "ast/ast.h"
11+
#include "ast/async_ids.h"
1112
#include "bpftrace.h"
1213
#include "types.h"
1314

@@ -51,7 +52,10 @@ using namespace llvm;
5152

5253
class IRBuilderBPF : public IRBuilder<> {
5354
public:
54-
IRBuilderBPF(LLVMContext &context, Module &module, BPFtrace &bpftrace);
55+
IRBuilderBPF(LLVMContext &context,
56+
Module &module,
57+
BPFtrace &bpftrace,
58+
AsyncIds &async_ids);
5559

5660
AllocaInst *CreateAllocaBPF(llvm::Type *ty, const std::string &name = "");
5761
AllocaInst *CreateAllocaBPF(const SizedType &stype,
@@ -279,14 +283,14 @@ class IRBuilderBPF : public IRBuilder<> {
279283
// both branches here:
280284
// BEGIN { if (nsecs > 0) { $a = 1 } else { $a = 2 } print($a); exit() }
281285
void hoist(const std::function<void()> &functor);
282-
int helper_error_id_ = 0;
283286

284287
// Returns the integer type used to represent pointers in traced code.
285288
llvm::Type *getPointerStorageTy(AddrSpace as);
286289

287290
private:
288291
Module &module_;
289292
BPFtrace &bpftrace_;
293+
AsyncIds &async_ids_;
290294

291295
Value *CreateUSDTReadArgument(Value *ctx,
292296
struct bcc_usdt_argument *argument,

0 commit comments

Comments
 (0)