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.

597 lines
15 KiB

  1. /*++
  2. Copyright (c) 2002 Microsoft Corporation
  3. Module Name:
  4. debug.c
  5. Abstract:
  6. This module provides debugging support.
  7. Author:
  8. Neil Sandlin (neilsa) Jan 1, 2002
  9. Revision History:
  10. --*/
  11. #include "pch.h"
  12. #if DBG
  13. //
  14. // Get mappings from status codes to strings
  15. //
  16. #include <ntstatus.dbg>
  17. #undef MAP
  18. #define MAP(_Value) { (_Value), #_Value }
  19. #define END_STRING_MAP { 0xFFFFFFFF, NULL }
  20. typedef struct _DBG_MASK_STRING {
  21. ULONG Mask;
  22. PUCHAR String;
  23. } DBG_MASK_STRING, *PDBG_MASK_STRING;
  24. DBG_MASK_STRING MaskStrings[] = {
  25. SDBUS_DEBUG_FAIL, "ERR",
  26. SDBUS_DEBUG_WARNING, "WNG",
  27. SDBUS_DEBUG_INFO, "INF",
  28. SDBUS_DEBUG_PNP, "PNP",
  29. SDBUS_DEBUG_POWER, "PWR",
  30. SDBUS_DEBUG_TUPLES, "TPL",
  31. SDBUS_DEBUG_ENUM, "ENU",
  32. SDBUS_DEBUG_EVENT, "EVT",
  33. SDBUS_DEBUG_CARD_EVT, "CEV",
  34. SDBUS_DEBUG_INTERFACE, "IFC",
  35. SDBUS_DEBUG_IOCTL, "IOC",
  36. SDBUS_DEBUG_WORKENG, "WKR",
  37. SDBUS_DEBUG_WORKPROC, "WKP",
  38. SDBUS_DEBUG_DEVICE, "DEV",
  39. SDBUS_DEBUG_DUMP_REGS, "REG",
  40. 0, NULL
  41. };
  42. PSDBUS_STRING_MAP SdbusDbgStatusStringMap = (PSDBUS_STRING_MAP) ntstatusSymbolicNames;
  43. SDBUS_STRING_MAP SdbusDbgPnpIrpStringMap[] = {
  44. MAP(IRP_MN_START_DEVICE),
  45. MAP(IRP_MN_QUERY_REMOVE_DEVICE),
  46. MAP(IRP_MN_REMOVE_DEVICE),
  47. MAP(IRP_MN_CANCEL_REMOVE_DEVICE),
  48. MAP(IRP_MN_STOP_DEVICE),
  49. MAP(IRP_MN_QUERY_STOP_DEVICE),
  50. MAP(IRP_MN_CANCEL_STOP_DEVICE),
  51. MAP(IRP_MN_QUERY_DEVICE_RELATIONS),
  52. MAP(IRP_MN_QUERY_INTERFACE),
  53. MAP(IRP_MN_QUERY_CAPABILITIES),
  54. MAP(IRP_MN_QUERY_RESOURCES),
  55. MAP(IRP_MN_QUERY_RESOURCE_REQUIREMENTS),
  56. MAP(IRP_MN_QUERY_DEVICE_TEXT),
  57. MAP(IRP_MN_FILTER_RESOURCE_REQUIREMENTS),
  58. MAP(IRP_MN_READ_CONFIG),
  59. MAP(IRP_MN_WRITE_CONFIG),
  60. MAP(IRP_MN_EJECT),
  61. MAP(IRP_MN_SET_LOCK),
  62. MAP(IRP_MN_QUERY_ID),
  63. MAP(IRP_MN_QUERY_PNP_DEVICE_STATE),
  64. MAP(IRP_MN_QUERY_BUS_INFORMATION),
  65. MAP(IRP_MN_DEVICE_USAGE_NOTIFICATION),
  66. MAP(IRP_MN_SURPRISE_REMOVAL),
  67. MAP(IRP_MN_QUERY_LEGACY_BUS_INFORMATION),
  68. END_STRING_MAP
  69. };
  70. SDBUS_STRING_MAP SdbusDbgPoIrpStringMap[] = {
  71. MAP(IRP_MN_WAIT_WAKE),
  72. MAP(IRP_MN_POWER_SEQUENCE),
  73. MAP(IRP_MN_SET_POWER),
  74. MAP(IRP_MN_QUERY_POWER),
  75. END_STRING_MAP
  76. };
  77. SDBUS_STRING_MAP SdbusDbgDeviceRelationStringMap[] = {
  78. MAP(BusRelations),
  79. MAP(EjectionRelations),
  80. MAP(PowerRelations),
  81. MAP(RemovalRelations),
  82. MAP(TargetDeviceRelation),
  83. END_STRING_MAP
  84. };
  85. SDBUS_STRING_MAP SdbusDbgSystemPowerStringMap[] = {
  86. MAP(PowerSystemUnspecified),
  87. MAP(PowerSystemWorking),
  88. MAP(PowerSystemSleeping1),
  89. MAP(PowerSystemSleeping2),
  90. MAP(PowerSystemSleeping3),
  91. MAP(PowerSystemHibernate),
  92. MAP(PowerSystemShutdown),
  93. MAP(PowerSystemMaximum),
  94. END_STRING_MAP
  95. };
  96. SDBUS_STRING_MAP SdbusDbgDevicePowerStringMap[] = {
  97. MAP(PowerDeviceUnspecified),
  98. MAP(PowerDeviceD0),
  99. MAP(PowerDeviceD1),
  100. MAP(PowerDeviceD2),
  101. MAP(PowerDeviceD3),
  102. MAP(PowerDeviceMaximum),
  103. END_STRING_MAP
  104. };
  105. SDBUS_STRING_MAP SdbusDbgTupleStringMap[] = {
  106. MAP(CISTPL_NULL),
  107. MAP(CISTPL_DEVICE),
  108. MAP(CISTPL_INDIRECT),
  109. MAP(CISTPL_CONFIG_CB),
  110. MAP(CISTPL_CFTABLE_ENTRY_CB),
  111. MAP(CISTPL_LONGLINK_MFC),
  112. MAP(CISTPL_CHECKSUM),
  113. MAP(CISTPL_LONGLINK_A),
  114. MAP(CISTPL_LONGLINK_C),
  115. MAP(CISTPL_LINKTARGET),
  116. MAP(CISTPL_NO_LINK),
  117. MAP(CISTPL_VERS_1),
  118. MAP(CISTPL_ALTSTR),
  119. MAP(CISTPL_DEVICE_A),
  120. MAP(CISTPL_JEDEC_C),
  121. MAP(CISTPL_JEDEC_A),
  122. MAP(CISTPL_CONFIG),
  123. MAP(CISTPL_CFTABLE_ENTRY),
  124. MAP(CISTPL_DEVICE_OC),
  125. MAP(CISTPL_DEVICE_OA),
  126. MAP(CISTPL_GEODEVICE),
  127. MAP(CISTPL_GEODEVICE_A),
  128. MAP(CISTPL_MANFID),
  129. MAP(CISTPL_FUNCID),
  130. MAP(CISTPL_FUNCE),
  131. MAP(CISTPL_VERS_2),
  132. MAP(CISTPL_FORMAT),
  133. MAP(CISTPL_GEOMETRY),
  134. MAP(CISTPL_BYTEORDER),
  135. MAP(CISTPL_DATE),
  136. MAP(CISTPL_BATTERY),
  137. MAP(CISTPL_ORG),
  138. MAP(CISTPL_LONGLINK_CB),
  139. MAP(CISTPL_END),
  140. END_STRING_MAP
  141. };
  142. SDBUS_STRING_MAP SdbusDbgWakeStateStringMap[] = {
  143. MAP(WAKESTATE_DISARMED),
  144. MAP(WAKESTATE_WAITING),
  145. MAP(WAKESTATE_WAITING_CANCELLED),
  146. MAP(WAKESTATE_ARMED),
  147. MAP(WAKESTATE_ARMING_CANCELLED),
  148. MAP(WAKESTATE_COMPLETING),
  149. END_STRING_MAP
  150. };
  151. SDBUS_STRING_MAP SdbusDbgEventStringMap[] = {
  152. MAP(SDBUS_EVENT_INSERTION),
  153. MAP(SDBUS_EVENT_REMOVAL),
  154. MAP(SDBUS_EVENT_CARD_RESPONSE),
  155. MAP(SDBUS_EVENT_CARD_RW_END),
  156. MAP(SDBUS_EVENT_BUFFER_EMPTY),
  157. MAP(SDBUS_EVENT_BUFFER_FULL),
  158. END_STRING_MAP
  159. };
  160. SDBUS_STRING_MAP SdbusDbgWPFunctionStringMap[] = {
  161. MAP(SDWP_READBLOCK),
  162. MAP(SDWP_WRITEBLOCK),
  163. MAP(SDWP_READIO),
  164. MAP(SDWP_WRITEIO),
  165. MAP(SDWP_READIO_EXTENDED),
  166. MAP(SDWP_WRITEIO_EXTENDED),
  167. MAP(SDWP_CARD_RESET),
  168. MAP(SDWP_PASSTHRU),
  169. MAP(SDWP_POWER_ON),
  170. MAP(SDWP_POWER_OFF),
  171. MAP(SDWP_IDENTIFY_IO_DEVICE),
  172. MAP(SDWP_IDENTIFY_MEMORY_DEVICE),
  173. MAP(SDWP_INITIALIZE_FUNCTION),
  174. END_STRING_MAP
  175. };
  176. SDBUS_STRING_MAP SdbusDbgWorkerStateStringMap[] = {
  177. MAP(WORKER_IDLE),
  178. MAP(PACKET_PENDING),
  179. MAP(IN_PROCESS),
  180. MAP(WAITING_FOR_TIMER),
  181. END_STRING_MAP
  182. };
  183. SDBUS_STRING_MAP SdbusDbgSocketStateStringMap[] = {
  184. MAP(SOCKET_EMPTY),
  185. MAP(CARD_DETECTED),
  186. MAP(CARD_NEEDS_ENUMERATION),
  187. MAP(CARD_ACTIVE),
  188. MAP(CARD_LOGICALLY_REMOVED),
  189. END_STRING_MAP
  190. };
  191. PCHAR
  192. SdbusDbgLookupString(
  193. IN PSDBUS_STRING_MAP Map,
  194. IN ULONG Id
  195. )
  196. /*++
  197. Routine Description:
  198. Looks up the string associated with Id in string map Map
  199. Arguments:
  200. Map - The string map
  201. Id - The id to lookup
  202. Return Value:
  203. The string
  204. --*/
  205. {
  206. PSDBUS_STRING_MAP current = Map;
  207. while(current->Id != 0xFFFFFFFF) {
  208. if (current->Id == Id) {
  209. return current->String;
  210. }
  211. current++;
  212. }
  213. return "** UNKNOWN **";
  214. }
  215. PUCHAR SdbusDbgLog;
  216. LONG SdbusDbgLogEntry;
  217. LONG SdbusDbgLogCount;
  218. VOID
  219. SdbusDebugPrint(
  220. ULONG DebugMask,
  221. PCCHAR DebugMessage,
  222. ...
  223. )
  224. /*++
  225. Routine Description:
  226. Debug print for the SDBUS enabler.
  227. Arguments:
  228. Check the mask value to see if the debug message is requested.
  229. Return Value:
  230. None
  231. --*/
  232. {
  233. va_list ap;
  234. char buffer[256];
  235. ULONG i = 0;
  236. if (DebugMask & SdbusDebugMask) {
  237. va_start(ap, DebugMessage);
  238. if (!(SdbusDebugMask & SDBUS_DEBUG_LOG)) {
  239. sprintf(buffer, "%s ", "Sdbus");
  240. } else {
  241. ULONGLONG diff;
  242. ULONGLONG interruptTime;
  243. static ULONGLONG lastInterruptTime = 0;
  244. LARGE_INTEGER performanceCounter;
  245. performanceCounter = KeQueryPerformanceCounter(NULL);
  246. interruptTime = performanceCounter.QuadPart;
  247. if (lastInterruptTime != 0) {
  248. diff = interruptTime - lastInterruptTime;
  249. } else {
  250. diff = 0;
  251. }
  252. lastInterruptTime = interruptTime;
  253. sprintf(buffer, "%s (%05d) - ", "Sdbus", (ULONG)diff);
  254. }
  255. for (i = 0; (MaskStrings[i].Mask != 0); i++) {
  256. if (DebugMask & MaskStrings[i].Mask) {
  257. strcat(buffer, MaskStrings[i].String);
  258. strcat(buffer, ": ");
  259. break;
  260. }
  261. }
  262. if (MaskStrings[i].Mask == 0) {
  263. strcat(buffer, "???: ");
  264. }
  265. vsprintf(&buffer[strlen(buffer)], DebugMessage, ap);
  266. if ((SdbusDebugMask & SDBUS_DEBUG_LOG) && (SdbusDbgLog != NULL)) {
  267. PUCHAR pNewLoc = &SdbusDbgLog[SdbusDbgLogEntry*DBGLOGWIDTH];
  268. SdbusDbgLogEntry++;
  269. if (SdbusDbgLogEntry >= DBGLOGCOUNT) {
  270. SdbusDbgLogEntry = 0;
  271. }
  272. if (SdbusDbgLogCount < DBGLOGCOUNT) {
  273. SdbusDbgLogCount++;
  274. }
  275. strncpy(pNewLoc, buffer, DBGLOGWIDTH);
  276. } else {
  277. DbgPrint(buffer);
  278. }
  279. va_end(ap);
  280. }
  281. } // end SdbusDebugPrint()
  282. VOID
  283. SdbusDumpDbgLog(
  284. )
  285. {
  286. ULONG i;
  287. for(i = DBGLOGCOUNT; i > 0; i--) {
  288. SdbusPrintLogEntry(i-1);
  289. }
  290. if (SdbusDebugMask & SDBUS_DEBUG_BREAK) {
  291. DbgBreakPoint();
  292. }
  293. SdbusClearDbgLog();
  294. }
  295. VOID
  296. SdbusPrintLogEntry(
  297. LONG index
  298. )
  299. {
  300. PUCHAR pLogEntry;
  301. LONG logIndex;
  302. if (index >= SdbusDbgLogCount) {
  303. return;
  304. }
  305. logIndex = (SdbusDbgLogEntry - 1) - index;
  306. if (logIndex < 0) {
  307. logIndex += DBGLOGCOUNT;
  308. }
  309. if ((logIndex < 0) || (logIndex >= DBGLOGCOUNT)) {
  310. DbgBreakPoint();
  311. return;
  312. }
  313. pLogEntry = &SdbusDbgLog[logIndex*DBGLOGWIDTH];
  314. DbgPrint(pLogEntry);
  315. }
  316. VOID
  317. SdbusInitializeDbgLog(
  318. PUCHAR buffer
  319. )
  320. {
  321. SdbusDbgLog = buffer;
  322. }
  323. VOID
  324. SdbusClearDbgLog(
  325. )
  326. {
  327. SdbusDbgLogEntry = 0;
  328. SdbusDbgLogCount = 0;
  329. }
  330. #if 0
  331. VOID
  332. MyFreePool(
  333. PVOID pointer
  334. )
  335. {
  336. DebugPrint((SDBUS_DEBUG_WARNING, "FREEPOOL: %08x\n", pointer));
  337. ExFreePool(pointer);
  338. }
  339. #endif
  340. VOID
  341. SdbusDebugDumpCsd(
  342. PSD_CSD sdCsd
  343. )
  344. {
  345. PUCHAR pUc = (PUCHAR) sdCsd;
  346. ULONG deviceSize;
  347. ULONG capacity, blockNr, mult, block_len;
  348. DebugPrint((SDBUS_DEBUG_ENUM, "------------------------CSD----------------------\n"));
  349. DebugPrint((SDBUS_DEBUG_ENUM, "%02x %02x %02x %02x %02x %02x %02x %02x-%02x %02x %02x %02x %02x %02x %02x\n",
  350. pUc[0], pUc[1], pUc[2], pUc[3],
  351. pUc[4], pUc[5], pUc[6], pUc[7],
  352. pUc[8], pUc[9], pUc[10],pUc[11],
  353. pUc[12], pUc[13], pUc[14]));
  354. DebugPrint((SDBUS_DEBUG_ENUM, "TAAC=%02x NSAC=%02x TRANSPEED=%02x\n",
  355. sdCsd->DataReadAccessTime1, sdCsd->DataReadAccessTime2, sdCsd->MaxDataTransferRate));
  356. deviceSize = sdCsd->b.DeviceSizeHigh << 2 | sdCsd->c.DeviceSizeLow;
  357. DebugPrint((SDBUS_DEBUG_ENUM, "DeviceSize=%08x\n", deviceSize));
  358. mult = (1 << (sdCsd->c.DeviceSizeMultiplier+2));
  359. blockNr = (deviceSize+1) * mult;
  360. block_len = (1 << sdCsd->b.MaxReadDataBlockLength);
  361. capacity = blockNr * block_len;
  362. DebugPrint((SDBUS_DEBUG_ENUM, "mult=%08x blockNr=%08x block_len=%08x capacity=%08x\n", mult, blockNr, block_len, capacity));
  363. DebugPrint((SDBUS_DEBUG_ENUM, "DsrImpl=%d RBlMisalign=%d WBlMisalign=%d PartialBlocksRead=%d\n",
  364. sdCsd->b.DsrImplemented, sdCsd->b.ReadBlockMisalignment, sdCsd->b.WriteBlockMisalignment,
  365. sdCsd->b.PartialBlocksRead));
  366. DebugPrint((SDBUS_DEBUG_ENUM, "MaxReadBlkLen=%d CCC=%03x\n",
  367. sdCsd->b.MaxReadDataBlockLength, sdCsd->b.CardCommandClasses));
  368. DebugPrint((SDBUS_DEBUG_ENUM, "WPGsize=%d EraseSectSize=%d EraseSBlEnable=%d DevSizeMult=%d\n",
  369. sdCsd->c.WriteProtectGroupSize, sdCsd->c.EraseSectorSize, sdCsd->c.EraseSingleBlockEnable,
  370. sdCsd->c.DeviceSizeMultiplier));
  371. DebugPrint((SDBUS_DEBUG_ENUM, "WvddMax=%d WvddMin=%d RvddMax=%d RvddMin=%d\n",
  372. sdCsd->c.WriteCurrentVddMax, sdCsd->c.WriteCurrentVddMin, sdCsd->c.ReadCurrentVddMax,
  373. sdCsd->c.ReadCurrentVddMin));
  374. DebugPrint((SDBUS_DEBUG_ENUM, "PartialBlkWrite=%d MaxWriteBlkLen=%d WriteSpeedFactor=%d WPGenable=%d\n",
  375. sdCsd->d.PartialBlocksWrite, sdCsd->d.MaxWriteDataBlockLength, sdCsd->d.WriteSpeedFactor,
  376. sdCsd->d.WriteProtectGroupEnable));
  377. DebugPrint((SDBUS_DEBUG_ENUM, "FileFmt=%d TempWP=%d PermWP=%d CopyFlag=%d FileFmtGroup=%d\n",
  378. sdCsd->e.FileFormat, sdCsd->e.TempWriteProtect, sdCsd->e.PermWriteProtect,
  379. sdCsd->e.CopyFlag, sdCsd->e.FileFormatGroup));
  380. }
  381. VOID
  382. SdbusDebugDumpCid(
  383. PSD_CID sdCid
  384. )
  385. {
  386. UCHAR productName[6];
  387. UCHAR j;
  388. PUCHAR pUc = (PUCHAR) sdCid;
  389. DebugPrint((SDBUS_DEBUG_ENUM, "------------------------CID----------------------\n"));
  390. DebugPrint((SDBUS_DEBUG_ENUM, "%02x %02x %02x %02x %02x %02x %02x %02x-%02x %02x %02x %02x %02x %02x %02x\n",
  391. pUc[0], pUc[1], pUc[2], pUc[3],
  392. pUc[4], pUc[5], pUc[6], pUc[7],
  393. pUc[8], pUc[9], pUc[10],pUc[11],
  394. pUc[12], pUc[13], pUc[14]));
  395. DebugPrint((SDBUS_DEBUG_ENUM, "ManufacturerId=%02x OEMId=%04x Rev=%02x PSN=%08x\n",
  396. sdCid->ManufacturerId, sdCid->OemId, sdCid->Revision, sdCid->SerialNumber));
  397. for (j=0; j<5; j++) productName[j] = sdCid->ProductName[4-j];
  398. productName[5] = 0;
  399. DebugPrint((SDBUS_DEBUG_ENUM, "ProductName = %s\n", productName));
  400. DebugPrint((SDBUS_DEBUG_ENUM, "ManufacturerDate=%d, %d\n",
  401. sdCid->a.ManufactureMonth, sdCid->a.ManufactureYearLow+(sdCid->b.ManufactureYearHigh<<4)+2000));
  402. }
  403. VOID
  404. DebugDumpSdResponse(
  405. PULONG pResp,
  406. UCHAR ResponseType
  407. )
  408. {
  409. {
  410. ULONG resp = pResp[0];
  411. switch (ResponseType) {
  412. case SDCMD_RESP_NONE:
  413. break;
  414. case SDCMD_RESP_1:
  415. DebugPrint((SDBUS_DEBUG_DEVICE, "R1 resp %08x currentState = %d\n", resp, (resp & 0x00001e00) >> 9));
  416. break;
  417. case SDCMD_RESP_5:
  418. DebugPrint((SDBUS_DEBUG_DEVICE, "R5 resp %08x state=%d%s%s%s%s%s\n", resp, (resp > 12) & 3,
  419. resp & 0x8000 ? " CRC ERR" : "",
  420. resp & 0x4000 ? " CMD ERR" : "",
  421. resp & 0x0800 ? " GEN ERR" : "",
  422. resp & 0x0200 ? " FUNC NUM ERR" : "",
  423. resp & 0x0100 ? " RANGE ERR" : ""));
  424. break;
  425. case SDCMD_RESP_4:
  426. case SDCMD_RESP_6:
  427. case SDCMD_RESP_2:
  428. case SDCMD_RESP_3:
  429. case SDCMD_RESP_1B:
  430. DebugPrint((SDBUS_DEBUG_DEVICE, "resp=%08x %08x %08x %08x\n",
  431. pResp[0], pResp[1], pResp[2], pResp[3]));
  432. break;
  433. default:
  434. ASSERT(FALSE);
  435. }
  436. }
  437. }
  438. #endif