crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbolLib/Sources/Map/Engine/MapFileEngineCollection.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;
       
    22 using System.Collections.Generic;
       
    23 using SymbianUtils;
       
    24 using SymbianUtils.Range;
       
    25 using SymbianUtils.Tracer;
       
    26 using SymbolLib.CodeSegDef;
       
    27 using SymbolLib.Generics;
       
    28 using SymbolLib.Engines;
       
    29 using SymbolLib.Sources.Map.Symbol;
       
    30 using SymbolLib.Sources.Map.File;
       
    31 using SymbolLib.Sources.Map.Parser;
       
    32 
       
    33 namespace SymbolLib.Sources.Map.Engine
       
    34 {
       
    35     internal class MapFileEngineCollection : GenericSymbolEngine
       
    36     {
       
    37         #region Delegates & Events
       
    38         public event AsyncReaderBase.Observer Observer;
       
    39         #endregion
       
    40 
       
    41         #region Constructors & destructor
       
    42         internal MapFileEngineCollection( ITracer aTracer )
       
    43             : base( aTracer )
       
    44         {
       
    45             iCodeSegResolver = new CodeSegResolver( aTracer );
       
    46             iCodeSegParser = new CodeSegDefinitionParser( iCodeSegResolver );
       
    47         }
       
    48         #endregion
       
    49 
       
    50         #region API
       
    51         public static TFileType IsSupported( string aFileName )
       
    52         {
       
    53             TFileType ret = TFileType.EFileNotSupported;
       
    54             //
       
    55             try
       
    56             {
       
    57                 using ( StreamReader reader = new StreamReader( aFileName ) )
       
    58                 {
       
    59                     const int maxLines = 100;
       
    60                     //
       
    61                     bool foundHeader = false;
       
    62                     bool foundBody = false;
       
    63                     //
       
    64                     int lineCounter = 0;
       
    65                     string line = reader.ReadLine();
       
    66                     while ( line != null && !( foundHeader && foundBody ) && lineCounter < maxLines )
       
    67                     {
       
    68                         if ( line.StartsWith( "ARM Linker, RVCT" ) )
       
    69                         {
       
    70                             foundHeader = true;
       
    71                         }
       
    72                         else if ( line.Contains( "Symbol Name" ) && line.Contains( "Value" ) && line.Contains( "Object" ) && line.Contains( "Section" ) )
       
    73                         {
       
    74                             foundBody = true;
       
    75                         }
       
    76 
       
    77                         line = reader.ReadLine();
       
    78                         ++lineCounter;
       
    79                     }
       
    80                     //
       
    81                     if ( foundBody && foundHeader )
       
    82                     {
       
    83                         ret = TFileType.EFileMap;
       
    84                     }
       
    85                 }
       
    86             }
       
    87             catch ( Exception )
       
    88             {
       
    89             }
       
    90             //
       
    91             return ret;
       
    92         }
       
    93 
       
    94         public MapFileEngine LoadFromFile( string aMapFileName, TSynchronicity aSynchronicity )
       
    95         {
       
    96             // Check if already exists
       
    97             MapFileEngine engine = null;
       
    98             //
       
    99             lock ( this )
       
   100             {
       
   101                 engine = FindByMapFileName( aMapFileName );
       
   102                 if ( engine != null )
       
   103                 {
       
   104                     iFiles.Remove( engine );
       
   105                 }
       
   106 
       
   107                 engine = new MapFileEngine( this );
       
   108                 engine.Observer += new AsyncReaderBase.Observer( MapEngine_ObserverProxy );
       
   109                 engine.LoadFromFile( aMapFileName, aSynchronicity );
       
   110                 iFiles.Add( engine );
       
   111             }
       
   112 
       
   113             return engine;
       
   114         }
       
   115 
       
   116         public bool Load( CodeSegDefinition aDefinition, TSynchronicity aSynchronicity )
       
   117         {
       
   118             bool ret = false;
       
   119             //
       
   120             if ( string.IsNullOrEmpty( aDefinition.MapFileName ) || !aDefinition.MapFileExists )
       
   121             {
       
   122             }
       
   123             else
       
   124             {
       
   125                 // First pass - try to find map engine that matches the specified
       
   126                 // PC file name. 
       
   127                 string mapFileName = aDefinition.MapFileName;
       
   128                 if ( aDefinition.MapFileExists )
       
   129                 {
       
   130                     System.Diagnostics.Debug.WriteLine( "   LOAD {M}: " + aDefinition.ToString() );
       
   131 
       
   132                     MapFileEngine engine = FindByMapFileName( mapFileName );
       
   133                     if ( engine != null )
       
   134                     {
       
   135                         engine.Load( aDefinition );
       
   136                         ret = true;
       
   137                     }
       
   138                     else
       
   139                     {
       
   140                         // Map file engine doesn't exist for the specified code segment.
       
   141                         // Can we load it from file?
       
   142                         engine = LoadFromFile( aDefinition.MapFileName, aSynchronicity );
       
   143                         if ( engine != null )
       
   144                         {
       
   145                             engine.Load( aDefinition );
       
   146                             ret = true;
       
   147                         }
       
   148                     }
       
   149                 }
       
   150             }
       
   151             //
       
   152             return ret;
       
   153         }
       
   154 
       
   155         public bool Unload( CodeSegDefinition aDefinition )
       
   156         {
       
   157             bool ret = false;
       
   158             //
       
   159             foreach ( MapFileEngine engine in iFiles )
       
   160             {
       
   161                 if ( engine.Unload( aDefinition ) )
       
   162                 {
       
   163                     ret = true;
       
   164                     break;
       
   165                 }
       
   166             }
       
   167             //
       
   168             return ret;
       
   169         }
       
   170 
       
   171         public void UnloadAll()
       
   172         {
       
   173             foreach ( MapFileEngine engine in iFiles )
       
   174             {
       
   175                 engine.UnloadAll();
       
   176             }
       
   177         }
       
   178 
       
   179         public bool IsLoaded( CodeSegDefinition aDefinition )
       
   180         {
       
   181             bool ret = false;
       
   182             //
       
   183             foreach ( MapFileEngine engine in iFiles )
       
   184             {
       
   185                 if ( engine.IsLoaded( aDefinition ) )
       
   186                 {
       
   187                     ret = true;
       
   188                     break;
       
   189                 }
       
   190             }
       
   191             //
       
   192             return ret;
       
   193         }
       
   194 
       
   195         public string MapFileName( int aIndex )
       
   196         {
       
   197             return iFiles[ aIndex ].MapFileName;
       
   198         }
       
   199         #endregion
       
   200 
       
   201         #region Properties
       
   202         public int MapFileCount
       
   203         {
       
   204             get { return iFiles.Count; }
       
   205         }
       
   206 
       
   207         public CodeSegResolver CodeSegResolver
       
   208         {
       
   209             get { return iCodeSegResolver; }
       
   210         }
       
   211 
       
   212         public CodeSegDefinitionParser CodeSegParser
       
   213         {
       
   214             get { return iCodeSegParser; }
       
   215         }
       
   216 
       
   217         public GenericSymbolEngine MapFileEngineAt( int aIndex )
       
   218         {
       
   219             return iFiles[ aIndex ];
       
   220         }
       
   221 
       
   222         public string[] BinaryFileNames
       
   223         {
       
   224             get
       
   225             {
       
   226                 List<string> fileNames = new List<string>( iFiles.Count );
       
   227                 //
       
   228                 foreach ( MapFileEngine engine in iFiles )
       
   229                 {
       
   230                     // Map files only contain symbols for one binary
       
   231                     fileNames.Add( engine[ 0 ].HostBinaryFileName );
       
   232                 }
       
   233                 //
       
   234                 return fileNames.ToArray();
       
   235             }
       
   236         }
       
   237 
       
   238         public string[] MapFileNames
       
   239         {
       
   240             get
       
   241             {
       
   242                 List<string> fileNames = new List<string>( iFiles.Count );
       
   243                 //
       
   244                 foreach ( MapFileEngine engine in iFiles )
       
   245                 {
       
   246                     fileNames.Add( engine.MapFileName );
       
   247                 }
       
   248                 //
       
   249                 return fileNames.ToArray();
       
   250             }
       
   251         }
       
   252         #endregion
       
   253 
       
   254         #region From GenericSymbolEngine
       
   255         public override void Reset()
       
   256         {
       
   257             iFiles.Clear();
       
   258             iCodeSegResolver.Clear();
       
   259         }
       
   260 
       
   261         public override bool IsLoaded( string aFileName )
       
   262         {
       
   263             MapFileEngine engine = FindByMapFileName( aFileName );
       
   264             return engine != null;
       
   265         }
       
   266 
       
   267         public override bool IsReady
       
   268         {
       
   269             get
       
   270             {
       
   271                 int readyCount = 0;
       
   272                 //
       
   273                 foreach ( MapFileEngine engine in iFiles )
       
   274                 {
       
   275                     if ( engine.IsReady )
       
   276                     {
       
   277                         ++readyCount;
       
   278                     }
       
   279                 }
       
   280                 //
       
   281                 return ( readyCount == iFiles.Count );
       
   282             }
       
   283         }
       
   284 
       
   285         public override GenericSymbolCollection this[ int aIndex ]
       
   286         {
       
   287             get
       
   288             {
       
   289                 // Map files contain only a single symbol collection;
       
   290                 GenericSymbolCollection ret = iFiles[ aIndex ][ 0 ];
       
   291                 return ret;
       
   292             }
       
   293         }
       
   294 
       
   295         public override AddressRange Range
       
   296         {
       
   297             get
       
   298             {
       
   299                 AddressRange ret = new AddressRange();
       
   300                 //
       
   301                 foreach ( MapFileEngine engine in iFiles )
       
   302                 {
       
   303                     ret.Update( engine.Range );
       
   304                 }
       
   305                 //
       
   306                 return ret;
       
   307             }
       
   308         }
       
   309 
       
   310         internal override void UnloadUntagged()
       
   311         {
       
   312         }
       
   313         #endregion
       
   314 
       
   315         #region From IGenericSymbolCollectionStatisticsInterface
       
   316         public override int NumberOfCollections
       
   317         {
       
   318             get
       
   319             {
       
   320                 // Map files contain only a single symbol collection
       
   321                 int count = iFiles.Count;
       
   322                 return count;
       
   323             }
       
   324         }
       
   325         #endregion
       
   326 
       
   327         #region Event handlers
       
   328         private void MapEngine_ObserverProxy( SymbianUtils.AsyncReaderBase.TEvent aEvent, SymbianUtils.AsyncReaderBase aSender )
       
   329         {
       
   330             if ( Observer != null )
       
   331             {
       
   332                 Observer( aEvent, aSender );
       
   333             }
       
   334         }
       
   335         #endregion
       
   336 
       
   337         #region Internal methods
       
   338         private MapFileEngine FindByMapFileName( string aFileName )
       
   339         {
       
   340             MapFileEngine ret = null;
       
   341             //
       
   342             foreach ( MapFileEngine engine in iFiles )
       
   343             {
       
   344                 if ( engine.MapFileName.ToLower() == aFileName.ToLower() )
       
   345                 {
       
   346                     ret = engine;
       
   347                     break;
       
   348                 }
       
   349             }
       
   350             //
       
   351             return ret;
       
   352         }
       
   353         #endregion
       
   354 
       
   355         #region Data members
       
   356         private readonly CodeSegResolver iCodeSegResolver;
       
   357         private readonly CodeSegDefinitionParser iCodeSegParser;
       
   358         private List<MapFileEngine> iFiles = new List<MapFileEngine>();
       
   359         #endregion
       
   360     }
       
   361 }