commands/btrace/btrace.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // btrace.cpp
       
     2 // 
       
     3 // Copyright (c) 2007 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 // 
       
     9 // Initial Contributors:
       
    10 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include "btrace.h"
       
    14 
       
    15 #if defined(FSHELL_MEMORY_ACCESS_SUPPORT)
       
    16 #define TEXT_TRACE_MODE_SUPPORTED
       
    17 #endif
       
    18 
       
    19 const TInt KMaxCategories = 256;
       
    20 const TUint KPrimingStopped = 0x102864BC;
       
    21 
       
    22 
       
    23 
       
    24 CCommandBase* CCmdBtrace::NewLC()
       
    25 	{
       
    26 	CCmdBtrace* self = new(ELeave) CCmdBtrace();
       
    27 	CleanupStack::PushL(self);
       
    28 	self->ConstructL();
       
    29 	return self;
       
    30 	}
       
    31 
       
    32 CCmdBtrace::~CCmdBtrace()
       
    33 	{
       
    34 	iPrimaryFilters.Close();
       
    35 	iSecondaryFilters.Close();
       
    36 	iBtrace.Close();
       
    37 	}
       
    38 
       
    39 CCmdBtrace::CCmdBtrace()
       
    40 	: iThreshold(50)
       
    41 	{
       
    42 	}
       
    43 
       
    44 void CCmdBtrace::ConstructL()
       
    45 	{
       
    46 	BaseConstructL();
       
    47 	}
       
    48 
       
    49 const TDesC& CCmdBtrace::Name() const
       
    50 	{
       
    51 #ifdef BUILDING_ATRACE
       
    52 	_LIT(KName, "atrace");
       
    53 #else
       
    54 	_LIT(KName, "btrace");
       
    55 #endif
       
    56 	return KName;
       
    57 	}
       
    58 
       
    59 #define CASE_LIT(x) case BTrace:: x : { _LIT(KName, #x); return &KName; }
       
    60 #define CASE_REALLY_LIT(x) case x : { _LIT(KName, #x); return &KName; }
       
    61 
       
    62 const TDesC* BtraceCategoryName(TInt aCategory)
       
    63 	{
       
    64 	switch (aCategory)
       
    65 		{
       
    66 		CASE_LIT(ERDebugPrintf)
       
    67 		CASE_LIT(EKernPrintf)
       
    68 		CASE_LIT(EPlatsecPrintf)
       
    69 		CASE_LIT(EThreadIdentification)
       
    70 		CASE_LIT(ECpuUsage)
       
    71 		CASE_LIT(EKernPerfLog)
       
    72 		CASE_LIT(EClientServer)
       
    73 		CASE_LIT(ERequests)
       
    74 		CASE_LIT(EChunks)
       
    75 		CASE_LIT(ECodeSegs)
       
    76 		CASE_LIT(EPaging)
       
    77 		CASE_LIT(EThreadPriority)
       
    78 		CASE_LIT(EPagingMedia)
       
    79 		CASE_LIT(EKernelMemory)
       
    80 		CASE_LIT(EHeap)
       
    81 		CASE_LIT(EMetaTrace)
       
    82 		CASE_LIT(ERamAllocator)
       
    83 		CASE_LIT(EFastMutex)
       
    84 		CASE_LIT(EProfiling)
       
    85 		CASE_REALLY_LIT(ExtraBTrace::EPubSub)
       
    86 		CASE_REALLY_LIT(ExtraBTrace::EFfsTrace)
       
    87 		CASE_LIT(EPlatformSpecificFirst)
       
    88 		CASE_LIT(EPlatformSpecificLast)
       
    89 		CASE_LIT(ESymbianExtentionsFirst)
       
    90 		CASE_LIT(ESymbianExtentionsLast)
       
    91 		CASE_LIT(ETest1)
       
    92 		CASE_LIT(ETest2)
       
    93 		default:
       
    94 			{
       
    95 			_LIT(KUnknown, "?");
       
    96 			return &KUnknown;
       
    97 			}
       
    98 		}
       
    99 	}
       
   100 
       
   101 void CCmdBtrace::DoRunL()
       
   102 	{
       
   103 #ifdef TEXT_TRACE_MODE_SUPPORTED
       
   104 	if (iOptions.IsPresent(&iTextTraceMode))
       
   105 		{
       
   106 		LoadMemoryAccessL();
       
   107 		const TInt ESerialOutMask = 3; // As per kernel.h
       
   108 		TInt err = iMemAccess.SetTextTraceMode(iTextTraceMode, ESerialOutMask);
       
   109 		LeaveIfErr(err, _L("Couldn't set text trace mode"));
       
   110 		}
       
   111 #endif
       
   112 
       
   113 	TInt i;
       
   114 	LeaveIfErr(iBtrace.Open(), _L("Couldn't open RBTrace"));
       
   115 
       
   116 	if (iOptions.IsPresent(&iReset))
       
   117 		{
       
   118 		ResetL();
       
   119 		}
       
   120 
       
   121 	if (iOptions.IsPresent(&iBufferSize))
       
   122 		{
       
   123 		iBtrace.ResizeBuffer(iBufferSize * 1024);
       
   124 		}
       
   125 
       
   126 	if (iOptions.IsPresent(&iMode))
       
   127 		{
       
   128 		SetMode(iMode);
       
   129 		}
       
   130 
       
   131 	iBtrace.SetTimestamp2Enabled(iEnableTimestamp2);
       
   132 
       
   133 	const TInt numPrimaryFilters = iPrimaryFilters.Count();
       
   134 	if (numPrimaryFilters > 0)
       
   135 		{
       
   136 		RemovePrimaryFilters();
       
   137 		EmptyBuffer();
       
   138 
       
   139 		// Enable the specified categories.
       
   140 		for (i = 0; i < numPrimaryFilters; ++i)
       
   141 			{
       
   142 			iBtrace.SetFilter(iPrimaryFilters[i], 1);
       
   143 			}
       
   144 		}
       
   145 	if (iTest)
       
   146 		{
       
   147 		TestL();
       
   148 		return;
       
   149 		}
       
   150 
       
   151 	const TInt numSecondaryFilters = iSecondaryFilters.Count();
       
   152 	if (numSecondaryFilters > 0)
       
   153 		{
       
   154 		TUint32* filterArray = new(ELeave) TUint32[iSecondaryFilters.Count()];
       
   155 		for (i = 0; i < numSecondaryFilters; ++i)
       
   156 			{
       
   157 			filterArray[i] = iSecondaryFilters[i];
       
   158 			}
       
   159 
       
   160 		TInt err = iBtrace.SetFilter2(filterArray, numSecondaryFilters);
       
   161 		if (err)
       
   162 			{
       
   163 			PrintError(err, _L("Couldn't set secondary filter"));
       
   164 			}
       
   165 		else
       
   166 			{
       
   167 			// Discard old data.
       
   168 			EmptyBuffer();
       
   169 			}
       
   170 
       
   171 		delete[] filterArray;
       
   172 		}
       
   173 
       
   174 	if (numPrimaryFilters > 0 || numSecondaryFilters > 0) 
       
   175 		{
       
   176 		BTrace8(BTrace::EMetaTrace, BTrace::EMetaTraceMeasurementEnd, KPrimingStopped, KPrimingStopped);
       
   177 		}
       
   178 
       
   179 	if (iFileName.Length())
       
   180 		{
       
   181 		RFile file;
       
   182 		User::LeaveIfError(file.Replace(FsL(), iFileName, EFileWrite));
       
   183 		file.Close();
       
   184 		}
       
   185 
       
   186 	if (iFileName.Length() || iDumpToRdebug)
       
   187 		{
       
   188 		if (iFollow)
       
   189 			{
       
   190 			if (iThreshold > 100)
       
   191 				{
       
   192 				PrintError(KErrArgument, _L("Invalid threshold percentage"));
       
   193 				User::Leave(KErrArgument);
       
   194 				}
       
   195 			Printf(_L("Hit any key to abort waiting and flush buffer...\r\n"));
       
   196 			TInt bytesRequired = (iBtrace.BufferSize() * iThreshold) / 100;
       
   197 			TBool finished(EFalse);
       
   198 			while (!finished)
       
   199 				{
       
   200 				TRequestStatus btraceStatus;
       
   201 				iBtrace.RequestData(btraceStatus, bytesRequired);
       
   202 				TRequestStatus keyStatus;
       
   203 				Stdin().WaitForKey(keyStatus);
       
   204 				if (iVerbose)
       
   205 					{
       
   206 					Printf(_L("Waiting for %d bytes in buffer\r\n"), bytesRequired);
       
   207 					}
       
   208 				User::WaitForRequest(btraceStatus, keyStatus);
       
   209 				if (keyStatus == KRequestPending)
       
   210 					{
       
   211 					Stdin().WaitForKeyCancel();
       
   212 					User::WaitForRequest(keyStatus);
       
   213 					User::LeaveIfError(btraceStatus.Int());
       
   214 					}
       
   215 				else
       
   216 					{
       
   217 					iBtrace.CancelRequestData();
       
   218 					User::WaitForRequest(btraceStatus);
       
   219 					finished = ETrue;
       
   220 					}
       
   221 
       
   222 				DumpL();
       
   223 				}
       
   224 			}
       
   225 		else
       
   226 			{
       
   227 			DumpL();
       
   228 			}
       
   229 		}
       
   230 
       
   231 	if (iVerbose || (iFileName.Length() == 0))
       
   232 		{
       
   233 		Printf(_L("Buffer size: %d\r\n"), iBtrace.BufferSize());
       
   234 		Printf(_L("Mode: %S (%u)\r\n"), ModeText(iBtrace.Mode()), iBtrace.Mode());
       
   235 #ifdef TEXT_TRACE_MODE_SUPPORTED
       
   236 		LoadMemoryAccessL();
       
   237 		TUint traceMode = 0;
       
   238 		TInt err = iMemAccess.SetTextTraceMode(traceMode, 0);
       
   239 		if (err)
       
   240 			{
       
   241 			PrintWarning(_L("Couldn't read text trace mode: err=%d"), err);
       
   242 			}
       
   243 		else
       
   244 			{
       
   245 			_LIT(KHuh, "<?>");
       
   246 			const TDesC* name = &KHuh;
       
   247 #define CASE_LIT2(x, y) case x: { _LIT(KName, #y); name = &KName; break; }
       
   248 			switch (traceMode)
       
   249 				{
       
   250 				CASE_LIT2(0, ESerialOutDefault)
       
   251 				CASE_LIT2(1, ESerialOutNever)
       
   252 				CASE_LIT2(2, ESerialOutAlways)
       
   253 				default:
       
   254 					break;
       
   255 				}
       
   256 			Printf(_L("Text trace mode: %S (%d)\r\n"), name, traceMode);
       
   257 			}
       
   258 #endif
       
   259 		Printf(_L("Primary filters:\r\n"));
       
   260 		TBool foundOne(EFalse);
       
   261 		for (TInt i = 0; i < KMaxCategories; ++i)
       
   262 			{
       
   263 			if (iBtrace.Filter(i) == 1)
       
   264 				{
       
   265 				Printf(_L("\t%d (%S)\r\n"), i, BtraceCategoryName(i));
       
   266 				foundOne = ETrue;
       
   267 				}
       
   268 			}
       
   269 		if (!foundOne)
       
   270 			{
       
   271 			Write(_L("\tnone\r\n"));
       
   272 			}
       
   273 		Printf(_L("Secondary filters: "));
       
   274 		TInt globalFilter;
       
   275 		TUint32* uids;
       
   276 		TInt ret = iBtrace.Filter2(uids, globalFilter);
       
   277 		if (ret >= 0)
       
   278 			{
       
   279 			if (globalFilter)
       
   280 				{
       
   281 				Printf(_L("[Secondary filters accept]\r\n"));
       
   282 				}
       
   283 			else
       
   284 				{
       
   285 				Printf(_L("[Secondary filters reject]\r\n"));
       
   286 				}
       
   287 			if (ret > 0)
       
   288 				{
       
   289 				for (TInt i = 0; i < ret; ++i)
       
   290 					{
       
   291 					Printf(_L("\t0x%08x\r\n"), uids[i]);
       
   292 					}
       
   293 				}
       
   294 			else
       
   295 				{
       
   296 				Write(_L("\tnone\r\n"));
       
   297 				}
       
   298 			delete uids;
       
   299 			}
       
   300 		else
       
   301 			{
       
   302 			PrintError(ret, _L("Unable to read secondary filters"));
       
   303 			}
       
   304 		}
       
   305 	}
       
   306 
       
   307 void CCmdBtrace::DumpL()
       
   308 	{
       
   309 	RFile file;
       
   310 	if (iFileName.Length())
       
   311 		{
       
   312 		if (iVerbose)
       
   313 			{
       
   314 			Printf(_L("Opening \"%S\"\r\n"), &iFileName);
       
   315 			}
       
   316 		User::LeaveIfError(file.Open(FsL(), iFileName, EFileWrite));
       
   317 		CleanupClosePushL(file);
       
   318 		TInt pos = 0;
       
   319 		User::LeaveIfError(file.Seek(ESeekEnd, pos));
       
   320 		}
       
   321 
       
   322 	TUint8* data;
       
   323 	TInt size;
       
   324 	TInt err = KErrNone;
       
   325 	while ((size = iBtrace.GetData(data)) != 0)
       
   326 		{
       
   327 		if (file.SubSessionHandle() && (err == KErrNone))
       
   328 			{
       
   329 			if (iVerbose)
       
   330 				{
       
   331 				Printf(_L("Writing %d bytes to \"%S\"\r\n"), size, &iFileName);
       
   332 				}
       
   333 			err = file.Write(TPtrC8(data,size));
       
   334 			if (err)
       
   335 				{
       
   336 				PrintError(err, _L("Couldn't write to file"));
       
   337 				}
       
   338 			}
       
   339 
       
   340 		if (iDumpToRdebug)
       
   341 			{
       
   342 			if (iVerbose)
       
   343 				{
       
   344 				Printf(_L("Writing %d bytes to LtkUtils::HexDumpToRDebug\r\n"), size);
       
   345 				}
       
   346 			LtkUtils::HexDumpToRDebug(TPtrC8(data, size));
       
   347 			}
       
   348 
       
   349 		iBtrace.DataUsed();
       
   350 		}
       
   351 
       
   352 	User::LeaveIfError(err);
       
   353 	if (iFileName.Length())
       
   354 		{
       
   355 		CleanupStack::PopAndDestroy(&file);
       
   356 		}
       
   357 	}
       
   358 
       
   359 void CCmdBtrace::ArgumentsL(RCommandArgumentList& aArguments)
       
   360 	{
       
   361 	_LIT(KArgFiles, "file_name");
       
   362 	aArguments.AppendFileNameL(iFileName, KArgFiles);
       
   363 	}
       
   364 
       
   365 void CCmdBtrace::OptionsL(RCommandOptionList& aOptions)
       
   366 	{
       
   367 	_LIT(KOptPrimaryFilter, "filter");
       
   368 	aOptions.AppendUintL(iPrimaryFilters, KOptPrimaryFilter);
       
   369 
       
   370 	_LIT(KOptSecondaryFilter, "secondary");
       
   371 	aOptions.AppendUintL(iSecondaryFilters, KOptSecondaryFilter);
       
   372 
       
   373 	_LIT(KOptMode, "mode");
       
   374 	aOptions.AppendUintL(iMode, KOptMode);
       
   375 
       
   376 	_LIT(KOptBufferSize, "buffer");
       
   377 	aOptions.AppendUintL(iBufferSize, KOptBufferSize);
       
   378 
       
   379 	_LIT(KOptDump, "dump");
       
   380 	aOptions.AppendBoolL(iDumpToRdebug, KOptDump);
       
   381 
       
   382 	_LIT(KOptFollow, "follow");
       
   383 	aOptions.AppendBoolL(iFollow, KOptFollow);
       
   384 
       
   385 	_LIT(KOptThreshold, "threshold");
       
   386 	aOptions.AppendUintL(iThreshold, KOptThreshold);
       
   387 
       
   388 	_LIT(KOptTimestamp2, "timestamp2");
       
   389 	aOptions.AppendBoolL(iEnableTimestamp2, KOptTimestamp2);
       
   390 
       
   391 	_LIT(KOptVerbose, "verbose");
       
   392 	aOptions.AppendBoolL(iVerbose, KOptVerbose);
       
   393 
       
   394 	_LIT(KOptTest, "test");
       
   395 	aOptions.AppendBoolL(iTest, KOptTest);
       
   396 
       
   397 	_LIT(KOptReset, "reset");
       
   398 	aOptions.AppendBoolL(iReset, KOptReset);
       
   399 
       
   400 #ifdef TEXT_TRACE_MODE_SUPPORTED
       
   401 	_LIT(KOptTextTraceMode, "text-trace-mode");
       
   402 	aOptions.AppendUintL(iTextTraceMode, KOptTextTraceMode);
       
   403 #endif
       
   404 	}
       
   405 
       
   406 #ifdef EXE_BUILD
       
   407 EXE_BOILER_PLATE(CCmdBtrace)
       
   408 #endif
       
   409 
       
   410 void CCmdBtrace::TestL()
       
   411 	{
       
   412 	SetMode(RBTrace::EEnable);
       
   413 	EmptyBuffer();
       
   414 	iBtrace.SetFilter(BTrace::ESymbianExtentionsFirst, 1);
       
   415 	_LIT8(KHello, "Hello world of btrace!!!");
       
   416 	TInt res = BTraceBig(BTrace::ESymbianExtentionsFirst, 0, 0, KHello().Ptr(), KHello().Size());
       
   417 	Printf(_L8("Writing text '%S' to btrace returned %d...\r\n"), &KHello, res);
       
   418 
       
   419 	TUint8* data;
       
   420 	TInt size;
       
   421 	TInt err = KErrNotFound;
       
   422 	while ((size = iBtrace.GetData(data)) != 0 && err == KErrNotFound)
       
   423 		{
       
   424 		Printf(_L("Read a buffer of size %d\r\n"), size);
       
   425 		TPtrC8 ptr(data, size);
       
   426 		if (ptr.Find(KHello) >= 0)
       
   427 			{
       
   428 			Printf(_L("Found text ok.\r\n"));
       
   429 			err = KErrNone;
       
   430 			}
       
   431 		}
       
   432 	if (err == KErrNotFound)
       
   433 		{
       
   434 		Printf(_L("Failed to find the test data\r\n"));
       
   435 		}
       
   436 	}
       
   437 
       
   438 void CCmdBtrace::ResetL()
       
   439 	{
       
   440 	SetMode(0);
       
   441 	EmptyBuffer();
       
   442 	RemovePrimaryFilters();
       
   443 	RemoveSecondaryFiltersL();
       
   444 	}
       
   445 
       
   446 void CCmdBtrace::SetMode(TUint aMode)
       
   447 	{
       
   448 	if (iVerbose)
       
   449 		{
       
   450 		Printf(_L("Setting mode to \"%S\" (%u)\r\n"), ModeText(aMode), aMode);
       
   451 		}
       
   452 
       
   453 	iBtrace.SetMode(aMode);
       
   454 	}
       
   455 
       
   456 void CCmdBtrace::EmptyBuffer()
       
   457 	{
       
   458 	if (iVerbose)
       
   459 		{
       
   460 		Write(_L("Discarding contents of trace buffer...\r\n"));
       
   461 		}
       
   462 
       
   463 	iBtrace.Empty();
       
   464 	}
       
   465 
       
   466 void CCmdBtrace::RemovePrimaryFilters()
       
   467 	{
       
   468 	if (iVerbose)
       
   469 		{
       
   470 		Write(_L("Removing all primary filters...\r\n"));
       
   471 		}
       
   472 
       
   473 	for (TInt i = 0; i < KMaxCategories; ++i)
       
   474 		{
       
   475 		iBtrace.SetFilter(i, 0);
       
   476 		}
       
   477 	}
       
   478 
       
   479 void CCmdBtrace::RemoveSecondaryFiltersL()
       
   480 	{
       
   481 	if (iVerbose)
       
   482 		{
       
   483 		Write(_L("Removing all secondary filters...\r\n"));
       
   484 		}
       
   485 
       
   486 	LeaveIfErr(iBtrace.SetFilter2((const TUint32*)NULL, 0), _L("Couldn't remove all secondary filters"));
       
   487 	}
       
   488 
       
   489 const TDesC* CCmdBtrace::ModeText(TUint aMode) const
       
   490 	{
       
   491 	_LIT(KDisabled, "disabled");
       
   492 	_LIT(KEnabled, "enabled");
       
   493 	_LIT(KFreeRunning, "enabled and free running");
       
   494 	_LIT(KUnknown, "unknown");
       
   495 
       
   496 	switch (aMode)
       
   497 		{
       
   498 		case 0:
       
   499 			return &KDisabled;
       
   500 		case RBTrace::EEnable:
       
   501 			return &KEnabled;
       
   502 		case RBTrace::EEnable | RBTrace::EFreeRunning:
       
   503 			return &KFreeRunning;
       
   504 		default:
       
   505 			return &KUnknown;
       
   506 		}
       
   507 	}
       
   508