File : s-traceb-xi-ppc.adb


   1 ------------------------------------------------------------------------------
   2 --                                                                          --
   3 --                         GNAT COMPILER COMPONENTS                         --
   4 --                                                                          --
   5 --                     S Y S T E M . T R A C E B A C K                      --
   6 --                                                                          --
   7 --                                 B o d y                                  --
   8 --                                                                          --
   9 --          Copyright (C) 1999-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 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
  29 --                                                                          --
  30 ------------------------------------------------------------------------------
  31 
  32 with System.Machine_Code; use System.Machine_Code;
  33 
  34 --  This is the bare board version of this package for PowerPC targets
  35 
  36 package body System.Traceback is
  37 
  38    procedure Call_Chain
  39      (Traceback   : in out System.Traceback_Entries.Tracebacks_Array;
  40       Max_Len     : Natural;
  41       Len         : out Natural;
  42       Exclude_Min : System.Address := System.Null_Address;
  43       Exclude_Max : System.Address := System.Null_Address;
  44       Skip_Frames : Natural := 1)
  45    is
  46       type PPC_Stack_Frame;
  47       --  Layout of the PowerPC stack frame, according to the ELF ABI
  48 
  49       type PPC_Stack_Frame_Acc is access PPC_Stack_Frame;
  50       pragma Convention (C, PPC_Stack_Frame_Acc);
  51 
  52       type PPC_Stack_Frame is record
  53          Back_Chain : PPC_Stack_Frame_Acc;
  54          --  Pointer to previous frame
  55 
  56          Saved_LR   : System.Address;
  57          --  LR save word
  58       end record;
  59       pragma Convention (C, PPC_Stack_Frame);
  60 
  61       Frame : PPC_Stack_Frame_Acc;
  62       --  Frame being processed
  63 
  64       LR : System.Address;
  65       --  Link register (return address)
  66 
  67       Last : Integer := Traceback'First - 1;
  68       --  Index of last traceback entry written to the buffer
  69 
  70    begin
  71       --  By default, the traceback is empty
  72 
  73       Len := 0;
  74 
  75       --  Move contents of r1 (sp) to "Frame"
  76 
  77       Asm ("or %0, 1, 1",
  78            Outputs => PPC_Stack_Frame_Acc'Asm_Output ("=r", Frame),
  79            Volatile => True);
  80 
  81       --  Skip the current frame
  82 
  83       Frame := Frame.Back_Chain;
  84 
  85       --  Exclude Skip_Frames frames from the traceback. ABI has
  86       --  System.Null_Address as the back pointer of the shallowest frame in
  87       --  the stack.
  88 
  89       for J in 1 .. Skip_Frames loop
  90          Frame := Frame.Back_Chain;
  91          LR := Frame.Saved_LR;
  92 
  93          if LR = System.Null_Address or else Frame = null then
  94 
  95             --  Something is wrong.  Skip_Frames is greater than the number of
  96             --  frames on the current stack. Do not return a trace.
  97 
  98             return;
  99          end if;
 100       end loop;
 101 
 102       while Frame /= null
 103         and then Last < Traceback'Last
 104         and then Len < Max_Len
 105       loop
 106          if LR not in Exclude_Min .. Exclude_Max then
 107 
 108             --  Skip specified routines, if any (e.g. Ada.Exceptions)
 109 
 110             Last := Last + 1;
 111             Len := Len + 1;
 112             Traceback (Last) := LR;
 113          end if;
 114 
 115          Frame := Frame.Back_Chain;
 116          LR := Frame.Saved_LR;
 117       end loop;
 118    end Call_Chain;
 119 
 120 end System.Traceback;