crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianSymbolLib/Plugins/SLPluginMap/Reader/GCCE/GCCEMapFileReader.cs
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     1 /*
       
     2 * Copyright (c) 2004-2008 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.IO;
       
    20 using System.Collections.Generic;
       
    21 using System.Text;
       
    22 using System.Text.RegularExpressions;
       
    23 using SymbianStructuresLib.Debug.Symbols;
       
    24 using SymbianSymbolLib.SourceManagement.Source;
       
    25 using SymbianSymbolLib.SourceManagement.Provisioning;
       
    26 using SymbianUtils;
       
    27 using SymbianUtils.FileTypes;
       
    28 using SymbianUtils.Tracer;
       
    29 
       
    30 namespace SLPluginMap.Reader.GCCE
       
    31 {
       
    32     internal class GCCEMapFileReader : MapReader
       
    33 	{
       
    34         #region Delegates & events
       
    35         #endregion
       
    36 
       
    37         #region Constructors
       
    38         public GCCEMapFileReader( SymSource aSource, ITracer aTracer )
       
    39             : base( aSource, aTracer )
       
    40 		{
       
    41             base.TrimLine = false;
       
    42             iSymbolParser = new GCCESymbolCreator( this, base.Collection );
       
    43 		}
       
    44 		#endregion
       
    45 
       
    46         #region API
       
    47         #endregion
       
    48 
       
    49         #region Properties
       
    50 		#endregion
       
    51 
       
    52 		#region From AsyncTextReaderBase
       
    53 		protected override void HandleFilteredLine( string aLine )
       
    54 		{
       
    55             switch ( iStateGlobal )
       
    56 			{
       
    57             case TGlobalState.EWaitingForImage_ER_RO_Base:
       
    58                 ParseGlobalBaseAddress( aLine );
       
    59                 break;
       
    60             case TGlobalState.EProcessingSymbols:
       
    61                 ParseSymbols( aLine );
       
    62                 break;
       
    63             default:
       
    64                 break;
       
    65             }
       
    66 		}
       
    67 
       
    68         protected override void HandleReadCompleted()
       
    69         {
       
    70             try
       
    71             {
       
    72                 SortAndSize();
       
    73             }
       
    74             finally
       
    75             {
       
    76                 base.HandleReadCompleted();
       
    77             }
       
    78         }
       
    79 		#endregion
       
    80 
       
    81         #region Internal methods
       
    82 		private void ParseGlobalBaseAddress( string aLine )
       
    83 		{
       
    84             Match m = KGlobalBaseAddressRegEx.Match( aLine );
       
    85             if ( m.Success )
       
    86             {
       
    87                 string value = m.Groups[ 1 ].Value;
       
    88                 base.GlobalBaseAddress = uint.Parse( value, System.Globalization.NumberStyles.HexNumber );
       
    89                 iStateGlobal = TGlobalState.EProcessingSymbols;
       
    90             }
       
    91 		}
       
    92 
       
    93 		private void ParseSymbols( string aLine )
       
    94 		{
       
    95 			try
       
    96 			{
       
    97                 // It's the start of a section...
       
    98                 Match m = KSectionRegEx.Match( aLine );
       
    99                 if ( m.Success )
       
   100                 {
       
   101                     iSectionType = GetSection( aLine );
       
   102                     iLastSeenObjectName = string.Empty;
       
   103                 }
       
   104                 else if ( iSectionType != TSection.ESectionUnsupported )
       
   105                 {
       
   106                     m = KSizeAndObjectRegEx.Match( aLine );
       
   107                     if ( m.Success )
       
   108                     {
       
   109                         iLastSeenObjectName = m.Groups[ "Object" ].Value.Trim();
       
   110                     }
       
   111                     else
       
   112                     {
       
   113                         Symbol sym = iSymbolParser.Parse( aLine );
       
   114                         if ( sym != null )
       
   115                         {
       
   116                             // Update object with cached information
       
   117                             sym.Object = iLastSeenObjectName;
       
   118                             iUnsortedSymbols.Add( sym );
       
   119                         }
       
   120                     }
       
   121                 }
       
   122 			}
       
   123 			catch( Exception )
       
   124 			{
       
   125 			}
       
   126 		}
       
   127 
       
   128         private void ListSymbols( IEnumerable<Symbol> aList )
       
   129         {
       
   130             foreach ( Symbol sym in aList )
       
   131             {
       
   132                 string line = sym.ToString();
       
   133                 if ( sym.InstructionSet == SymbianStructuresLib.Arm.TArmInstructionSet.ETHUMB )
       
   134                 {
       
   135                     line = "T " + line;
       
   136                 }
       
   137                 else
       
   138                 {
       
   139                     line = "  " + line;
       
   140                 }
       
   141                 //
       
   142                 System.Diagnostics.Debug.WriteLine( line );
       
   143             }
       
   144         }
       
   145 
       
   146         private void SortAndSize()
       
   147         {
       
   148             // Sort the symbols into ascending address order
       
   149             Comparison<Symbol> comparer = delegate( Symbol aLeft, Symbol aRight )
       
   150             {
       
   151                 // compare by address
       
   152                 int ret = aLeft.Address.CompareTo( aRight.Address );
       
   153                 return ret;
       
   154             };
       
   155             iUnsortedSymbols.Sort( comparer );
       
   156 
       
   157             // Subsume duplicates
       
   158             int count = iUnsortedSymbols.Count;
       
   159             for ( int i = count - 1; i >= 1; i-- )
       
   160             {
       
   161                 Symbol sym1 = iUnsortedSymbols[ i - 0];
       
   162                 Symbol sym2 = iUnsortedSymbols[ i - 1 ];
       
   163                 //
       
   164                 if ( sym1.Name == sym2.Name && sym1.Object == sym2.Object )
       
   165                 {
       
   166                     // Since the symbol is the same and since we do not have any
       
   167                     // size information for GCCE map files, then we can safely
       
   168                     // discard one of the symbols.
       
   169                     iUnsortedSymbols.RemoveAt( i );
       
   170                 }
       
   171             }
       
   172 
       
   173             // Now calculate sizes
       
   174             count = iUnsortedSymbols.Count;
       
   175             for ( int i = 0; i < count - 1; i++ )
       
   176             {
       
   177                 Symbol sym1 = iUnsortedSymbols[ i + 0 ];
       
   178                 Symbol sym2 = iUnsortedSymbols[ i + 1 ];
       
   179                 
       
   180                 // Calculate the symbol size based upon the address of the next symbol.
       
   181                 // This may not be totally accurate, but it's the best we can do
       
   182                 sym1.Size = ( sym2.Address - sym1.Address );
       
   183 
       
   184                 // Save the symbol
       
   185                 base.ReportSymbol( sym1 );
       
   186             }
       
   187         }
       
   188 
       
   189         private static TSection GetSection( string aText )
       
   190         {
       
   191             TSection ret = TSection.ESectionUnsupported;
       
   192             //
       
   193             Match m = KSectionRegEx.Match( aText );
       
   194             if ( m.Success )
       
   195             {
       
   196                 string sectionName = m.Groups[ 0 ].Value;
       
   197                 switch ( sectionName )
       
   198                 {
       
   199                 case ".text":
       
   200                     ret = TSection.ESectionText;
       
   201                     break;
       
   202                 case ".emb_text":
       
   203                     ret = TSection.ESectionEmbText;
       
   204                     break;
       
   205                 case ".rodata":
       
   206                     ret = TSection.ESectionReadOnlyData;
       
   207                     break;
       
   208                 default:
       
   209                     break;
       
   210                 }
       
   211             }
       
   212             //
       
   213             return ret;
       
   214         }
       
   215 		#endregion
       
   216 
       
   217 		#region Internal enumerations
       
   218 		private enum TGlobalState
       
   219 		{
       
   220 			EWaitingForImage_ER_RO_Base = 0,
       
   221 			EProcessingSymbols
       
   222 		}
       
   223 
       
   224         private enum TSection
       
   225         {
       
   226             ESectionUnsupported = -1,
       
   227             ESectionText = 0,
       
   228             ESectionEmbText,
       
   229             ESectionReadOnlyData
       
   230         }
       
   231 		#endregion
       
   232 
       
   233         #region Internal constants
       
   234         private readonly static Regex KGlobalBaseAddressRegEx = new Regex(
       
   235               @"\s*0x(?<Address>[A-Fa-f0-9]{8})\s+PROVIDE \(Image\$\$ER_RO\$\$Base",
       
   236             RegexOptions.Multiline
       
   237             | RegexOptions.CultureInvariant
       
   238             | RegexOptions.Compiled
       
   239             );
       
   240 
       
   241         private readonly static Regex KSectionRegEx = new Regex(
       
   242               "^\\.([a-zA-Z0-9_\\$\\.]*)",
       
   243             RegexOptions.Multiline
       
   244             | RegexOptions.CultureInvariant
       
   245             | RegexOptions.Compiled
       
   246             );
       
   247         private readonly static Regex KSizeAndObjectRegEx = new Regex(
       
   248               "(?<Prefix>.+?)0x(?<Address>[A-Fa-f0-9]{8})\\s+0x(?<Size>[A-F" +
       
   249               "a-f0-9]{1,8})\\s(?<Object>.+)",
       
   250             RegexOptions.Multiline
       
   251             | RegexOptions.CultureInvariant
       
   252             | RegexOptions.Compiled
       
   253             );
       
   254         #endregion
       
   255 
       
   256         #region Data members
       
   257 		private TGlobalState iStateGlobal = TGlobalState.EWaitingForImage_ER_RO_Base;
       
   258         private readonly GCCESymbolCreator iSymbolParser;
       
   259         private List<Symbol> iUnsortedSymbols = new List<Symbol>();
       
   260         private string iLastSeenObjectName = string.Empty;
       
   261         private TSection iSectionType = TSection.ESectionUnsupported;
       
   262 		#endregion
       
   263 	}
       
   264 }