Source code of Windows XP (NT5)
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.

249 lines
4.2 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. tracelog.c
  5. Abstract:
  6. This module implements a trace log.
  7. A trace log is a fast, in-memory, thread safe activity log useful
  8. for debugging certain classes of problems. They are especially useful
  9. when debugging reference count bugs.
  10. Author:
  11. Keith Moore (keithmo) 30-Apr-1997
  12. Revision History:
  13. --*/
  14. #include "spudp.h"
  15. //
  16. // Private contants.
  17. //
  18. #define ALLOC_MEM(cb) \
  19. SPUD_ALLOCATE_POOL( \
  20. NonPagedPool, \
  21. (ULONG)(cb), \
  22. SPUD_TRACE_LOG_POOL_TAG \
  23. )
  24. #define FREE_MEM(ptr) \
  25. SPUD_FREE_POOL( \
  26. (PVOID)(ptr) \
  27. )
  28. #define DBG_ASSERT ASSERT
  29. #if 0
  30. NOT PAGEABLE -- CreateTraceLog
  31. NOT PAGEABLE -- DestroyTraceLog
  32. NOT PAGEABLE -- WriteTraceLog
  33. NOT PAGEABLE -- ResetTraceLog
  34. #endif
  35. //
  36. // Public functions.
  37. //
  38. PTRACE_LOG
  39. CreateTraceLog(
  40. IN LONG LogSize,
  41. IN LONG ExtraBytesInHeader,
  42. IN LONG EntrySize
  43. )
  44. /*++
  45. Routine Description:
  46. Creates a new (empty) trace log buffer.
  47. Arguments:
  48. LogSize - The number of entries in the log.
  49. ExtraBytesInHeader - The number of extra bytes to include in the
  50. log header. This is useful for adding application-specific
  51. data to the log.
  52. EntrySize - The size (in bytes) of each entry.
  53. Return Value:
  54. PTRACE_LOG - Pointer to the newly created log if successful,
  55. NULL otherwise.
  56. --*/
  57. {
  58. LONG totalSize;
  59. PTRACE_LOG log;
  60. //
  61. // Sanity check the parameters.
  62. //
  63. DBG_ASSERT( LogSize > 0 );
  64. DBG_ASSERT( EntrySize > 0 );
  65. DBG_ASSERT( ( EntrySize & 3 ) == 0 );
  66. //
  67. // Allocate & initialize the log structure.
  68. //
  69. totalSize = sizeof(*log) + ( LogSize * EntrySize ) + ExtraBytesInHeader;
  70. DBG_ASSERT( totalSize > 0 );
  71. log = (PTRACE_LOG)ALLOC_MEM( totalSize );
  72. //
  73. // Initialize it.
  74. //
  75. if( log != NULL ) {
  76. RtlZeroMemory( log, totalSize );
  77. log->Signature = TRACE_LOG_SIGNATURE;
  78. log->LogSize = LogSize;
  79. log->NextEntry = -1;
  80. log->EntrySize = EntrySize;
  81. log->LogBuffer = (PUCHAR)( log + 1 ) + ExtraBytesInHeader;
  82. }
  83. return log;
  84. } // CreateTraceLog
  85. VOID
  86. DestroyTraceLog(
  87. IN PTRACE_LOG Log
  88. )
  89. /*++
  90. Routine Description:
  91. Destroys a trace log buffer created with CreateTraceLog().
  92. Arguments:
  93. Log - The trace log buffer to destroy.
  94. Return Value:
  95. None.
  96. --*/
  97. {
  98. DBG_ASSERT( Log != NULL );
  99. DBG_ASSERT( Log->Signature == TRACE_LOG_SIGNATURE );
  100. Log->Signature = TRACE_LOG_SIGNATURE_X;
  101. FREE_MEM( Log );
  102. } // DestroyTraceLog
  103. VOID
  104. WriteTraceLog(
  105. IN PTRACE_LOG Log,
  106. IN PVOID Entry
  107. )
  108. /*++
  109. Routine Description:
  110. Writes a new entry to the specified trace log.
  111. Arguments:
  112. Log - The log to write to.
  113. Entry - Pointer to the data to write. This buffer is assumed to be
  114. Log->EntrySize bytes long.
  115. Return Value:
  116. None
  117. --*/
  118. {
  119. PUCHAR target;
  120. LONG index;
  121. DBG_ASSERT( Log != NULL );
  122. DBG_ASSERT( Log->Signature == TRACE_LOG_SIGNATURE );
  123. DBG_ASSERT( Entry != NULL );
  124. //
  125. // Find the next slot, copy the entry to the slot.
  126. //
  127. index = InterlockedIncrement( &Log->NextEntry ) % Log->LogSize;
  128. target = Log->LogBuffer + ( index * Log->EntrySize );
  129. RtlCopyMemory(
  130. target,
  131. Entry,
  132. Log->EntrySize
  133. );
  134. } // WriteTraceLog
  135. VOID
  136. ResetTraceLog(
  137. IN PTRACE_LOG Log
  138. )
  139. /*++
  140. Routine Description:
  141. Resets the specified trace log.
  142. Arguments:
  143. Log - The log to reset.
  144. Return Value:
  145. None
  146. --*/
  147. {
  148. DBG_ASSERT( Log != NULL );
  149. DBG_ASSERT( Log->Signature == TRACE_LOG_SIGNATURE );
  150. RtlZeroMemory(
  151. ( Log + 1 ),
  152. Log->LogSize * Log->EntrySize
  153. );
  154. Log->NextEntry = -1;
  155. } // ResetTraceLog