os/kernelhwsrv/kerneltest/f32test/server/t_ftrace.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 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".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // f32test\server\t_ftrace.cpp
    15 // 
    16 //
    17 
    18 #include <f32file.h>
    19 #include <f32tracedef.h>
    20 #include <e32test.h>
    21 #include "t_server.h"
    22 
    23 #include "../../../kernel/eka/include/d32btrace.h"
    24 #include "../../../kernel/eka/include/e32btrace.h"
    25 #include <utraceefsrv.h>
    26 
    27 RTest test(_L("T_FTRACE"));
    28 
    29 RBTrace Trace;
    30 
    31 void SetBTraceFilter(const TUint32* aNew,TUint32* aOld)
    32 	{
    33 	TUint category = 0;
    34 	do
    35 		{
    36 		TUint32 newBits = *aNew++;
    37 		TUint32 oldBits = 0;
    38 		do
    39 			{
    40 			oldBits >>= 1;
    41 			if(Trace.SetFilter(category,newBits&1))
    42 				oldBits |= 0x80000000u;
    43 			newBits >>= 1;
    44 			++category;
    45 			}
    46 		while(category&31);
    47 		if(aOld)
    48 			*aOld++ = oldBits;
    49 		}
    50 	while(category<256);
    51 	}
    52 
    53 
    54 
    55 //---------------------------------------------------------------------------------------------------------------------
    56 //! @SYMTestCaseID				KBASE-T_FTRACE-0001
    57 //! @SYMTestCaseDesc			Test File Server Tracing of RFile::Replace()
    58 //! @SYMTestType				UT
    59 //! @SYMPREQ					PREQ1617
    60 //! @SYMTestPriority			Medium
    61 //! @SYMTestActions				
    62 //! 	1.	Call RFile::Replace() to create a file
    63 //! 	2.	Get trace data from BTrace and verify that the expected trace data is present
    64 //! 
    65 //! @SYMTestExpectedResults
    66 //! 	1.	Trace data payload should be as expected, i.e. it should contain the file name, mode etc.
    67 //---------------------------------------------------------------------------------------------------------------------
    68 void TestRFileReplace()
    69 	{
    70 	test.Start(_L("Test trace output from creating a file"));
    71 	RFile file;
    72 	TFileName testFileName = _L("File.txt");
    73 
    74 	TheFs.Delete(testFileName);
    75 
    76 	Trace.Empty();
    77 
    78 	TInt r = file.Replace(TheFs,testFileName,EFileStreamText);
    79 	test(r==KErrNone);
    80 
    81 
    82 	TBool funcInFound = EFalse;
    83 	TBool funcOutFound = EFalse;
    84 
    85 	TBuf8<1024> buf;
    86 	for(;;)
    87 		{
    88 		TUint8* record;
    89 		TInt dataSize = Trace.GetData(record);
    90 		if(!dataSize)
    91 			break;
    92 		TUint8* end = record+dataSize;
    93 
    94 		while(record<end)
    95 			{
    96 			TUint size = record[BTrace::ESizeIndex];
    97 			TUint flags = record[BTrace::EFlagsIndex];
    98 			TUint category = record[BTrace::ECategoryIndex];
    99 			TUint subCategory = record[BTrace::ESubCategoryIndex];
   100 			TUint8* data = record+4;
   101 			size -= 4;
   102 
   103 			buf.Zero();
   104 			if(flags&(BTrace::EHeader2Present))
   105 				{
   106 				data += 4;
   107 				size -= 4;
   108 				}
   109 
   110 			if((flags&(BTrace::ETimestampPresent|BTrace::ETimestamp2Present))==(BTrace::ETimestampPresent|BTrace::ETimestamp2Present))
   111 				{
   112 				buf.AppendFormat(_L8("time:%08x:%08x "),((TUint32*)data)[1],*(TUint32*)data);
   113 				data += 8;
   114 				size -= 8;
   115 				}
   116 			else if(flags&(BTrace::ETimestampPresent|BTrace::ETimestamp2Present))
   117 				{
   118 				buf.AppendFormat(_L8("time:%08x "),*(TUint32*)data);
   119 				data += 4;
   120 				size -= 4;
   121 				}
   122 
   123 			if(flags&(BTrace::EContextIdPresent))
   124 				{
   125 				buf.AppendFormat(_L8("context:%08x "),*(TUint32*)data);
   126 				data += 4;
   127 				size -= 4;
   128 				}
   129 			else
   130 				{
   131 				buf.AppendFormat(_L8("                 "));
   132 				}
   133 
   134 			if(flags&(BTrace::EPcPresent))
   135 				{
   136 				buf.AppendFormat(_L8("pc:%08x "),*(TUint32*)data);
   137 				data += 4;
   138 				size -= 4;
   139 				}
   140 
   141 			if(flags&(BTrace::EExtraPresent))
   142 				{
   143 				data += 4;
   144 				size -= 4;
   145 				}
   146 
   147 			TUint32 data0 = (size>0) ? *(TUint32*)(data) : 0;
   148 			TUint32 data1 = (size>4) ? *(TUint32*)(data+4) : 0;
   149 			TPtrC8 des(0,0);
   150 			if(size>=8)
   151 				des.Set(data+8,size-8);
   152 
   153 			buf.AppendFormat(_L8("size:%d flags:%02x cat:%d,%d data: "),size,flags,category,subCategory);
   154 			for(TUint i=0; i<size; i+=4)
   155 				buf.AppendFormat(_L8("%08x "),*(TUint32*)(data+i));
   156 			buf.Append('\r');
   157 			buf.Append('\n');
   158 			test(buf.MaxLength() >= (buf.Length()*2));
   159 			RDebug::RawPrint(buf.Expand());
   160 
   161 
   162 			if (category == UTF::EBorder && subCategory == 0 && data0 == EF32TraceUidEfsrv)
   163 				{
   164 				if (data1 == UTraceModuleEfsrv::EFileReplace)
   165 					{
   166 					TInt sessionHandle = (size>8) ? *(TUint32*)(data+8) : 0;
   167 					TUint32 fileMode = (size>12) ? *(TUint32*)(data+12) : 0;
   168 					TInt fileNameLen = (size>16) ? *(TUint32*)(data+16) : 0;
   169 					fileNameLen/= 2;	// convert to unicode length
   170 					TText16* fileName = (TText16*) ((size>20) ? (data+20) : NULL);
   171 
   172 					test(sessionHandle == TheFs.Handle());
   173 					test(fileMode == EFileStreamText);
   174 					test(fileNameLen == testFileName.Length());
   175 					TPtrC16 fileNamePtr (fileName, fileNameLen);
   176 					test(fileName != NULL);
   177 					test(testFileName.Compare(fileNamePtr) == 0);
   178 					funcInFound = ETrue;
   179 					}
   180 				else if (data1 == UTraceModuleEfsrv::EFileReplaceReturn)
   181 					{
   182 					TInt retCode = (size>8) ? *(TUint32*)(data+8) : 0;
   183 					TInt subsessionHandle = (size>12) ? *(TUint32*)(data+12) : 0;
   184 
   185 					test(retCode == KErrNone);
   186 					test(subsessionHandle == file.SubSessionHandle());
   187 					funcOutFound = ETrue;
   188 					}
   189 				}
   190 
   191 			record = BTrace::NextRecord(record);
   192 			}
   193 		Trace.DataUsed();
   194 		}
   195 
   196 	file.Close();
   197 	TheFs.Delete(testFileName);
   198 
   199 	test (funcInFound);
   200 	test (funcOutFound);
   201 	}
   202 
   203 //---------------------------------------------------------------------------------------------------------------------
   204 //! @SYMTestCaseID				KBASE-T_FTRACE-0002
   205 //! @SYMTestCaseDesc			Test File Server Tracing of RFs::Rename()
   206 //! @SYMTestType				UT
   207 //! @SYMPREQ					PREQ1617
   208 //! @SYMTestPriority			Medium
   209 //! @SYMTestActions				
   210 //! 	1.	Call RFile::Replace() to create a file
   211 //! 	2.	Close the file
   212 //! 	3.	Call RFs::Rename to rename the file
   213 //! 	4.	Get trace data from BTrace and verify that the expected trace data is present
   214 //! 
   215 //! @SYMTestExpectedResults
   216 //! 	1.	Trace data payload should be as expected, i.e. it should contain both file names, etc.
   217 //---------------------------------------------------------------------------------------------------------------------
   218 void TestRFsRename()
   219 	{
   220 	test.Start(_L("Test trace output from renaming a file"));
   221 	RFile file;
   222 	TFileName testFileName1 = _L("File1.txt");
   223 	TFileName testFileName2 = _L("File2.txt");
   224 
   225 	TheFs.Delete(testFileName1);
   226 	TheFs.Delete(testFileName2);
   227 
   228 	TInt r = file.Replace(TheFs,testFileName1,EFileStreamText);
   229 	test(r==KErrNone || KErrAlreadyExists);
   230 	file.Close();
   231 
   232 	Trace.Empty();
   233 
   234 	r = TheFs.Rename(testFileName1, testFileName2);
   235 	test(r==KErrNone);
   236 
   237 
   238 	TBool funcInFound = EFalse;
   239 	TBool funcOutFound = EFalse;
   240 
   241 	TBuf8<1024> buf;
   242 	for(;;)
   243 		{
   244 		TUint8* record;
   245 		TInt dataSize = Trace.GetData(record);
   246 		if(!dataSize)
   247 			break;
   248 		TUint8* end = record+dataSize;
   249 
   250 		while(record<end)
   251 			{
   252 			TUint size = record[BTrace::ESizeIndex];
   253 			TUint flags = record[BTrace::EFlagsIndex];
   254 			TUint category = record[BTrace::ECategoryIndex];
   255 			TUint subCategory = record[BTrace::ESubCategoryIndex];
   256 			TUint8* data = record+4;
   257 			size -= 4;
   258 
   259 			buf.Zero();
   260 			if(flags&(BTrace::EHeader2Present))
   261 				{
   262 				data += 4;
   263 				size -= 4;
   264 				}
   265 
   266 			if((flags&(BTrace::ETimestampPresent|BTrace::ETimestamp2Present))==(BTrace::ETimestampPresent|BTrace::ETimestamp2Present))
   267 				{
   268 				buf.AppendFormat(_L8("time:%08x:%08x "),((TUint32*)data)[1],*(TUint32*)data);
   269 				data += 8;
   270 				size -= 8;
   271 				}
   272 			else if(flags&(BTrace::ETimestampPresent|BTrace::ETimestamp2Present))
   273 				{
   274 				buf.AppendFormat(_L8("time:%08x "),*(TUint32*)data);
   275 				data += 4;
   276 				size -= 4;
   277 				}
   278 
   279 			if(flags&(BTrace::EContextIdPresent))
   280 				{
   281 				buf.AppendFormat(_L8("context:%08x "),*(TUint32*)data);
   282 				data += 4;
   283 				size -= 4;
   284 				}
   285 			else
   286 				{
   287 				buf.AppendFormat(_L8("                 "));
   288 				}
   289 
   290 			if(flags&(BTrace::EPcPresent))
   291 				{
   292 				buf.AppendFormat(_L8("pc:%08x "),*(TUint32*)data);
   293 				data += 4;
   294 				size -= 4;
   295 				}
   296 
   297 			if(flags&(BTrace::EExtraPresent))
   298 				{
   299 				data += 4;
   300 				size -= 4;
   301 				}
   302 
   303 			TUint32 data0 = (size>0) ? *(TUint32*)(data) : 0;
   304 			TUint32 data1 = (size>4) ? *(TUint32*)(data+4) : 0;
   305 			TPtrC8 des(0,0);
   306 			if(size>=8)
   307 				des.Set(data+8,size-8);
   308 
   309 			buf.AppendFormat(_L8("size:%d flags:%02x cat:%d,%d data: "),size,flags,category,subCategory);
   310 			for(TUint i=0; i<size; i+=4)
   311 				buf.AppendFormat(_L8("%08x "),*(TUint32*)(data+i));
   312 			buf.Append('\r');
   313 			buf.Append('\n');
   314 			test(buf.MaxLength() >= (buf.Length()*2));
   315 			RDebug::RawPrint(buf.Expand());
   316 
   317 
   318 			if (category == UTF::EBorder && subCategory == 0 && data0 == EF32TraceUidEfsrv)
   319 				{
   320 				TUint8* recData = data+8;
   321 				if (data1 == UTraceModuleEfsrv::EFsRename)
   322 					{
   323 					TInt sessionHandle = *(TUint32*) recData; recData+= 4;
   324 
   325 					TInt fileNameLen1 = *(TUint32*) recData; recData+= 4;
   326 					TText16* fileName1 = (TText16*) recData; recData+= ((fileNameLen1 +4) & ~3);
   327 
   328 					TInt fileNameLen2 = *(TUint32*) recData; recData+= 4;
   329 					TText16* fileName2 = (TText16*) recData; recData+= fileNameLen2;
   330 
   331 					fileNameLen1/= 2;	// convert to unicode length
   332 					fileNameLen2/= 2;	// convert to unicode length
   333 
   334 
   335 					test(sessionHandle == TheFs.Handle());
   336 					
   337 					test(fileNameLen1 == testFileName1.Length());
   338 					TPtrC16 fileNamePtr1 (fileName1, fileNameLen1);
   339 					test(fileName1 != NULL);
   340 					test(testFileName1.Compare(fileNamePtr1) == 0);
   341 
   342 					test(fileNameLen2 == testFileName2.Length());
   343 					TPtrC16 fileNamePtr2 (fileName2, fileNameLen2);
   344 					test(fileName2 != NULL);
   345 					test(testFileName2.Compare(fileNamePtr2) == 0);
   346 
   347 					funcInFound = ETrue;
   348 					}
   349 				else if (data1 == UTraceModuleEfsrv::EFsRenameReturn)
   350 					{
   351 					TInt retCode = (size>8) ? *(TUint32*)(data+8) : 0;
   352 
   353 					test(retCode == KErrNone);
   354 
   355 					funcOutFound = ETrue;
   356 					}
   357 				}
   358 
   359 			record = BTrace::NextRecord(record);
   360 			}
   361 		Trace.DataUsed();
   362 		}
   363 
   364 
   365 	test (funcInFound);
   366 	test (funcOutFound);
   367 
   368 	TheFs.Delete(testFileName1);
   369 	TheFs.Delete(testFileName2);
   370 	}
   371 
   372 void CallTestsL()
   373 	{
   374 
   375 // By default, file server trace-points are only compiled in in debug mode
   376 #if defined(_DEBUG)
   377 	test.Title();
   378 	TInt r;
   379 
   380 	test.Start(_L("Open LDD"));
   381 	r = Trace.Open();
   382 	test(r == KErrNone);
   383 
   384 
   385 	TUint32 OldTraceFilter[8] = {0};
   386 
   387 	TUint savedMode = Trace.Mode();
   388 	SetBTraceFilter(OldTraceFilter,OldTraceFilter);
   389 
   390 	Trace.ResizeBuffer(0x100000);
   391 	Trace.Empty();
   392 
   393 	Trace.SetMode(RBTrace::EEnable | RBTrace::EFreeRunning);
   394 
   395 	TBool b;
   396 //	b = Trace.SetFilter(BTrace::EThreadIdentification, ETrue);
   397 //	test(b >= 0);
   398 	b = Trace.SetFilter(UTF::EPanic, ETrue);
   399 	test(b >= 0);
   400 	b = Trace.SetFilter(UTF::EError, ETrue);
   401 	test(b >= 0);
   402 	b = Trace.SetFilter(UTF::EBorder, ETrue);
   403 	test(b >= 0);
   404 
   405 	b = Trace.SetFilter2(EF32TraceUidEfsrv, ETrue);
   406 	test(b >= 0);
   407 
   408 	TestRFileReplace();
   409 	TestRFsRename();
   410 
   411 	// restore trace settings...
   412 	Trace.SetMode(0);
   413 	SetBTraceFilter(OldTraceFilter,OldTraceFilter);
   414 	Trace.SetMode(savedMode);
   415 
   416 
   417 	test.Next(_L("Close LDD"));
   418 	Trace.Close();
   419 
   420 	test.End();
   421 #endif
   422 	}
   423