crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianSymbolLib/Relocator/SymbolRelocator.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.Collections.Generic;
       
    20 using System.Text;
       
    21 using System.IO;
       
    22 using SymbianUtils;
       
    23 using SymbianUtils.Range;
       
    24 using SymbianUtils.Tracer;
       
    25 using SymbianUtils.FileTypes;
       
    26 using SymbianStructuresLib.Debug.Symbols;
       
    27 using SymbianStructuresLib.CodeSegments;
       
    28 using SymbianSymbolLib.DbgEnginePlugin;
       
    29 using SymbianSymbolLib.SourceManagement.Source;
       
    30 using SymbianSymbolLib.QueryAPI;
       
    31 
       
    32 namespace SymbianSymbolLib.Relocator
       
    33 {
       
    34     internal class SymbolRelocator : IEnumerable<SymSourceAndCollection>
       
    35     {
       
    36         #region Constructors
       
    37         public SymbolRelocator( SymbolPlugin aPlugin )
       
    38 		{
       
    39             iPlugin = aPlugin;
       
    40 		}
       
    41 		#endregion
       
    42 
       
    43         #region API
       
    44         public SymbolCollection Activate( CodeSegDefinition aCodeSegment )
       
    45         {
       
    46             SymbolCollection ret = null;
       
    47 
       
    48             // Find the corresponding code seg
       
    49             SymSourceAndCollection pair = SourceManager[ aCodeSegment ];
       
    50             if ( pair != null  )
       
    51             {
       
    52                 SymbolCollection col = pair.Collection;
       
    53                 lock ( col.SyncRoot )
       
    54                 {
       
    55                     if ( col.IsFixed )
       
    56                     {
       
    57                         // Cannot activate a fixed code segment - TODO: should this return "true", i.e. already activated?
       
    58                     }
       
    59                     else
       
    60                     {
       
    61                         bool safe = CheckSafeToActivate( aCodeSegment );
       
    62                         if ( safe )
       
    63                         {
       
    64                             // Deep copy the collection
       
    65                             SymbolCollection dupe = SymbolCollection.NewCopy( iPlugin.ProvisioningManager.IdAllocator, col );
       
    66 
       
    67                             // Set new process-specific relocated base address
       
    68                             dupe.Relocate( aCodeSegment.Base );
       
    69 
       
    70                             // Save so that we can unload it later
       
    71                             pair = new SymSourceAndCollection( pair, dupe );
       
    72                             AddToActivationList( pair, aCodeSegment );
       
    73 
       
    74                             // We managed to activate a binary, so return the collection
       
    75                             ret = dupe;
       
    76 
       
    77                             iPlugin.Trace( "[S] ACTIVATE - {0}", aCodeSegment );
       
    78                         }
       
    79                     }
       
    80                 }
       
    81             }
       
    82             //
       
    83             return ret;
       
    84         }
       
    85 
       
    86         public bool Deactivate( CodeSegDefinition aCodeSegment )
       
    87         {
       
    88             bool activated = iActivationLUT.ContainsKey( aCodeSegment );
       
    89             if ( activated )
       
    90             {
       
    91                 SymSourceAndCollection pair = iActivationLUT[ aCodeSegment ];
       
    92                 //
       
    93                 lock ( iActivatedCollections )
       
    94                 {
       
    95                     iActivatedCollections.Remove( pair.Collection );
       
    96                 }
       
    97                 lock ( iActivationLUT )
       
    98                 {
       
    99                     iActivationLUT.Remove( aCodeSegment );
       
   100                 }
       
   101                 //
       
   102                 iPlugin.Trace( "[S] DEACTIVATE - {0} @ {1}", pair.Collection.FileName, aCodeSegment );
       
   103             }
       
   104             //
       
   105             return activated;
       
   106         }
       
   107         #endregion
       
   108 
       
   109 		#region Properties
       
   110         public SymbolCollectionList CollectionList
       
   111         {
       
   112             get { return iActivatedCollections; }
       
   113         }
       
   114 
       
   115         public int Count
       
   116         {
       
   117             get
       
   118             {
       
   119                 lock ( iActivatedCollections )
       
   120                 {
       
   121                     lock ( iActivationLUT )
       
   122                     {
       
   123                         System.Diagnostics.Debug.Assert( iActivatedCollections.Count == iActivationLUT.Count );
       
   124                         return iActivationLUT.Count;
       
   125                     }
       
   126                 }
       
   127             }
       
   128         }
       
   129         #endregion
       
   130 
       
   131         #region Internal methods
       
   132         internal SymbolPlugin Plugin
       
   133         {
       
   134             get { return iPlugin; }
       
   135         }
       
   136 
       
   137         internal SymSourceManager SourceManager
       
   138         {
       
   139             get { return Plugin.SourceManager; }
       
   140         }
       
   141 
       
   142         private void AddToActivationList( SymSourceAndCollection aEntry, CodeSegDefinition aCodeSegment )
       
   143         {
       
   144             lock ( iActivatedCollections )
       
   145             {
       
   146                 iActivatedCollections.AddAndBuildCache( aEntry.Collection );
       
   147             }
       
   148             lock ( iActivationLUT )
       
   149             {
       
   150                 iActivationLUT.Add( aCodeSegment, aEntry );
       
   151             }
       
   152         }
       
   153 
       
   154         private bool CheckSafeToActivate( CodeSegDefinition aCodeSegment )
       
   155         {
       
   156             lock ( iActivationLUT )
       
   157             {
       
   158                 bool alreadyExists = iActivationLUT.ContainsKey( aCodeSegment );
       
   159                 if ( alreadyExists )
       
   160                 {
       
   161                     // Specified code segment already activated
       
   162                     return false;
       
   163                 }
       
   164                 else
       
   165                 {
       
   166                     // We must check that there's no overlap in activation ranges between code segments.
       
   167                     foreach ( KeyValuePair<CodeSegDefinition, SymSourceAndCollection> kvp in iActivationLUT )
       
   168                     {
       
   169                         AddressRange range = kvp.Key;
       
   170                         if ( range.Contains( aCodeSegment ) )
       
   171                         {
       
   172                             // Overlaps with existing activated code segment
       
   173                             return false;
       
   174                         }
       
   175                     }
       
   176                 }
       
   177                 //
       
   178                 return true;
       
   179             }
       
   180         }
       
   181         #endregion
       
   182 
       
   183         #region From IEnumerable<SymSourceAndCollection>
       
   184         public IEnumerator<SymSourceAndCollection> GetEnumerator()
       
   185         {
       
   186             foreach ( KeyValuePair<CodeSegDefinition, SymSourceAndCollection> kvp in iActivationLUT )
       
   187             {
       
   188                 yield return kvp.Value;
       
   189             }
       
   190         }
       
   191 
       
   192         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
       
   193         {
       
   194             foreach ( KeyValuePair<CodeSegDefinition, SymSourceAndCollection> kvp in iActivationLUT )
       
   195             {
       
   196                 yield return kvp.Value;
       
   197             }
       
   198         }
       
   199         #endregion
       
   200 
       
   201         #region Data members
       
   202         private readonly SymbolPlugin iPlugin;
       
   203         private SymbolCollectionList iActivatedCollections = new SymbolCollectionList();
       
   204         private Dictionary<CodeSegDefinition, SymSourceAndCollection> iActivationLUT = new Dictionary<CodeSegDefinition, SymSourceAndCollection>();
       
   205         #endregion
       
   206     }
       
   207 }