Commit ae7e2a5c authored by Alastair Robertson's avatar Alastair Robertson

Make argument_list a template function so it works with Types too

parent fd008e4b
......@@ -74,11 +74,11 @@ void SemanticAnalyser::visit(Map &map)
if (search != map_args_.end()) {
if (search->second != args) {
err_ << "Argument mismatch for " << map.ident << ": ";
err_ << "trying to access with arguments: [ ";
for (Type t : args) { err_ << typestr(t) << " "; }
err_ << "]\n\twhen map expects arguments: [ ";
for (Type t : search->second) { err_ << typestr(t) << " "; }
err_ << "]\n" << std::endl;
err_ << "trying to access with arguments: ";
err_ << argument_list(args, true);
err_ << "\n\twhen map expects arguments: ";
err_ << argument_list(search->second, true);
err_ << "\n" << std::endl;
}
}
else {
......
#include <iostream>
#include <sstream>
#include "types.h"
namespace ebpf {
namespace bpftrace {
std::ostream &operator<<(std::ostream &os, Type type)
{
os << typestr(type);
return os;
}
std::string typestr(Type t)
{
switch (t)
......@@ -38,23 +43,5 @@ bpf_prog_type progtype(ProbeType t)
}
}
std::string argument_list(const std::vector<uint64_t> &items)
{
return argument_list(items, items.size());
}
std::string argument_list(const std::vector<uint64_t> &items, size_t n)
{
if (n == 0)
return "";
std::ostringstream list;
list << "[";
for (size_t i = 0; i < n-1; i++)
list << items.at(i) << ", ";
list << items.at(n-1) << "]";
return list.str();
}
} // namespace bpftrace
} // namespace ebpf
#pragma once
#include <ostream>
#include <sstream>
#include <string>
#include <unistd.h>
#include <vector>
......@@ -17,6 +19,8 @@ enum class Type
count,
};
std::ostream &operator<<(std::ostream &os, Type type);
enum class ProbeType
{
kprobe,
......@@ -26,8 +30,6 @@ enum class ProbeType
std::string typestr(Type t);
bpf_probe_attach_type attachtype(ProbeType t);
bpf_prog_type progtype(ProbeType t);
std::string argument_list(const std::vector<uint64_t> &items);
std::string argument_list(const std::vector<uint64_t> &items, size_t n);
class Probe
{
......@@ -37,5 +39,29 @@ public:
std::string name;
};
template <typename T>
std::string argument_list(const std::vector<T> &items, size_t n, bool show_empty=false)
{
if (n == 0)
{
if (show_empty)
return "[]";
return "";
}
std::ostringstream list;
list << "[";
for (size_t i = 0; i < n-1; i++)
list << items.at(i) << ", ";
list << items.at(n-1) << "]";
return list.str();
}
template <typename T>
std::string argument_list(const std::vector<T> &items, bool show_empty=false)
{
return argument_list(items, items.size(), show_empty);
}
} // namespace bpftrace
} // namespace ebpf
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