Commit 2881f7f3 authored by williangaspar's avatar williangaspar

tests

parent 38276a11
......@@ -677,33 +677,32 @@ void SemanticAnalyser::visit(Predicate &pred)
void SemanticAnalyser::visit(AttachPoint &ap)
{
auto type = probetypeName(ap.provider);
type = type == "" ? ap.provider : type;
ap.provider = probetypeName(ap.provider);
if (type == "kprobe" || type == "kretprobe") {
if (ap.provider == "kprobe" || ap.provider == "kretprobe") {
if (ap.target != "")
err_ << "kprobes should not have a target" << std::endl;
if (ap.func == "")
err_ << "kprobes should be attached to a function" << std::endl;
}
else if (type == "uprobe" || type == "uretprobe") {
else if (ap.provider == "uprobe" || ap.provider == "uretprobe") {
if (ap.target == "")
err_ << "uprobes should have a target" << std::endl;
if (ap.func == "")
err_ << "uprobes should be attached to a function" << std::endl;
}
else if (type == "usdt") {
else if (ap.provider == "usdt") {
if (ap.target == "" || ap.func == "")
err_ << "usdt probe must have a target" << std::endl;
struct stat s;
if (stat(ap.target.c_str(), &s) != 0)
err_ << "usdt target file " << ap.target << " does not exist" << std::endl;
}
else if (type == "tracepoint") {
else if (ap.provider == "tracepoint") {
if (ap.target == "" || ap.func == "")
err_ << "tracepoint probe must have a target" << std::endl;
}
else if (type == "profile") {
else if (ap.provider == "profile") {
if (ap.target == "")
err_ << "profile probe must have unit of time" << std::endl;
else if (ap.target != "hz" &&
......@@ -716,7 +715,7 @@ void SemanticAnalyser::visit(AttachPoint &ap)
else if (ap.freq <= 0)
err_ << "profile frequency should be a positive integer" << std::endl;
}
else if (type == "interval") {
else if (ap.provider == "interval") {
if (ap.target == "")
err_ << "interval probe must have unit of time" << std::endl;
else if (ap.target != "ms" &&
......@@ -725,7 +724,7 @@ void SemanticAnalyser::visit(AttachPoint &ap)
if (ap.func != "")
err_ << "interval probe must have an integer frequency" << std::endl;
}
else if (type == "software") {
else if (ap.provider == "software") {
if (ap.target == "")
err_ << "software probe must have a software event name" << std::endl;
else if (ap.target != "cpu-clock" && ap.target != "cpu" &&
......@@ -745,7 +744,7 @@ void SemanticAnalyser::visit(AttachPoint &ap)
else if (ap.freq < 0)
err_ << "software count should be a positive integer" << std::endl;
}
else if (type == "hardware") {
else if (ap.provider == "hardware") {
if (ap.target == "")
err_ << "hardware probe must have a hardware event name" << std::endl;
else if (ap.target != "cpu-cycles" && ap.target != "cycles" &&
......@@ -763,16 +762,16 @@ void SemanticAnalyser::visit(AttachPoint &ap)
else if (ap.freq < 0)
err_ << "hardware frequency should be a positive integer" << std::endl;
}
else if (type == "BEGIN" || type == "END") {
else if (ap.provider == "BEGIN" || ap.provider == "END") {
if (ap.target != "" || ap.func != "")
err_ << "BEGIN/END probes should not have a target" << std::endl;
if (is_final_pass()) {
if (type == "BEGIN") {
if (ap.provider == "BEGIN") {
if (has_begin_probe_)
err_ << "More than one BEGIN probe defined" << std::endl;
has_begin_probe_ = true;
}
if (type == "END") {
if (ap.provider == "END") {
if (has_end_probe_)
err_ << "More than one END probe defined" << std::endl;
has_end_probe_ = true;
......
......@@ -70,7 +70,7 @@ std::string probetypeName(const std::string &type)
if (type == PROBE_LIST[i].name || type == PROBE_LIST[i].abbr)
return PROBE_LIST[i].name;
}
return "";
return type;
}
uint64_t asyncactionint(AsyncAction a)
......
......@@ -85,7 +85,7 @@ const ProbeItem PROBE_LIST[] =
{ "kretprobe", "kr", ProbeType::kretprobe },
{ "uprobe", "u", ProbeType::uprobe },
{ "uretprobe", "ur", ProbeType::uretprobe },
{ "usdt", "usdt", ProbeType::usdt },
{ "usdt", "U", ProbeType::usdt },
{ "BEGIN", "BEGIN", ProbeType::uprobe },
{ "END", "END", ProbeType::uprobe },
{ "tracepoint", "t", ProbeType::tracepoint },
......
......@@ -9,6 +9,7 @@ add_executable(bpftrace_test
codegen.cpp
main.cpp
parser.cpp
probe.cpp
semantic_analyser.cpp
tracepoint_format_parser.cpp
utils.cpp
......
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "bpforc.h"
#include "bpftrace.h"
#include "clang_parser.h"
#include "codegen_llvm.h"
#include "driver.h"
#include "fake_map.h"
#include "semantic_analyser.h"
namespace bpftrace
{
namespace test
{
namespace probe
{
using bpftrace::ast::AttachPoint;
using bpftrace::ast::AttachPointList;
using bpftrace::ast::Probe;
void gen_bytecode(const std::string &input, std::stringstream &out)
{
BPFtrace bpftrace;
Driver driver;
FakeMap::next_mapfd_ = 1;
ASSERT_EQ(driver.parse_str(input), 0);
ClangParser clang;
clang.parse(driver.root_, bpftrace.structs_);
ast::SemanticAnalyser semantics(driver.root_, bpftrace);
ASSERT_EQ(semantics.analyse(), 0);
ASSERT_EQ(semantics.create_maps(true), 0);
ast::CodegenLLVM codegen(driver.root_, bpftrace);
codegen.compile(DebugLevel::kDebug, out);
}
void compare_bytecode(const std::string &input1, const std::string &input2)
{
std::stringstream expected_output1;
std::stringstream expected_output2;
gen_bytecode(input1, expected_output1);
gen_bytecode(input2, expected_output2);
EXPECT_EQ(expected_output1.str(), expected_output2.str());
}
TEST(probe, short_name)
{
compare_bytecode("tracepoint:a:b { args }", "t:a:b { args }");
compare_bytecode("kprobe:f { pid }", "k:f { pid }");
compare_bytecode("kretprobe:f { pid }", "kr:f { pid }");
compare_bytecode("uprobe:path:f { 1 }", "u:path:f { 1 }");
compare_bytecode("profile:hz:997 { 1 }", "p:hz:997 { 1 }");
compare_bytecode("hardware:cache-references:1000000 { 1 }", "h:cache-references:1000000 { 1 }");
compare_bytecode("software:faults:1000 { 1 }", "s:faults:1000 { 1 }");
compare_bytecode("interval:s:1 { 1 }", "i:s:1 { 1 }");
}
} // namespace probe
} // namespace test
} // namespace bpftrace
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment