crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbolLib/Generic/Symbol/GenericSymbol.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.Text;
       
    20 using System.Text.RegularExpressions;
       
    21 using SymbianUtils.Range;
       
    22 using SymbianUtils;
       
    23 
       
    24 namespace SymbolLib.Generics
       
    25 {
       
    26     #region Exception
       
    27 	public class GenericSymbolicCreationException : Exception
       
    28 	{
       
    29 	}
       
    30     #endregion
       
    31 
       
    32 	public abstract class GenericSymbol
       
    33 	{
       
    34 		#region Enumerations
       
    35 		public enum TAddressType
       
    36 		{
       
    37             // Do not change the order - these are priority based with
       
    38             // the most important symbol type appearing with a larger
       
    39             // value
       
    40 			EAddressTypeUnknown = -1,
       
    41             EAddressTypeReadOnlySymbol = 0,
       
    42 			EAddressTypeKernelGlobalVariable,
       
    43             EAddressTypeSubObject,
       
    44             EAddressTypeLabel,
       
    45 			EAddressTypeRAMSymbol,
       
    46             EAddressTypeROMSymbol,
       
    47 		}
       
    48 
       
    49         public enum TSourceType
       
    50         {
       
    51             ESourceTypeFileSymbol = 0,
       
    52             ESourceTypeFileMap
       
    53         }
       
    54 
       
    55         public enum TInstructionSet
       
    56         {
       
    57             EInstructionSetARM = 0,
       
    58             EInstructionSetTHUMB
       
    59         }
       
    60 		#endregion
       
    61 
       
    62         #region Public constants
       
    63         public const int KNullEntryAddress = 0;
       
    64         public const string KNonMatchingObjectName = "Unknown Object";
       
    65         public const string KNonMatchingSymbolName = "Unknown Symbol";
       
    66         #endregion
       
    67 
       
    68         #region Constructors
       
    69         protected GenericSymbol( GenericSymbolCollection aCollection )
       
    70 		{
       
    71             iCollection = aCollection;
       
    72 		}
       
    73 		#endregion
       
    74 
       
    75 		#region Abstract API
       
    76         public abstract TSourceType SourceType { get; }
       
    77 
       
    78 		public abstract bool Parse( string aLine );
       
    79 		#endregion
       
    80 
       
    81 		#region Framework API
       
    82 		public virtual bool FallsWithinDomain(long aAddress)
       
    83 		{
       
    84 			return (aAddress >= Address && aAddress <= EndAddress);
       
    85 		}
       
    86 
       
    87 		public virtual bool IsSubObject
       
    88 		{
       
    89 			get
       
    90 			{
       
    91 				bool ret = ( AddressType == TAddressType.EAddressTypeSubObject );
       
    92 				return ret;
       
    93 			}
       
    94 		}
       
    95 
       
    96 		public virtual bool IsLabel
       
    97 		{
       
    98 			get
       
    99 			{
       
   100 				bool ret = ( AddressType == TAddressType.EAddressTypeLabel );
       
   101 				return ret;
       
   102 			}
       
   103 		}
       
   104 
       
   105 		public virtual bool IsSymbol
       
   106 		{
       
   107 			get
       
   108 			{
       
   109 				bool ret = ( Collection.BaseAddress == 0 || AddressType == TAddressType.EAddressTypeRAMSymbol || AddressType == TAddressType.EAddressTypeROMSymbol );
       
   110 				return ret;
       
   111 			}
       
   112 		}
       
   113 
       
   114 		public virtual MemoryModel.TMemoryModelType MemoryModelType // Guess
       
   115 		{
       
   116 			get
       
   117 			{
       
   118 				return iStaticMemoryModelType;
       
   119 			}
       
   120 		}
       
   121 
       
   122 		public virtual TAddressType AddressType
       
   123 		{
       
   124 			get
       
   125 			{
       
   126 				TAddressType ret = TAddressType.EAddressTypeUnknown;
       
   127 
       
   128 				if	( Address >= 0xF8000000 && Address < 0xFFEFFFFF )
       
   129 				{
       
   130 					// ROM Symbol, Moving Memory Model
       
   131 					ret = TAddressType.EAddressTypeROMSymbol;
       
   132 				}
       
   133 				else if ( Address >= 0xF4000000 && Address < 0xF7FFFFFF )
       
   134 				{
       
   135 					// RAM Symbol, Moving Memory Model
       
   136 					ret = TAddressType.EAddressTypeRAMSymbol;
       
   137 				}
       
   138 				else if ( Address >= 0x64000000 && Address < 0x64FFFFFF )
       
   139 				{
       
   140 					// Kernel global, Moving Memory Model
       
   141 					ret = TAddressType.EAddressTypeKernelGlobalVariable;
       
   142 				}
       
   143 				else if ( Address >= 0xc8000000 && Address < 0xC8FFFFFF)
       
   144 				{
       
   145 					// Kernel global, Multiple Memory Model
       
   146 					ret = TAddressType.EAddressTypeKernelGlobalVariable;
       
   147 				}
       
   148 				else if ( Address >= 0x80000000 && Address < 0x8FFFFFFF )
       
   149 				{
       
   150 					// ROM Symbol, Multiple Memory Model
       
   151 					ret = TAddressType.EAddressTypeROMSymbol;
       
   152 				}
       
   153 				else if ( Address >= 0x3C000000 && Address < 0x3DFFFFFF )
       
   154 				{
       
   155 					// RAM Symbol, Moving Memory Model [1gb]
       
   156 					ret = TAddressType.EAddressTypeRAMSymbol;
       
   157 				}
       
   158 				else if ( Address >= 0x70000000 && Address < 0x7FFFFFFF )
       
   159 				{
       
   160 					// RAM Symbol, Moving Memory Model [2gb]
       
   161 					ret = TAddressType.EAddressTypeRAMSymbol;
       
   162 				}
       
   163                 else if ( Address < 0x10000000 )
       
   164                 {
       
   165                     // A non-fixed up ROFS symbol entry
       
   166                     ret = TAddressType.EAddressTypeRAMSymbol;
       
   167                 }
       
   168 				
       
   169 				// These can over-ride the previous items...
       
   170 				if	( iSymbol.IndexOf("Image$$ER_RO$$") >= 0 )
       
   171 				{
       
   172 					ret = TAddressType.EAddressTypeReadOnlySymbol;
       
   173 				}
       
   174 				else if ( iSymbol.IndexOf("__sub_object(") > 0 )
       
   175 				{
       
   176 					ret = TAddressType.EAddressTypeSubObject;
       
   177 				}
       
   178 				else if ( ret != TAddressType.EAddressTypeKernelGlobalVariable )
       
   179 				{
       
   180                     bool containsBrackets = iSymbol.Contains( "(" ) && iSymbol.Contains( ")" );
       
   181                     if ( !containsBrackets )
       
   182                     {
       
   183                         ret = TAddressType.EAddressTypeLabel;
       
   184                     }
       
   185 				}
       
   186 
       
   187 				return ret;
       
   188 			}
       
   189 		}
       
   190 		#endregion
       
   191 
       
   192         #region API
       
   193         public static string UnknownOffset()
       
   194         {
       
   195             string text = "[+ ??????]";
       
   196             return text;
       
   197         }
       
   198 
       
   199         public uint Offset( uint aInstructionAddress )
       
   200         {
       
   201             uint baseAddressOffset = aInstructionAddress - System.Convert.ToUInt32( Address );
       
   202             return baseAddressOffset;
       
   203         }
       
   204 
       
   205         public string OffsetAsString( uint aInstructionAddress )
       
   206         {
       
   207             uint baseAddressOffset = Offset( aInstructionAddress );
       
   208             string text = "[+ 0x" + baseAddressOffset.ToString( "x4" ) + "]";
       
   209             return text;
       
   210         }
       
   211         #endregion
       
   212 
       
   213         #region Properties
       
   214         public TInstructionSet InstructionSet
       
   215         {
       
   216             get
       
   217             {
       
   218                 TInstructionSet ret = TInstructionSet.EInstructionSetARM;
       
   219                 //
       
   220                 if ( ( iFlags & TFlags.EFlagsIsThumb ) == TFlags.EFlagsIsThumb )
       
   221                 {
       
   222                     ret = TInstructionSet.EInstructionSetTHUMB;
       
   223                 }
       
   224                 //
       
   225                 return ret;
       
   226             }
       
   227         }
       
   228 
       
   229         public long BaseAddress
       
   230         {
       
   231             get { return iCollection.BaseAddress; }
       
   232         }
       
   233 
       
   234 		public long Address
       
   235 		{
       
   236 			get
       
   237             {
       
   238                 long address = BaseAddress + iOffsetAddress;
       
   239                 return address;
       
   240             }
       
   241 		}
       
   242 
       
   243 		public long EndAddress
       
   244 		{
       
   245 			get
       
   246             {
       
   247                 long ret = BaseAddress + iOffsetEndAddress;
       
   248                 return ret;
       
   249             }
       
   250 		}
       
   251 
       
   252 		public long Size
       
   253 		{
       
   254 			get { return iSize; }
       
   255 			set
       
   256 			{
       
   257 				System.Diagnostics.Debug.Assert( value >= 0 );
       
   258 				iSize = value;
       
   259                 
       
   260                 // We need to update the end address
       
   261                 if ( iSize > 0 )
       
   262                 {
       
   263                     long address = OffsetAddress;
       
   264                     OffsetEndAddress = address + iSize - 1;
       
   265                 }
       
   266                 else
       
   267                 {
       
   268                     OffsetEndAddress = OffsetAddress;
       
   269                 }
       
   270 
       
   271                 System.Diagnostics.Debug.Assert( iOffsetEndAddress >= OffsetAddress );
       
   272 			}
       
   273 		}
       
   274 
       
   275         public AddressRange AddressRange
       
   276         {
       
   277             get { return new AddressRange( Address, EndAddress ); }
       
   278         }
       
   279 
       
   280 		public string Symbol
       
   281 		{
       
   282 			get { return iSymbol; }
       
   283 			set
       
   284 			{
       
   285 				iSymbol = value;
       
   286                 if ( StartsWithAny( KVTableOrTypeInfoPrefixes, value ) )
       
   287                 {
       
   288                     iFlags |= TFlags.EFlagsVTable;
       
   289                 }
       
   290 			}
       
   291 		}
       
   292 
       
   293 		public string SymbolNameWithoutVTablePrefix
       
   294 		{
       
   295 			get
       
   296             {
       
   297                 StringBuilder ret = new StringBuilder( Symbol );
       
   298                 //
       
   299                 ret = ret.Replace( "vtable for ", string.Empty );
       
   300                 ret = ret.Replace( "typeinfo for ", string.Empty );
       
   301                 ret = ret.Replace( "typeinfo name for ", string.Empty );
       
   302                 //
       
   303                 return ret.ToString();
       
   304             }
       
   305 		}
       
   306 
       
   307 		public string Object
       
   308 		{
       
   309 			get { return iObject; }
       
   310 			set { iObject = value; }
       
   311 		}
       
   312 
       
   313 		public string ObjectWithoutSection
       
   314 		{
       
   315 			get
       
   316 			{
       
   317 				string ret = string.Empty;
       
   318                 //
       
   319                 if  ( Object != null )
       
   320                 {
       
   321                     ret = Object;
       
   322                     //
       
   323                     int bracketPos = ret.IndexOf( "(" );
       
   324                     if	( bracketPos > 0 )
       
   325                     {
       
   326                         ret = ret.Substring( 0, bracketPos ).Trim();
       
   327                     }
       
   328                 }
       
   329 
       
   330                 // Casing looks odd and it can also confuse
       
   331                 // hash table look ups in dependent code.
       
   332                 //ret = SymbianUtils.Strings.StringCaser.PrettyCase( ret );
       
   333 				return ret;
       
   334 			}
       
   335 		}
       
   336 
       
   337         public GenericSymbolCollection Collection
       
   338         {
       
   339             get { return iCollection; }
       
   340         }
       
   341   
       
   342         public bool IsUnknownSymbol
       
   343         {
       
   344             get
       
   345             {
       
   346                 bool ret = Symbol.StartsWith( KNonMatchingSymbolName ) &&
       
   347                            ( OffsetAddress == GenericSymbol.KNullEntryAddress );
       
   348                 return ret;
       
   349             }
       
   350         }
       
   351 
       
   352         public bool IsVTable
       
   353         {
       
   354             get
       
   355             {
       
   356                 bool ret = ( iFlags & TFlags.EFlagsVTable ) == TFlags.EFlagsVTable;
       
   357                 return ret;
       
   358             }
       
   359         }
       
   360         #endregion
       
   361         
       
   362         #region Internal enumerations
       
   363         [Flags]
       
   364         private enum TFlags : byte
       
   365         {
       
   366             EFlagsNone = 0,
       
   367             EFlagsVTable = 1,
       
   368             EFlagsIsThumb = 2,
       
   369         };
       
   370         #endregion
       
   371 
       
   372         #region Internal properties
       
   373         internal long OffsetAddress
       
   374         {
       
   375             get { return iOffsetAddress; }
       
   376             set
       
   377             {
       
   378                 long remainder = value & 0x1;
       
   379                 if ( remainder != 0 )
       
   380                 {
       
   381                     iFlags |= TFlags.EFlagsIsThumb;
       
   382                 }
       
   383                 else
       
   384                 {
       
   385                     iFlags &= ~TFlags.EFlagsIsThumb;
       
   386                 }
       
   387 
       
   388                 iOffsetAddress = value & 0xFFFFFFFE;
       
   389 
       
   390                 // If we've not yet decided upon the type of memory model,
       
   391                 // now is the time to work it out.
       
   392                 if  ( iOffsetAddress > 0 && iStaticMemoryModelType == MemoryModel.TMemoryModelType.EMemoryModelUnknown )
       
   393                 {
       
   394                     long address = Address;
       
   395                     iStaticMemoryModelType = MemoryModel.TypeByAddress( address );
       
   396                 }
       
   397             }
       
   398         }
       
   399 
       
   400         private long OffsetEndAddress
       
   401         {
       
   402             set { iOffsetEndAddress = value; }
       
   403         }
       
   404         #endregion
       
   405 
       
   406 		#region Internal constants
       
   407 		protected const int KBaseHex = 16;
       
   408         private static readonly string[] KVTableOrTypeInfoPrefixes = new string[] { "vtable for ", "typeinfo for ", "typeinfo name for " };
       
   409 		#endregion
       
   410 
       
   411 		#region From System.Object
       
   412 		public override string ToString()
       
   413 		{
       
   414             string ret = string.Format( "{0:x8}    {1:x4}    {2} [{3}]", Address, Size, Symbol, Object );
       
   415 			return ret;
       
   416 		}
       
   417         
       
   418         public string ToStringForStream()
       
   419         {
       
   420             StringBuilder ret = new StringBuilder();
       
   421             //
       
   422             ret.Append( Address.ToString( "x8" ) );
       
   423             ret.Append( "    " );
       
   424             ret.Append( Size.ToString( "x4" ) );
       
   425             ret.Append( "    " );
       
   426             ret.Append( Symbol.PadRight( 40, ' ' ) );
       
   427             ret.Append( " " );
       
   428             ret.Append( Object );
       
   429             //
       
   430             return ret.ToString();
       
   431         }
       
   432 
       
   433         public static bool StartsWithAny( string[] aPrefixes, string aText )
       
   434         {
       
   435             bool ret = false;
       
   436             //
       
   437             foreach ( string p in aPrefixes )
       
   438             {
       
   439                 if ( aText.StartsWith( p ) )
       
   440                 {
       
   441                     ret = true;
       
   442                     break;
       
   443                 }
       
   444             }
       
   445             //
       
   446             return ret;
       
   447         }
       
   448 	    #endregion
       
   449 
       
   450 		#region Data members
       
   451 		protected long iSize;
       
   452 		#endregion
       
   453 
       
   454 		#region Internal data members
       
   455 		private long iOffsetAddress;
       
   456 		private long iOffsetEndAddress;
       
   457         private string iSymbol = string.Empty;
       
   458 		private string iObject = string.Empty;
       
   459         private TFlags iFlags = TFlags.EFlagsNone;
       
   460         private readonly GenericSymbolCollection iCollection;
       
   461         private static MemoryModel.TMemoryModelType iStaticMemoryModelType = MemoryModel.TMemoryModelType.EMemoryModelUnknown;
       
   462 		#endregion
       
   463 	}
       
   464 }