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.

230 lines
5.1 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1990 - 2000
  3. All rights reserved.
  4. Module Name:
  5. dbgutil.c
  6. Abstract:
  7. This module provides all the Spooler Subsystem Debugger utility
  8. functions.
  9. Author:
  10. Krishna Ganugapati (KrishnaG) 1-July-1993
  11. Revision History:
  12. --*/
  13. #include "precomp.h"
  14. #pragma hdrstop
  15. #include "dbglocal.h"
  16. #include "dbgsec.h"
  17. SYSTEM_INFO gSysInfo;
  18. DWORD EvalValue(
  19. LPSTR *pptstr,
  20. PNTSD_GET_EXPRESSION EvalExpression,
  21. PNTSD_OUTPUT_ROUTINE Print)
  22. {
  23. LPSTR lpArgumentString;
  24. LPSTR lpAddress;
  25. DWORD dw;
  26. char ach[80];
  27. UINT_PTR cch;
  28. UNREFERENCED_PARAMETER(Print);
  29. lpArgumentString = *pptstr;
  30. while (isspace(*lpArgumentString))
  31. lpArgumentString++;
  32. lpAddress = lpArgumentString;
  33. while ((!isspace(*lpArgumentString)) && (*lpArgumentString != 0))
  34. lpArgumentString++;
  35. cch = (UINT_PTR)lpArgumentString - (UINT_PTR)lpAddress;
  36. if (cch > 79)
  37. cch = 79;
  38. strncpy(ach, lpAddress, (UINT)cch);
  39. // Print("\"%s\"\n", lpAddress);
  40. dw = (DWORD)EvalExpression(lpAddress);
  41. *pptstr = lpArgumentString;
  42. return dw;
  43. }
  44. VOID
  45. ConvertSidToAsciiString(
  46. PSID pSid,
  47. LPSTR String
  48. )
  49. /*++
  50. Routine Description:
  51. This function generates a printable unicode string representation
  52. of a SID.
  53. The resulting string will take one of two forms. If the
  54. IdentifierAuthority value is not greater than 2^32, then
  55. the SID will be in the form:
  56. S-1-281736-12-72-9-110
  57. ^ ^^ ^^ ^ ^^^
  58. | | | | |
  59. +-----+--+-+--+---- Decimal
  60. Otherwise it will take the form:
  61. S-1-0x173495281736-12-72-9-110
  62. ^^^^^^^^^^^^^^ ^^ ^^ ^ ^^^
  63. Hexidecimal | | | |
  64. +--+-+--+---- Decimal
  65. Arguments:
  66. pSid - opaque pointer that supplies the SID that is to be
  67. converted to Unicode.
  68. Return Value:
  69. If the Sid is successfully converted to a Unicode string, a
  70. pointer to the Unicode string is returned, else NULL is
  71. returned.
  72. --*/
  73. {
  74. UCHAR Buffer[256];
  75. UCHAR i;
  76. ULONG Tmp;
  77. SID_IDENTIFIER_AUTHORITY *pSidIdentifierAuthority;
  78. PUCHAR pSidSubAuthorityCount;
  79. if (!IsValidSid( pSid )) {
  80. *String= '\0';
  81. return;
  82. }
  83. sprintf(Buffer, "S-%u-", (USHORT)(((PISID)pSid)->Revision ));
  84. strcpy(String, Buffer);
  85. pSidIdentifierAuthority = GetSidIdentifierAuthority(pSid);
  86. if ( (pSidIdentifierAuthority->Value[0] != 0) ||
  87. (pSidIdentifierAuthority->Value[1] != 0) ){
  88. sprintf(Buffer, "0x%02hx%02hx%02hx%02hx%02hx%02hx",
  89. (USHORT)pSidIdentifierAuthority->Value[0],
  90. (USHORT)pSidIdentifierAuthority->Value[1],
  91. (USHORT)pSidIdentifierAuthority->Value[2],
  92. (USHORT)pSidIdentifierAuthority->Value[3],
  93. (USHORT)pSidIdentifierAuthority->Value[4],
  94. (USHORT)pSidIdentifierAuthority->Value[5] );
  95. strcat(String, Buffer);
  96. } else {
  97. Tmp = (ULONG)pSidIdentifierAuthority->Value[5] +
  98. (ULONG)(pSidIdentifierAuthority->Value[4] << 8) +
  99. (ULONG)(pSidIdentifierAuthority->Value[3] << 16) +
  100. (ULONG)(pSidIdentifierAuthority->Value[2] << 24);
  101. sprintf(Buffer, "%lu", Tmp);
  102. strcat(String, Buffer);
  103. }
  104. pSidSubAuthorityCount = GetSidSubAuthorityCount(pSid);
  105. for (i=0;i< *(pSidSubAuthorityCount);i++ ) {
  106. sprintf(Buffer, "-%lu", *(GetSidSubAuthority(pSid, i)));
  107. strcat(String, Buffer);
  108. }
  109. }
  110. BOOL
  111. ReadProcessString(
  112. IN HANDLE hProcess,
  113. IN LPCVOID lpBaseAddress,
  114. OUT LPVOID lpBuffer,
  115. IN SIZE_T nSize,
  116. OUT SIZE_T *lpNumberOfBytesRead
  117. )
  118. {
  119. BOOL bRetval = FALSE;
  120. SIZE_T cbRead = 0;
  121. UINT_PTR nAddress = 0;
  122. //
  123. // Attempt to read the memory, up to the provided size.
  124. //
  125. bRetval = ReadProcessMemory(hProcess, lpBaseAddress, lpBuffer, nSize, &cbRead);
  126. //
  127. // The string in the debugged process may have unmapped memory just after
  128. // the end of the string, (this is true when page heap is enabled),
  129. //
  130. // If the read failed and the address plus the string buffer size crosses
  131. // a page boundary then retry the operation up to the page end.
  132. //
  133. if (!bRetval)
  134. {
  135. nAddress = (UINT_PTR)lpBaseAddress;
  136. //
  137. // If we have crossed a page boundary.
  138. //
  139. if (((nAddress & (gSysInfo.dwPageSize-1)) + nSize) > gSysInfo.dwPageSize-1)
  140. {
  141. nSize = (SIZE_T)((gSysInfo.dwPageSize-1) - (nAddress & (gSysInfo.dwPageSize-1)));
  142. bRetval = ReadProcessMemory(hProcess, lpBaseAddress, lpBuffer, nSize, &cbRead);
  143. }
  144. }
  145. //
  146. // The read succeeded.
  147. //
  148. if (bRetval)
  149. {
  150. //
  151. // If the caller wants to know the number of bytes read.
  152. //
  153. if (lpNumberOfBytesRead)
  154. {
  155. *lpNumberOfBytesRead = cbRead;
  156. }
  157. }
  158. return bRetval;
  159. }
  160. //
  161. // Query the system for the page size.
  162. //
  163. BOOL
  164. QuerySystemInformation(
  165. VOID
  166. )
  167. {
  168. GetSystemInfo(&gSysInfo);
  169. return TRUE;
  170. }