#include "table.h" //BaseTable::BaseTable(ebpf::BPF *bpf_instance, const std::string &tb_name) //: bpf(bpf_instance), tb_name(tb_name){} void BaseTable::printName() { std::cout << "Table name: " << tb_name << std::endl; } std::string cb_fn; void PerfEventArrayTable::callbackfn(void *cookie, void *data, int data_size) { Php::Value phpData((const char *) data, data_size); Php::call(cb_fn.c_str(), nullptr, phpData, data_size); } void PerfEventArrayTable::php_open_perf_buffer(Php::Parameters ¶ms) { cb_fn = params[0].stringValue(); auto res = this->bpf->open_perf_buffer(this->tb_name, &PerfEventArrayTable::callbackfn); if (!res.ok()) { throw Php::Exception("open_perf_buffer error:" + res.msg()); } } int PerfEventArrayTable::perf_buffer_poll(int timeout_ms) { return bpf->poll_perf_buffer(this->tb_name, timeout_ms); // perf_reader_event_read } Php::Value ArrayTable::php_get_value(Php::Parameters ¶m) { auto index = param[0].numericValue(); // todo val type uint64_t val; auto table = bpf->get_array_table(tb_name); auto res = table.get_value(index, val); if (!res.ok()) { throw Php::Exception("Get value error in" + std::string(tb_name)); } Php::Value phpValue = static_cast(val); return phpValue; } Php::Value StackTraceTable::php_get_values(Php::Parameters ¶m) { auto stack_id = param[0].numericValue(); int pid = -1; if (param.size() > 1) { auto param_pid = param[1].numericValue(); if (param_pid >= 0) pid = param_pid; } auto table = bpf->get_stack_table(tb_name); auto symbols = table.get_stack_symbol((int) stack_id, (int) pid); std::vector result; for (const auto &str: symbols) { result.push_back(Php::Value(str)); } return Php::Array(result); } std::string trim_braces(const std::string& input) { std::string result = input; if (result.size() >= 2 && result.substr(0, 2) == "{ ") { result = result.substr(2); } if (result.size() >= 2 && result.substr(result.size() - 2) == " }") { result = result.substr(0, result.size() - 2); } return result; } Php::Value HashTable::php_get_values() { Php::Array result; std::vector, std::vector>> entries; auto raw_table = bpf->get_table(tb_name); auto status = raw_table.get_table_offline_ptr(entries); if (status.code() != 0) { return result; } int i = 0; for (const auto& [key_data, val_data] : entries) { Php::Value phpKeyData(key_data.data(), key_data.size()); Php::Value phpValData(val_data.data(), val_data.size()); Php::Array entry; entry.set("key", phpKeyData); entry.set("value", phpValData); result.set(i++, entry); } return result; } void HashTable::php_clear() { auto raw_table = bpf->get_table(tb_name); raw_table.clear_table_non_atomic(); } Php::Value PerCpuArrayTable::php_sum_value(Php::Parameters ¶m) { auto index = param[0].numericValue(); std::vector val; auto table = bpf->get_percpu_array_table(tb_name); auto res = table.get_value(index, val); if (!res.ok()) { throw Php::Exception("Get value error in" + std::string(tb_name)); } unsigned long long sum = 0; for (const auto &v: val) { sum += v; } Php::Value phpValue = static_cast(sum); return phpValue; }