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.

392 lines
9.7 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995.
  5. //
  6. // File: dbgtool.c
  7. //
  8. // Contents:
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 4-03-95 RichardW Created
  15. //
  16. //----------------------------------------------------------------------------
  17. #include "dbgtool.h"
  18. #include <stdlib.h>
  19. HANDLE hMapping;
  20. PDebugHeader pHeader;
  21. ULONG_PTR Xlate ;
  22. #define TranslatePointer( x ) (PVOID) ( (x) ? ((PUCHAR) x + Xlate) : NULL )
  23. PVOID
  24. MapDebugMemory(DWORD pid)
  25. {
  26. WCHAR szMapping[32];
  27. PDebugHeader pHeader = NULL;
  28. PVOID pvMap;
  29. DWORD CommitSize;
  30. SYSTEM_INFO SysInfo;
  31. SECURITY_DESCRIPTOR min ;
  32. GetSystemInfo(&SysInfo);
  33. swprintf(szMapping, TEXT("Debug.Memory.%x"), pid);
  34. hMapping = OpenFileMapping( FILE_MAP_ALL_ACCESS,
  35. FALSE,
  36. szMapping);
  37. if ( !hMapping && GetLastError() == ERROR_ACCESS_DENIED )
  38. {
  39. hMapping = OpenFileMapping( WRITE_DAC | READ_CONTROL,
  40. FALSE,
  41. szMapping );
  42. if ( hMapping )
  43. {
  44. InitializeSecurityDescriptor(&min, 1);
  45. SetSecurityDescriptorDacl(&min,FALSE,NULL,FALSE);
  46. SetKernelObjectSecurity(hMapping, DACL_SECURITY_INFORMATION, &min );
  47. CloseHandle( hMapping );
  48. hMapping = OpenFileMapping( FILE_MAP_ALL_ACCESS,
  49. FALSE,
  50. szMapping);
  51. }
  52. }
  53. if (hMapping)
  54. {
  55. pHeader = MapViewOfFileEx( hMapping,
  56. FILE_MAP_ALL_ACCESS,
  57. 0,
  58. 0,
  59. 0,
  60. NULL);
  61. if (pHeader && (pHeader != pHeader->pvSection))
  62. {
  63. //
  64. // Rats. Remap at preferred address:
  65. //
  66. pvMap = pHeader->pvSection;
  67. UnmapViewOfFile(pHeader);
  68. pHeader = MapViewOfFileEx( hMapping,
  69. FILE_MAP_READ | FILE_MAP_WRITE,
  70. 0,
  71. 0,
  72. 0,
  73. pvMap);
  74. if (pHeader)
  75. {
  76. CommitSize = pHeader->CommitRange;
  77. }
  78. else
  79. {
  80. //
  81. // Can't map at same address, unfortunately. Set up the translation:
  82. //
  83. pHeader = MapViewOfFileEx( hMapping,
  84. FILE_MAP_READ | FILE_MAP_WRITE,
  85. 0,
  86. 0,
  87. 0,
  88. NULL );
  89. if ( pHeader )
  90. {
  91. Xlate = (ULONG_PTR) ((PUCHAR) pHeader - (PUCHAR) pHeader->pvSection );
  92. }
  93. }
  94. }
  95. else
  96. {
  97. }
  98. }
  99. return(pHeader);
  100. }
  101. //+---------------------------------------------------------------------------
  102. //
  103. // Function: DbgpFindModule
  104. //
  105. // Synopsis: Locates a module based on a name
  106. //
  107. // Arguments: [pHeader] -- Header to search
  108. // [pszName] -- module to find
  109. //
  110. // History: 3-22-95 RichardW Created
  111. //
  112. // Notes:
  113. //
  114. //----------------------------------------------------------------------------
  115. PDebugModule
  116. DbgFindModule(
  117. PDebugHeader pHeader,
  118. CHAR * pszName)
  119. {
  120. PDebugModule pSearch;
  121. pSearch = TranslatePointer( pHeader->pModules);
  122. while ( pSearch )
  123. {
  124. if (_stricmp( TranslatePointer( pSearch->pModuleName ), pszName) == 0)
  125. {
  126. return(pSearch);
  127. }
  128. pSearch = TranslatePointer( pSearch->pNext );
  129. }
  130. return(NULL);
  131. }
  132. RenameKeys(
  133. HWND hDlg,
  134. PDebugModule pModule)
  135. {
  136. int i;
  137. DWORD f;
  138. for (i = 0, f = 1; i < 32 ; i++, f <<= 1 )
  139. {
  140. if (pModule->TagLevels[i])
  141. {
  142. SetDlgItemTextA(hDlg, i + IDD_CHECK_0, TranslatePointer( pModule->TagLevels[i] ) );
  143. }
  144. else
  145. {
  146. SetDlgItemTextA(hDlg, i + IDD_CHECK_0, "");
  147. }
  148. CheckDlgButton(hDlg, i + IDD_CHECK_0, (pModule->InfoLevel & f) ? 1 : 0);
  149. }
  150. return(0);
  151. }
  152. InitDialog(
  153. HWND hDlg)
  154. {
  155. PDebugModule pSearch;
  156. LRESULT index;
  157. HWND hLB;
  158. CHAR szText[MAX_PATH] = {0};
  159. CHAR szTitle[MAX_PATH] = {0};
  160. //
  161. // Load Listbox:
  162. //
  163. hLB = GetDlgItem(hDlg, IDD_DEBUG_LB);
  164. pSearch = TranslatePointer( pHeader->pModules );
  165. while (pSearch)
  166. {
  167. index = SendMessageA(
  168. hLB,
  169. LB_ADDSTRING,
  170. 0,
  171. (LPARAM) TranslatePointer( pSearch->pModuleName ) );
  172. SendMessage(
  173. hLB,
  174. LB_SETITEMDATA,
  175. index,
  176. (LPARAM) pSearch );
  177. pSearch = TranslatePointer( pSearch->pNext );
  178. }
  179. SetFocus(hLB);
  180. SendMessage(hLB, LB_SETSEL, 1, 0);
  181. ShowWindow(GetDlgItem(hDlg, IDD_MODULE_TEXT), SW_HIDE);
  182. ShowWindow(GetDlgItem(hDlg, IDD_MODULE_OUTPUT), SW_HIDE);
  183. if (pHeader->pszExeName)
  184. {
  185. GetWindowTextA(hDlg, szText, RTL_NUMBER_OF(szText) - 1);
  186. _snprintf( szTitle, RTL_NUMBER_OF(szTitle) - 1, "%s : %s", szText, TranslatePointer( pHeader->pszExeName ) );
  187. SetWindowTextA(hDlg, szTitle);
  188. }
  189. return(TRUE);
  190. }
  191. int
  192. ListBoxNotify(
  193. HWND hDlg,
  194. WPARAM wParam,
  195. LPARAM lParam)
  196. {
  197. LRESULT i;
  198. PDebugModule pModule;
  199. char Total[16];
  200. if (HIWORD(wParam) == LBN_SELCHANGE)
  201. {
  202. if (!GetWindowLongPtr(hDlg, GWLP_USERDATA))
  203. {
  204. ShowWindow(GetDlgItem(hDlg, IDD_MODULE_TEXT), SW_NORMAL);
  205. ShowWindow(GetDlgItem(hDlg, IDD_MODULE_OUTPUT), SW_NORMAL);
  206. }
  207. i = SendMessage(GetDlgItem(hDlg, IDD_DEBUG_LB), LB_GETCURSEL, 0, 0);
  208. pModule = (PDebugModule) SendMessage(GetDlgItem(hDlg, IDD_DEBUG_LB),
  209. LB_GETITEMDATA, (WPARAM) i, 0);
  210. RenameKeys(hDlg, pModule);
  211. sprintf(Total, "%d bytes", pModule->TotalOutput);
  212. SetDlgItemTextA(hDlg, IDD_MODULE_OUTPUT, Total);
  213. SetWindowLongPtr(hDlg, GWLP_USERDATA, (LPARAM) pModule);
  214. }
  215. return(TRUE);
  216. }
  217. HandleCheck(
  218. HWND hDlg,
  219. WPARAM wParam,
  220. LPARAM lParam)
  221. {
  222. int bit;
  223. PDebugModule pModule;
  224. pModule = (PDebugModule) GetWindowLongPtr(hDlg, GWLP_USERDATA);
  225. bit = 1 << (LOWORD(wParam) - IDD_CHECK_0);
  226. if (IsDlgButtonChecked(hDlg, LOWORD(wParam)))
  227. {
  228. pModule->InfoLevel |= bit;
  229. }
  230. else
  231. {
  232. pModule->InfoLevel&= ~(bit);
  233. }
  234. pModule->fModule |= DEBUGMOD_CHANGE_INFOLEVEL;
  235. return(0);
  236. }
  237. LRESULT
  238. CALLBACK
  239. DialogProc(
  240. HWND hDlg,
  241. UINT Message,
  242. WPARAM wParam,
  243. LPARAM lParam)
  244. {
  245. char Total[16];
  246. switch (Message)
  247. {
  248. case WM_INITDIALOG:
  249. return(InitDialog(hDlg));
  250. case WM_COMMAND:
  251. sprintf(Total, "%d bytes", pHeader->TotalWritten);
  252. SetDlgItemTextA(hDlg, IDD_TOTAL_OUTPUT, Total);
  253. switch (LOWORD(wParam))
  254. {
  255. case IDOK:
  256. EndDialog(hDlg, IDOK);
  257. return(TRUE);
  258. case IDCANCEL:
  259. EndDialog(hDlg, IDCANCEL);
  260. return(TRUE);
  261. case IDD_DEBUG_LB:
  262. return(ListBoxNotify(hDlg, wParam, lParam));
  263. }
  264. if ((LOWORD(wParam) >= IDD_CHECK_0) &&
  265. (LOWORD(wParam) <= IDD_CHECK_31))
  266. {
  267. HandleCheck(hDlg, wParam, lParam);
  268. }
  269. return(TRUE);
  270. }
  271. return(FALSE);
  272. }
  273. int
  274. ErrorMessage(
  275. HWND hWnd,
  276. PWSTR pszTitleBar,
  277. DWORD Buttons)
  278. {
  279. WCHAR szMessage[256];
  280. FormatMessage(
  281. FORMAT_MESSAGE_FROM_SYSTEM,
  282. NULL, // ignored
  283. (GetLastError()), // message id
  284. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), // message language
  285. szMessage, // address of buffer pointer
  286. 199, // minimum buffer size
  287. NULL ); // no other arguments
  288. return(MessageBox(hWnd, szMessage, pszTitleBar, Buttons));
  289. }
  290. int WINAPI WinMain(
  291. HINSTANCE hInstance,
  292. HINSTANCE hPrevInstance,
  293. LPSTR lpszCmdParam,
  294. int nCmdShow)
  295. {
  296. int pid;
  297. LRESULT Status ;
  298. pid = strtol(lpszCmdParam, NULL, 0);
  299. pHeader = MapDebugMemory(pid);
  300. if (!pHeader)
  301. {
  302. ErrorMessage(NULL, TEXT("Map Debug Memory"), MB_OK | MB_ICONSTOP);
  303. return(0);
  304. }
  305. Status = DialogBox( hInstance,
  306. MAKEINTRESOURCE(IDD_DEBUG_TOOL),
  307. GetDesktopWindow(),
  308. DialogProc);
  309. if ( Status < 0 )
  310. {
  311. ErrorMessage(NULL, TEXT("DialogBox"), MB_OK | MB_ICONSTOP);
  312. }
  313. return(0);
  314. }