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.

257 lines
4.9 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1992 - 1999
  3. Module Name:
  4. util.c
  5. Abstract:
  6. Utility library used for the various debugger extensions in this library.
  7. Author:
  8. Peter Wieland (peterwie) 16-Oct-1995
  9. Environment:
  10. User Mode.
  11. Revision History:
  12. --*/
  13. #include "pch.h"
  14. PUCHAR devicePowerStateNames[] = {
  15. "PowerDeviceUnspecified",
  16. "PowerDeviceD0",
  17. "PowerDeviceD1",
  18. "PowerDeviceD2",
  19. "PowerDeviceD3",
  20. "PowerDeviceMaximum",
  21. "Invalid"
  22. };
  23. FLAG_NAME SrbFlags[] = {
  24. FLAG_NAME(SRB_FLAGS_QUEUE_ACTION_ENABLE),
  25. FLAG_NAME(SRB_FLAGS_DISABLE_DISCONNECT),
  26. FLAG_NAME(SRB_FLAGS_DISABLE_SYNCH_TRANSFER),
  27. FLAG_NAME(SRB_FLAGS_BYPASS_FROZEN_QUEUE),
  28. FLAG_NAME(SRB_FLAGS_DISABLE_AUTOSENSE),
  29. FLAG_NAME(SRB_FLAGS_DATA_IN),
  30. FLAG_NAME(SRB_FLAGS_DATA_OUT),
  31. FLAG_NAME(SRB_FLAGS_NO_QUEUE_FREEZE),
  32. FLAG_NAME(SRB_FLAGS_ADAPTER_CACHE_ENABLE),
  33. FLAG_NAME(SRB_FLAGS_IS_ACTIVE),
  34. FLAG_NAME(SRB_FLAGS_ALLOCATED_FROM_ZONE),
  35. FLAG_NAME(SRB_FLAGS_SGLIST_FROM_POOL),
  36. FLAG_NAME(SRB_FLAGS_BYPASS_LOCKED_QUEUE),
  37. FLAG_NAME(SRB_FLAGS_NO_KEEP_AWAKE),
  38. FLAG_NAME(SRB_FLAGS_PORT_DRIVER_ALLOCSENSE),
  39. FLAG_NAME(SRB_FLAGS_PORT_DRIVER_SENSEHASPORT),
  40. FLAG_NAME(SRB_FLAGS_DONT_START_NEXT_PACKET),
  41. FLAG_NAME(SRB_FLAGS_PORT_DRIVER_RESERVED),
  42. FLAG_NAME(SRB_FLAGS_CLASS_DRIVER_RESERVED),
  43. {0,0}
  44. };
  45. PUCHAR
  46. DevicePowerStateToString(
  47. IN DEVICE_POWER_STATE State
  48. )
  49. {
  50. if(State > PowerDeviceMaximum) {
  51. return devicePowerStateNames[PowerDeviceMaximum + 1];
  52. } else {
  53. return devicePowerStateNames[(UCHAR) State];
  54. }
  55. }
  56. VOID
  57. xdprintf(
  58. ULONG Depth,
  59. PCCHAR S,
  60. ...
  61. )
  62. {
  63. va_list ap;
  64. ULONG i;
  65. CCHAR DebugBuffer[256];
  66. for (i=0; i<Depth; i++) {
  67. dprintf (" ");
  68. }
  69. va_start(ap, S);
  70. vsprintf(DebugBuffer, S, ap);
  71. dprintf (DebugBuffer);
  72. va_end(ap);
  73. }
  74. VOID
  75. DumpFlags(
  76. ULONG Depth,
  77. PUCHAR Name,
  78. ULONG Flags,
  79. PFLAG_NAME FlagTable
  80. )
  81. {
  82. ULONG i;
  83. ULONG mask = 0;
  84. ULONG count = 0;
  85. UCHAR prolog[64];
  86. sprintf(prolog, "%s (0x%08x): ", Name, Flags);
  87. xdprintfEx(Depth, ("%s", prolog));
  88. if(Flags == 0) {
  89. dprintf("\n");
  90. return;
  91. }
  92. memset(prolog, ' ', strlen(prolog));
  93. for(i = 0; FlagTable[i].Name != 0; i++) {
  94. PFLAG_NAME flag = &(FlagTable[i]);
  95. mask |= flag->Flag;
  96. if((Flags & flag->Flag) == flag->Flag) {
  97. //
  98. // print trailing comma
  99. //
  100. if(count != 0) {
  101. dprintf(", ");
  102. //
  103. // Only print two flags per line.
  104. //
  105. if((count % 2) == 0) {
  106. dprintf("\n");
  107. xdprintfEx(Depth, ("%s", prolog));
  108. }
  109. }
  110. dprintf("%s", flag->Name);
  111. count++;
  112. }
  113. }
  114. dprintf("\n");
  115. if((Flags & (~mask)) != 0) {
  116. xdprintfEx(Depth, ("%sUnknown flags %#010lx\n", prolog, (Flags & (~mask))));
  117. }
  118. return;
  119. }
  120. BOOLEAN
  121. GetAnsiString(
  122. IN ULONG_PTR Address,
  123. IN PUCHAR Buffer,
  124. IN OUT PULONG Length
  125. )
  126. {
  127. ULONG i = 0;
  128. //
  129. // Grab the string in 64 character chunks until we find a NULL or the
  130. // read fails.
  131. //
  132. while((i < *Length) && (!CheckControlC())) {
  133. ULONG transferSize;
  134. ULONG result;
  135. if(*Length - i < 128) {
  136. transferSize = *Length - i;
  137. } else {
  138. transferSize = 128;
  139. }
  140. if(!ReadMemory(Address + i,
  141. Buffer + i,
  142. transferSize,
  143. &result)) {
  144. //
  145. // read failed and we didn't find the NUL the last time. Don't
  146. // expect to find it this time.
  147. // BUGBUG - figure out if i should expect it this time.
  148. //
  149. *Length = i;
  150. return FALSE;
  151. } else {
  152. ULONG j;
  153. //
  154. // Scan from where we left off looking for that NUL character.
  155. //
  156. for(j = 0; j < transferSize; j++) {
  157. if(Buffer[i + j] == '\0') {
  158. *Length = i + j;
  159. return TRUE;
  160. }
  161. }
  162. }
  163. i += transferSize;
  164. }
  165. //
  166. // We never found the NUL. Don't need to update Length since it's currently
  167. // equal to i.
  168. //
  169. return FALSE;
  170. }
  171. PCHAR
  172. GuidToString(
  173. GUID* Guid
  174. )
  175. {
  176. static CHAR Buffer [64];
  177. sprintf (Buffer,
  178. "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
  179. Guid->Data1,
  180. Guid->Data2,
  181. Guid->Data3,
  182. Guid->Data4[0],
  183. Guid->Data4[1],
  184. Guid->Data4[2],
  185. Guid->Data4[3],
  186. Guid->Data4[4],
  187. Guid->Data4[5],
  188. Guid->Data4[6],
  189. Guid->Data4[7]
  190. );
  191. return Buffer;
  192. }