Add checks of table type in constructor
diff --git a/src/cc/api/BPFTable.cc b/src/cc/api/BPFTable.cc
index c6d7928..33803ad 100644
--- a/src/cc/api/BPFTable.cc
+++ b/src/cc/api/BPFTable.cc
@@ -191,6 +191,9 @@
bool use_debug_file,
bool check_debug_file_crc)
: BPFTableBase<int, stacktrace_t>(desc) {
+ if (desc.type != BPF_MAP_TYPE_STACK_TRACE)
+ throw std::invalid_argument("Table '" + desc.name + "' is not a stack table");
+
symbol_option_ = {
.use_debug_file = use_debug_file,
.check_debug_file_crc = check_debug_file_crc,
@@ -254,6 +257,12 @@
return res;
}
+BPFPerfBuffer::BPFPerfBuffer(const TableDesc& desc)
+ : BPFTableBase<int, int>(desc), epfd_(-1) {
+ if (desc.type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
+ throw std::invalid_argument("Table '" + desc.name + "' is not a perf buffer");
+}
+
StatusTuple BPFPerfBuffer::open_on_cpu(perf_reader_raw_cb cb,
perf_reader_lost_cb lost_cb, int cpu,
void* cb_cookie, int page_cnt) {
@@ -363,6 +372,12 @@
<< std::endl;
}
+BPFPerfEventArray::BPFPerfEventArray(const TableDesc& desc)
+ : BPFTableBase<int, int>(desc) {
+ if (desc.type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
+ throw std::invalid_argument("Table '" + desc.name + "' is not a perf event array");
+}
+
StatusTuple BPFPerfEventArray::open_all_cpu(uint32_t type, uint64_t config) {
if (cpu_fds_.size() != 0)
return StatusTuple(-1, "Previously opened perf event not cleaned");
@@ -436,6 +451,12 @@
}
}
+BPFProgTable::BPFProgTable(const TableDesc& desc)
+ : BPFTableBase<int, int>(desc) {
+ if (desc.type != BPF_MAP_TYPE_PROG_ARRAY)
+ throw std::invalid_argument("Table '" + desc.name + "' is not a prog table");
+}
+
StatusTuple BPFProgTable::update_value(const int& index, const int& prog_fd) {
if (!this->update(const_cast<int*>(&index), const_cast<int*>(&prog_fd)))
return StatusTuple(-1, "Error updating value: %s", std::strerror(errno));
@@ -448,6 +469,12 @@
return StatusTuple(0);
}
+BPFCgroupArray::BPFCgroupArray(const TableDesc& desc)
+ : BPFTableBase<int, int>(desc) {
+ if (desc.type != BPF_MAP_TYPE_CGROUP_ARRAY)
+ throw std::invalid_argument("Table '" + desc.name + "' is not a cgroup array");
+}
+
StatusTuple BPFCgroupArray::update_value(const int& index,
const int& cgroup2_fd) {
if (!this->update(const_cast<int*>(&index), const_cast<int*>(&cgroup2_fd)))
diff --git a/src/cc/api/BPFTable.h b/src/cc/api/BPFTable.h
index 2ec3210..6eb1bd7 100644
--- a/src/cc/api/BPFTable.h
+++ b/src/cc/api/BPFTable.h
@@ -299,8 +299,7 @@
class BPFPerfBuffer : public BPFTableBase<int, int> {
public:
- BPFPerfBuffer(const TableDesc& desc)
- : BPFTableBase<int, int>(desc), epfd_(-1) {}
+ BPFPerfBuffer(const TableDesc& desc);
~BPFPerfBuffer();
StatusTuple open_all_cpu(perf_reader_raw_cb cb, perf_reader_lost_cb lost_cb,
@@ -321,8 +320,7 @@
class BPFPerfEventArray : public BPFTableBase<int, int> {
public:
- BPFPerfEventArray(const TableDesc& desc)
- : BPFTableBase<int, int>(desc) {}
+ BPFPerfEventArray(const TableDesc& desc);
~BPFPerfEventArray();
StatusTuple open_all_cpu(uint32_t type, uint64_t config);
@@ -337,11 +335,7 @@
class BPFProgTable : public BPFTableBase<int, int> {
public:
- BPFProgTable(const TableDesc& desc)
- : BPFTableBase<int, int>(desc) {
- if (desc.type != BPF_MAP_TYPE_PROG_ARRAY)
- throw std::invalid_argument("Table '" + desc.name + "' is not a prog table");
- }
+ BPFProgTable(const TableDesc& desc);
StatusTuple update_value(const int& index, const int& prog_fd);
StatusTuple remove_value(const int& index);
@@ -349,11 +343,7 @@
class BPFCgroupArray : public BPFTableBase<int, int> {
public:
- BPFCgroupArray(const TableDesc& desc)
- : BPFTableBase<int, int>(desc) {
- if (desc.type != BPF_MAP_TYPE_CGROUP_ARRAY)
- throw std::invalid_argument("Table '" + desc.name + "' is not a cgroup array");
- }
+ BPFCgroupArray(const TableDesc& desc);
StatusTuple update_value(const int& index, const int& cgroup2_fd);
StatusTuple update_value(const int& index, const std::string& cgroup2_path);