sysperfana/heapanalyser/Libraries/Engine/HeapLib/Reconstructor/DataSources/Analyser/Interpreter/Implementations/InterpreterText.cs
changeset 8 15296fd0af4a
equal deleted inserted replaced
7:8e12a575a9b5 8:15296fd0af4a
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * Redistribution and use in source and binary forms, with or without
       
     6 * modification, are permitted provided that the following conditions are met:
       
     7 *
       
     8 * - Redistributions of source code must retain the above copyright notice,
       
     9 *   this list of conditions and the following disclaimer.
       
    10 * - Redistributions in binary form must reproduce the above copyright notice,
       
    11 *   this list of conditions and the following disclaimer in the documentation
       
    12 *   and/or other materials provided with the distribution.
       
    13 * - Neither the name of Nokia Corporation nor the names of its contributors
       
    14 *   may be used to endorse or promote products derived from this software
       
    15 *   without specific prior written permission.
       
    16 *
       
    17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
       
    18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
       
    20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
       
    21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
       
    22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
       
    23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
       
    24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
       
    25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
       
    26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
       
    27 * POSSIBILITY OF SUCH DAMAGE.
       
    28 * 
       
    29 * Initial Contributors:
       
    30 * Nokia Corporation - initial contribution.
       
    31 *
       
    32 * Contributors:
       
    33 *
       
    34 * Description: 
       
    35 *
       
    36 */
       
    37 
       
    38 using System;
       
    39 using System.Collections.Generic;
       
    40 using System.Text;
       
    41 using System.Text.RegularExpressions;
       
    42 using SymbianUtils;
       
    43 using SymbianStructuresLib.CodeSegments;
       
    44 using HeapLib.Reconstructor.Misc;
       
    45 using HeapLib.Reconstructor.DataSources;
       
    46 using HeapLib.Reconstructor.DataSources.Analyser.Extractor;
       
    47 
       
    48 namespace HeapLib.Reconstructor.DataSources.Analyser.Interpreter
       
    49 {
       
    50     internal sealed class InterpreterText : InterpreterBase
       
    51     {
       
    52         #region Constructors & destructor
       
    53         public InterpreterText()
       
    54         {
       
    55         }
       
    56         #endregion
       
    57 
       
    58         #region API
       
    59         public override bool CanInterpretFrom( Extractor.Extractor aExtractor )
       
    60         {
       
    61             // The text interpreter only works with the text extractor
       
    62             bool ret = ( aExtractor is Extractor.Implementations.ExtractorText );
       
    63             if ( ret )
       
    64             {
       
    65             }
       
    66             //
       
    67             return ret;
       
    68         }
       
    69 
       
    70         public override void PrepareToStart( Extractor.Extractor aExtractor )
       
    71         {
       
    72             // Seed the data source with the thread name. We can do this immediately
       
    73             // since the thread name is identified during the extractor initialisation
       
    74             DataSource.ThreadName = aExtractor.Hash;
       
    75         }
       
    76 
       
    77         public override void Interpret( ExtractedData aItem )
       
    78         {
       
    79             // [15:21:32.719] xti2:MCU_ASCII_PRINTF; channel:0xE0; msg:HeapData - EComServer::!ecomserver - HEAP INFO FOR THREAD 'EComServer::!ecomserver'
       
    80             // [15:21:32.719] xti2:MCU_ASCII_PRINTF; channel:0xE0; msg:HeapData - EComServer::!ecomserver - ==============================================
       
    81             // [15:21:32.719] xti2:MCU_ASCII_PRINTF; channel:0xE0; msg:HeapData - EComServer::!ecomserver - HeapInfo - heapBaseAddress:     0x00700074
       
    82             // [15:21:32.719] xti2:MCU_ASCII_PRINTF; channel:0xE0; msg:HeapData - EComServer::!ecomserver - HeapInfo - heapSize:                745352
       
    83             // [15:21:32.719] xti2:MCU_ASCII_PRINTF; channel:0xE0; msg:HeapData - EComServer::!ecomserver - HeapInfo - heapChunkSize:           745472
       
    84             // ...
       
    85             // [15:21:32.719] xti2:MCU_ASCII_PRINTF; channel:0xE0; msg:HeapData - EComServer::!ecomserver - 00700084: 00 00 00 00 03 03 03 03 03 03 03 03 03 03 03 03 ................
       
    86             // [15:21:32.719] xti2:MCU_ASCII_PRINTF; channel:0xE0; msg:HeapData - EComServer::!ecomserver - 00700094: 03 03 03 03 03 03 03 03 28 00 00 00 ec 47 28 80 ........(....G(.
       
    87             // [15:21:32.719] xti2:MCU_ASCII_PRINTF; channel:0xE0; msg:HeapData - EComServer::!ecomserver - 007000a4: d8 0c 70 00 58 0d 70 00 f8 0c 70 00 03 03 03 03 ..p.X.p...p.....
       
    88             // [15:21:32.719] xti2:MCU_ASCII_PRINTF; channel:0xE0; msg:HeapData - EComServer::!ecomserver - 007000b4: 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 ................
       
    89             // [15:21:32.719] xti2:MCU_ASCII_PRINTF; channel:0xE0; msg:HeapData - EComServer::!ecomserver - 007000c4: 48 00 00 00 e8 2b 30 80 00 2c 30 80 ec 02 70 00 H....+0..,0...p.
       
    90             //
       
    91             // or
       
    92             //
       
    93             // HEAP INFO FOR THREAD 'ecomserver::!ecomserver'
       
    94             // ==============================================
       
    95             // HeapInfo - heapBaseAddress:     0x0f8c0074
       
    96             // HeapInfo - heapSize:                262028
       
    97 
       
    98             // Save the line - we must do this in order to be able to write a zip file
       
    99             // containing the original source data for this data source.
       
   100             DataSource.AddLine( aItem.OriginalLine );
       
   101             
       
   102             if ( aItem.Type == ExtractedData.TType.EBinaryData )
       
   103             {
       
   104                 DataSource.MetaData.HeapData.Add( aItem.BinaryData );
       
   105             }
       
   106             else if ( aItem.Type == ExtractedData.TType.EText )
       
   107             {
       
   108                 if ( CheckForKnownElement( aItem.Text ) )
       
   109                 {
       
   110                 }
       
   111             }
       
   112         }
       
   113         #endregion
       
   114 
       
   115         #region Internal methods
       
   116         private bool CheckForKnownElement( string aLine )
       
   117         {
       
   118             bool handled = false;
       
   119             string origLine = aLine;
       
   120             //
       
   121             if ( CheckForKnownElement( ref aLine, iPrefixes.HeapBaseAddress ) )
       
   122             {
       
   123                 DataSource.MetaData.Heap.HeapBaseAddress = PrefixParser.ReadUint( ref aLine );
       
   124                 handled = true;
       
   125             }
       
   126             else if ( CheckForKnownElement( ref aLine, iPrefixes.HeapChunkSize ) )
       
   127             {
       
   128                 DataSource.MetaData.Heap.ChunkSize = PrefixParser.ReadUint( ref aLine );
       
   129                 handled = true;
       
   130             }
       
   131             else if ( CheckForKnownElement( ref aLine, iPrefixes.SizeOfRHeap ) )
       
   132             {
       
   133                 DataSource.MetaData.Heap.SizeOfRHeap = PrefixParser.ReadUint( ref aLine );
       
   134                 handled = true;
       
   135             }
       
   136             else if ( CheckForKnownElement( ref aLine, iPrefixes.HeapCellMinimumSize ) )
       
   137             {
       
   138                 DataSource.MetaData.Heap.MinCellSize = PrefixParser.ReadUint( ref aLine );
       
   139                 handled = true;
       
   140             }
       
   141             else if ( CheckForKnownElement( ref aLine, iPrefixes.FreeCellAddress ) )
       
   142             {
       
   143                 DataSource.MetaData.Heap.InfoFree.FreeCellAddress = PrefixParser.ReadUint( ref aLine );
       
   144                 handled = true;
       
   145             }
       
   146             else if ( CheckForKnownElement( ref aLine, iPrefixes.FreeCellLength ) )
       
   147             {
       
   148                 DataSource.MetaData.Heap.InfoFree.FreeCellLength = PrefixParser.ReadUint( ref aLine );
       
   149                 handled = true;
       
   150             }
       
   151             else if ( CheckForKnownElement( ref aLine, iPrefixes.DebugAllocator ) )
       
   152             {
       
   153                 DataSource.MetaData.Heap.DebugAllocator = PrefixParser.ReadBool( ref aLine );
       
   154                 handled = true;
       
   155             }
       
   156             else if ( CheckForKnownElement( ref aLine, iPrefixes.MiscRHeapRand ) )
       
   157             {
       
   158                 long val = PrefixParser.ReadLong( ref aLine );
       
   159                 uint rand = (uint) val;
       
   160                 DataSource.MetaData.Heap.Rand = rand;
       
   161                 handled = true;
       
   162             }
       
   163             else if ( CheckForKnownElement( ref aLine, iPrefixes.FreeStatistics ) )
       
   164             {
       
   165                 iState = TState.EStateStatisticsFree;
       
   166                 handled = true;
       
   167             }
       
   168             else if ( CheckForKnownElement( ref aLine, iPrefixes.AllocStatistics ) )
       
   169             {
       
   170                 iState = TState.EStateStatisticsAlloc;
       
   171                 handled = true;
       
   172             }
       
   173             else if ( CheckForKnownElement( ref aLine, iPrefixes.FreeCellCount ) )
       
   174             {
       
   175                 if ( iState == TState.EStateNone || iState == TState.EStateStatisticsFree )
       
   176                 {
       
   177                     DataSource.MetaData.Heap.InfoFree.FreeCellCount = PrefixParser.ReadInt( ref aLine );
       
   178                     handled = true;
       
   179                 }
       
   180                 else if ( iState == TState.EStateStatisticsAlloc )
       
   181                 {
       
   182                     // Must also handle alloc cells inside the free block, because in the new file format
       
   183                     // both alloc and free cell statistics contain the same prefix.
       
   184                     DataSource.MetaData.Heap.InfoAlloc.AllocCellCount = PrefixParser.ReadInt( ref aLine );
       
   185                     handled = true;
       
   186                 }
       
   187             }
       
   188             else if ( CheckForKnownElement( ref aLine, iPrefixes.CommonStatisticsSizeOfCells ) )
       
   189             {
       
   190                 if ( iState == TState.EStateStatisticsFree )
       
   191                 {
       
   192                     DataSource.MetaData.Heap.InfoFree.FreeCellTotalSpace = PrefixParser.ReadUint( ref aLine );
       
   193                     handled = true;
       
   194                 }
       
   195                 else if ( iState == TState.EStateStatisticsAlloc )
       
   196                 {
       
   197                     DataSource.MetaData.Heap.InfoAlloc.AllocCellTotalSpace = PrefixParser.ReadUint( ref aLine );
       
   198                     handled = true;
       
   199                 }
       
   200             }
       
   201             else if ( CheckForKnownElement( ref aLine, iPrefixes.AllocCellCount ) )
       
   202             {
       
   203                 if ( iState == TState.EStateNone || iState == TState.EStateStatisticsAlloc )
       
   204                 {
       
   205                     DataSource.MetaData.Heap.InfoAlloc.AllocCellCount = PrefixParser.ReadInt( ref aLine );
       
   206                     handled = true;
       
   207                 }
       
   208             }
       
   209             else if ( CheckForKnownElement( ref aLine, iPrefixes.FreeCellList ) )
       
   210             {
       
   211                 iState = TState.EStateFreeCellList;
       
   212             }
       
   213             else
       
   214             {
       
   215                 // Check for free cell list match
       
   216                 if ( iState == TState.EStateFreeCellList )
       
   217                 {
       
   218                     Match freeCellListMatch = KFreeCellRegEx.Match( aLine );
       
   219                     if ( freeCellListMatch.Success )
       
   220                     {
       
   221                         int index = int.Parse( freeCellListMatch.Groups[ "Index" ].Value );
       
   222                         uint address = uint.Parse( freeCellListMatch.Groups[ "Address" ].Value, System.Globalization.NumberStyles.AllowHexSpecifier );
       
   223                         int length = int.Parse( freeCellListMatch.Groups[ "Length" ].Value );
       
   224                         int type = int.Parse( freeCellListMatch.Groups[ "Type" ].Value );
       
   225 
       
   226                         // The index should be between 1 and FreeCellCount
       
   227                         int max = DataSource.MetaData.Heap.InfoFree.FreeCellCount;
       
   228 
       
   229                         // However, if we didn't read the free cell count then don't panic...
       
   230                         if ( max > 0 )
       
   231                         {
       
   232                             if ( index >= 1 && index <= max )
       
   233                             {
       
   234                                 DataSource.MetaData.Heap.InfoFree.AddFreeCell( address, length, type );
       
   235                             }
       
   236                             else
       
   237                             {
       
   238                                 DataSource.AddError( DataSource.TErrorTypes.EErrorTypeInvalidFreeCellIndexInFreeList );
       
   239                             }
       
   240                         }
       
   241                         else
       
   242                         {
       
   243                             // No free cell to validate against, just hope it's okay!
       
   244                             DataSource.MetaData.Heap.InfoFree.AddFreeCell( address, length, type );
       
   245                         }
       
   246                     }
       
   247                 }
       
   248 
       
   249                 // Is it a code segment?
       
   250                 if ( !handled )
       
   251                 {
       
   252                     CodeSegDefinition def = CodeSegDefinitionParser.ParseDefinition( aLine );
       
   253                     if ( def != null )
       
   254                     {
       
   255                         DataSource.MetaData.CodeSegments.Add( def );
       
   256                     }
       
   257                 }
       
   258             }
       
   259             //
       
   260             return handled;
       
   261         }
       
   262 
       
   263         private bool CheckForKnownElement( ref string aLine, string[] aPrefixes )
       
   264         {
       
   265             bool handled = false;
       
   266             //
       
   267             foreach ( string prefix in aPrefixes )
       
   268             {
       
   269                 if ( aLine.Contains( prefix ) )
       
   270                 {
       
   271                     PrefixParser.SkipPrefix( prefix, ref aLine );
       
   272                     handled = true;
       
   273                     break;
       
   274                 }
       
   275             }
       
   276             //
       
   277             return handled;
       
   278         }
       
   279         #endregion
       
   280 
       
   281         #region Internal enumerations
       
   282         private enum TState
       
   283         {
       
   284             EStateNone = 0,
       
   285             EStateStatisticsFree,
       
   286             EStateStatisticsAlloc,
       
   287             EStateFreeCellList,
       
   288         }
       
   289         #endregion
       
   290 
       
   291         #region Internal constants
       
   292         private readonly static Regex KFreeCellRegEx = new Regex(
       
   293            "FC\\s(?<Index>[0-9]{4})\\s+0x(?<Address>[0-9a-fA-F]{8})\\s+(" +
       
   294            "?<Length>[0-9]+)\\s(?<Type>[0-9]{1})\r\n",
       
   295            RegexOptions.Multiline
       
   296          | RegexOptions.CultureInvariant
       
   297          | RegexOptions.IgnorePatternWhitespace
       
   298          | RegexOptions.Compiled
       
   299          ); 
       
   300         #endregion
       
   301 
       
   302         #region Data members
       
   303         private Prefixes iPrefixes = new Prefixes();
       
   304         private TState iState = TState.EStateNone;
       
   305         #endregion
       
   306     }
       
   307 }