Skip to content

Commit

Permalink
Add user-friendly error for multiple for loops
Browse files Browse the repository at this point in the history
Currently, we do not allow for-loops to be used from multiple probes at
the same time (see [1] for details). If that happens, detect it in the
semantic analyser and print a nice error instead of a generic "Error
loading program".

[1] #3021
  • Loading branch information
viktormalik committed Jun 19, 2024
1 parent 91592d8 commit cdf9e9d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/ast/passes/semantic_analyser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2065,6 +2065,13 @@ void SemanticAnalyser::visit(For &f)
* }
*/

if (scope_with_for_loop_.has_value() && *scope_with_for_loop_ != scope_) {
LOG(ERROR, f.loc, err_)
<< "Currently, for-loops can be used only in a single probe.";
} else {
scope_with_for_loop_ = scope_;
}

// Validate decl
const auto &decl_name = f.decl->ident;
if (variable_val_[scope_].find(decl_name) != variable_val_[scope_].end()) {
Expand Down
1 change: 1 addition & 0 deletions src/ast/passes/semantic_analyser.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class SemanticAnalyser : public Visitor {
bool has_end_probe_ = false;
bool has_child_ = false;
bool has_pos_param_ = false;
std::optional<ast::Scope *> scope_with_for_loop_ = std::nullopt;
};

Pass CreateSemanticPass();
Expand Down
14 changes: 14 additions & 0 deletions tests/semantic_analyser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3767,6 +3767,20 @@ kprobe:f { @map[0] = 1; for ($kv : @map) { arg0 } }
)");
}

TEST(semantic_analyser, for_loop_multiple_probes)
{
test_error(R"(
BEGIN { @map[0] = 1 }
k:f1 { for ($kv : @map) { print($kv); } }
k:f2 { for ($kv : @map) { print($kv); } }
)",
R"(
stdin:3:14-17: ERROR: Currently, for-loops can be used only in a single probe.
k:f2 { for ($kv : @map) { print($kv); } }
~~~
)");
}

TEST_F(semantic_analyser_btf, args_builtin_mixed_probes)
{
test_error("kfunc:func_1,tracepoint:sched:sched_one { args }", R"(
Expand Down

0 comments on commit cdf9e9d

Please sign in to comment.