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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32\debug\crashMonitor\src\scmonitor.cpp
15 // Core dump server - Kernel side crash monitor
24 #include <scmonitor.h>
25 #include <kernel/monitor.h>
27 #include <drivers/crashflash.h>
28 #include <kernel/klib.h>
29 #include <crashlogwalker.h>
30 #include <scmconfigitem.h>
32 #include "scmdatasave.h"
34 GLDEF_D SCMonitor TheSCMonitor; //global definition of SCMonitor
36 //keep things 4 byte aligned
37 const TInt KRestartType = SCMonitor::ESoftRestart;
42 SCMonitor::SCMonitor()
43 : iMultiCrashInfo(NULL)
47 SCMonitor::~SCMonitor()
49 delete iMultiCrashInfo;
53 Print data to the corresponding output channel. Derived from monitor
54 @param aDes the buffer containing the data
56 void SCMonitor::Print (const TDesC8& aDes )
58 //intended to do nothing
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
65 void SCMonitor::StableConstruction()
68 iDataSave = new SCMDataSave(this, TheSCMonitor.iFlash);
70 //Configuration object for use upon crash
71 iScmConfig = new SCMConfiguration();
72 TInt err = iScmConfig->SetDefaultConfig();
75 CLTRACE1("SCMonitor::StableConstruction - Unable to set default config err = %d", err);
79 #ifdef NO_MULTICRASHINFO
80 iMultiCrashInfo = NULL;
83 //We need to take a look at the flash map from variant_norflash_layout.h
84 iMultiCrashInfo = new SCMMultiCrashInfo();
86 TUint numberBlocks = KCrashLogSize / KCrashLogBlockSize;
87 for(TUint32 cnt = 0; cnt < numberBlocks; cnt++)
89 iMultiCrashInfo->AddBlock(new SCMCrashBlockEntry(cnt, cnt * KCrashLogBlockSize, KCrashLogBlockSize));
95 * Start a secondary DFC queue for the Flash and Init the flash in the variant(h4)
98 void StartSecondary (TAny* )
101 //InitFlash is implemented in the variant as it creates a variant
102 //specific derived CrashFlash
103 TheSCMonitor.InitFlash ( );
104 TheSCMonitor.StableConstruction();
108 * Global method to create a dfc queue
109 * @param Method to intialise the flash.
111 * @param Gets the address of the supervisor thread DFC queue
112 * @param TDfcQ priority number
113 * @return a DFC object
115 GLDEF_C TDfc StartSecondaryDfc(&StartSecondary, NULL, Kern::SvMsgQue(), KMaxDfcPriority-1);
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
122 GLDEF_C TInt KernelModuleEntry(TInt aReason)
124 if(aReason==KModuleEntryReasonVariantInit0)
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.
133 CLTRACE("Installing System Crash Monitor");
134 Monitor::RegisterMonitorImpl (&TheSCMonitor );
137 else if (aReason==KModuleEntryReasonExtensionInit0 )
141 else if (aReason==KModuleEntryReasonExtensionInit1 )
144 CLTRACE("Enqueing dfc to init crash flash for System Crash Monitor after all modules loaded");
145 StartSecondaryDfc.Enque ( );
152 Method to intialize the system crash monitor
153 @param aCategory the fault category type
154 @param aReason the reason for crash
157 TInt SCMonitor::Init2 (TAny* aCategory, TInt aReason )
160 __KTRACE_OPT(KALWAYS, Kern::Printf("\n\nSystem Crash Monitor Launched: To Analyse Crash Produced Use Core Dump Server\n"));
162 //Start logging the data:
163 //Need to lock kernel to access object containers (it technically is anyway, but flag isnt set)
165 DoCrash(aCategory, aReason);
168 __KTRACE_OPT(KALWAYS, Kern::Printf("System Crash Monitor Finished: Log Size = [%d]\n", iDataSave->iCrashInf.iLogSize));
174 * This is responsible for setting up any structures required for processing of the crash
175 * @param aCategory the fault category type
178 void SCMonitor::DoCrash(TAny* aCategory, TInt aReason )
181 TInt dbgMask = Kern::SuperPage().iDebugMask[0];
183 // if we are writing to the comm port then we need to turn off other debug messages
184 if( iDataSave->GetWriteSelect() == SCMDataSave::EWriteComm)
186 Kern::SuperPage().iDebugMask[0] = 0;
191 CLTRACE("\tNULL category retrieved and returning");
192 TheSCMonitor.iFlash->EndTransaction();
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 );
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
210 CLTRACE2("SCMonitor::DoCrash next crash will be written at blocknumber = %d offset %d"
211 , block.iBlockNumber, block.iBlockOffset);
215 CLTRACE1("SCMonitor::DoCrash Failed to find a valid block to write to, can not continue. err = [%d]", err);
219 TUint crashId = block.iBlockNumber;
220 iDataSave->iWriter->ResetBytesWritten();
222 //Write the crash (1st pass is to gather header data)
223 TInt spaceRequired = ProcessCrash(block, crashId, EFalse);
225 // now do the real write
226 // prepare flash for data
227 TheSCMonitor.iFlash->StartTransaction();
228 TheSCMonitor.iFlash->SetWritePos(block.iBlockOffset);
230 //write the crash this time
231 ProcessCrash(block, crashId, ETrue);
233 TheSCMonitor.iFlash->EndTransaction();
235 // restore debug mask
236 Kern::SuperPage().iDebugMask[0] = dbgMask;
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
245 TInt SCMonitor::GetNextCrashStartPoint(SCMCrashBlockEntry& aBlockEntry)
249 //First thing is to try and read the config
250 TBool configFound = (iDataSave->ReadConfig(*iScmConfig) == KErrNone);
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
259 iMultiCrashInfo->Reset();
260 SCMCrashBlockEntry* block = iMultiCrashInfo->GetNextBlock();
261 TBool blockFound = EFalse;
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;
269 CLTRACE1("SCMonitor::GetNextCrashStartPoint Processing block number %d", block->iBlockNumber );
271 //If we have already found our block, we should erase subsequent ones for use
274 TInt err = EraseFlashBlock(*block);
280 block = iMultiCrashInfo->GetNextBlock();
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)
287 //special condition if we have a config
288 TUint startPos = block->iBlockOffset;
290 if(configFound && block->iBlockOffset == 0)
292 startPos+=iScmConfig->GetSize();
294 //must align to flash for read
295 skipBytes = startPos % KFlashAlignment;
296 startPos -= skipBytes;
299 // try and read an info header at these flash coords
300 TBuf8<TCrashInfoHeader::KSCMCrashInfoMaxSize + KFlashAlignment> buf;
301 buf.SetLength(TCrashInfoHeader::KSCMCrashInfoMaxSize + KFlashAlignment);
303 CLTRACE1("(SCMonitor::GetNextCrashStartPoint) reading at offset %d", block->iBlockOffset);
305 TheSCMonitor.iFlash->SetReadPos(startPos);
306 TheSCMonitor.iFlash->Read(buf);
308 // create the buffer applying the offset of bytes skipped
309 TByteStreamReader reader(const_cast<TUint8*> (buf.Ptr() + skipBytes));
311 TCrashInfoHeader header;
312 TInt err = header.Deserialize(reader);
314 if(err == KErrCorrupt)
316 CLTRACE2("(SCMonitor::GetNextCrashStartPoint) Found empty block blocknumber %d blockoffset = %d"
317 , block->iBlockNumber, block->iBlockOffset);
320 aBlockEntry = *block;
322 continue; //Dont get next block, as next run will erase this current block for use
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);
331 block = iMultiCrashInfo->GetNextBlock();
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
346 // no multi crash info supplied - use default first block settings
347 TInt err = EraseEntireFlashPartition();
350 CLTRACE1("Unable to delete area required to log to flash. Aborting. Error - [%d]", err);
354 aBlockEntry = SCMCrashBlockEntry(0,0,0);
359 * Handles the processing of the crash
360 * @return The size of the crash log (including header) that has been/will be written
362 TInt SCMonitor::ProcessCrash(const SCMCrashBlockEntry& aBlock, TUint aCrashId, TBool aCommit)
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);
368 // reset writer for start of each crash
369 iDataSave->iWriter->ResetBytesWritten();
375 iDataSave->iWriter->EnablePhysicalWriting();
380 logLevel = KDEBUGGER;
382 logLevel = KALWAYS; //Doesnt matter, KTRACE OPT is empty for rel builds
383 if(logLevel != KALWAYS)
385 //This is to avoid warning
389 iDataSave->iWriter->DisablePhysicalWriting();
392 iDataSave->SetByteCount(aBlock.iBlockOffset);
393 if(aBlock.iBlockOffset == 0 && aBlock.iBlockNumber == 0)
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);
399 //Config is not part of crash so reset bytes written
400 iDataSave->SetCrashStartingPoint(iDataSave->iWriter->GetBytesWritten());
404 iDataSave->SetCrashStartingPoint(aBlock.iBlockOffset);
407 iDataSave->iWriter->ResetBytesWritten();
410 TUint sizeOfObjectDumped = 0;
412 TInt err = iDataSave->LogCrashHeader(iFaultCategory, iFaultReason, aCrashId, sizeOfObjectDumped);
415 CLTRACE("System Crash Monitor: Failed to create crash info header - (TCrashInfo)");
419 logSize += sizeOfObjectDumped;
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();
425 //Always want the crash context
426 iDataSave->iHdr.iCTFullRegOffset = logSize + iDataSave->GetCrashStartingPoint();
428 err = iDataSave->LogCPURegisters(sizeOfObjectDumped);
431 CLTRACE1("\tError logging full registers = %d", err);
434 logSize += sizeOfObjectDumped;
436 CLTRACE("\tAbout to enter processing loop");
437 SCMDataSave::TDataToDump dump;
441 //now we get each item by priority from the configuration
442 TConfigItem* configItem = iScmConfig->GetNextItem();
450 CLTRACE1("\nLooking at item type [%d]", configItem->GetDataType());
451 if(configItem->GetSpaceRequired() > iDataSave->SpaceRemaining())
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()));
458 CLTRACE1("Will require [%d] bytes for this item", configItem->GetSpaceRequired());
461 // only interested in logging items with priority > 0
462 if( configItem->GetPriority() <= 0)
464 CLTRACE1("\tIgnored config item type %d priority 0", configItem->GetDataType());
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;
472 //now we check the type of data we wish to dump
473 switch(configItem->GetDataType())
475 case TConfigItem::ECrashedThreadMetaData:
477 __KTRACE_OPT(logLevel, Kern::Printf("\tDoing: ECrashedThreadMetaData at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
479 err = LogThreadMetaData(SCMDataSave::EThreadSpecific, sizeOfObjectDumped);
480 offsetPointer = &(iDataSave->iHdr.iCTMetaOffset);
484 case TConfigItem::EThreadsMetaData:
486 __KTRACE_OPT(logLevel, Kern::Printf("\tDoing: EThreadsMetaData at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
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);
495 case TConfigItem::ECrashedProcessMetaData:
497 __KTRACE_OPT(logLevel, Kern::Printf("\tDoing: ECrashedProcessMetaData at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
499 err = LogProcessMetaData(SCMDataSave::EProcessSpecific, sizeOfObjectDumped);
500 offsetPointer = &(iDataSave->iHdr.iCPMetaOffset);
504 case TConfigItem::EProcessMetaData:
506 __KTRACE_OPT(logLevel, Kern::Printf("\tDoing: EProcessMetaData at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
508 err = LogProcessMetaData(SCMDataSave::ESystemWide, sizeOfObjectDumped);
509 offsetPointer = &(iDataSave->iHdr.iPLstOffset);
513 case TConfigItem::ECrashedProcessUsrStacks:
515 __KTRACE_OPT(logLevel, Kern::Printf("\tDoing: ECrashedProcessUsrStacks at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
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);
527 case TConfigItem::EThreadsUsrStack:
529 __KTRACE_OPT(logLevel, Kern::Printf("\tDoing: EThreadsUsrStack at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
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;
537 err = LogObjectContainers(EThread, SCMDataSave::ESystemWide, dump, sizeOfObjectDumped);
538 offsetPointer = &(iDataSave->iHdr.iSysSvrStkOffset);
542 case TConfigItem::ECrashedProcessSvrStacks:
544 __KTRACE_OPT(logLevel, Kern::Printf("\tDoing: ECrashedProcessSvrStacks at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
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;
552 err = LogObjectContainers(EThread, SCMDataSave::EProcessSpecific, dump, sizeOfObjectDumped);
553 offsetPointer = &(iDataSave->iHdr.iCTSvrStkOffset);
557 case TConfigItem::EThreadsSvrStack:
559 __KTRACE_OPT(logLevel, Kern::Printf("\tDoing: EThreadsSvrStack at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
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;
567 err = LogObjectContainers(EThread, SCMDataSave::ESystemWide, dump, sizeOfObjectDumped);
568 offsetPointer = &(iDataSave->iHdr.iSysSvrStkOffset);
572 case TConfigItem::EThreadsUsrRegisters:
574 __KTRACE_OPT(logLevel, Kern::Printf("\tDoing: EThreadsUsrRegisters at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
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;
582 err = LogObjectContainers(EThread, SCMDataSave::ESystemWide, dump, sizeOfObjectDumped);
583 offsetPointer = &(iDataSave->iHdr.iSysUsrRegOffset);
587 case TConfigItem::EThreadsSvrRegisters:
589 __KTRACE_OPT(logLevel, Kern::Printf("\tDoing: EThreadsSvrRegisters at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
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;
597 err = LogObjectContainers(EThread, SCMDataSave::ESystemWide, dump, sizeOfObjectDumped);
598 offsetPointer = &(iDataSave->iHdr.iSysSvrRegOffset);
602 case TConfigItem::EExceptionStacks:
604 __KTRACE_OPT(logLevel, Kern::Printf("\tDoing: EExceptionStacks at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
606 err = iDataSave->LogExceptionStacks(sizeOfObjectDumped);
607 offsetPointer = &(iDataSave->iHdr.iExcStkOffset);
611 case TConfigItem::ECrashedProcessCodeSegs:
613 __KTRACE_OPT(logLevel, Kern::Printf("\tDoing: ECrashedProcessCodeSegs at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
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;
621 err = LogObjectContainers(EProcess, SCMDataSave::EProcessSpecific, dump, sizeOfObjectDumped);
622 offsetPointer = &(iDataSave->iHdr.iCPCodeSegOffset);
626 case TConfigItem::EProcessCodeSegs:
628 __KTRACE_OPT(logLevel, Kern::Printf("\tDoing: EProcessCodeSegs at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
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);
640 case TConfigItem::ETraceData:
642 __KTRACE_OPT(logLevel, Kern::Printf("\tDoing: ETraceData at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
644 err = iDataSave->LogTraceBuffer(configItem->GetSizeToDump(), sizeOfObjectDumped);
645 offsetPointer = &(iDataSave->iHdr.iTraceOffset);
649 case TConfigItem::ELocks:
651 __KTRACE_OPT(logLevel, Kern::Printf("\tDoing: ELocks at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
653 err = iDataSave->LogLocks(sizeOfObjectDumped);
654 offsetPointer = &(iDataSave->iHdr.iScmLocksOffset);
658 case TConfigItem::EKernelHeap:
660 __KTRACE_OPT(logLevel, Kern::Printf("\tDoing: EKernelHeap at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
662 err = iDataSave->LogKernelHeap(sizeOfObjectDumped);
663 offsetPointer = &(iDataSave->iHdr.iKernelHeapOffset);
667 case TConfigItem::EVariantSpecificData:
669 __KTRACE_OPT(logLevel, Kern::Printf("\tDoing: EVariantSpecificData at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
671 err = iDataSave->LogVariantSpecificData(sizeOfObjectDumped);
672 offsetPointer = &(iDataSave->iHdr.iVarSpecInfOffset);
676 case TConfigItem::ERomInfo:
678 __KTRACE_OPT(logLevel, Kern::Printf("\tDoing: ERomInfo at [%d] offset from [%d]", iDataSave->iWriter->GetBytesWritten(), iDataSave->GetCrashStartingPoint()));
680 err = iDataSave->LogRomInfo(sizeOfObjectDumped);
681 offsetPointer = &(iDataSave->iHdr.iRomInfoOffset);
685 //unknown configuration type - something bad is going on
691 __KTRACE_OPT(logLevel, Kern::Printf("\tError logging data: [%d] Type = [%d]", err, aBlock.iBlockOffset));
695 //Set the space required so next time around we will know in advance how much space we need
696 configItem->SetSpaceRequired(sizeOfObjectDumped);
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
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())
705 //now, we must record where in the crash log this item will be dumped
706 *offsetPointer = absoluteLogPos;
707 logSize += sizeOfObjectDumped;
711 iDataSave->iCrashInf.iLogSize = logSize;
712 iDataSave->iWriter->FlushCache();
714 return iDataSave->iCrashInf.iLogSize;
718 * Logs the meta data for processes
719 * @param aCurrentProcess - scope to dump
720 * @return one of the OS wide codes
722 TInt SCMonitor::LogProcessMetaData(SCMDataSave::TDumpScope aScope, TUint& aSizeDumped) const
726 SCMDataSave::TDataToDump dump;
727 dump.iMetaData = ETrue;
729 return LogObjectContainers(EProcess, aScope, dump, aSizeDumped);
734 * @param aCurrentThread - to only do the current (crashed thread) or to do all the others
735 * @return one of the OS wide codes
737 TInt SCMonitor::LogThreadMetaData(SCMDataSave::TDumpScope aScope, TUint& aSizeDumped) const
741 SCMDataSave::TDataToDump dump;
742 dump.iMetaData = ETrue;
744 return LogObjectContainers(EThread, aScope, dump, aSizeDumped);
748 * Generic method that looks at all kernel objects of 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
755 TInt SCMonitor::LogObjectContainers(TObjectType aObjectType, SCMDataSave::TDumpScope aDumpScope, const SCMDataSave::TDataToDump& aDataToDump, TUint& aSizeDumped) const
759 if(aObjectType >= ENumObjectTypes)
764 //Get the object container for the given object type
765 DObjectCon* objectContainer = Kern::Containers()[aObjectType];
766 if(objectContainer == NULL)
768 CLTRACE("tFailed to get object container");
772 //Must check the mutex on this is ok otherwise the data will be in an inconsistent state
773 if(objectContainer->iMutex->iHoldCount)
775 CLTRACE("\tObject Container is in an inconsistant state");
779 TInt numObjects = objectContainer->Count();
782 for(TInt cnt = 0; cnt< numObjects; cnt ++)
784 DObject* object = (*objectContainer)[cnt];
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)
791 case SCMDataSave::EThreadSpecific :
793 //if we are interested in the current thread and this is not it, continue
794 if(((DThread*)object) != &Kern::CurrentThread())
798 case SCMDataSave::EProcessSpecific :
800 //if we are interested in the current proc and this is not it, continue
801 if(((DThread*)object)->iOwningProcess != &Kern::CurrentProcess())
805 case SCMDataSave::ESystemWide :
810 else if(aObjectType == EProcess)
814 case SCMDataSave::EProcessSpecific :
816 if((DProcess*)object != &Kern::CurrentProcess())
820 case SCMDataSave::EThreadSpecific : //thread specific process doesnt make sense
822 case SCMDataSave::ESystemWide :
828 //Now we look at the data we have been asked to dump
829 if(aDataToDump.iMetaData)
832 err = HelpDumpMetaData(object, aObjectType, dumped);
835 CLTRACE1("Failed to meta data: [%d]", err);
838 aSizeDumped += dumped;
841 if(aDataToDump.iCodeSegs)
843 if(aObjectType != EProcess)
849 err = iDataSave->LogCodeSegments((DProcess*)object, dumped);
852 CLTRACE1("Failed to log code segments: [%d]", err);
855 aSizeDumped += dumped;
858 if(aDataToDump.iStk != SCMDataSave::EStackTypeNone)
861 err = HelpDumpStacks(object, aObjectType, dumped, aDataToDump.iStk);
864 CLTRACE1("Failed to log stacks: [%d]", err);
867 aSizeDumped += dumped;
870 if(aDataToDump.iReg != SCMDataSave::ERegSetNone)
872 if(aObjectType != EThread)
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
880 CLTRACE1("Failed to log registers: [%d]", err);
883 aSizeDumped += dumped;
891 * Helper method for dumping stacks. Looks to see what type of stack we want and then calls
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
898 * @see SCMDataSave::TStackType
899 * @return One of the system wide codes
901 TInt SCMonitor::HelpDumpStacks(DObject* aObject, TObjectType aObjectType, TUint& aSizeDumped, SCMDataSave::TStackType aStkType) const
904 if(aObjectType != EThread)
911 case SCMDataSave::EUsrStack:
913 return iDataSave->LogThreadUserStack((DThread*)aObject, ETrue, aSizeDumped);
915 case SCMDataSave::ESvrStack:
917 return iDataSave->LogThreadSupervisorStack((DThread*)aObject, ETrue, aSizeDumped);
919 default: return KErrArgument;
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
930 TInt SCMonitor::HelpDumpMetaData(DObject* aObject, TObjectType aObjectType, TUint& aSizeDumped) const
938 return iDataSave->LogThreadData((DThread*)aObject, aSizeDumped);
942 return iDataSave->LogProcessData((DProcess*)aObject, aSizeDumped);
944 default: return KErrArgument;
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
954 TInt SCMonitor::EraseFlashBlock(const SCMCrashBlockEntry& aBlock)
956 iFlash->StartTransaction();
958 TInt numAttempts = 0;
959 while(numAttempts < KFlashEraseAttempts)
961 iFlash->SetWritePos(aBlock.iBlockOffset);
962 iFlash->EraseFlashBlock(aBlock.iBlockOffset);
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));
968 iFlash->SetReadPos(aBlock.iBlockOffset);
971 volatile TUint32* result = (TUint32*)buf.Ptr();
972 if(*result == 0xFFFFFFFF)
974 __KTRACE_OPT(KALWAYS, Kern::Printf("Erase of block [0x%X] succesful after [%d] attempts", aBlock.iBlockOffset, numAttempts+1))
975 iFlash->EndTransaction();
981 //Sometimes a write to the block helps the next erase
982 TUint32 bytesWritten = 0;
983 while(bytesWritten < aBlock.iBlockSize)
985 TBuf8<sizeof(TUint8)> num;
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));
996 iFlash->EndTransaction();
1001 * This erases each block in the flash partition
1002 * @return One of the system wide codes
1004 TInt SCMonitor::EraseEntireFlashPartition()
1008 iMultiCrashInfo->Reset();
1010 SCMCrashBlockEntry* block = iMultiCrashInfo->GetNextBlock();
1013 TInt err = EraseFlashBlock(*block);
1019 block = iMultiCrashInfo->GetNextBlock();
1025 CLTRACE("SCMonitor::EraseEntireFlashPartition() -- No Flash MAP available, trying to use the raw driver to delete.");
1026 TheSCMonitor.iFlash->EraseLogArea();