|
| 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, <); |
| 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 |
| 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 | +} |
0 commit comments