File: config_analyser.cpp

package info (click to toggle)
bpftrace 0.24.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,496 kB
  • sloc: cpp: 60,982; ansic: 10,952; python: 953; yacc: 665; sh: 536; lex: 295; makefile: 22
file content (172 lines) | stat: -rw-r--r-- 5,207 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "ast/passes/config_analyser.h"
#include "driver.h"
#include "mocks.h"
#include "types.h"
#include "gtest/gtest.h"

namespace bpftrace::test::config_analyser {

using ::testing::_;
using ::testing::HasSubstr;

void test(BPFtrace &bpftrace,
          const std::string &input,
          std::string_view expected_error,
          bool expected_result)
{
  ast::ASTContext ast("stdin", input);
  std::stringstream msg;
  msg << "\nInput:\n" << input << "\n\nOutput:\n";

  auto ok = ast::PassManager()
                .put(ast)
                .put(bpftrace)
                .add(CreateParsePass())
                .add(ast::CreateConfigPass())
                .run();
  ASSERT_TRUE(bool(ok)) << msg.str();
  EXPECT_EQ(ast.diagnostics().ok(), expected_result) << msg.str();

  if (expected_error.data()) {
    if (!expected_error.empty() && expected_error[0] == '\n')
      expected_error.remove_prefix(1); // Remove initial '\n'

    // Reproduce the full string.
    std::stringstream out;
    ast.diagnostics().emit(out);
    EXPECT_EQ(out.str(), expected_error);
  }
}

ast::ASTContext test_for_warning(BPFtrace &bpftrace,
                                 const std::string &input,
                                 const std::string &warning,
                                 bool invert)
{
  ast::ASTContext ast("stdin", input);

  auto ok = ast::PassManager()
                .put(ast)
                .put(bpftrace)
                .add(CreateParsePass())
                .add(ast::CreateConfigPass())
                .run();
  EXPECT_TRUE(bool(ok));

  std::stringstream out;
  ast.diagnostics().emit(out);
  if (invert)
    EXPECT_THAT(out.str(), Not(HasSubstr(warning)));
  else
    EXPECT_THAT(out.str(), HasSubstr(warning));

  return ast;
}

ast::ASTContext test_for_warning(const std::string &input,
                                 const std::string &warning)
{
  auto bpftrace = get_mock_bpftrace();
  return test_for_warning(*bpftrace, input, warning, false);
}

ast::ASTContext test_for_no_warning(const std::string &input,
                                    const std::string &warning)
{
  auto bpftrace = get_mock_bpftrace();
  return test_for_warning(*bpftrace, input, warning, true);
}

void test(const std::string &input, bool expected_result)
{
  auto bpftrace = get_mock_bpftrace();

  test(*bpftrace, input, {}, expected_result);
}

void test(const std::string &input,
          std::string_view expected_error,
          bool expected_result)
{
  auto bpftrace = get_mock_bpftrace();

  test(*bpftrace, input, expected_error, expected_result);
}

void test(BPFtrace &bpftrace, const std::string &input)
{
  test(bpftrace, input, {}, true);
}

TEST(config_analyser, config)
{
  test("config = { BAD_CONFIG=1 } begin { }", false);
  test("config = { BPFTRACE_MAX_MAP_KEYS=1 } begin { }", true);

  test("config = { BPFTRACE_MAX_MAP_KEYS=perf } begin { }", false);
  test("config = { BPFTRACE_STACK_MODE=perf } begin { }", true);
  test("config = { stack_mode=perf } begin { }", true);
  test("config = { BPFTRACE_MAX_MAP_KEYS=1; stack_mode=perf } begin { $ns = "
       "nsecs(); }",
       true);
  test("config = { BPFTRACE_CACHE_USER_SYMBOLS=\"PER_PROGRAM\" } begin { $ns = "
       "nsecs(); }",
       true);
}

TEST(config_analyser, config_error)
{
  test("config = { BAD_CONFIG=1 } begin { }",
       R"(stdin:1:12-24: ERROR: BAD_CONFIG: not a known configuration option
config = { BAD_CONFIG=1 } begin { }
           ~~~~~~~~~~~~
)",
       false);
  test(
      "config = { BPFTRACE_MAX_PROBES = \"hello\" } begin { }",
      R"(stdin:1:12-41: ERROR: BPFTRACE_MAX_PROBES: expecting a number, got hello
config = { BPFTRACE_MAX_PROBES = "hello" } begin { }
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
)",
      false);
  test(
      "config = { max_ast_nodes=1 } begin { }",
      R"(stdin:1:12-27: ERROR: max_ast_nodes: can only be set as an environment variable
config = { max_ast_nodes=1 } begin { }
           ~~~~~~~~~~~~~~~
)",
      false);
}

TEST(config_analyser, deprecated)
{
  test_for_warning("config = { symbol_source=\"symbol_table\" } begin { }",
                   "symbol_source is deprecated and has no effect");
  test_for_warning("config = { symbol_source=\"zzz\" } begin { }",
                   "symbol_source is deprecated and has no effect");
}

TEST(config_analyser, config_setting)
{
  auto bpftrace = get_mock_bpftrace();

  EXPECT_NE(bpftrace->config_->max_map_keys, 9);
  test(*bpftrace, "config = { BPFTRACE_MAX_MAP_KEYS=9 } begin { }");
  EXPECT_EQ(bpftrace->config_->max_map_keys, 9);

  EXPECT_NE(bpftrace->config_->stack_mode, StackMode::perf);
  test(*bpftrace, "config = { stack_mode=perf } begin { }");
  EXPECT_EQ(bpftrace->config_->stack_mode, StackMode::perf);

  EXPECT_NE(bpftrace->config_->user_symbol_cache_type,
            UserSymbolCacheType::per_program);
  EXPECT_NE(bpftrace->config_->log_size, 150);
  test(*bpftrace,
       "config = { BPFTRACE_CACHE_USER_SYMBOLS=\"PER_PROGRAM\"; log_size=150 "
       "} begin { }");
  EXPECT_EQ(bpftrace->config_->user_symbol_cache_type,
            UserSymbolCacheType::per_program);
  EXPECT_EQ(bpftrace->config_->log_size, 150);
}

} // namespace bpftrace::test::config_analyser