crashanalysercmd/Libraries/Engine/CrashItemLib/Crash/Symbols/CISymbolDictionary.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.Text;
       
    19 using System.IO;
       
    20 using System.Collections.Generic;
       
    21 using CrashItemLib.Crash;
       
    22 using CrashItemLib.Crash.Base;
       
    23 using CrashItemLib.Crash.Registers;
       
    24 using CrashItemLib.Crash.Processes;
       
    25 using CrashItemLib.Crash.Container;
       
    26 using CrashItemLib.Crash.Stacks;
       
    27 using SymbianStructuresLib.Debug.Symbols;
       
    28 using SymbianDebugLib.Engine;
       
    29 using SymbianDebugLib.PluginAPI.Types;
       
    30 using SymbianDebugLib.PluginAPI.Types.Symbol;
       
    31 
       
    32 namespace CrashItemLib.Crash.Symbols
       
    33 {
       
    34     public class CISymbolDictionary : CIElementList<CISymbol>
       
    35     {
       
    36         #region Constructors
       
    37         internal CISymbolDictionary( CIContainer aContainer )
       
    38             : base( aContainer )
       
    39 		{
       
    40             base.IsToBeFinalizedLast = true;
       
    41 		}
       
    42 		#endregion
       
    43 
       
    44         #region API
       
    45         internal CISymbol Register( uint aSymbolAddress )
       
    46         {
       
    47             // First try to find an existing CISymbol that matches the address.
       
    48             CISymbol symbol = this[ aSymbolAddress ];
       
    49             if ( symbol == null )
       
    50             {
       
    51                 // No existing entry defined, so create a new symbol which is just based upon
       
    52                 // an address. 
       
    53                 symbol = new CISymbol( base.Container, aSymbolAddress );
       
    54                 DoRegister( symbol );
       
    55             }
       
    56             else
       
    57             {
       
    58                 // Found an existing symbol object - increment reference count
       
    59                 symbol.ReferenceCountIncrement();
       
    60             }
       
    61             //
       
    62             return symbol;
       
    63         }
       
    64 
       
    65         internal CISymbol Register( Symbol aSymbol )
       
    66         {
       
    67             // First try to find an existing CISymbol that matches the address.
       
    68             CISymbol symbol = this[ aSymbol.Address ];
       
    69             if ( symbol == null )
       
    70             {
       
    71                 // No existing entry defined, so create a new symbol with explicit symbol definition
       
    72                 symbol = new CISymbol( base.Container, aSymbol );
       
    73                 DoRegister( symbol );
       
    74             }
       
    75             else
       
    76             {
       
    77                 // Found an existing symbol object - however, does it have an associated
       
    78                 // symbol yet?
       
    79                 symbol.AssignPermanentSymbol( aSymbol );
       
    80 
       
    81                 // Also, we must increment the reference count because it's now shared by another client
       
    82                 symbol.ReferenceCountIncrement();
       
    83             }
       
    84             //
       
    85             return symbol;
       
    86         }
       
    87 
       
    88         internal CISymbol RefreshRegistration( CIElement aElement, uint aSymbolAddress, CISymbol aOldSymbolOrNull )
       
    89         {
       
    90             CISymbol ret = aOldSymbolOrNull;
       
    91             bool needToRegister = false;
       
    92 
       
    93             if ( aOldSymbolOrNull != null )
       
    94             {
       
    95                 // We only need to do this if the symbol address changed.
       
    96                 if ( aSymbolAddress != aOldSymbolOrNull.Address )
       
    97                 {
       
    98                     // We must unregister the symbol from the parent element because potentially we might
       
    99                     // replace the symbol with an entirely new one.
       
   100                     aElement.RemoveChild( aOldSymbolOrNull );
       
   101 
       
   102                     // However, we only do this if there are no other client of the same symbol.
       
   103                     int newRefCount = aOldSymbolOrNull.ReferenceCountDecrement();
       
   104                     if ( newRefCount <= 0 )
       
   105                     {
       
   106                         // Remove old references to dead symbol
       
   107                         DoDeregister( aOldSymbolOrNull );
       
   108                     }
       
   109 
       
   110                     // Since the address of the symbol has changed, we will need to 
       
   111                     // re-register a new symbol
       
   112                     needToRegister = true;
       
   113                 }
       
   114                 else
       
   115                 {
       
   116                     // The symbol address hasn't changed
       
   117                 }
       
   118             }
       
   119             else if ( aSymbolAddress == 0 && aOldSymbolOrNull == null )
       
   120             {
       
   121                 // Err, nothing to do here.
       
   122             }
       
   123             else
       
   124             {
       
   125                 // We weren't supplied with an original symbol object which means we will
       
   126                 // definitely need to register a symbol (if we don't find a pre-existing match).
       
   127                 needToRegister = true;
       
   128             }
       
   129 
       
   130             // Do we need to register a new symbol?
       
   131             if ( needToRegister )
       
   132             {
       
   133                 // Create new entry (or look up existing reference). This will also
       
   134                 // ensure any new symbol is registered with this dictionary.
       
   135                 ret = Register( aSymbolAddress );
       
   136 
       
   137                 // Associate with parent element
       
   138                 aElement.AddChild( ret );
       
   139             }
       
   140 
       
   141             return ret;
       
   142         }
       
   143         #endregion
       
   144 
       
   145         #region Properties
       
   146         internal CISymbol this[ uint aAddress ]
       
   147         {
       
   148             get
       
   149             {
       
   150                 CISymbol ret = null;
       
   151                 //
       
   152                 if ( iAddressLUT.ContainsKey( aAddress ) )
       
   153                 {
       
   154                     ret = iAddressLUT[ aAddress ];
       
   155                 }
       
   156                 //
       
   157                 return ret;
       
   158             }
       
   159         }
       
   160         #endregion
       
   161 
       
   162         #region Internal methods
       
   163         private void DoRegister( CISymbol aSymbol )
       
   164         {
       
   165             CISymbol check = this[ aSymbol.Address ];
       
   166             System.Diagnostics.Debug.Assert( check == null );
       
   167             aSymbol.ReferenceCountIncrement();
       
   168             Add( aSymbol );
       
   169         }
       
   170 
       
   171         private void DoDeregister( CISymbol aSymbol )
       
   172         {
       
   173             bool alreadyRegistered = base.Contains( aSymbol );
       
   174             if ( alreadyRegistered )
       
   175             {
       
   176                 Remove( aSymbol );
       
   177             }
       
   178         }
       
   179         #endregion
       
   180 
       
   181         #region From CIElementDictionary
       
   182         public override bool Add( CISymbol aEntry )
       
   183         {
       
   184             bool okayToAdd = iAddressLUT.ContainsKey( aEntry.Address ) == false;
       
   185             if ( okayToAdd )
       
   186             {
       
   187                 base.Add( aEntry );
       
   188                 iAddressLUT.Add( aEntry.Address, aEntry );
       
   189             }
       
   190 
       
   191             return okayToAdd;
       
   192         }
       
   193 
       
   194         public override void Remove( CISymbol aEntry )
       
   195         {
       
   196             bool alreadyExists = iAddressLUT.ContainsKey( aEntry.Address );
       
   197             if ( alreadyExists )
       
   198             {
       
   199                 iAddressLUT.Remove( aEntry.Address );
       
   200             }
       
   201             base.Remove( aEntry );
       
   202         }
       
   203         #endregion
       
   204 
       
   205         #region From CIElement
       
   206         internal override void OnFinalize( CIElementFinalizationParameters aParams )
       
   207         {
       
   208             try
       
   209             {
       
   210                 base.OnFinalize( aParams );
       
   211             }
       
   212             finally
       
   213             {
       
   214             }
       
   215         }
       
   216         #endregion
       
   217 
       
   218         #region Data members
       
   219         private Dictionary<uint, CISymbol> iAddressLUT = new Dictionary<uint, CISymbol>();
       
   220         #endregion
       
   221     }
       
   222 }