crashanalysercmd/Libraries/File Formats/Plugins/DExcPlugin/Transformer/DExcTransformer.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.Text.RegularExpressions;
       
    20 using System.IO;
       
    21 using System.Collections.Generic;
       
    22 using CrashItemLib.PluginAPI;
       
    23 using CrashItemLib.Crash;
       
    24 using CrashItemLib.Crash.Source;
       
    25 using CrashItemLib.Crash.Processes;
       
    26 using CrashItemLib.Crash.Registers;
       
    27 using CrashItemLib.Crash.Registers.Special;
       
    28 using CrashItemLib.Crash.Utils;
       
    29 using CrashItemLib.Crash.Threads;
       
    30 using CrashItemLib.Crash.CodeSegs;
       
    31 using CrashItemLib.Crash.Stacks;
       
    32 using CrashItemLib.Crash.Summarisable;
       
    33 using CrashItemLib.Crash.Messages;
       
    34 using CrashItemLib.Crash.Telephony;
       
    35 using CrashItemLib.Crash.Memory;
       
    36 using CrashItemLib.Crash.InfoHW;
       
    37 using CrashItemLib.Crash.InfoSW;
       
    38 using CrashItemLib.Crash.Events;
       
    39 using CrashItemLib.Crash.Header;
       
    40 using CrashItemLib.Crash.Container;
       
    41 using SymbianStructuresLib.Arm.Registers;
       
    42 using SymbianUtils.DataBuffer;
       
    43 using SymbianUtils.DataBuffer.Primer;
       
    44 using SymbianUtils.Range;
       
    45 using DExcPlugin.Extractor;
       
    46 using EM=DExcPlugin.ExpressionManager.DExcExpressionManager;
       
    47 
       
    48 namespace DExcPlugin.Transformer
       
    49 {
       
    50 	internal class DExcTransformer
       
    51 	{
       
    52 		#region Constructors
       
    53         public DExcTransformer( CFFSource aDescriptor, CFFDataProvider aDataProvider, DExcExtractedData aData )
       
    54 		{
       
    55             iData = aData;
       
    56             iDescriptor = aDescriptor;
       
    57             iDataProvider = aDataProvider;
       
    58         }
       
    59         #endregion
       
    60 
       
    61         #region API
       
    62         public CIContainer Transform()
       
    63         {
       
    64             try
       
    65             {
       
    66                 iContainer = iDataProvider.CreateContainer( iDescriptor );
       
    67 
       
    68                 SaveInputData();
       
    69 
       
    70                 CreateHeader();
       
    71                 CIProcess process = CreateProcess();
       
    72                 CreateThread( process );
       
    73             }
       
    74             catch ( Exception e )
       
    75             {
       
    76 #if DEBUG
       
    77                 System.Diagnostics.Debug.WriteLine( "DEXC READER QUEUE EXCEPTION: " + e.Message );
       
    78                 System.Diagnostics.Debug.WriteLine( "DEXC READER QUEUE STACK: " + e.StackTrace );
       
    79 #endif
       
    80                 //
       
    81                 iContainer = iDataProvider.CreateErrorContainer( iDescriptor );
       
    82                 CIMessageError error = new CIMessageError( iContainer, "Error" );
       
    83                 error.AddLine( e.Message );
       
    84                 iContainer.Messages.Add( error );
       
    85             }
       
    86             //
       
    87             return iContainer;
       
    88         }
       
    89         #endregion
       
    90 
       
    91         #region Internal methods
       
    92         private void SaveInputData()
       
    93         {
       
    94             string inputData = iData.ToString();
       
    95 
       
    96             // Convert the entire text data into binary
       
    97             List<byte> inputDataAsBytes = new List<byte>();
       
    98 
       
    99             // We just want to map the raw unicode character onto a single byte.
       
   100             // ASCII range is probably not sufficient (guess?) so this is why we
       
   101             // do not use System.Text.ASCIIEncoding, but rather roll our own.
       
   102             foreach ( char c in inputData )
       
   103             {
       
   104                 byte b = System.Convert.ToByte( c );
       
   105                 inputDataAsBytes.Add( b );
       
   106             }
       
   107 
       
   108             CISourceElement source = iContainer.Source;
       
   109             source.InputDataClear();
       
   110             source.InputDataAdd( inputDataAsBytes.ToArray() );
       
   111         }
       
   112 
       
   113         private void CreateHeader()
       
   114         {
       
   115             CIHeader header = iContainer.Header;
       
   116             header.CrashTime = iDescriptor.MasterFile.LastWriteTime;
       
   117             header.FileFormatVersion = "D_EXC for Symbian OS EKA2";
       
   118         }
       
   119 
       
   120         private CIProcess CreateProcess()
       
   121         {
       
   122             CIProcess process = new CIProcess( iContainer );
       
   123 
       
   124             ExtractProcess( process );
       
   125             ExtractProcessCodeSegs( process );
       
   126 
       
   127             iContainer.AddChild( process );
       
   128             return process;
       
   129         }
       
   130 
       
   131         private CIThread CreateThread( CIProcess aProcess )
       
   132         {
       
   133             // Make a new thread
       
   134             CIThread thread = aProcess.CreateThread();
       
   135 
       
   136             // Extract items
       
   137             ExtractThread( thread );
       
   138             ExtractThreadExitReason( thread );
       
   139             ExtractThreadRegisters( thread );
       
   140             ExtractThreadStack( thread );
       
   141 
       
   142             iContainer.AddChild( thread );
       
   143             return thread;
       
   144         }
       
   145         #endregion
       
   146 
       
   147         #region Internal constants
       
   148         #endregion
       
   149 
       
   150         #region Helpers - process
       
   151         private void ExtractProcess( CIProcess aProcess )
       
   152         {
       
   153             // Extract process info from thread full name.
       
   154             DExcExtractorList threadInfo = iData[ DExcExtractorListType.EListThread ];
       
   155             foreach ( string line in threadInfo )
       
   156             {
       
   157                 Match m = EM.ThreadName.Match( line );
       
   158                 if ( m.Success )
       
   159                 {
       
   160                     CIFullNameUtils parser = new CIFullNameUtils( m.Groups[ 1].Value );
       
   161                     parser.GetProcessInfo( aProcess );
       
   162                     
       
   163                     return;
       
   164                 }
       
   165             }
       
   166         }
       
   167 
       
   168         private void ExtractProcessCodeSegs( CIProcess aProcess )
       
   169         {
       
   170             DExcExtractorList codeSegInfo = iData[ DExcExtractorListType.EListCodeSegments ];
       
   171             foreach ( string line in codeSegInfo )
       
   172             {
       
   173                 Match m = EM.CodeSegmentsEntry.Match( line );
       
   174                 if ( m.Success )
       
   175                 {
       
   176                     GroupCollection groups = m.Groups;
       
   177                     //
       
   178                     uint codeSegBase = uint.Parse( groups[ 1 ].Value, System.Globalization.NumberStyles.HexNumber );
       
   179                     uint codeSegLimit = uint.Parse( groups[ 2 ].Value, System.Globalization.NumberStyles.HexNumber );
       
   180                     string codeSegName = groups[ 3 ].Value;
       
   181                     //
       
   182                     aProcess.CreateCodeSeg( codeSegName, codeSegBase, codeSegLimit );
       
   183                 }
       
   184             }
       
   185         }
       
   186         #endregion
       
   187 
       
   188         #region Helpers - thread
       
   189         private void ExtractThread( CIThread aThread )
       
   190         {
       
   191             // Extract process info from thread full name.
       
   192             DExcExtractorList threadInfo = iData[ DExcExtractorListType.EListThread ];
       
   193             foreach ( string line in threadInfo )
       
   194             {
       
   195                 Match m = EM.ThreadName.Match( line );
       
   196                 if ( m.Success )
       
   197                 {
       
   198                     CIFullNameUtils parser = new CIFullNameUtils( m.Groups[ 1 ].Value );
       
   199                     parser.GetThreadInfo( aThread );
       
   200                 }
       
   201                 else
       
   202                 {
       
   203                     m = EM.ThreadId.Match( line );
       
   204                     if ( m.Success )
       
   205                     {
       
   206                         aThread.Id = int.Parse( m.Groups[ 1 ].Value );
       
   207                     }
       
   208                 }
       
   209             }
       
   210         }
       
   211 
       
   212         private void ExtractThreadExitReason( CIThread aThread )
       
   213         {
       
   214             aThread.ExitInfo.Type = CrashItemLib.Crash.ExitInfo.CIExitInfo.TExitType.EExitTypeException;
       
   215 
       
   216             // Extract process info from thread full name.
       
   217             DExcExtractorList threadInfo = iData[ DExcExtractorListType.EListThread ];
       
   218             foreach ( string line in threadInfo )
       
   219             {
       
   220                 Match m = EM.ThreadPanicDetails.Match( line );
       
   221                 if ( m.Success )
       
   222                 {
       
   223                     aThread.ExitInfo.Type = CrashItemLib.Crash.ExitInfo.CIExitInfo.TExitType.EExitTypePanic;
       
   224                     aThread.ExitInfo.Category = m.Groups[ 1 ].Value;
       
   225                     aThread.ExitInfo.Reason = int.Parse( m.Groups[ 2 ].Value );
       
   226                 }
       
   227             }
       
   228         }
       
   229 
       
   230         private void ExtractThreadRegisters( CIThread aThread )
       
   231         {
       
   232             CIThreadRegisterListCollection threadRegs = aThread.Registers;
       
   233             CIRegisterList regListUser = threadRegs[ TArmRegisterBank.ETypeUser ];
       
   234             CIRegisterList regListEXC = threadRegs[ TArmRegisterBank.ETypeException ];
       
   235             CIRegisterList regListCOP = threadRegs[ TArmRegisterBank.ETypeCoProcessor ];
       
   236             CIRegisterList regListSVC = threadRegs[ TArmRegisterBank.ETypeSupervisor ];
       
   237 
       
   238             #region User registers
       
   239             foreach ( string line in iData[ DExcExtractorListType.EListRegistersUser ] )
       
   240             {
       
   241                 Match m = EM.RegistersUserSet.Match( line );
       
   242                 if ( m.Success )
       
   243                 {
       
   244                     GroupCollection groups = m.Groups;
       
   245                     int firstReg = int.Parse( groups[ 1 ].Value );
       
   246                     for ( int i = firstReg; i < firstReg + 4; i++ )
       
   247                     {
       
   248                         Group gp = groups[ 2 + ( i - firstReg ) ];
       
   249                         uint value = uint.Parse( gp.Value, System.Globalization.NumberStyles.HexNumber );
       
   250                         TArmRegisterType regType = (TArmRegisterType) i;
       
   251                         regListUser[ regType ].Value = value;
       
   252                     }
       
   253                 }
       
   254                 else
       
   255                 {
       
   256                     m = EM.RegistersUserCPSR.Match( line );
       
   257                     if ( m.Success )
       
   258                     {
       
   259                         // Get CPSR value and set it
       
   260                         uint cpsrValue = uint.Parse( m.Groups[ 1 ].Value, System.Globalization.NumberStyles.HexNumber );
       
   261                         threadRegs.CPSR = cpsrValue;
       
   262                     }
       
   263                 }
       
   264             }
       
   265             #endregion
       
   266 
       
   267             #region Exception registers
       
   268             foreach ( string line in iData[ DExcExtractorListType.EListRegistersException ] )
       
   269             {
       
   270                 Match m = EM.RegistersExceptionSet1.Match( line );
       
   271                 if ( m.Success )
       
   272                 {
       
   273                     GroupCollection groups = m.Groups;
       
   274                     //
       
   275                     regListEXC[ TArmRegisterType.EArmReg_EXCCODE ].Value = uint.Parse( m.Groups[ 1 ].Value, System.Globalization.NumberStyles.HexNumber );
       
   276                     regListEXC[ TArmRegisterType.EArmReg_EXCPC ].Value = uint.Parse( m.Groups[ 2 ].Value, System.Globalization.NumberStyles.HexNumber );
       
   277                     //
       
   278                     regListCOP[ TArmRegisterType.EArmReg_FAR ].Value = uint.Parse( m.Groups[ 3 ].Value, System.Globalization.NumberStyles.HexNumber );
       
   279                     regListCOP[ TArmRegisterType.EArmReg_FSR ].Value = uint.Parse( m.Groups[ 4 ].Value, System.Globalization.NumberStyles.HexNumber );
       
   280 
       
   281                     if ( regListEXC.Contains( TArmRegisterType.EArmReg_EXCCODE ) )
       
   282                     {
       
   283                         CIRegister reg = regListEXC[ TArmRegisterType.EArmReg_EXCCODE ];
       
   284                         System.Diagnostics.Debug.Assert( reg is CIRegisterExcCode );
       
   285                         CIRegisterExcCode excReg = (CIRegisterExcCode) reg;
       
   286                         //
       
   287                         excReg.ExpandToFullExceptionRange();
       
   288                     }
       
   289 
       
   290                     // It also means it was an exception
       
   291                     aThread.ExitInfo.Type = CrashItemLib.Crash.ExitInfo.CIExitInfo.TExitType.EExitTypeException;
       
   292                 }
       
   293                 else
       
   294                 {
       
   295                     m = EM.RegistersExceptionSet2.Match( line );
       
   296                     if ( m.Success )
       
   297                     {
       
   298                         GroupCollection groups = m.Groups;
       
   299                         //
       
   300                         regListSVC[ TArmRegisterType.EArmReg_SP ].Value = uint.Parse( m.Groups[ 1 ].Value, System.Globalization.NumberStyles.HexNumber );
       
   301                         regListSVC[ TArmRegisterType.EArmReg_LR ].Value = uint.Parse( m.Groups[ 2 ].Value, System.Globalization.NumberStyles.HexNumber );
       
   302                         regListSVC[ TArmRegisterType.EArmReg_SPSR ].Value = uint.Parse( m.Groups[ 3 ].Value, System.Globalization.NumberStyles.HexNumber );
       
   303                     }
       
   304                 }
       
   305             }
       
   306             #endregion
       
   307         }
       
   308 
       
   309         private void ExtractThreadStack( CIThread aThread )
       
   310         {
       
   311             DExcExtractorListStackData stackDataList = (DExcExtractorListStackData) iData[ DExcExtractorListType.EListStackData ];
       
   312             DataBuffer stackData = stackDataList.StackData;
       
   313 
       
   314             // Get stack range details
       
   315             DExcExtractorListThreadInfo threadInfo = (DExcExtractorListThreadInfo) iData[ DExcExtractorListType.EListThread ];
       
   316             AddressRange stackRange = threadInfo.StackRange;
       
   317 
       
   318             // If we didn't get the stack range, we cannot create a stack entry
       
   319             if ( !stackRange.IsValid || stackRange.Max == 0 || stackRange.Min == 0 )
       
   320             {
       
   321                 CIMessageWarning warning = new CIMessageWarning( aThread.Container, "Stack Address Range Unavailable" );
       
   322                 warning.AddLine( "The stack address range details are invalid." );
       
   323                 aThread.AddChild( warning );
       
   324             }
       
   325             else if ( stackData.Count == 0 )
       
   326             {
       
   327                 // No stack data
       
   328                 CIMessageWarning warning = new CIMessageWarning( aThread.Container, "Stack Data Unavailable" );
       
   329                 warning.AddLine( "The crash details contain no stack data." );
       
   330                 aThread.AddChild( warning );
       
   331             }
       
   332             else
       
   333             {
       
   334                 // Set base address of data buffer if not already set
       
   335                 if ( stackData.AddressOffset == 0 )
       
   336                 {
       
   337                     stackData.AddressOffset = stackRange.Min;
       
   338                 }
       
   339 
       
   340                 // In theory, D_EXC only ever captures user-side crashes (panics/exceptions) therefore
       
   341                 // we should always be able to assume that the stack data goes with a user-side thread.
       
   342                 CIRegisterList userRegs = aThread.Registers[ TArmRegisterBank.ETypeUser ];
       
   343                 if ( userRegs != null )
       
   344                 {
       
   345                     CIStack stack = aThread.CreateStack( userRegs, stackData, stackData.AddressOffset, stackRange );
       
   346 
       
   347                     // Register it as a specific crash instance
       
   348                     iContainer.AddChild( stack );
       
   349                 }
       
   350             }
       
   351         }
       
   352         #endregion
       
   353 
       
   354         #region Data members
       
   355         private readonly CFFDataProvider iDataProvider;
       
   356         private readonly CFFSource iDescriptor;
       
   357         private readonly DExcExtractedData iData = null;
       
   358         private CIContainer iContainer = null;
       
   359 		#endregion
       
   360 	}
       
   361 }