sysutils.inc 8.4 KB

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