os/kernelhwsrv/kerneltest/e32test/debug/t_logtofile.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32test\debyg\LogToFile.cpp
sl@0
    15
// t_logtofile.exe tests (alongside with d_logtofile.ldd) trace handler hook (TTraceHandler).
sl@0
    16
// Usage:
sl@0
    17
// 1. start t_logtofile -p=Pattern
sl@0
    18
// - Starts logging into memory.(RDebug::Pring, NKern::Print & PletSec log are all considered).
sl@0
    19
// - Only logs that start with "Pattern" will be logged (case sensitive).Leave '-p=' empty to log them all.
sl@0
    20
// - All debug loggings to serial port (or epocwnd.out on emulator) are suppressed.
sl@0
    21
// - There are 64KB memory available for the log. Once the memory is full, the logging stops.
sl@0
    22
// 2. start t_logtofile stop
sl@0
    23
// - Stops the logging (unless already stopped due to full memory).
sl@0
    24
// - Transfers collected logs into c:\logtofile.dat
sl@0
    25
// The format of the file is as follows:
sl@0
    26
// The pattern:		Pattern
sl@0
    27
// Buffer Size is:		65536
sl@0
    28
// Fast counter freq:	3579545
sl@0
    29
// 93559955	U	MsgSent
sl@0
    30
// 108774945	K	Thread t_suser.EXE::Main created @ 0x973fe8 - Win32 Thread ID 0xbbc
sl@0
    31
// 108810756	U	RTEST TITLE: T_SUSER 2.00(1013)
sl@0
    32
// The first column is the current value of the fast counter.
sl@0
    33
// The second column indicates U - user side, K - kernel or P-PlatSec logging.
sl@0
    34
// 
sl@0
    35
//
sl@0
    36
sl@0
    37
#include <e32debug.h>
sl@0
    38
#include <e32cons.h>
sl@0
    39
#include <f32file.h>
sl@0
    40
#include <e32base.h>
sl@0
    41
#include <e32base_private.h>
sl@0
    42
#include "d_logtofile.h"
sl@0
    43
sl@0
    44
sl@0
    45
// The name of the output file use to save the sample data
sl@0
    46
_LIT(KFileName,"C:\\LogToFile.DAT");
sl@0
    47
_LIT(KAppName,"Logtofile");
sl@0
    48
const TInt KHeapSize=0x2000;
sl@0
    49
sl@0
    50
#define KPatternMaxSize 10
sl@0
    51
TBuf8<KPatternMaxSize> Pattern;
sl@0
    52
sl@0
    53
/**Server*/
sl@0
    54
class CLogToFileServer : public CServer2
sl@0
    55
	{
sl@0
    56
public:
sl@0
    57
	static CLogToFileServer* New(TInt aPriority) {return new CLogToFileServer(aPriority);}
sl@0
    58
private:
sl@0
    59
	CLogToFileServer(TInt aPriority) : CServer2(aPriority){}
sl@0
    60
	CSession2* NewSessionL(const TVersion& aVersion,const RMessage2& aMessage) const;
sl@0
    61
public:
sl@0
    62
static RLogToFileDevice iDevice;
sl@0
    63
static TChunkCreateStr iChunkStr;
sl@0
    64
static RChunk Chunk;
sl@0
    65
sl@0
    66
	};
sl@0
    67
sl@0
    68
/**Session*/
sl@0
    69
class CLogToFileSession : public CSession2
sl@0
    70
	{
sl@0
    71
public:
sl@0
    72
	enum TState {EStart, EStop};
sl@0
    73
private:
sl@0
    74
	void ServiceL(const RMessage2& aMessage);
sl@0
    75
	TInt Stop();
sl@0
    76
	TInt Start();
sl@0
    77
	};
sl@0
    78
sl@0
    79
/*Client-side session*/
sl@0
    80
class RLogToFileSession : private RSessionBase
sl@0
    81
	{
sl@0
    82
public:
sl@0
    83
public:
sl@0
    84
	static inline TInt Start(){return Control(CLogToFileSession::EStart);}
sl@0
    85
	static inline TInt Stop() {return Control(CLogToFileSession::EStop);}
sl@0
    86
private:
sl@0
    87
	static inline TInt Control(CLogToFileSession::TState aRequest);
sl@0
    88
	};
sl@0
    89
sl@0
    90
//------------globals---------------------
sl@0
    91
RLogToFileDevice CLogToFileServer::iDevice;
sl@0
    92
TChunkCreateStr  CLogToFileServer::iChunkStr;
sl@0
    93
RChunk           CLogToFileServer::Chunk;
sl@0
    94
sl@0
    95
sl@0
    96
/**Creates a new client for this server.*/
sl@0
    97
CSession2* CLogToFileServer::NewSessionL(const TVersion&, const RMessage2&) const
sl@0
    98
	{
sl@0
    99
	return new(ELeave) CLogToFileSession();
sl@0
   100
	}
sl@0
   101
sl@0
   102
/**Entry point of the session request*/
sl@0
   103
void CLogToFileSession::ServiceL(const RMessage2& aMessage)
sl@0
   104
	{
sl@0
   105
	TInt r=KErrNone;
sl@0
   106
	switch (aMessage.Function())
sl@0
   107
		{
sl@0
   108
	case EStart:
sl@0
   109
		{
sl@0
   110
		r = Start();
sl@0
   111
		break;
sl@0
   112
		}
sl@0
   113
	case EStop:
sl@0
   114
		{
sl@0
   115
		r = Stop();
sl@0
   116
		CActiveScheduler::Stop();//This will stop the server thread.
sl@0
   117
		break;
sl@0
   118
		}
sl@0
   119
	default:
sl@0
   120
		r=KErrNotSupported;
sl@0
   121
		}
sl@0
   122
	aMessage.Complete(r);
sl@0
   123
	}
sl@0
   124
sl@0
   125
/**
sl@0
   126
This will:
sl@0
   127
 - Load t_logtofile.ldd
sl@0
   128
 - Tell ldd to create the chunk
sl@0
   129
 - Tell ldd to start logging
sl@0
   130
*/
sl@0
   131
TInt CLogToFileSession::Start()
sl@0
   132
	{
sl@0
   133
	TInt r = User::LoadLogicalDevice(KLogToFileName);
sl@0
   134
	if(r !=KErrNone && r!=KErrAlreadyExists)
sl@0
   135
		return r;
sl@0
   136
	if((r = CLogToFileServer::iDevice.Open())!=KErrNone)	
sl@0
   137
		{
sl@0
   138
		User::FreeLogicalDevice(KLogToFileName);
sl@0
   139
		return r;
sl@0
   140
		}
sl@0
   141
	CLogToFileServer::iChunkStr.iSize = 0x10000; //64K chunk size - hard coded
sl@0
   142
	if((r=CLogToFileServer::iDevice.CreateChunk(&CLogToFileServer::iChunkStr))<0)
sl@0
   143
	{
sl@0
   144
		User::FreeLogicalDevice(KLogToFileName);
sl@0
   145
		return r;
sl@0
   146
	}
sl@0
   147
	CLogToFileServer::Chunk.SetHandle(r);
sl@0
   148
	CLogToFileServer::iDevice.Start();	
sl@0
   149
	return KErrNone;
sl@0
   150
}
sl@0
   151
sl@0
   152
/**
sl@0
   153
This will:
sl@0
   154
 - Tell ldd to stop logging
sl@0
   155
 - Put the content of the chunk into c:\logtofile.dat
sl@0
   156
 - Tell ldd to close the chunk
sl@0
   157
 - Unload t_logtofile.ldd
sl@0
   158
*/
sl@0
   159
TInt CLogToFileSession::Stop()
sl@0
   160
{
sl@0
   161
	TInt bytes = CLogToFileServer::iDevice.Stop();	
sl@0
   162
sl@0
   163
	
sl@0
   164
	RFs fs;
sl@0
   165
	RFile file;
sl@0
   166
	TInt r;
sl@0
   167
	TRequestStatus status;
sl@0
   168
sl@0
   169
	if(KErrNone != (r = fs.Connect()))
sl@0
   170
		return r;
sl@0
   171
	if(KErrNone != (r = file.Replace(fs, KFileName,EFileWrite)))
sl@0
   172
		{
sl@0
   173
		fs.Close();
sl@0
   174
		return r;
sl@0
   175
		}
sl@0
   176
sl@0
   177
	TPtrC8 log(CLogToFileServer::Chunk.Base(),bytes);
sl@0
   178
	file.Write(log,status);
sl@0
   179
	User::WaitForRequest(status);
sl@0
   180
	r = status.Int();
sl@0
   181
	file.Close();
sl@0
   182
	fs.Close();
sl@0
   183
	CLogToFileServer::Chunk.Close();
sl@0
   184
sl@0
   185
	CLogToFileServer::iDevice.RemoveChunk();	
sl@0
   186
	User::FreeLogicalDevice(KLogToFileName);
sl@0
   187
	return r;
sl@0
   188
}
sl@0
   189
sl@0
   190
/**Sends request to the server*/
sl@0
   191
TInt RLogToFileSession::Control(CLogToFileSession::TState aRequest)
sl@0
   192
	{
sl@0
   193
	RLogToFileSession p;
sl@0
   194
	TInt r = p.CreateSession(KAppName, TVersion(), 0);
sl@0
   195
	if (r == KErrNone)
sl@0
   196
		p.SendReceive(aRequest);
sl@0
   197
	return r;
sl@0
   198
	}
sl@0
   199
sl@0
   200
/**The entry point for the server thread.*/
sl@0
   201
LOCAL_C TInt serverThreadEntryPoint(TAny*)
sl@0
   202
	{
sl@0
   203
	TInt r=0;
sl@0
   204
	CLogToFileServer* pS=0;
sl@0
   205
	
sl@0
   206
	CActiveScheduler *pR=new CActiveScheduler;
sl@0
   207
	if (!pR) User::Panic(_L("Create ActSchdlr error"), KErrNoMemory);
sl@0
   208
	CActiveScheduler::Install(pR);
sl@0
   209
sl@0
   210
	pS=CLogToFileServer::New(0);
sl@0
   211
	if (!pS)
sl@0
   212
	{
sl@0
   213
		delete pR;
sl@0
   214
		User::Panic(_L("Create svr error"), KErrNoMemory);
sl@0
   215
	}
sl@0
   216
sl@0
   217
	r=pS->Start(KAppName);
sl@0
   218
	if(r)
sl@0
   219
		{
sl@0
   220
		delete pS;
sl@0
   221
		delete pR;
sl@0
   222
		User::Panic(_L("Start svr error"), r);
sl@0
   223
		}
sl@0
   224
sl@0
   225
	RThread::Rendezvous(KErrNone);
sl@0
   226
	CActiveScheduler::Start();
sl@0
   227
	delete pS;
sl@0
   228
	delete pR;
sl@0
   229
	return(KErrNone);
sl@0
   230
	}
sl@0
   231
sl@0
   232
/**Reads the command line and set the matching pattern to be - what is after '-p='*/
sl@0
   233
void SetPattern(void)
sl@0
   234
	{
sl@0
   235
	_LIT8(KPattern,"-p=");
sl@0
   236
	TBuf<64> c;
sl@0
   237
	User::CommandLine(c);
sl@0
   238
	#if defined(_UNICODE)
sl@0
   239
	TPtr8 ptr = c.Collapse();
sl@0
   240
	#else
sl@0
   241
	TPtr8 ptr(c.Ptr(),c.Length(),c.MaxLEngth());
sl@0
   242
	#endif
sl@0
   243
sl@0
   244
	TInt patternStart = ptr.FindF(KPattern);
sl@0
   245
	if (patternStart < 0)
sl@0
   246
		{
sl@0
   247
		Pattern.SetLength(0);
sl@0
   248
		return;
sl@0
   249
		}
sl@0
   250
	patternStart+=3;
sl@0
   251
	
sl@0
   252
	TPtrC8 pattern (ptr.Ptr()+patternStart,Min(ptr.Length()-patternStart, KPatternMaxSize) );
sl@0
   253
	CLogToFileServer::iChunkStr.iPattern.Copy(pattern);
sl@0
   254
	}
sl@0
   255
sl@0
   256
/**The main program if we have to start logging
sl@0
   257
   It creates the server and sends start-logging request.*/
sl@0
   258
void MainL()
sl@0
   259
	{
sl@0
   260
	RThread server;
sl@0
   261
	TRequestStatus status;
sl@0
   262
	TInt r = 0;
sl@0
   263
sl@0
   264
	SetPattern();
sl@0
   265
	r=server.Create(_L("LogToFileServer"),serverThreadEntryPoint,KDefaultStackSize,KHeapSize,KHeapSize,NULL);
sl@0
   266
	User::LeaveIfError(r);
sl@0
   267
	server.Resume();
sl@0
   268
	server.Rendezvous(status);
sl@0
   269
	User::WaitForRequest(status);
sl@0
   270
	User::LeaveIfError(status.Int());
sl@0
   271
sl@0
   272
	User::LeaveIfError(RLogToFileSession::Start());
sl@0
   273
sl@0
   274
	server.Logon(status);
sl@0
   275
	User::WaitForRequest(status);
sl@0
   276
	}
sl@0
   277
sl@0
   278
/**Returns true if 'stop' is found in the command line*/
sl@0
   279
TBool GetStop()
sl@0
   280
	{
sl@0
   281
	_LIT(KStop,"stop");
sl@0
   282
	TBuf<64> c;
sl@0
   283
	User::CommandLine(c);
sl@0
   284
	if (c.FindF(KStop) >= 0)
sl@0
   285
		return ETrue;
sl@0
   286
	return EFalse;
sl@0
   287
	}
sl@0
   288
sl@0
   289
/**LogToFile.exe entry point*/
sl@0
   290
TInt E32Main()
sl@0
   291
	{
sl@0
   292
	if (GetStop())
sl@0
   293
		return RLogToFileSession::Stop();
sl@0
   294
sl@0
   295
	CTrapCleanup::New();
sl@0
   296
	TRAPD(r,MainL());
sl@0
   297
	return r;
sl@0
   298
	}