os/kernelhwsrv/kerneltest/e32test/debug/d_logtofile.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
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\debug\d_logtofile.cpp
sl@0
    15
// d_logtofile.ldd is kernel side of t_logtofile application that tests *
sl@0
    16
// trace handler hook (TTraceHandler). See t_logtofile.cpp for details. *
sl@0
    17
// 
sl@0
    18
//
sl@0
    19
sl@0
    20
#include "d_logtofile.h"
sl@0
    21
#include <kernel/kern_priv.h>
sl@0
    22
sl@0
    23
_LIT(PatternInfo, "Pattern:");
sl@0
    24
_LIT(BufferInfo, "Buffer Size:");
sl@0
    25
_LIT(FastCounterInfo, "Fast couner freq:");
sl@0
    26
const TInt MaxLogSize=300;
sl@0
    27
///////////////////////////////////////////////////////////////////////
sl@0
    28
class DLogToFile : public DLogicalChannelBase
sl@0
    29
	{
sl@0
    30
public:
sl@0
    31
	DLogToFile();
sl@0
    32
	~DLogToFile();
sl@0
    33
	static TBool Handler (const TDesC8& aText, TTraceSource aTraceSource);
sl@0
    34
protected:
sl@0
    35
	virtual TInt DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
sl@0
    36
	virtual TInt Request(TInt aFunction, TAny* a1, TAny* a2);
sl@0
    37
private:
sl@0
    38
	TInt CreateChunk();
sl@0
    39
	void Start();
sl@0
    40
	TInt Stop();
sl@0
    41
	void RemoveChunk();
sl@0
    42
sl@0
    43
private:
sl@0
    44
    DChunk* iChunk;					/**Shared chunk*/
sl@0
    45
    TLinAddr iChunkKernelAddr;		/**Kernel base address of the chunk*/
sl@0
    46
    TUint32 iChunkMapAttr;			/**Mapping attributes of the chunk*/
sl@0
    47
	TBool iBufferFull;				/**Chunk full indication*/
sl@0
    48
	TText8* iCurrent;				/**Current pointer to the chunk space*/
sl@0
    49
	TChunkCreateStr iChunkCreateStr;/**Input arguments(matching pattern and the chunk size) from user side*/
sl@0
    50
	TTraceHandler iOldHook;			/**Previous trace logging hook*/
sl@0
    51
	};
sl@0
    52
sl@0
    53
DLogToFile* Logger;
sl@0
    54
sl@0
    55
DLogToFile::DLogToFile() 
sl@0
    56
	{
sl@0
    57
	}
sl@0
    58
sl@0
    59
DLogToFile::~DLogToFile()
sl@0
    60
	{
sl@0
    61
	Logger = NULL;
sl@0
    62
	}
sl@0
    63
sl@0
    64
/*
sl@0
    65
Trace handler hook.
sl@0
    66
Should be able to run in any content (including ISR).
sl@0
    67
@return EFalse, always and that way suppresses debug logging to serial port.
sl@0
    68
*/
sl@0
    69
TBool DLogToFile::Handler (const TDesC8& aText, TTraceSource aTraceSource)
sl@0
    70
	{
sl@0
    71
	if(Logger->iBufferFull)
sl@0
    72
		return ETrue;
sl@0
    73
sl@0
    74
	TInt irq=NKern::DisableAllInterrupts(); //Disable interrupts to prevent loggings overlapping each other.
sl@0
    75
sl@0
    76
	if (aText.Length() < Logger->iChunkCreateStr.iPattern.Length())
sl@0
    77
		{
sl@0
    78
		NKern::RestoreInterrupts(irq);
sl@0
    79
		return ETrue; //The log is too short to match the pattern
sl@0
    80
		}
sl@0
    81
	TPtrC8 ptr( aText.Ptr(), Logger->iChunkCreateStr.iPattern.Length());
sl@0
    82
	if (ptr != Logger->iChunkCreateStr.iPattern) //Compare the pattern against the start of the log
sl@0
    83
		{
sl@0
    84
		NKern::RestoreInterrupts(irq);
sl@0
    85
		return ETrue; //Does not match
sl@0
    86
		}
sl@0
    87
sl@0
    88
	TBuf8<20> fcBuffer;
sl@0
    89
	fcBuffer.AppendNum(NKern::FastCounter());//If it was a real tool, we should not do this here.
sl@0
    90
sl@0
    91
	memcpy (Logger->iCurrent, fcBuffer.Ptr(), fcBuffer.Length());
sl@0
    92
	Logger->iCurrent+=fcBuffer.Length();
sl@0
    93
	*(Logger->iCurrent++) = '\t';
sl@0
    94
sl@0
    95
	switch(aTraceSource)
sl@0
    96
		{
sl@0
    97
		case EKernelTrace:
sl@0
    98
			{
sl@0
    99
			*(Logger->iCurrent++) = 'K';
sl@0
   100
			*(Logger->iCurrent++) = '\t';
sl@0
   101
			memcpy (Logger->iCurrent, aText.Ptr(),aText.Length());
sl@0
   102
			break;	
sl@0
   103
			}
sl@0
   104
sl@0
   105
		case EPlatSecTrace:
sl@0
   106
			{
sl@0
   107
			*(Logger->iCurrent++) = 'P';
sl@0
   108
			*(Logger->iCurrent++) = '\t';
sl@0
   109
			//PlatSec log could be of any size. Additional checking required:
sl@0
   110
			if ((TInt)Logger->iCurrent > (TInt)(Logger->iChunkKernelAddr + Logger->iChunkCreateStr.iSize - aText.Length()))
sl@0
   111
				{
sl@0
   112
				Logger->iBufferFull = ETrue;
sl@0
   113
				break;
sl@0
   114
				}
sl@0
   115
			memcpy (Logger->iCurrent, aText.Ptr(),aText.Length());
sl@0
   116
			break;	
sl@0
   117
			}
sl@0
   118
		
sl@0
   119
		case EUserTrace:
sl@0
   120
			{
sl@0
   121
			*(Logger->iCurrent++) = 'U';
sl@0
   122
			*(Logger->iCurrent++) = '\t';
sl@0
   123
			memcpy(Logger->iCurrent, aText.Ptr(),aText.Length());
sl@0
   124
			break;	
sl@0
   125
			}
sl@0
   126
		
sl@0
   127
		default:
sl@0
   128
			break;
sl@0
   129
		}
sl@0
   130
sl@0
   131
	Logger->iCurrent+=aText.Length();
sl@0
   132
	*(Logger->iCurrent++) = '\n';
sl@0
   133
sl@0
   134
	if ((TInt)Logger->iCurrent > (TInt)(Logger->iChunkKernelAddr + Logger->iChunkCreateStr.iSize - MaxLogSize))
sl@0
   135
		Logger->iBufferFull = ETrue;
sl@0
   136
sl@0
   137
	NKern::RestoreInterrupts(irq);
sl@0
   138
sl@0
   139
	return ETrue;
sl@0
   140
	}
sl@0
   141
sl@0
   142
/**
sl@0
   143
Creates the channel
sl@0
   144
*/
sl@0
   145
TInt DLogToFile::DoCreate(TInt /*aUnit*/, const TDesC8* /*anInfo*/, const TVersion& /*aVer*/)
sl@0
   146
	{
sl@0
   147
	return KErrNone;
sl@0
   148
	}
sl@0
   149
sl@0
   150
/*
sl@0
   151
Creates chunk and opens another chunk handle for user side.
sl@0
   152
Returns user side handle.
sl@0
   153
*/
sl@0
   154
TInt DLogToFile::CreateChunk()
sl@0
   155
{
sl@0
   156
	TInt r;
sl@0
   157
    TChunkCreateInfo info;
sl@0
   158
    info.iType         = TChunkCreateInfo::ESharedKernelSingle;
sl@0
   159
    info.iMaxSize      = iChunkCreateStr.iSize;//This is hard coded to 64K. No need to round to page size.
sl@0
   160
#ifdef __EPOC32__
sl@0
   161
    info.iMapAttr      = (TInt)EMapAttrFullyBlocking; // Full caching
sl@0
   162
#endif
sl@0
   163
    info.iOwnsMemory   = ETrue; // Use memory from system's free pool
sl@0
   164
    info.iDestroyedDfc = NULL;
sl@0
   165
sl@0
   166
	NKern::ThreadEnterCS();
sl@0
   167
    if (KErrNone != (r = Kern::ChunkCreate(info, iChunk, iChunkKernelAddr, iChunkMapAttr)))
sl@0
   168
		{
sl@0
   169
		NKern::ThreadLeaveCS();
sl@0
   170
		return r;
sl@0
   171
		}
sl@0
   172
	r = Kern::ChunkCommit(iChunk,0,iChunkCreateStr.iSize);
sl@0
   173
    if(r!=KErrNone)
sl@0
   174
        {
sl@0
   175
		Kern::ChunkClose(iChunk);
sl@0
   176
		NKern::ThreadLeaveCS();
sl@0
   177
		return r;
sl@0
   178
		}
sl@0
   179
	r = Kern::MakeHandleAndOpen(NULL, iChunk); 
sl@0
   180
    if(r < KErrNone)
sl@0
   181
        {
sl@0
   182
		Kern::ChunkClose(iChunk);
sl@0
   183
		NKern::ThreadLeaveCS();
sl@0
   184
		return r;
sl@0
   185
		}
sl@0
   186
	NKern::ThreadLeaveCS();
sl@0
   187
	iCurrent = (TText8*)iChunkKernelAddr;
sl@0
   188
	return r;
sl@0
   189
}
sl@0
   190
sl@0
   191
sl@0
   192
/*
sl@0
   193
Logs input parameters info into chunk and starts logging.
sl@0
   194
*/
sl@0
   195
void DLogToFile::Start()
sl@0
   196
	{
sl@0
   197
	//Log pattern info
sl@0
   198
	memcpy (Logger->iCurrent, ((const TDesC&)PatternInfo).Ptr(), ((const TDesC&)PatternInfo).Length());
sl@0
   199
	iCurrent+=((const TDesC&)PatternInfo).Length();
sl@0
   200
	*(Logger->iCurrent++) = '\t';
sl@0
   201
	memcpy (Logger->iCurrent, iChunkCreateStr.iPattern.Ptr(), iChunkCreateStr.iPattern.Length());
sl@0
   202
	Logger->iCurrent += iChunkCreateStr.iPattern.Length();
sl@0
   203
	*(iCurrent++) = '\n';
sl@0
   204
sl@0
   205
	//Log buffern info
sl@0
   206
	memcpy (iCurrent, ((const TDesC&)BufferInfo).Ptr(), ((const TDesC&)BufferInfo).Length());
sl@0
   207
	iCurrent+=((const TDesC&)BufferInfo).Length();
sl@0
   208
	*(Logger->iCurrent++) = '\t';
sl@0
   209
	TBuf8<20> buf;
sl@0
   210
	buf.AppendNum(iChunkCreateStr.iSize);
sl@0
   211
	memcpy (iCurrent, buf.Ptr(), buf.Length());
sl@0
   212
	iCurrent+=buf.Length();
sl@0
   213
	*(iCurrent++) = '\n';
sl@0
   214
sl@0
   215
	//Log fast counter info
sl@0
   216
	memcpy (iCurrent, ((const TDesC&)FastCounterInfo).Ptr(), ((const TDesC&)FastCounterInfo).Length());
sl@0
   217
	iCurrent+=((const TDesC&)FastCounterInfo).Length();
sl@0
   218
	*(iCurrent++) = '\t';
sl@0
   219
	buf.SetLength(0);
sl@0
   220
	buf.AppendNum(NKern::FastCounterFrequency());
sl@0
   221
	memcpy (iCurrent, buf.Ptr(), buf.Length());
sl@0
   222
	iCurrent+=buf.Length();
sl@0
   223
	*(iCurrent++) = '\n';
sl@0
   224
sl@0
   225
	//Start logging	
sl@0
   226
	iOldHook = Kern::SetTraceHandler(DLogToFile::Handler);
sl@0
   227
	}
sl@0
   228
sl@0
   229
/*
sl@0
   230
Stops logging into chunk. Returns the size of the log.
sl@0
   231
*/
sl@0
   232
TInt DLogToFile::Stop()
sl@0
   233
	{
sl@0
   234
	//Setting the old hook is always risky. Is the old hook still valid?
sl@0
   235
	//We'll do it for the sake of testing.
sl@0
   236
	Kern::SetTraceHandler(iOldHook);
sl@0
   237
	return (TInt)Logger->iCurrent  - Logger->iChunkKernelAddr;
sl@0
   238
	}
sl@0
   239
sl@0
   240
/*
sl@0
   241
Closes the shared chunk
sl@0
   242
*/
sl@0
   243
void DLogToFile::RemoveChunk()
sl@0
   244
{
sl@0
   245
	NKern::ThreadEnterCS();
sl@0
   246
	//It has been a while since we removed the hook. It should be safe to close the chunk now. 
sl@0
   247
	Kern::ChunkClose(iChunk);
sl@0
   248
	NKern::ThreadLeaveCS();
sl@0
   249
}
sl@0
   250
sl@0
   251
/**
sl@0
   252
User side request entry point.
sl@0
   253
*/
sl@0
   254
TInt DLogToFile::Request(TInt aFunction, TAny* a1, TAny* /*a2*/)
sl@0
   255
	{
sl@0
   256
	TInt r = KErrNone;
sl@0
   257
	switch (aFunction)
sl@0
   258
	{
sl@0
   259
		case RLogToFileDevice::EControlCreateChunk:
sl@0
   260
			{
sl@0
   261
			kumemget(&iChunkCreateStr, a1,sizeof(TChunkCreateStr));
sl@0
   262
			r = CreateChunk();
sl@0
   263
			break;
sl@0
   264
			}
sl@0
   265
		case RLogToFileDevice::EControlStart:
sl@0
   266
			{
sl@0
   267
			Start();
sl@0
   268
			break;
sl@0
   269
			}
sl@0
   270
		case RLogToFileDevice::EControlStop:
sl@0
   271
			{
sl@0
   272
			r = Stop();
sl@0
   273
			break;
sl@0
   274
			}
sl@0
   275
		case RLogToFileDevice::EControlRemoveChunk:
sl@0
   276
			{
sl@0
   277
			RemoveChunk();
sl@0
   278
			break;
sl@0
   279
			}
sl@0
   280
		default:
sl@0
   281
			r=KErrNotSupported;
sl@0
   282
			break;
sl@0
   283
		}
sl@0
   284
	return r;
sl@0
   285
	}
sl@0
   286
sl@0
   287
//////////////////////////////////////////
sl@0
   288
class DTestFactory : public DLogicalDevice
sl@0
   289
	{
sl@0
   290
public:
sl@0
   291
	DTestFactory();
sl@0
   292
	// from DLogicalDevice
sl@0
   293
	virtual TInt Install();
sl@0
   294
	virtual void GetCaps(TDes8& aDes) const;
sl@0
   295
	virtual TInt Create(DLogicalChannelBase*& aChannel);
sl@0
   296
	};
sl@0
   297
sl@0
   298
DTestFactory::DTestFactory()
sl@0
   299
    {
sl@0
   300
    iParseMask = KDeviceAllowUnit;
sl@0
   301
    iUnitsMask = 0x3;
sl@0
   302
    }
sl@0
   303
sl@0
   304
TInt DTestFactory::Create(DLogicalChannelBase*& aChannel)
sl@0
   305
    {
sl@0
   306
	Logger = new DLogToFile;
sl@0
   307
	aChannel = Logger;
sl@0
   308
	return (aChannel ? KErrNone : KErrNoMemory);
sl@0
   309
    }
sl@0
   310
sl@0
   311
TInt DTestFactory::Install()
sl@0
   312
    {
sl@0
   313
    return SetName(&KLogToFileName);
sl@0
   314
    }
sl@0
   315
sl@0
   316
void DTestFactory::GetCaps(TDes8& /*aDes*/) const
sl@0
   317
    {
sl@0
   318
    }
sl@0
   319
sl@0
   320
DECLARE_STANDARD_LDD()
sl@0
   321
	{
sl@0
   322
    return new DTestFactory;
sl@0
   323
	}