1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef APR_DBG_WIN32_HANDLES_H
18#define APR_DBG_WIN32_HANDLES_H
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24/* USAGE:
25 *
26 * Add the following include to apr_private.h for internal debugging,
27 * or copy this header into apr/include add the include below to apr.h
28 * for really global debugging;
29 *
30 *   #include "apr_dbg_win32_handles.h"
31 *
32 * apr_dbg_log is the crux of this function ... it uses Win32 API and
33 * no apr calls itself to log all activity to a file named for the
34 * executing application with a .pid suffix.  Ergo several instances
35 * may be executing and logged at once.
36 *
37 * HANDLE apr_dbg_log(char* fn, HANDLE ha, char* fl, int ln, int nh
38 *                           [, HANDLE *hv, char *dsc...])
39 *
40 * returns: the handle passed in ha, which is cast back to the real return type.
41 *
42 * formats one line into the debug log file if nh is zero;
43 * ha (hex) seq(hex) tid(hex) fn     fl         ln
44 * xxxxxxxx xxxxxxxx xxxxxxxx func() sourcefile:lineno
45 * The macro apr_dbg_rv makes this simple to implement for many APIs
46 * that simply take args that don't interest us, and return a handle.
47 *
48 * formats multiple lines (nh) into the debug log file for each hv/dsc pair
49 * (nh must correspond to the number of pairs);
50 * hv (hex) seq(hex) tid(hex) fn   dsc  fl         ln
51 * xxxxxxxx xxxxxxxx xxxxxxxx func(arg) sourcefile:lineno
52 * In this later usage, hv is the still the return value but is not
53 * treated as a handle.
54 */
55
56APR_DECLARE_NONSTD(HANDLE) apr_dbg_log(char* fn, HANDLE ha, char* fl, int ln,
57                                       int nh,/* HANDLE *hv, char *dsc */...);
58
59#define apr_dbg_rv(fn, args) (apr_dbg_log(#fn,(fn) args,__FILE__,__LINE__,0))
60
61#define CloseHandle(h) \
62    ((BOOL)apr_dbg_log("CloseHandle", \
63                       (HANDLE)(CloseHandle)(h), \
64                       __FILE__,__LINE__,1, \
65                       &(h),""))
66
67#define CreateEventA(sd,b1,b2,nm) apr_dbg_rv(CreateEventA,(sd,b1,b2,nm))
68#define CreateEventW(sd,b1,b2,nm) apr_dbg_rv(CreateEventW,(sd,b1,b2,nm))
69
70#define CreateFileA(nm,d1,d2,sd,d3,d4,h) apr_dbg_rv(CreateFileA,(nm,d1,d2,sd,d3,d4,h))
71#define CreateFileW(nm,d1,d2,sd,d3,d4,h) apr_dbg_rv(CreateFileW,(nm,d1,d2,sd,d3,d4,h))
72
73#define CreateFileMappingA(fh,sd,d1,d2,d3,nm) apr_dbg_rv(CreateFileMappingA,(fh,sd,d1,d2,d3,nm))
74#define CreateFileMappingW(fh,sd,d1,d2,d3,nm) apr_dbg_rv(CreateFileMappingW,(fh,sd,d1,d2,d3,nm))
75
76#define CreateMutexA(sd,b,nm) apr_dbg_rv(CreateMutexA,(sd,b,nm))
77#define CreateMutexW(sd,b,nm) apr_dbg_rv(CreateMutexW,(sd,b,nm))
78
79#define CreateIoCompletionPort(h1,h2,pd1,d2) apr_dbg_rv(CreateIoCompletionPort,(h1,h2,pd1,d2))
80
81#define CreateNamedPipeA(nm,d1,d2,d3,d4,d5,d6,sd) apr_dbg_rv(CreateNamedPipeA,(nm,d1,d2,d3,d4,d5,d6,sd))
82#define CreateNamedPipeW(nm,d1,d2,d3,d4,d5,d6,sd) apr_dbg_rv(CreateNamedPipeW,(nm,d1,d2,d3,d4,d5,d6,sd))
83
84#define CreatePipe(ph1,ph2,sd,d) \
85    ((BOOL)apr_dbg_log("CreatePipe", \
86                       (HANDLE)(CreatePipe)(ph1,ph2,sd,d), \
87                       __FILE__,__LINE__,2, \
88                       (ph1),"hRead", \
89                       (ph2),"hWrite"))
90
91#define CreateProcessA(s1,s2,sd1,sd2,b,d1,s3,s4,pd2,hr) \
92    ((BOOL)apr_dbg_log("CreateProcessA", \
93                       (HANDLE)(CreateProcessA)(s1,s2,sd1,sd2,b,d1,s3,s4,pd2,hr), \
94                       __FILE__,__LINE__,2, \
95                       &((hr)->hProcess),"hProcess", \
96                       &((hr)->hThread),"hThread"))
97#define CreateProcessW(s1,s2,sd1,sd2,b,d1,s3,s4,pd2,hr) \
98    ((BOOL)apr_dbg_log("CreateProcessW", \
99                       (HANDLE)(CreateProcessW)(s1,s2,sd1,sd2,b,d1,s3,s4,pd2,hr), \
100                       __FILE__,__LINE__,2, \
101                       &((hr)->hProcess),"hProcess", \
102                       &((hr)->hThread),"hThread"))
103
104#define CreateSemaphoreA(sd,d1,d2,nm) apr_dbg_rv(CreateSemaphoreA,(sd,d1,d2,nm))
105#define CreateSemaphoreW(sd,d1,d2,nm) apr_dbg_rv(CreateSemaphoreW,(sd,d1,d2,nm))
106
107#define CreateThread(sd,d1,fn,pv,d2,pd3) apr_dbg_rv(CreateThread,(sd,d1,fn,pv,d2,pd3))
108
109#define DeregisterEventSource(h) \
110    ((BOOL)apr_dbg_log("DeregisterEventSource", \
111                       (HANDLE)(DeregisterEventSource)(h), \
112                       __FILE__,__LINE__,1, \
113                       &(h),""))
114
115#define DuplicateHandle(h1,h2,h3,ph4,d1,b,d2) \
116    ((BOOL)apr_dbg_log("DuplicateHandle", \
117                       (HANDLE)(DuplicateHandle)(h1,h2,h3,ph4,d1,b,d2), \
118                       __FILE__,__LINE__,2, \
119                       (ph4),((h3)==GetCurrentProcess()) \
120                                   ? "Target" : "EXTERN Target", \
121                       &(h2),((h1)==GetCurrentProcess()) \
122                                 ? "Source" : "EXTERN Source"))
123
124#define GetCurrentProcess() \
125    (apr_dbg_log("GetCurrentProcess", \
126                 (GetCurrentProcess)(),__FILE__,__LINE__,0))
127
128#define GetCurrentThread() \
129    (apr_dbg_log("GetCurrentThread", \
130                 (GetCurrentThread)(),__FILE__,__LINE__,0))
131
132#define GetModuleHandleA(nm) apr_dbg_rv(GetModuleHandleA,(nm))
133#define GetModuleHandleW(nm) apr_dbg_rv(GetModuleHandleW,(nm))
134
135#define GetStdHandle(d) apr_dbg_rv(GetStdHandle,(d))
136
137#define LoadLibraryA(nm) apr_dbg_rv(LoadLibraryA,(nm))
138#define LoadLibraryW(nm) apr_dbg_rv(LoadLibraryW,(nm))
139
140#define LoadLibraryExA(nm,h,d) apr_dbg_rv(LoadLibraryExA,(nm,h,d))
141#define LoadLibraryExW(nm,h,d) apr_dbg_rv(LoadLibraryExW,(nm,h,d))
142
143#define OpenEventA(d,b,nm) apr_dbg_rv(OpenEventA,(d,b,nm))
144#define OpenEventW(d,b,nm) apr_dbg_rv(OpenEventW,(d,b,nm))
145
146#define OpenFileMappingA(d,b,nm) apr_dbg_rv(OpenFileMappingA,(d,b,nm))
147#define OpenFileMappingW(d,b,nm) apr_dbg_rv(OpenFileMappingW,(d,b,nm))
148
149#define RegisterEventSourceA(s1,s2) apr_dbg_rv(RegisterEventSourceA,(s1,s2))
150#define RegisterEventSourceW(s1,s2) apr_dbg_rv(RegisterEventSourceW,(s1,s2))
151
152#define SetEvent(h) \
153    ((BOOL)apr_dbg_log("SetEvent", \
154                       (HANDLE)(SetEvent)(h), \
155                       __FILE__,__LINE__,1, \
156                       &(h),""))
157
158#define SetStdHandle(d,h) \
159    ((BOOL)apr_dbg_log("SetStdHandle", \
160                       (HANDLE)(SetStdHandle)(d,h), \
161                       __FILE__,__LINE__,1,&(h),""))
162
163#define socket(i1,i2,i3) \
164    ((SOCKET)apr_dbg_log("socket", \
165                         (HANDLE)(socket)(i1,i2,i3), \
166                       __FILE__,__LINE__,0))
167
168#define WaitForSingleObject(h,d) \
169    ((DWORD)apr_dbg_log("WaitForSingleObject", \
170                        (HANDLE)(WaitForSingleObject)(h,d), \
171                        __FILE__,__LINE__,1,&(h),"Signaled"))
172
173#define WaitForSingleObjectEx(h,d,b) \
174    ((DWORD)apr_dbg_log("WaitForSingleObjectEx", \
175                        (HANDLE)(WaitForSingleObjectEx)(h,d,b), \
176                        __FILE__,__LINE__,1,&(h),"Signaled"))
177
178#define WaitForMultipleObjects(d1,ah,b,d2) \
179    ((DWORD)apr_dbg_log("WaitForMultipleObjects", \
180                        (HANDLE)(WaitForMultipleObjects)(d1,ah,b,d2), \
181                        __FILE__,__LINE__,1,ah,"Signaled"))
182
183#define WaitForMultipleObjectsEx(d1,ah,b1,d2,b2) \
184    ((DWORD)apr_dbg_log("WaitForMultipleObjectsEx", \
185                        (HANDLE)(WaitForMultipleObjectsEx)(d1,ah,b1,d2,b2), \
186                        __FILE__,__LINE__,1,ah,"Signaled"))
187
188#define WSASocketA(i1,i2,i3,pi,g,dw) \
189    ((SOCKET)apr_dbg_log("WSASocketA", \
190                         (HANDLE)(WSASocketA)(i1,i2,i3,pi,g,dw), \
191                       __FILE__,__LINE__,0))
192
193#define WSASocketW(i1,i2,i3,pi,g,dw) \
194    ((SOCKET)apr_dbg_log("WSASocketW", \
195                         (HANDLE)(WSASocketW)(i1,i2,i3,pi,g,dw), \
196                       __FILE__,__LINE__,0))
197
198#define closesocket(sh) \
199    ((int)apr_dbg_log("closesocket", \
200                      (HANDLE)(closesocket)(sh), \
201                      __FILE__,__LINE__,1,&(sh),""))
202
203#define _beginthread(fn,d,pv) \
204    ((unsigned long)apr_dbg_log("_beginthread", \
205                                (HANDLE)(_beginthread)(fn,d,pv), \
206                                __FILE__,__LINE__,0))
207
208#define _beginthreadex(sd,d1,fn,pv,d2,pd3) \
209    ((unsigned long)apr_dbg_log("_beginthreadex", \
210                                (HANDLE)(_beginthreadex)(sd,d1,fn,pv,d2,pd3), \
211                                __FILE__,__LINE__,0))
212
213#ifdef __cplusplus
214}
215#endif
216
217#endif /* !defined(APR_DBG_WIN32_HANDLES_H) */
218