sysutils.pp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Florian Klaempfl
  5. member of the Free Pascal development team
  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. unit sysutils;
  13. interface
  14. {$MODE objfpc}
  15. { force ansistrings }
  16. {$H+}
  17. uses
  18. {$ifdef linux}
  19. linux
  20. {$endif}
  21. {$ifdef win32}
  22. dos,windows
  23. {$endif}
  24. {$ifdef go32v1}
  25. go32,dos
  26. {$endif}
  27. {$ifdef go32v2}
  28. go32,dos
  29. {$endif}
  30. {$ifdef os2}
  31. doscalls,dos
  32. {$endif}
  33. ;
  34. type
  35. { some helpful data types }
  36. tprocedure = procedure;
  37. tfilename = string;
  38. longrec = packed record
  39. lo,hi : word;
  40. end;
  41. wordrec = packed record
  42. lo,hi : byte;
  43. end;
  44. TMethod = packed record
  45. Code, Data: Pointer;
  46. end;
  47. { exceptions }
  48. exception = class(TObject)
  49. private
  50. fmessage : string;
  51. fhelpcontext : longint;
  52. public
  53. constructor create(const msg : string);
  54. constructor createfmt(const msg : string; const args : array of const);
  55. constructor createres(ident : longint);
  56. { !!!! }
  57. property helpcontext : longint read fhelpcontext write fhelpcontext;
  58. property message : string read fmessage write fmessage;
  59. end;
  60. exceptclass = class of exception;
  61. { integer math exceptions }
  62. EInterror = Class(Exception);
  63. EDivByZero = Class(EIntError);
  64. ERangeError = Class(EIntError);
  65. EIntOverflow = Class(EIntError);
  66. { General math errors }
  67. EMathError = Class(Exception);
  68. EInvalidOp = Class(EMathError);
  69. EZeroDivide = Class(EMathError);
  70. EOverflow = Class(EMathError);
  71. EUnderflow = Class(EMathError);
  72. { Run-time and I/O Errors }
  73. EInOutError = class(Exception)
  74. public
  75. ErrorCode : Longint;
  76. end;
  77. EInvalidPointer = Class(Exception);
  78. EOutOfMemory = Class(Exception);
  79. EAccessViolation = Class(Exception);
  80. EInvalidCast = Class(Exception);
  81. { String conversion errors }
  82. EConvertError = class(Exception);
  83. { Other errors }
  84. EAbort = Class(Exception);
  85. EAbstractError = Class(Exception);
  86. EAssertionFailed = Class(Exception);
  87. { FileRec/TextRec }
  88. {$i filerec.inc}
  89. {$i textrec.inc}
  90. { Read internationalization settings }
  91. {$i sysinth.inc}
  92. { Read date & Time function declarations }
  93. {$i datih.inc}
  94. { Read String Handling functions declaration }
  95. {$i sysstrh.inc}
  96. { Read pchar handling functions declration }
  97. {$i syspchh.inc}
  98. { Read filename handling functions declaration }
  99. {$i finah.inc}
  100. { Read other file handling function declarations }
  101. {$i filutilh.inc}
  102. { Read disk function declarations }
  103. {$i diskh.inc}
  104. implementation
  105. { Read message string definitions }
  106. {
  107. Add a language with IFDEF LANG_NAME
  108. just befor the final ELSE. This way English will always be the default.
  109. }
  110. {$IFDEF LANG_GERMAN}
  111. {$i strg.inc} // Does not exist yet !!
  112. {$ELSE}
  113. {$i stre.inc}
  114. {$ENDIF}
  115. { Read filename handling functions implementation }
  116. {$i fina.inc}
  117. { Read String Handling functions implementation }
  118. {$i sysstr.inc}
  119. { Read other file handling function implementations }
  120. {$i filutil.inc}
  121. { Read disk function implementations }
  122. {$i disk.inc}
  123. { Read date & Time function implementations }
  124. {$i dati.inc}
  125. { Read pchar handling functions implementation }
  126. {$i syspch.inc}
  127. constructor exception.create(const msg : string);
  128. begin
  129. inherited create;
  130. fmessage:=msg;
  131. end;
  132. constructor exception.createfmt(const msg : string; const args : array of const);
  133. begin
  134. inherited create;
  135. fmessage:=Format(msg,args);
  136. end;
  137. constructor exception.createres(ident : longint);
  138. begin
  139. inherited create;
  140. {!!!!!}
  141. end;
  142. {$ifopt S+}
  143. {$define STACKCHECK_WAS_ON}
  144. {$S-}
  145. {$endif OPT S }
  146. Procedure CatchUnhandledException (Obj : TObject; Addr,Frame: Pointer);
  147. Var
  148. Message : String;
  149. begin
  150. Writeln(stdout,'An unhandled exception occurred at 0x',HexStr(Longint(Addr),8),' :');
  151. if Obj is exception then
  152. begin
  153. Message:=Exception(Obj).Message;
  154. Writeln(stdout,Message);
  155. end
  156. else
  157. Writeln(stdout,'Exception object ',Obj.ClassName,' is not of class Exception.');
  158. { to get a nice symify }
  159. Writeln(stdout,BackTraceStrFunc(Longint(Addr)));
  160. Dump_Stack(stdout,longint(frame));
  161. Writeln(stdout,'');
  162. Halt(217);
  163. end;
  164. Var OutOfMemory : EOutOfMemory;
  165. InValidPointer : EInvalidPointer;
  166. Procedure RunErrorToExcept (ErrNo : Longint; Address,Frame : Pointer);
  167. Var E : Exception;
  168. S : String;
  169. begin
  170. Case Errno of
  171. 1,203 : E:=OutOfMemory;
  172. 204 : E:=InvalidPointer;
  173. 2,3,4,5,6,100,101,102,103,105,106 : { I/O errors }
  174. begin
  175. Case Errno of
  176. 2 : S:=SFileNotFound;
  177. 3 : S:=SInvalidFileName;
  178. 4 : S:=STooManyOpenFiles;
  179. 5 : S:=SAccessDenied;
  180. 6 : S:=SInvalidFileHandle;
  181. 15 : S:=SInvalidDrive;
  182. 100 : S:=SEndOfFile;
  183. 101 : S:=SDiskFull;
  184. 102 : S:=SFileNotAssigned;
  185. 103 : S:=SFileNotOpen;
  186. 104 : S:=SFileNotOpenForInput;
  187. 105 : S:=SFileNotOpenForOutput;
  188. 106 : S:=SInvalidInput;
  189. end;
  190. E:=EinOutError.Create (S);
  191. EInoutError(E).ErrorCode:=IOresult; // Clears InOutRes !!
  192. end;
  193. // We don't set abstracterrorhandler, but we do it here.
  194. // Unless the use sets another handler we'll get here anyway...
  195. 200 : E:=EDivByZero.Create(SDivByZero);
  196. 201 : E:=ERangeError.Create(SRangeError);
  197. 205 : E:=EOverflow.Create(SOverflow);
  198. 206 : E:=EOverflow.Create(SUnderflow);
  199. 207 : E:=EInvalidOp.Create(SInvalidOp);
  200. 211 : E:=EAbstractError.Create(SAbstractError);
  201. 215 : E:=EIntOverflow.Create(SIntOverflow);
  202. 216 : E:=EAccessViolation.Create(SAccessViolation);
  203. 219 : E:=EInvalidCast.Create(SInvalidCast);
  204. 227 : E:=EAssertionFailed.Create(SAssertionFailed);
  205. else
  206. E:=Exception.CreateFmt (SUnKnownRunTimeError,[Errno]);
  207. end;
  208. Raise E at longint(Address){$ifdef ENHANCEDRAISE},longint(Frame){$endif};
  209. end;
  210. Procedure AssertErrorHandler (Const Msg,FN : ShortString;LineNo,TheAddr : Longint);
  211. Var
  212. S : String;
  213. begin
  214. If Msg='' then
  215. S:=SAssertionFailed
  216. else
  217. S:=Msg;
  218. Raise EAssertionFailed.Createfmt(SAssertError,[S,Fn,LineNo]); // at Pointer(theAddr);
  219. end;
  220. {$ifdef STACKCHECK_WAS_ON}
  221. {$S+}
  222. {$endif}
  223. Procedure InitExceptions;
  224. {
  225. Must install uncaught exception handler (ExceptProc)
  226. and install exceptions for system exceptions or signals.
  227. (e.g: SIGSEGV -> ESegFault or so.)
  228. }
  229. begin
  230. ExceptProc:=@CatchUnhandledException;
  231. // Create objects that may have problems when there is no memory.
  232. OutOfMemory:=EOutOfMemory.Create(SOutOfMemory);
  233. InvalidPointer:=EInvalidPointer.Create(SInvalidPointer);
  234. AssertErrorProc:=@AssertErrorHandler;
  235. ErrorProc:=@RunErrorToExcept;
  236. end;
  237. { Initialization code. }
  238. Initialization
  239. InitExceptions; { Initialize exceptions. OS independent }
  240. InitInternational; { Initialize internationalization settings }
  241. Finalization
  242. OutOfMemory.Free;
  243. InValidPointer.Free;
  244. end.
  245. {
  246. $Log$
  247. Revision 1.45 2000-04-24 13:34:29 peter
  248. * added enhancedraise define
  249. Revision 1.43 2000/03/30 13:54:15 pierre
  250. No stack check inside CatchUnhandledException
  251. Revision 1.42 2000/02/10 22:56:43 florian
  252. * quick hack for stack trace in the case of an unhandled exception
  253. Revision 1.41 2000/02/09 16:59:33 peter
  254. * truncated log
  255. Revision 1.40 2000/01/16 19:10:25 hajny
  256. * 'uses Dos' added for OS/2 target
  257. Revision 1.39 2000/01/07 16:41:44 daniel
  258. * copyright 2000
  259. Revision 1.38 1999/12/26 19:30:53 hajny
  260. * OS/2 target added to the uses clause
  261. Revision 1.36 1999/11/15 21:49:47 peter
  262. * exception address fixes
  263. Revision 1.35 1999/11/06 14:41:31 peter
  264. * truncated log
  265. Revision 1.34 1999/10/30 17:39:05 peter
  266. * memorymanager expanded with allocmem/reallocmem
  267. Revision 1.33 1999/10/26 12:29:07 peter
  268. * assert handler must use shortstring
  269. Revision 1.32 1999/09/15 20:26:30 florian
  270. * patch from Sebastian Guenther applied: TMethod implementation
  271. Revision 1.31 1999/08/28 14:53:27 florian
  272. * bug 471 fixed: run time error 2 is now converted into a file not
  273. found exception
  274. Revision 1.30 1999/08/18 11:28:24 michael
  275. * Fixed reallocmem bug 535
  276. Revision 1.29 1999/07/27 13:01:12 peter
  277. + filerec,textrec declarations
  278. }