crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianStackLib/Plugins/Heuristic/StackAlgHeuristic.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.Threading;
       
    20 using System.ComponentModel;
       
    21 using System.Collections.Generic;
       
    22 using System.IO;
       
    23 using SymbianUtils.DataBuffer.Entry;
       
    24 using SymbianStructuresLib.Arm.Registers;
       
    25 using SymbianStructuresLib.CodeSegments;
       
    26 using SymbianStructuresLib.Debug.Symbols;
       
    27 using SymbianUtils;
       
    28 using SymbianUtils.Range;
       
    29 using SymbianUtils.Utilities;
       
    30 using SymbianDebugLib.PluginAPI.Types;
       
    31 using SymbianDebugLib.Engine;
       
    32 using SymbianStackLib.Data.Source;
       
    33 using SymbianStackLib.Data.Output;
       
    34 using SymbianStackLib.Data.Output.Entry;
       
    35 using SymbianStackLib.Interfaces;
       
    36 using SymbianStackLib.Algorithms;
       
    37 
       
    38 namespace SymbianStackAlgorithmHeuristic
       
    39 {
       
    40     internal class StackAlgHeuristic : StackAlgorithm
       
    41 	{
       
    42 		#region Constructors
       
    43         public StackAlgHeuristic( IStackAlgorithmManager aManager, IStackAlgorithmObserver aObserver )
       
    44             : base( aManager, aObserver )
       
    45         {
       
    46         }
       
    47 		#endregion
       
    48 
       
    49 		#region API
       
    50 		#endregion
       
    51 
       
    52 		#region Properties
       
    53 		#endregion
       
    54 
       
    55         #region From StackAlgorithm
       
    56         public override string Name
       
    57         {
       
    58             get { return "Heuristic"; }
       
    59         }
       
    60 
       
    61         public override int Priority
       
    62         {
       
    63             get { return 100; } 
       
    64         }
       
    65         #endregion
       
    66 
       
    67         #region From AsyncReaderBase
       
    68         protected override void PerformOperation()
       
    69         {
       
    70             // Get the source data we need to reconstruct and signal we're about to start
       
    71             StackSourceData sourceData = base.SourceData;
       
    72 
       
    73             // Get the output data sink
       
    74             StackOutputData outputData = base.OutputData;
       
    75             outputData.Clear();
       
    76             outputData.AlgorithmName = Name;
       
    77 
       
    78             // Get the address range of the stack pointer data
       
    79             AddressRange pointerRange = base.Engine.AddressInfo.StackPointerRange;
       
    80             AddressRange pointerRangeExtended = base.Engine.AddressInfo.StackPointerRangeWithExtensionArea;
       
    81 
       
    82             // Indicates if we added LR and PC to the call stack
       
    83             bool addedLRandPC = false;
       
    84 
       
    85             // Get registers
       
    86             ArmRegisterCollection regs = base.Engine.Registers;
       
    87 
       
    88             foreach( DataBufferUint sourceEntry in sourceData.GetUintEnumerator() )
       
    89             {
       
    90                 // Check if it is within the stack domain, taking into account
       
    91                 // our extended range
       
    92                 if ( pointerRangeExtended.Contains( sourceEntry.Address ) )
       
    93                 {
       
    94                     StackOutputEntry outputEntry = new StackOutputEntry( sourceEntry.Address, sourceEntry.Uint, base.DebugEngineView );
       
    95 
       
    96                     // Is it the element tht corresponds to the current value of SP?
       
    97                     bool isCurrentSPEntry = ( outputEntry.AddressRange.Contains( base.Engine.AddressInfo.Pointer ) );
       
    98                     outputEntry.IsCurrentStackPointerEntry = isCurrentSPEntry;
       
    99 
       
   100                     // Is it within the pure 'stack pointer' address range?
       
   101                     bool outsidePureStackPointerRange = !pointerRange.Contains( sourceEntry.Address );
       
   102                     outputEntry.IsOutsideCurrentStackRange = outsidePureStackPointerRange;
       
   103 
       
   104                     // These are never accurate, but neither are they ghosts
       
   105                     outputEntry.IsAccurate = false;
       
   106                     outputEntry.IsGhost = false;
       
   107                     
       
   108                     // Save entry
       
   109                     EmitElement( outputEntry );
       
   110 
       
   111                     // If we're inside the stack address range, then poke in the PC and LR values
       
   112                     if ( isCurrentSPEntry )
       
   113                     {
       
   114                         System.Diagnostics.Debug.Assert( !addedLRandPC );
       
   115                         addedLRandPC = AddLRAndPC();
       
   116                     }
       
   117                 }
       
   118                 else
       
   119                 {
       
   120                     // Nope, ignore it...
       
   121                 }
       
   122 
       
   123                 NotifyEvent( TEvent.EReadingProgress );
       
   124                 ++iDWordIndex;
       
   125             }
       
   126 
       
   127             // If the stack overflowed, then SP might be outside of the stack range. Therefore
       
   128             // LR and PC will not be added yet.
       
   129             if ( !addedLRandPC )
       
   130             {
       
   131                 AddLRAndPC();
       
   132             }
       
   133         }
       
   134 
       
   135         protected override long Size
       
   136         {
       
   137             get
       
   138             { 
       
   139                 int bytes = base.SourceData.Count;
       
   140                 int dWords = bytes / 4;
       
   141                 return dWords;
       
   142             }
       
   143         }
       
   144 
       
   145         protected override long Position
       
   146         {
       
   147             get
       
   148             {
       
   149                 return iDWordIndex;
       
   150             }
       
   151         }
       
   152         #endregion
       
   153 
       
   154 		#region Internal methods
       
   155         private bool AddLRAndPC()
       
   156         {
       
   157             bool addedLRandPC = false;
       
   158             //
       
   159             ArmRegisterCollection regs = base.Engine.Registers;
       
   160             
       
   161             // If we're inside the stack address range, then poke in the PC and LR values
       
   162             if ( regs.Count > 0 )
       
   163             {
       
   164                 // Working bottom up, so LR should go on the stack first
       
   165                 if ( regs.Contains( TArmRegisterType.EArmReg_LR ) )
       
   166                 {
       
   167                     ArmRegister regLR = regs[ TArmRegisterType.EArmReg_LR ];
       
   168 
       
   169                     StackOutputEntry entryLR = new StackOutputEntry( 0, regLR.Value, base.DebugEngineView );
       
   170                     entryLR.IsRegisterBasedEntry = true;
       
   171                     entryLR.IsOutsideCurrentStackRange = true;
       
   172                     entryLR.AssociatedRegister = regLR.RegType;
       
   173                     EmitElement( entryLR );
       
   174                 }
       
   175 
       
   176                 // Then the PC...
       
   177                 if ( regs.Contains( TArmRegisterType.EArmReg_PC ) )
       
   178                 {
       
   179                     ArmRegister regPC = regs[ TArmRegisterType.EArmReg_PC ];
       
   180 
       
   181                     StackOutputEntry entryPC = new StackOutputEntry( 0, regPC.Value, base.DebugEngineView );
       
   182                     entryPC.IsRegisterBasedEntry = true;
       
   183                     entryPC.IsOutsideCurrentStackRange = true;
       
   184                     entryPC.AssociatedRegister = regPC.RegType;
       
   185                     EmitElement( entryPC );
       
   186                 }
       
   187 
       
   188                 // Even if they weren't added, we at least attempted to addd them
       
   189                 addedLRandPC = true;
       
   190             }
       
   191 
       
   192             return addedLRandPC;
       
   193         }
       
   194 
       
   195         private void EmitElement( StackOutputEntry aEntry )
       
   196         {
       
   197             // Flush entry
       
   198             base.StackObserver.StackBuildingElementConstructed( this, aEntry );
       
   199         }
       
   200         #endregion
       
   201 
       
   202 		#region Data members
       
   203         private int iDWordIndex = 0;
       
   204 		#endregion
       
   205     }
       
   206 }