summaryrefslogtreecommitdiff
path: root/general/tracing/evntdrv/evntctrl/tracectl.c
blob: 2b9a0b78efb195a1afa61dbf915df78d39d0aa99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*++

Copyright (c) Microsoft Corporation.  All rights reserved.

    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
    KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
    PURPOSE.

Module Name:

    tracectl.c

Environment:

    User mode Win32 console application

Revision History:


--*/
#define UNICODE
#define _UNICODE
#include <windows.h>
#include <winioctl.h>
#include <tchar.h>
#include <stdio.h>
#include "drvioctl.h"
#include "install.h"
#include <conio.h>
#include <strsafe.h>

#define WAIT_TIME       10

BOOLEAN
SetupDriverName(
    _Out_writes_(MAX_PATH)LPTSTR DriverLocation
    );

int _cdecl main(int argc, LPCTSTR argv[])
{
    HANDLE hDevice;              // handle to a device, file, or directory 
    DWORD  dwError = ERROR_SUCCESS;
    LPVOID lpFileName = _T("\\\\.\\EventEtw") ;
    TCHAR  driverLocation[MAX_PATH];
    DWORD     dwOutBuffer[2048];
    DWORD  dwOutBufferCount ;
    int ch;
  
    UNREFERENCED_PARAMETER(argc);
    UNREFERENCED_PARAMETER(argv);

    if ((hDevice = CreateFile(
                        lpFileName,             // pointer to name of the file
                        0,                      // access (read-write) mode
                        0,                      // share mode
                        NULL,                   // pointer to security attributes
                        OPEN_EXISTING,          // how to create
                        FILE_ATTRIBUTE_NORMAL,  // file attributes
                        NULL                    // handle to file with attributes to 
                                                // copy
                                    )) == INVALID_HANDLE_VALUE) {
        dwError = GetLastError();

        if ( dwError != ERROR_FILE_NOT_FOUND ) {
            _tprintf(_T("CreateFile failed ! error: %d\n"), dwError);
            return 1;
        }

        //
        //  Setup full path to driver name
        //

        if (!SetupDriverName(driverLocation)) {

            return 2;

        }

        //
        // Install driver
        //

        if (!ManageDriver(DRIVER_NAME,
                          driverLocation,
                          DRIVER_FUNC_INSTALL
                          )) {

            _tprintf(_T("Unable to install driver. \n"));

            //
            // Error - remove driver.
            //

            ManageDriver(DRIVER_NAME,
                         driverLocation,
                         DRIVER_FUNC_REMOVE
                         );
            
            return 3;
        }

        if ((hDevice = CreateFile(
                                lpFileName,             // pointer to name of the file
                                0,                      // access (read-write) mode
                                0,                      // share mode
                                NULL,                   // pointer to security attributes
                                OPEN_EXISTING,          // how to create
                                FILE_ATTRIBUTE_NORMAL,  // file attributes
                                NULL                    // handle to file with attributes to 
                                                        // copy
                                            )) == INVALID_HANDLE_VALUE) {        

            _tprintf(_T("Error: CreateFile failed\n"));
            return 4;
        }
    }



    _tprintf(_T("\nPress 'q' to exit, any other key to send ioctl...\n"));
    fflush(stdin);
    ch = _getche();

    while(tolower(ch) != 'q' )
    {
    
         _tprintf(_T("Making IOCTL_EVNTKMP_TRACE_EVENT_A ioctl to log events\n"));
        if (DeviceIoControl(
            hDevice,                    // handle to a device, file, or directory 
            IOCTL_EVNTKMP_TRACE_EVENT_A, // control code of operation to perform
            NULL,                        // pointer to buffer to supply input data
            0,                            // size, in bytes, of input buffer
            dwOutBuffer,                // pointer to buffer to receive output data
            2048,                        // size, in bytes, of output buffer
            &dwOutBufferCount,            // pointer to variable to receive byte count
            NULL                        // pointer to structure for asynchronous operation
            ) == 0) {

            _tprintf(_T("DeviceIOControl Failed %d\n"),GetLastError());
            return 5;

        }
        ch = _getche();
    }

    if (CloseHandle(hDevice) == 0) {

        _tprintf(_T("CloseHandle Failed %d\n"),GetLastError());
        return 6;

    }

    //
    //  stop the driver
    //
    
    ManageDriver(DRIVER_NAME,
                 driverLocation,
                 DRIVER_FUNC_REMOVE
                 );

    _tprintf(_T("Driver '%s' is removed\n"), DRIVER_NAME);

    return 0;
}

BOOLEAN
SetupDriverName(
    _Out_writes_(MAX_PATH) LPTSTR DriverLocation
    )
{
    HANDLE fileHandle;

    DWORD driverLocLen = 0;

    //
    // Get the current directory.
    //

    driverLocLen = GetCurrentDirectory(MAX_PATH,
                                       DriverLocation
                                       );

    if (!driverLocLen) {

        _tprintf(_T("GetCurrentDirectory failed!  Error = %d \n"), GetLastError());

        return FALSE;
    }

    //
    // Setup path name to driver file.
    //

    if (StringCchPrintf(&DriverLocation[_tcslen(DriverLocation)], 
                        (MAX_PATH - _tcslen(DriverLocation)),
                        _T("\\%s.sys"),
                        DRIVER_NAME) != S_OK){
        _tprintf(_T("Failed to generate DriverLocation!,  StringCchPrintf Error = %d \n"), GetLastError());
        return FALSE;
    }

    //
    // Insure driver file is in the specified directory.
    //

    if ((fileHandle = CreateFile(DriverLocation,
                                 GENERIC_READ,
                                 0,
                                 NULL,
                                 OPEN_EXISTING,
                                 FILE_ATTRIBUTE_NORMAL,
                                 NULL
                                 )) == INVALID_HANDLE_VALUE) {


        _tprintf(_T("Driver: '%s' is not in the current directory. \n"), DRIVER_NAME);

        //
        // Indicate failure.
        //

        return FALSE;
    }

    //
    // Close open file handle.
    //

    if (fileHandle) {

        CloseHandle(fileHandle);
    }

    //
    // Indicate success.
    //

    return TRUE;


}   // SetupDriverName