Setup.DebugClient.pas 11 KB

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