Skip to content

Commit e8f4cd2

Browse files
committed
DBG: moved some functions to different files
1 parent b29695b commit e8f4cd2

File tree

5 files changed

+68
-66
lines changed

5 files changed

+68
-66
lines changed

x64_dbg_dbg/_dbgfunctions.cpp

Lines changed: 4 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,6 @@ static bool _patchrestore(duint addr)
9292
return patchdel(addr, true);
9393
}
9494

95-
static int _modpathfromaddr(duint addr, char* path, int size)
96-
{
97-
Memory<wchar_t*> wszModPath(size * sizeof(wchar_t), "_modpathfromaddr:wszModPath");
98-
if(!GetModuleFileNameExW(fdProcessInfo->hProcess, (HMODULE)modbasefromaddr(addr), wszModPath, size))
99-
{
100-
*path = '\0';
101-
return 0;
102-
}
103-
strcpy_s(path, size, StringUtils::Utf16ToUtf8(wszModPath()).c_str());
104-
return (int)strlen(path);
105-
}
106-
107-
static int _modpathfromname(const char* modname, char* path, int size)
108-
{
109-
return _modpathfromaddr(modbasefromname(modname), path, size);
110-
}
111-
11295
static void _getcallstack(DBGCALLSTACK* callstack)
11396
{
11497
stackgetcallstack(GetContextDataEx(hActiveThread, UE_CSP), (CALLSTACK*)callstack);
@@ -181,46 +164,6 @@ static void _memupdatemap()
181164
memupdatemap(fdProcessInfo->hProcess);
182165
}
183166

184-
static duint _fileoffsettova(const char* modname, duint offset)
185-
{
186-
char modpath[MAX_PATH] = "";
187-
if(DbgFunctions()->ModPathFromName(modname, modpath, MAX_PATH))
188-
{
189-
HANDLE FileHandle;
190-
DWORD LoadedSize;
191-
HANDLE FileMap;
192-
ULONG_PTR FileMapVA;
193-
if(StaticFileLoadW(StringUtils::Utf8ToUtf16(modpath).c_str(), UE_ACCESS_READ, false, &FileHandle, &LoadedSize, &FileMap, &FileMapVA))
194-
{
195-
ULONGLONG rva = ConvertFileOffsetToVA(FileMapVA, //FileMapVA
196-
FileMapVA + (ULONG_PTR)offset, //Offset inside FileMapVA
197-
false); //Return without ImageBase
198-
StaticFileUnloadW(StringUtils::Utf8ToUtf16(modpath).c_str(), true, FileHandle, LoadedSize, FileMap, FileMapVA);
199-
return offset < LoadedSize ? (duint)rva + modbasefromname(modname) : 0;
200-
}
201-
}
202-
return 0;
203-
}
204-
205-
static duint _vatofileoffset(duint va)
206-
{
207-
char modpath[MAX_PATH] = "";
208-
if(DbgFunctions()->ModPathFromAddr(va, modpath, MAX_PATH))
209-
{
210-
HANDLE FileHandle;
211-
DWORD LoadedSize;
212-
HANDLE FileMap;
213-
ULONG_PTR FileMapVA;
214-
if(StaticFileLoadW(StringUtils::Utf8ToUtf16(modpath).c_str(), UE_ACCESS_READ, false, &FileHandle, &LoadedSize, &FileMap, &FileMapVA))
215-
{
216-
ULONGLONG offset = ConvertVAtoFileOffsetEx(FileMapVA, LoadedSize, 0, va - modbasefromaddr(va), true, false);
217-
StaticFileUnloadW(StringUtils::Utf8ToUtf16(modpath).c_str(), true, FileHandle, LoadedSize, FileMap, FileMapVA);
218-
return (duint)offset;
219-
}
220-
}
221-
return 0;
222-
}
223-
224167
void dbgfunctionsinit()
225168
{
226169
_dbgfunctions.AssembleAtEx = _assembleatex;
@@ -237,8 +180,8 @@ void dbgfunctionsinit()
237180
_dbgfunctions.PatchEnum = (PATCHENUM)patchenum;
238181
_dbgfunctions.PatchRestore = _patchrestore;
239182
_dbgfunctions.PatchFile = (PATCHFILE)patchfile;
240-
_dbgfunctions.ModPathFromAddr = _modpathfromaddr;
241-
_dbgfunctions.ModPathFromName = _modpathfromname;
183+
_dbgfunctions.ModPathFromAddr = modpathfromaddr;
184+
_dbgfunctions.ModPathFromName = modpathfromname;
242185
_dbgfunctions.DisasmFast = disasmfast;
243186
_dbgfunctions.MemUpdateMap = _memupdatemap;
244187
_dbgfunctions.GetCallStack = _getcallstack;
@@ -253,6 +196,6 @@ void dbgfunctionsinit()
253196
_dbgfunctions.IsProcessElevated = IsProcessElevated;
254197
_dbgfunctions.GetCmdline = _getcmdline;
255198
_dbgfunctions.SetCmdline = _setcmdline;
256-
_dbgfunctions.FileOffsetToVa = _fileoffsettova;
257-
_dbgfunctions.VaToFileOffset = _vatofileoffset;
199+
_dbgfunctions.FileOffsetToVa = valfileoffsettova;
200+
_dbgfunctions.VaToFileOffset = valvatofileoffset;
258201
}

x64_dbg_dbg/addrinfo.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,23 @@ uint modentryfromaddr(uint addr)
323323
return found->second.entry;
324324
}
325325

326+
int modpathfromaddr(duint addr, char* path, int size)
327+
{
328+
Memory<wchar_t*> wszModPath(size * sizeof(wchar_t), "modpathfromaddr:wszModPath");
329+
if(!GetModuleFileNameExW(fdProcessInfo->hProcess, (HMODULE)modbasefromaddr(addr), wszModPath, size))
330+
{
331+
*path = '\0';
332+
return 0;
333+
}
334+
strcpy_s(path, size, StringUtils::Utf16ToUtf8(wszModPath()).c_str());
335+
return (int)strlen(path);
336+
}
337+
338+
int modpathfromname(const char* modname, char* path, int size)
339+
{
340+
return modpathfromaddr(modbasefromname(modname), path, size);
341+
}
342+
326343
///api functions
327344
bool apienumexports(uint base, EXPORTENUMCALLBACK cbEnum)
328345
{

x64_dbg_dbg/addrinfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ uint modbasefromname(const char* modname);
136136
uint modsizefromaddr(uint addr);
137137
bool modsectionsfromaddr(uint addr, std::vector<MODSECTIONINFO>* sections);
138138
uint modentryfromaddr(uint addr);
139+
int modpathfromaddr(duint addr, char* path, int size);
140+
int modpathfromname(const char* modname, char* path, int size);
139141

140142
bool apienumexports(uint base, EXPORTENUMCALLBACK cbEnum);
141143

x64_dbg_dbg/value.cpp

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,7 +1535,7 @@ bool valfromstring(const char* string, uint* value, bool silent, bool baseonly,
15351535
return false; //nothing was OK
15361536
}
15371537

1538-
bool longEnough(const char* str, size_t min_length)
1538+
static bool longEnough(const char* str, size_t min_length)
15391539
{
15401540
size_t length = 0;
15411541
while(str[length] && length < min_length)
@@ -1545,7 +1545,7 @@ bool longEnough(const char* str, size_t min_length)
15451545
return false;
15461546
}
15471547

1548-
bool startsWith(const char* pre, const char* str)
1548+
static bool startsWith(const char* pre, const char* str)
15491549
{
15501550
size_t lenpre = strlen(pre);
15511551
return longEnough(str, lenpre) ? StrNCmpI(str, pre, (int) lenpre) == 0 : false;
@@ -1561,8 +1561,7 @@ bool startsWith(const char* pre, const char* str)
15611561
#define x8780BITFPU_PRE_FIELD_STRING "x87r"
15621562
#define STRLEN_USING_SIZEOF(string) (sizeof(string) - 1)
15631563

1564-
1565-
void fpustuff(const char* string, uint value)
1564+
static void fpustuff(const char* string, uint value)
15661565
{
15671566
uint xorval = 0;
15681567
uint flags = 0;
@@ -2053,3 +2052,43 @@ bool valtostring(const char* string, uint* value, bool silent)
20532052
}
20542053
return varset(string, *value, false); //variable
20552054
}
2055+
2056+
uint valfileoffsettova(const char* modname, uint offset)
2057+
{
2058+
char modpath[MAX_PATH] = "";
2059+
if(modpathfromname(modname, modpath, MAX_PATH))
2060+
{
2061+
HANDLE FileHandle;
2062+
DWORD LoadedSize;
2063+
HANDLE FileMap;
2064+
ULONG_PTR FileMapVA;
2065+
if(StaticFileLoadW(StringUtils::Utf8ToUtf16(modpath).c_str(), UE_ACCESS_READ, false, &FileHandle, &LoadedSize, &FileMap, &FileMapVA))
2066+
{
2067+
ULONGLONG rva = ConvertFileOffsetToVA(FileMapVA, //FileMapVA
2068+
FileMapVA + (ULONG_PTR)offset, //Offset inside FileMapVA
2069+
false); //Return without ImageBase
2070+
StaticFileUnloadW(StringUtils::Utf8ToUtf16(modpath).c_str(), true, FileHandle, LoadedSize, FileMap, FileMapVA);
2071+
return offset < LoadedSize ? (duint)rva + modbasefromname(modname) : 0;
2072+
}
2073+
}
2074+
return 0;
2075+
}
2076+
2077+
uint valvatofileoffset(uint va)
2078+
{
2079+
char modpath[MAX_PATH] = "";
2080+
if(modpathfromaddr(va, modpath, MAX_PATH))
2081+
{
2082+
HANDLE FileHandle;
2083+
DWORD LoadedSize;
2084+
HANDLE FileMap;
2085+
ULONG_PTR FileMapVA;
2086+
if(StaticFileLoadW(StringUtils::Utf8ToUtf16(modpath).c_str(), UE_ACCESS_READ, false, &FileHandle, &LoadedSize, &FileMap, &FileMapVA))
2087+
{
2088+
ULONGLONG offset = ConvertVAtoFileOffsetEx(FileMapVA, LoadedSize, 0, va - modbasefromaddr(va), true, false);
2089+
StaticFileUnloadW(StringUtils::Utf8ToUtf16(modpath).c_str(), true, FileHandle, LoadedSize, FileMap, FileMapVA);
2090+
return (duint)offset;
2091+
}
2092+
}
2093+
return 0;
2094+
}

x64_dbg_dbg/value.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ bool valx87controlwordflagfromstring(uint controlword, const char* string);
1616
unsigned short valmxcsrfieldfromstring(uint mxcsrflags, const char* string);
1717
unsigned short valx87statuswordfieldfromstring(uint statusword, const char* string);
1818
unsigned short valx87controlwordfieldfromstring(uint controlword, const char* string);
19-
void fpustuff(const char* string, uint value);
19+
uint valfileoffsettova(const char* modname, uint offset);
20+
uint valvatofileoffset(uint va);
2021

2122
#endif // _VALUE_H

0 commit comments

Comments
 (0)