Windows: SYMSTORE the entire WINNT/afsrdr tree
[openafs.git] / src / WINNT / afsrdr / tools / settrace / settrace.cpp
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 Kernel Drivers, LLC.
3  * Copyright (c) 2009, 2010, 2011 Your File System, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright notice,
11  *   this list of conditions and the following disclaimer.
12  * - Redistributions in binary form must reproduce the above copyright
13  *   notice,
14  *   this list of conditions and the following disclaimer in the
15  *   documentation
16  *   and/or other materials provided with the distribution.
17  * - Neither the names of Kernel Drivers, LLC and Your File System, Inc.
18  *   nor the names of their contributors may be used to endorse or promote
19  *   products derived from this software without specific prior written
20  *   permission from Kernel Drivers, LLC and Your File System, Inc.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
26  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #ifndef _WIN32_WINNT
36 #define _WIN32_WINNT 0x0500
37 #endif
38
39 #include <windows.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <shlwapi.h>
43 #include <devioctl.h>
44
45 #include "AFSUserDefines.h"
46 #include "AFSUserIoctl.h"
47 #include "AFSUserStructs.h"
48
49 void
50 usage( void)
51 {
52
53     printf("SetTrace /l <Logging level> | /s <Subsystem> | /b <Buffer size in KB> | /f <debug Flags>\n");
54 }
55
56 int main(int argc, char* argv[])
57 {
58
59     ULONG rc = 0;
60     DWORD bytesReturned = 0;
61     HANDLE hControlDevice = NULL;
62     DWORD dwError = 0;
63     AFSTraceConfigCB stConfig;
64     int dwLevel = -1, dwSystem = -1, dwBufferLen = -1, dwFlags = -1;
65     int dwIndex;
66
67     if( argc < 3)
68     {
69
70         usage();
71
72         return 1;
73     }
74
75     hControlDevice = CreateFile( AFS_SYMLINK,
76                                  GENERIC_READ | GENERIC_WRITE,
77                                  FILE_SHARE_READ | FILE_SHARE_WRITE,
78                                  NULL,
79                                  OPEN_EXISTING,
80                                  0,
81                                  NULL );
82
83     if( hControlDevice == INVALID_HANDLE_VALUE)
84     {
85
86         printf( "SetTrace: Failed to open control device error: %d\n", GetLastError());
87
88         return 2;
89     }
90
91     dwIndex = 1;
92
93     while( dwIndex < argc)
94     {
95
96         if( _strcmpi(argv[ dwIndex], "/l") == 0)
97         {
98
99             if ( !StrToIntExA( argv[ ++dwIndex], STIF_SUPPORT_HEX, &dwLevel))
100             {
101                 printf("SetTrace Failed to parse parameter %s\n", argv[ dwIndex]);
102
103                 dwError = 1;
104
105                 break;
106             }
107         }
108         else if( _strcmpi(argv[ dwIndex], "/s") == 0)
109         {
110
111             if ( !StrToIntExA( argv[ ++dwIndex], STIF_SUPPORT_HEX, &dwSystem))
112             {
113                 printf("SetTrace Failed to parse parameter %s\n", argv[ dwIndex]);
114
115                 dwError = 1;
116
117                 break;
118             }
119         }
120         else if( _strcmpi(argv[ dwIndex], "/b") == 0)
121         {
122
123             if ( !StrToIntExA( argv[ ++dwIndex], STIF_SUPPORT_HEX, &dwBufferLen))
124             {
125                 printf("SetTrace Failed to parse parameter %s\n", argv[ dwIndex]);
126
127                 dwError = 1;
128
129                 break;
130             }
131         }
132         else if( _strcmpi(argv[ dwIndex], "/d") == 0 ||
133                  _strcmpi(argv[ dwIndex], "/f") == 0)
134         {
135
136             if ( !StrToIntExA( argv[ ++dwIndex], STIF_SUPPORT_HEX, &dwFlags))
137             {
138                 printf("SetTrace Failed to parse parameter %s\n", argv[ dwIndex]);
139
140                 dwError = 1;
141
142                 break;
143             }
144         }
145         else
146         {
147
148             usage();
149
150             dwError = 1;
151
152             break;
153         }
154
155         dwIndex++;
156     }
157
158     if ( dwError)
159     {
160
161         rc = 4;
162
163         goto cleanup;
164     }
165
166     stConfig.Subsystem = dwSystem;
167
168     stConfig.TraceLevel = dwLevel;
169
170     stConfig.TraceBufferLength = dwBufferLen;
171
172     stConfig.DebugFlags = dwFlags;
173
174     dwError = DeviceIoControl( hControlDevice,
175                                IOCTL_AFS_CONFIGURE_DEBUG_TRACE,
176                                &stConfig,
177                                sizeof( AFSTraceConfigCB),
178                                NULL,
179                                0,
180                                &bytesReturned,
181                                NULL);
182
183     if( !dwError)
184     {
185
186         printf( "SetTrace Failed to set attributes %d\n", GetLastError());
187
188         rc = 3;
189     }
190     else
191     {
192
193         printf( "SetTrace: Successfully set parameters\n");
194     }
195
196   cleanup:
197
198     CloseHandle( hControlDevice);
199
200     return rc;
201 }
202