Permalink
Browse files

DBG: very hacky type parser working

  • Loading branch information...
1 parent 943aa09 commit eea599b0ac6870b6112c30f06b4b78c9cf61954a @mrexodia mrexodia committed Nov 21, 2016
View
@@ -4,3 +4,6 @@
[submodule "src/gui/Translations"]
path = src/gui/Translations
url = https://github.com/x64dbg/Translations.git
+[submodule "src/dbg/btparser"]
+ path = src/dbg/btparser
+ url = https://github.com/x64dbg/btparser
Submodule btparser added at 87003f
@@ -608,4 +608,24 @@ bool cbInstrLoadTypes(int argc, char* argv[])
}
dputs(QT_TRANSLATE_NOOP("DBG", "Types loaded"));
return true;
+}
+
+bool cbInstrParseTypes(int argc, char* argv[])
+{
+ if(IsArgumentsLessThan(argc, 2))
+ return false;
+ auto owner = FileHelper::GetFileName(argv[1]);
+ std::string data;
+ if(!FileHelper::ReadAllText(argv[1], data))
+ {
+ dputs("failed to read file!");
+ return false;
+ }
+ if(!ParseTypes(data, owner))
+ {
+ dputs("ParseTypes failed");
+ return false;
+ }
+ dputs("Types parsed");
+ return true;
}
@@ -35,4 +35,5 @@ bool cbInstrVisitType(int argc, char* argv[]);
bool cbInstrClearTypes(int argc, char* argv[]);
bool cbInstrRemoveType(int argc, char* argv[]);
bool cbInstrEnumTypes(int argc, char* argv[]);
-bool cbInstrLoadTypes(int argc, char* argv[]);
+bool cbInstrLoadTypes(int argc, char* argv[]);
+bool cbInstrParseTypes(int argc, char* argv[]);
View
@@ -585,86 +585,89 @@ static void loadFunctions(const JSON froot, std::vector<Function> & functions)
}
}
-bool LoadTypesJson(const std::string & json, const std::string & owner)
+void LoadModel(const std::string & owner, Model & model)
{
- EXCLUSIVE_ACQUIRE(LockTypeManager);
- auto root = json_loads(json.c_str(), 0, 0);
- if(root)
+ //Add all base struct/union types first to avoid errors later
+ for(auto & su : model.structUnions)
{
- std::vector<Member> types;
- loadTypes(json_object_get(root, "types"), types);
- std::vector<StructUnion> structUnions;
- loadStructUnions(json_object_get(root, "structs"), false, structUnions);
- loadStructUnions(json_object_get(root, "unions"), true, structUnions);
- std::vector<Function> functions;
- loadFunctions(json_object_get(root, "functions"), functions);
-
- //Add all base struct/union types first to avoid errors later
- for(auto & su : structUnions)
+ auto success = su.isunion ? typeManager.AddUnion(owner, su.name) : typeManager.AddStruct(owner, su.name);
+ if(!success)
{
- auto success = su.isunion ? typeManager.AddUnion(owner, su.name) : typeManager.AddStruct(owner, su.name);
- if(!success)
- {
- su.name.clear(); //signal error
- //TODO properly handle errors
- dprintf(QT_TRANSLATE_NOOP("DBG", "Failed to add %s %s;\n"), su.isunion ? "union" : "struct", su.name);
- }
+ su.name.clear(); //signal error
+ //TODO properly handle errors
+ dprintf(QT_TRANSLATE_NOOP("DBG", "Failed to add %s %s;\n"), su.isunion ? "union" : "struct", su.name.c_str());
}
+ }
- //Add simple typedefs
- for(auto & type : types)
+ //Add simple typedefs
+ for(auto & type : model.types)
+ {
+ auto success = typeManager.AddType(owner, type.type, type.name);
+ if(!success)
{
- auto success = typeManager.AddType(owner, type.type, type.name);
- if(!success)
- {
- //TODO properly handle errors
- dprintf(QT_TRANSLATE_NOOP("DBG", "Failed to add typedef %s %s;\n"), type.type, type.name);
- }
+ //TODO properly handle errors
+ dprintf(QT_TRANSLATE_NOOP("DBG", "Failed to add typedef %s %s;\n"), type.type.c_str(), type.name.c_str());
}
+ }
- //Add base function types to avoid errors later
- for(auto & function : functions)
+ //Add base function types to avoid errors later
+ for(auto & function : model.functions)
+ {
+ auto success = typeManager.AddFunction(owner, function.name, function.rettype, function.callconv, function.noreturn);
+ if(!success)
{
- auto success = typeManager.AddFunction(owner, function.name, function.rettype, function.callconv, function.noreturn);
- if(!success)
- {
- function.name.clear(); //signal error
- //TODO properly handle errors
- dprintf(QT_TRANSLATE_NOOP("DBG", "Failed to add function %s %s()\n"), function.rettype, function.name);
- }
+ function.name.clear(); //signal error
+ //TODO properly handle errors
+ dprintf(QT_TRANSLATE_NOOP("DBG", "Failed to add function %s %s()\n"), function.rettype.c_str(), function.name.c_str());
}
+ }
- //Add struct/union members
- for(auto & su : structUnions)
+ //Add struct/union members
+ for(auto & su : model.structUnions)
+ {
+ if(su.name.empty()) //skip error-signalled structs/unions
+ continue;
+ for(auto & member : su.members)
{
- if(su.name.empty()) //skip error-signalled structs/unions
- continue;
- for(auto & member : su.members)
+ auto success = typeManager.AddMember(su.name, member.type, member.name, member.arrsize, member.offset);
+ if(!success)
{
- auto success = typeManager.AddMember(su.name, member.type, member.name, member.arrsize, member.offset);
- if(!success)
- {
- //TODO properly handle errors
- dprintf(QT_TRANSLATE_NOOP("DBG", "Failed to add member %s %s.%s;\n"), member.type, su.name, member.name);
- }
+ //TODO properly handle errors
+ dprintf(QT_TRANSLATE_NOOP("DBG", "Failed to add member %s %s.%s;\n"), member.type.c_str(), su.name.c_str(), member.name.c_str());
}
}
+ }
- //Add function arguments
- for(auto & function : functions)
+ //Add function arguments
+ for(auto & function : model.functions)
+ {
+ if(function.name.empty()) //skip error-signalled functions
+ continue;
+ for(auto & arg : function.args)
{
- if(function.name.empty()) //skip error-signalled functions
- continue;
- for(auto & arg : function.args)
+ auto success = typeManager.AddArg(function.name, arg.type, arg.name);
+ if(!success)
{
- auto success = typeManager.AddArg(function.name, arg.type, arg.name);
- if(!success)
- {
- //TODO properly handle errors
- dprintf(QT_TRANSLATE_NOOP("DBG", "Failed to add argument %s %s.%s;\n"), arg.type, function.name, arg.name);
- }
+ //TODO properly handle errors
+ dprintf(QT_TRANSLATE_NOOP("DBG", "Failed to add argument %s %s.%s;\n"), arg.type.c_str(), function.name.c_str(), arg.name.c_str());
}
}
+ }
+}
+
+bool LoadTypesJson(const std::string & json, const std::string & owner)
+{
+ EXCLUSIVE_ACQUIRE(LockTypeManager);
+ auto root = json_loads(json.c_str(), 0, 0);
+ if(root)
+ {
+ Model model;
+ loadTypes(json_object_get(root, "types"), model.types);
+ loadStructUnions(json_object_get(root, "structs"), false, model.structUnions);
+ loadStructUnions(json_object_get(root, "unions"), true, model.structUnions);
+ loadFunctions(json_object_get(root, "functions"), model.functions);
+
+ LoadModel(owner, model);
// Free root
json_decref(root);
View
@@ -119,6 +119,13 @@ namespace Types
bool addType(const Type & t);
bool visitMember(const Member & root, Visitor & visitor) const;
};
+
+ struct Model
+ {
+ std::vector<Member> types;
+ std::vector<StructUnion> structUnions;
+ std::vector<Function> functions;
+ };
};
bool AddType(const std::string & owner, const std::string & type, const std::string & name);
@@ -135,4 +142,5 @@ void ClearTypes(const std::string & owner = "");
bool RemoveType(const std::string & type);
void EnumTypes(std::vector<Types::TypeManager::Summary> & typeList);
bool LoadTypesJson(const std::string & json, const std::string & owner);
-bool LoadTypesFile(const std::string & path, const std::string & owner);
+bool LoadTypesFile(const std::string & path, const std::string & owner);
+bool ParseTypes(const std::string & parse, const std::string & owner);
Oops, something went wrong.

0 comments on commit eea599b

Please sign in to comment.