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.

301 lines
7.8 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. #include "debug.tmh"
  14. #if MEM_CHECKING
  15. typedef struct MEM_HDR {
  16. LIST_ENTRY ListEntry;
  17. ULONG Size;
  18. CHAR File[16];
  19. ULONG Line;
  20. } MEM_HDR;
  21. LIST_ENTRY leAlloc;
  22. NDIS_SPIN_LOCK slAlloc;
  23. VOID InitMemory()
  24. {
  25. NdisAllocateSpinLock(&slAlloc);
  26. NdisInitializeListHead(&leAlloc);
  27. }
  28. VOID DeinitMemory()
  29. {
  30. PLIST_ENTRY ListEntry;
  31. NdisAcquireSpinLock(&slAlloc);
  32. for (ListEntry=leAlloc.Flink;
  33. ListEntry!=&leAlloc;
  34. ListEntry = ListEntry->Flink)
  35. {
  36. MEM_HDR *Hdr = (MEM_HDR*)ListEntry;
  37. DEBUGMSG(DBG_ERROR, (DTEXT("PPTP Unfreed Memory:%p size:%X <%s:%d>\n"),
  38. &Hdr[1], Hdr->Size, Hdr->File, Hdr->Line));
  39. WPLOG(LL_A, LM_Res, ("PPTP Unfreed Memory:%p size:%X <%s:%d>",
  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. WPLOG(LL_A, LM_Res, ("Memory allocation failed size %d tag %x", size, tag));
  66. memptr = NULL;
  67. gCounters.ulMemAllocFail++;
  68. }
  69. #if MEM_CHECKING
  70. else
  71. {
  72. MEM_HDR *Hdr = memptr;
  73. UINT FileNameLen = strlen(file);
  74. Hdr->Size = size;
  75. Hdr->Line = line;
  76. if (FileNameLen>sizeof(Hdr->File)-1)
  77. strcpy(Hdr->File, &file[FileNameLen-sizeof(Hdr->File)+1]);
  78. else
  79. strcpy(Hdr->File, file);
  80. MyInterlockedInsertHeadList(&leAlloc, &Hdr->ListEntry, &slAlloc);
  81. memptr = &Hdr[1];
  82. }
  83. #endif
  84. return memptr;
  85. }
  86. VOID
  87. MyMemFree(
  88. PVOID memptr,
  89. UINT size
  90. )
  91. {
  92. #if MEM_CHECKING
  93. PLIST_ENTRY ListEntry;
  94. MEM_HDR *Hdr = (MEM_HDR*)((PUCHAR)memptr-sizeof(MEM_HDR));
  95. NdisAcquireSpinLock(&slAlloc);
  96. for (ListEntry = leAlloc.Flink;
  97. ListEntry != &leAlloc;
  98. ListEntry = ListEntry->Flink)
  99. {
  100. if (ListEntry==&Hdr->ListEntry)
  101. {
  102. RemoveEntryList(&Hdr->ListEntry);
  103. break;
  104. }
  105. }
  106. if (ListEntry==&leAlloc)
  107. {
  108. DEBUGMSG(DBG_ERROR, (DTEXT("PPTP: Freeing memory not owned %p\n"), memptr));
  109. WPLOG(LL_A, LM_Res, ("PPTP: Freeing memory not owned %p", memptr));
  110. }
  111. NdisReleaseSpinLock(&slAlloc);
  112. NdisFreeMemory(Hdr, size+sizeof(MEM_HDR), 0);
  113. #else
  114. NdisFreeMemory(memptr, size, 0);
  115. #endif
  116. }
  117. #if LIST_CHECKING
  118. VOID FASTCALL CheckList(PLIST_ENTRY ListHead)
  119. {
  120. PLIST_ENTRY ListEntry, PrevListEntry;
  121. if (ListHead->Flink==ListHead)
  122. {
  123. if (ListHead->Blink!=ListHead)
  124. {
  125. DEBUGMSG(DBG_ERROR|DBG_BREAK,(DTEXT("PPTP: Corrupt list head:%p Flink:%p Blink:%p\n"), ListHead, ListHead->Flink, ListHead->Blink));
  126. WPLOG(LL_A, LM_Res, ("PPTP: Corrupt list head:%p Flink:%p Blink:%p", ListHead, ListHead->Flink, ListHead->Blink));
  127. }
  128. }
  129. else
  130. {
  131. ListEntry = ListHead;
  132. do
  133. {
  134. PrevListEntry = ListEntry;
  135. ListEntry = ListEntry->Flink;
  136. if (ListEntry->Blink!=PrevListEntry)
  137. {
  138. DEBUGMSG(DBG_ERROR|DBG_BREAK, (DTEXT("PPTP: Corrupt LIST_ENTRY Head:%p %p->Flink==%p %p->Blink==%p\n"),
  139. ListHead, PrevListEntry, PrevListEntry->Flink, ListEntry, ListEntry->Blink));
  140. WPLOG(LL_A, LM_Res, ("PPTP: Corrupt LIST_ENTRY Head:%p %p->Flink==%p %p->Blink==%p",
  141. ListHead, PrevListEntry, PrevListEntry->Flink, ListEntry, ListEntry->Blink));
  142. }
  143. } while (ListEntry!=ListHead);
  144. }
  145. }
  146. // Check if the entry exists in the list
  147. BOOLEAN FASTCALL CheckListEntry(PLIST_ENTRY ListHead, PLIST_ENTRY Entry)
  148. {
  149. PLIST_ENTRY ListEntry;
  150. ListEntry = ListHead->Flink;
  151. while(ListEntry != ListHead)
  152. {
  153. if(ListEntry == Entry)
  154. {
  155. return TRUE;
  156. }
  157. ListEntry = ListEntry->Flink;
  158. }
  159. return FALSE;
  160. }
  161. #endif
  162. PLIST_ENTRY FASTCALL MyInterlockedInsertHeadList(PLIST_ENTRY Head, PLIST_ENTRY Entry, PNDIS_SPIN_LOCK SpinLock)
  163. {
  164. PLIST_ENTRY RetVal;
  165. NdisAcquireSpinLock(SpinLock);
  166. if (IsListEmpty(Head))
  167. RetVal = NULL;
  168. else
  169. RetVal = Head->Flink;
  170. InsertHeadList(Head, Entry);
  171. NdisReleaseSpinLock(SpinLock);
  172. return RetVal;
  173. }
  174. PLIST_ENTRY FASTCALL MyInterlockedInsertTailList(PLIST_ENTRY Head, PLIST_ENTRY Entry, PNDIS_SPIN_LOCK SpinLock)
  175. {
  176. PLIST_ENTRY RetVal;
  177. NdisAcquireSpinLock(SpinLock);
  178. if (IsListEmpty(Head))
  179. RetVal = NULL;
  180. else
  181. RetVal = Head->Blink;
  182. InsertTailList(Head, Entry);
  183. NdisReleaseSpinLock(SpinLock);
  184. return RetVal;
  185. }
  186. PLIST_ENTRY FASTCALL MyInterlockedRemoveHeadList(PLIST_ENTRY Head, PNDIS_SPIN_LOCK SpinLock)
  187. {
  188. PLIST_ENTRY RetVal;
  189. NdisAcquireSpinLock(SpinLock);
  190. //RemoveHeadList uses RemoveEntryList, which is redefined to call CheckList in DEBUG.H
  191. RetVal = RemoveHeadList(Head);
  192. if (RetVal==Head)
  193. RetVal = NULL;
  194. else
  195. RetVal->Flink = RetVal->Blink = NULL;
  196. NdisReleaseSpinLock(SpinLock);
  197. return RetVal;
  198. }
  199. #define CASE_RETURN_NAME(x) case x: return DTEXT(#x)
  200. #ifdef DBG
  201. char *ControlStateToString(ULONG State)
  202. {
  203. switch( State ){
  204. CASE_RETURN_NAME(STATE_CTL_INVALID);
  205. CASE_RETURN_NAME(STATE_CTL_LISTEN);
  206. CASE_RETURN_NAME(STATE_CTL_DIALING);
  207. CASE_RETURN_NAME(STATE_CTL_WAIT_REQUEST);
  208. CASE_RETURN_NAME(STATE_CTL_WAIT_REPLY);
  209. CASE_RETURN_NAME(STATE_CTL_ESTABLISHED);
  210. CASE_RETURN_NAME(STATE_CTL_WAIT_STOP);
  211. CASE_RETURN_NAME(STATE_CTL_CLEANUP);
  212. default:
  213. return DTEXT("UNKNOWN CONTROL STATE");
  214. }
  215. }
  216. char *CallStateToString(ULONG State)
  217. {
  218. switch( State ){
  219. CASE_RETURN_NAME(STATE_CALL_INVALID);
  220. CASE_RETURN_NAME(STATE_CALL_CLOSED);
  221. CASE_RETURN_NAME(STATE_CALL_IDLE);
  222. CASE_RETURN_NAME(STATE_CALL_OFFHOOK);
  223. CASE_RETURN_NAME(STATE_CALL_OFFERING);
  224. CASE_RETURN_NAME(STATE_CALL_PAC_OFFERING);
  225. CASE_RETURN_NAME(STATE_CALL_PAC_WAIT);
  226. CASE_RETURN_NAME(STATE_CALL_DIALING);
  227. CASE_RETURN_NAME(STATE_CALL_PROCEEDING);
  228. CASE_RETURN_NAME(STATE_CALL_ESTABLISHED);
  229. CASE_RETURN_NAME(STATE_CALL_WAIT_DISCONNECT);
  230. CASE_RETURN_NAME(STATE_CALL_CLEANUP);
  231. default:
  232. return DTEXT("UNKNOWN CALL STATE");
  233. }
  234. }
  235. #endif
  236. #if LOCK_CHECKING
  237. VOID FASTCALL
  238. _MyAcquireSpinLock(
  239. PMY_SPIN_LOCK pLock,
  240. PUCHAR file,
  241. UINT line
  242. )
  243. {
  244. UINT FileNameLen = strlen(file);
  245. NdisAcquireSpinLock((PNDIS_SPIN_LOCK)pLock);
  246. if (FileNameLen>sizeof(pLock->File)-1)
  247. strcpy(pLock->File, &file[FileNameLen-sizeof(pLock->File)+1]);
  248. else
  249. strcpy(pLock->File, file);
  250. pLock->Line = line;
  251. }
  252. #endif