Skip to content

Commit

Permalink
Adding output for struct types (#60)
Browse files Browse the repository at this point in the history
* adding output for struct types

the json is not yet valid and I am going insane
Signed-off-by: vsoch <vsoch@users.noreply.github.com>
* Fix JSON formatting for structure fields
This adds a missing comma before the start of printing the fields
and a bit of extra whitespace

* fixed json output!
Signed-off-by: vsoch <vsoch@users.noreply.github.com>

Co-authored-by: vsoch <vsoch@users.noreply.github.com>
Co-authored-by: Tim Haines <thaines@cs.wisc.edu>
  • Loading branch information
3 people committed Aug 4, 2021
1 parent 2c527ef commit 1ec3d7b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
40 changes: 31 additions & 9 deletions source/parser/x86_64/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,35 @@ namespace smeagle::x86_64::types {
out << "\n" << buf << "}";
}
};
struct struct_t final : detail::param {
template <typename T> struct struct_t final : detail::param {
T *dyninst_obj;
void toJson(std::ostream &out, int indent) const {
auto buf = std::string(indent, ' ');
out << buf << "{\n";
detail::toJson(*this, out, indent + 2);
out << "\n" << buf << "}";
auto fields = *dyninst_obj->getFields();

// Only print if we have fields
if (fields.size() > 0) {
auto buf = std::string(indent + 2, ' ');
out << ",\n" << buf << "\"fields\": [\n";
for (auto *field : fields) {
// If we are at the last entry, no comma
auto endcomma = (field == fields.back()) ? "" : ",";
out << buf << " {\"size\" : \"" << field->getSize() << "\",\n";
out << buf << " \"name\" : \"" << field->getName() << "\",\n";
out << buf << " \"type\" : \"" << field->getType()->getName() << "\"}" << endcomma
<< "\n";
}
out << buf << "]\n";
}
out << buf << "}";
}
};
struct array_t final : detail::param {

// NOTE: we need to be able to parse call sites to do arrays
template <typename T> struct array_t final : detail::param {
T *dyninst_obj;
void toJson(std::ostream &out, int indent) const {
auto buf = std::string(indent, ' ');
out << buf << "{\n";
Expand All @@ -83,14 +103,16 @@ namespace smeagle::x86_64::types {
auto buf = std::string(indent, ' ');
out << buf << "{\n";
detail::toJson(*this, out, indent + 2);
out << "\n" << buf << "},\n" << buf << "\"constants\": {\n";
out << ",\n" << buf << " \"constants\": {\n";

// TODO: Dyninst does not provide information about underlying type
// which we would need here
for (auto const &c : dyninst_obj->getConstants()) {
out << buf << " \"" << c.first << "\" : \"" << c.second << "\",\n";
auto constants = dyninst_obj->getConstants();
for (auto const &c : constants) {
auto endcomma = (c == constants.back()) ? "" : ",";
out << buf << " \"" << c.first << "\" : \"" << c.second << "\"" << endcomma << "\n";
}
out << buf << "}";
out << buf << "}}";
}
};

Expand All @@ -111,9 +133,9 @@ namespace smeagle::x86_64::types {
out << buf << "{\n";
detail::toJson(*this, out, indent + 2);
out << ",\n" << buf << " \"indirections\":\"" << pointer_indirections << "\"";
out << ",\n" << buf << " \"underlying_type\":\n";
out << ",\n" << buf << " \"underlying_type\": ";
underlying_type.toJson(out, indent + 4);
out << "\n" << buf << "}";
out << "}";
}
};
} // namespace smeagle::x86_64::types
6 changes: 4 additions & 2 deletions source/parser/x86_64/x86_64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,16 @@ namespace smeagle::x86_64 {
typelocs.push_back(
classify<types::scalar_t>(param_name, t, param_type, allocator, ptr_cnt));
} else if (auto *t = underlying_type->getStructType()) {
using dyn_t = std::decay_t<decltype(*t)>;
typelocs.push_back(
classify<types::struct_t>(param_name, t, param_type, allocator, ptr_cnt));
classify<types::struct_t<dyn_t>>(param_name, t, param_type, allocator, ptr_cnt, t));
} else if (auto *t = underlying_type->getUnionType()) {
typelocs.push_back(
classify<types::union_t>(param_name, t, param_type, allocator, ptr_cnt));
} else if (auto *t = underlying_type->getArrayType()) {
using dyn_t = std::decay_t<decltype(*t)>;
typelocs.push_back(
classify<types::array_t>(param_name, t, param_type, allocator, ptr_cnt));
classify<types::array_t<dyn_t>>(param_name, t, param_type, allocator, ptr_cnt, t));
} else if (auto *t = underlying_type->getEnumType()) {
using dyn_t = std::decay_t<decltype(*t)>;
typelocs.push_back(
Expand Down

0 comments on commit 1ec3d7b

Please sign in to comment.