dbugintf.pp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2003 by the Free Pascal development team
  5. User interface for debug server.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$ifdef fpc}
  13. {$mode objfpc}
  14. {$h+}
  15. {$endif}
  16. unit dbugintf;
  17. interface
  18. uses
  19. {$ifdef fpc}
  20. linux,
  21. {$else}
  22. Libc,
  23. {$endif}
  24. msgintf,
  25. classes,
  26. ssockets;
  27. Type
  28. TDebugLevel = (dlInformation,dlWarning,dlError);
  29. {$ifdef fpc}
  30. pid_t = longint;
  31. {$endif}
  32. procedure SendBoolean(const Identifier: string; const Value: Boolean);
  33. procedure SendDateTime(const Identifier: string; const Value: TDateTime);
  34. procedure SendDebugEx(const Msg: string; MType: TDebugLevel);
  35. procedure SendDebug(const Msg: string);
  36. procedure SendInteger(const Identifier: string; const Value: Integer);
  37. procedure SendMethodEnter(const MethodName: string);
  38. procedure SendMethodExit(const MethodName: string);
  39. procedure SendSeparator;
  40. procedure SendDebugFmt(const Msg: string; const Args: array of const);
  41. procedure SendDebugFmtEx(const Msg: string; const Args: array of const; MType: TDebugLevel);
  42. { low-level routines }
  43. procedure SendDebugMessage(Const Msg : TDebugMessage);
  44. function CreateDebugStream : TStream;
  45. function StartDebugServer : pid_t;
  46. Procedure InitDebugStream;
  47. Const
  48. SendError : String = '';
  49. ResourceString
  50. SProcessID = 'Process %d: %s';
  51. SEntering = '> Entering ';
  52. SExiting = '< Exiting ';
  53. SSeparator = '>-=-=-=-=-=-=-=-=-=-=-=-=-=-=-<';
  54. implementation
  55. Uses SysUtils,process;
  56. // UnixProcessUtils;
  57. Const
  58. DmtInformation = lctInformation;
  59. DmtWarning = lctWarning;
  60. DmtError = lctError;
  61. ErrorLevel : Array[TDebugLevel] of integer
  62. = (dmtInformation,dmtWarning,dmtError);
  63. Const
  64. DebugStream : TStream = nil;
  65. Procedure WriteMessage(S : TStream; Const Msg : TDebugMessage);
  66. Var
  67. MsgSize : Integer;
  68. begin
  69. S.WriteBuffer(Msg.MsgType,SizeOf(Integer));
  70. S.WriteBuffer(Msg.MsgTimeStamp,SizeOf(TDateTime));
  71. MsgSize:=Length(Msg.Msg);
  72. S.WriteBuffer(MsgSize,SizeOf(Integer));
  73. S.WriteBuffer(Msg.msg[1],MsgSize);
  74. end;
  75. procedure SendDebugMessage(Const Msg : TDebugMessage);
  76. begin
  77. try
  78. If DebugStream=Nil then
  79. begin
  80. InitDebugStream;
  81. end;
  82. WriteMessage(debugStream,Msg);
  83. except
  84. On E : Exception do
  85. SendError:=E.Message;
  86. end;
  87. end;
  88. procedure SendBoolean(const Identifier: string; const Value: Boolean);
  89. Const
  90. Booleans : Array[Boolean] of string = ('False','True');
  91. begin
  92. SendDebugFmt('%s = %s',[Identifier,Booleans[value]]);
  93. end;
  94. procedure SendDateTime(const Identifier: string; const Value: TDateTime);
  95. begin
  96. SendDebugFmt('%s = %s',[Identifier,DateTimeToStr(Value)]);
  97. end;
  98. procedure SendDebugEx(const Msg: string; MType: TDebugLevel);
  99. Var
  100. Mesg : TDebugMessage;
  101. begin
  102. Mesg.MsgTimeStamp:=Now;
  103. Mesg.MsgType:=ErrorLevel[MTYpe];
  104. Mesg.Msg:=Msg;
  105. SendDebugMessage(Mesg);
  106. end;
  107. procedure SendDebug(const Msg: string);
  108. Var
  109. Mesg : TDebugMessage;
  110. begin
  111. Mesg.MsgTimeStamp:=Now;
  112. Mesg.MsgType:=dmtInformation;
  113. Mesg.Msg:=Msg;
  114. SendDebugMessage(Mesg);
  115. end;
  116. procedure SendInteger(const Identifier: string; const Value: Integer);
  117. begin
  118. SendDebugFmt('%s = %d',[identifier,Value]);
  119. end;
  120. procedure SendMethodEnter(const MethodName: string);
  121. begin
  122. SendDebug(SEntering+MethodName);
  123. end;
  124. procedure SendMethodExit(const MethodName: string);
  125. begin
  126. SendDebug(SExiting+MethodName);
  127. end;
  128. procedure SendSeparator;
  129. begin
  130. SendDebug(SSeparator);
  131. end;
  132. procedure SendDebugFmt(const Msg: string; const Args: array of const);
  133. Var
  134. Mesg : TDebugMessage;
  135. begin
  136. Mesg.MsgTimeStamp:=Now;
  137. Mesg.MsgType:=dmtInformation;
  138. Mesg.Msg:=Format(Msg,Args);
  139. SendDebugMessage(Mesg);
  140. end;
  141. procedure SendDebugFmtEx(const Msg: string; const Args: array of const; MType: TDebugLevel);
  142. Var
  143. Mesg : TDebugMessage;
  144. begin
  145. Mesg.MsgTimeStamp:=Now;
  146. Mesg.MsgType:=ErrorLevel[mType];
  147. Mesg.Msg:=Format(Msg,Args);
  148. SendDebugMessage(Mesg);
  149. end;
  150. function StartDebugServer : pid_t;
  151. begin
  152. With TProcess.Create(Nil) do
  153. Try
  154. CommandLine:='debugserver';
  155. Execute;
  156. Finally
  157. Free;
  158. end;
  159. end;
  160. function CreateUnixDebugStream(SocketFile : String) : TStream;
  161. {$ifdef fpc}
  162. Var
  163. tv,tr : timespec;
  164. {$endif}
  165. begin
  166. If Not FileExists(DebugSocket) then
  167. begin
  168. StartDebugServer;
  169. {$ifndef fpc}
  170. sleep(1000);
  171. {$else}
  172. tv.tv_sec:=1;
  173. tv.tv_nsec:=0;
  174. nanosleep(tv,tr);
  175. {$endif}
  176. end;
  177. {$ifdef fpc}
  178. Result:=TUnixSocket.Create(SocketFile);
  179. {$else}
  180. Result:=TUnixSocket.CreateFromFile(SocketFile);
  181. {$endif}
  182. end;
  183. Function CreateInetDebugStream (HostName : String; Port : Word) : TStream;
  184. begin
  185. Result:=TInetSocket.Create(HostName,Port);
  186. end;
  187. function CreateDebugStream : TStream;
  188. Var
  189. Msg : TDebugMessage;
  190. begin
  191. Case DebugConnection of
  192. dcUnix : Result:=CreateUnixDebugStream(DebugSocket);
  193. dcInet : Result:=CreateInetDebugStream(DebugHostName,DebugPort);
  194. end;
  195. Msg.MsgType:=lctIdentify;
  196. Msg.MsgTimeStamp:=Now;
  197. Msg.Msg:=Format(SProcessID,[getPID,ExtractFileName(Paramstr(0))]);
  198. WriteMessage(REsult,Msg);
  199. end;
  200. procedure FreeDebugStream;
  201. Var i : Integer;
  202. begin
  203. If (DebugStream<>Nil) then
  204. try
  205. i:=-1;
  206. DebugStream.WriteBuffer(I,SizeOf(I));
  207. DebugStream.Free;
  208. except
  209. end;
  210. end;
  211. Procedure InitDebugStream;
  212. begin
  213. debugstream:=CreateDebugStream;
  214. end;
  215. Initialization
  216. Finalization
  217. FreeDebugStream;
  218. end.
  219. {
  220. $Log$
  221. Revision 1.1 2003-01-02 14:44:29 michael
  222. + Initial implementation
  223. }