| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- G N A T . E X C E P T I O N _ A C T I O N S -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2002-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 11 |
-- GNAT is free software; you can redistribute it and/or modify it under -- |
| 12 |
-- terms of the GNU General Public License as published by the Free Soft- -- |
| 13 |
-- ware Foundation; either version 3, or (at your option) any later ver- -- |
| 14 |
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
| 15 |
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- |
| 16 |
-- or FITNESS FOR A PARTICULAR PURPOSE. -- |
| 17 |
-- -- |
| 18 |
-- As a special exception under Section 7 of GPL version 3, you are granted -- |
| 19 |
-- additional permissions described in the GCC Runtime Library Exception, -- |
| 20 |
-- version 3.1, as published by the Free Software Foundation. -- |
| 21 |
-- -- |
| 22 |
-- You should have received a copy of the GNU General Public License and -- |
| 23 |
-- a copy of the GCC Runtime Library Exception along with this program; -- |
| 24 |
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- |
| 25 |
-- <http://www.gnu.org/licenses/>. -- |
| 26 |
-- -- |
| 27 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 28 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 29 |
-- -- |
| 30 |
------------------------------------------------------------------------------ |
| 31 |
|
| 32 |
with Ada.Unchecked_Conversion; |
| 33 |
with System; |
| 34 |
with System.Soft_Links; use System.Soft_Links; |
| 35 |
with System.Standard_Library; use System.Standard_Library; |
| 36 |
with System.Exception_Table; use System.Exception_Table; |
| 37 |
with Interfaces.C; use Interfaces.C; |
| 38 |
|
| 39 |
package body GNAT.Exception_Actions is |
| 40 |
|
| 41 |
Global_Action : Exception_Action; |
| 42 |
pragma Import |
| 43 |
(Ada, Global_Action, "__gnat_exception_actions_global_action"); |
| 44 |
pragma Atomic (Global_Action); |
| 45 |
-- Imported from Ada.Exceptions. Any change in the external name needs to |
| 46 |
-- be coordinated with a-exextr.adb. |
| 47 |
|
| 48 |
Global_Unhandled_Action : Exception_Action; |
| 49 |
pragma Import |
| 50 |
(Ada, Global_Unhandled_Action, |
| 51 |
"__gnat_exception_actions_global_unhandled_action"); |
| 52 |
pragma Atomic (Global_Unhandled_Action); |
| 53 |
-- Imported from Ada.Exceptions. Any change in the external name needs to |
| 54 |
-- be coordinated with a-exextr.adb. |
| 55 |
|
| 56 |
Raise_Hook_Initialized : Boolean; |
| 57 |
pragma Import |
| 58 |
(Ada, Raise_Hook_Initialized, "__gnat_exception_actions_initialized"); |
| 59 |
|
| 60 |
function To_Raise_Action is new Ada.Unchecked_Conversion |
| 61 |
(Exception_Action, Raise_Action); |
| 62 |
|
| 63 |
-- ??? Would be nice to have this in System.Standard_Library |
| 64 |
function To_Data is new Ada.Unchecked_Conversion |
| 65 |
(Exception_Id, Exception_Data_Ptr); |
| 66 |
function To_Id is new Ada.Unchecked_Conversion |
| 67 |
(Exception_Data_Ptr, Exception_Id); |
| 68 |
|
| 69 |
---------------------------- |
| 70 |
-- Register_Global_Action -- |
| 71 |
---------------------------- |
| 72 |
|
| 73 |
procedure Register_Global_Action (Action : Exception_Action) is |
| 74 |
begin |
| 75 |
Global_Action := Action; |
| 76 |
end Register_Global_Action; |
| 77 |
|
| 78 |
-------------------------------------- |
| 79 |
-- Register_Global_Unhandled_Action -- |
| 80 |
-------------------------------------- |
| 81 |
|
| 82 |
procedure Register_Global_Unhandled_Action (Action : Exception_Action) is |
| 83 |
begin |
| 84 |
Global_Unhandled_Action := Action; |
| 85 |
end Register_Global_Unhandled_Action; |
| 86 |
|
| 87 |
------------------------ |
| 88 |
-- Register_Id_Action -- |
| 89 |
------------------------ |
| 90 |
|
| 91 |
procedure Register_Id_Action |
| 92 |
(Id : Exception_Id; |
| 93 |
Action : Exception_Action) |
| 94 |
is |
| 95 |
begin |
| 96 |
if Id = Null_Id then |
| 97 |
raise Program_Error; |
| 98 |
end if; |
| 99 |
|
| 100 |
Lock_Task.all; |
| 101 |
To_Data (Id).Raise_Hook := To_Raise_Action (Action); |
| 102 |
Raise_Hook_Initialized := True; |
| 103 |
Unlock_Task.all; |
| 104 |
end Register_Id_Action; |
| 105 |
|
| 106 |
--------------- |
| 107 |
-- Core_Dump -- |
| 108 |
--------------- |
| 109 |
|
| 110 |
procedure Core_Dump (Occurrence : Exception_Occurrence) is separate; |
| 111 |
|
| 112 |
------------------------ |
| 113 |
-- Exception_Language -- |
| 114 |
------------------------ |
| 115 |
|
| 116 |
function Exception_Language |
| 117 |
(E : Exception_Occurrence) |
| 118 |
return Exception_Languages is |
| 119 |
|
| 120 |
Foreign_Exception : aliased Exception_Data; |
| 121 |
pragma Import |
| 122 |
(Ada, Foreign_Exception, "system__exceptions__foreign_exception"); |
| 123 |
|
| 124 |
function To_Exception_Data_Ptr is new |
| 125 |
Ada.Unchecked_Conversion (Exception_Id, Exception_Data_Ptr); |
| 126 |
|
| 127 |
IdD : constant Exception_Data_Ptr |
| 128 |
:= To_Exception_Data_Ptr (Exception_Identity (E)); |
| 129 |
Lang : constant Character := IdD.Lang; |
| 130 |
|
| 131 |
begin |
| 132 |
if Lang in 'B' .. 'C' then |
| 133 |
return EL_Cpp; |
| 134 |
end if; |
| 135 |
|
| 136 |
if Lang /= 'A' then |
| 137 |
return EL_Unknown; |
| 138 |
end if; |
| 139 |
|
| 140 |
if IdD /= Foreign_Exception'Unchecked_Access then |
| 141 |
return EL_Ada; |
| 142 |
end if; |
| 143 |
|
| 144 |
declare |
| 145 |
function Get_Exception_Machine_Occurrence |
| 146 |
(E : Exception_Occurrence) return System.Address; |
| 147 |
pragma Import (Ada, Get_Exception_Machine_Occurrence, |
| 148 |
"__gnat_get_exception_machine_occurrence"); |
| 149 |
|
| 150 |
function Exception_Language_Is_CPlusPlus |
| 151 |
(E : System.Address) |
| 152 |
return C_bool; |
| 153 |
pragma Import (C, Exception_Language_Is_CPlusPlus, |
| 154 |
"__gnat_exception_language_is_cplusplus"); |
| 155 |
|
| 156 |
function Exception_Language_Is_Ada |
| 157 |
(E : System.Address) |
| 158 |
return C_bool; |
| 159 |
pragma Import (C, Exception_Language_Is_Ada, |
| 160 |
"__gnat_exception_language_is_ada"); |
| 161 |
|
| 162 |
Occurrence : constant System.Address |
| 163 |
:= Get_Exception_Machine_Occurrence (E); |
| 164 |
begin |
| 165 |
if Exception_Language_Is_CPlusPlus (Occurrence) then |
| 166 |
return EL_Cpp; |
| 167 |
elsif Exception_Language_Is_Ada (Occurrence) then |
| 168 |
-- This might some day indicate third-party Ada exceptions. |
| 169 |
-- With GNAT exceptions, we should not reach this point. |
| 170 |
return EL_Ada; |
| 171 |
end if; |
| 172 |
end; |
| 173 |
|
| 174 |
return EL_Unknown; |
| 175 |
end Exception_Language; |
| 176 |
|
| 177 |
-------------------------- |
| 178 |
-- Is_Foreign_Exception -- |
| 179 |
-------------------------- |
| 180 |
|
| 181 |
function Is_Foreign_Exception (E : Exception_Occurrence) return Boolean is |
| 182 |
begin |
| 183 |
return Exception_Language (E) /= EL_Ada; |
| 184 |
end Is_Foreign_Exception; |
| 185 |
|
| 186 |
---------------- |
| 187 |
-- Name_To_Id -- |
| 188 |
---------------- |
| 189 |
|
| 190 |
function Name_To_Id (Name : String) return Exception_Id is |
| 191 |
begin |
| 192 |
return To_Id (Internal_Exception (Name, Create_If_Not_Exist => False)); |
| 193 |
end Name_To_Id; |
| 194 |
|
| 195 |
--------------------------------- |
| 196 |
-- Registered_Exceptions_Count -- |
| 197 |
--------------------------------- |
| 198 |
|
| 199 |
function Registered_Exceptions_Count return Natural renames |
| 200 |
System.Exception_Table.Registered_Exceptions_Count; |
| 201 |
|
| 202 |
------------------------------- |
| 203 |
-- Get_Registered_Exceptions -- |
| 204 |
------------------------------- |
| 205 |
-- This subprogram isn't an iterator to avoid concurrency problems, |
| 206 |
-- since the exceptions are registered dynamically. Since we have to lock |
| 207 |
-- the runtime while computing this array, this means that any callback in |
| 208 |
-- an active iterator would be unable to access the runtime. |
| 209 |
|
| 210 |
procedure Get_Registered_Exceptions |
| 211 |
(List : out Exception_Id_Array; |
| 212 |
Last : out Integer) |
| 213 |
is |
| 214 |
Ids : Exception_Data_Array (List'Range); |
| 215 |
begin |
| 216 |
Get_Registered_Exceptions (Ids, Last); |
| 217 |
|
| 218 |
for L in List'First .. Last loop |
| 219 |
List (L) := To_Id (Ids (L)); |
| 220 |
end loop; |
| 221 |
end Get_Registered_Exceptions; |
| 222 |
|
| 223 |
end GNAT.Exception_Actions; |