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.

206 lines
3.5 KiB

  1. /*++
  2. Copyright (c) 1997 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 <nt.h>
  15. #include <ntrtl.h>
  16. #include <nturtl.h>
  17. #include <windows.h>
  18. #include <pudebug.h>
  19. #include <tracelog.h>
  20. #define ALLOC_MEM(cb) (PVOID)LocalAlloc( LPTR, (cb) )
  21. #define FREE_MEM(ptr) (VOID)LocalFree( (HLOCAL)(ptr) )
  22. PTRACE_LOG
  23. CreateTraceLog(
  24. IN LONG LogSize,
  25. IN LONG ExtraBytesInHeader,
  26. IN LONG EntrySize
  27. )
  28. /*++
  29. Routine Description:
  30. Creates a new (empty) trace log buffer.
  31. Arguments:
  32. LogSize - The number of entries in the log.
  33. ExtraBytesInHeader - The number of extra bytes to include in the
  34. log header. This is useful for adding application-specific
  35. data to the log.
  36. EntrySize - The size (in bytes) of each entry.
  37. Return Value:
  38. PTRACE_LOG - Pointer to the newly created log if successful,
  39. NULL otherwise.
  40. --*/
  41. {
  42. LONG totalSize;
  43. PTRACE_LOG log;
  44. //
  45. // Sanity check the parameters.
  46. //
  47. DBG_ASSERT( LogSize > 0 );
  48. DBG_ASSERT( EntrySize > 0 );
  49. DBG_ASSERT( ( EntrySize & 3 ) == 0 );
  50. //
  51. // Allocate & initialize the log structure.
  52. //
  53. totalSize = sizeof(*log) + ( LogSize * EntrySize ) + ExtraBytesInHeader;
  54. DBG_ASSERT( totalSize > 0 );
  55. log = (PTRACE_LOG)ALLOC_MEM( totalSize );
  56. //
  57. // Initialize it.
  58. //
  59. if( log != NULL ) {
  60. RtlZeroMemory( log, totalSize );
  61. log->Signature = TRACE_LOG_SIGNATURE;
  62. log->LogSize = LogSize;
  63. log->NextEntry = -1;
  64. log->EntrySize = EntrySize;
  65. log->LogBuffer = (PUCHAR)( log + 1 ) + ExtraBytesInHeader;
  66. }
  67. return log;
  68. } // CreateTraceLog
  69. VOID
  70. DestroyTraceLog(
  71. IN PTRACE_LOG Log
  72. )
  73. /*++
  74. Routine Description:
  75. Destroys a trace log buffer created with CreateTraceLog().
  76. Arguments:
  77. Log - The trace log buffer to destroy.
  78. Return Value:
  79. None.
  80. --*/
  81. {
  82. if ( Log != NULL ) {
  83. DBG_ASSERT( Log->Signature == TRACE_LOG_SIGNATURE );
  84. Log->Signature = TRACE_LOG_SIGNATURE_X;
  85. FREE_MEM( Log );
  86. }
  87. } // DestroyTraceLog
  88. LONG
  89. WriteTraceLog(
  90. IN PTRACE_LOG Log,
  91. IN PVOID Entry
  92. )
  93. /*++
  94. Routine Description:
  95. Writes a new entry to the specified trace log.
  96. Arguments:
  97. Log - The log to write to.
  98. Entry - Pointer to the data to write. This buffer is assumed to be
  99. Log->EntrySize bytes long.
  100. Return Value:
  101. Index of entry in log. This is useful for correlating the output
  102. of !inetdbg.ref to a particular point in the output debug stream
  103. --*/
  104. {
  105. PUCHAR target;
  106. LONG index;
  107. DBG_ASSERT( Log != NULL );
  108. DBG_ASSERT( Log->Signature == TRACE_LOG_SIGNATURE );
  109. DBG_ASSERT( Entry != NULL );
  110. //
  111. // Find the next slot, copy the entry to the slot.
  112. //
  113. index = InterlockedIncrement( &Log->NextEntry ) % Log->LogSize;
  114. target = Log->LogBuffer + ( index * Log->EntrySize );
  115. RtlCopyMemory(
  116. target,
  117. Entry,
  118. Log->EntrySize
  119. );
  120. return index;
  121. } // WriteTraceLog
  122. VOID
  123. ResetTraceLog(
  124. IN PTRACE_LOG Log
  125. )
  126. {
  127. DBG_ASSERT( Log != NULL );
  128. DBG_ASSERT( Log->Signature == TRACE_LOG_SIGNATURE );
  129. RtlZeroMemory(
  130. ( Log + 1 ),
  131. Log->LogSize * Log->EntrySize
  132. );
  133. Log->NextEntry = -1;
  134. } // ResetTraceLog