Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add -q option for quiet #1616

Merged
merged 4 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ and this project adheres to
- [#1563](https://github.com/iovisor/bpftrace/pull/1563)
- Remove "BTF: using data from ..." message when using -v flag
- [#1554](https://github.com/iovisor/bpftrace/pull/1554)
- Add -q option for quiet
- [#1616](https://github.com/iovisor/bpftrace/pull/1616)

#### Deprecated

Expand Down
1 change: 1 addition & 0 deletions docs/reference_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ OPTIONS:
-l [search] list probes
-p PID enable USDT probes on PID
-c 'CMD' run CMD and enable USDT probes on resulting process
-q keep messages quiet
-v verbose messages
-k emit a warning when a bpf helper returns an error (except read functions)
-kk check all bpf helper functions
Expand Down
1 change: 1 addition & 0 deletions src/bpftrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ std::set<std::string> find_wildcard_matches_internal(
} // namespace

DebugLevel bt_debug = DebugLevel::kNone;
bool bt_quiet = false;
bool bt_verbose = false;
volatile sig_atomic_t BPFtrace::exitsig_recv = false;
const int FMT_BUF_SZ = 512;
Expand Down
1 change: 1 addition & 0 deletions src/bpftrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ enum class DebugLevel;

// globals
extern DebugLevel bt_debug;
extern bool bt_quiet;
extern bool bt_verbose;

enum class DebugLevel
Expand Down
11 changes: 8 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ void usage()
std::cerr << " --usdt-file-activation" << std::endl;
std::cerr << " activate usdt semaphores based on file path" << std::endl;
std::cerr << " --unsafe allow unsafe builtin functions" << std::endl;
std::cerr << " -q keep messages quiet" << std::endl;
std::cerr << " -v verbose messages" << std::endl;
std::cerr << " --info Print information about kernel BPF support" << std::endl;
std::cerr << " -k emit a warning when a bpf helper returns an error (except read functions)" << std::endl;
Expand Down Expand Up @@ -282,7 +283,7 @@ int main(int argc, char *argv[])
OutputBufferConfig obc = OutputBufferConfig::UNSET;
int c;

const char* const short_options = "dbB:f:e:hlp:vc:Vo:I:k";
const char* const short_options = "dbB:f:e:hlp:vqc:Vo:I:k";
option long_options[] = {
option{ "help", no_argument, nullptr, 'h' },
option{ "version", no_argument, nullptr, 'V' },
Expand Down Expand Up @@ -335,6 +336,9 @@ int main(int argc, char *argv[])
return 1;
}
break;
case 'q':
bt_quiet = true;
break;
case 'v':
bt_verbose = true;
break;
Expand Down Expand Up @@ -831,7 +835,8 @@ int main(int argc, char *argv[])
uint64_t num_probes = bpftrace.num_probes();
if (num_probes == 0)
{
std::cout << "No probes to attach" << std::endl;
if (!bt_quiet)
std::cout << "No probes to attach" << std::endl;
return 1;
}
else if (num_probes > bpftrace.max_probes_)
Expand All @@ -845,7 +850,7 @@ int main(int argc, char *argv[])
<< "attached can cause your system to crash.";
return 1;
}
else
else if (!bt_quiet)
bpftrace.out_->attached_probes(num_probes);

err = bpftrace.run(move(bpforc));
Expand Down
44 changes: 22 additions & 22 deletions tests/runtime/json-output
Original file line number Diff line number Diff line change
@@ -1,70 +1,70 @@
NAME invalid_format
RUN bpftrace -f jsonx -e 'BEGIN { @scalar = 5; exit(); }'
RUN bpftrace -q -f jsonx -e 'BEGIN { @scalar = 5; exit(); }'
EXPECT ^ERROR: Invalid output format "jsonx"$
TIMEOUT 5

NAME scalar
RUN bpftrace -f json -e 'BEGIN { @scalar = 5; exit(); }' | grep -v attached_probes | python -c 'import sys,json; print(json.load(sys.stdin) == json.load(open("runtime/outputs/scalar.json")))'
RUN bpftrace -q -f json -e 'BEGIN { @scalar = 5; exit(); }' | python -c 'import sys,json; print(json.load(sys.stdin) == json.load(open("runtime/outputs/scalar.json")))'
EXPECT ^True$
TIMEOUT 5

NAME scalar_str
RUN bpftrace -f json -e 'BEGIN { @scalar_str = "a b \n d e"; exit(); }' | grep -v attached_probes | python -c 'import sys,json; print(json.load(sys.stdin) == json.load(open("runtime/outputs/scalar_str.json")))'
RUN bpftrace -q -f json -e 'BEGIN { @scalar_str = "a b \n d e"; exit(); }' | python -c 'import sys,json; print(json.load(sys.stdin) == json.load(open("runtime/outputs/scalar_str.json")))'
EXPECT ^True$
TIMEOUT 5

NAME complex
RUN bpftrace -f json -e 'BEGIN { @complex[comm,2] = 5; exit(); }' | grep -v attached_probes | python -c 'import sys,json; print(json.load(sys.stdin) == json.load(open("runtime/outputs/complex.json")))'
RUN bpftrace -q -f json -e 'BEGIN { @complex[comm,2] = 5; exit(); }' | python -c 'import sys,json; print(json.load(sys.stdin) == json.load(open("runtime/outputs/complex.json")))'
EXPECT ^True$
TIMEOUT 5

NAME map
RUN bpftrace -f json -e 'BEGIN { @map["key1"] = 2; @map["key2"] = 3; exit(); }' | grep -v attached_probes | python -c 'import sys,json; print(json.load(sys.stdin) == json.load(open("runtime/outputs/map.json")))'
RUN bpftrace -q -f json -e 'BEGIN { @map["key1"] = 2; @map["key2"] = 3; exit(); }' | python -c 'import sys,json; print(json.load(sys.stdin) == json.load(open("runtime/outputs/map.json")))'
EXPECT ^True$
TIMEOUT 5

NAME histogram
RUN bpftrace -f json -e 'BEGIN { @hist = hist(2); @hist = hist(1025); exit(); }' | grep -v attached_probes | python -c 'import sys,json; print(json.load(sys.stdin) == json.load(open("runtime/outputs/hist.json")))'
RUN bpftrace -q -f json -e 'BEGIN { @hist = hist(2); @hist = hist(1025); exit(); }' | python -c 'import sys,json; print(json.load(sys.stdin) == json.load(open("runtime/outputs/hist.json")))'
EXPECT ^True$
TIMEOUT 5

NAME multiple histograms
RUN bpftrace -f json -e 'BEGIN { @["bpftrace"] = hist(2); @["curl"] = hist(-1); @["curl"] = hist(0); @["curl"] = hist(511); @["curl"] = hist(1024); @["curl"] = hist(1025); exit(); }' | grep -v attached_probes | python -c 'import sys,json; print(json.load(sys.stdin) == json.load(open("runtime/outputs/hist_multiple.json")))'
RUN bpftrace -q -f json -e 'BEGIN { @["bpftrace"] = hist(2); @["curl"] = hist(-1); @["curl"] = hist(0); @["curl"] = hist(511); @["curl"] = hist(1024); @["curl"] = hist(1025); exit(); }' | python -c 'import sys,json; print(json.load(sys.stdin) == json.load(open("runtime/outputs/hist_multiple.json")))'
EXPECT ^True$
TIMEOUT 5

NAME linear histogram
RUN bpftrace -f json -e 'BEGIN { @h = lhist(2, 0, 100, 10); @h = lhist(50, 0, 100, 10); @h = lhist(1000, 0, 100, 10); exit(); }' | grep -v attached_probes | python -c 'import sys,json; print(json.load(sys.stdin) == json.load(open("runtime/outputs/lhist.json")))'
RUN bpftrace -q -f json -e 'BEGIN { @h = lhist(2, 0, 100, 10); @h = lhist(50, 0, 100, 10); @h = lhist(1000, 0, 100, 10); exit(); }' | python -c 'import sys,json; print(json.load(sys.stdin) == json.load(open("runtime/outputs/lhist.json")))'
EXPECT ^True$
TIMEOUT 5

NAME multiple linear histograms
RUN bpftrace -f json -e 'BEGIN { @stats["bpftrace"] = lhist(2, 0, 100, 10); @stats["curl"] = lhist(50, 0, 100, 10); @stats["bpftrace"] = lhist(1000, 0, 100, 10); exit(); }' | grep -v attached_probes | python -c 'import sys,json; print(json.load(sys.stdin) == json.load(open("runtime/outputs/lhist_multiple.json")))'
RUN bpftrace -q -f json -e 'BEGIN { @stats["bpftrace"] = lhist(2, 0, 100, 10); @stats["curl"] = lhist(50, 0, 100, 10); @stats["bpftrace"] = lhist(1000, 0, 100, 10); exit(); }' | python -c 'import sys,json; print(json.load(sys.stdin) == json.load(open("runtime/outputs/lhist_multiple.json")))'
EXPECT ^True$
TIMEOUT 5

NAME stats
RUN bpftrace -f json -e 'BEGIN { @stats = stats(2); @stats = stats(10); exit(); }' | grep -v attached_probes | python -c 'import sys,json; print(json.load(sys.stdin) == json.load(open("runtime/outputs/stats.json")))'
RUN bpftrace -q -f json -e 'BEGIN { @stats = stats(2); @stats = stats(10); exit(); }' | python -c 'import sys,json; print(json.load(sys.stdin) == json.load(open("runtime/outputs/stats.json")))'
EXPECT ^True$
TIMEOUT 5

NAME multiple stats
RUN bpftrace -f json -e 'BEGIN { @stats["curl"] = stats(2); @stats["zsh"] = stats(10); exit(); }' | grep -v attached_probes | python -c 'import sys,json; print(json.load(sys.stdin) == json.load(open("runtime/outputs/stats_multiple.json")))'
RUN bpftrace -q -f json -e 'BEGIN { @stats["curl"] = stats(2); @stats["zsh"] = stats(10); exit(); }' | python -c 'import sys,json; print(json.load(sys.stdin) == json.load(open("runtime/outputs/stats_multiple.json")))'
EXPECT ^True$
TIMEOUT 5

NAME printf
RUN bpftrace -f json -v -e 'BEGIN { printf("test %d", 5); exit(); }'
RUN bpftrace -q -f json -v -e 'BEGIN { printf("test %d", 5); exit(); }'
EXPECT ^{"type": "printf", "data": "test 5"}$
TIMEOUT 5

NAME printf_escaping
RUN bpftrace -f json -v -e 'BEGIN { printf("test \r \n \t \\ \" bar"); exit(); }'
RUN bpftrace -q -f json -v -e 'BEGIN { printf("test \r \n \t \\ \" bar"); exit(); }'
EXPECT ^{"type": "printf", "data": "test \\r \\n \\t \\\\ \\\" bar"}$
TIMEOUT 5

NAME time
RUN bpftrace -f json -v -e 'BEGIN { time(); exit(); }'
RUN bpftrace -q -f json -v -e 'BEGIN { time(); exit(); }'
EXPECT ^{"type": "time", "data": "[0-9]*:[0-9]*:[0-9]*\\n"}$
TIMEOUT 5

Expand All @@ -86,41 +86,41 @@ TIMEOUT 5
# Careful with '[' and ']', they are read by the test engine as a regex
# character class, so make sure to escape them.
NAME tuple
RUN bpftrace -f json -e 'BEGIN { @ = (1, 2, "string", (4, 5)); exit(); }'
RUN bpftrace -q -f json -e 'BEGIN { @ = (1, 2, "string", (4, 5)); exit(); }'
EXPECT ^{"type": "map", "data": {"@": \[1,2,"string",\[4,5\]\]}}$
TIMEOUT 5

NAME print_non_map
RUN bpftrace -f json -e 'BEGIN { $x = 5; print($x); exit() }'
RUN bpftrace -q -f json -e 'BEGIN { $x = 5; print($x); exit() }'
EXPECT ^{"type": "value", "data": 5}$
TIMEOUT 1

NAME print_non_map_builtin
RUN bpftrace -f json -e 'BEGIN { print(comm); exit() }'
RUN bpftrace -q -f json -e 'BEGIN { print(comm); exit() }'
EXPECT ^{"type": "value", "data": "bpftrace"}$
TIMEOUT 1

NAME print_non_map_tuple
RUN bpftrace -f json -e 'BEGIN { $t = (1, 2, "string"); print($t); exit() }'
RUN bpftrace -q -f json -e 'BEGIN { $t = (1, 2, "string"); print($t); exit() }'
EXPECT ^{"type": "value", "data": \[1,2,"string"\]}$
TIMEOUT 1

NAME print_avg_map_args
RUN bpftrace -f json -e 'BEGIN { @["a"] = avg(10); @["b"] = avg(20); @["c"] = avg(30); @["d"] = avg(40); print(@, 2, 10); clear(@); exit(); }'
RUN bpftrace -q -f json -e 'BEGIN { @["a"] = avg(10); @["b"] = avg(20); @["c"] = avg(30); @["d"] = avg(40); print(@, 2, 10); clear(@); exit(); }'
EXPECT {"type": "stats", "data": {"@": { *"c": 3, *"d": 4}}}
TIMEOUT 1

NAME print_avg_map_with_large_top
RUN bpftrace -f json -e 'BEGIN { @["a"] = avg(10); @["b"] = avg(20); @["c"] = avg(30); @["d"] = avg(40); print(@, 10, 10); clear(@); exit(); }'
RUN bpftrace -q -f json -e 'BEGIN { @["a"] = avg(10); @["b"] = avg(20); @["c"] = avg(30); @["d"] = avg(40); print(@, 10, 10); clear(@); exit(); }'
EXPECT {"type": "stats", "data": {"@": { *"a": 1, *"b": 2, *"c": 3, *"d": 4}}}
TIMEOUT 1

NAME print_hist_with_top_arg
RUN bpftrace -f json -e 'BEGIN { @[1] = hist(10); @[2] = hist(20); @[3] = hist(30); print(@, 2); clear(@); exit(); }'
RUN bpftrace -q -f json -e 'BEGIN { @[1] = hist(10); @[2] = hist(20); @[3] = hist(30); print(@, 2); clear(@); exit(); }'
EXPECT {"type": "hist", "data": {"@": {"2": \[{"min": 16, "max": 31, "count": 1}\], "3": \[{"min": 16, "max": 31, "count": 1}\]}}}
TIMEOUT 1

NAME print_hist_with_large_top_arg
RUN bpftrace -f json -e 'BEGIN { @[1] = hist(10); @[2] = hist(20); @[3] = hist(30); print(@, 10); clear(@); exit(); }'
RUN bpftrace -q -f json -e 'BEGIN { @[1] = hist(10); @[2] = hist(20); @[3] = hist(30); print(@, 10); clear(@); exit(); }'
EXPECT {"type": "hist", "data": {"@": {"1": \[{"min": 8, "max": 15, "count": 1}\], "2": \[{"min": 16, "max": 31, "count": 1}\], "3": \[{"min": 16, "max": 31, "count": 1}\]}}}
TIMEOUT 1