sysutils.inc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. { Read message string definitions }
  13. {
  14. Add a language with IFDEF LANG_NAME
  15. just befor the final ELSE. This way English will always be the default.
  16. }
  17. {$IFDEF LANG_GERMAN}
  18. {$i strg.inc} // Does not exist yet !!
  19. {$ELSE}
  20. {$i stre.inc}
  21. {$ENDIF}
  22. { Read filename handling functions implementation }
  23. {$i fina.inc}
  24. { Read String Handling functions implementation }
  25. {$i sysstr.inc}
  26. { Read date & Time function implementations }
  27. {$i dati.inc}
  28. { Read pchar handling functions implementation }
  29. {$i syspch.inc}
  30. procedure FreeAndNil(var obj);
  31. var
  32. temp: tobject;
  33. begin
  34. temp:=tobject(obj);
  35. pointer(obj):=nil;
  36. temp.free;
  37. end;
  38. constructor Exception.Create(const msg : string);
  39. begin
  40. inherited create;
  41. fmessage:=msg;
  42. end;
  43. constructor Exception.CreateFmt(const msg : string; const args : array of const);
  44. begin
  45. inherited create;
  46. fmessage:=Format(msg,args);
  47. end;
  48. constructor Exception.CreateRes(ResString: PString);
  49. begin
  50. inherited create;
  51. fmessage:=ResString^;
  52. end;
  53. constructor Exception.CreateResFmt(ResString: PString; const Args: array of const);
  54. begin
  55. inherited create;
  56. fmessage:=Format(ResString^,args);
  57. end;
  58. constructor Exception.CreateHelp(const Msg: string; AHelpContext: Integer);
  59. begin
  60. inherited create;
  61. fmessage:=Msg;
  62. fhelpcontext:=AHelpContext;
  63. end;
  64. constructor Exception.CreateFmtHelp(const Msg: string; const Args: array of const;
  65. AHelpContext: Integer);
  66. begin
  67. inherited create;
  68. fmessage:=Format(Msg,args);
  69. fhelpcontext:=AHelpContext;
  70. end;
  71. constructor Exception.CreateResHelp(ResString: PString; AHelpContext: Integer);
  72. begin
  73. inherited create;
  74. fmessage:=ResString^;
  75. fhelpcontext:=AHelpContext;
  76. end;
  77. constructor Exception.CreateResFmtHelp(ResString: PString; const Args: array of const;
  78. AHelpContext: Integer);
  79. begin
  80. inherited create;
  81. fmessage:=Format(ResString^,args);
  82. fhelpcontext:=AHelpContext;
  83. end;
  84. procedure EHeapMemoryError.FreeInstance;
  85. begin
  86. if AllowFree then
  87. inherited FreeInstance;
  88. end;
  89. {$ifopt S+}
  90. {$define STACKCHECK_WAS_ON}
  91. {$S-}
  92. {$endif OPT S }
  93. Procedure CatchUnhandledException (Obj : TObject; Addr,Frame: Pointer);
  94. Var
  95. Message : String;
  96. begin
  97. Writeln(stdout,'An unhandled exception occurred at 0x',HexStr(Longint(Addr),8),' :');
  98. if Obj is exception then
  99. begin
  100. Message:=Exception(Obj).Message;
  101. Writeln(stdout,Message);
  102. end
  103. else
  104. Writeln(stdout,'Exception object ',Obj.ClassName,' is not of class Exception.');
  105. { to get a nice symify }
  106. Writeln(stdout,BackTraceStrFunc(Longint(Addr)));
  107. Dump_Stack(stdout,longint(frame));
  108. Writeln(stdout,'');
  109. Halt(217);
  110. end;
  111. Var OutOfMemory : EOutOfMemory;
  112. InValidPointer : EInvalidPointer;
  113. Procedure RunErrorToExcept (ErrNo : Longint; Address,Frame : Pointer);
  114. Var E : Exception;
  115. S : String;
  116. begin
  117. Case Errno of
  118. 1,203 : E:=OutOfMemory;
  119. 204 : E:=InvalidPointer;
  120. 2,3,4,5,6,100,101,102,103,105,106 : { I/O errors }
  121. begin
  122. Case Errno of
  123. 2 : S:=SFileNotFound;
  124. 3 : S:=SInvalidFileName;
  125. 4 : S:=STooManyOpenFiles;
  126. 5 : S:=SAccessDenied;
  127. 6 : S:=SInvalidFileHandle;
  128. 15 : S:=SInvalidDrive;
  129. 100 : S:=SEndOfFile;
  130. 101 : S:=SDiskFull;
  131. 102 : S:=SFileNotAssigned;
  132. 103 : S:=SFileNotOpen;
  133. 104 : S:=SFileNotOpenForInput;
  134. 105 : S:=SFileNotOpenForOutput;
  135. 106 : S:=SInvalidInput;
  136. end;
  137. E:=EinOutError.Create (S);
  138. EInoutError(E).ErrorCode:=IOresult; // Clears InOutRes !!
  139. end;
  140. // We don't set abstracterrorhandler, but we do it here.
  141. // Unless the use sets another handler we'll get here anyway...
  142. 200 : E:=EDivByZero.Create(SDivByZero);
  143. 201 : E:=ERangeError.Create(SRangeError);
  144. 205 : E:=EOverflow.Create(SOverflow);
  145. 206 : E:=EOverflow.Create(SUnderflow);
  146. 207 : E:=EInvalidOp.Create(SInvalidOp);
  147. 211 : E:=EAbstractError.Create(SAbstractError);
  148. 215 : E:=EIntOverflow.Create(SIntOverflow);
  149. 216 : E:=EAccessViolation.Create(SAccessViolation);
  150. 219 : E:=EInvalidCast.Create(SInvalidCast);
  151. 227 : E:=EAssertionFailed.Create(SAssertionFailed);
  152. else
  153. E:=Exception.CreateFmt (SUnKnownRunTimeError,[Errno]);
  154. end;
  155. Raise E at longint(Address){$ifdef ENHANCEDRAISE},longint(Frame){$endif};
  156. end;
  157. Procedure AssertErrorHandler (Const Msg,FN : ShortString;LineNo,TheAddr : Longint);
  158. Var
  159. S : String;
  160. begin
  161. If Msg='' then
  162. S:=SAssertionFailed
  163. else
  164. S:=Msg;
  165. Raise EAssertionFailed.Createfmt(SAssertError,[S,Fn,LineNo]); // at Pointer(theAddr);
  166. end;
  167. {$ifdef STACKCHECK_WAS_ON}
  168. {$S+}
  169. {$endif}
  170. Procedure InitExceptions;
  171. {
  172. Must install uncaught exception handler (ExceptProc)
  173. and install exceptions for system exceptions or signals.
  174. (e.g: SIGSEGV -> ESegFault or so.)
  175. }
  176. begin
  177. ExceptProc:=@CatchUnhandledException;
  178. // Create objects that may have problems when there is no memory.
  179. OutOfMemory:=EOutOfMemory.Create(SOutOfMemory);
  180. OutOfMemory.AllowFree:=false;
  181. InvalidPointer:=EInvalidPointer.Create(SInvalidPointer);
  182. InvalidPointer.AllowFree:=false;
  183. AssertErrorProc:=@AssertErrorHandler;
  184. ErrorProc:=@RunErrorToExcept;
  185. OnShowException:=Nil;
  186. end;
  187. Procedure DoneExceptions;
  188. begin
  189. OutOfMemory.AllowFree:=true;
  190. OutOfMemory.Free;
  191. InValidPointer.AllowFree:=true;
  192. InValidPointer.Free;
  193. end;
  194. { Exception handling routines }
  195. function ExceptObject: TObject;
  196. begin
  197. If RaiseList=Nil then
  198. Result:=Nil
  199. else
  200. Result:=RaiseList^.FObject;
  201. end;
  202. function ExceptAddr: Pointer;
  203. begin
  204. If RaiseList=Nil then
  205. Result:=Nil
  206. else
  207. Result:=RaiseList^.Addr;
  208. end;
  209. function ExceptionErrorMessage(ExceptObject: TObject; ExceptAddr: Pointer;
  210. Buffer: PChar; Size: Integer): Integer;
  211. Var
  212. S : AnsiString;
  213. Len : Integer;
  214. begin
  215. S:=Format(SExceptionErrorMessage,[ExceptObject.ClassName,ExceptAddr]);
  216. If ExceptObject is Exception then
  217. S:=Format('%s:'#10'%s',[S,Exception(ExceptObject).Message]);
  218. Len:=Length(S);
  219. If S[Len]<>'.' then
  220. begin
  221. S:=S+'.';
  222. Inc(len);
  223. end;
  224. If Len>Size then
  225. Len:=Size;
  226. if Len > 0 then
  227. Move(S[1],Buffer^,Len);
  228. Result:=Len;
  229. end;
  230. procedure ShowException(ExceptObject: TObject; ExceptAddr: Pointer);
  231. // use shortstring. On exception, the heap may be corrupt.
  232. Var
  233. Buf : ShortString;
  234. begin
  235. SetLength(Buf,ExceptionErrorMessage(ExceptObject,ExceptAddr,@Buf[1],255));
  236. If IsConsole Then
  237. writeln(Buf)
  238. else
  239. If Assigned(OnShowException) Then
  240. OnShowException (Buf);
  241. end;
  242. procedure Abort;
  243. begin
  244. Raise EAbort.Create(SAbortError) at Get_Caller_addr(Get_Frame)
  245. end;
  246. procedure OutOfMemoryError;
  247. begin
  248. Raise OutOfMemory;
  249. end;
  250. {
  251. $Log$
  252. Revision 1.5 2001-08-12 22:11:48 peter
  253. * freeandnil added
  254. Revision 1.4 2001/06/03 15:18:01 peter
  255. * eoutofmemory and einvalidpointer fix
  256. Revision 1.3 2000/11/23 11:04:26 sg
  257. * Protected some Move()'s by 'if' clauses so that the Move won't be
  258. executed when the length would be 0. Otherwise, the corresponding
  259. routines might get an RTE when compiled with $R+.
  260. Revision 1.2 2000/08/20 15:46:46 peter
  261. * sysutils.pp moved to target and merged with disk.inc, filutil.inc
  262. }