DebugClient.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. unit 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 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. { 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(DebugWnd, WM_Debugger_Hello, WPARAM(DebugClientWnd), LPARAM(WantCodeText));
  65. end;
  66. procedure EndDebug;
  67. begin
  68. Debugging := False;
  69. if DebugWnd <> 0 then begin
  70. SendMessage(DebugWnd, WM_Debugger_Goodbye, 0, 0);
  71. DebugWnd := 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(DebugWnd, 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(DebugWnd, WM_Debugger_CallStackCount, CallStackCount, 0);
  102. SendCopyDataMessageStr(DebugWnd, 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 begin
  131. { First ask the debugger to call SetForegroundWindow() on our window. If
  132. we don't do this then Windows (98/2000+) will prevent our window from
  133. becoming activated if the debugger is currently in the foreground. }
  134. SendMessage(DebugWnd, WM_Debugger_SetForegroundWindow, WPARAM(TopWindow), 0);
  135. { Now call SetForegroundWindow() ourself. Why? When a remote thread
  136. calls SetForegroundWindow(), the request is queued; the window doesn't
  137. actually become active until the next time the window's thread checks
  138. the message queue. This call causes the window to become active
  139. immediately. }
  140. SetForegroundWindow(TopWindow);
  141. end;
  142. end;
  143. end;
  144. function DebugNotify(Kind: TDebugEntryKind; Index: Integer;
  145. var ADebugContinueStepOver: Boolean;
  146. const GetCallStack: TDebugNotifyGetCallStack = nil): Boolean;
  147. begin
  148. Result := InternalDebugNotify(WM_Debugger_Stepped, Kind, Index,
  149. ADebugContinueStepOver, GetCallStack);
  150. end;
  151. function DebugNotifyIntermediate(Kind: TDebugEntryKind; Index: Integer;
  152. var ADebugContinueStepOver: Boolean): Boolean;
  153. begin
  154. Result := InternalDebugNotify(WM_Debugger_SteppedIntermediate, Kind, Index,
  155. ADebugContinueStepOver, nil);
  156. end;
  157. procedure DebugNotifyException(Exception: String; Kind: TDebugEntryKind; Index: Integer);
  158. var
  159. B: Boolean;
  160. begin
  161. SendCopyDataMessageStr(DebugWnd, DebugClientWnd, CD_Debugger_ExceptionW,
  162. Exception);
  163. InternalDebugNotify(WM_Debugger_Exception, Kind, Index, B);
  164. end;
  165. procedure DebugNotifyTempDir(const Dir: String);
  166. begin
  167. SendCopyDataMessageStr(DebugWnd, DebugClientWnd, CD_Debugger_TempDirW, Dir);
  168. end;
  169. procedure DebugNotifyUninstExe(UninstExe: String);
  170. begin
  171. SendCopyDataMessageStr(DebugWnd, DebugClientWnd, CD_Debugger_UninstExeW, UninstExe);
  172. end;
  173. procedure DebugNotifyLogMessage(const Msg: String);
  174. begin
  175. SendCopyDataMessageStr(DebugWnd, DebugClientWnd, CD_Debugger_LogMessageW, Msg);
  176. end;
  177. class procedure TDummyClass.DebugClientWndProc(var Message: TMessage);
  178. var
  179. VariableDebugEntry: TVariableDebugEntry;
  180. EvaluateExp, EvaluateResult: String;
  181. begin
  182. try
  183. case Message.Msg of
  184. WM_DebugClient_Detach: begin
  185. Debugging := False;
  186. DebugWnd := 0;
  187. { If it's paused, force it to continue }
  188. DebugContinue := True;
  189. DebugContinueStepOver := False;
  190. { Make the GetMessage call in DebugNotify return immediately }
  191. PostMessage(0, 0, 0, 0);
  192. end;
  193. WM_DebugClient_Continue: begin
  194. DebugContinue := True;
  195. DebugContinueStepOver := Message.wParam = 1;
  196. { Make the GetMessage call in DebugNotify return immediately }
  197. PostMessage(0, 0, 0, 0);
  198. end;
  199. WM_DebugClient_SetForegroundWindow: begin
  200. SetForegroundWindow(HWND(Message.WParam));
  201. end;
  202. WM_COPYDATA: begin
  203. case TWMCopyData(Message).CopyDataStruct.dwData of
  204. CD_DebugClient_EvaluateConstantW: begin
  205. try
  206. SetString(EvaluateExp, PChar(TWMCopyData(Message).CopyDataStruct.lpData),
  207. TWMCopyData(Message).CopyDataStruct.cbData div SizeOf(Char));
  208. try
  209. Inc(DisableCodeConsts);
  210. try
  211. EvaluateResult := ExpandConst(EvaluateExp);
  212. finally
  213. Dec(DisableCodeConsts);
  214. end;
  215. Message.Result := 1;
  216. except
  217. EvaluateResult := GetExceptMessage;
  218. Message.Result := 2;
  219. end;
  220. SendCopyDataMessageStr(DebugWnd, DebugClientWnd, CD_Debugger_ReplyW,
  221. EvaluateResult);
  222. except
  223. { don't propagate exceptions }
  224. end;
  225. end;
  226. CD_DebugClient_EvaluateVariableEntry: begin
  227. try
  228. Move(TWMCopyData(Message).CopyDataStruct.lpData^, VariableDebugEntry, SizeOf(VariableDebugEntry));
  229. try
  230. if CodeRunner = nil then
  231. raise Exception.Create('Cannot evaluate variable because [Code] isn''t running yet');
  232. EvaluateResult := CodeRunner.EvaluateUsedVariable(VariableDebugEntry.Param1,
  233. VariableDebugEntry.Param2, VariableDebugEntry.Param3, VariableDebugEntry.Param4);
  234. Message.Result := 1;
  235. except
  236. EvaluateResult := GetExceptMessage;
  237. Message.Result := 2;
  238. end;
  239. SendCopyDataMessageStr(DebugWnd, DebugClientWnd, CD_Debugger_ReplyW,
  240. EvaluateResult);
  241. except
  242. { don't propagate exceptions }
  243. end;
  244. end;
  245. CD_DebugClient_CompiledCodeTextA: begin
  246. try
  247. DebugClientCompiledCodeText := '';
  248. SetString(DebugClientCompiledCodeText, PAnsiChar(TWMCopyData(Message).CopyDataStruct.lpData),
  249. TWMCopyData(Message).CopyDataStruct.cbData div SizeOf(AnsiChar));
  250. Message.Result := 1;
  251. except
  252. { don't propagate exceptions }
  253. end;
  254. end;
  255. CD_DebugClient_CompiledCodeDebugInfoA: begin
  256. try
  257. DebugClientCompiledCodeDebugInfo := '';
  258. SetString(DebugClientCompiledCodeDebugInfo, PAnsiChar(TWMCopyData(Message).CopyDataStruct.lpData),
  259. TWMCopyData(Message).CopyDataStruct.cbData div SizeOf(AnsiChar));
  260. Message.Result := 1;
  261. except
  262. { don't propagate exceptions }
  263. end;
  264. end;
  265. end;
  266. end;
  267. else
  268. with Message do
  269. Result := DefWindowProc(DebugClientWnd, Msg, WParam, LParam);
  270. end;
  271. except
  272. Application.HandleException(nil);
  273. end
  274. end;
  275. end.