From 0b7291a43341f16064f99edabf363cec9366ef92 Mon Sep 17 00:00:00 2001 From: Nukem Date: Mon, 30 Mar 2015 16:33:19 -0400 Subject: [PATCH] Finish labels refactor --- x64_dbg_dbg/_plugins.cpp | 3 + x64_dbg_dbg/addrinfo.cpp | 4 +- x64_dbg_dbg/comment.cpp | 1 + x64_dbg_dbg/function.cpp | 19 ++--- x64_dbg_dbg/label.cpp | 166 ++++++++++++++++++++++----------------- x64_dbg_dbg/label.h | 4 +- x64_dbg_dbg/thread.cpp | 4 +- 7 files changed, 112 insertions(+), 89 deletions(-) diff --git a/x64_dbg_dbg/_plugins.cpp b/x64_dbg_dbg/_plugins.cpp index dd0c8576..5f972108 100644 --- a/x64_dbg_dbg/_plugins.cpp +++ b/x64_dbg_dbg/_plugins.cpp @@ -29,9 +29,12 @@ PLUG_IMPEXP bool _plugin_unregistercommand(int pluginHandle, const char* command PLUG_IMPEXP void _plugin_logprintf(const char* format, ...) { + // TODO: This should just be an alias to dprintf va_list args; va_start(args, format); vsprintf(msg, format, args); + va_end(args); + GuiAddLogMessage(msg); } diff --git a/x64_dbg_dbg/addrinfo.cpp b/x64_dbg_dbg/addrinfo.cpp index 5d8da198..c9050a74 100644 --- a/x64_dbg_dbg/addrinfo.cpp +++ b/x64_dbg_dbg/addrinfo.cpp @@ -22,7 +22,7 @@ void dbsave() DWORD ticks = GetTickCount(); JSON root = json_object(); CommentCacheSave(root); - labelcachesave(root); + LabelCacheSave(root); BookmarkCacheSave(root); FunctionCacheSave(root); loopcachesave(root); @@ -83,7 +83,7 @@ void dbload() return; } CommentCacheLoad(root); - labelcacheload(root); + LabelCacheLoad(root); BookmarkCacheLoad(root); FunctionCacheLoad(root); loopcacheload(root); diff --git a/x64_dbg_dbg/comment.cpp b/x64_dbg_dbg/comment.cpp index b2fa4985..1fc34315 100644 --- a/x64_dbg_dbg/comment.cpp +++ b/x64_dbg_dbg/comment.cpp @@ -165,6 +165,7 @@ void CommentCacheLoad(JSON Root) json_array_foreach(Object, i, value) { COMMENTSINFO commentInfo; + memset(&commentInfo, 0, sizeof(COMMENTSINFO)); // Module const char* mod = json_string_value(json_object_get(value, "module")); diff --git a/x64_dbg_dbg/function.cpp b/x64_dbg_dbg/function.cpp index 48059ad1..9a007059 100644 --- a/x64_dbg_dbg/function.cpp +++ b/x64_dbg_dbg/function.cpp @@ -186,27 +186,28 @@ void FunctionCacheLoad(JSON Root) JSON value; json_array_foreach(Object, i, value) { - FUNCTIONSINFO function; + FUNCTIONSINFO functionInfo; + memset(&functionInfo, 0, sizeof(FUNCTIONSINFO)); // Copy module name const char* mod = json_string_value(json_object_get(value, "module")); if(mod && *mod && strlen(mod) < MAX_MODULE_SIZE) - strcpy_s(function.mod, mod); + strcpy_s(functionInfo.mod, mod); else - function.mod[0] = '\0'; + functionInfo.mod[0] = '\0'; // Function address - function.start = (uint)json_hex_value(json_object_get(value, "start")); - function.end = (uint)json_hex_value(json_object_get(value, "end")); - function.manual = Manual; + functionInfo.start = (uint)json_hex_value(json_object_get(value, "start")); + functionInfo.end = (uint)json_hex_value(json_object_get(value, "end")); + functionInfo.manual = Manual; // Sanity check - if(function.end < function.start) + if(functionInfo.end < functionInfo.start) continue; - const uint key = ModHashFromName(function.mod); - functions.insert(std::make_pair(ModuleRange(ModHashFromName(function.mod), Range(function.start, function.end)), function)); + const uint key = ModHashFromName(functionInfo.mod); + functions.insert(std::make_pair(ModuleRange(key, Range(functionInfo.start, functionInfo.end)), functionInfo)); } }; diff --git a/x64_dbg_dbg/label.cpp b/x64_dbg_dbg/label.cpp index 85e95903..cef674a3 100644 --- a/x64_dbg_dbg/label.cpp +++ b/x64_dbg_dbg/label.cpp @@ -143,87 +143,105 @@ void LabelDelRange(uint Start, uint End) } } -void labelcachesave(JSON root) +void LabelCacheSave(JSON Root) { - CriticalSectionLocker locker(LockLabels); - const JSON jsonlabels = json_array(); - const JSON jsonautolabels = json_array(); - for(LabelsInfo::iterator i = labels.begin(); i != labels.end(); ++i) + EXCLUSIVE_ACQUIRE(LockLabels); + + // Create the sub-root structures in memory + const JSON jsonLabels = json_array(); + const JSON jsonAutoLabels = json_array(); + + // Iterator each label + for(auto& itr : labels) { - const LABELSINFO curLabel = i->second; - JSON curjsonlabel = json_object(); - json_object_set_new(curjsonlabel, "module", json_string(curLabel.mod)); - json_object_set_new(curjsonlabel, "address", json_hex(curLabel.addr)); - json_object_set_new(curjsonlabel, "text", json_string(curLabel.text)); - if(curLabel.manual) - json_array_append_new(jsonlabels, curjsonlabel); + JSON jsonLabel = json_object(); + json_object_set_new(jsonLabel, "module", json_string(itr.second.mod)); + json_object_set_new(jsonLabel, "address", json_hex(itr.second.addr)); + json_object_set_new(jsonLabel, "text", json_string(itr.second.text)); + + // Was the label manually added? + if(itr.second.manual) + json_array_append_new(jsonLabels, jsonLabel); else - json_array_append_new(jsonautolabels, curjsonlabel); + json_array_append_new(jsonAutoLabels, jsonLabel); } - if(json_array_size(jsonlabels)) - json_object_set(root, "labels", jsonlabels); - json_decref(jsonlabels); - if(json_array_size(jsonautolabels)) - json_object_set(root, "autolabels", jsonautolabels); - json_decref(jsonautolabels); + + // Apply the object to the global root + if(json_array_size(jsonLabels)) + json_object_set(Root, "labels", jsonLabels); + + if(json_array_size(jsonAutoLabels)) + json_object_set(Root, "autolabels", jsonAutoLabels); + + json_decref(jsonLabels); + json_decref(jsonAutoLabels); } -void labelcacheload(JSON root) +void LabelCacheLoad(JSON Root) { - CriticalSectionLocker locker(LockLabels); - labels.clear(); - const JSON jsonlabels = json_object_get(root, "labels"); - if(jsonlabels) - { - size_t i; - JSON value; - json_array_foreach(jsonlabels, i, value) - { - LABELSINFO curLabel; - const char* mod = json_string_value(json_object_get(value, "module")); - if(mod && *mod && strlen(mod) < MAX_MODULE_SIZE) - strcpy_s(curLabel.mod, mod); - else - *curLabel.mod = '\0'; - curLabel.addr = (uint)json_hex_value(json_object_get(value, "address")); - curLabel.manual = true; - const char* text = json_string_value(json_object_get(value, "text")); - if(text) - strcpy_s(curLabel.text, text); - else - continue; //skip - int len = (int)strlen(curLabel.text); - for(int i = 0; i < len; i++) - if(curLabel.text[i] == '&') - curLabel.text[i] = ' '; - const uint key = ModHashFromName(curLabel.mod) + curLabel.addr; - labels.insert(std::make_pair(key, curLabel)); - } - } - JSON jsonautolabels = json_object_get(root, "autolabels"); - if(jsonautolabels) - { - size_t i; - JSON value; - json_array_foreach(jsonautolabels, i, value) - { - LABELSINFO curLabel; - const char* mod = json_string_value(json_object_get(value, "module")); - if(mod && *mod && strlen(mod) < MAX_MODULE_SIZE) - strcpy_s(curLabel.mod, mod); - else - *curLabel.mod = '\0'; - curLabel.addr = (uint)json_hex_value(json_object_get(value, "address")); - curLabel.manual = false; - const char* text = json_string_value(json_object_get(value, "text")); - if(text) - strcpy_s(curLabel.text, text); - else - continue; //skip - const uint key = ModHashFromName(curLabel.mod) + curLabel.addr; - labels.insert(std::make_pair(key, curLabel)); - } - } + EXCLUSIVE_ACQUIRE(LockLabels); + + // Inline lambda to parse each JSON entry + auto AddLabels = [](const JSON Object, bool Manual) + { + size_t i; + JSON value; + + json_array_foreach(Object, i, value) + { + LABELSINFO labelInfo; + memset(&labelInfo, 0, sizeof(LABELSINFO)); + + // Module + const char* mod = json_string_value(json_object_get(value, "module")); + + if (mod && *mod && strlen(mod) < MAX_MODULE_SIZE) + strcpy_s(labelInfo.mod, mod); + else + labelInfo.mod[0] = '\0'; + + // Address/Manual + labelInfo.addr = (uint)json_hex_value(json_object_get(value, "address")); + labelInfo.manual = Manual; + + // Text string + const char* text = json_string_value(json_object_get(value, "text")); + + if (text) + strcpy_s(labelInfo.text, text); + else + { + // Skip empty strings + continue; + } + + // Go through the string replacing '&' with spaces + for (char *ptr = labelInfo.text; ptr[0] != '\0'; ptr++) + { + if (ptr[0] == '&') + ptr[0] = ' '; + } + + // Finally insert the data + const uint key = ModHashFromName(labelInfo.mod) + labelInfo.addr; + + labels.insert(std::make_pair(key, labelInfo)); + } + }; + + // Remove previous data + labels.clear(); + + const JSON jsonLabels = json_object_get(Root, "labels"); + const JSON jsonAutoLabels = json_object_get(Root, "autolabels"); + + // Load user-set comments + if (jsonLabels) + AddLabels(jsonLabels, true); + + // Load auto-set comments + if (jsonAutoLabels) + AddLabels(jsonAutoLabels, false); } bool LabelEnum(LABELSINFO* List, size_t* Size) diff --git a/x64_dbg_dbg/label.h b/x64_dbg_dbg/label.h index a7fe5c3d..41d89918 100644 --- a/x64_dbg_dbg/label.h +++ b/x64_dbg_dbg/label.h @@ -15,7 +15,7 @@ bool LabelFromString(const char* Text, uint* Address); bool LabelGet(uint Address, char* Text); bool LabelDelete(uint Address); void LabelDelRange(uint Start, uint End); -void labelcachesave(JSON root); -void labelcacheload(JSON root); +void LabelCacheSave(JSON root); +void LabelCacheLoad(JSON root); bool LabelEnum(LABELSINFO* List, size_t* Size); void LabelClear(); \ No newline at end of file diff --git a/x64_dbg_dbg/thread.cpp b/x64_dbg_dbg/thread.cpp index d5cf7188..cbcedd76 100644 --- a/x64_dbg_dbg/thread.cpp +++ b/x64_dbg_dbg/thread.cpp @@ -70,8 +70,8 @@ void ThreadGetList(THREADLIST* list) SHARED_ACQUIRE(LockThreads); // - // This function converts a C++ std::vector to a C-style THREADLIST[] - // Also assume BridgeAlloc zeros the returned buffer + // This function converts a C++ std::vector to a C-style THREADLIST[]. + // Also assume BridgeAlloc zeros the returned buffer. // size_t count = threadList.size();