Windows NT 4.0 source code leak
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.

412 lines
11 KiB

4 years ago
  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. callmon.c
  5. Abstract:
  6. USAGE: callmon [callmon switches] command-line-of-application
  7. Author:
  8. Mark Lucovsky (markl) 26-Jan-1995
  9. --*/
  10. #include "callmonp.h"
  11. int
  12. WINAPI
  13. WinMain(
  14. HINSTANCE hInstance,
  15. HINSTANCE hPrevInstance,
  16. LPSTR lpCmdLine,
  17. int nShowCmd
  18. )
  19. {
  20. CHAR Line[256];
  21. int i;
  22. InitializeCallmon();
  23. exit(0);
  24. return 0;
  25. }
  26. int _CRTAPI1
  27. ulcomp(const void *e1,const void *e2)
  28. {
  29. PBREAKPOINT_INFO p1;
  30. PBREAKPOINT_INFO p2;
  31. p1 = (*(PBREAKPOINT_INFO *)e1);
  32. p2 = (*(PBREAKPOINT_INFO *)e2);
  33. if ( p1 && p2 ) {
  34. return (p2->ApiCount - p1->ApiCount);
  35. }
  36. else {
  37. return 1;
  38. }
  39. }
  40. VOID
  41. ProcessCallmonData(
  42. VOID
  43. )
  44. {
  45. CHAR OutputBuffer[ 512 ];
  46. UCHAR LastKey;
  47. LONG ScrollDelta;
  48. WORD DisplayLine, LastDetailRow;
  49. int i;
  50. DWORD WaitRet;
  51. if ( !fBreakPointsInitialized ) {
  52. return;
  53. }
  54. sprintf(OutputBuffer,"%dms",GetTickCount() - BaseTime);
  55. SetWindowText(GetDlgItem(hwndDlg, ID_TOTAL_MS),OutputBuffer);
  56. if ( !NewCallmonData ) {
  57. return;
  58. }
  59. sprintf(OutputBuffer,"%dms",GetTickCount() - StartingTick);
  60. SetWindowText(GetDlgItem(hwndDlg, ID_INTERVAL_MS),OutputBuffer);
  61. NewCallmonData = FALSE;
  62. SendMessage(hwndOutput, WM_SETREDRAW, FALSE, 0);
  63. SendMessage(hwndOutput, LB_RESETCONTENT, 0L, 0L);
  64. qsort((void *)RegisteredBreakpoints,(size_t)NumberOfBreakpoints,(size_t)sizeof(PBREAKPOINT_INFO),ulcomp);
  65. for(i=0;i<NumberOfBreakpoints;i++) {
  66. if (RegisteredBreakpoints[i]) {
  67. if ( RegisteredBreakpoints[i]->ApiCount ) {
  68. sprintf(
  69. OutputBuffer,
  70. "%d\t%s",
  71. RegisteredBreakpoints[i]->ApiCount,
  72. RegisteredBreakpoints[i]->ApiName
  73. );
  74. SendMessage(hwndOutput,LB_ADDSTRING, 0L, (LPARAM)OutputBuffer);
  75. }
  76. }
  77. }
  78. SendMessage(hwndOutput, WM_SETREDRAW, TRUE, 0);
  79. sprintf(OutputBuffer,"%d",TotalBreakPoints);
  80. SetWindowText(GetDlgItem(hwndDlg, ID_TOTAL_API),OutputBuffer);
  81. sprintf(OutputBuffer,"%d",RunningBreakPoints);
  82. SetWindowText(GetDlgItem(hwndDlg, ID_INTERVAL_API),OutputBuffer);
  83. }
  84. BOOL
  85. CALLBACK
  86. CallmonDlgProc(
  87. HWND hDlg,
  88. UINT uMsg,
  89. WPARAM wParam,
  90. LPARAM lParam
  91. )
  92. {
  93. BOOL ReturnValue;
  94. ReturnValue = TRUE;
  95. hwndDlg = hDlg;
  96. switch (uMsg) {
  97. HANDLE_MSG(hDlg, WM_INITDIALOG, CallmonDlgInit);
  98. HANDLE_MSG(hDlg, WM_COMMAND, CallmonDlgCommand);
  99. HANDLE_MSG(hDlg, WM_TIMER, CallmonDlgTimer);
  100. default:
  101. ReturnValue = FALSE;
  102. break;
  103. }
  104. return ReturnValue;
  105. }
  106. BOOL
  107. CallmonDlgInit(
  108. HWND hwnd,
  109. HWND hwndFocus,
  110. LPARAM lParam
  111. )
  112. {
  113. HANDLE Thread;
  114. DWORD ThreadId;
  115. CHAR OutputBuffer[ 512 ];
  116. //
  117. // Get output window handle
  118. //
  119. hwndOutput = GetDlgItem(hwnd, ID_OUTPUT);
  120. if ( fNtDll ) {
  121. CheckDlgButton(hwndDlg,ID_NTDLL,1);
  122. }
  123. if ( fGdi32 ) {
  124. CheckDlgButton(hwndDlg,ID_GDI32,1);
  125. }
  126. if ( fKernel32 ) {
  127. CheckDlgButton(hwndDlg,ID_KERNEL32,1);
  128. }
  129. if ( fWsock32 ) {
  130. CheckDlgButton(hwndDlg,ID_WSOCK32,1);
  131. }
  132. if ( fUser32 ) {
  133. CheckDlgButton(hwndDlg,ID_USER32,1);
  134. }
  135. if ( fOle32 ) {
  136. CheckDlgButton(hwndDlg,ID_OLE32,1);
  137. }
  138. EnableWindow(GetDlgItem(hwnd, ID_KERNEL32),FALSE);
  139. EnableWindow(GetDlgItem(hwnd, ID_WSOCK32),FALSE);
  140. EnableWindow(GetDlgItem(hwnd, ID_USER32),FALSE);
  141. EnableWindow(GetDlgItem(hwnd, ID_GDI32),FALSE);
  142. EnableWindow(GetDlgItem(hwnd, ID_OLE32),FALSE);
  143. EnableWindow(GetDlgItem(hwnd, ID_NTDLL),FALSE);
  144. EnableWindow(GetDlgItem(hwnd, ID_START),FALSE);
  145. EnableWindow(GetDlgItem(hwnd, ID_BREAKCONTROL),FALSE);
  146. EnableWindow(GetDlgItem(hwnd, ID_CLEAR),FALSE);
  147. EnableWindow(GetDlgItem(hwnd, ID_STARTBUTTON),FALSE);
  148. EnableWindow(GetDlgItem(hwnd, ID_LOG),FALSE);
  149. if ( fBreakPointsValid ) {
  150. SetWindowText(GetDlgItem(hwnd, ID_BREAKCONTROL),"Disable Breakpoints");
  151. }
  152. else {
  153. SetWindowText(GetDlgItem(hwnd, ID_BREAKCONTROL),"Enable Breakpoints");
  154. }
  155. BaseTime = GetTickCount();
  156. sprintf(OutputBuffer,"Api Call Monitor - %s",RestOfCommandLine);
  157. SetWindowText(hwnd,OutputBuffer);
  158. Thread = CreateThread(NULL,0L,(PVOID)DebuggerThread,NULL,0,&ThreadId);
  159. if ( !Thread ) {
  160. return FALSE;
  161. }
  162. CloseHandle(Thread);
  163. return(TRUE);
  164. }
  165. void
  166. CallmonDlgCommand (
  167. HWND hwnd,
  168. int id,
  169. HWND hwndCtl,
  170. UINT codeNotify
  171. )
  172. {
  173. CHAR OutputBuffer[ 512 ];
  174. int i;
  175. switch (id) {
  176. case ID_STARTBUTTON:
  177. case IDOK:
  178. if ( !DebugeeActive && !fExiting ) {
  179. SendMessage(hwndOutput, LB_RESETCONTENT, 0L, 0L);
  180. EnableWindow(GetDlgItem(hwnd, ID_KERNEL32),FALSE);
  181. EnableWindow(GetDlgItem(hwnd, ID_WSOCK32),FALSE);
  182. EnableWindow(GetDlgItem(hwnd, ID_USER32),FALSE);
  183. EnableWindow(GetDlgItem(hwnd, ID_GDI32),FALSE);
  184. EnableWindow(GetDlgItem(hwnd, ID_OLE32),FALSE);
  185. EnableWindow(GetDlgItem(hwnd, ID_NTDLL),FALSE);
  186. fKernel32 = IsDlgButtonChecked(hwnd, ID_KERNEL32);
  187. fWsock32 = IsDlgButtonChecked(hwnd, ID_WSOCK32);
  188. fUser32 = IsDlgButtonChecked(hwnd, ID_USER32);
  189. fGdi32 = IsDlgButtonChecked(hwnd, ID_GDI32);
  190. fOle32 = IsDlgButtonChecked(hwnd, ID_OLE32);
  191. fNtDll = IsDlgButtonChecked(hwnd, ID_NTDLL);
  192. SetWindowText(GetDlgItem(hwnd, ID_STARTBUTTON),"Stop");
  193. Timer = SetTimer(hwnd,1,1000,NULL);
  194. DebugeeActive = TRUE;
  195. SetEvent(ReleaseDebugeeEvent);
  196. }
  197. else {
  198. KillTimer(hwnd,Timer);
  199. EnableWindow(GetDlgItem(hwnd, ID_KERNEL32),FALSE);
  200. EnableWindow(GetDlgItem(hwnd, ID_WSOCK32),FALSE);
  201. EnableWindow(GetDlgItem(hwnd, ID_USER32),FALSE);
  202. EnableWindow(GetDlgItem(hwnd, ID_GDI32),FALSE);
  203. EnableWindow(GetDlgItem(hwnd, ID_OLE32),FALSE);
  204. EnableWindow(GetDlgItem(hwnd, ID_NTDLL),FALSE);
  205. EnableWindow(GetDlgItem(hwnd, ID_START),FALSE);
  206. EnableWindow(GetDlgItem(hwnd, ID_BREAKCONTROL),FALSE);
  207. EnableWindow(GetDlgItem(hwnd, ID_CLEAR),FALSE);
  208. EnableWindow(GetDlgItem(hwnd, ID_STARTBUTTON),FALSE);
  209. }
  210. break;
  211. case ID_CLEAR:
  212. //
  213. // Clear the Api counts
  214. //
  215. SendMessage(hwndOutput, LB_RESETCONTENT, 0L, 0L);
  216. for(i=0;i<NumberOfBreakpoints;i++) {
  217. if (RegisteredBreakpoints[i]) {
  218. RegisteredBreakpoints[i]->ApiCount = 0;
  219. }
  220. }
  221. StartingTick = GetTickCount();
  222. RunningBreakPoints = 0;
  223. sprintf(OutputBuffer,"%d",TotalBreakPoints);
  224. SetWindowText(GetDlgItem(hwndDlg, ID_TOTAL_API),OutputBuffer);
  225. sprintf(OutputBuffer,"%d",RunningBreakPoints);
  226. SetWindowText(GetDlgItem(hwndDlg, ID_INTERVAL_API),OutputBuffer);
  227. sprintf(OutputBuffer,"%dms",GetTickCount() - BaseTime);
  228. SetWindowText(GetDlgItem(hwndDlg, ID_TOTAL_MS),OutputBuffer);
  229. SetWindowText(GetDlgItem(hwndDlg, ID_INTERVAL_MS),"0ms");
  230. if ( LogFile ) {
  231. goto dolog;
  232. }
  233. break;
  234. case ID_LOG:
  235. if ( !LogFile ) {
  236. LogFile = fopen("Callmon.log","wt");
  237. if ( !LogFile ) {
  238. EnableWindow(GetDlgItem(hwndDlg, ID_LOG),FALSE);
  239. return;
  240. }
  241. }
  242. dolog:
  243. fprintf(LogFile,"\n Total APIs %d Running %d Time %dms (Since last clear %dms) \n\n",
  244. TotalBreakPoints,
  245. RunningBreakPoints,
  246. GetTickCount() - BaseTime,
  247. GetTickCount() - StartingTick
  248. );
  249. qsort((void *)RegisteredBreakpoints,(size_t)NumberOfBreakpoints,(size_t)sizeof(PBREAKPOINT_INFO),ulcomp);
  250. for(i=0;i<NumberOfBreakpoints;i++) {
  251. if (RegisteredBreakpoints[i]) {
  252. if ( RegisteredBreakpoints[i]->ApiCount ) {
  253. sprintf(
  254. OutputBuffer,
  255. "( %8d ) %8d %s\n",
  256. RegisteredBreakpoints[i]->TotalApiCount,
  257. RegisteredBreakpoints[i]->ApiCount,
  258. RegisteredBreakpoints[i]->ApiName
  259. );
  260. fprintf(LogFile,"%s",OutputBuffer);
  261. }
  262. }
  263. }
  264. break;
  265. case ID_BREAKCONTROL:
  266. if ( fBreakPointsValid ) {
  267. SetWindowText(GetDlgItem(hwnd, ID_BREAKCONTROL),"Enable Breakpoints");
  268. }
  269. else {
  270. SetWindowText(GetDlgItem(hwnd, ID_BREAKCONTROL),"Disable Breakpoints");
  271. }
  272. if ( !fBreakPointsInitialized ) {
  273. if ( fBreakPointsValid ) {
  274. fBreakPointsValid = FALSE;
  275. }
  276. else {
  277. fBreakPointsValid = TRUE;
  278. }
  279. return;
  280. }
  281. EnterCriticalSection(&BreakTable);
  282. //
  283. // if breakpoints are enabled, then disable them all
  284. // otherwise enable them all
  285. //
  286. EndingTick = GetTickCount();
  287. if ( fBreakPointsValid ) {
  288. RunningBreakPoints = 0;
  289. }
  290. for(i=0;i<NumberOfBreakpoints;i++) {
  291. if (RegisteredBreakpoints[i]) {
  292. //
  293. // If breakpoints are currently valid, then
  294. // disable them
  295. //
  296. if ( fBreakPointsValid ) {
  297. RemoveBreakpoint(TheProcess,RegisteredBreakpoints[i]);
  298. }
  299. else {
  300. InstallBreakpoint(TheProcess,RegisteredBreakpoints[i]);
  301. }
  302. }
  303. }
  304. NewCallmonData = TRUE;
  305. ProcessCallmonData();
  306. if ( fBreakPointsValid ) {
  307. fBreakPointsValid = FALSE;
  308. }
  309. else {
  310. fBreakPointsValid = TRUE;
  311. }
  312. LeaveCriticalSection(&BreakTable);
  313. break;
  314. case IDCANCEL:
  315. EndDialog(hwnd, id);
  316. break;
  317. }
  318. return;
  319. }
  320. void
  321. CallmonDlgTimer (
  322. HWND hwnd,
  323. UINT wParam
  324. )
  325. {
  326. ProcessCallmonData();
  327. }