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.

231 lines
5.5 KiB

  1. // DbgPrint - does debug prints to Out_Debug_String. This allows messages to be
  2. // printed when using SOFT-ICE/W to debug VxDs.
  3. //
  4. // File courtesy Hans Hurvig.
  5. //
  6. void DbgPrint(char *afmt, ...);
  7. #include <basedef.h>
  8. #include <vmm.h>
  9. #include <stdarg.h>
  10. #define DBGPRINT_BUFFER_SIZE 1024
  11. UCHAR DbgBuf [DBGPRINT_BUFFER_SIZE+1];
  12. PUCHAR pDbgBuf = DbgBuf;
  13. PUCHAR pDbgBufEnd = DbgBuf+DBGPRINT_BUFFER_SIZE;
  14. UCHAR DbgHexChars[] = "0123456789ABCDEF";
  15. #ifdef DBCS_SUPPORT
  16. UCHAR DbgLeadByte=0; // Nonzero lead byte if in the middle of outputting a DBCS char
  17. #endif
  18. _inline void DbgFlush(void)
  19. {
  20. if (pDbgBuf != DbgBuf) {
  21. *pDbgBuf = 0;
  22. VMM_Out_Debug_String ( pDbgBuf = DbgBuf );
  23. }
  24. }
  25. _inline void DbgPutcLiteral(UCHAR c)
  26. {
  27. if (pDbgBuf >= pDbgBufEnd) {
  28. DbgFlush();
  29. }
  30. *(pDbgBuf++) = c;
  31. }
  32. _inline void DbcPutHexByteLiteral(UCHAR c)
  33. {
  34. DbgPutcLiteral(DbgHexChars[c >> 4]);
  35. DbgPutcLiteral(DbgHexChars[c & 0x0F]);
  36. }
  37. void _fastcall DbgPutc(UCHAR c)
  38. {
  39. #ifdef DBCS_SUPPORT
  40. if (DbgLeadByte != 0) {
  41. UCHAR c1 = DbgLeadByte;
  42. DbgLeadByte = 0;
  43. DbgPutcLiteral('<');
  44. DbcPutHexByteLiteral(c1);
  45. DbcPutHexByteLiteral(c);
  46. DbgPutcLiteral('>');
  47. } else if (c == '\0') {
  48. DbgPutcLiteral('<');
  49. DbcPutHexByteLiteral('N');
  50. DbcPutHexByteLiteral('U');
  51. DbcPutHexByteLiteral('L');
  52. DbgPutcLiteral('>');
  53. } else if (IsDBCSLeadByte(c)) {
  54. DbgLeadByte = c;
  55. } else
  56. #endif
  57. if (c == '\n') {
  58. DbgPutcLiteral('\r');
  59. DbgPutcLiteral('\n');
  60. } else if (c >= 0x7f || c < ' ') {
  61. DbgPutcLiteral('<');
  62. DbcPutHexByteLiteral(c);
  63. DbgPutcLiteral('>');
  64. } else {
  65. DbgPutcLiteral(c);
  66. }
  67. }
  68. void
  69. putl(unsigned long ul, unsigned short base, short mindig, UCHAR pad)
  70. {
  71. static UCHAR buf[12];
  72. register UCHAR *cp = buf;
  73. buf[0] = 0;
  74. do {
  75. --mindig;
  76. *++cp = DbgHexChars[ul % base];
  77. } while ((ul /= base) != 0);
  78. for ( ; mindig > 0 ; --mindig)
  79. DbgPutc(pad);
  80. do {
  81. DbgPutc(*cp);
  82. } while (*--cp);
  83. }
  84. void DbgPrint(char *afmt, ...)
  85. {
  86. register int c;
  87. PUCHAR psz;
  88. unsigned short base;
  89. /** va_list list; **/
  90. PUCHAR list;
  91. PUCHAR oldfmt;
  92. register UCHAR *fmt = afmt;
  93. va_start(list, afmt);
  94. for (; (c = *fmt) != 0 ; ++fmt) {
  95. oldfmt = fmt;
  96. if (c != '%') {
  97. DbgPutc((UCHAR)c);
  98. #ifdef DBCS_SUPPORT
  99. if (IsDBCSLeadByte(c)) {
  100. c = *(++fmt);
  101. if (c == '\0')
  102. goto endloop;
  103. DbgPutc((UCHAR)c);
  104. }
  105. #endif
  106. } else {
  107. char fLong = 0;
  108. char fFar = 0;
  109. unsigned short minchr = 0, maxchr = 0xffff;
  110. char fLJ = 0;
  111. char pad = ' ';
  112. base = 10;
  113. if (fmt[1] == '-') {
  114. fLJ++;
  115. fmt++;
  116. }
  117. if (fmt[1] == '0')
  118. pad = '0';
  119. while ((c = *++fmt) >= '0' && c <= '9')
  120. minchr = minchr*10 + c - '0';
  121. if (c == '.') {
  122. maxchr = 0;
  123. while ((c = *++fmt) >= '0' && c <= '9')
  124. maxchr = maxchr*10 + c - '0';
  125. }
  126. if (c == 'l') {
  127. fLong = 1;
  128. c = *++fmt;
  129. }
  130. if (c == 'F') {
  131. fFar = 1;
  132. c = *++fmt;
  133. }
  134. switch (c) {
  135. case 'c':
  136. DbgPutc((char) va_arg(list, int));
  137. break;
  138. case 'p':
  139. case 'P':
  140. if (fLong) {
  141. DbgFlush();
  142. if (fFar)
  143. VMM_Out_Debug_Data_Label( va_arg(list, void *) );
  144. else
  145. VMM_Out_Debug_Code_Label( va_arg(list, void *) );
  146. break;
  147. }
  148. // not a "long" pointer; treat like 'X'
  149. case 'x':
  150. case 'X':
  151. base = 16;
  152. case 'd':
  153. putl(va_arg(list, unsigned long), base, minchr, pad);
  154. break;
  155. case 's':
  156. psz = va_arg(list, char *);
  157. {
  158. unsigned sln;
  159. unsigned i;
  160. if (!fLong)
  161. sln = strlen(psz);
  162. else {
  163. sln = *(unsigned char *)psz; // Treat "l" attrib on string as PSTRING
  164. psz++;
  165. }
  166. if (maxchr) {
  167. if (sln > maxchr)
  168. sln = maxchr;
  169. }
  170. if (minchr && !fLJ) {
  171. while (minchr-- > sln)
  172. DbgPutc(' ');
  173. }
  174. for (i=0;i<sln;i++)
  175. DbgPutc(*psz++);
  176. while (i++ < minchr)
  177. DbgPutc(' ');
  178. }
  179. break;
  180. case '%':
  181. DbgPutc('%');
  182. break;
  183. default:
  184. DbgPutc('%');
  185. fmt = oldfmt;
  186. break;
  187. }
  188. }
  189. }
  190. endloop:
  191. va_end(list);
  192. DbgFlush();
  193. }