os/kernelhwsrv/kerneltest/e32utils/crashread/crashread.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32utils/crashread/crashread.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,347 @@
     1.4 +// Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include <e32std.h>
    1.20 +#include <e32std_private.h>
    1.21 +#include <f32file.h>
    1.22 +#include <d32locd.h>
    1.23 +#include <e32cons.h>
    1.24 +#include "crashflash.h"
    1.25 +#include <partitions.h>
    1.26 +#include <ftlcontrolio.h>
    1.27 +
    1.28 +#ifdef _DEBUG
    1.29 +#define TRACE(a) RDebug::Print(a); PrintLine(a)
    1.30 +#define TRACE1(a,b) RDebug::Print(a,b); PrintLine(a,b)
    1.31 +#define TRACE2(a,b,c) RDebug::Print(a,b,c); PrintLine(a,b,c)
    1.32 +#define TRACE5(a,b,c,d,e,f) RDebug::Print(a,b,c,d,e,f); PrintLine(a,b,c,d,e,f)
    1.33 +#else
    1.34 +#define TRACE(a) 
    1.35 +#define TRACE1(a,b) 
    1.36 +#define TRACE2(a,b,c) 
    1.37 +#define TRACE5(a,b,c,d,e,f)
    1.38 +#endif
    1.39 +
    1.40 +#ifndef _CRASHLOG_COMPR
    1.41 +_LIT(KCrashLogFileName, "?:\\crashlog.txt");
    1.42 +#else
    1.43 +_LIT(KCrashLogCompFileName, "?:\\crashlog.gz");
    1.44 +_LIT(KCrashLogCompTruncatedFileName, "?:\\crashlog_truncated.gz");
    1.45 +#endif //_CRASHLOG_COMPR
    1.46 +
    1.47 +_LIT8(KCrashLogSignatureStomp, "\x00\x00\x00\x00");
    1.48 +
    1.49 +CConsoleBase* console = 0;
    1.50 +
    1.51 +RLocalDrive gLd;
    1.52 +TLocalDriveCapsV4 gCaps;
    1.53 +TPckg<TLocalDriveCapsV4> gCapsBuf(gCaps);
    1.54 +
    1.55 +#ifdef _DEBUG
    1.56 +LOCAL_C void CheckConsoleCreated()
    1.57 +	{
    1.58 +	if(!console)
    1.59 +		{
    1.60 +		TRAPD(r, console = Console::NewL(_L("crashread"), 
    1.61 +			TSize(KConsFullScreen,KConsFullScreen)));
    1.62 +		__ASSERT_ALWAYS(r == KErrNone, User::Panic(_L("Could not create console"), 1));
    1.63 +		}
    1.64 +	}
    1.65 +
    1.66 +LOCAL_C void PrintLine(TRefByValue<const TDesC> aFmt,...)
    1.67 +	{
    1.68 +    // Print to a console screen.
    1.69 +    VA_LIST list;
    1.70 +    VA_START(list, aFmt);
    1.71 +    TBuf<0x100> aBuf;
    1.72 +    aBuf.AppendFormatList(aFmt, list);
    1.73 +    CheckConsoleCreated();
    1.74 +    console->Write(aBuf);
    1.75 +	console->Write(_L("\n\r"));
    1.76 +	}
    1.77 +#endif
    1.78 +
    1.79 +/** Read the signature from the flash and verify it is correct.
    1.80 +	@return ETrue when signature found, EFalse otherwise
    1.81 +*/
    1.82 +LOCAL_C TBool SignatureExistsL()
    1.83 +	{
    1.84 +	TBuf8<KCrashLogSignatureBytes> buf(0);
    1.85 +	User::LeaveIfError(gLd.Read(KCrashLogSizeFieldBytes,KCrashLogSignatureBytes,buf));
    1.86 +
    1.87 +	if(buf.Compare(KCrashLogSignature) == 0)
    1.88 +		{
    1.89 +		return ETrue;
    1.90 +		}
    1.91 +
    1.92 +	return EFalse;
    1.93 +	}
    1.94 +
    1.95 +LOCAL_C TInt LogSizeL()
    1.96 +	{
    1.97 +	TBuf8<KCrashLogSizeFieldBytes> buf(0);
    1.98 +	User::LeaveIfError(gLd.Read(0,KCrashLogSizeFieldBytes,buf));
    1.99 +	TInt size = *((TUint*)(buf.Ptr()));
   1.100 +	size -= (KCrashLogHeaderSize);
   1.101 +	return size;
   1.102 +	}
   1.103 +
   1.104 +#ifdef _CRASHLOG_COMPR	
   1.105 +/** Read the log flags from the flash.  Flags located after the log size and uncompressed size
   1.106 +	@return The log flags byte
   1.107 +*/
   1.108 +LOCAL_C TUint32 LogFlagsL()
   1.109 +	{
   1.110 +	TBuf8<KCrashLogFlagsFieldBytes> buf(0);
   1.111 +	User::LeaveIfError(gLd.Read(KCrashLogSizeFieldBytes+KCrashLogUncompSizeFieldBytes+KCrashLogSignatureBytes,
   1.112 +						KCrashLogFlagsFieldBytes,buf));
   1.113 +	return *((TUint32*)buf.Ptr());
   1.114 +	}
   1.115 +#endif //_CRASHLOG_COMPR
   1.116 +	
   1.117 +LOCAL_C TInt InvalidateSignature()
   1.118 +	{
   1.119 +	//On Nand we erase the block.
   1.120 +	if(gCaps.iType == EMediaNANDFlash)
   1.121 +		{
   1.122 +		return gLd.Format(0,gCaps.iNumBytesMain * gCaps.iNumPagesPerBlock);
   1.123 +		}
   1.124 +	//On Nor we just stomp on the first 4 bytes of the signature
   1.125 +	return gLd.Write(KCrashLogSizeFieldBytes,KCrashLogSignatureStomp);
   1.126 +	}
   1.127 +
   1.128 +/**
   1.129 +@return KErrNone if no read errors, otherwise the last read error. 
   1.130 +@leave if other errors occur.	
   1.131 +@param aFileName Where the log wll be copied to
   1.132 +@param aStartPosition Where to begin reads within the flash section.
   1.133 +@param aLogSize The total amount to read.
   1.134 +*/
   1.135 +TInt CopyToFileL(const TDesC& aFileName, const TInt aStartPosition, const TInt aLogSize)
   1.136 +	{
   1.137 +	// Connect to f32 and write out the file
   1.138 +	RFs fs;
   1.139 +	RFile file;
   1.140 +	User::LeaveIfError(fs.Connect());
   1.141 +	CleanupClosePushL(fs);
   1.142 +	User::LeaveIfError(file.Replace(fs, aFileName, EFileWrite));
   1.143 +	CleanupClosePushL(file);
   1.144 +
   1.145 +	//create buffer
   1.146 +	const TInt KBufferSize=32*1024;
   1.147 +	HBufC8* buf = HBufC8::NewLC(KBufferSize);
   1.148 +	TPtr8 ptr = buf->Des();
   1.149 +
   1.150 +	TInt readError = KErrNone;
   1.151 +	for(TInt offset=0; offset<aLogSize; offset+=KBufferSize)
   1.152 +		{
   1.153 +		//don't read beyond end on final iteration.
   1.154 +		const TInt readLength = Min(KBufferSize, aLogSize-offset);
   1.155 +
   1.156 +		ptr.SetLength(0);
   1.157 +		TInt r = gLd.Read(aStartPosition+offset,readLength,ptr);
   1.158 +
   1.159 +		// in case of error store it, but attempt to continue.
   1.160 +		if (r!=KErrNone)
   1.161 +			{
   1.162 +			readError=r;
   1.163 +			}
   1.164 +
   1.165 +		User::LeaveIfError(file.Write(offset, ptr));
   1.166 +		}
   1.167 +
   1.168 +	User::LeaveIfError(file.Flush());
   1.169 +	CleanupStack::PopAndDestroy(buf);
   1.170 +	CleanupStack::PopAndDestroy(&file);
   1.171 +	CleanupStack::PopAndDestroy(&fs);
   1.172 +	return readError;
   1.173 +	}
   1.174 +
   1.175 +
   1.176 +LOCAL_C TInt MainL()
   1.177 +	{
   1.178 +	// check if command line argument is 'reset'
   1.179 +	RBuf cl;
   1.180 +	cl.CreateL(User::CommandLineLength());
   1.181 +	cl.CleanupClosePushL();
   1.182 +	User::CommandLine(cl);
   1.183 +	TBool reset = (cl==_L("reset"));
   1.184 +	CleanupStack::PopAndDestroy();
   1.185 +	
   1.186 +	TBool changed;
   1.187 +	TInt r = 0;
   1.188 +	TInt i=0;
   1.189 +	// 1) Find a crash log partition.
   1.190 +	for(; i<KMaxLocalDrives; i++)
   1.191 +		{
   1.192 +		r = gLd.Connect(i,changed);
   1.193 +		if(r == KErrNone)
   1.194 +			{
   1.195 +			r = gLd.Caps(gCapsBuf);
   1.196 +			if(r != KErrNone)
   1.197 +				{
   1.198 +				//TRACE1(_L("Could not retrieve gCaps for drive: %d.  Skipping to next..."),i);
   1.199 +				continue;
   1.200 +				}
   1.201 +			if(gCaps.iPartitionType == (TUint16)KPartitionTypeSymbianCrashLog)
   1.202 +				{
   1.203 +				TRACE1(_L("Found Symbian crash log partition on drive: %d"),i);
   1.204 +				CleanupClosePushL(gLd);
   1.205 +				// 1) See if there is an existing crash log
   1.206 +				TBool exists = SignatureExistsL();
   1.207 +				if(!exists)
   1.208 +					{
   1.209 +					TRACE(_L("Did not find an existing crash log signature on this crash log partition..."));
   1.210 +					//There may be a second crash log partition. (nor or nand
   1.211 +					//depending on ordering in variantmediadef.h).  So we continue searching
   1.212 +					CleanupStack::PopAndDestroy(&gLd);
   1.213 +					continue; 
   1.214 +					}
   1.215 +				TRACE1(_L("Found a crash log signature on drive: %d."),i);
   1.216 +				//We've found a crash log partition with a signature on it.
   1.217 +				break;
   1.218 +				}
   1.219 +			else
   1.220 +				{
   1.221 +				//TRACE2(_L("Partition type on drive: %d is %d"),i,gCaps.iPartitionType);
   1.222 +				}
   1.223 +			}
   1.224 +		}
   1.225 +	if(i == KMaxLocalDrives)
   1.226 +		{
   1.227 +		TRACE(_L("No crash log partition found with valid crash log signature found.  Exiting..."));
   1.228 +		User::Leave(KErrNotFound);
   1.229 +		}
   1.230 +
   1.231 +	// If we're doing a reset, don't try to read the crash log, just skip to stomping the signature
   1.232 +	if(!reset)
   1.233 +		{
   1.234 +		TUint8 systemDriveChar = (TUint8) RFs::GetSystemDriveChar();
   1.235 +#ifndef _CRASHLOG_COMPR
   1.236 +		// Determine size of crash log and copy to file.
   1.237 +		TInt logSize = LogSizeL();
   1.238 +		TRACE1(_L("Reading crash log of %d bytes..."), logSize);
   1.239 +		TBuf<sizeof(KCrashLogFileName)> crashLogFileName(KCrashLogFileName);
   1.240 +		crashLogFileName[0] = systemDriveChar;
   1.241 +		r = CopyToFileL(crashLogFileName, KCrashLogSizeFieldBytes+KCrashLogSignatureBytes, logSize);
   1.242 +
   1.243 +		if (r==KErrNone)
   1.244 +			{
   1.245 +			TRACE1(_L("Crash log successfully written to: %S."), &crashLogFileName);
   1.246 +			}
   1.247 +		else
   1.248 +			{
   1.249 +			TRACE1(_L("Crash log written to %S but errors were encountered when reading, it may be incomplete or corrupt."), &crashLogFileName);
   1.250 +			}
   1.251 +
   1.252 +#else
   1.253 +		// 2) 	Read crash log header to get the compressed and uncompressed size of the log
   1.254 +		//		also need to read the flags to determine if the log had to be truncated and
   1.255 +		//		if the expected log format is found
   1.256 +		const TUint32 logFlags = LogFlagsL();
   1.257 +		
   1.258 +		// Extract byte offset from the end of the header to the start of the log data
   1.259 +		const TInt logOff = logFlags>>KCrashLogFlagOffShift;
   1.260 +		
   1.261 +		// Work out if the log had to be truncated
   1.262 +		const TInt truncated = logFlags&KCrashLogFlagTruncated;			
   1.263 +		
   1.264 +		// Check the crashlog type flag is that expected - here we can only cope with GZIP compatible logs
   1.265 +		if ((logFlags & (0xffffffff>>(32-KCrashLogFlagTypeBits))) != KCrashLogFlagGzip)
   1.266 +			{// wrong log type so can't extract it
   1.267 +			TRACE(_L("Crash Log data is stored in an incompatible data format so can't be read"));
   1.268 +			}
   1.269 +		else
   1.270 +			{
   1.271 +			// 2) Read the log data
   1.272 +			const TInt logSize = LogSizeL()-logOff; // don't include any offset bytes	
   1.273 +			TRACE1(_L("Reading compressed crash log of %d bytes..."), logSize);
   1.274 +
   1.275 +			
   1.276 +			TRACE1(_L("Writing compressed crash log to file..."), logSize);
   1.277 +			RBuf crashLogCompFileName;
   1.278 +			if (!truncated)
   1.279 +				{
   1.280 +				crashLogCompFileName.CreateL(KCrashLogCompFileName);
   1.281 +				}
   1.282 +			else
   1.283 +				{
   1.284 +				crashLogCompFileName.CreateL(KCrashLogCompTruncatedFileName);
   1.285 +				}
   1.286 +			crashLogCompFileName.CleanupClosePushL();
   1.287 +
   1.288 +			crashLogCompFileName[0] = systemDriveChar;
   1.289 +			r = CopyToFileL(crashLogCompFileName, KCrashLogHeaderSize+logOff, logSize);
   1.290 +				
   1.291 +			if (r==KErrNone)
   1.292 +				{
   1.293 +				if (!truncated)
   1.294 +					{
   1.295 +					TRACE1(_L("Crash log successfully written to: %S."), &crashLogCompFileName);
   1.296 +					}
   1.297 +				else
   1.298 +					{
   1.299 +					TRACE(_L("Crash log was truncated, some log data has been lost"));
   1.300 +					TRACE1(_L("Crash log successfully written to: %S."), &crashLogCompFileName);
   1.301 +					}						
   1.302 +				}
   1.303 +			else
   1.304 +				{
   1.305 +				if(!truncated)
   1.306 +					{
   1.307 +					TRACE1(_L("Crash log written to %S but errors were encountered when reading, it may be incomplete or corrupt."), &crashLogCompFileName);
   1.308 +					}
   1.309 +				else
   1.310 +					{
   1.311 +					TRACE1(_L("Crash log written to %S but errors were encountered when reading, it may be incomplete or corrupt."), &crashLogCompFileName);
   1.312 +					}
   1.313 +				}
   1.314 +			CleanupStack::PopAndDestroy(&crashLogCompFileName);
   1.315 +			}
   1.316 +#endif //_CRASHLOG_COMPR			
   1.317 +		}
   1.318 +
   1.319 +	// 5) Stomp on the signature to mark it eligible to be overwritten
   1.320 +	TRACE(_L("Overwriting existing signature to indicate crash log has been read..."));
   1.321 +	User::LeaveIfError(InvalidateSignature());
   1.322 +
   1.323 +	CleanupStack::PopAndDestroy(&gLd);
   1.324 +
   1.325 +	if (r==KErrNone)
   1.326 +		{
   1.327 +		TRACE(_L("Crash reader finished successfully."));
   1.328 +		}
   1.329 +	else
   1.330 +		{
   1.331 +		TRACE(_L("Crash reader finished but with errors."));
   1.332 +		}
   1.333 +	return KErrNone;
   1.334 +	}
   1.335 +
   1.336 +GLDEF_C TInt E32Main()
   1.337 +	{
   1.338 +	__UHEAP_MARK;
   1.339 +	CTrapCleanup* cleanup=CTrapCleanup::New();
   1.340 +	TRAPD(ret, MainL());
   1.341 +	if(console)
   1.342 +		{
   1.343 +		console->Getch();
   1.344 +		delete console;
   1.345 +		}
   1.346 +	if (ret){} // stops compile warning
   1.347 +	delete cleanup;
   1.348 +	__UHEAP_MARKEND;
   1.349 +	return KErrNone;
   1.350 +	}