os/kernelhwsrv/kerneltest/e32test/debug/t_logtofile.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/debug/t_logtofile.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,298 @@
     1.4 +// Copyright (c) 2005-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 +// e32test\debyg\LogToFile.cpp
    1.18 +// t_logtofile.exe tests (alongside with d_logtofile.ldd) trace handler hook (TTraceHandler).
    1.19 +// Usage:
    1.20 +// 1. start t_logtofile -p=Pattern
    1.21 +// - Starts logging into memory.(RDebug::Pring, NKern::Print & PletSec log are all considered).
    1.22 +// - Only logs that start with "Pattern" will be logged (case sensitive).Leave '-p=' empty to log them all.
    1.23 +// - All debug loggings to serial port (or epocwnd.out on emulator) are suppressed.
    1.24 +// - There are 64KB memory available for the log. Once the memory is full, the logging stops.
    1.25 +// 2. start t_logtofile stop
    1.26 +// - Stops the logging (unless already stopped due to full memory).
    1.27 +// - Transfers collected logs into c:\logtofile.dat
    1.28 +// The format of the file is as follows:
    1.29 +// The pattern:		Pattern
    1.30 +// Buffer Size is:		65536
    1.31 +// Fast counter freq:	3579545
    1.32 +// 93559955	U	MsgSent
    1.33 +// 108774945	K	Thread t_suser.EXE::Main created @ 0x973fe8 - Win32 Thread ID 0xbbc
    1.34 +// 108810756	U	RTEST TITLE: T_SUSER 2.00(1013)
    1.35 +// The first column is the current value of the fast counter.
    1.36 +// The second column indicates U - user side, K - kernel or P-PlatSec logging.
    1.37 +// 
    1.38 +//
    1.39 +
    1.40 +#include <e32debug.h>
    1.41 +#include <e32cons.h>
    1.42 +#include <f32file.h>
    1.43 +#include <e32base.h>
    1.44 +#include <e32base_private.h>
    1.45 +#include "d_logtofile.h"
    1.46 +
    1.47 +
    1.48 +// The name of the output file use to save the sample data
    1.49 +_LIT(KFileName,"C:\\LogToFile.DAT");
    1.50 +_LIT(KAppName,"Logtofile");
    1.51 +const TInt KHeapSize=0x2000;
    1.52 +
    1.53 +#define KPatternMaxSize 10
    1.54 +TBuf8<KPatternMaxSize> Pattern;
    1.55 +
    1.56 +/**Server*/
    1.57 +class CLogToFileServer : public CServer2
    1.58 +	{
    1.59 +public:
    1.60 +	static CLogToFileServer* New(TInt aPriority) {return new CLogToFileServer(aPriority);}
    1.61 +private:
    1.62 +	CLogToFileServer(TInt aPriority) : CServer2(aPriority){}
    1.63 +	CSession2* NewSessionL(const TVersion& aVersion,const RMessage2& aMessage) const;
    1.64 +public:
    1.65 +static RLogToFileDevice iDevice;
    1.66 +static TChunkCreateStr iChunkStr;
    1.67 +static RChunk Chunk;
    1.68 +
    1.69 +	};
    1.70 +
    1.71 +/**Session*/
    1.72 +class CLogToFileSession : public CSession2
    1.73 +	{
    1.74 +public:
    1.75 +	enum TState {EStart, EStop};
    1.76 +private:
    1.77 +	void ServiceL(const RMessage2& aMessage);
    1.78 +	TInt Stop();
    1.79 +	TInt Start();
    1.80 +	};
    1.81 +
    1.82 +/*Client-side session*/
    1.83 +class RLogToFileSession : private RSessionBase
    1.84 +	{
    1.85 +public:
    1.86 +public:
    1.87 +	static inline TInt Start(){return Control(CLogToFileSession::EStart);}
    1.88 +	static inline TInt Stop() {return Control(CLogToFileSession::EStop);}
    1.89 +private:
    1.90 +	static inline TInt Control(CLogToFileSession::TState aRequest);
    1.91 +	};
    1.92 +
    1.93 +//------------globals---------------------
    1.94 +RLogToFileDevice CLogToFileServer::iDevice;
    1.95 +TChunkCreateStr  CLogToFileServer::iChunkStr;
    1.96 +RChunk           CLogToFileServer::Chunk;
    1.97 +
    1.98 +
    1.99 +/**Creates a new client for this server.*/
   1.100 +CSession2* CLogToFileServer::NewSessionL(const TVersion&, const RMessage2&) const
   1.101 +	{
   1.102 +	return new(ELeave) CLogToFileSession();
   1.103 +	}
   1.104 +
   1.105 +/**Entry point of the session request*/
   1.106 +void CLogToFileSession::ServiceL(const RMessage2& aMessage)
   1.107 +	{
   1.108 +	TInt r=KErrNone;
   1.109 +	switch (aMessage.Function())
   1.110 +		{
   1.111 +	case EStart:
   1.112 +		{
   1.113 +		r = Start();
   1.114 +		break;
   1.115 +		}
   1.116 +	case EStop:
   1.117 +		{
   1.118 +		r = Stop();
   1.119 +		CActiveScheduler::Stop();//This will stop the server thread.
   1.120 +		break;
   1.121 +		}
   1.122 +	default:
   1.123 +		r=KErrNotSupported;
   1.124 +		}
   1.125 +	aMessage.Complete(r);
   1.126 +	}
   1.127 +
   1.128 +/**
   1.129 +This will:
   1.130 + - Load t_logtofile.ldd
   1.131 + - Tell ldd to create the chunk
   1.132 + - Tell ldd to start logging
   1.133 +*/
   1.134 +TInt CLogToFileSession::Start()
   1.135 +	{
   1.136 +	TInt r = User::LoadLogicalDevice(KLogToFileName);
   1.137 +	if(r !=KErrNone && r!=KErrAlreadyExists)
   1.138 +		return r;
   1.139 +	if((r = CLogToFileServer::iDevice.Open())!=KErrNone)	
   1.140 +		{
   1.141 +		User::FreeLogicalDevice(KLogToFileName);
   1.142 +		return r;
   1.143 +		}
   1.144 +	CLogToFileServer::iChunkStr.iSize = 0x10000; //64K chunk size - hard coded
   1.145 +	if((r=CLogToFileServer::iDevice.CreateChunk(&CLogToFileServer::iChunkStr))<0)
   1.146 +	{
   1.147 +		User::FreeLogicalDevice(KLogToFileName);
   1.148 +		return r;
   1.149 +	}
   1.150 +	CLogToFileServer::Chunk.SetHandle(r);
   1.151 +	CLogToFileServer::iDevice.Start();	
   1.152 +	return KErrNone;
   1.153 +}
   1.154 +
   1.155 +/**
   1.156 +This will:
   1.157 + - Tell ldd to stop logging
   1.158 + - Put the content of the chunk into c:\logtofile.dat
   1.159 + - Tell ldd to close the chunk
   1.160 + - Unload t_logtofile.ldd
   1.161 +*/
   1.162 +TInt CLogToFileSession::Stop()
   1.163 +{
   1.164 +	TInt bytes = CLogToFileServer::iDevice.Stop();	
   1.165 +
   1.166 +	
   1.167 +	RFs fs;
   1.168 +	RFile file;
   1.169 +	TInt r;
   1.170 +	TRequestStatus status;
   1.171 +
   1.172 +	if(KErrNone != (r = fs.Connect()))
   1.173 +		return r;
   1.174 +	if(KErrNone != (r = file.Replace(fs, KFileName,EFileWrite)))
   1.175 +		{
   1.176 +		fs.Close();
   1.177 +		return r;
   1.178 +		}
   1.179 +
   1.180 +	TPtrC8 log(CLogToFileServer::Chunk.Base(),bytes);
   1.181 +	file.Write(log,status);
   1.182 +	User::WaitForRequest(status);
   1.183 +	r = status.Int();
   1.184 +	file.Close();
   1.185 +	fs.Close();
   1.186 +	CLogToFileServer::Chunk.Close();
   1.187 +
   1.188 +	CLogToFileServer::iDevice.RemoveChunk();	
   1.189 +	User::FreeLogicalDevice(KLogToFileName);
   1.190 +	return r;
   1.191 +}
   1.192 +
   1.193 +/**Sends request to the server*/
   1.194 +TInt RLogToFileSession::Control(CLogToFileSession::TState aRequest)
   1.195 +	{
   1.196 +	RLogToFileSession p;
   1.197 +	TInt r = p.CreateSession(KAppName, TVersion(), 0);
   1.198 +	if (r == KErrNone)
   1.199 +		p.SendReceive(aRequest);
   1.200 +	return r;
   1.201 +	}
   1.202 +
   1.203 +/**The entry point for the server thread.*/
   1.204 +LOCAL_C TInt serverThreadEntryPoint(TAny*)
   1.205 +	{
   1.206 +	TInt r=0;
   1.207 +	CLogToFileServer* pS=0;
   1.208 +	
   1.209 +	CActiveScheduler *pR=new CActiveScheduler;
   1.210 +	if (!pR) User::Panic(_L("Create ActSchdlr error"), KErrNoMemory);
   1.211 +	CActiveScheduler::Install(pR);
   1.212 +
   1.213 +	pS=CLogToFileServer::New(0);
   1.214 +	if (!pS)
   1.215 +	{
   1.216 +		delete pR;
   1.217 +		User::Panic(_L("Create svr error"), KErrNoMemory);
   1.218 +	}
   1.219 +
   1.220 +	r=pS->Start(KAppName);
   1.221 +	if(r)
   1.222 +		{
   1.223 +		delete pS;
   1.224 +		delete pR;
   1.225 +		User::Panic(_L("Start svr error"), r);
   1.226 +		}
   1.227 +
   1.228 +	RThread::Rendezvous(KErrNone);
   1.229 +	CActiveScheduler::Start();
   1.230 +	delete pS;
   1.231 +	delete pR;
   1.232 +	return(KErrNone);
   1.233 +	}
   1.234 +
   1.235 +/**Reads the command line and set the matching pattern to be - what is after '-p='*/
   1.236 +void SetPattern(void)
   1.237 +	{
   1.238 +	_LIT8(KPattern,"-p=");
   1.239 +	TBuf<64> c;
   1.240 +	User::CommandLine(c);
   1.241 +	#if defined(_UNICODE)
   1.242 +	TPtr8 ptr = c.Collapse();
   1.243 +	#else
   1.244 +	TPtr8 ptr(c.Ptr(),c.Length(),c.MaxLEngth());
   1.245 +	#endif
   1.246 +
   1.247 +	TInt patternStart = ptr.FindF(KPattern);
   1.248 +	if (patternStart < 0)
   1.249 +		{
   1.250 +		Pattern.SetLength(0);
   1.251 +		return;
   1.252 +		}
   1.253 +	patternStart+=3;
   1.254 +	
   1.255 +	TPtrC8 pattern (ptr.Ptr()+patternStart,Min(ptr.Length()-patternStart, KPatternMaxSize) );
   1.256 +	CLogToFileServer::iChunkStr.iPattern.Copy(pattern);
   1.257 +	}
   1.258 +
   1.259 +/**The main program if we have to start logging
   1.260 +   It creates the server and sends start-logging request.*/
   1.261 +void MainL()
   1.262 +	{
   1.263 +	RThread server;
   1.264 +	TRequestStatus status;
   1.265 +	TInt r = 0;
   1.266 +
   1.267 +	SetPattern();
   1.268 +	r=server.Create(_L("LogToFileServer"),serverThreadEntryPoint,KDefaultStackSize,KHeapSize,KHeapSize,NULL);
   1.269 +	User::LeaveIfError(r);
   1.270 +	server.Resume();
   1.271 +	server.Rendezvous(status);
   1.272 +	User::WaitForRequest(status);
   1.273 +	User::LeaveIfError(status.Int());
   1.274 +
   1.275 +	User::LeaveIfError(RLogToFileSession::Start());
   1.276 +
   1.277 +	server.Logon(status);
   1.278 +	User::WaitForRequest(status);
   1.279 +	}
   1.280 +
   1.281 +/**Returns true if 'stop' is found in the command line*/
   1.282 +TBool GetStop()
   1.283 +	{
   1.284 +	_LIT(KStop,"stop");
   1.285 +	TBuf<64> c;
   1.286 +	User::CommandLine(c);
   1.287 +	if (c.FindF(KStop) >= 0)
   1.288 +		return ETrue;
   1.289 +	return EFalse;
   1.290 +	}
   1.291 +
   1.292 +/**LogToFile.exe entry point*/
   1.293 +TInt E32Main()
   1.294 +	{
   1.295 +	if (GetStop())
   1.296 +		return RLogToFileSession::Stop();
   1.297 +
   1.298 +	CTrapCleanup::New();
   1.299 +	TRAPD(r,MainL());
   1.300 +	return r;
   1.301 +	}