Skip to content

Commit b92b78f

Browse files
author
Jan Wilmans
committed
implemented monitoring kernel messages, rought version, verbose by default
1 parent 07e44f1 commit b92b78f

File tree

7 files changed

+151
-153
lines changed

7 files changed

+151
-153
lines changed

application/DebugViewpp/version.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
#define VERSION 1,9,0,56
2-
#define VERSION_STR "1.9.0.56"
1+
#define VERSION 1,9,0,58
2+
#define VERSION_STR "1.9.0.58"

application/DebugViewpp/version.wxi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
<?define ProductVersion.Major="1" ?>
44
<?define ProductVersion.Minor="9" ?>
55
<?define ProductVersion.Revision="0" ?>
6-
<?define ProductVersion.Build="56" ?>
7-
<?define ProductVersion="1.9.0.56" ?>
6+
<?define ProductVersion.Build="58" ?>
7+
<?define ProductVersion="1.9.0.58" ?>
88
</Include>

application/DebugViewppLib/Debugview_kernel_client.cpp

Lines changed: 4 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -3,144 +3,15 @@
33
// (See accompanying file LICENSE_1_0.txt or copy at
44
// http://www.boost.org/LICENSE_1_0.txt)
55

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
6+
#include "DebugViewppLib/Debugview_kernel_client.h"
1367

1378
constexpr const char* DRIVER_SERVICE_NAME = "debugviewdriver";
1389
constexpr const char* DRIVER_DISPLAY_NAME = "DebugViewPP Kernel Message Driver";
13910
const std::string driverPath = "dbgv.sys";
14011

141-
void InstallDriver()
12+
void InstallKernelMessagesDriver()
14213
{
143-
std::cout << "InstallDriver...\n";
14+
UninstallKernelMessagesDriver();
14415
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
14516
if (!hSCManager) {
14617
std::cout << "Failed to open Service Control Manager. Error: " << GetLastError() << std::endl;
@@ -166,15 +37,12 @@ void InstallDriver()
16637
std::cout << "Failed to create service. Error: " << GetLastError() << std::endl;
16738
CloseServiceHandle(hSCManager);
16839
}
169-
17040
CloseServiceHandle(hService);
17141
CloseServiceHandle(hSCManager);
172-
std::cout << "InstallDriver done...\n";
17342
}
17443

175-
void UninstallDriver()
44+
void UninstallKernelMessagesDriver()
17645
{
177-
std::cout << "UninstallDriver...\n";
17846
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
17947
if (!hSCManager) {
18048
std::cout << "Failed to open Service Control Manager. Error: " << GetLastError() << std::endl;
@@ -191,8 +59,6 @@ void UninstallDriver()
19159
CloseServiceHandle(hService);
19260
CloseServiceHandle(hSCManager);
19361
}
194-
19562
CloseServiceHandle(hService);
19663
CloseServiceHandle(hSCManager);
197-
std::cout << "UninstallDriver done...\n";
19864
}

application/DebugViewppLib/Debugview_kernel_client.h

Lines changed: 0 additions & 9 deletions
This file was deleted.

application/DebugViewppLib/KernelReader.cpp

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,74 @@
77
#include "DebugViewppLib/PolledLogSource.h"
88
#include "DebugViewppLib/KernelReader.h"
99
#include "DebugViewppLib/LineBuffer.h"
10+
#include "DebugViewppLib/Debugview_kernel_client.h"
1011

1112
namespace fusion {
1213
namespace debugviewpp {
1314

15+
void KernelReader::StartListening()
16+
{
17+
Win32::Handle handle(::CreateFile(strDbgviewKernelDriverDeviceName,
18+
GENERIC_READ,
19+
FILE_SHARE_READ,
20+
NULL,
21+
OPEN_EXISTING,
22+
FILE_ATTRIBUTE_NORMAL,
23+
NULL));
24+
if (handle.get() == INVALID_HANDLE_VALUE)
25+
{
26+
AddMessage(0, "internal", "Could not connected to kernel messages driver");
27+
return;
28+
}
29+
30+
// enable capture
31+
DWORD dwOut = 0;
32+
DWORD dwReturned = 0;
33+
BOOL bRet = DeviceIoControl(handle.get(), DBGV_CAPTURE_KERNEL, NULL, 0, &dwOut, sizeof(dwOut), &dwReturned, NULL);
34+
if (!bRet)
35+
{
36+
printf("DBGV_CAPTURE_KERNEL failed, err=%d\n", ::GetLastError());
37+
return;
38+
}
39+
40+
// enable verbose kernel messages
41+
bRet = DeviceIoControl(handle.get(), DBGV_ENABLE_FILTER_STATE, NULL, 0, NULL, 0, NULL, NULL);
42+
if (!bRet)
43+
{
44+
printf("DBGV_ENABLE_FILTER_STATE failed, err=%d\n", ::GetLastError());
45+
return;
46+
}
47+
m_handle = std::move(handle);
48+
m_pBuf = (PLOG_ITEM)malloc(dwBufLen);
49+
}
50+
51+
void KernelReader::StopListening()
52+
{
53+
BOOL bRet = DeviceIoControl(m_handle.get(), DBGV_UNCAPTURE_KERNEL, NULL, 0, NULL, 0, NULL, NULL);
54+
if (!bRet)
55+
{
56+
printf("DBGV_UNCAPTURE_KERNEL failed, err=%d\n", ::GetLastError());
57+
}
58+
m_handle.reset();
59+
::free(m_pBuf);
60+
}
61+
1462
KernelReader::KernelReader(Timer& timer, ILineBuffer& linebuffer) :
1563
PolledLogSource(timer, SourceType::Pipe, linebuffer, 1)
1664
{
1765
SetDescription(L"Kernel Message Reader");
66+
InstallKernelMessagesDriver();
1867
AddMessage(0, "kernel", "Started capturing kernel messages");
1968
Signal();
69+
StartListening();
2070
StartThread();
2171
}
2272

23-
KernelReader::~KernelReader() = default;
73+
KernelReader::~KernelReader()
74+
{
75+
StopListening();
76+
UninstallKernelMessagesDriver();
77+
}
2478

2579
void KernelReader::Abort()
2680
{
@@ -36,7 +90,34 @@ bool KernelReader::AtEnd() const
3690

3791
void KernelReader::Poll()
3892
{
39-
AddMessage(0, "kernel", "Test message...");
93+
memset(m_pBuf, 0, dwBufLen);
94+
DWORD dwOut = 0;
95+
::DeviceIoControl(m_handle.get(), DBGV_READ_LOG, NULL, 0, m_pBuf, dwBufLen, &dwOut, NULL);
96+
if (dwOut == 0) return; // no messages to be read
97+
98+
PLOG_ITEM pNextItem = m_pBuf;
99+
while (pNextItem->dwIndex != 0)
100+
{
101+
SYSTEMTIME st = { 0 };
102+
FILETIME lt = { 0 };
103+
FileTimeToLocalFileTime(&pNextItem->liSystemTime, &lt);
104+
FileTimeToSystemTime(&lt, &st);
105+
106+
char message[4000];
107+
sprintf(message, "%d, Time:%04d-%02d-%02d %02d:%02d:%02d.%03d, %s\n",
108+
pNextItem->dwIndex,
109+
st.wYear,
110+
st.wMonth,
111+
st.wDay,
112+
st.wHour,
113+
st.wMinute,
114+
st.wSecond,
115+
st.wMilliseconds,
116+
pNextItem->strData);
117+
118+
AddMessage(0, "kernel", message);
119+
pNextItem = (PLOG_ITEM)((char*)pNextItem + sizeof(LOG_ITEM) + (strlen(pNextItem->strData) + 4) / 4 * 4);
120+
}
40121
}
41122

42123
} // namespace debugviewpp
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
#pragma once
7+
8+
void InstallKernelMessagesDriver();
9+
void UninstallKernelMessagesDriver();
10+
11+
#include <windows.h>
12+
#include <tchar.h>
13+
#include <winioctl.h>
14+
#include <winsvc.h>
15+
16+
#include <string>
17+
#include <iostream>
18+
19+
// this is the dbgv.sys interface
20+
#define FILE_DEVICE_DBGV 0x8305
21+
22+
#define DBGV_CAPTURE_KERNEL CTL_CODE(FILE_DEVICE_DBGV, DWORD(0), METHOD_BUFFERED, FILE_ANY_ACCESS) //0x00 //enable capture kernel
23+
#define DBGV_UNCAPTURE_KERNEL CTL_CODE(FILE_DEVICE_DBGV, DWORD(1), METHOD_BUFFERED, FILE_ANY_ACCESS) //0x04 //
24+
#define DBGV_CLEAR_DISPLAY CTL_CODE(FILE_DEVICE_DBGV, DWORD(2), METHOD_BUFFERED, FILE_ANY_ACCESS) //0x08 //clear display
25+
#define DBGV_READ_LOG CTL_CODE(FILE_DEVICE_DBGV, DWORD(3), METHOD_NEITHER, FILE_ANY_ACCESS) //0x0f //read kernel log
26+
#define DBGV_SET_PASSTHROUGH CTL_CODE(FILE_DEVICE_DBGV, DWORD(4), METHOD_BUFFERED, FILE_ANY_ACCESS) //0x10 //enable passthrough
27+
#define DBGV_UNSET_PASSTHROUGH CTL_CODE(FILE_DEVICE_DBGV, DWORD(5), METHOD_BUFFERED, FILE_ANY_ACCESS) //0x14 //
28+
#define DBGV_IS_DRIVER_AVAILABLE CTL_CODE(FILE_DEVICE_DBGV, DWORD(8), METHOD_BUFFERED, FILE_ANY_ACCESS) //0x20 //test driver is valid or functional
29+
#define DBGV_GET_DRIVER_VERSION CTL_CODE(FILE_DEVICE_DBGV, DWORD(9), METHOD_BUFFERED, FILE_ANY_ACCESS) //0x24 //driver version, 4.70 = 0x800
30+
#define DBGV_SET_CARRIAGE_RETURN CTL_CODE(FILE_DEVICE_DBGV, DWORD(0x0d), METHOD_BUFFERED, FILE_ANY_ACCESS) //0x34 //force carriage return
31+
#define DBGV_UNSET_CARRIAGE_RETURN CTL_CODE(FILE_DEVICE_DBGV, DWORD(0x0e), METHOD_BUFFERED, FILE_ANY_ACCESS) //0x38 //
32+
#define DBGV_ENABLE_FILTER_STATE CTL_CODE(FILE_DEVICE_DBGV, DWORD(0x0f), METHOD_BUFFERED, FILE_ANY_ACCESS) //0x3C //enable log verbose
33+
#define DBGV_SET_FILTER_STATE CTL_CODE(FILE_DEVICE_DBGV, DWORD(0x10), METHOD_BUFFERED, FILE_ANY_ACCESS) //0x40 //reset log verbose
34+
35+
// suppress the non-standard use of zero-sized array in struct/union
36+
#pragma warning(disable:4200)
37+
38+
#pragma pack(1)
39+
typedef struct
40+
{
41+
DWORD dwIndex;
42+
FILETIME liSystemTime;
43+
LARGE_INTEGER liPerfCounter;
44+
CHAR strData[0];
45+
} LOG_ITEM, *PLOG_ITEM;
46+
#pragma pack()
47+
48+
constexpr const wchar_t * strDbgviewKernelDriverDeviceName = L"\\\\.\\dbgv";
49+
constexpr const DWORD dwBufLen = 0x10000;
50+

application/include/DebugViewppLib/KernelReader.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
#include "PipeReader.h"
99
#include "PolledLogSource.h"
1010

11+
#include "Win32/Win32Lib.h"
12+
13+
#include "Debugview_kernel_client.h"
14+
1115
namespace fusion {
1216
namespace debugviewpp {
1317

@@ -23,6 +27,12 @@ class KernelReader : public PolledLogSource
2327

2428
private:
2529
void Poll() override;
30+
31+
void StartListening();
32+
void StopListening();
33+
34+
Win32::Handle m_handle;
35+
PLOG_ITEM m_pBuf;
2636
};
2737

2838
} // namespace debugviewpp

0 commit comments

Comments
 (0)