crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbolLib/SearchAndReplace/SymbolicSearchAndReplaceParser.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.Collections;
       
    19 using System.Collections.Specialized;
       
    20 using System.Text.RegularExpressions;
       
    21 using System.IO;
       
    22 using System.Text;
       
    23 using SymbianUtils;
       
    24 using SymbolLib.Engines;
       
    25 using SymbolLib.CodeSegDef;
       
    26 
       
    27 namespace SymbolLib.SearchAndReplace
       
    28 {
       
    29 	public class SymbolicSearchAndReplaceParser : AsyncTextFileReader
       
    30 	{
       
    31 		#region Constructors & destructor
       
    32 		public SymbolicSearchAndReplaceParser( SymbolManager aSymbolManager, string aSourceFile, string aDestinationFile )
       
    33         :   this( aSymbolManager, aSourceFile, aDestinationFile, string.Empty )
       
    34 		{
       
    35 		}
       
    36         
       
    37         public SymbolicSearchAndReplaceParser( SymbolManager aSymbolManager, string aSourceFile, string aDestinationFile, string aPrefix )
       
    38             : base( aSourceFile, new AsyncTextReaderPrefix( aPrefix ), true )
       
    39         {
       
    40             iSymbolManager = aSymbolManager;
       
    41             iWriter = new StreamWriter( aDestinationFile, false );
       
    42         }
       
    43         #endregion
       
    44 
       
    45         #region API
       
    46         public void SearchAndReplace()
       
    47         {
       
    48             base.AsyncRead();
       
    49         }
       
    50         #endregion
       
    51 
       
    52         #region Properties
       
    53         public SymbolManager SymbolManager
       
    54 		{
       
    55 			get { return iSymbolManager; }
       
    56 		}
       
    57 		#endregion
       
    58 
       
    59 		#region From AsyncTextReaderBase
       
    60         protected override void HandleFilteredLine( string aLine )
       
    61 		{
       
    62 			const string KHexChars = "abcdefABCDEF1234567890";
       
    63 
       
    64             int startPos = 0;
       
    65             StringBuilder line = new StringBuilder();
       
    66 
       
    67             CodeSegDefinition def = iSymbolManager.ROFSEngine.DefinitionParser.ParseAndResolveDefinition( aLine );
       
    68             if ( def != null )
       
    69             {
       
    70                 SymbolManager.LoadDynamicCodeSegment( def, TSynchronicity.ESynchronous );
       
    71                 line.Append( aLine );
       
    72             }
       
    73             else
       
    74             {
       
    75                 // Look through the line looking for 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f in runs of 8 characters
       
    76                 MatchCollection collection = iAddressRegEx.Matches( aLine );
       
    77                 if ( collection != null && collection.Count > 0 )
       
    78                 {
       
    79                     foreach ( Match m in collection )
       
    80                     {
       
    81                         // Now get the stack address
       
    82                         CaptureCollection captures = m.Captures;
       
    83                         foreach ( Capture capture in captures )
       
    84                         {
       
    85                             string matchText = capture.Value.Trim();
       
    86 
       
    87                             // Take all the initial text
       
    88                             int capturePos = capture.Index;
       
    89 
       
    90                             // Check whether it is a discrete word
       
    91                             bool checkForSymbolMatch = true;
       
    92                             if ( capturePos > 0 )
       
    93                             {
       
    94                                 // Check previous character wasn't a match from our group
       
    95                                 char prevCharacter = aLine[ capturePos - 1 ];
       
    96                                 checkForSymbolMatch = ( KHexChars.IndexOf( prevCharacter ) < 0 );
       
    97                             }
       
    98                             if ( checkForSymbolMatch && ( capturePos + matchText.Length < aLine.Length ) )
       
    99                             {
       
   100                                 // Check next character too
       
   101                                 char nextCharacter = aLine[ capturePos + matchText.Length ];
       
   102                                 checkForSymbolMatch = ( KHexChars.IndexOf( nextCharacter ) < 0 );
       
   103                             }
       
   104 
       
   105                             // Take any preceeding text...
       
   106                             if ( capturePos > 0 )
       
   107                             {
       
   108                                 int length = capturePos - startPos;
       
   109                                 line.Append( aLine.Substring( startPos, length ) );
       
   110                                 startPos = capturePos;
       
   111                             }
       
   112 
       
   113                             // Always store the original text
       
   114                             line.Append( matchText );
       
   115 
       
   116                             // Decide if we can try to find a symbol...
       
   117                             if ( checkForSymbolMatch )
       
   118                             {
       
   119                                 // And now take the text as a symbol (if we have
       
   120                                 // a match).
       
   121                                 long address = SymbianUtils.NumberBaseUtils.TextToDecimalNumber( matchText, NumberBaseUtils.TNumberBase.EHex );
       
   122                                 Generics.GenericSymbol symbol = iSymbolManager.EntryByAddress( address );
       
   123 
       
   124                                 if ( symbol != null )
       
   125                                 {
       
   126                                     line.Append( " [ " + symbol.Symbol + " ]" );
       
   127                                 }
       
   128                                 else if ( iSymbolManager.AddressInRange( address ) )
       
   129                                 {
       
   130                                     line.Append( " [ #UNKNOWN# ]" );
       
   131                                 }
       
   132                             }
       
   133                             else
       
   134                             {
       
   135                                 // Not a match, just take the original match text and move on...
       
   136                             }
       
   137 
       
   138                             startPos += matchText.Length;
       
   139                         }
       
   140                     }
       
   141                 }
       
   142 
       
   143                 // Remember to add anything that is left at the end...
       
   144                 string remainder = aLine.Substring( startPos );
       
   145                 line.Append( remainder );
       
   146             }
       
   147 
       
   148             iWriter.WriteLine( line.ToString() );
       
   149         }
       
   150 
       
   151 		protected override void HandleReadCompleted()
       
   152 		{
       
   153 			try
       
   154 			{
       
   155                 if ( iWriter != null )
       
   156                 {
       
   157                     iWriter.Close();
       
   158                 }
       
   159                 iWriter = null;
       
   160 			}
       
   161 			finally
       
   162 			{
       
   163 				base.HandleReadCompleted();
       
   164 			}
       
   165 		}
       
   166 		#endregion
       
   167 
       
   168         #region From DisposableObject
       
   169         protected override void CleanupManagedResources()
       
   170         {
       
   171             try
       
   172             {
       
   173                 if ( iWriter != null )
       
   174                 {
       
   175                     iWriter.Close();
       
   176                 }
       
   177                 iWriter = null;
       
   178             }
       
   179             finally
       
   180             {
       
   181                 base.CleanupManagedResources();
       
   182             }
       
   183         }
       
   184         #endregion
       
   185 
       
   186         #region Data members
       
   187         private readonly SymbolManager iSymbolManager;
       
   188 		private static Regex iAddressRegEx = new Regex( @"[a-fA-F0-9]{8}", RegexOptions.IgnoreCase );
       
   189         private StreamWriter iWriter;
       
   190 		#endregion
       
   191 	}
       
   192 }
       
   193