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 , <);
95- FileTimeToSystemTime (<, &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
1378constexpr const char * DRIVER_SERVICE_NAME = " debugviewdriver" ;
1389constexpr const char * DRIVER_DISPLAY_NAME = " DebugViewPP Kernel Message Driver" ;
13910const 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}
0 commit comments