Commit 6f90769a authored by Alastair Robertson's avatar Alastair Robertson

Create Struct class

parent 0d465821
......@@ -307,13 +307,13 @@ void SemanticAnalyser::visit(FieldAccess &acc)
abort();
}
auto fields = bpftrace_.structs_[cast_type];
auto fields = bpftrace_.structs_[cast_type].fields;
if (fields.count(acc.field) == 0) {
err_ << "Struct/union of type '" << cast_type << "' does not contain "
<< "a field named '" << acc.field << "'" << std::endl;
}
else {
acc.type = std::get<0>(fields[acc.field]);
acc.type = fields[acc.field].type;
}
}
......
......@@ -11,6 +11,7 @@
#include "ast.h"
#include "attached_probe.h"
#include "imap.h"
#include "struct.h"
#include "types.h"
namespace bpftrace {
......@@ -30,8 +31,7 @@ public:
std::map<std::string, std::unique_ptr<IMap>> maps_;
std::map<std::string, std::tuple<uint8_t *, uintptr_t>> sections_;
// structs_ = { struct_name: { field_name: { sized_type, offset } } }
std::map<std::string, std::map<std::string, std::tuple<SizedType, int>>> structs_;
std::map<std::string, Struct> structs_;
std::vector<std::tuple<std::string, std::vector<SizedType>>> printf_args_;
std::unique_ptr<IMap> stackid_map_;
std::unique_ptr<IMap> perf_event_map_;
......
#pragma once
#include <map>
#include "types.h"
namespace bpftrace {
class Field {
public:
SizedType type;
int offset;
};
class Struct
{
public:
int size;
std::map<std::string, Field> fields;
};
} // namespace bpftrace
......@@ -40,10 +40,16 @@ void test(Driver &driver, const std::string &input, int expected_result=0)
void test(const std::string &input, int expected_result=0)
{
Field field = { SizedType(Type::integer, 8), 0 };
Field mystr = { SizedType(Type::string, 8), 8 };
Struct type1 = { 16, {{"field", field}, { "mystr", mystr}} };
Struct type2 = { 8, {{"field", field}} };
BPFtrace bpftrace;
bpftrace.structs_["type1"]["field"] = std::make_tuple(SizedType(Type::integer, 8), 0);
bpftrace.structs_["type1"]["mystr"] = std::make_tuple(SizedType(Type::string, 8), 8);
bpftrace.structs_["type2"]["field"] = std::make_tuple(SizedType(Type::integer, 8), 0);
bpftrace.structs_["type1"] = type1;
bpftrace.structs_["type2"] = type2;
Driver driver;
test(bpftrace, driver, input, expected_result);
}
......
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