sysutils.inc 7.6 KB

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