crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianETMLib/Common/Engine/ETEngineBase.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 SymbianDebugLib.Engine;
       
    24 using SymbianDebugLib.PluginAPI.Types;
       
    25 using SymbianDebugLib.PluginAPI.Types.Symbol;
       
    26 using SymbianDebugLib.PluginAPI.Types.Code;
       
    27 using SymbianStructuresLib.Arm;
       
    28 using SymbianStructuresLib.Arm.Exceptions;
       
    29 using SymbianStructuresLib.Arm.Disassembler;
       
    30 using SymbianStructuresLib.Debug.Symbols;
       
    31 using SymbianStructuresLib.Debug.Code;
       
    32 using SymbianETMLib.Common.Buffer;
       
    33 using SymbianETMLib.Common.State;
       
    34 using SymbianETMLib.Common.Config;
       
    35 using SymbianETMLib.Common.Utilities;
       
    36 using SymbianETMLib.Common.Types;
       
    37 
       
    38 namespace SymbianETMLib.Common.Engine
       
    39 {
       
    40     public class ETEngineBase : ITracer
       
    41     {
       
    42         #region Delegates & events
       
    43         public delegate void BranchHandler( ETMBranch aBranch );
       
    44         public event BranchHandler Branch;
       
    45 
       
    46         public delegate void ExceptionModeHandler( TArmExceptionType aExceptionMode );
       
    47         public event ExceptionModeHandler Exception;
       
    48 
       
    49         public delegate void ContextSwitchHandler( uint aContextId, string aThreadName );
       
    50         public event ContextSwitchHandler ContextSwitch;
       
    51 
       
    52         public delegate void SynchronizationHandler( uint aPosition );
       
    53         public event SynchronizationHandler Synchronized;
       
    54         #endregion
       
    55 
       
    56         #region Constructors
       
    57         public ETEngineBase( ETBufferBase aBuffer, ETConfigBase aConfig )
       
    58         {
       
    59             iBuffer = aBuffer;
       
    60             iConfig = aConfig;
       
    61         }
       
    62         #endregion
       
    63 
       
    64         #region API
       
    65         public void Decode()
       
    66         {
       
    67             int lastBufferCountValue = 0;
       
    68             //
       
    69             ETMStateData stateManager = new ETMStateData( this );
       
    70             while ( !iBuffer.IsEmpty )
       
    71             {
       
    72                 byte b = iBuffer.Dequeue();
       
    73                 if ( iBuffer.Count != lastBufferCountValue )
       
    74                 {
       
    75                     Trace( "[0x{0:x4}] byte: 0x{1:x2} {2}",
       
    76                         stateManager.PacketNumber,
       
    77                         b,
       
    78                         Convert.ToString( b, 2 ).PadLeft( 8, '0' )
       
    79                       );
       
    80                 }
       
    81                 //
       
    82                 lastBufferCountValue = iBuffer.Count;
       
    83                 stateManager.PrepareToHandleByte( b );
       
    84             }
       
    85         }
       
    86 
       
    87         internal string LookUpSymbol( uint aAddress )
       
    88         {
       
    89             StringBuilder line = new StringBuilder();
       
    90             //
       
    91             if ( iDebugEngineView != null )
       
    92             {
       
    93                 SymbolCollection col = null;
       
    94                 Symbol sym = LookUpSymbol( aAddress, out col );
       
    95                 //
       
    96                 if ( sym != null )
       
    97                 {
       
    98                     line.AppendFormat( "0x{0:x8} {1} {2}", sym.Address, sym.ToStringOffset( aAddress ), sym.Name );
       
    99                 }
       
   100                 else if ( col != null )
       
   101                 {
       
   102                     line.AppendFormat( "Unknown Symbol from \'{0}\'", col.FileName.FileNameInHost );
       
   103                 }
       
   104                 else
       
   105                 {
       
   106                     bool isExceptionVector = Config.IsExceptionVector( aAddress );
       
   107                     if ( isExceptionVector )
       
   108                     {
       
   109                         line.Append( "Exception Vector" );
       
   110                     }
       
   111                     else
       
   112                     {
       
   113                         line.Append( "????" );
       
   114                     }
       
   115                 }
       
   116             }
       
   117             //
       
   118             return line.ToString();
       
   119         }
       
   120 
       
   121         internal Symbol LookUpSymbol( uint aAddress, out SymbolCollection aCollection )
       
   122         {
       
   123             Symbol ret = null;
       
   124             aCollection = null;
       
   125             //
       
   126             if ( iDebugEngineView != null )
       
   127             {
       
   128                 ret = iDebugEngineView.Symbols.Lookup( aAddress, out aCollection );
       
   129             }
       
   130             //
       
   131             return ret;
       
   132         }
       
   133         #endregion
       
   134 
       
   135         #region Properties
       
   136         public ETBufferBase Buffer
       
   137         {
       
   138             get { return iBuffer; }
       
   139         }
       
   140 
       
   141         public ETConfigBase Config
       
   142         {
       
   143             get { return iConfig; }
       
   144         }
       
   145 
       
   146         public DbgEngineView DebugEngineView
       
   147         {
       
   148             get { return iDebugEngineView; }
       
   149             set { iDebugEngineView = value; }
       
   150         }
       
   151         #endregion
       
   152 
       
   153         #region Internal event propgation methods
       
   154         internal void OnExceptionModeChange( TArmExceptionType aNewException )
       
   155         {
       
   156             if ( Exception != null )
       
   157             {
       
   158                 Exception( aNewException );
       
   159             }
       
   160         }
       
   161 
       
   162         internal void OnBranch( uint aAddress, TArmInstructionSet aInstructionSet, TArmExceptionType aExceptionType, TETMBranchType aBranchType )
       
   163         {
       
   164             if ( Branch != null )
       
   165             {
       
   166                 ETMBranch branch = new ETMBranch( new SymAddress( aAddress ), iBranchCounter, aBranchType, aInstructionSet, aExceptionType );
       
   167                 
       
   168                 // Try to find a symbol
       
   169                 SymbolCollection col = null;
       
   170                 Symbol sym = LookUpSymbol( aAddress, out col );
       
   171                 //
       
   172                 if ( sym != null )
       
   173                 {
       
   174                     branch.Symbol = sym;
       
   175                 }
       
   176                 else if ( col != null )
       
   177                 {
       
   178                     branch.SymbolText = string.Format( "Unknown Symbol from \'{0}\'", col.FileName.FileNameInHost );
       
   179                 }
       
   180 
       
   181                 // Cascade event
       
   182                 Branch( branch );
       
   183             }
       
   184 
       
   185             ++iBranchCounter;
       
   186         }
       
   187 
       
   188         internal void OnContextSwitch( uint aNewContextId )
       
   189         {
       
   190             if ( ContextSwitch != null )
       
   191             {
       
   192                 string threadName = iConfig.GetContextID( aNewContextId );
       
   193                 ContextSwitch( aNewContextId, threadName );
       
   194             }
       
   195         }
       
   196 
       
   197         internal void OnSynchronised( uint aOffset )
       
   198         {
       
   199             if ( Synchronized != null )
       
   200             {
       
   201                 Synchronized( aOffset );
       
   202             }
       
   203         }
       
   204         #endregion
       
   205 
       
   206         #region From ITracer
       
   207         public void Trace( string aText )
       
   208         {
       
   209             if ( System.Diagnostics.Debugger.IsAttached )
       
   210             {
       
   211                 System.Diagnostics.Debug.WriteLine( aText );
       
   212             }
       
   213             if ( iConfig.Verbose )
       
   214             {
       
   215                 System.Console.WriteLine( aText );
       
   216             }
       
   217         }
       
   218 
       
   219         public void Trace( string aFormat, params object[] aParams )
       
   220         {
       
   221             if ( iConfig.Verbose || System.Diagnostics.Debugger.IsAttached )
       
   222             {
       
   223                 string text = string.Format( aFormat, aParams );
       
   224                 Trace( text );
       
   225             }
       
   226         }
       
   227         #endregion
       
   228 
       
   229         #region From System.Object
       
   230         #endregion
       
   231 
       
   232         #region Data members
       
   233         private readonly ETBufferBase iBuffer;
       
   234         private readonly ETConfigBase iConfig;
       
   235         private int iBranchCounter = 0;
       
   236         private DbgEngineView iDebugEngineView;
       
   237         #endregion
       
   238     }
       
   239 }