crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianETMLib/Common/BranchDecoder/ETMBranchDecoder.cs
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 using System;
       
    19 using System.Collections.Generic;
       
    20 using System.Text;
       
    21 using SymbianUtils.Tracer;
       
    22 using SymbianUtils.BasicTypes;
       
    23 using SymbianStructuresLib.Arm;
       
    24 using SymbianStructuresLib.Arm.Exceptions;
       
    25 using SymbianStructuresLib.Arm.SecurityMode;
       
    26 using SymbianETMLib.Common.Types;
       
    27 using SymbianETMLib.Common.State;
       
    28 using SymbianETMLib.Common.Config;
       
    29 
       
    30 namespace SymbianETMLib.Common.BranchDecoder
       
    31 {
       
    32     internal abstract class ETMBranchDecoder : ITracer
       
    33     {
       
    34         #region Factory
       
    35         public static ETMBranchDecoder New( ETMStateData aStateData )
       
    36         {
       
    37             ETMBranchDecoder ret = null;
       
    38             //
       
    39             switch ( aStateData.Config.BranchCompressionScheme )
       
    40             {
       
    41             default:
       
    42             case TETMBranchCompressionScheme.EOriginal:
       
    43                 ret = new ETMBranchDecoderOriginal( aStateData );
       
    44                 break;
       
    45             case TETMBranchCompressionScheme.EAlternative:
       
    46                 ret = new ETMBranchDecoderAlternative( aStateData );
       
    47                 break;
       
    48             }
       
    49             //
       
    50             return ret;
       
    51         }
       
    52         #endregion
       
    53 
       
    54         #region Constructors
       
    55         protected ETMBranchDecoder( ETMStateData aStateData )
       
    56         {
       
    57             iStateData = aStateData;
       
    58             iInstructionSet = aStateData.CurrentInstructionSet;
       
    59         }
       
    60         #endregion
       
    61 
       
    62         #region API
       
    63         public void FlushChanges()
       
    64         {
       
    65             TArmInstructionSet originalInstructionSet = iStateData.CurrentInstructionSet;
       
    66             SymAddress originalAddress = new SymAddress( iStateData.CurrentAddress.Address );
       
    67 
       
    68             //if ( !IsLastInstructionCancelled )
       
    69             {
       
    70                 // Set known address
       
    71                 iStateData.SetKnownAddressBits( iBranchAddress.Address,
       
    72                                               iBranchAddress.KnownBits,
       
    73                                               TETMBranchType.EBranchExplicit );
       
    74 
       
    75                 // Handle a change in security mode
       
    76                 if ( iSecurityMode != TArmSecurityMode.EUnknown )
       
    77                 {
       
    78                     iStateData.CurrentSecurityMode = iSecurityMode;
       
    79                 }
       
    80 
       
    81                 if ( iExceptionType != TArmExceptionType.EUnknown )
       
    82                 {
       
    83                     iStateData.CurrentException = iExceptionType;
       
    84                 }
       
    85 
       
    86                 // Handle a change in instruction set
       
    87                 if ( iStateData.CurrentInstructionSet != iInstructionSet )
       
    88                 {
       
    89                     iStateData.CurrentInstructionSet = iInstructionSet;
       
    90                 }
       
    91             }
       
    92 
       
    93             DbgTrace( originalAddress, originalInstructionSet );
       
    94         }
       
    95 
       
    96         public void DecodeBranch()
       
    97         {
       
    98             System.Diagnostics.Debug.Assert( IsBranchAddressAvailable );
       
    99             System.Diagnostics.Debug.Assert( Count > 0 && Count <= 5 );
       
   100             //
       
   101             if ( Count == 5 )
       
   102             {
       
   103                 DecodeFull();
       
   104             }
       
   105             else
       
   106             {
       
   107                 DecodePartial();
       
   108             }
       
   109         }
       
   110         #endregion
       
   111 
       
   112         #region Framework API
       
   113         public abstract void Offer( SymByte aByte );
       
   114 
       
   115         public abstract void DecodeException( SymByte aByte );
       
   116 
       
   117         protected abstract void DecodeFull();
       
   118 
       
   119         protected abstract void DecodePartial();
       
   120 
       
   121         public virtual bool IsPacketComplete
       
   122         {
       
   123             get
       
   124             {
       
   125                 // If the 7th bit is clear, then this might be
       
   126                 // the last byte.
       
   127                 //
       
   128                 // If:
       
   129                 //
       
   130                 // a) the number of bytes forming the branch packet
       
   131                 //    is less than 5, then this is the last byte.
       
   132                 //
       
   133                 // b) the number of bytes forming the branch packet
       
   134                 //    is 5, and
       
   135                 //    i)  bit 6 is clear => normal branch, this is the last byte
       
   136                 //    ii) bit 7 is set => inline exception, but this is still the last byte.
       
   137                 //
       
   138                 bool ret = false;
       
   139                 bool bit7 = LastByte[ 7 ];
       
   140                 //
       
   141                 switch ( iBytes.Count )
       
   142                 {
       
   143                 default:
       
   144                 case 0:
       
   145                     ret = false;
       
   146                     break;
       
   147                 case 1:
       
   148                 case 2:
       
   149                 case 3:
       
   150                 case 4:
       
   151                     ret = ( bit7 == false ); // (a)
       
   152                     break;
       
   153                 case 5:
       
   154                     if ( bit7 )
       
   155                     {
       
   156                         // (b), part (ii)
       
   157                         ret = true;
       
   158                     }
       
   159                     else
       
   160                     {
       
   161                         // (b), part (i) - i.e. bit 6 and 7 are both clear => normal branch byte
       
   162                         ret = ( LastByte[ 6 ] == false );
       
   163                     }
       
   164                     break;
       
   165                 }
       
   166                 return ret;
       
   167             }
       
   168         }
       
   169 
       
   170         public virtual bool IsBranchAddressAvailable
       
   171         {
       
   172             get
       
   173             {
       
   174                 // The branch address is only available once 
       
   175                 // bit seven is clear, or then once we've reached
       
   176                 // an entire run of 5 bytes.
       
   177                 bool ret = false;
       
   178                 //
       
   179                 if ( Count == 5 )
       
   180                 {
       
   181                     ret = true;
       
   182                 }
       
   183                 else
       
   184                 {
       
   185                     ret = ( LastByte[ 7 ] == false );
       
   186                 }
       
   187                 //
       
   188                 return ret;
       
   189             }
       
   190         }
       
   191         #endregion
       
   192 
       
   193         #region Internal API
       
   194         protected void Save( SymByte aByte )
       
   195         {
       
   196             iBytes.Add( aByte );
       
   197         }
       
   198         #endregion
       
   199 
       
   200         #region Properties
       
   201         public int Count
       
   202         {
       
   203             get { return iBytes.Count; }
       
   204         }
       
   205 
       
   206         public bool IsLastInstructionCancelled
       
   207         {
       
   208             get { return iLastInstructionCancelled; }
       
   209             protected set { iLastInstructionCancelled = value; }
       
   210         }
       
   211 
       
   212         public SymByte this[ int aIndex ]
       
   213         {
       
   214             get { return iBytes[ aIndex ]; }
       
   215         }
       
   216 
       
   217         public ETConfigBase Config
       
   218         {
       
   219             get { return iStateData.Config; }
       
   220         }
       
   221 
       
   222         public TArmExceptionType ExceptionType
       
   223         {
       
   224             get { return iExceptionType; }
       
   225             protected set { iExceptionType = value; }
       
   226         }
       
   227 
       
   228         public TArmSecurityMode SecurityMode
       
   229         {
       
   230             get { return iSecurityMode; }
       
   231             protected set { iSecurityMode = value; }
       
   232         }
       
   233 
       
   234         public TArmInstructionSet InstructionSet
       
   235         {
       
   236             get { return iInstructionSet; }
       
   237             protected set { iInstructionSet = value; }
       
   238         }
       
   239 
       
   240         public SymAddressWithKnownBits BranchAddress
       
   241         {
       
   242             get { return iBranchAddress; }
       
   243             protected set { iBranchAddress = value; }
       
   244         }
       
   245         #endregion
       
   246 
       
   247         #region Internal methods
       
   248         protected SymByte LastByte
       
   249         {
       
   250             get
       
   251             {
       
   252                 SymByte ret = new SymByte( 0 );
       
   253                 //
       
   254                 int count = iBytes.Count;
       
   255                 if ( count > 0 )
       
   256                 {
       
   257                     ret = iBytes[ count - 1 ];
       
   258                 }
       
   259                 //
       
   260                 return ret;
       
   261             }
       
   262         }
       
   263 
       
   264         protected static int CompressionLeftShiftCount( TArmInstructionSet aIS )
       
   265         {
       
   266             int ret = 2;
       
   267 
       
   268             // Interpret the bytes to form an address based upon current
       
   269             // instruction set.
       
   270             switch ( aIS )
       
   271             {
       
   272             default:
       
   273             case TArmInstructionSet.EARM:
       
   274                 break;
       
   275             case TArmInstructionSet.ETHUMB:
       
   276                 ret = 1;
       
   277                 break;
       
   278             case TArmInstructionSet.EJAZELLE:
       
   279                 ret = 0;
       
   280                 break;
       
   281             }
       
   282             //
       
   283             return ret;
       
   284         }
       
   285 
       
   286         private void DbgTrace( SymAddress aOriginalAddress, TArmInstructionSet aOriginalISet )
       
   287         {
       
   288             if ( Count == 5 )
       
   289             {
       
   290                 DbgTrace( "BRANCH-F", aOriginalAddress, aOriginalISet );
       
   291             }
       
   292             else
       
   293             {
       
   294                 DbgTrace( "BRANCH-P", aOriginalAddress, aOriginalISet );
       
   295             }
       
   296         }
       
   297 
       
   298         private void DbgTrace( string aType, SymAddress aOriginalAddress, TArmInstructionSet aOriginalISet )
       
   299         {
       
   300             StringBuilder lines = new StringBuilder();
       
   301             lines.AppendLine( "   " + aType );
       
   302             //
       
   303             if ( iStateData.LastBranch.IsKnown )
       
   304             {
       
   305                 lines.AppendLine( string.Format( "      using: {0} 0x{1} to go...",
       
   306                     iBranchAddress.AddressBinary,
       
   307                     iBranchAddress.AddressHex )
       
   308                     );
       
   309                 lines.AppendLine( string.Format( "       from: {0} 0x{1} {2} 0x{3:x8} {4}", 
       
   310                     aOriginalAddress.AddressBinary,
       
   311                     aOriginalAddress.AddressHex,
       
   312                     ETMDecodeState.MakeInstructionSetPrefix( aOriginalISet ),
       
   313                     aOriginalAddress,
       
   314                     iStateData.Engine.LookUpSymbol( aOriginalAddress ) ) 
       
   315                     );
       
   316                 lines.AppendLine( string.Format( "         to: {0} 0x{1} {2} 0x{3:x8} {4}", 
       
   317                     iStateData.CurrentAddress.AddressBinary,
       
   318                     iStateData.CurrentAddress.AddressHex,
       
   319                     ETMDecodeState.MakeInstructionSetPrefix( iStateData.CurrentInstructionSet ),
       
   320                     iStateData.CurrentAddress, 
       
   321                     iStateData.Engine.LookUpSymbol( iStateData.CurrentAddress ) ) 
       
   322                     );
       
   323             }
       
   324             //
       
   325             Trace( lines.ToString() );
       
   326         }
       
   327         #endregion
       
   328 
       
   329         #region From ITracer
       
   330         public void Trace( string aText )
       
   331         {
       
   332             iStateData.Trace( aText );
       
   333         }
       
   334 
       
   335         public void Trace( string aFormat, params object[] aParams )
       
   336         {
       
   337             iStateData.Trace( aFormat, aParams );
       
   338         }
       
   339         #endregion
       
   340 
       
   341         #region Data members
       
   342         private readonly ETMStateData iStateData;
       
   343         private bool iLastInstructionCancelled = false;
       
   344         private List<SymByte> iBytes = new List<SymByte>();
       
   345         private TArmExceptionType iExceptionType = TArmExceptionType.EUnknown;
       
   346         private TArmSecurityMode iSecurityMode = TArmSecurityMode.EUnknown;
       
   347         private TArmInstructionSet iInstructionSet = TArmInstructionSet.EARM;
       
   348         private SymAddressWithKnownBits iBranchAddress = new SymAddressWithKnownBits();
       
   349         #endregion
       
   350     }
       
   351 }