crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbolLib/Generic/Engine/GenericSymbolEngine.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 using System;
       
    18 using System.IO;
       
    19 using System.Text;
       
    20 using System.Threading;
       
    21 using System.Collections.Generic;
       
    22 using SymbianUtils;
       
    23 using SymbianUtils.Range;
       
    24 using SymbianUtils.Tracer;
       
    25 
       
    26 namespace SymbolLib.Generics
       
    27 {
       
    28     public abstract class GenericSymbolEngine : IGenericSymbolCollectionLookupInterface, IGenericSymbolCollectionStatisticsInterface, IEnumerable<GenericSymbolCollection>, ITracer
       
    29     {
       
    30         #region Constructors
       
    31         protected internal GenericSymbolEngine( ITracer aTracer )
       
    32         {
       
    33             iTracer = aTracer;
       
    34         }
       
    35         #endregion
       
    36 
       
    37         #region Framework API
       
    38         public virtual void ClearTags()
       
    39         {
       
    40             lock ( this )
       
    41             {
       
    42                 foreach ( GenericSymbolCollection collection in this )
       
    43                 {
       
    44                     collection.ClearTag();
       
    45                 }
       
    46             }
       
    47         }
       
    48 
       
    49         public virtual void SaveTaggedCollections( string aFileName )
       
    50         {
       
    51             using ( StreamWriter writer = new StreamWriter( aFileName, false ) )
       
    52             {
       
    53                 lock ( this )
       
    54                 {
       
    55                     foreach ( GenericSymbolCollection collection in this )
       
    56                     {
       
    57                         if ( collection.Tagged )
       
    58                         {
       
    59                             collection.WriteToStream( writer );
       
    60                         }
       
    61                     }
       
    62                 }
       
    63             }
       
    64         }
       
    65 
       
    66         public abstract void Reset();
       
    67 
       
    68         public abstract bool IsReady { get; }
       
    69 
       
    70         public abstract bool IsLoaded( string aFileName );
       
    71 
       
    72         public abstract GenericSymbolCollection this[ int aIndex ]
       
    73         {
       
    74             get;
       
    75         }
       
    76 
       
    77         public abstract AddressRange Range { get; }
       
    78 
       
    79         internal abstract void UnloadUntagged();
       
    80         #endregion
       
    81 
       
    82         #region From IEnumerable<GenericSymbolCollection>
       
    83         IEnumerator<GenericSymbolCollection> IEnumerable<GenericSymbolCollection>.GetEnumerator()
       
    84         {
       
    85             return new GenericSymbolEngineEnumerator( this );
       
    86         }
       
    87 
       
    88         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
       
    89         {
       
    90             return new GenericSymbolEngineEnumerator( this );
       
    91         }
       
    92         #endregion
       
    93 
       
    94         #region From IGenericSymbolCollectionStatisticsInterface
       
    95         public int NumberOfEntries
       
    96         {
       
    97             get
       
    98             {
       
    99                 int count = 0;
       
   100                 //
       
   101                 lock ( this )
       
   102                 {
       
   103                     foreach ( GenericSymbolCollection collection in this )
       
   104                     {
       
   105                         count += collection.Count;
       
   106                     }
       
   107                 }
       
   108                 // 
       
   109                 return count;
       
   110             }
       
   111         }
       
   112 
       
   113         public abstract int NumberOfCollections
       
   114         {
       
   115             get;
       
   116         }
       
   117         #endregion
       
   118 
       
   119         #region From IGenericSymbolCollectionLookupInterface
       
   120         public GenericSymbolCollection[] CollectionsByHostBinarySearch( string aFileName )
       
   121         {
       
   122             string searchingFor = aFileName.ToUpper();
       
   123             //
       
   124             List<GenericSymbolCollection> ret = new List<GenericSymbolCollection>();
       
   125             //
       
   126             lock ( this )
       
   127             {
       
   128                 for( int i=NumberOfCollections - 1; i>=0; i-- )
       
   129                 {
       
   130                     GenericSymbolCollection collection = this[ i ];
       
   131                     string hostBinName = collection.HostBinaryFileName.ToUpper();
       
   132                     //
       
   133                     if ( hostBinName.Contains( searchingFor ) )
       
   134                     {
       
   135                         ret.Add( collection );
       
   136                     }
       
   137                 }
       
   138             }
       
   139             //
       
   140             return ret.ToArray();
       
   141         }
       
   142         
       
   143         public GenericSymbol EntryByAddress( long aAddress )
       
   144         {
       
   145             GenericSymbolCollection notUsed = null;
       
   146             return EntryByAddress( aAddress, ref notUsed );
       
   147         }
       
   148 
       
   149         public GenericSymbol EntryByAddress( long aAddress, ref GenericSymbolCollection aCollection )
       
   150         {
       
   151             GenericSymbol symbol = null;
       
   152             aCollection = null;
       
   153             //
       
   154             lock ( this )
       
   155             {
       
   156                 // Debugging code
       
   157                 int debug = 0;
       
   158                 if ( debug != 0 )
       
   159                 {
       
   160                     int count = NumberOfCollections;
       
   161                     for ( int i = 0; i < count; i++ )
       
   162                     {
       
   163                         GenericSymbolCollection collection = this[ i ];
       
   164                         string line = string.Empty;
       
   165                         if ( collection.AddressFallsWithinRange( aAddress ) )
       
   166                         {
       
   167                             line = i.ToString( "d8" ) + " * [" + collection.AddressRangeStart.ToString( "x8" ) + " - " + collection.AddressRangeEnd.ToString( "x8" ) + "] " + " [" + ( collection.HostBinaryExists ? "Exists] " : "Notfnd] " ) + collection.HostBinaryFileName;
       
   168                         }
       
   169                         else
       
   170                         {
       
   171                             line = i.ToString( "d8" ) + "   [" + collection.AddressRangeStart.ToString( "x8" ) + " - " + collection.AddressRangeEnd.ToString( "x8" ) + "] " + " [" + ( collection.HostBinaryExists ? "Exists] " : "Notfnd] " ) + collection.HostBinaryFileName;
       
   172                         }
       
   173                         System.Diagnostics.Debug.WriteLine( line );
       
   174                     }
       
   175                 }
       
   176 
       
   177                 // Production search code
       
   178                 if ( Range.Contains( aAddress ) )
       
   179                 {
       
   180                     // Search for a suitable symbol entry that matches aAddress
       
   181                     int count = NumberOfCollections;
       
   182                     for ( int i = 0; i < count; i++ )
       
   183                     {
       
   184                         GenericSymbolCollection collection = this[ i ];
       
   185                         if ( collection.AddressFallsWithinRange( aAddress ) )
       
   186                         {
       
   187                             // Should be able to locate a symbol within this collection...
       
   188                             symbol = collection.SymbolForAddress( aAddress );
       
   189 
       
   190                             aCollection = collection;
       
   191                             aCollection.Tagged = true;
       
   192                             break;
       
   193                         }
       
   194                     }
       
   195                 }
       
   196             }
       
   197             //
       
   198             return symbol;
       
   199         }
       
   200 
       
   201         public GenericSymbolCollection CollectionByAddress( long aAddress )
       
   202         {
       
   203             GenericSymbolCollection ret = null;
       
   204 
       
   205             lock ( this )
       
   206             {
       
   207                 // Search for a suitable symbol entry that matches aAddress
       
   208                 int count = NumberOfCollections;
       
   209                 for ( int i = 0; i < count; i++ )
       
   210                 {
       
   211                     GenericSymbolCollection collection = this[ i ];
       
   212                     if ( collection.AddressFallsWithinRange( aAddress ) )
       
   213                     {
       
   214                         ret = collection;
       
   215                         ret.Tagged = true;
       
   216                         break;
       
   217                     }
       
   218                 }
       
   219             }
       
   220             //
       
   221             return ret;
       
   222         }
       
   223         #endregion
       
   224 
       
   225         #region ITracer Members
       
   226         public void Trace( string aMessage )
       
   227         {
       
   228             if ( iTracer != null )
       
   229             {
       
   230                 iTracer.Trace( aMessage );
       
   231             }
       
   232         }
       
   233 
       
   234         public void Trace( string aFormat, params object[] aParams )
       
   235         {
       
   236             Trace( string.Format( aFormat, aParams ) );
       
   237         }
       
   238         #endregion
       
   239 
       
   240         #region Data members
       
   241         private readonly ITracer iTracer;
       
   242         #endregion
       
   243     }
       
   244 }