crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbolLib/Sources/Symbol/Parser/SymbolFileParser.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 SymbianUtils;
       
    23 using SymbianUtils.Tracer;
       
    24 using SymbolLib.Generics;
       
    25 using SymbolLib.Sources.Symbol.File;
       
    26 using SymbolLib.Sources.Symbol.Engine;
       
    27 using SymbolLib.Sources.Symbol.Symbol;
       
    28 using SymbolLib.Sources.Symbol.Collection;
       
    29 
       
    30 namespace SymbolLib.Sources.Symbol.Parser
       
    31 {
       
    32     #region SymbolCollectionCreator interface
       
    33     internal interface SymbolCollectionCreator
       
    34     {
       
    35         SymbolsForBinary CreateCollection( string aHostFileName );
       
    36     }
       
    37     #endregion
       
    38 
       
    39     #region SymbolEntryCreator interface
       
    40     internal interface SymbolEntryCreator
       
    41     {
       
    42         SymbolSymbol CreateSymbol();
       
    43     }
       
    44     #endregion
       
    45 
       
    46 	internal class SymbolFileParser : AsyncTextFileReader
       
    47 	{
       
    48         #region Delegates & events
       
    49         public delegate void CollectionCreatedHandler( SymbolsForBinary aCollection );
       
    50         public event CollectionCreatedHandler CollectionCreated;
       
    51 
       
    52         public delegate bool CollectionCompletedHandler( SymbolsForBinary aCollection );
       
    53         public event CollectionCompletedHandler CollectionCompleted;
       
    54 
       
    55         public delegate void SymbolCreatedHandler( SymbolSymbol aSymbol );
       
    56         public event SymbolCreatedHandler SymbolCreated;
       
    57         #endregion
       
    58 
       
    59 		#region Constructors
       
    60 		internal SymbolFileParser( SymbolCollectionCreator aCollectionCreator, SymbolEntryCreator aEntryCreator, string aFileName, ITracer aTracer )
       
    61 		:	base( aFileName, aTracer )
       
    62 		{
       
    63             iCollectionCreator = aCollectionCreator;
       
    64             iEntryCreator = aEntryCreator;
       
    65 		}
       
    66 		#endregion
       
    67 
       
    68         #region Constants
       
    69         public const bool KCollectionCompletedAndAbortParsing = false;
       
    70         public const bool KCollectionCompletedAndContinueParsing = true;
       
    71         #endregion
       
    72 
       
    73         #region Parsing API
       
    74         public void Read( TSynchronicity aSynchronicity )
       
    75         {
       
    76             base.StartRead( aSynchronicity );
       
    77         }
       
    78 		#endregion
       
    79 
       
    80         #region Properties
       
    81         public bool ContainedAtLeastOneCollectionFileName
       
    82         {
       
    83             get { return iContainedAtLeastOneCollectionFileName; }
       
    84         }
       
    85         #endregion
       
    86 
       
    87         #region From AsyncTextReader
       
    88         protected override bool ImmediateAbort()
       
    89         {
       
    90             return iImmediateAbort;
       
    91         }
       
    92 
       
    93         protected override void HandleReadStarted()
       
    94         {
       
    95 #if PROFILING
       
    96             System.DateTime startTime = DateTime.Now;
       
    97 
       
    98             using ( System.IO.StreamReader reader = new StreamReader( this.FileName ) )
       
    99             {
       
   100                 string line = reader.ReadLine();
       
   101                 while ( line != null )
       
   102                 {
       
   103                     line = reader.ReadLine();
       
   104                 }
       
   105             }
       
   106 
       
   107             System.DateTime endTime = DateTime.Now;
       
   108             long tickDuration = ( ( endTime.Ticks - startTime.Ticks ) / 100 );
       
   109             System.Diagnostics.Debug.WriteLine( "SIMPLE READ COMPLETE - " + tickDuration.ToString( "d6" ) );
       
   110 #endif
       
   111 
       
   112             base.HandleReadStarted();
       
   113         }
       
   114 
       
   115         protected override void HandleFilteredLine( string aLine )
       
   116 		{
       
   117             if ( aLine != null )
       
   118             {
       
   119                 int len = aLine.Length;
       
   120                 if ( len > 5 && aLine.Substring( 0, 5 ) == "From " )
       
   121 			    {
       
   122                     aLine = aLine.Substring( 5 ).TrimStart();
       
   123                     HandleStartOfNewCollection( aLine );
       
   124                 }
       
   125                 else
       
   126                 {
       
   127                     HandleSymbolLine( aLine );
       
   128                 }
       
   129             }
       
   130         }
       
   131 
       
   132 		protected override void HandleReadCompleted()
       
   133 		{
       
   134 			try
       
   135 			{
       
   136                 OnCollectionCompleted( iCurrentCollection );
       
   137 			}
       
   138 			finally
       
   139 			{
       
   140                 base.HandleReadCompleted();
       
   141 			}
       
   142 		}
       
   143 
       
   144 		protected override void HandleReadException( Exception aException )
       
   145 		{
       
   146             base.HandleReadException( aException );
       
   147             //
       
   148             System.Windows.Forms.MessageBox.Show( aException.StackTrace, aException.Message );
       
   149 		}
       
   150 		#endregion
       
   151 
       
   152 		#region Internal methods
       
   153         private void OnCollectionCreated( SymbolsForBinary aCollection )
       
   154         {
       
   155             if ( CollectionCreated != null && aCollection != null )
       
   156             {
       
   157                 CollectionCreated( aCollection );
       
   158             }
       
   159         }
       
   160 
       
   161         private void OnCollectionCompleted( SymbolsForBinary aCollection )
       
   162         {
       
   163             if ( CollectionCompleted != null && aCollection != null )
       
   164             {
       
   165                 // If returns immediate abort then we will stop parsing
       
   166                 // straight away.
       
   167                 iImmediateAbort = ( CollectionCompleted( aCollection ) == KCollectionCompletedAndAbortParsing );
       
   168             }
       
   169         }
       
   170 
       
   171         private void HandleStartOfNewCollection( string aHostFileName )
       
   172         {
       
   173             // Current collection is now complete
       
   174             OnCollectionCompleted( iCurrentCollection );
       
   175             
       
   176             // We will create the new collection once we see the first symbol
       
   177             iCurrentCollection = null;
       
   178 
       
   179             // Cache the name of the new collection
       
   180             iCurrentCollectionHostFileName = aHostFileName;
       
   181 
       
   182             // We've seen at least one "From" line.
       
   183             iContainedAtLeastOneCollectionFileName = true;
       
   184         }
       
   185 
       
   186         private void HandleSymbolLine( string aLine )
       
   187         {
       
   188             try
       
   189             {
       
   190                 // If we have not yet made a collection for this symbol, then do so now
       
   191                 CreateNewCollection();
       
   192 
       
   193                 SymbolSymbol entry = iEntryCreator.CreateSymbol();
       
   194                 bool entryOk = entry.Parse( aLine );
       
   195                 if ( entryOk && SymbolCreated != null )
       
   196                 {
       
   197 
       
   198                     // Notify that we created a symbol
       
   199                     SymbolCreated( entry );
       
   200                 }
       
   201             }
       
   202             catch( GenericSymbolicCreationException )
       
   203             {
       
   204 #if TRACE
       
   205                 System.Diagnostics.Debug.WriteLine( "SymbolParseException: " + aLine );
       
   206 #endif
       
   207             }
       
   208         }
       
   209 
       
   210         private void CreateNewCollection()
       
   211         {
       
   212             if ( iCurrentCollection == null )
       
   213             {
       
   214                 System.Diagnostics.Debug.Assert( !string.IsNullOrEmpty( iCurrentCollectionHostFileName ) );
       
   215 
       
   216                 // Combine symbol file drive letter with binary name from the symbol file itself.
       
   217                 string hostBinaryFileName = Path.GetPathRoot( base.FileName );
       
   218                 if ( iCurrentCollectionHostFileName.StartsWith( @"\" ) )
       
   219                 {
       
   220                     iCurrentCollectionHostFileName = iCurrentCollectionHostFileName.Substring( 1 );
       
   221                 }
       
   222                 hostBinaryFileName = Path.Combine( hostBinaryFileName, iCurrentCollectionHostFileName );
       
   223 
       
   224                 iCurrentCollection = iCollectionCreator.CreateCollection( hostBinaryFileName );
       
   225                 OnCollectionCreated( iCurrentCollection );
       
   226                 iCurrentCollectionHostFileName = string.Empty;
       
   227             }
       
   228         }
       
   229         #endregion
       
   230 
       
   231 		#region Data members
       
   232         private SymbolsForBinary iCurrentCollection = null;
       
   233         private readonly SymbolEntryCreator iEntryCreator;
       
   234         private readonly SymbolCollectionCreator iCollectionCreator;
       
   235         private bool iImmediateAbort = false;
       
   236         private string iCurrentCollectionHostFileName = string.Empty;
       
   237         private bool iContainedAtLeastOneCollectionFileName = false;
       
   238         #endregion
       
   239 	}
       
   240 }