Skip to content

Commit 200c861

Browse files
torusrxxxmrexodia
authored andcommitted
fixed winerror & ntstatus fmt funcs
Thanks @Mattiwatti
1 parent 1c79384 commit 200c861

File tree

2 files changed

+57
-46
lines changed

2 files changed

+57
-46
lines changed

src/dbg/formatfunctions.cpp

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,55 @@
33
#include "value.h"
44
#include "memory.h"
55
#include "exception.h"
6+
#include "ntdll/ntdll.h"
67

78
std::unordered_map<String, FormatFunctions::Function> FormatFunctions::mFunctions;
89

10+
static FORMATRESULT formatErrorMsg(HMODULE DLL, const String & errName, DWORD code, char* dest, size_t destCount)
11+
{
12+
const NTSTATUS ErrorStatus = code;
13+
PMESSAGE_RESOURCE_ENTRY Entry;
14+
NTSTATUS Status = RtlFindMessage(DLL,
15+
LDR_FORMAT_MESSAGE_FROM_SYSTEM_MESSAGE_TABLE,
16+
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
17+
ErrorStatus,
18+
&Entry);
19+
if(!NT_SUCCESS(Status))
20+
{
21+
if(destCount < errName.size() + 1)
22+
return FORMAT_BUFFER_TOO_SMALL;
23+
else
24+
{
25+
memcpy(dest, errName.c_str(), errName.size() + 1);
26+
return FORMAT_SUCCESS;
27+
}
28+
}
29+
30+
if((Entry->Flags & MESSAGE_RESOURCE_UNICODE) != 0)
31+
{
32+
String UTF8Description = StringUtils::Utf16ToUtf8((const wchar_t*)Entry->Text);
33+
if(UTF8Description.size() + 3 + errName.size() > destCount)
34+
return FORMAT_BUFFER_TOO_SMALL;
35+
else
36+
{
37+
sprintf_s(dest, destCount, "%s: %s", errName.c_str(), UTF8Description.c_str());
38+
return FORMAT_SUCCESS;
39+
}
40+
}
41+
else
42+
{
43+
0;//printf("%s\n", (const char*)Entry->Text);
44+
String UTF8Description = StringUtils::LocalCpToUtf8((const char*)Entry->Text);
45+
if(UTF8Description.size() + 3 + errName.size() > destCount)
46+
return FORMAT_BUFFER_TOO_SMALL;
47+
else
48+
{
49+
sprintf_s(dest, destCount, "%s: %s", errName.c_str(), UTF8Description.c_str());
50+
return FORMAT_SUCCESS;
51+
}
52+
}
53+
}
54+
955
void FormatFunctions::Init()
1056
{
1157
Register("mem", [](char* dest, size_t destCount, int argc, char* argv[], duint addr, void* userdata)
@@ -53,28 +99,8 @@ void FormatFunctions::Init()
5399
#endif //_WIN64
54100
if(errName.size() == 0)
55101
errName = StringUtils::sprintf("%08X", DWORD(code));
56-
DWORD success = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, DWORD(code), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), helpMessage.data(), DWORD(destCount), NULL);
57-
if(success > 0)
58-
{
59-
String UTF8ErrorMessage = StringUtils::Utf16ToUtf8(helpMessage.data());
60-
if(destCount < errName.size() + 3 + UTF8ErrorMessage.size())
61-
return FORMAT_BUFFER_TOO_SMALL;
62-
else
63-
{
64-
sprintf_s(dest, destCount, "%s: %s", errName.c_str(), UTF8ErrorMessage.c_str());
65-
return FORMAT_SUCCESS;
66-
}
67-
}
68-
else
69-
{
70-
if(destCount < errName.size() + 1)
71-
return FORMAT_BUFFER_TOO_SMALL;
72-
else
73-
{
74-
memcpy(dest, errName.c_str(), errName.size() + 1);
75-
return FORMAT_SUCCESS;
76-
}
77-
}
102+
103+
return formatErrorMsg(GetModuleHandleW(L"kernel32.dll"), errName, code, dest, destCount);
78104
});
79105

80106
Register("ntstatus", [](char* dest, size_t destCount, int argc, char* argv[], duint code, void* userdata)
@@ -83,28 +109,8 @@ void FormatFunctions::Init()
83109
String errName = NtStatusCodeToName((unsigned int)code);
84110
if(errName.size() == 0)
85111
errName = StringUtils::sprintf("%08X", DWORD(code));
86-
DWORD success = FormatMessageW(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS, GetModuleHandleW(L"ntdll.dll"), DWORD(code), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), helpMessage.data(), DWORD(destCount), NULL);
87-
if(success > 0)
88-
{
89-
String UTF8ErrorMessage = StringUtils::Utf16ToUtf8(helpMessage.data());
90-
if(destCount < errName.size() + 3 + UTF8ErrorMessage.size())
91-
return FORMAT_BUFFER_TOO_SMALL;
92-
else
93-
{
94-
sprintf_s(dest, destCount, "%s: %s", errName.c_str(), UTF8ErrorMessage.c_str());
95-
return FORMAT_SUCCESS;
96-
}
97-
}
98-
else
99-
{
100-
if(destCount < errName.size() + 1)
101-
return FORMAT_BUFFER_TOO_SMALL;
102-
else
103-
{
104-
memcpy(dest, errName.c_str(), errName.size() + 1);
105-
return FORMAT_SUCCESS;
106-
}
107-
}
112+
113+
return formatErrorMsg(GetModuleHandleW(L"ntdll.dll"), errName, code, dest, destCount);
108114
});
109115
}
110116

src/gui/Src/Gui/RegistersView.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,8 +1629,13 @@ QString RegistersView::helpRegister(REGISTER_NAME reg)
16291629
else
16301630
return tr("The value of GetLastError(). This value is stored in the TEB.");
16311631
case LastStatus:
1632-
//TODO: display help message of the specific status instead of this very generic message.
1633-
return tr("The NTSTATUS in the LastStatusValue field of the TEB.");
1632+
char dat1[1024];
1633+
LASTSTATUS* error1;
1634+
error1 = (LASTSTATUS*)registerValue(&wRegDumpStruct, LastStatus);
1635+
if(DbgFunctions()->StringFormatInline(QString().sprintf("{ntstatus@%X}", error1->code).toUtf8().constData(), sizeof(dat1), dat1) == 1) //FORMAT_SUCCESS
1636+
return dat1;
1637+
else
1638+
return tr("The NTSTATUS in the LastStatusValue field of the TEB.");
16341639
#ifdef _WIN64
16351640
case GS:
16361641
return tr("The TEB of the current thread can be accessed as an offset of segment register GS (x64).\nThe TEB can be used to get a lot of information on the process without calling Win32 API.");

0 commit comments

Comments
 (0)