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.

483 lines
12 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 1999
  6. //
  7. // File: debug.c
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "pch.h"
  11. // Diagnostic globals - used during development
  12. ULONG d1;
  13. ULONG d2;
  14. ULONG d3;
  15. ULONG d4;
  16. ULONG d5;
  17. ULONG d6;
  18. ULONG d7;
  19. ULONG d8;
  20. ULONG d9;
  21. // Debug globals
  22. ULONG Trace;
  23. ULONG Break;
  24. ULONG AllowAsserts = 0;
  25. //
  26. // set bits using PPT_DD_* bit defs to mask off debug spew for a specific device
  27. //
  28. ULONG DbgMaskFdo = 0;
  29. ULONG DbgMaskRawPort = 0;
  30. ULONG DbgMaskDaisyChain0 = 0;
  31. ULONG DbgMaskDaisyChain1 = 0;
  32. ULONG DbgMaskEndOfChain = 0;
  33. ULONG DbgMaskLegacyZip = 0;
  34. ULONG DbgMaskNoDevice = 0;
  35. PCHAR PnpIrpName[] = {
  36. "0x00 - IRP_MN_START_DEVICE",
  37. "0x01 - IRP_MN_QUERY_REMOVE_DEVICE",
  38. "0x02 - IRP_MN_REMOVE_DEVICE",
  39. "0x03 - IRP_MN_CANCEL_REMOVE_DEVICE",
  40. "0x04 - IRP_MN_STOP_DEVICE",
  41. "0x05 - IRP_MN_QUERY_STOP_DEVICE",
  42. "0x06 - IRP_MN_CANCEL_STOP_DEVICE",
  43. "0x07 - IRP_MN_QUERY_DEVICE_RELATIONS",
  44. "0x08 - IRP_MN_QUERY_INTERFACE",
  45. "0x09 - IRP_MN_QUERY_CAPABILITIES",
  46. "0x0A - IRP_MN_QUERY_RESOURCES",
  47. "0x0B - IRP_MN_QUERY_RESOURCE_REQUIREMENTS",
  48. "0x0C - IRP_MN_QUERY_DEVICE_TEXT",
  49. "0x0D - IRP_MN_FILTER_RESOURCE_REQUIREMENTS",
  50. "0x0E - unused Pnp MinorFunction",
  51. "0x0F - IRP_MN_READ_CONFIG",
  52. "0x10 - IRP_MN_WRITE_CONFIG",
  53. "0x11 - IRP_MN_EJECT",
  54. "0x12 - IRP_MN_SET_LOCK",
  55. "0x13 - IRP_MN_QUERY_ID",
  56. "0x14 - IRP_MN_QUERY_PNP_DEVICE_STATE",
  57. "0x15 - IRP_MN_QUERY_BUS_INFORMATION",
  58. "0x16 - IRP_MN_DEVICE_USAGE_NOTIFICATION",
  59. "0x17 - IRP_MN_SURPRISE_REMOVAL",
  60. "0x18 - IRP_MN_QUERY_LEGACY_BUS_INFORMATION"
  61. };
  62. PCHAR PhaseName[] = {
  63. "PHASE_UNKNOWN",
  64. "PHASE_NEGOTIATION",
  65. "PHASE_SETUP", // Used in ECP mode only
  66. "PHASE_FORWARD_IDLE",
  67. "PHASE_FORWARD_XFER",
  68. "PHASE_FWD_TO_REV",
  69. "PHASE_REVERSE_IDLE",
  70. "PHASE_REVERSE_XFER",
  71. "PHASE_REV_TO_FWD",
  72. "PHASE_TERMINATE",
  73. "PHASE_DATA_AVAILABLE", // Used in nibble and byte modes only
  74. "PHASE_DATA_NOT_AVAIL", // Used in nibble and byte modes only
  75. "PHASE_INTERRUPT_HOST" // Used in nibble and byte modes only
  76. };
  77. #if 1 == DBG_SHOW_BYTES
  78. ULONG DbgShowBytes = 1; // turn off via reg setting: Services\Parport\Parameters\DbgShowBytes : REG_DWORD : 0x0
  79. #endif
  80. #if 1 == PptEnableDebugSpew
  81. VOID
  82. P5TraceIrpArrival( PDEVICE_OBJECT DevObj, PIRP Irp ) {
  83. PCOMMON_EXTENSION cdx = DevObj->DeviceExtension;
  84. DD(cdx,DDE,"Irp arrival %x\n",Irp);
  85. }
  86. VOID
  87. P5TraceIrpCompletion( PIRP Irp ) {
  88. //PCOMMON_EXTENSION cdx = DevObj->DeviceExtension;
  89. DD(NULL,DDE,"Irp completion %x\n",Irp);
  90. }
  91. VOID
  92. P5SetPhase( PPDO_EXTENSION Pdx, P1284_PHASE Phase ) {
  93. LARGE_INTEGER tickCount;
  94. KeQueryTickCount( &tickCount );
  95. if( Pdx->CurrentPhase != Phase ) {
  96. Pdx->CurrentPhase = Phase;
  97. PptAssert(Phase < arraysize(PhaseName));
  98. DD((PCE)Pdx,DDT,"P5SetPhase at %I64x to %2d - %s\n",tickCount,Phase,PhaseName[Phase]);
  99. }
  100. }
  101. VOID
  102. P5BSetPhase( PIEEE_STATE IeeeState, P1284_PHASE Phase ) {
  103. LARGE_INTEGER tickCount;
  104. KeQueryTickCount( &tickCount );
  105. if( IeeeState->CurrentPhase != Phase ) {
  106. IeeeState->CurrentPhase = Phase;
  107. PptAssert(Phase < arraysize(PhaseName));
  108. DD(NULL,DDT,"P5BSetPhase at %I64x to %2d - %s\n",tickCount,Phase,PhaseName[Phase]);
  109. }
  110. }
  111. #include "stdarg.h"
  112. VOID
  113. PptPrint( PCOMMON_EXTENSION Ext, ULONG Flags, PCHAR FmtStr, ... )
  114. {
  115. CHAR buf[ 256 ];
  116. LONG count;
  117. va_list va;
  118. const LONG bufSize = sizeof(buf);
  119. const LONG maxLocationLength = sizeof("LPTx.y: ");
  120. ULONG trace;
  121. ULONG mask;
  122. PPDO_EXTENSION pdx;
  123. PCHAR location;
  124. PptAssertMsg( "0 == Flags", Flags );
  125. PptAssertMsg( "NULL FmtStr", FmtStr );
  126. if( Ext ) {
  127. location = Ext->Location;
  128. if( (NULL == location) || ('\0' == *location) ) {
  129. location = "NoLoc";
  130. }
  131. switch( Ext->DevType ) {
  132. case DevTypeFdo:
  133. mask = DbgMaskFdo;
  134. break;
  135. case DevTypePdo:
  136. pdx = (PPDO_EXTENSION)Ext;
  137. switch( pdx->PdoType ) {
  138. case PdoTypeRawPort:
  139. mask = DbgMaskRawPort;
  140. break;
  141. case PdoTypeEndOfChain:
  142. mask = DbgMaskEndOfChain;
  143. break;
  144. case PdoTypeDaisyChain:
  145. switch( pdx->Ieee1284_3DeviceId ) {
  146. case 0:
  147. mask = DbgMaskDaisyChain0;
  148. break;
  149. case 1:
  150. mask = DbgMaskDaisyChain1;
  151. break;
  152. default:
  153. mask = 0;
  154. PptAssert(FALSE);
  155. }
  156. break;
  157. case PdoTypeLegacyZip:
  158. mask = DbgMaskLegacyZip;
  159. break;
  160. default:
  161. // PptAssertMsg( "Invalid PdoType", FALSE );
  162. mask = 0;
  163. }
  164. break;
  165. default:
  166. // PptAssert(FALSE);
  167. mask = 0;
  168. }
  169. } else {
  170. // not device specific
  171. location = "LPTx";
  172. mask = DbgMaskNoDevice;
  173. }
  174. trace = Flags & Trace & ~mask;
  175. if( trace != 0 ) {
  176. va_start( va, FmtStr );
  177. __try {
  178. count = _snprintf( buf, maxLocationLength, "%-6s: ", location );
  179. if( count < 0 ) {
  180. __leave;
  181. }
  182. count += _vsnprintf( buf+count, bufSize-count-1, FmtStr, va );
  183. if( count >= bufSize ) {
  184. __leave;
  185. }
  186. DbgPrint( "%s", buf );
  187. }
  188. __finally {
  189. va_end( va );
  190. }
  191. } // endif ( trace != 0 )
  192. return;
  193. }
  194. UCHAR
  195. P5ReadPortUchar( PUCHAR Port ) {
  196. UCHAR value;
  197. value = READ_PORT_UCHAR( Port );
  198. if( d1 ) {
  199. DbgPrint("zzz - READ_ PORT_UCHAR %x %02x\n",Port,value);
  200. }
  201. return value;
  202. }
  203. VOID
  204. P5ReadPortBufferUchar( PUCHAR Port, PUCHAR Buffer, ULONG Count ) {
  205. READ_PORT_BUFFER_UCHAR( Port, Buffer, Count );
  206. return;
  207. }
  208. VOID
  209. P5WritePortUchar( PUCHAR Port, UCHAR Value ) {
  210. if( d1 ) {
  211. DbgPrint("zzz - WRITE_PORT_UCHAR %x %02x\n",Port,Value);
  212. }
  213. WRITE_PORT_UCHAR( Port, Value );
  214. return;
  215. }
  216. VOID
  217. P5WritePortBufferUchar( PUCHAR Port, PUCHAR Buffer, ULONG Count )
  218. {
  219. WRITE_PORT_BUFFER_UCHAR( Port, Buffer, Count );
  220. return;
  221. }
  222. VOID
  223. PptFdoDumpPnpIrpInfo(
  224. PDEVICE_OBJECT Fdo,
  225. PIRP Irp
  226. )
  227. {
  228. PFDO_EXTENSION fdx = Fdo->DeviceExtension;
  229. PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation( Irp );
  230. ULONG minorFunction = irpSp->MinorFunction;
  231. PptAssert( DevTypeFdo == fdx->DevType );
  232. if( minorFunction < arraysize(PnpIrpName) ) {
  233. DD((PCE)fdx,DDT,"PptFdoDumpPnpIrpInfo - %s\n",PnpIrpName[ minorFunction ]);
  234. } else {
  235. DD((PCE)fdx,DDT,"PptFdoDumpPnpIrpInfo - bogus MinorFunction = %x\n",minorFunction);
  236. }
  237. }
  238. VOID
  239. PptPdoDumpPnpIrpInfo(
  240. PDEVICE_OBJECT Pdo,
  241. PIRP Irp
  242. )
  243. {
  244. PPDO_EXTENSION pdx = Pdo->DeviceExtension;
  245. PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation( Irp );
  246. ULONG minorFunction = irpSp->MinorFunction;
  247. PptAssert( DevTypePdo == pdx->DevType );
  248. if( minorFunction < arraysize(PnpIrpName) ) {
  249. DD((PCE)pdx,DDT,"PptPdoDumpPnpIrpInfo - %s\n",PnpIrpName[ minorFunction ]);
  250. } else {
  251. DD((PCE)pdx,DDT,"PptPdoDumpPnpIrpInfo - bogus MinorFunction = %x\n",minorFunction);
  252. }
  253. }
  254. NTSTATUS
  255. PptAcquireRemoveLock(
  256. IN PIO_REMOVE_LOCK RemoveLock,
  257. IN PVOID Tag OPTIONAL
  258. )
  259. {
  260. return IoAcquireRemoveLock(RemoveLock, Tag);
  261. }
  262. VOID
  263. PptReleaseRemoveLock(
  264. IN PIO_REMOVE_LOCK RemoveLock,
  265. IN PVOID Tag OPTIONAL
  266. )
  267. {
  268. IoReleaseRemoveLock(RemoveLock, Tag);
  269. }
  270. VOID
  271. PptReleaseRemoveLockAndWait(
  272. IN PIO_REMOVE_LOCK RemoveLock,
  273. IN PVOID Tag
  274. )
  275. {
  276. IoReleaseRemoveLockAndWait(RemoveLock, Tag);
  277. }
  278. #endif // 1 == PptEnableDebugSpew
  279. #if (1 == DVRH_PAR_LOGFILE)
  280. #include "stdarg.h"
  281. /**************************************************************************
  282. Function: DVRH_LogMessage()
  283. Description:Logs message to configured output
  284. Inputs: Parameter indicated message log level and
  285. Format string and parameters
  286. Outputs: Boolean value indicating success or failure
  287. ***************************************************************************/
  288. BOOLEAN DVRH_LogMessage(PCHAR szFormat, ...)
  289. {
  290. ULONG Length;
  291. char messagebuf[256];
  292. va_list va;
  293. IO_STATUS_BLOCK IoStatus;
  294. OBJECT_ATTRIBUTES objectAttributes;
  295. NTSTATUS status;
  296. HANDLE FileHandle;
  297. UNICODE_STRING fileName;
  298. //format the string
  299. va_start(va,szFormat);
  300. _vsnprintf(messagebuf,sizeof(messagebuf),szFormat,va);
  301. va_end(va);
  302. //get a handle to the log file object
  303. fileName.Buffer = NULL;
  304. fileName.Length = 0;
  305. fileName.MaximumLength = sizeof(DEFAULT_LOG_FILE_NAME) + sizeof(UNICODE_NULL);
  306. fileName.Buffer = ExAllocatePool(PagedPool,
  307. fileName.MaximumLength);
  308. if (!fileName.Buffer) {
  309. DD(NULL,DDE,"LogMessage: FAIL. ExAllocatePool Failed.\n");
  310. return FALSE;
  311. }
  312. RtlZeroMemory(fileName.Buffer, fileName.MaximumLength);
  313. status = RtlAppendUnicodeToString(&fileName, (PWSTR)DEFAULT_LOG_FILE_NAME);
  314. InitializeObjectAttributes (&objectAttributes,
  315. (PUNICODE_STRING)&fileName,
  316. OBJ_CASE_INSENSITIVE,
  317. NULL,
  318. NULL );
  319. status = ZwCreateFile( &FileHandle,
  320. FILE_APPEND_DATA,
  321. &objectAttributes,
  322. &IoStatus,
  323. 0,
  324. FILE_ATTRIBUTE_NORMAL,
  325. FILE_SHARE_WRITE,
  326. FILE_OPEN_IF,
  327. FILE_SYNCHRONOUS_IO_NONALERT,
  328. NULL,
  329. 0 );
  330. if( NT_SUCCESS(status) ) {
  331. CHAR buf[300];
  332. LARGE_INTEGER time;
  333. KeQuerySystemTime(&time);
  334. //put a time stamp on the output message
  335. sprintf(buf,"%10u-%10u %s",time.HighPart,time.LowPart,messagebuf);
  336. //format the string to make sure it appends a newline carrage-return to the
  337. //end of the string.
  338. Length=strlen(buf);
  339. if( buf[Length-1]=='\n' ) {
  340. buf[Length-1]='\r';
  341. strcat(buf,"\n");
  342. Length++;
  343. } else {
  344. strcat(buf,"\r\n");
  345. Length+=2;
  346. }
  347. ZwWriteFile( FileHandle, NULL, NULL, NULL, &IoStatus, buf, Length, NULL, NULL );
  348. ZwClose( FileHandle );
  349. }
  350. if( fileName.Buffer ) {
  351. ExFreePool (fileName.Buffer);
  352. }
  353. return STATUS_SUCCESS;
  354. }
  355. /**************************************************************************
  356. Function: DVRH_LogByteData()
  357. Description:Formats byte data to be displayed in the configured output
  358. Inputs: Log level, Whether this is input or output data, a pointer to
  359. the byte data buffer and the size of the buffer
  360. Outputs: Boolean indicated success or failure
  361. ***************************************************************************/
  362. BOOLEAN DVRH_LogByteData(BOOLEAN READ,PCHAR szBuff,ULONG dwTransferred)
  363. {
  364. CString cStr;
  365. ULONG MAX_SIZE=80;
  366. UNICODE_STRING UniStr;
  367. ANSI_STRING AnsiStr;
  368. WCHAR wStr[8];
  369. PCHAR szTemp=szBuff;
  370. UCHAR bTemp;
  371. ULONG dwDisplaySize;
  372. UniStr.Length=0;
  373. UniStr.MaximumLength=8;
  374. UniStr.Buffer=wStr;
  375. AnsiStr.Length=0;
  376. AnsiStr.MaximumLength=0;
  377. AnsiStr.Buffer=NULL;
  378. if(READ)
  379. cStr=L"<RCV";
  380. else
  381. cStr=L"SND>";
  382. //make sure the size of the requested string is within the set range
  383. dwDisplaySize=(((dwTransferred*3)+10) > MAX_SIZE)?((MAX_SIZE-10)/3):dwTransferred;
  384. //format byte data
  385. while(dwDisplaySize)
  386. {
  387. bTemp=szTemp[0];
  388. if(bTemp > 0xF)
  389. cStr+=L" ";
  390. else
  391. cStr+=L" 0";
  392. RtlIntegerToUnicodeString(bTemp,16,&UniStr);
  393. cStr+=UniStr.Buffer;
  394. szTemp++;
  395. dwDisplaySize--;
  396. }
  397. cStr.StringToAnsiString(&AnsiStr);
  398. LogMessage("%5u %s",dwTransferred,AnsiStr.Buffer);
  399. RtlFreeAnsiString(&AnsiStr);
  400. return (TRUE);
  401. }
  402. #endif // (1 == DVRH_PAR_LOGFILE)