Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

572 lines
15 KiB

  1. //============================================================================
  2. // Copyright (c) 1995, Microsoft Corporation
  3. //
  4. // File: trace.h
  5. //
  6. // History:
  7. // Abolade Gbadegesin July-24-1995 Created
  8. //
  9. // Private declarations for tracing API functions
  10. //
  11. // Support for both ANSI and Unicode is achieved by having separate
  12. // versions of all code-page dependent functions for each.
  13. // All source files containing code-page dependent functions contain only
  14. // code-page dependent functions, and the build instructions copy the
  15. // source files containing code-page dependent functions to two separate
  16. // files, one of which is compiled with UNICODE defined, the other without.
  17. //
  18. // At this time the macros in this header file are resolved either to
  19. // ANSI declarations or Unicode declarations for types and functions.
  20. // Thus there is only one set of sources to maintain but compilation produces
  21. // separate execution paths for clients using ANSI and Unicode.
  22. // For example the file api.c which contains TraceRegisterEx will be copied
  23. // to both api_a.c and api_w.c, and api_w.c will be compiled with -DUNICODE
  24. // causing TraceRegisterEx in the file to resolve to TraceRegisterExW,
  25. // and causing TCHAR and LPTSTR and LPCTSTR to resolve to WCHAR, LPWSTR, and
  26. // LPCWSTR respectively; api_a.c will be compiled with UNICODE undefined,
  27. // causing TraceRegisterEx to resolve to TraceRegisterExA, and causing TCHAR,
  28. // LPTSTR and LPCTSTR to resolve to CHAR, LPSTR and LPCSTR respectively.
  29. // The final DLL will then contain both TraceRegisterExA and TraceRegisterExW
  30. // and clients which define UNICODE will end up invoking TraceRegisterExW
  31. // and the other Unicode variants, while clients which do not will invoke
  32. // TraceRegisterExA and the other ANSI variants.
  33. //
  34. // The server thread and the functions it invokes include explicit
  35. // calls to the correct code-page dependent function based on whether the
  36. // flag TRACEFLAGS_UNICODE is set in the client structure being operated on.
  37. // Thus, the server is the only part of the system aware that more than one
  38. // code page is in use, and the file server.c is compiled as is so the
  39. // single set of its functions deals with both ANSI and Unicode clients.
  40. // The alternative would be to have two server threads when both
  41. // ANSI and Unicode clients register in a single process, but this alternative
  42. // is too costly in terms of resources and additional synchronization.
  43. //============================================================================
  44. #ifndef _TRACE_H_
  45. #define _TRACE_H_
  46. #define TRACEFLAGS_DISABLED 0x00000001
  47. #define TRACEFLAGS_USEFILE 0x00000002
  48. #define TRACEFLAGS_USECONSOLE 0x00000004
  49. #define TRACEFLAGS_REGCONFIG 0x00000008
  50. #define TRACEFLAGS_SERVERTHREAD 0x00000010
  51. #define MAX_CLIENT_COUNT 60
  52. #define MAX_CLIENTNAME_LENGTH 64
  53. #define CLIENT_SIGNATURE 0xdcba0000
  54. //
  55. // strings associated with client registry configuration
  56. //
  57. #define REGKEY_TRACING TEXT("Software\\Microsoft\\Tracing")
  58. #define REGVAL_ENABLEFILETRACING TEXT("EnableFileTracing")
  59. #define REGVAL_ENABLECONSOLETRACING TEXT("EnableConsoleTracing")
  60. #define REGVAL_FILETRACINGMASK TEXT("FileTracingMask")
  61. #define REGVAL_CONSOLETRACINGMASK TEXT("ConsoleTracingMask")
  62. #define REGVAL_MAXFILESIZE TEXT("MaxFileSize")
  63. #define REGVAL_FILEDIRECTORY TEXT("FileDirectory")
  64. #define DEF_ENABLEFILETRACING 0
  65. #define DEF_ENABLECONSOLETRACING 0
  66. #define DEF_FILETRACINGMASK 0xffff0000
  67. #define DEF_CONSOLETRACINGMASK 0xffff0000
  68. #define DEF_MAXFILESIZE 0x100000
  69. #define DEF_FILEDIRECTORY TEXT("%windir%\\tracing")
  70. #define DEF_SCREENBUF_WIDTH 128
  71. #define DEF_SCREENBUF_HEIGHT 4000
  72. //max line length
  73. #define DEF_PRINT_BUFSIZE 5000
  74. #define BYTES_PER_DUMPLINE 16
  75. #define STR_DIRSEP TEXT("\\")
  76. #define STR_LOGEXT TEXT(".LOG")
  77. #define STR_OLDEXT TEXT(".OLD")
  78. //
  79. // read-write lock. writers dont get starved
  80. //
  81. typedef struct _READ_WRITE_LOCK {
  82. CRITICAL_SECTION RWL_ReadWriteBlock;
  83. LONG RWL_ReaderCount;
  84. HANDLE RWL_ReaderDoneEvent;
  85. } READ_WRITE_LOCK, *PREAD_WRITE_LOCK;
  86. //
  87. // structure describing each client.
  88. // a client struct should be locked for writing when
  89. // enabling or disabling it, and when loading its configuration
  90. // a client struct should be locked for reading on all other accesses
  91. //
  92. typedef struct _TRACE_CLIENT {
  93. READ_WRITE_LOCK ReadWriteLock;
  94. DWORD TC_Flags;
  95. DWORD TC_ClientID;
  96. CHAR TC_ClientNameA[MAX_CLIENTNAME_LENGTH];
  97. WCHAR TC_ClientNameW[MAX_CLIENTNAME_LENGTH];
  98. #ifdef UNICODE
  99. #define TC_ClientName TC_ClientNameW
  100. #else
  101. #define TC_ClientName TC_ClientNameA
  102. #endif
  103. HANDLE TC_File;
  104. HANDLE TC_Console;
  105. DWORD TC_FileMask;
  106. DWORD TC_ConsoleMask;
  107. DWORD TC_MaxFileSize;
  108. CHAR TC_FileDirA[MAX_PATH+1];
  109. WCHAR TC_FileDirW[MAX_PATH+1];
  110. #ifdef UNICODE
  111. #define TC_FileDir TC_FileDirW
  112. #else
  113. #define TC_FileDir TC_FileDirA
  114. #endif
  115. HKEY TC_ConfigKey;
  116. HANDLE TC_ConfigEvent;
  117. } TRACE_CLIENT, *LPTRACE_CLIENT;
  118. //
  119. // structure describing each server.
  120. // a server struct must be locked for writing when adding
  121. // or removing a client to the client table, and when changing
  122. // the owner of the console
  123. // a server should be locked for reading on all other accesses
  124. //
  125. typedef struct _TRACE_SERVER {
  126. READ_WRITE_LOCK ReadWriteLock;
  127. DWORD TS_Flags;
  128. DWORD TS_ClientCount;
  129. DWORD TS_ConsoleOwner;
  130. DWORD TS_ConsoleCreated;
  131. HANDLE TS_Console;
  132. HANDLE TS_StopEvent;
  133. HANDLE TS_TableEvent;
  134. LIST_ENTRY TS_ClientEventsToClose; // clients put TC_ConfigEvent
  135. //in this list as they should not close the handle
  136. DWORD TS_FlagsCache[MAX_CLIENT_COUNT];
  137. LPTRACE_CLIENT TS_ClientTable[MAX_CLIENT_COUNT];
  138. } TRACE_SERVER, *LPTRACE_SERVER;
  139. #define GET_TRACE_SERVER() ( \
  140. (g_server!=NULL) ? g_server : TraceCreateServer(&g_server) \
  141. )
  142. #define GET_TRACE_SERVER_NO_INIT() (g_server)
  143. //
  144. // macros used to lock client and server structures
  145. //
  146. DWORD CreateReadWriteLock(PREAD_WRITE_LOCK pRWL);
  147. VOID DeleteReadWriteLock(PREAD_WRITE_LOCK pRWL);
  148. VOID AcquireReadLock(PREAD_WRITE_LOCK pRWL);
  149. VOID ReleaseReadLock(PREAD_WRITE_LOCK pRWL);
  150. VOID AcquireWriteLock(PREAD_WRITE_LOCK pRWL);
  151. VOID ReleaseWriteLock(PREAD_WRITE_LOCK pRWL);
  152. #define TRACE_STARTUP_LOCKING(ob) \
  153. CreateReadWriteLock(&(ob)->ReadWriteLock)
  154. #define TRACE_CLEANUP_LOCKING(ob) { \
  155. if ((ob)->ReadWriteLock.RWL_ReaderDoneEvent != NULL) \
  156. DeleteReadWriteLock(&(ob)->ReadWriteLock);}
  157. #define TRACE_ACQUIRE_READLOCK(ob) \
  158. AcquireReadLock(&(ob)->ReadWriteLock)
  159. #define TRACE_RELEASE_READLOCK(ob) \
  160. ReleaseReadLock(&(ob)->ReadWriteLock)
  161. #define TRACE_ACQUIRE_WRITELOCK(ob) \
  162. AcquireWriteLock(&(ob)->ReadWriteLock)
  163. #define TRACE_RELEASE_WRITELOCK(ob) \
  164. ReleaseWriteLock(&(ob)->ReadWriteLock)
  165. #define TRACE_READ_TO_WRITELOCK(ob) \
  166. (TRACE_RELEASE_READLOCK(ob),TRACE_ACQUIRE_WRITELOCK(ob))
  167. #define TRACE_WRITE_TO_READLOCK(ob) \
  168. (TRACE_RELEASE_WRITELOCK(ob),TRACE_ACQUIRE_READLOCK(ob))
  169. //
  170. // macros used to interpret client flags
  171. //
  172. #define TRACE_CLIENT_IS_DISABLED(c) \
  173. ((c)->TC_Flags & TRACEFLAGS_DISABLED)
  174. #define TRACE_CLIENT_USES_FILE(c) \
  175. ((c)->TC_Flags & TRACEFLAGS_USEFILE)
  176. #define TRACE_CLIENT_USES_CONSOLE(c) \
  177. ((c)->TC_Flags & TRACEFLAGS_USECONSOLE)
  178. #define TRACE_CLIENT_USES_REGISTRY(c) \
  179. ((c)->TC_Flags & TRACEFLAGS_REGCONFIG)
  180. #define TRACE_CLIENT_USES_UNICODE(c) \
  181. ((c)->TC_Flags & TRACEFLAGS_UNICODE)
  182. // macro used to create server thread if required
  183. DWORD
  184. TraceCreateServerThread (
  185. DWORD Flags,
  186. BOOL bHaveLock,
  187. BOOL bTraceRegister
  188. );
  189. #define CREATE_SERVER_THREAD_IF_REQUIRED() {\
  190. if (!g_serverThread) TraceCreateServerThread(0, FALSE,FALSE);}
  191. //
  192. // code-page independent function declarations
  193. //
  194. LPTRACE_SERVER
  195. TraceCreateServer(
  196. LPTRACE_SERVER *lpserver
  197. );
  198. BOOL
  199. TraceShutdownServer(
  200. LPTRACE_SERVER lpserver
  201. );
  202. DWORD
  203. TraceCleanUpServer(
  204. LPTRACE_SERVER lpserver
  205. );
  206. DWORD
  207. TraceServerThread(
  208. LPVOID lpvParam
  209. );
  210. DWORD
  211. TraceCreateServerComplete(
  212. LPTRACE_SERVER lpserver
  213. );
  214. DWORD
  215. TraceProcessConsoleInput(
  216. LPTRACE_SERVER lpserver
  217. );
  218. DWORD
  219. TraceShiftConsoleWindow(
  220. LPTRACE_CLIENT lpclient,
  221. INT iXShift,
  222. INT iYShift,
  223. PCONSOLE_SCREEN_BUFFER_INFO pcsbi
  224. );
  225. DWORD
  226. TraceUpdateConsoleOwner(
  227. LPTRACE_SERVER lpserver,
  228. INT dir
  229. );
  230. VOID
  231. SetWaitArray(
  232. LPTRACE_SERVER lpserver
  233. );
  234. //
  235. // code-page dependent function declarations
  236. //
  237. // ANSI declarations
  238. //
  239. LPTRACE_CLIENT
  240. TraceFindClientA(
  241. LPTRACE_SERVER lpserver,
  242. LPCSTR lpszClient
  243. );
  244. DWORD
  245. TraceCreateClientA(
  246. LPTRACE_CLIENT *lplpentry
  247. );
  248. DWORD
  249. TraceDeleteClientA(
  250. LPTRACE_SERVER lpserver,
  251. LPTRACE_CLIENT *lplpentry
  252. );
  253. DWORD
  254. TraceEnableClientA(
  255. LPTRACE_SERVER lpserver,
  256. LPTRACE_CLIENT lpclient,
  257. BOOL bFirstTime
  258. );
  259. DWORD
  260. TraceDisableClientA(
  261. LPTRACE_SERVER lpserver,
  262. LPTRACE_CLIENT lpclient
  263. );
  264. DWORD
  265. TraceRegConfigClientA(
  266. LPTRACE_CLIENT lpclient,
  267. BOOL bFirstTime
  268. );
  269. DWORD
  270. TraceRegCreateDefaultsA(
  271. LPCSTR lpszTracing,
  272. PHKEY phkeyTracing
  273. );
  274. DWORD
  275. TraceOpenClientConsoleA(
  276. LPTRACE_SERVER lpserver,
  277. LPTRACE_CLIENT lpclient
  278. );
  279. DWORD
  280. TraceCloseClientConsoleA(
  281. LPTRACE_SERVER lpserver,
  282. LPTRACE_CLIENT lpclient
  283. );
  284. DWORD
  285. TraceCreateClientFileA(
  286. LPTRACE_CLIENT lpclient
  287. );
  288. DWORD
  289. TraceMoveClientFileA(
  290. LPTRACE_CLIENT lpclient
  291. );
  292. DWORD
  293. TraceCloseClientFileA(
  294. LPTRACE_CLIENT lpclient
  295. );
  296. DWORD
  297. TraceWriteOutputA(
  298. LPTRACE_SERVER lpserver,
  299. LPTRACE_CLIENT lpclient,
  300. DWORD dwFlags,
  301. LPCSTR lpszOutput
  302. );
  303. DWORD
  304. TraceUpdateConsoleTitleA(
  305. LPTRACE_CLIENT lpclient
  306. );
  307. DWORD
  308. TraceDumpLineA(
  309. LPTRACE_SERVER lpserver,
  310. LPTRACE_CLIENT lpclient,
  311. DWORD dwFlags,
  312. LPBYTE lpbBytes,
  313. DWORD dwLine,
  314. DWORD dwGroup,
  315. BOOL bPrefixAddr,
  316. LPBYTE lpbPrefix,
  317. LPCSTR lpszPrefix
  318. );
  319. DWORD
  320. TraceVprintfInternalA(
  321. DWORD dwTraceID,
  322. DWORD dwFlags,
  323. LPCSTR lpszFormat,
  324. va_list arglist
  325. );
  326. //
  327. // Unicode declarations
  328. //
  329. LPTRACE_CLIENT
  330. TraceFindClientW(
  331. LPTRACE_SERVER lpserver,
  332. LPCWSTR lpszClient
  333. );
  334. DWORD
  335. TraceCreateClientW(
  336. LPTRACE_CLIENT *lplpentry
  337. );
  338. DWORD
  339. TraceDeleteClientW(
  340. LPTRACE_SERVER lpserver,
  341. LPTRACE_CLIENT *lplpentry
  342. );
  343. DWORD
  344. TraceEnableClientW(
  345. LPTRACE_SERVER lpserver,
  346. LPTRACE_CLIENT lpclient,
  347. BOOL bFirstTime
  348. );
  349. DWORD
  350. TraceDisableClientW(
  351. LPTRACE_SERVER lpserver,
  352. LPTRACE_CLIENT lpclient
  353. );
  354. DWORD
  355. TraceRegConfigClientW(
  356. LPTRACE_CLIENT lpclient,
  357. BOOL bFirstTime
  358. );
  359. DWORD
  360. TraceRegCreateDefaultsW(
  361. LPCWSTR lpszTracing,
  362. PHKEY phkeyTracing
  363. );
  364. DWORD
  365. TraceOpenClientConsoleW(
  366. LPTRACE_SERVER lpserver,
  367. LPTRACE_CLIENT lpclient
  368. );
  369. DWORD
  370. TraceCloseClientConsoleW(
  371. LPTRACE_SERVER lpserver,
  372. LPTRACE_CLIENT lpclient
  373. );
  374. DWORD
  375. TraceCreateClientFileW(
  376. LPTRACE_CLIENT lpclient
  377. );
  378. DWORD
  379. TraceMoveClientFileW(
  380. LPTRACE_CLIENT lpclient
  381. );
  382. DWORD
  383. TraceCloseClientFileW(
  384. LPTRACE_CLIENT lpclient
  385. );
  386. DWORD
  387. TraceWriteOutputW(
  388. LPTRACE_SERVER lpserver,
  389. LPTRACE_CLIENT lpclient,
  390. DWORD dwFlags,
  391. LPCWSTR lpszOutput
  392. );
  393. DWORD
  394. TraceUpdateConsoleTitleW(
  395. LPTRACE_CLIENT lpclient
  396. );
  397. DWORD
  398. TraceDumpLineW(
  399. LPTRACE_SERVER lpserver,
  400. LPTRACE_CLIENT lpclient,
  401. DWORD dwFlags,
  402. LPBYTE lpbBytes,
  403. DWORD dwLine,
  404. DWORD dwGroup,
  405. BOOL bPrefixAddr,
  406. LPBYTE lpbPrefix,
  407. LPCWSTR lpszPrefix
  408. );
  409. DWORD
  410. TraceVprintfInternalW(
  411. DWORD dwTraceID,
  412. DWORD dwFlags,
  413. LPCWSTR lpszFormat,
  414. va_list arglist
  415. );
  416. //
  417. // code-page independent macro definitions
  418. //
  419. #ifdef UNICODE
  420. #define TraceFindClient TraceFindClientW
  421. #define TraceCreateClient TraceCreateClientW
  422. #define TraceDeleteClient TraceDeleteClientW
  423. #define TraceEnableClient TraceEnableClientW
  424. #define TraceDisableClient TraceDisableClientW
  425. #define TraceRegConfigClient TraceRegConfigClientW
  426. #define TraceRegCreateDefaults TraceRegCreateDefaultsW
  427. #define TraceOpenClientConsole TraceOpenClientConsoleW
  428. #define TraceCloseClientConsole TraceCloseClientConsoleW
  429. #define TraceCreateClientFile TraceCreateClientFileW
  430. #define TraceMoveClientFile TraceMoveClientFileW
  431. #define TraceCloseClientFile TraceCloseClientFileW
  432. #define TraceWriteOutput TraceWriteOutputW
  433. #define TraceUpdateConsoleTitle TraceUpdateConsoleTitleW
  434. #define TraceDumpLine TraceDumpLineW
  435. #define TraceVprintfInternal TraceVprintfInternalW
  436. #else
  437. #define TraceFindClient TraceFindClientA
  438. #define TraceCreateClient TraceCreateClientA
  439. #define TraceDeleteClient TraceDeleteClientA
  440. #define TraceEnableClient TraceEnableClientA
  441. #define TraceDisableClient TraceDisableClientA
  442. #define TraceRegConfigClient TraceRegConfigClientA
  443. #define TraceRegCreateDefaults TraceRegCreateDefaultsA
  444. #define TraceOpenClientConsole TraceOpenClientConsoleA
  445. #define TraceCloseClientConsole TraceCloseClientConsoleA
  446. #define TraceCreateClientFile TraceCreateClientFileA
  447. #define TraceMoveClientFile TraceMoveClientFileA
  448. #define TraceCloseClientFile TraceCloseClientFileA
  449. #define TraceWriteOutput TraceWriteOutputA
  450. #define TraceUpdateConsoleTitle TraceUpdateConsoleTitleA
  451. #define TraceDumpLine TraceDumpLineA
  452. #define TraceVprintfInternal TraceVprintfInternalA
  453. #endif
  454. // global data declarations
  455. //
  456. extern LPTRACE_SERVER g_server;
  457. extern HANDLE g_serverThread;
  458. extern PTCHAR g_FormatBuffer;
  459. extern PTCHAR g_PrintBuffer;
  460. #endif // _TRACE_H_