Skip to content

Commit 320d748

Browse files
author
Jan Wilmans
committed
added test code for receive kernel messages
1 parent 7b77aa0 commit 320d748

File tree

5 files changed

+230
-21
lines changed

5 files changed

+230
-21
lines changed

application/DebugViewpp/DebugView++.cpp

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
#include "DebugViewppLib/DBWinWriter.h"
1313
#include "Win32/Com.h"
1414

15-
#include <iostream>
16-
1715
#include "atleverything.h"
1816

1917
//#define ENABLE_CRASHPAD
@@ -90,10 +88,31 @@ int ForwardMessagesFromPipe(HANDLE hPipe)
9088
return 0;
9189
}
9290

91+
void WriteDriverFromResource()
92+
{
93+
HRSRC hRes = FindResource(NULL, MAKEINTRESOURCE(IDR_DBGV_DRIVER), RT_RCDATA);
94+
if (hRes)
95+
{
96+
HGLOBAL hLoadedRes = LoadResource(NULL, hRes);
97+
if (hLoadedRes)
98+
{
99+
DWORD dwSize = SizeofResource(NULL, hRes);
100+
void* pLockedRes = LockResource(hLoadedRes);
101+
if (pLockedRes)
102+
{
103+
std::ofstream outFile("dbgv.sys", std::ios::binary);
104+
outFile.write(static_cast<const char*>(pLockedRes), dwSize);
105+
outFile.close();
106+
}
107+
}
108+
}
109+
}
110+
93111
int Main(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPWSTR /*lpstrCmdLine*/, int cmdShow)
94112
{
95113
Win32::SetPrivilege(SE_DEBUG_NAME, true);
96114
Win32::SetPrivilege(SE_CREATE_GLOBAL_NAME, true);
115+
Win32::SetPrivilege(SE_LOAD_DRIVER_NAME, true);
97116

98117
Win32::ComInitialization com;
99118

@@ -110,24 +129,6 @@ int Main(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPWSTR /*lpstrCmdLine
110129
std::cout.clear();
111130
#endif
112131

113-
114-
HRSRC hRes = FindResource(NULL, MAKEINTRESOURCE(IDR_DBGV_DRIVER), RT_RCDATA);
115-
if (hRes)
116-
{
117-
HGLOBAL hLoadedRes = LoadResource(NULL, hRes);
118-
if (hLoadedRes)
119-
{
120-
DWORD dwSize = SizeofResource(NULL, hRes);
121-
void* pLockedRes = LockResource(hLoadedRes);
122-
if (pLockedRes)
123-
{
124-
std::ofstream outFile("dbgv.sys", std::ios::binary);
125-
outFile.write(static_cast<const char*>(pLockedRes), dwSize);
126-
outFile.close();
127-
}
128-
}
129-
}
130-
131132
CAppModuleInitialization moduleInit(_Module, hInstance);
132133

133134
HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);

application/DebugViewpp/DebugView++.rc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ IDR_BOOKMARK ICON "res\\Bookmark.ico"
250250
// RCDATA
251251
//
252252

253-
IDR_DBGV_DRIVER RCDATA "res\\Dbgv.sys"
253+
IDR_DBGV_DRIVER RCDATA "res\\Dbgv.sys"
254254

255255
/////////////////////////////////////////////////////////////////////////////
256256
//

application/DebugViewppLib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ add_library(${PROJECT_NAME}
77
Conversions.cpp
88
CTimelineView.cpp
99
DbgviewReader.cpp
10+
Debugview_kernel_client.cpp
1011
DBWinBuffer.cpp
1112
DBWinReader.cpp
1213
DBWinWriter.cpp
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
// (C) Copyright Gert-Jan de Vos and Jan Wilmans 2013.
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#define WIN32_LEAN_AND_MEAN
7+
#include <windows.h>
8+
#include <tchar.h>
9+
#include <winioctl.h>
10+
#include <winsvc.h>
11+
12+
#include <string>
13+
#include <iostream>
14+
15+
#pragma warning(disable:4200)
16+
17+
#define FILE_DEVICE_DBGV 0x8305
18+
19+
#define DBGV_CAPTURE_KERNEL CTL_CODE(FILE_DEVICE_DBGV, 0, METHOD_BUFFERED, FILE_ANY_ACCESS) //0x00 //enable capture kernel
20+
#define DBGV_UNCAPTURE_KERNEL CTL_CODE(FILE_DEVICE_DBGV, 1, METHOD_BUFFERED, FILE_ANY_ACCESS) //0x04 //
21+
#define DBGV_CLEAR_DISPLAY CTL_CODE(FILE_DEVICE_DBGV, 2, METHOD_BUFFERED, FILE_ANY_ACCESS) //0x08 //clear display
22+
#define DBGV_READ_LOG CTL_CODE(FILE_DEVICE_DBGV, 3, METHOD_NEITHER, FILE_ANY_ACCESS) //0x0f //read kernel log
23+
#define DBGV_SET_PASSTHROUGH CTL_CODE(FILE_DEVICE_DBGV, 4, METHOD_BUFFERED, FILE_ANY_ACCESS) //0x10 //enable passthrough
24+
#define DBGV_UNSET_PASSTHROUGH CTL_CODE(FILE_DEVICE_DBGV, 5, METHOD_BUFFERED, FILE_ANY_ACCESS) //0x14 //
25+
#define DBGV_IS_DRIVER_AVAILABLE CTL_CODE(FILE_DEVICE_DBGV, 8, METHOD_BUFFERED, FILE_ANY_ACCESS) //0x20 //test driver is valid or functional
26+
#define DBGV_GET_DRIVER_VERSION CTL_CODE(FILE_DEVICE_DBGV, 9, METHOD_BUFFERED, FILE_ANY_ACCESS) //0x24 //driver version, 4.70 = 0x800
27+
#define DBGV_SET_CARRIAGE_RETURN CTL_CODE(FILE_DEVICE_DBGV, 0x0d, METHOD_BUFFERED, FILE_ANY_ACCESS) //0x34 //force carriage return
28+
#define DBGV_UNSET_CARRIAGE_RETURN CTL_CODE(FILE_DEVICE_DBGV, 0x0e, METHOD_BUFFERED, FILE_ANY_ACCESS) //0x38 //
29+
#define DBGV_ENABLE_FILTER_STATE CTL_CODE(FILE_DEVICE_DBGV, 0x0f, METHOD_BUFFERED, FILE_ANY_ACCESS) //0x3C //enable log verbose
30+
#define DBGV_SET_FILTER_STATE CTL_CODE(FILE_DEVICE_DBGV, 0x10, METHOD_BUFFERED, FILE_ANY_ACCESS) //0x40 //reset log verbose
31+
32+
#pragma pack(1)
33+
typedef struct
34+
{
35+
DWORD dwIndex;
36+
FILETIME liSystemTime;
37+
LARGE_INTEGER liPerfCounter;
38+
CHAR strData[0];
39+
}LOG_ITEM, *PLOG_ITEM;
40+
#pragma pack()
41+
42+
int monitor_kernel()
43+
{
44+
std::wstring strDeviceName = L"\\\\.\\dbgv";
45+
46+
HANDLE hFile = CreateFile(strDeviceName.c_str(),
47+
GENERIC_READ,
48+
FILE_SHARE_READ,
49+
NULL,
50+
OPEN_EXISTING,
51+
FILE_ATTRIBUTE_NORMAL,
52+
NULL);
53+
DWORD dwErr = ::GetLastError();
54+
if (hFile != INVALID_HANDLE_VALUE)
55+
{
56+
BOOL bRet = FALSE;
57+
58+
//enable capture
59+
DWORD dwOut = 0;
60+
DWORD dwReturned = 0;
61+
bRet = DeviceIoControl(hFile, DBGV_CAPTURE_KERNEL, NULL, 0, &dwOut, sizeof(dwOut), &dwReturned, NULL);
62+
if (!bRet)
63+
{
64+
printf("DBGV_CAPTURE_KERNEL failed, err=%d\n", ::GetLastError());
65+
CloseHandle(hFile);
66+
return -1;
67+
}
68+
69+
//enable kernel verboase log
70+
bRet = DeviceIoControl(hFile, DBGV_ENABLE_FILTER_STATE, NULL, 0, NULL, 0, NULL, NULL);
71+
if (!bRet)
72+
{
73+
printf("DBGV_ENABLE_FILTER_STATE failed, err=%d\n", ::GetLastError());
74+
CloseHandle(hFile);
75+
return -2;
76+
}
77+
78+
//try capture 1000 logs and exit
79+
const DWORD dwBufLen = 0x10000;
80+
PLOG_ITEM pBuf = (PLOG_ITEM)malloc(dwBufLen);
81+
DWORD nCount = 0, nMaxCount = 1000;
82+
while (1)
83+
{
84+
memset(pBuf, 0, dwBufLen);
85+
dwOut = 0;
86+
bRet = DeviceIoControl(hFile, DBGV_READ_LOG, NULL, 0, pBuf, dwBufLen, &dwOut, NULL);
87+
if (dwOut > 0)
88+
{
89+
PLOG_ITEM pNextItem = pBuf;
90+
while (pNextItem->dwIndex != 0)
91+
{
92+
SYSTEMTIME st = { 0 };
93+
FILETIME lt = { 0 };
94+
FileTimeToLocalFileTime(&pNextItem->liSystemTime, &lt);
95+
FileTimeToSystemTime(&lt, &st);
96+
printf("%d, Time:%04d-%02d-%02d %02d:%02d:%02d.%03d, %s\n",
97+
pNextItem->dwIndex,
98+
st.wYear,
99+
st.wMonth,
100+
st.wDay,
101+
st.wHour,
102+
st.wMinute,
103+
st.wSecond,
104+
st.wMilliseconds,
105+
pNextItem->strData);
106+
pNextItem = (PLOG_ITEM)((char*)pNextItem + sizeof(LOG_ITEM) + (strlen(pNextItem->strData) + 4) / 4 * 4);
107+
108+
nCount++;
109+
if (nCount > nMaxCount)
110+
{
111+
break;
112+
}
113+
}
114+
}
115+
116+
::Sleep(10);
117+
}
118+
119+
::free(pBuf);
120+
121+
bRet = DeviceIoControl(hFile, DBGV_UNCAPTURE_KERNEL, NULL, 0, NULL, 0, NULL, NULL);
122+
if (!bRet)
123+
{
124+
printf("DBGV_UNCAPTURE_KERNEL failed, err=%d\n", ::GetLastError());
125+
CloseHandle(hFile);
126+
return -1;
127+
}
128+
129+
CloseHandle(hFile);
130+
}
131+
132+
return 0;
133+
};
134+
135+
/// Driver
136+
137+
constexpr const char* DRIVER_SERVICE_NAME = "debugviewdriver";
138+
constexpr const char* DRIVER_DISPLAY_NAME = "DebugViewPP Kernel Message Driver";
139+
const std::string driverPath = "dbgv.sys";
140+
141+
void InstallDriver()
142+
{
143+
std::cout << "InstallDriver...\n";
144+
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
145+
if (!hSCManager) {
146+
std::cout << "Failed to open Service Control Manager. Error: " << GetLastError() << std::endl;
147+
}
148+
149+
SC_HANDLE hService = CreateServiceA(
150+
hSCManager,
151+
DRIVER_SERVICE_NAME,
152+
DRIVER_DISPLAY_NAME,
153+
SERVICE_ALL_ACCESS,
154+
SERVICE_KERNEL_DRIVER,
155+
SERVICE_DEMAND_START,
156+
SERVICE_ERROR_NORMAL,
157+
driverPath.c_str(),
158+
NULL, NULL, NULL, NULL, NULL
159+
);
160+
161+
if (!hService) {
162+
if (GetLastError() == ERROR_SERVICE_EXISTS) {
163+
std::cout << "Service already exists.\n";
164+
CloseServiceHandle(hSCManager);
165+
}
166+
std::cout << "Failed to create service. Error: " << GetLastError() << std::endl;
167+
CloseServiceHandle(hSCManager);
168+
}
169+
170+
CloseServiceHandle(hService);
171+
CloseServiceHandle(hSCManager);
172+
std::cout << "InstallDriver done...\n";
173+
}
174+
175+
void UninstallDriver()
176+
{
177+
std::cout << "UninstallDriver...\n";
178+
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
179+
if (!hSCManager) {
180+
std::cout << "Failed to open Service Control Manager. Error: " << GetLastError() << std::endl;
181+
}
182+
183+
SC_HANDLE hService = OpenServiceA(hSCManager, DRIVER_SERVICE_NAME, DELETE);
184+
if (!hService) {
185+
std::cout << "Failed to open service. Error: " << GetLastError() << std::endl;
186+
CloseServiceHandle(hSCManager);
187+
}
188+
189+
if (!DeleteService(hService)) {
190+
std::cout << "Failed to delete service. Error: " << GetLastError() << std::endl;
191+
CloseServiceHandle(hService);
192+
CloseServiceHandle(hSCManager);
193+
}
194+
195+
CloseServiceHandle(hService);
196+
CloseServiceHandle(hSCManager);
197+
std::cout << "UninstallDriver done...\n";
198+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// (C) Copyright Gert-Jan de Vos and Jan Wilmans 2013.
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
int monitor_kernel();
7+
void InstallDriver();
8+
void UninstallDriver();
9+

0 commit comments

Comments
 (0)