/** \brief Gets a variable value. \param Name The name of the variable. \param [out] Value This function can get the variable value. If this value is null, it is ignored. \param [out] Size This function can get the variable size. If this value is null, it is ignored. \param [out] Type This function can get the variable type. If this value is null, it is ignored. \return true if the variable was found and the optional values were retrieved successfully, false otherwise. */ bool varget(const char* Name, VAR_VALUE* Value, int* Size, VAR_TYPE* Type) { SHARED_ACQUIRE(LockVariables); String name_; if(*Name != '$') name_ = "$"; name_ += Name; auto found = variables.find(name_); if(found == variables.end()) //not found return false; if(found->second.alias.length()) { // Release the lock (potential deadlock here) SHARED_RELEASE(); return varget(found->second.alias.c_str(), Value, Size, Type); } if(Type) *Type = found->second.type; if(Size) *Size = found->second.value.size; if(Value) *Value = found->second.value; return true; }
bool stackcommentget(duint addr, STACK_COMMENT* comment) { SHARED_ACQUIRE(LockSehCache); const auto found = SehCache.find(addr); if(found != SehCache.end()) { *comment = found->second; return true; } SHARED_RELEASE(); duint data = 0; memset(comment, 0, sizeof(STACK_COMMENT)); MemRead(addr, &data, sizeof(duint)); if(!MemIsValidReadPtr(data)) //the stack value is no pointer return false; duint size = 0; duint base = MemFindBaseAddr(data, &size); duint readStart = data - 16 * 4; if(readStart < base) readStart = base; unsigned char disasmData[256]; MemRead(readStart, disasmData, sizeof(disasmData)); duint prev = disasmback(disasmData, 0, sizeof(disasmData), data - readStart, 1); duint previousInstr = readStart + prev; BASIC_INSTRUCTION_INFO basicinfo; bool valid = disasmfast(disasmData + prev, previousInstr, &basicinfo); if(valid && basicinfo.call) //call { char label[MAX_LABEL_SIZE] = ""; ADDRINFO addrinfo; addrinfo.flags = flaglabel; if(_dbg_addrinfoget(data, SEG_DEFAULT, &addrinfo)) strcpy_s(label, addrinfo.label); char module[MAX_MODULE_SIZE] = ""; ModNameFromAddr(data, module, false); char returnToAddr[MAX_COMMENT_SIZE] = ""; if(*module) sprintf(returnToAddr, "%s.", module); if(!*label) sprintf_s(label, "%p", data); strcat(returnToAddr, label); data = basicinfo.addr; if(data) { *label = 0; addrinfo.flags = flaglabel; if(_dbg_addrinfoget(data, SEG_DEFAULT, &addrinfo)) strcpy_s(label, addrinfo.label); *module = 0; ModNameFromAddr(data, module, false); char returnFromAddr[MAX_COMMENT_SIZE] = ""; if(*module) sprintf_s(returnFromAddr, "%s.", module); if(!*label) sprintf_s(label, "%p", data); strcat_s(returnFromAddr, label); sprintf_s(comment->comment, GuiTranslateText(QT_TRANSLATE_NOOP("DBG", "return to %s from %s")), returnToAddr, returnFromAddr); } else sprintf_s(comment->comment, GuiTranslateText(QT_TRANSLATE_NOOP("DBG", "return to %s from ???")), returnToAddr); strcpy_s(comment->color, "!rtnclr"); // Special token for return address color; return true; } //string char string[MAX_STRING_SIZE] = ""; if(DbgGetStringAt(data, string)) { strncpy_s(comment->comment, string, _TRUNCATE); return true; } //label char label[MAX_LABEL_SIZE] = ""; ADDRINFO addrinfo; addrinfo.flags = flaglabel; if(_dbg_addrinfoget(data, SEG_DEFAULT, &addrinfo)) strcpy_s(label, addrinfo.label); char module[MAX_MODULE_SIZE] = ""; ModNameFromAddr(data, module, false); if(*module) //module { if(*label) //+label sprintf_s(comment->comment, "%s.%s", module, label); else //module only sprintf_s(comment->comment, "%s.%p", module, data); return true; } else if(*label) //label only { sprintf_s(comment->comment, "<%s>", label); return true; } return false; }
int RefFind(duint Address, duint Size, CBREF Callback, void* UserData, bool Silent, const char* Name, REFFINDTYPE type) { char fullName[deflen]; char moduleName[MAX_MODULE_SIZE]; duint scanStart, scanSize; REFINFO refInfo; if(type == CURRENT_REGION) // Search in current Region { duint regionSize = 0; duint regionBase = MemFindBaseAddr(Address, ®ionSize, true); // If the memory page wasn't found, fail if(!regionBase || !regionSize) { if(!Silent) dprintf("Invalid memory page 0x%p\n", Address); return 0; } // Assume the entire range is used scanStart = regionBase; scanSize = regionSize; // Otherwise use custom boundaries if size was supplied if(Size) { duint maxsize = Size - (Address - regionBase); // Make sure the size fits in one page scanStart = Address; scanSize = min(Size, maxsize); } // Determine the full module name if(ModNameFromAddr(scanStart, moduleName, true)) sprintf_s(fullName, "%s (Region %s)", Name, moduleName); else sprintf_s(fullName, "%s (Region %p)", Name, scanStart); // Initialize disassembler Capstone cp; // Allow an "initialization" notice refInfo.refcount = 0; refInfo.userinfo = UserData; refInfo.name = fullName; RefFindInRange(scanStart, scanSize, Callback, UserData, Silent, refInfo, cp, true, [](int percent) { GuiReferenceSetCurrentTaskProgress(percent, "Region Search"); GuiReferenceSetProgress(percent); }); } else if(type == CURRENT_MODULE) // Search in current Module { SHARED_ACQUIRE(LockModules); auto modInfo = ModInfoFromAddr(Address); if(!modInfo) { if(!Silent) dprintf("Couldn't locate module for 0x%p\n", Address); return 0; } duint modBase = modInfo->base; duint modSize = modInfo->size; SHARED_RELEASE(); scanStart = modBase; scanSize = modSize; // Determine the full module name if(ModNameFromAddr(scanStart, moduleName, true)) sprintf_s(fullName, "%s (%s)", Name, moduleName); else sprintf_s(fullName, "%s (%p)", Name, scanStart); // Initialize disassembler Capstone cp; // Allow an "initialization" notice refInfo.refcount = 0; refInfo.userinfo = UserData; refInfo.name = fullName; RefFindInRange(scanStart, scanSize, Callback, UserData, Silent, refInfo, cp, true, [](int percent) { GuiReferenceSetCurrentTaskProgress(percent, "Module Search"); GuiReferenceSetProgress(percent); }); } else if(type == ALL_MODULES) // Search in all Modules { bool initCallBack = true; std::vector<MODINFO> modList; ModGetList(modList); if(!modList.size()) { if(!Silent) dprintf("Couldn't get module list"); return 0; } // Initialize disassembler Capstone cp; // Determine the full module sprintf_s(fullName, "All Modules (%s)", Name); // Allow an "initialization" notice refInfo.refcount = 0; refInfo.userinfo = UserData; refInfo.name = fullName; for(duint i = 0; i < modList.size(); i++) { scanStart = modList[i].base; scanSize = modList[i].size; if(i != 0) initCallBack = false; RefFindInRange(scanStart, scanSize, Callback, UserData, Silent, refInfo, cp, initCallBack, [&i, &modList](int percent) { float fPercent = (float)percent / 100.f; float fTotalPercent = ((float)i + fPercent) / (float)modList.size(); int totalPercent = (int)floor(fTotalPercent * 100.f); char tst[256]; strcpy_s(tst, modList[i].name); GuiReferenceSetCurrentTaskProgress(percent, modList[i].name); GuiReferenceSetProgress(totalPercent); }); } } GuiReferenceSetProgress(100); GuiReferenceReloadData(); return refInfo.refcount; }