os/kernelhwsrv/kernel/eka/debug/crashMonitor/src/scmonitor.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // e32\debug\crashMonitor\src\scmonitor.cpp
    15 // Core dump server - Kernel side crash monitor
    16 // 
    17 //
    18 
    19 /**
    20  @file
    21  @internalTechnology
    22 */
    23 
    24 #include <scmonitor.h>
    25 #include <kernel/monitor.h>
    26 #include <assp.h>
    27 #include <drivers/crashflash.h>
    28 #include <kernel/klib.h>
    29 #include <crashlogwalker.h>
    30 #include <scmconfigitem.h>
    31 
    32 #include "scmdatasave.h"
    33 
    34 GLDEF_D SCMonitor TheSCMonitor; //global definition of SCMonitor
    35 
    36 //keep things 4 byte aligned
    37 const TInt KRestartType = SCMonitor::ESoftRestart;
    38 
    39 /**
    40 SCMonitor constructor
    41 */
    42 SCMonitor::SCMonitor()
    43 	: iMultiCrashInfo(NULL)
    44 	{
    45 	}
    46 
    47 SCMonitor::~SCMonitor()
    48 	{
    49 	delete iMultiCrashInfo;
    50 	}
    51 
    52 /**
    53  Print data to the corresponding output channel. Derived from monitor
    54  @param aDes the buffer containing the data
    55  */
    56 void SCMonitor::Print (const TDesC8& aDes )
    57 	{
    58 	//intended to do nothing
    59 	}
    60 
    61 /**
    62  * Allocates resources for SCMonitor
    63  * cant fully construct in constructor as we are a kernel extension and resources are limited when we are created
    64  */
    65 void SCMonitor::StableConstruction()
    66 	{
    67 	LOG_CONTEXT
    68 	iDataSave = new SCMDataSave(this, TheSCMonitor.iFlash);	
    69 	
    70 	//Configuration object for use upon crash
    71 	iScmConfig = new SCMConfiguration();
    72 	TInt err = iScmConfig->SetDefaultConfig();	
    73 	if(KErrNone != err)
    74 		{
    75 		CLTRACE1("SCMonitor::StableConstruction - Unable to set default config err = %d", err);
    76 		}
    77 	
    78 	
    79 #ifdef NO_MULTICRASHINFO	
    80 	iMultiCrashInfo = NULL;
    81 #else	
    82 
    83 	//We need to take a look at the flash map from variant_norflash_layout.h
    84 	iMultiCrashInfo = new SCMMultiCrashInfo();
    85 
    86 	TUint numberBlocks = KCrashLogSize / KCrashLogBlockSize;
    87 	for(TUint32 cnt = 0; cnt < numberBlocks; cnt++)
    88 		{
    89 		iMultiCrashInfo->AddBlock(new SCMCrashBlockEntry(cnt, cnt * KCrashLogBlockSize, KCrashLogBlockSize));
    90 		}
    91 #endif
    92 	}
    93 
    94 /**
    95  * Start a secondary DFC queue for the Flash and Init the flash in the variant(h4)
    96  * @param aAny
    97  */
    98 void StartSecondary (TAny* )
    99 	{
   100 	LOG_CONTEXT
   101 	//InitFlash is implemented in the variant as it creates a variant
   102 	//specific derived CrashFlash
   103 	TheSCMonitor.InitFlash ( );
   104 	TheSCMonitor.StableConstruction();
   105 	}
   106 
   107 /**
   108  * Global method to create a dfc queue
   109  * @param Method to intialise the flash.
   110  * @param Null
   111  * @param Gets the address of the supervisor thread DFC queue
   112  * @param TDfcQ priority number
   113  * @return a DFC object
   114  */
   115 GLDEF_C TDfc StartSecondaryDfc(&StartSecondary, NULL, Kern::SvMsgQue(), KMaxDfcPriority-1);
   116 
   117 /**
   118  * Kernel Main module entry - Own implementation( similar to crash logger)
   119  * @param aReason reason to enter to the method
   120  * @return One of the system wide codes
   121  */
   122 GLDEF_C TInt KernelModuleEntry(TInt aReason)
   123 	{	
   124 	if(aReason==KModuleEntryReasonVariantInit0)
   125 		{
   126 		new(&TheSCMonitor) SCMonitor;
   127 		// We are going to register the system Crash monitor here so that the order
   128 		// the monitor modules are placed in rom is preserved.  
   129 		// The monitor is not fully intialised here.
   130 		//the variant target is missing as we still have to finalise on the crash flash 
   131 		//implementation. H2 & H4 doesnt support currently.
   132 		LOG_CONTEXT		
   133 		CLTRACE("Installing System Crash Monitor");
   134 		Monitor::RegisterMonitorImpl (&TheSCMonitor );
   135 		return KErrNone;
   136 		}
   137 	else if (aReason==KModuleEntryReasonExtensionInit0 )
   138 		{
   139 		return KErrNone;
   140 		}
   141 	else if (aReason==KModuleEntryReasonExtensionInit1 )
   142 		{
   143 		LOG_CONTEXT
   144 		CLTRACE("Enqueing dfc to init crash flash for System Crash Monitor after all modules loaded");
   145 		StartSecondaryDfc.Enque ( );
   146 		return KErrNone;
   147 		}
   148 	return KErrArgument;
   149 	}
   150 
   151 /**
   152  Method to intialize the system crash monitor
   153  @param aCategory the fault category type
   154  @param aReason the reason for crash
   155  @return restart type
   156  */
   157 TInt SCMonitor::Init2 (TAny* aCategory, TInt aReason )
   158 	{
   159 	LOG_CONTEXT
   160 	__KTRACE_OPT(KALWAYS, Kern::Printf("\n\nSystem Crash Monitor Launched: To Analyse Crash Produced Use Core Dump Server\n"));
   161 
   162 	//Start logging the data:	
   163 	//Need to lock kernel to access object containers (it technically is anyway, but flag isnt set)
   164 	NKern::Lock();
   165 	DoCrash(aCategory, aReason);	
   166 	NKern::Unlock();		
   167 		
   168 	__KTRACE_OPT(KALWAYS, Kern::Printf("System Crash Monitor Finished: Log Size = [%d]\n", iDataSave->iCrashInf.iLogSize));
   169 	
   170 	return KRestartType;
   171 	}
   172 
   173 /**
   174  * This is responsible for setting up any structures required for processing of the crash
   175  * @param aCategory the fault category type
   176  * @param aReason 
   177  */
   178 void SCMonitor::DoCrash(TAny* aCategory, TInt aReason )
   179 	{
   180 	// get debug mask
   181 	TInt dbgMask = Kern::SuperPage().iDebugMask[0];
   182 	
   183 	// if we are writing to the comm port then we need to turn off other debug messages
   184 	if( iDataSave->GetWriteSelect() == SCMDataSave::EWriteComm)
   185 		{
   186 		Kern::SuperPage().iDebugMask[0] = 0;
   187 		}
   188 	
   189 	if(!aCategory)
   190 		{
   191 		CLTRACE("\tNULL category retrieved and returning");
   192 		TheSCMonitor.iFlash->EndTransaction();
   193 		return;
   194 		}
   195 
   196 	iFrame = NULL;
   197 	
   198 	CLTRACE("\tAbout to set category -- note: can occasionaly crash board");	
   199 	iFaultCategory = *(const TDesC8*)aCategory;  // this crashes the board sometimes		
   200 	iFaultReason = aReason;
   201 	Epoc::SetMonitorExceptionHandler ((TLinAddr)HandleException );
   202 	
   203 	// get the first start block
   204 	// will retieve start of flash by default
   205 	SCMCrashBlockEntry block;
   206 	TInt err = GetNextCrashStartPoint(block);  // will also attempt to read iScmConfig
   207 
   208 	if(KErrNone == err)
   209 		{
   210 		CLTRACE2("SCMonitor::DoCrash next crash will be written at blocknumber = %d offset  %d"
   211 				, block.iBlockNumber, block.iBlockOffset);
   212 		}
   213 	else
   214 		{
   215 		CLTRACE1("SCMonitor::DoCrash Failed to find a valid block to write to, can not continue. err = [%d]", err);
   216 		return;
   217 		}
   218 	
   219 	TUint crashId = block.iBlockNumber;	
   220 	iDataSave->iWriter->ResetBytesWritten();		
   221 	
   222 	//Write the crash (1st pass is to gather header data)
   223 	TInt spaceRequired = ProcessCrash(block, crashId, EFalse);					
   224 	
   225 	// now do the real write	
   226 	// prepare flash for data	
   227 	TheSCMonitor.iFlash->StartTransaction();	
   228 	TheSCMonitor.iFlash->SetWritePos(block.iBlockOffset);
   229 
   230 	//write the crash this time
   231 	ProcessCrash(block, crashId, ETrue);
   232 	
   233 	TheSCMonitor.iFlash->EndTransaction();	
   234 	
   235 	// restore debug mask
   236 	Kern::SuperPage().iDebugMask[0] = dbgMask;
   237 	}
   238 
   239 
   240 /**
   241  * This walks the existing crash log and finds out where current crashes finish
   242  * @param aBlockEntry Block to use. Only valid if KErrNone is returned.
   243  * @return One of the OS wide codes
   244  */
   245 TInt SCMonitor::GetNextCrashStartPoint(SCMCrashBlockEntry& aBlockEntry)
   246 	{
   247 	LOG_CONTEXT	
   248 	
   249 	//First thing is to try and read the config
   250 	TBool configFound = (iDataSave->ReadConfig(*iScmConfig) == KErrNone);
   251 	
   252 	if( iMultiCrashInfo)	
   253 		{				
   254 		/**
   255 		 * data save has been configured to use multicrash info to find the next block we are on we need to scan each
   256 		 * block to see if it contains a valid header. if we find an empty block in our block list then that is the
   257 		 * one we will use if we find no empty blocks then we have no room left	
   258 		 */
   259 		iMultiCrashInfo->Reset();
   260 		SCMCrashBlockEntry* block = iMultiCrashInfo->GetNextBlock();
   261 		TBool blockFound = EFalse;				
   262 		
   263 		//For any crashes in flash, we need to record where they end, so that we can then go to the next
   264 		//block after the one in which it ends
   265 		TInt crashEndPoint = 0;		
   266 		
   267 		while(block)
   268 			{	
   269 			CLTRACE1("SCMonitor::GetNextCrashStartPoint Processing block number %d", block->iBlockNumber );			
   270 			
   271 			//If we have already found our block, we should erase subsequent ones for use
   272 			if(blockFound)
   273 				{
   274 				TInt err = EraseFlashBlock(*block);
   275 				if(err != KErrNone)
   276 					{					
   277 					return err;
   278 					}
   279 				
   280 				block = iMultiCrashInfo->GetNextBlock(); 
   281 				continue;
   282 				}	
   283 			
   284 			//is this block before a crash end? if it is, we cant use it as a crash can span multiple blocks
   285 			if(block->iBlockOffset >= crashEndPoint)
   286 				{
   287 				//special condition if we have a config
   288 				TUint startPos = block->iBlockOffset;
   289 				TUint skipBytes = 0;
   290 				if(configFound && block->iBlockOffset == 0)
   291 					{
   292 					startPos+=iScmConfig->GetSize();
   293 					
   294 					//must align to flash for read
   295 					skipBytes = startPos % KFlashAlignment;	
   296 					startPos -= skipBytes;
   297 					}
   298 				
   299 				// try and read an info header at these flash coords
   300 				TBuf8<TCrashInfoHeader::KSCMCrashInfoMaxSize + KFlashAlignment> buf;
   301 				buf.SetLength(TCrashInfoHeader::KSCMCrashInfoMaxSize + KFlashAlignment);
   302 	
   303 				CLTRACE1("(SCMonitor::GetNextCrashStartPoint) reading at offset %d", block->iBlockOffset);
   304 				
   305 				TheSCMonitor.iFlash->SetReadPos(startPos);
   306 				TheSCMonitor.iFlash->Read(buf);
   307 				
   308 				// create the buffer applying the offset of bytes skipped
   309 				TByteStreamReader reader(const_cast<TUint8*> (buf.Ptr() + skipBytes));
   310 				
   311 				TCrashInfoHeader header;								
   312 				TInt err = header.Deserialize(reader);
   313 				
   314 				if(err == KErrCorrupt)
   315 					{
   316 					CLTRACE2("(SCMonitor::GetNextCrashStartPoint) Found empty block blocknumber %d blockoffset = %d"
   317 							, block->iBlockNumber, block->iBlockOffset);
   318 										
   319 					blockFound = ETrue;
   320 					aBlockEntry = *block;
   321 					
   322 					continue; //Dont get next block, as next run will erase this current block for use
   323 					}
   324 				else
   325 					{					
   326 					crashEndPoint = header.iLogSize + startPos;
   327 					CLTRACE3("(SCMonitor::GetNextCrashStartPoint) In block [%d] we found a valid crash header. This crash finishes at [%d] [0x%X]", block->iBlockNumber, crashEndPoint, crashEndPoint);
   328 					}
   329 				}
   330 			
   331 			block = iMultiCrashInfo->GetNextBlock();
   332 			}									
   333 			
   334 		if(blockFound)
   335 			{
   336 			return KErrNone;
   337 			}
   338 		else
   339 			{
   340 			//CLTRACE("(SCMonitor::GetNextCrashStartPoint) No available blocks TREATING as NO MULTICRASH INFO will write to default block");
   341 			//In this case should we just overwrite old crashes and return the first block as the comment above suggests
   342 			//return blockFound;
   343 			}
   344 		}
   345 
   346 	// no multi crash info supplied - use default first block settings
   347 	TInt err = EraseEntireFlashPartition();
   348 	if(err != KErrNone)
   349 		{
   350 		CLTRACE1("Unable to delete area required to log to flash. Aborting. Error - [%d]", err);
   351 		return err;
   352 		}
   353 	
   354 	aBlockEntry = SCMCrashBlockEntry(0,0,0);
   355 	return KErrNone;
   356 	}
   357 
   358 /**
   359  * Handles the processing of the crash
   360  * @return The size of the crash log (including header) that has been/will be written
   361  */
   362 TInt SCMonitor::ProcessCrash(const SCMCrashBlockEntry& aBlock, TUint aCrashId, TBool aCommit)
   363 	{	
   364 	LOG_CONTEXT
   365 	CLTRACE5("aBlock.iBlockOffset = [%d]  [0x%X] aBlock.iBlockNumber = %d aBlock.iBlockSize = [%d]  [0x%X]",
   366 			aBlock.iBlockOffset, aBlock.iBlockOffset, aBlock.iBlockNumber, aBlock.iBlockSize, aBlock.iBlockSize);		
   367 	
   368 	// reset writer for start of each crash
   369 	iDataSave->iWriter->ResetBytesWritten();
   370 	TInt logLevel = 0;
   371 
   372 	if(aCommit)
   373 		{
   374 		logLevel = KALWAYS;
   375 		iDataSave->iWriter->EnablePhysicalWriting();	
   376 		}
   377 	else
   378 		{
   379 #if defined(_DEBUG)
   380 		logLevel = KDEBUGGER;
   381 #else
   382 		logLevel = KALWAYS; //Doesnt matter, KTRACE OPT is empty for rel builds 
   383 		if(logLevel != KALWAYS)
   384 			{
   385 			//This is to avoid warning
   386 			}
   387 #endif
   388 		
   389 		iDataSave->iWriter->DisablePhysicalWriting();	
   390 		}
   391 	
   392 	iDataSave->SetByteCount(aBlock.iBlockOffset);	
   393 	if(aBlock.iBlockOffset == 0 && aBlock.iBlockNumber == 0)
   394 		{	
   395 		// this is the first crash - we need to save the config here first
   396 		CLTRACE("(SCMonitor::ProcessCrash) - this is block 0 - WRITING CONFIG");
   397 		iDataSave->LogConfig(*iScmConfig);	
   398 		
   399 		//Config is not part of crash so reset bytes written			
   400 		iDataSave->SetCrashStartingPoint(iDataSave->iWriter->GetBytesWritten());		
   401 		}	
   402 	else
   403 		{
   404 		iDataSave->SetCrashStartingPoint(aBlock.iBlockOffset);
   405 		}	
   406 	
   407 	iDataSave->iWriter->ResetBytesWritten();
   408 	
   409 	TUint32 logSize = 0;
   410 	TUint sizeOfObjectDumped = 0;
   411 		
   412 	TInt err = iDataSave->LogCrashHeader(iFaultCategory, iFaultReason, aCrashId, sizeOfObjectDumped);		
   413 	if(KErrNone != err)
   414 		{
   415 		CLTRACE("System Crash Monitor: Failed to create crash info header - (TCrashInfo)"); 
   416 		return KRestartType;
   417 		}	
   418 	
   419 	logSize += sizeOfObjectDumped;
   420 	
   421 	//Now we must read the configuration to use. This is held at the start of our flash partition
   422 	//and managed by the iConfig object
   423 	iScmConfig->ResetToHighestPriority();	
   424 		
   425 	//Always want the crash context
   426 	iDataSave->iHdr.iCTFullRegOffset = logSize + iDataSave->GetCrashStartingPoint();
   427 	
   428 	err = iDataSave->LogCPURegisters(sizeOfObjectDumped);
   429 	if(KErrNone != err)
   430 		{
   431 		CLTRACE1("\tError logging full registers = %d", err);
   432 		}	
   433 	
   434 	logSize += sizeOfObjectDumped;
   435 	
   436 	CLTRACE("\tAbout to enter processing loop");		
   437 	SCMDataSave::TDataToDump dump;
   438 		
   439 	for(;;)		
   440 		{		
   441 		//now we get each item by priority from the configuration
   442 		TConfigItem* configItem = iScmConfig->GetNextItem();
   443 
   444 		if(!configItem)
   445 			{
   446 			// end of list
   447 			break;
   448 			}
   449 		
   450 		CLTRACE1("\nLooking at item type [%d]", configItem->GetDataType());
   451 		if(configItem->GetSpaceRequired() > iDataSave->SpaceRemaining())
   452 			{
   453 			__KTRACE_OPT(logLevel, Kern::Printf("\t\tFor Item Type [%d]: Unable to log [0x%X] [%d] bytes because we only have [0x%X] [%d] bytes left", configItem->GetDataType(), configItem->GetSpaceRequired(), configItem->GetSpaceRequired(), iDataSave->SpaceRemaining(), iDataSave->SpaceRemaining()));
   454 			continue;
   455 			}
   456 		else
   457 			{
   458 			CLTRACE1("Will require [%d] bytes for this item", configItem->GetSpaceRequired());
   459 			}
   460 		
   461 		// only interested in logging items with priority > 0
   462 		if( configItem->GetPriority() <= 0)
   463 			{
   464 			CLTRACE1("\tIgnored config item type %d priority 0", configItem->GetDataType());
   465 			continue;
   466 			}
   467 			
   468 		//there are a lot of TUints in the hdr to record where we wrote this item. 
   469 		//This will point to the one of interest for this configItem
   470 		TUint32* offsetPointer = NULL;				
   471 		
   472 		//now we check the type of data we wish to dump
   473 		switch(configItem->GetDataType())
   474 			{
   475 			case TConfigItem::ECrashedThreadMetaData:			
   476 				{
   477 				__KTRACE_OPT(logLevel, Kern::Printf("\tDoing: ECrashedThreadMetaData at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
   478 				
   479 				err = LogThreadMetaData(SCMDataSave::EThreadSpecific, sizeOfObjectDumped);
   480 				offsetPointer = &(iDataSave->iHdr.iCTMetaOffset);
   481 				
   482 				break;
   483 				}
   484 			case TConfigItem::EThreadsMetaData:			
   485 				{
   486 				__KTRACE_OPT(logLevel, Kern::Printf("\tDoing: EThreadsMetaData at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
   487 				
   488 				//record location we are writing to in the header
   489 				iDataSave->iHdr.iTLstOffset = iDataSave->iWriter->GetBytesWritten();
   490 				err = LogThreadMetaData(SCMDataSave::ESystemWide, sizeOfObjectDumped);
   491 				offsetPointer = &(iDataSave->iHdr.iTLstOffset);
   492 				
   493 				break;
   494 				}
   495 			case TConfigItem::ECrashedProcessMetaData:			
   496 				{
   497 				__KTRACE_OPT(logLevel, Kern::Printf("\tDoing: ECrashedProcessMetaData at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
   498 					
   499 				err = LogProcessMetaData(SCMDataSave::EProcessSpecific, sizeOfObjectDumped);
   500 				offsetPointer = &(iDataSave->iHdr.iCPMetaOffset);
   501 				
   502 				break;
   503 				}
   504 			case TConfigItem::EProcessMetaData:		
   505 				{
   506 				__KTRACE_OPT(logLevel, Kern::Printf("\tDoing: EProcessMetaData at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
   507 								
   508 				err = LogProcessMetaData(SCMDataSave::ESystemWide, sizeOfObjectDumped);
   509 				offsetPointer = &(iDataSave->iHdr.iPLstOffset);
   510 				
   511 				break;
   512 				}
   513 			case TConfigItem::ECrashedProcessUsrStacks:			
   514 				{
   515 				__KTRACE_OPT(logLevel, Kern::Printf("\tDoing: ECrashedProcessUsrStacks at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
   516 								
   517 				//define what we wish to dump
   518 				dump.iMetaData = EFalse;
   519 				dump.iCodeSegs = EFalse;
   520 				dump.iStk = SCMDataSave::EUsrStack;
   521 				dump.iReg = SCMDataSave::ERegSetNone;	
   522 				err = LogObjectContainers(EThread, SCMDataSave::EProcessSpecific, dump, sizeOfObjectDumped);	
   523 				offsetPointer = &(iDataSave->iHdr.iCTUsrStkOffset);
   524 				
   525 				break;
   526 				}
   527 			case TConfigItem::EThreadsUsrStack:			
   528 				{
   529 				__KTRACE_OPT(logLevel, Kern::Printf("\tDoing: EThreadsUsrStack at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
   530 				
   531 				//define what we wish to dump
   532 				dump.iMetaData = EFalse;
   533 				dump.iCodeSegs = EFalse;
   534 				dump.iStk = SCMDataSave::EUsrStack;
   535 				dump.iReg = SCMDataSave::ERegSetNone;
   536 
   537 				err = LogObjectContainers(EThread, SCMDataSave::ESystemWide, dump, sizeOfObjectDumped);
   538 				offsetPointer = &(iDataSave->iHdr.iSysSvrStkOffset);
   539 				
   540 				break;
   541 				}
   542 			case TConfigItem::ECrashedProcessSvrStacks:			
   543 				{
   544 				__KTRACE_OPT(logLevel, Kern::Printf("\tDoing: ECrashedProcessSvrStacks at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
   545 								
   546 				//define what we wish to dump
   547 				dump.iMetaData = EFalse;
   548 				dump.iCodeSegs = EFalse;
   549 				dump.iStk = SCMDataSave::ESvrStack;
   550 				dump.iReg = SCMDataSave::ERegSetNone;
   551 				
   552 				err = LogObjectContainers(EThread, SCMDataSave::EProcessSpecific, dump, sizeOfObjectDumped);
   553 				offsetPointer = &(iDataSave->iHdr.iCTSvrStkOffset);
   554 				
   555 				break;
   556 				}
   557 			case TConfigItem::EThreadsSvrStack:			
   558 				{
   559 				__KTRACE_OPT(logLevel, Kern::Printf("\tDoing: EThreadsSvrStack at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
   560 								
   561 				//define what we wish to dump
   562 				dump.iMetaData = EFalse;
   563 				dump.iCodeSegs = EFalse;
   564 				dump.iStk = SCMDataSave::ESvrStack;
   565 				dump.iReg = SCMDataSave::ERegSetNone;
   566 				
   567 				err = LogObjectContainers(EThread, SCMDataSave::ESystemWide, dump, sizeOfObjectDumped);
   568 				offsetPointer = &(iDataSave->iHdr.iSysSvrStkOffset);
   569 				
   570 				break;
   571 				}
   572 			case TConfigItem::EThreadsUsrRegisters:		
   573 				{
   574 				__KTRACE_OPT(logLevel, Kern::Printf("\tDoing: EThreadsUsrRegisters at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
   575 								
   576 				//define what we wish to dump
   577 				dump.iMetaData = EFalse;
   578 				dump.iCodeSegs = EFalse;
   579 				dump.iStk = SCMDataSave::EStackTypeNone;
   580 				dump.iReg = SCMDataSave::EUserRegisters;
   581 				
   582 				err = LogObjectContainers(EThread, SCMDataSave::ESystemWide, dump, sizeOfObjectDumped);
   583 				offsetPointer = &(iDataSave->iHdr.iSysUsrRegOffset);
   584 				
   585 				break;
   586 				}
   587 			case TConfigItem::EThreadsSvrRegisters:		
   588 				{
   589 				__KTRACE_OPT(logLevel, Kern::Printf("\tDoing: EThreadsSvrRegisters at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
   590 								
   591 				//define what we wish to dump
   592 				dump.iMetaData = EFalse;
   593 				dump.iCodeSegs = EFalse;
   594 				dump.iStk = SCMDataSave::EStackTypeNone;
   595 				dump.iReg = SCMDataSave::ESupervisorRegisters;
   596 				
   597 				err = LogObjectContainers(EThread, SCMDataSave::ESystemWide, dump, sizeOfObjectDumped);
   598 				offsetPointer = &(iDataSave->iHdr.iSysSvrRegOffset);
   599 				
   600 				break;
   601 				}
   602 			case TConfigItem::EExceptionStacks:
   603 				{
   604 				__KTRACE_OPT(logLevel, Kern::Printf("\tDoing: EExceptionStacks at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
   605 							
   606 				err = iDataSave->LogExceptionStacks(sizeOfObjectDumped);
   607 				offsetPointer = &(iDataSave->iHdr.iExcStkOffset);
   608 				
   609 				break;
   610 				}
   611 			case TConfigItem::ECrashedProcessCodeSegs:
   612 				{	
   613 				__KTRACE_OPT(logLevel, Kern::Printf("\tDoing: ECrashedProcessCodeSegs at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
   614 								
   615 				//define what we wish to dump
   616 				dump.iMetaData = EFalse;
   617 				dump.iCodeSegs = ETrue;
   618 				dump.iStk = SCMDataSave::EStackTypeNone;
   619 				dump.iReg = SCMDataSave::ERegSetNone;				
   620 				
   621 				err = LogObjectContainers(EProcess, SCMDataSave::EProcessSpecific, dump, sizeOfObjectDumped);
   622 				offsetPointer = &(iDataSave->iHdr.iCPCodeSegOffset);
   623 				
   624 				break;
   625 				}
   626 			case TConfigItem::EProcessCodeSegs:
   627 				{
   628 				__KTRACE_OPT(logLevel, Kern::Printf("\tDoing: EProcessCodeSegs at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
   629 								
   630 				//define what we wish to dump
   631 				dump.iMetaData = EFalse;
   632 				dump.iCodeSegs = ETrue;
   633 				dump.iStk = SCMDataSave::EStackTypeNone;
   634 				dump.iReg = SCMDataSave::ERegSetNone;
   635 				err = LogObjectContainers(EProcess, SCMDataSave::ESystemWide, dump, sizeOfObjectDumped);
   636 				offsetPointer = &(iDataSave->iHdr.iSysCodeSegOffset);
   637 				
   638 				break;
   639 				}
   640 			case TConfigItem::ETraceData:
   641 				{
   642 				__KTRACE_OPT(logLevel, Kern::Printf("\tDoing: ETraceData at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
   643 									
   644 				err = iDataSave->LogTraceBuffer(configItem->GetSizeToDump(), sizeOfObjectDumped);
   645 				offsetPointer = &(iDataSave->iHdr.iTraceOffset);
   646 				
   647 				break;
   648 				}		
   649 			case TConfigItem::ELocks:
   650 				{
   651 				__KTRACE_OPT(logLevel, Kern::Printf("\tDoing: ELocks at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));	
   652 					
   653 				err = iDataSave->LogLocks(sizeOfObjectDumped);
   654 				offsetPointer = &(iDataSave->iHdr.iScmLocksOffset);
   655 				
   656 				break;
   657 				}
   658 			case TConfigItem::EKernelHeap:
   659 				{
   660 				__KTRACE_OPT(logLevel, Kern::Printf("\tDoing: EKernelHeap at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
   661 				
   662 				err = iDataSave->LogKernelHeap(sizeOfObjectDumped);
   663 				offsetPointer = &(iDataSave->iHdr.iKernelHeapOffset);
   664 				
   665 				break;
   666 				}
   667 			case TConfigItem::EVariantSpecificData:
   668 				{
   669 				__KTRACE_OPT(logLevel, Kern::Printf("\tDoing: EVariantSpecificData at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
   670 				
   671 				err = iDataSave->LogVariantSpecificData(sizeOfObjectDumped);
   672 				offsetPointer = &(iDataSave->iHdr.iVarSpecInfOffset);
   673 				
   674 				break;
   675 				}
   676 			case TConfigItem::ERomInfo:
   677 				{
   678 				__KTRACE_OPT(logLevel, Kern::Printf("\tDoing: ERomInfo at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
   679 				
   680 				err = iDataSave->LogRomInfo(sizeOfObjectDumped);
   681 				offsetPointer = &(iDataSave->iHdr.iRomInfoOffset);
   682 				
   683 				break;
   684 				}
   685 			//unknown configuration type - something bad is going on
   686 			default: return 0;			
   687 			}				
   688 		
   689 		if(KErrNone != err)
   690 			{
   691 			__KTRACE_OPT(logLevel, Kern::Printf("\tError logging data: [%d]   Type = [%d]", err, aBlock.iBlockOffset));
   692 			continue;
   693 			}
   694 		
   695 		//Set the space required so next time around we will know in advance how much space we need
   696 		configItem->SetSpaceRequired(sizeOfObjectDumped);		
   697 		
   698 		//Note: the following steps are only required for the first time we call process crash. The second time,
   699 		//when physical writing is enabled, these will have been written already and so they dont matter
   700 		
   701 		//update the offset and logsize if we are going to dump this item
   702 		TUint32 absoluteLogPos = logSize + iDataSave->GetCrashStartingPoint();
   703 		if(absoluteLogPos+sizeOfObjectDumped < iDataSave->MaxLogSize())
   704 			{
   705 			//now, we must record where in the crash log this item will be dumped
   706 			*offsetPointer = absoluteLogPos;
   707 			logSize += sizeOfObjectDumped;
   708 			}
   709 		}
   710 	
   711 	iDataSave->iCrashInf.iLogSize = logSize;	
   712 	iDataSave->iWriter->FlushCache();		
   713 	
   714 	return iDataSave->iCrashInf.iLogSize;
   715 	}
   716 
   717 /**
   718  * Logs the meta data for processes
   719  * @param aCurrentProcess - scope to dump
   720  * @return one of the OS wide codes
   721  */
   722 TInt SCMonitor::LogProcessMetaData(SCMDataSave::TDumpScope aScope, TUint& aSizeDumped) const
   723 	{
   724 	LOG_CONTEXT
   725 	
   726 	SCMDataSave::TDataToDump dump;
   727 	dump.iMetaData = ETrue;
   728 	
   729 	return LogObjectContainers(EProcess, aScope, dump, aSizeDumped);
   730 	}
   731 
   732 /**
   733  * 
   734  * @param aCurrentThread -  to only do the current (crashed thread) or to do all the others
   735  * @return one of the OS wide codes
   736  */
   737 TInt SCMonitor::LogThreadMetaData(SCMDataSave::TDumpScope aScope, TUint& aSizeDumped) const
   738 	{
   739 	LOG_CONTEXT
   740 	
   741 	SCMDataSave::TDataToDump dump; 
   742 	dump.iMetaData = ETrue;
   743 	
   744 	return LogObjectContainers(EThread, aScope, dump, aSizeDumped);
   745 	}
   746 
   747 /**
   748  * Generic method that looks at all kernel objects of aObjectType
   749  * @param aObjectType
   750  * @param aDumpScope - if you wish to dump for the the current process, current thread or entire system
   751  * @param aDataToDump - data you wish to dump
   752  * @param aSizeDumped - records how much was dumped
   753  * @return
   754  */
   755 TInt SCMonitor::LogObjectContainers(TObjectType aObjectType, SCMDataSave::TDumpScope aDumpScope, const SCMDataSave::TDataToDump& aDataToDump, TUint& aSizeDumped) const
   756 	{
   757 	aSizeDumped = 0;
   758 	
   759 	if(aObjectType >= ENumObjectTypes)
   760 		{
   761 		return KErrArgument;
   762 		}
   763 	
   764 	//Get the object container for the given object type
   765 	DObjectCon* objectContainer = Kern::Containers()[aObjectType];
   766 	if(objectContainer == NULL)
   767 		{		
   768 		CLTRACE("tFailed to get object container");
   769 		return KErrNotFound;
   770 		}
   771 	
   772 	//Must check the mutex on this is ok otherwise the data will be in an inconsistent state
   773 	if(objectContainer->iMutex->iHoldCount)
   774 		{
   775 		CLTRACE("\tObject Container is in an inconsistant state");
   776 		return KErrCorrupt;
   777 		}
   778 	
   779 	TInt numObjects = objectContainer->Count();
   780 	TInt err = KErrNone;	
   781 	
   782 	for(TInt cnt = 0; cnt< numObjects; cnt ++)
   783 		{	
   784 		DObject* object = (*objectContainer)[cnt];
   785 		
   786 		//Are we interested in the object? scope only relevant for thread and process objects, for others, the scope is implicit
   787 		if(aObjectType == EThread)			
   788 			{
   789 			switch(aDumpScope)
   790 				{
   791 				case SCMDataSave::EThreadSpecific :
   792 					{
   793 					//if we are interested in the current thread and this is not it, continue
   794 					if(((DThread*)object) != &Kern::CurrentThread())
   795 						continue;
   796 					break;
   797 					}
   798 				case SCMDataSave::EProcessSpecific :
   799 					{
   800 					//if we are interested in the current proc and this is not it, continue
   801 					if(((DThread*)object)->iOwningProcess != &Kern::CurrentProcess())
   802 						continue;					
   803 					break;
   804 					}
   805 				case SCMDataSave::ESystemWide :
   806 				default: 
   807 					break;
   808 				}
   809 			}
   810 		else if(aObjectType == EProcess)
   811 			{
   812 			switch(aDumpScope)
   813 				{
   814 				case SCMDataSave::EProcessSpecific :
   815 					{
   816 					if((DProcess*)object != &Kern::CurrentProcess())
   817 						continue;
   818 					break;
   819 					}
   820 				case SCMDataSave::EThreadSpecific :  //thread specific process doesnt make sense
   821 					return KErrArgument;				
   822 				case SCMDataSave::ESystemWide :
   823 				default:
   824 					break;
   825 				}
   826 			}
   827 		
   828 		//Now we look at the data we have been asked to dump
   829 		if(aDataToDump.iMetaData)
   830 			{
   831 			TUint dumped = 0;
   832 			err = HelpDumpMetaData(object, aObjectType, dumped);
   833 			if(KErrNone != err)
   834 				{
   835 				CLTRACE1("Failed to meta data: [%d]", err);
   836 				return err;
   837 				}
   838 			aSizeDumped += dumped;
   839 			}
   840 		
   841 		if(aDataToDump.iCodeSegs)
   842 			{
   843 			if(aObjectType != EProcess)
   844 				{
   845 				return KErrArgument;
   846 				}
   847 			
   848 			TUint dumped = 0;
   849 			err = iDataSave->LogCodeSegments((DProcess*)object, dumped);
   850 			if(KErrNone != err)
   851 				{
   852 				CLTRACE1("Failed to log code segments: [%d]", err);
   853 				return err;
   854 				}
   855 			aSizeDumped += dumped;
   856 			}
   857 		
   858 		if(aDataToDump.iStk != SCMDataSave::EStackTypeNone)
   859 			{
   860 			TUint dumped = 0;
   861 			err = HelpDumpStacks(object, aObjectType, dumped, aDataToDump.iStk);
   862 			if(KErrNone != err)
   863 				{
   864 				CLTRACE1("Failed to log stacks: [%d]", err);
   865 				return err;
   866 				}
   867 			aSizeDumped += dumped;
   868 			}
   869 		
   870 		if(aDataToDump.iReg != SCMDataSave::ERegSetNone)
   871 			{			
   872 			if(aObjectType != EThread)
   873 				{
   874 				return KErrArgument;
   875 				}
   876 			TUint dumped = 0;
   877 			err = iDataSave->LogRegisters((DThread*)object, aDataToDump.iReg, dumped);	
   878 			if(KErrNone != err && KErrNotSupported !=err)  //we expect to send down a KErrNotSupported when we ask for Full CPU set for the non crashed thread - thats fine
   879 				{
   880 				CLTRACE1("Failed to log registers: [%d]", err);
   881 				return err;
   882 				}
   883 			aSizeDumped += dumped;
   884 			}
   885 		}
   886 	
   887 	return KErrNone;
   888 	}
   889 
   890 /**
   891  * Helper method for dumping stacks. Looks to see what type of stack we want and then calls
   892  * appropriate method
   893  * @param aObject The DThread object whose stack we want
   894  * @param aObjectType The object type of this aObject. Anything other than EThread will give KErrArgument
   895  * @param aSizeDumped Holds the size of the stack dumped after processing 
   896  * @param aStkType The type of stack to be dumped
   897  * @see TObjectType
   898  * @see SCMDataSave::TStackType
   899  * @return One of the system wide codes
   900  */
   901 TInt SCMonitor::HelpDumpStacks(DObject* aObject, TObjectType aObjectType, TUint& aSizeDumped, SCMDataSave::TStackType aStkType) const
   902 	{
   903 	//verify args
   904 	if(aObjectType != EThread)
   905 		{
   906 		return KErrArgument;
   907 		}
   908 	
   909 	switch(aStkType)
   910 		{
   911 		case SCMDataSave::EUsrStack:
   912 			{
   913 			return iDataSave->LogThreadUserStack((DThread*)aObject, ETrue, aSizeDumped);
   914 			}
   915 		case SCMDataSave::ESvrStack:
   916 			{
   917 			return iDataSave->LogThreadSupervisorStack((DThread*)aObject, ETrue, aSizeDumped);
   918 			}
   919 		default: return KErrArgument;
   920 		}
   921 	}
   922 
   923 /**
   924  * Helper method to dump meta data about a DThread or a DProcess object
   925  * @param aObject DObject to use
   926  * @param aObjectType Type of DObject. Must be EThread or EProcess
   927  * @param aSizeDumped Holds the size of the stack dumped after processing 
   928  * @return
   929  */
   930 TInt SCMonitor::HelpDumpMetaData(DObject* aObject, TObjectType aObjectType, TUint& aSizeDumped) const
   931 	{
   932 	aSizeDumped = 0;
   933 	
   934 	switch(aObjectType)
   935 		{
   936 		case EThread:
   937 			{
   938 			return iDataSave->LogThreadData((DThread*)aObject, aSizeDumped);	 
   939 			}
   940 		case EProcess:
   941 			{
   942 			return iDataSave->LogProcessData((DProcess*)aObject, aSizeDumped);
   943 			}
   944 		default: return KErrArgument;
   945 		}
   946 	}
   947 
   948 /**
   949  * Wrapper method around the flash erase block fundtion to determine if the erase was succesful.
   950  * If the erase was not succesful we can't continue as we cannot write.
   951  * @param aBlockOffset Block to erase
   952  * @return One of the OS wide codes
   953  */
   954 TInt SCMonitor::EraseFlashBlock(const SCMCrashBlockEntry& aBlock)
   955 	{	
   956 	iFlash->StartTransaction();
   957 	
   958 	TInt numAttempts = 0;
   959 	while(numAttempts < KFlashEraseAttempts)
   960 		{
   961 		iFlash->SetWritePos(aBlock.iBlockOffset);
   962 		iFlash->EraseFlashBlock(aBlock.iBlockOffset);
   963 		
   964 		//we will read the flash to make sure that it set the block to all 1's (well not all, just the start)
   965 		TBuf8<sizeof(TUint32)> buf;
   966 		buf.SetLength(sizeof(TUint32));
   967 				
   968 		iFlash->SetReadPos(aBlock.iBlockOffset);
   969 		iFlash->Read(buf);
   970 		
   971 		volatile TUint32* result = (TUint32*)buf.Ptr();
   972 		if(*result == 0xFFFFFFFF)
   973 			{			
   974 			__KTRACE_OPT(KALWAYS, Kern::Printf("Erase of block [0x%X] succesful after [%d] attempts", aBlock.iBlockOffset, numAttempts+1))			
   975 			iFlash->EndTransaction();			
   976 			return KErrNone;
   977 			}
   978 		
   979 		numAttempts++;
   980 		
   981 		//Sometimes a write to the block helps the next erase
   982 		TUint32 bytesWritten = 0;		
   983 		while(bytesWritten < aBlock.iBlockSize)
   984 			{
   985 			TBuf8<sizeof(TUint8)> num;
   986 			num.Append(0x0);
   987 			iFlash->Write(num);
   988 			bytesWritten++;
   989 			}
   990 		}		
   991 
   992 	__KTRACE_OPT(KALWAYS, Kern::Printf("After %d attempts, we were unable to erase the flash block at [0x%X]. This could be because "
   993 			"the driver is defective or because the flash has gone past its lifetime. Whatever it is though, "
   994 			"we cannot continue.", KFlashEraseAttempts, aBlock.iBlockOffset));
   995 	
   996 	iFlash->EndTransaction();
   997 	return KErrAbort;
   998 	}
   999 
  1000 /**
  1001  * This erases each block in the flash partition
  1002  * @return One of the system wide codes
  1003  */
  1004 TInt SCMonitor::EraseEntireFlashPartition()
  1005 	{
  1006 	if(iMultiCrashInfo)
  1007 		{
  1008 		iMultiCrashInfo->Reset();
  1009 		
  1010 		SCMCrashBlockEntry* block = iMultiCrashInfo->GetNextBlock();
  1011 		while(block)
  1012 			{
  1013 			TInt err = EraseFlashBlock(*block);
  1014 			if(KErrNone != err)
  1015 				{
  1016 				return err;
  1017 				}
  1018 			
  1019 			block = iMultiCrashInfo->GetNextBlock();
  1020 			}
  1021 		
  1022 		return KErrNone;
  1023 		}
  1024 	
  1025 	CLTRACE("SCMonitor::EraseEntireFlashPartition() -- No Flash MAP available, trying to use the raw driver to delete.");
  1026 	TheSCMonitor.iFlash->EraseLogArea();
  1027 	
  1028 	return KErrNone;
  1029 	}
  1030 
  1031 //eof scmonitor.cpp
  1032