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.

294 lines
6.2 KiB

  1. #if DBG
  2. #include <irda.h>
  3. #include <dbgmsg.h>
  4. #include <stdio.h>
  5. #include <stdarg.h>
  6. #include <string.h>
  7. CHAR DbgMsgs[DBG_MSG_CNT][MAX_MSG_LEN];
  8. UINT First, Last;
  9. CTETimer DbgTimer;
  10. BOOLEAN TimerRunning;
  11. CTELock DbgLock;
  12. PIRP pDbgIrp;
  13. UCHAR *IrpBuf;
  14. ULONG IrpBufLen;
  15. ULONG IrpBufWritten;
  16. VOID DbgTimerExp(CTEEvent *Event, void *Arg);
  17. VOID
  18. DbgMsgInit()
  19. {
  20. pDbgIrp = NULL;
  21. First = 0;
  22. Last = 0;
  23. TimerRunning = FALSE;
  24. CTEInitLock(&DbgLock);
  25. CTEInitTimer(&DbgTimer);
  26. }
  27. VOID
  28. DbgMsgUninit()
  29. {
  30. CTELockHandle LockHandle;
  31. KIRQL Irql;
  32. CTEGetLock(&DbgLock, &LockHandle);
  33. if (pDbgIrp)
  34. {
  35. IoAcquireCancelSpinLock(&Irql);
  36. IoSetCancelRoutine(pDbgIrp, NULL);
  37. IoReleaseCancelSpinLock(Irql);
  38. pDbgIrp->IoStatus.Information = 0;
  39. pDbgIrp->IoStatus.Status = STATUS_UNSUCCESSFUL;
  40. DbgPrint("Complete irp!\n");
  41. IoCompleteRequest(pDbgIrp, IO_NO_INCREMENT);
  42. pDbgIrp = NULL;
  43. }
  44. CTEFreeLock(&DbgLock, LockHandle);
  45. }
  46. VOID
  47. DbgMsg(CHAR *Format, ...)
  48. {
  49. va_list Args;
  50. CTELockHandle LockHandle;
  51. CHAR Temp[MAX_MSG_LEN];
  52. LARGE_INTEGER Time;
  53. ULONG UlongTime;
  54. KeQueryTickCount(&Time);
  55. //
  56. // change it milliseconds and stuff it in a dword
  57. //
  58. UlongTime=(ULONG)((Time.QuadPart * KeQueryTimeIncrement()) / 10000);
  59. sprintf(Temp,"%6d.%03d - ",UlongTime/1000, UlongTime%1000);
  60. va_start(Args, Format);
  61. vsprintf(&Temp[strlen(Temp)], Format, Args);
  62. if (DbgOutput & DBG_OUTPUT_DEBUGGER)
  63. {
  64. DbgPrint(Temp);
  65. }
  66. if (DbgOutput & DBG_OUTPUT_BUFFER)
  67. {
  68. CTEGetLock(&DbgLock, &LockHandle);
  69. strcpy(DbgMsgs[Last], Temp);
  70. Last++;
  71. if (Last == DBG_MSG_CNT)
  72. Last = 0;
  73. if (First == Last)
  74. {
  75. First++;
  76. if (First == DBG_MSG_CNT)
  77. First = 0;
  78. }
  79. if (pDbgIrp && !TimerRunning)
  80. {
  81. CTEStartTimer(&DbgTimer, DBG_TIMER_INTERVAL,
  82. DbgTimerExp, NULL);
  83. TimerRunning = TRUE;
  84. }
  85. CTEFreeLock(&DbgLock, LockHandle);
  86. }
  87. va_end(Args);
  88. }
  89. NTSTATUS
  90. FillDbgIrp(UCHAR Msg[])
  91. {
  92. NTSTATUS Status = STATUS_PENDING;
  93. UINT i;
  94. if ((IrpBufLen - IrpBufWritten) < MAX_MSG_LEN)
  95. {
  96. Status = STATUS_SUCCESS;
  97. }
  98. else
  99. {
  100. Msg[MAX_MSG_LEN - 1] = 0; // just to be sure
  101. i = 0;
  102. while (1)
  103. {
  104. IrpBuf[IrpBufWritten++] = Msg[i];
  105. if (Msg[i] == 0)
  106. break;
  107. i++;
  108. }
  109. }
  110. return Status;
  111. }
  112. VOID CancelDbgIrp(
  113. PDEVICE_OBJECT DeviceObject,
  114. PIRP pIrp)
  115. {
  116. // DbgPrint("CancelDbgIrp %x\n", pIrp);
  117. pDbgIrp = NULL;
  118. IoReleaseCancelSpinLock(pIrp->CancelIrql);
  119. pIrp->IoStatus.Status = STATUS_CANCELLED;
  120. pIrp->IoStatus.Information = 0;
  121. IoCompleteRequest(pIrp, IO_NO_INCREMENT);
  122. }
  123. NTSTATUS
  124. DbgMsgIrp(
  125. PIRP pIrp,
  126. PIO_STACK_LOCATION pIrpSp)
  127. {
  128. CTELockHandle LockHandle;
  129. NTSTATUS Status = STATUS_PENDING;
  130. if (pDbgIrp != NULL)
  131. return STATUS_DEVICE_BUSY;
  132. CTEGetLock(&DbgLock, &LockHandle);
  133. IrpBufLen = pIrpSp->Parameters.DeviceIoControl.OutputBufferLength;
  134. IrpBufWritten = 0;
  135. if (IrpBufLen < MAX_MSG_LEN)
  136. {
  137. CTEFreeLock(&DbgLock, LockHandle);
  138. return STATUS_BUFFER_OVERFLOW;
  139. }
  140. IrpBuf = pIrp->AssociatedIrp.SystemBuffer;
  141. while (First != Last)
  142. {
  143. Status = FillDbgIrp(DbgMsgs[First]);
  144. if (Status == STATUS_SUCCESS)
  145. break;
  146. First++;
  147. if (First == DBG_MSG_CNT)
  148. First = 0;
  149. }
  150. if (Status == STATUS_SUCCESS)
  151. {
  152. pIrp->IoStatus.Information = IrpBufWritten;
  153. }
  154. else if (Status == STATUS_PENDING)
  155. {
  156. KIRQL Irql;
  157. PDRIVER_CANCEL PrevCancel;
  158. pDbgIrp = pIrp;
  159. IoMarkIrpPending(pIrp);
  160. IoAcquireCancelSpinLock(&Irql);
  161. PrevCancel = IoSetCancelRoutine(pIrp, CancelDbgIrp);
  162. CTEAssert(PrevCancel == NULL);
  163. IoReleaseCancelSpinLock(Irql);
  164. if (IrpBufWritten != 0)
  165. {
  166. CTEStartTimer(&DbgTimer, DBG_TIMER_INTERVAL,
  167. DbgTimerExp, NULL);
  168. TimerRunning = TRUE;
  169. }
  170. }
  171. CTEFreeLock(&DbgLock, LockHandle);
  172. //DbgPrint("DbgIrp status %x, bw %d, irp %x\n", Status, IrpBufWritten, pIrp);
  173. return Status;
  174. }
  175. VOID
  176. DbgTimerExp(CTEEvent *Event, void *Arg)
  177. {
  178. CTELockHandle LockHandle;
  179. PIRP pIrp;
  180. KIRQL Irql;
  181. //DbgPrint("Texp\n");
  182. if (pDbgIrp == NULL)
  183. {
  184. DbgPrint("DbgIrp is null\n");
  185. return;
  186. }
  187. IoAcquireCancelSpinLock(&Irql);
  188. IoSetCancelRoutine(pDbgIrp, NULL);
  189. IoReleaseCancelSpinLock(Irql);
  190. if (pDbgIrp->Cancel)
  191. {
  192. DbgPrint("DbgIrp is being canceled\n");
  193. pDbgIrp = NULL;
  194. return;
  195. }
  196. CTEGetLock(&DbgLock, &LockHandle);
  197. TimerRunning = FALSE;
  198. while (First != Last)
  199. {
  200. if (FillDbgIrp(DbgMsgs[First]) == STATUS_SUCCESS)
  201. break;
  202. First++;
  203. if (First == DBG_MSG_CNT)
  204. First = 0;
  205. }
  206. pIrp = pDbgIrp;
  207. pDbgIrp = NULL;
  208. CTEFreeLock(&DbgLock, LockHandle);
  209. pIrp->IoStatus.Information = IrpBufWritten;
  210. pIrp->IoStatus.Status = STATUS_SUCCESS;
  211. // DbgPrint("Comp bw %d, irp %x\n", IrpBufWritten, pIrp);
  212. IoCompleteRequest(pIrp, IO_NO_INCREMENT);
  213. }
  214. #endif