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.

185 lines
4.4 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. logging.c
  5. Abstract:
  6. This module contains routines for trace logging.
  7. Author:
  8. Stephen Hsiao (shsiao) 01-Jan-2000
  9. Revision History:
  10. --*/
  11. #include "perfp.h"
  12. #ifndef NTPERF
  13. #ifdef ALLOC_PRAGMA
  14. #pragma alloc_text(PAGEWMI, PerfInfoReserveBytes)
  15. #pragma alloc_text(PAGEWMI, PerfInfoLogBytes)
  16. #endif //ALLOC_PRAGMA
  17. #endif // !NTPERF
  18. NTSTATUS
  19. PerfInfoReserveBytes(
  20. PPERFINFO_HOOK_HANDLE Hook,
  21. USHORT HookId,
  22. ULONG BytesToReserve
  23. )
  24. /*++
  25. Routine Description:
  26. Reserves memory for the hook via WMI and initializes the header.
  27. Arguments:
  28. Hook - pointer to hook handle (used for reference decrement)
  29. HookId - Id for the hook
  30. BytesToLog - size of data in bytes
  31. Return Value:
  32. STATUS_SUCCESS on success
  33. STATUS_UNSUCCESSFUL if the buffer memory couldn't be allocated.
  34. --*/
  35. {
  36. NTSTATUS Status = STATUS_UNSUCCESSFUL;
  37. PPERFINFO_TRACE_HEADER PPerfTraceHeader = NULL;
  38. PWMI_BUFFER_HEADER PWmiBufferHeader = NULL;
  39. PERF_ASSERT((BytesToReserve + FIELD_OFFSET(PERFINFO_TRACE_HEADER, Data)) <= MAXUSHORT);
  40. PERF_ASSERT(Hook != NULL);
  41. PPerfTraceHeader = WmiReserveWithPerfHeader(BytesToReserve, &PWmiBufferHeader);
  42. if (PPerfTraceHeader != NULL) {
  43. PPerfTraceHeader->Packet.HookId = HookId;
  44. Hook->PerfTraceHeader = PPerfTraceHeader;
  45. Hook->WmiBufferHeader = PWmiBufferHeader;
  46. Status = STATUS_SUCCESS;
  47. } else {
  48. *Hook = PERF_NULL_HOOK_HANDLE;
  49. }
  50. return Status;
  51. }
  52. NTSTATUS
  53. PerfInfoLogBytes(
  54. USHORT HookId,
  55. PVOID Data,
  56. ULONG BytesToLog
  57. )
  58. /*++
  59. Routine Description:
  60. Reserves memory for the hook, copies the data, and unref's the hook entry.
  61. Arguments:
  62. HookId - Id for the hook
  63. Data - pointer to the data to be logged
  64. BytesToLog - size of data in bytes
  65. Return Value:
  66. STATUS_SUCCESS on success
  67. --*/
  68. {
  69. PERFINFO_HOOK_HANDLE Hook;
  70. NTSTATUS Status;
  71. Status = PerfInfoReserveBytes(&Hook, HookId, BytesToLog);
  72. if (!NT_SUCCESS(Status)) {
  73. return Status;
  74. }
  75. RtlCopyMemory(PERFINFO_HOOK_HANDLE_TO_DATA(Hook, PPERF_BYTE), Data, BytesToLog);
  76. PERF_FINISH_HOOK(Hook);
  77. return STATUS_SUCCESS;
  78. }
  79. #ifdef NTPERF
  80. PVOID
  81. FASTCALL
  82. PerfInfoReserveBytesFromPerfMem(
  83. ULONG BytesToReserve
  84. )
  85. /*++
  86. Routine Description:
  87. Reserves memory for the hook from the buffer, initializes the header,
  88. and the hook handle.
  89. Arguments:
  90. Hook - pointer to hook handle (used for reference decrement)
  91. HookId - Id for the hook
  92. BytesToLog - size of data in bytes
  93. Return Value:
  94. STATUS_SUCCESS on success
  95. STATUS_UNSUCCESSFUL if the buffer memory couldn't be allocated.
  96. --*/
  97. {
  98. PPERFINFO_TRACEBUF_HEADER pPerfBufHdr;
  99. PPERF_BYTE CurrentPtr;
  100. PPERF_BYTE NewPtr;
  101. PPERF_BYTE OriginalPtr;
  102. BOOLEAN Done = FALSE;
  103. ULONG AlignedTotBytes;
  104. pPerfBufHdr = PerfBufHdr();
  105. AlignedTotBytes = ALIGN_TO_POWER2(BytesToReserve, DEFAULT_TRACE_ALIGNMENT);
  106. OriginalPtr = pPerfBufHdr->Current.Ptr;
  107. while (!Done) {
  108. NewPtr = OriginalPtr + AlignedTotBytes;
  109. if (NewPtr <= pPerfBufHdr->Max.Ptr) {
  110. //
  111. // If the buffer pointer has not changed, returned value will be == to the comparand,
  112. // OriginalPointer, and the Destenation will be updated with the new end of buffer.
  113. //
  114. // If it did change, the Destination will not change and the a new end of buffer will
  115. // be returned. We loop until we get it in or the buffer is full.
  116. //
  117. CurrentPtr = (PPERF_BYTE) InterlockedCompareExchangePointer(
  118. (PVOID *)&(pPerfBufHdr->Current.Ptr),
  119. (PVOID)NewPtr,
  120. (PVOID)OriginalPtr
  121. );
  122. if (OriginalPtr == CurrentPtr) {
  123. Done = TRUE;
  124. } else {
  125. OriginalPtr = CurrentPtr;
  126. }
  127. } else {
  128. //
  129. // Buffer overflow
  130. //
  131. Done = TRUE;
  132. CurrentPtr = NULL;
  133. }
  134. }
  135. return CurrentPtr;
  136. }
  137. #endif //NTPERF