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.

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