DebugClient.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. unit DebugClient;
  2. {
  3. Inno Setup
  4. Copyright (C) 1997-2019 Jordan Russell
  5. Portions by Martijn Laan
  6. For conditions of distribution and use, see LICENSE.TXT.
  7. Debug info stuff
  8. }
  9. interface
  10. uses
  11. Windows, SysUtils, Messages, DebugStruct;
  12. var
  13. Debugging: Boolean;
  14. DebugClientCompiledCodeText: AnsiString;
  15. DebugClientCompiledCodeDebugInfo: AnsiString;
  16. type
  17. TDebugNotifyGetCallStack = function(var CallStackCount: Cardinal): String of object;
  18. function DebugNotify(Kind: TDebugEntryKind; Index: Integer;
  19. var ADebugContinueStepOver: Boolean; const GetCallStack: TDebugNotifyGetCallStack = nil): Boolean;
  20. procedure DebugNotifyException(Exception: String; Kind: TDebugEntryKind; Index: Integer);
  21. function DebugNotifyIntermediate(Kind: TDebugEntryKind; Index: Integer;
  22. var ADebugContinueStepOver: Boolean): Boolean;
  23. procedure DebugNotifyLogMessage(const Msg: String);
  24. procedure DebugNotifyTempDir(const Dir: String);
  25. procedure DebugNotifyUninstExe(UninstExe: String);
  26. procedure EndDebug;
  27. procedure SetDebugWnd(Wnd: HWND; WantCodeText: Boolean);
  28. implementation
  29. uses
  30. Forms, Classes, CmnFunc2, Struct, InstFunc, Main;
  31. type
  32. TDummyClass = class
  33. private
  34. class procedure DebugClientWndProc(var Message: TMessage);
  35. end;
  36. var
  37. DebugWnd: HWND;
  38. DebugClientWnd: HWND;
  39. DebugContinue: Boolean;
  40. DebugContinueStepOver: Boolean;
  41. procedure SetDebugWnd(Wnd: HWND; WantCodeText: Boolean);
  42. var
  43. DebuggerVersion: Cardinal;
  44. I: Integer;
  45. begin
  46. { First, verify that the debugger/compiler is the same version as Setup.
  47. A mismatch is possible when debugging an uninstaller if the uninstaller
  48. EXE was created by an installer built with a later version of IS. We can't
  49. continue in such a case because the debugger would send over updated
  50. "compiled code text" that is incompatible with this version of Setup. }
  51. DebuggerVersion := SendMessage(Wnd, WM_Debugger_QueryVersion, 0, 0);
  52. if DebuggerVersion <> SetupBinVersion then
  53. raise Exception.CreateFmt('Cannot debug. Debugger version ($%.8x) does ' +
  54. 'not match Setup version ($%.8x)', [DebuggerVersion, SetupBinVersion]);
  55. Debugging := True;
  56. DebugWnd := Wnd;
  57. DebugClientWnd := AllocateHWnd(TDummyClass.DebugClientWndProc);
  58. if DebugClientWnd = 0 then
  59. InternalError('Failed to create DebugClientWnd');
  60. { On Vista, unprivileged processes can't send messages to elevated processes
  61. by default. Allow the debugger (which normally runs unprivileged) to send
  62. messages to us. }
  63. for I := Low(DebugClientMessages) to High(DebugClientMessages) do
  64. AddToWindowMessageFilterEx(DebugClientWnd, DebugClientMessages[I]);
  65. SendMessage(DebugWnd, WM_Debugger_Hello, WPARAM(DebugClientWnd), LPARAM(WantCodeText));
  66. end;
  67. procedure EndDebug;
  68. begin
  69. Debugging := False;
  70. if DebugWnd <> 0 then begin
  71. SendMessage(DebugWnd, WM_Debugger_Goodbye, 0, 0);
  72. DebugWnd := 0;
  73. end;
  74. if DebugClientWnd <> 0 then begin
  75. DeallocateHWnd(DebugClientWnd);
  76. DebugClientWnd := 0;
  77. end;
  78. end;
  79. function InternalDebugNotify(DebuggerMsg: UINT; Kind: TDebugEntryKind;
  80. Index: Integer; var ADebugContinueStepOver: Boolean;
  81. const GetCallStack: TDebugNotifyGetCallStack = nil; const GetCallStackData: Pointer = nil): Boolean;
  82. { Returns True if the debugger paused. ADebugContinueStepOver is set to True
  83. if the debugger paused and the user resumed via Step Over, False otherwise. }
  84. var
  85. SaveAppTitle, CallStack: String;
  86. WindowList: Pointer;
  87. Msg: TMsg;
  88. TopWindow: HWND;
  89. CallStackCount: Cardinal;
  90. begin
  91. Result := False;
  92. ADebugContinueStepOver := False;
  93. if not Debugging then
  94. Exit;
  95. DebugContinue := False;
  96. if SendMessage(DebugWnd, DebuggerMsg, Ord(Kind), Index) = 0 then begin
  97. { Don't pause }
  98. Exit;
  99. end;
  100. if Assigned(GetCallStack) then begin
  101. CallStack := GetCallStack(CallStackCount);
  102. SendMessage(DebugWnd, WM_Debugger_CallStackCount, CallStackCount, 0);
  103. SendCopyDataMessageStr(DebugWnd, DebugClientWnd, CD_Debugger_CallStackW, CallStack);
  104. end;
  105. Result := True;
  106. { Wait until we get clearance to continue }
  107. SaveAppTitle := Application.Title;
  108. WindowList := DisableTaskWindows(0);
  109. try
  110. Application.Title := '[Paused] ' + SaveAppTitle;
  111. while not DebugContinue do begin
  112. case Integer(GetMessage(Msg, 0, 0, 0)) of
  113. -1: Break; { if GetMessage failed }
  114. 0: begin
  115. { Repost WM_QUIT messages }
  116. PostQuitMessage(Msg.WParam);
  117. Break;
  118. end;
  119. end;
  120. TranslateMessage(Msg);
  121. DispatchMessage(Msg);
  122. end;
  123. ADebugContinueStepOver := DebugContinueStepOver;
  124. finally
  125. EnableTaskWindows(WindowList);
  126. Application.Title := SaveAppTitle;
  127. end;
  128. { Bring us back to the foreground, unless we've been detached }
  129. if Debugging then begin
  130. TopWindow := GetThreadTopWindow;
  131. if TopWindow <> 0 then begin
  132. { First ask the debugger to call SetForegroundWindow() on our window. If
  133. we don't do this then Windows (98/2000+) will prevent our window from
  134. becoming activated if the debugger is currently in the foreground. }
  135. SendMessage(DebugWnd, WM_Debugger_SetForegroundWindow, WPARAM(TopWindow), 0);
  136. { Now call SetForegroundWindow() ourself. Why? When a remote thread
  137. calls SetForegroundWindow(), the request is queued; the window doesn't
  138. actually become active until the next time the window's thread checks
  139. the message queue. This call causes the window to become active
  140. immediately. }
  141. SetForegroundWindow(TopWindow);
  142. end;
  143. end;
  144. end;
  145. function DebugNotify(Kind: TDebugEntryKind; Index: Integer;
  146. var ADebugContinueStepOver: Boolean;
  147. const GetCallStack: TDebugNotifyGetCallStack = nil): Boolean;
  148. begin
  149. Result := InternalDebugNotify(WM_Debugger_Stepped, Kind, Index,
  150. ADebugContinueStepOver, GetCallStack);
  151. end;
  152. function DebugNotifyIntermediate(Kind: TDebugEntryKind; Index: Integer;
  153. var ADebugContinueStepOver: Boolean): Boolean;
  154. begin
  155. Result := InternalDebugNotify(WM_Debugger_SteppedIntermediate, Kind, Index,
  156. ADebugContinueStepOver, nil);
  157. end;
  158. procedure DebugNotifyException(Exception: String; Kind: TDebugEntryKind; Index: Integer);
  159. var
  160. B: Boolean;
  161. begin
  162. SendCopyDataMessageStr(DebugWnd, DebugClientWnd, CD_Debugger_ExceptionW,
  163. Exception);
  164. InternalDebugNotify(WM_Debugger_Exception, Kind, Index, B);
  165. end;
  166. procedure DebugNotifyTempDir(const Dir: String);
  167. begin
  168. SendCopyDataMessageStr(DebugWnd, DebugClientWnd, CD_Debugger_TempDirW, Dir);
  169. end;
  170. procedure DebugNotifyUninstExe(UninstExe: String);
  171. begin
  172. SendCopyDataMessageStr(DebugWnd, DebugClientWnd, CD_Debugger_UninstExeW, UninstExe);
  173. end;
  174. procedure DebugNotifyLogMessage(const Msg: String);
  175. begin
  176. SendCopyDataMessageStr(DebugWnd, DebugClientWnd, CD_Debugger_LogMessageW, Msg);
  177. end;
  178. class procedure TDummyClass.DebugClientWndProc(var Message: TMessage);
  179. var
  180. VariableDebugEntry: TVariableDebugEntry;
  181. EvaluateExp, EvaluateResult: String;
  182. begin
  183. try
  184. case Message.Msg of
  185. WM_DebugClient_Detach: begin
  186. Debugging := False;
  187. DebugWnd := 0;
  188. { If it's paused, force it to continue }
  189. DebugContinue := True;
  190. DebugContinueStepOver := False;
  191. { Make the GetMessage call in DebugNotify return immediately }
  192. PostMessage(0, 0, 0, 0);
  193. end;
  194. WM_DebugClient_Continue: begin
  195. DebugContinue := True;
  196. DebugContinueStepOver := Message.wParam = 1;
  197. { Make the GetMessage call in DebugNotify return immediately }
  198. PostMessage(0, 0, 0, 0);
  199. end;
  200. WM_DebugClient_SetForegroundWindow: begin
  201. SetForegroundWindow(HWND(Message.WParam));
  202. end;
  203. WM_COPYDATA: begin
  204. case TWMCopyData(Message).CopyDataStruct.dwData of
  205. CD_DebugClient_EvaluateConstantW: begin
  206. try
  207. SetString(EvaluateExp, PChar(TWMCopyData(Message).CopyDataStruct.lpData),
  208. TWMCopyData(Message).CopyDataStruct.cbData div SizeOf(Char));
  209. try
  210. Inc(DisableCodeConsts);
  211. try
  212. EvaluateResult := ExpandConst(EvaluateExp);
  213. finally
  214. Dec(DisableCodeConsts);
  215. end;
  216. Message.Result := 1;
  217. except
  218. EvaluateResult := GetExceptMessage;
  219. Message.Result := 2;
  220. end;
  221. SendCopyDataMessageStr(DebugWnd, DebugClientWnd, CD_Debugger_ReplyW,
  222. EvaluateResult);
  223. except
  224. { don't propagate exceptions }
  225. end;
  226. end;
  227. CD_DebugClient_EvaluateVariableEntry: begin
  228. try
  229. Move(TWMCopyData(Message).CopyDataStruct.lpData^, VariableDebugEntry, SizeOf(VariableDebugEntry));
  230. try
  231. if CodeRunner = nil then
  232. raise Exception.Create('Cannot evaluate variable because [Code] isn''t running yet');
  233. EvaluateResult := CodeRunner.EvaluateUsedVariable(VariableDebugEntry.Param1,
  234. VariableDebugEntry.Param2, VariableDebugEntry.Param3, VariableDebugEntry.Param4);
  235. Message.Result := 1;
  236. except
  237. EvaluateResult := GetExceptMessage;
  238. Message.Result := 2;
  239. end;
  240. SendCopyDataMessageStr(DebugWnd, DebugClientWnd, CD_Debugger_ReplyW,
  241. EvaluateResult);
  242. except
  243. { don't propagate exceptions }
  244. end;
  245. end;
  246. CD_DebugClient_CompiledCodeTextA: begin
  247. try
  248. DebugClientCompiledCodeText := '';
  249. SetString(DebugClientCompiledCodeText, PAnsiChar(TWMCopyData(Message).CopyDataStruct.lpData),
  250. TWMCopyData(Message).CopyDataStruct.cbData div SizeOf(AnsiChar));
  251. Message.Result := 1;
  252. except
  253. { don't propagate exceptions }
  254. end;
  255. end;
  256. CD_DebugClient_CompiledCodeDebugInfoA: begin
  257. try
  258. DebugClientCompiledCodeDebugInfo := '';
  259. SetString(DebugClientCompiledCodeDebugInfo, PAnsiChar(TWMCopyData(Message).CopyDataStruct.lpData),
  260. TWMCopyData(Message).CopyDataStruct.cbData div SizeOf(AnsiChar));
  261. Message.Result := 1;
  262. except
  263. { don't propagate exceptions }
  264. end;
  265. end;
  266. end;
  267. end;
  268. else
  269. with Message do
  270. Result := DefWindowProc(DebugClientWnd, Msg, WParam, LParam);
  271. end;
  272. except
  273. Application.HandleException(nil);
  274. end
  275. end;
  276. end.