File : a-elchha-minimal.adb


   1 ------------------------------------------------------------------------------
   2 --                                                                          --
   3 --                         GNAT RUN-TIME COMPONENTS                         --
   4 --                                                                          --
   5 --    A D A . E X C E P T I O N S . L A S T _ C H A N C E _ H A N D L E R   --
   6 --                                                                          --
   7 --                                 B o d y                                  --
   8 --                                                                          --
   9 --          Copyright (C) 2012-2014, 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 --                                                                          --
  19 --                                                                          --
  20 --                                                                          --
  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 --  Default last chance handler for no propagation runtimes
  33 
  34 --  It dumps the non-symbolic traceback from the point where the exception
  35 --  was triggered.
  36 
  37 with Ada.Exceptions.Traceback; use Ada.Exceptions.Traceback;
  38 with Ada.Unchecked_Conversion;
  39 with System.Machine_Reset;
  40 with System.Traceback;
  41 
  42 with GNAT.IO;                  use GNAT.IO;
  43 with GNAT.Debug_Utilities;     use GNAT.Debug_Utilities;
  44 --  We rely on GNAT packages for the output. Usually, Ada predefined units
  45 --  cannot depends on GNAT units, as the user could use the GNAT hierarchy.
  46 --  However, this implementation of Last_Chance_Handler is a default one, that
  47 --  could be redefined by the user.
  48 
  49 procedure Ada.Exceptions.Last_Chance_Handler
  50   (Msg : System.Address; Line : Integer)
  51 is
  52    procedure Put (Str : System.Address);
  53    --  Put for a nul-terminated string (a C string)
  54 
  55    ---------
  56    -- Put --
  57    ---------
  58 
  59    procedure Put (Str : System.Address) is
  60 
  61       type C_String_Ptr is access String (1 .. Positive'Last);
  62       function To_C_String_Ptr is new Ada.Unchecked_Conversion
  63         (System.Address, C_String_Ptr);
  64 
  65       Msg_Str : constant C_String_Ptr := To_C_String_Ptr (Str);
  66    begin
  67       for J in Msg_Str'Range loop
  68          exit when Msg_Str (J) = Character'Val (0);
  69          Put (Msg_Str (J));
  70       end loop;
  71    end Put;
  72 
  73    Traceback : Tracebacks_Array (1 .. 64);
  74    Len       : Natural;
  75 
  76 begin
  77    Put_Line ("In last chance handler");
  78    if Line /= 0 then
  79       Put ("Predefined exception raised at ");
  80       Put (Msg);
  81       Put (':');
  82       Put (Line);
  83    else
  84       Put ("User defined exception, message: ");
  85       Put (Msg);
  86    end if;
  87    New_Line;
  88 
  89    Put_Line ("Call stack traceback locations:");
  90 
  91    --  Dump backtrace PC values
  92 
  93    System.Traceback.Call_Chain (Traceback, Traceback'Length, Len);
  94 
  95    for J in 1 .. Len loop
  96       Put (Image_C (Traceback (J)));
  97       Put (" ");
  98    end loop;
  99 
 100    New_Line;
 101 
 102    --  Stop the program
 103    System.Machine_Reset.Stop;
 104 end Ada.Exceptions.Last_Chance_Handler;