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.

269 lines
6.4 KiB

  1. /*****************************************************************************
  2. *
  3. * Copyright (c) 1998-1999 Microsoft Corporation
  4. *
  5. * DEBUG.C - debugging functions, etc.
  6. *
  7. * Author: Stan Adermann (stana)
  8. *
  9. * Created: 9/2/1998
  10. *
  11. *****************************************************************************/
  12. #include "raspptp.h"
  13. #if DBG
  14. PPPTP_ADAPTER gAdapter;
  15. #endif
  16. #if MEM_CHECKING
  17. typedef struct MEM_HDR {
  18. LIST_ENTRY ListEntry;
  19. ULONG Size;
  20. CHAR File[16];
  21. ULONG Line;
  22. } MEM_HDR;
  23. LIST_ENTRY leAlloc;
  24. NDIS_SPIN_LOCK slAlloc;
  25. VOID InitMemory()
  26. {
  27. NdisAllocateSpinLock(&slAlloc);
  28. NdisInitializeListHead(&leAlloc);
  29. }
  30. VOID DeinitMemory()
  31. {
  32. PLIST_ENTRY ListEntry;
  33. NdisAcquireSpinLock(&slAlloc);
  34. for (ListEntry=leAlloc.Flink;
  35. ListEntry!=&leAlloc;
  36. ListEntry = ListEntry->Flink)
  37. {
  38. MEM_HDR *Hdr = (MEM_HDR*)ListEntry;
  39. DEBUGMSG(DBG_ERROR, (DTEXT("PPTP Unfreed Memory:0x%08X size:%X <%s:%d>\n"),
  40. &Hdr[1], Hdr->Size, Hdr->File, Hdr->Line));
  41. }
  42. NdisReleaseSpinLock(&slAlloc);
  43. NdisFreeSpinLock(&slAlloc);
  44. }
  45. #endif
  46. #if MEM_CHECKING
  47. PVOID
  48. _MyMemAlloc(UINT size, ULONG tag, PUCHAR file, UINT line)
  49. #else
  50. PVOID
  51. MyMemAlloc(UINT size, ULONG tag)
  52. #endif
  53. {
  54. PVOID memptr;
  55. NDIS_STATUS status;
  56. #if MEM_CHECKING
  57. status = NdisAllocateMemoryWithTag(&memptr, size+sizeof(MEM_HDR), tag);
  58. #else
  59. status = NdisAllocateMemoryWithTag(&memptr, size, tag);
  60. #endif
  61. if (status != NDIS_STATUS_SUCCESS)
  62. {
  63. DEBUGMSG(DBG_ERROR, (DTEXT("Memory allocation failed <%s:%d>.\n"),
  64. file, line));
  65. memptr = NULL;
  66. }
  67. #if MEM_CHECKING
  68. else
  69. {
  70. MEM_HDR *Hdr = memptr;
  71. UINT FileNameLen = strlen(file);
  72. Hdr->Size = size;
  73. Hdr->Line = line;
  74. if (FileNameLen>sizeof(Hdr->File)-1)
  75. strcpy(Hdr->File, &file[FileNameLen-sizeof(Hdr->File)+1]);
  76. else
  77. strcpy(Hdr->File, file);
  78. MyInterlockedInsertHeadList(&leAlloc, &Hdr->ListEntry, &slAlloc);
  79. memptr = &Hdr[1];
  80. }
  81. #endif
  82. return memptr;
  83. }
  84. VOID
  85. MyMemFree(
  86. PVOID memptr,
  87. UINT size
  88. )
  89. {
  90. #if MEM_CHECKING
  91. PLIST_ENTRY ListEntry;
  92. MEM_HDR *Hdr = (MEM_HDR*)((PUCHAR)memptr-sizeof(MEM_HDR));
  93. NdisAcquireSpinLock(&slAlloc);
  94. for (ListEntry = leAlloc.Flink;
  95. ListEntry != &leAlloc;
  96. ListEntry = ListEntry->Flink)
  97. {
  98. if (ListEntry==&Hdr->ListEntry)
  99. {
  100. RemoveEntryList(&Hdr->ListEntry);
  101. break;
  102. }
  103. }
  104. if (ListEntry==&leAlloc)
  105. {
  106. DEBUGMSG(DBG_ERROR, (DTEXT("PPTP: Freeing memory not owned %x\n"), memptr));
  107. }
  108. NdisReleaseSpinLock(&slAlloc);
  109. NdisFreeMemory(Hdr, size+sizeof(MEM_HDR), 0);
  110. #else
  111. NdisFreeMemory(memptr, size, 0);
  112. #endif
  113. }
  114. #if LIST_CHECKING
  115. VOID FASTCALL CheckList(PLIST_ENTRY ListHead)
  116. {
  117. PLIST_ENTRY ListEntry, PrevListEntry;
  118. if (ListHead->Flink==ListHead)
  119. {
  120. if (ListHead->Blink!=ListHead)
  121. {
  122. DEBUGMSG(DBG_ERROR|DBG_BREAK,(DTEXT("PPTP: Corrupt list head:%x Flink:%x Blink:%x\n"), ListHead, ListHead->Flink, ListHead->Blink));
  123. }
  124. }
  125. else
  126. {
  127. ListEntry = ListHead;
  128. do
  129. {
  130. PrevListEntry = ListEntry;
  131. ListEntry = ListEntry->Flink;
  132. if (ListEntry->Blink!=PrevListEntry)
  133. {
  134. DEBUGMSG(DBG_ERROR|DBG_BREAK, (DTEXT("PPTP: Corrupt LIST_ENTRY Head:%08x %08x->Flink==%08x %08x->Blink==%08x\n"),
  135. ListHead, PrevListEntry, PrevListEntry->Flink, ListEntry, ListEntry->Blink));
  136. }
  137. } while (ListEntry!=ListHead);
  138. }
  139. }
  140. PLIST_ENTRY FASTCALL MyInterlockedInsertHeadList(PLIST_ENTRY Head, PLIST_ENTRY Entry, PNDIS_SPIN_LOCK SpinLock)
  141. {
  142. PLIST_ENTRY RetVal;
  143. NdisAcquireSpinLock(SpinLock);
  144. if (IsListEmpty(Head))
  145. RetVal = NULL;
  146. else
  147. RetVal = Head->Flink;
  148. CheckedInsertHeadList(Head, Entry);
  149. NdisReleaseSpinLock(SpinLock);
  150. return RetVal;
  151. }
  152. PLIST_ENTRY FASTCALL MyInterlockedInsertTailList(PLIST_ENTRY Head, PLIST_ENTRY Entry, PNDIS_SPIN_LOCK SpinLock)
  153. {
  154. PLIST_ENTRY RetVal;
  155. NdisAcquireSpinLock(SpinLock);
  156. if (IsListEmpty(Head))
  157. RetVal = NULL;
  158. else
  159. RetVal = Head->Blink;
  160. CheckedInsertTailList(Head, Entry);
  161. NdisReleaseSpinLock(SpinLock);
  162. return RetVal;
  163. }
  164. PLIST_ENTRY FASTCALL MyInterlockedRemoveHeadList(PLIST_ENTRY Head, PNDIS_SPIN_LOCK SpinLock)
  165. {
  166. PLIST_ENTRY RetVal;
  167. NdisAcquireSpinLock(SpinLock);
  168. //RemoveHeadList uses RemoveEntryList, which is redefined to call CheckList in DEBUG.H
  169. RetVal = RemoveHeadList(Head);
  170. if (RetVal==Head)
  171. RetVal = NULL;
  172. else
  173. RetVal->Flink = RetVal->Blink = NULL;
  174. NdisReleaseSpinLock(SpinLock);
  175. return RetVal;
  176. }
  177. #endif
  178. #define CASE_RETURN_NAME(x) case x: return DTEXT(#x)
  179. #ifdef DBG
  180. char *ControlStateToString(ULONG State)
  181. {
  182. switch( State ){
  183. CASE_RETURN_NAME(STATE_CTL_INVALID);
  184. CASE_RETURN_NAME(STATE_CTL_LISTEN);
  185. CASE_RETURN_NAME(STATE_CTL_DIALING);
  186. CASE_RETURN_NAME(STATE_CTL_WAIT_REQUEST);
  187. CASE_RETURN_NAME(STATE_CTL_WAIT_REPLY);
  188. CASE_RETURN_NAME(STATE_CTL_ESTABLISHED);
  189. CASE_RETURN_NAME(STATE_CTL_WAIT_STOP);
  190. CASE_RETURN_NAME(STATE_CTL_CLEANUP);
  191. default:
  192. return DTEXT("UNKNOWN CONTROL STATE");
  193. }
  194. }
  195. char *CallStateToString(ULONG State)
  196. {
  197. switch( State ){
  198. CASE_RETURN_NAME(STATE_CALL_INVALID);
  199. CASE_RETURN_NAME(STATE_CALL_CLOSED);
  200. CASE_RETURN_NAME(STATE_CALL_IDLE);
  201. CASE_RETURN_NAME(STATE_CALL_OFFHOOK);
  202. CASE_RETURN_NAME(STATE_CALL_OFFERING);
  203. CASE_RETURN_NAME(STATE_CALL_PAC_OFFERING);
  204. CASE_RETURN_NAME(STATE_CALL_PAC_WAIT);
  205. CASE_RETURN_NAME(STATE_CALL_DIALING);
  206. CASE_RETURN_NAME(STATE_CALL_PROCEEDING);
  207. CASE_RETURN_NAME(STATE_CALL_ESTABLISHED);
  208. CASE_RETURN_NAME(STATE_CALL_WAIT_DISCONNECT);
  209. CASE_RETURN_NAME(STATE_CALL_CLEANUP);
  210. default:
  211. return DTEXT("UNKNOWN CALL STATE");
  212. }
  213. }
  214. #endif
  215. #if LOCK_CHECKING
  216. VOID FASTCALL
  217. _MyAcquireSpinLock(
  218. PMY_SPIN_LOCK pLock,
  219. PUCHAR file,
  220. UINT line
  221. )
  222. {
  223. UINT FileNameLen = strlen(file);
  224. NdisAcquireSpinLock((PNDIS_SPIN_LOCK)pLock);
  225. if (FileNameLen>sizeof(pLock->File)-1)
  226. strcpy(pLock->File, &file[FileNameLen-sizeof(pLock->File)+1]);
  227. else
  228. strcpy(pLock->File, file);
  229. pLock->Line = line;
  230. }
  231. #endif