os/kernelhwsrv/kerneltest/e32utils/trace/btrace.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2005-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 // e32utils\trace\btrace.cpp
    15 // 
    16 //
    17 
    18 #include <e32cons.h>
    19 #include <f32file.h>
    20 #include <d32btrace.h>
    21 
    22 CConsoleBase* Console;
    23 extern RBTrace Trace;
    24 
    25 #undef ASSERT
    26 #define ASSERT(c) (void)((c)||(AssertFailed(__LINE__)))
    27 
    28 TInt AssertFailed(TInt aLine)
    29 	{
    30 	_LIT(KPanicCategory,"ASSERT");
    31 	User::Panic(KPanicCategory,aLine);
    32 	return 0;
    33 	}
    34 
    35 
    36 int strlen(const char* string)
    37 	{
    38 	int len=0;
    39 	while(string[len]) ++len;
    40 	return len;
    41 	}
    42 
    43 
    44 class TDesTruncate : public TDes8Overflow { void Overflow(TDes8&) {}; } IgnoreOverflow;
    45 
    46 void printf(const char* aFormat,...)
    47 	{
    48 	TPtrC8 formatDes((TUint8*)aFormat,strlen(aFormat));
    49 	VA_LIST list;
    50 	VA_START(list, aFormat);
    51 	TBuf16<0x100> buffer;
    52 	// coverity[uninit_use_in_call]
    53 	((TDes8&)buffer).AppendFormatList(formatDes, list, &IgnoreOverflow);
    54 	Console->Write(((TDes8&)buffer).Expand());
    55 	}
    56 
    57 
    58 TInt getch()
    59 	{
    60 	TRequestStatus keyStat;
    61 	Console->Read(keyStat);
    62 	User::WaitForRequest(keyStat);
    63 	return Console->KeyCode();
    64 	}
    65 
    66 
    67 TInt getch(TInt aTimeout)
    68 	{
    69 	TRequestStatus keyStat;
    70 	Console->Read(keyStat);
    71 	RTimer timer;
    72 	TInt r=timer.CreateLocal();
    73 	ASSERT(r==KErrNone);
    74 	TRequestStatus timerStat;
    75 	timer.After(timerStat,aTimeout*1000000);
    76 	User::WaitForRequest(timerStat,keyStat);
    77 	TInt key = -1;
    78 	if(keyStat!=KRequestPending)
    79 		key = Console->KeyCode();
    80 	timer.Cancel();
    81 	Console->ReadCancel();
    82 	User::WaitForAnyRequest();
    83 	return key;
    84 	}
    85 
    86 
    87 int Main(int argc, char** argv);
    88 
    89 
    90 void exit_btrace(int result)
    91 	{
    92 	User::Exit(result);
    93 	}
    94 
    95 
    96 TInt E32Main()
    97 	{
    98 	// create console...
    99 	TFileName exeFile = RProcess().FileName();
   100 	TRAPD(r, Console = Console::NewL(exeFile,TSize(KConsFullScreen,KConsFullScreen)));
   101 	ASSERT(r==KErrNone);
   102 
   103 	// get command-line...
   104 	RBuf clDes;
   105 	ASSERT(clDes.Create(User::CommandLineLength()+1)==KErrNone);
   106 	User::CommandLine(clDes);
   107 	char* cl = (char*)clDes.Collapse().PtrZ(); // convert to null terminated C string
   108 
   109 	// split up args...
   110 	RPointerArray<TAny> argArray;
   111 	ASSERT(KErrNone==argArray.Append(exeFile.Collapse().Ptr())); // first arg is program name
   112 	for(;;)
   113 		{
   114 		while((unsigned)(*cl-1)<(unsigned)' ') ++cl; // skip whitespace
   115 		if(!*cl) break;
   116 		ASSERT(KErrNone==argArray.Append(cl));
   117 		while(*++cl>' ') {}; // skip non-whitespace
   118 		if(!*cl) break;
   119 		*cl++ = 0; // add null terminator to arg
   120 		}
   121 
   122 	// call main...
   123 	return Main(argArray.Count(),(char**)&argArray[0]);
   124 	}
   125 
   126 
   127 int Error()
   128 	{
   129 	getch(5); // pause for a while
   130 	exit_btrace(-1);
   131 	return 0;
   132 	}
   133 
   134 
   135 char* FileName = 0;
   136 char* PrimaryFilterArg = NULL;
   137 bool SetFilter2 = false;
   138 RArray<TUint32> Filter2;
   139 bool SetMode = false;
   140 unsigned Mode = 0;
   141 bool SetBufferSize = false;
   142 unsigned BufferSize = 0;
   143 bool DumpToDebugPort = false;
   144 TInt AnalysisLevel = 0;
   145 bool Analyse = false;
   146 bool DiscardOldData = true;
   147 
   148 RFs Fs;
   149 RFile File;
   150 void RePrime();
   151 
   152 void Dump()
   153 	{
   154 	TUint oldMode = Trace.Mode();
   155 	Trace.SetMode(0); // turn off trace capture while we dump
   156 
   157 	TUint8* data;
   158 	TInt size;
   159 	while((size=Trace.GetData(data))!=0)
   160 		{
   161 		if(FileName)
   162 			{
   163 			TInt r=File.Write(TPtrC8(data,size));
   164 			if(r!=KErrNone)
   165 				{
   166 				printf("Error writing to file (1). (Code %d)",r);
   167 				Error();
   168 				}
   169 			}
   170 		if(DumpToDebugPort)
   171 			{
   172 			do
   173 				{
   174 				int s = size;
   175 				if(s>256) s=256;
   176 				RDebug::RawPrint(TPtrC8(data,s));
   177 				data += s;
   178 				size -= s;
   179 				}
   180 			while(size);
   181 			}
   182 		Trace.DataUsed();
   183 		}
   184 	// Flush the file here so we can be sure to detect whether the btrace data 
   185 	// has been written successfully to the file.
   186 	if (FileName)
   187 		{
   188 		// Flush the file here so we can be sure to detect whether the btrace data 
   189 		// has been written successfully to the file.
   190 		TInt r = File.Flush();
   191 		if (r != KErrNone)
   192 			{
   193 			printf("Error writing to file (2). (Code %d)",r);
   194 			Error();
   195 			}
   196 		File.Close();
   197 		}
   198 
   199 	Trace.SetMode(oldMode);
   200 	RePrime();
   201 	}
   202 
   203 bool SetFilter(char* args);
   204 void DoAnalyse(TInt aAnalysisLevel);
   205 
   206 int DoCommand()
   207 	{
   208 
   209 	if(SetBufferSize)
   210 		Trace.ResizeBuffer(BufferSize*1024);
   211 
   212 	if(SetMode)
   213 		Trace.SetMode(Mode);
   214 
   215 	if (PrimaryFilterArg)
   216 		SetFilter(PrimaryFilterArg);
   217 
   218 	if(SetFilter2)
   219 		{
   220 		TInt r = Trace.SetFilter2(&Filter2[0], Filter2.Count());
   221 		if(r<0)
   222 			{
   223 			printf("Error setting secondary filter. (%d)",r);
   224 			Error();
   225 			}
   226 		if (DiscardOldData)
   227 			Trace.Empty(); // discard old data
   228 		}
   229 
   230 	if(FileName || DumpToDebugPort)
   231 		Dump();
   232 
   233 	if(Analyse)
   234 		DoAnalyse(AnalysisLevel);
   235 
   236 	return 0;
   237 	}
   238 
   239 
   240 unsigned int ParseDecimal(char*& args)
   241 	{
   242 	unsigned int i=0;
   243 	unsigned int d;
   244 	while((d=*args-'0')<10u)
   245 		{
   246 		++args;
   247 		i = i*10+d;
   248 		}
   249 	return i;
   250 	}
   251 
   252 
   253 bool SetFilter(char* args)
   254 	{
   255 	unsigned i;
   256 	// Turn everything off and discard old data
   257 	for(i=0; i<256; i++)
   258 		{			
   259 		Trace.SetFilter(i,0); 
   260 		}
   261 	if (DiscardOldData)
   262 		Trace.Empty();
   263 
   264 	// Iterate over comma-seperated filter numbers
   265 	bool set_metatrace = false;
   266 	while((unsigned)(*args-'0')<10u)
   267 		{
   268 		unsigned int i = ParseDecimal(args);
   269 		if(i>=256)
   270 			{
   271 			printf("Primary filter value out of range");
   272 			Error();
   273 			}
   274 		if (!set_metatrace) 
   275 			{
   276 			set_metatrace = true;
   277 			Trace.SetFilter(BTrace::EMetaTrace,1);
   278 			}
   279 		Trace.SetFilter(i,1);
   280 		if(*args==',')
   281 			++args;
   282 		}
   283 	return (*args==0);
   284 	}
   285 
   286 
   287 bool ParseFilter2(char* args)
   288 	{
   289 	while((unsigned)(*args-'0') < 10u)
   290 		{
   291 		unsigned int i = ParseDecimal(args);
   292 		TInt r = Filter2.Append(i);
   293 		if(r<0)
   294 			{
   295 			printf("Error parsing secondary filter. (%d)",r);
   296 			Error();
   297 			}
   298 		if(*args==',')
   299 			{
   300 			++args;
   301 			}
   302 		}
   303 	return SetFilter2=(*args==0);
   304 	}
   305 
   306 
   307 bool ParseMode(char* args)
   308 	{
   309 	Mode = ParseDecimal(args);
   310 	return SetMode=(*args==0);
   311 	}
   312 
   313 
   314 bool ParseBufferSize(char* args)
   315 	{
   316 	BufferSize = ParseDecimal(args);
   317 	return SetBufferSize=(*args==0);
   318 	}
   319 
   320 
   321 bool ParseAnalyse(char* args)
   322 	{
   323 	AnalysisLevel = ParseDecimal(args);
   324 	return Analyse=(*args==0);
   325 	}
   326 
   327 
   328 int CreateFile(char* name)
   329 	{
   330 	if(FileName)
   331 		{
   332 		printf("Too many arguments");
   333 		return Error();
   334 		}
   335 
   336 	FileName = name;
   337 	unsigned nameLen = strlen(name);
   338 	if(nameLen>(unsigned)KMaxFileName)
   339 		{
   340 		printf("File name too long.\n");
   341 		return Error();
   342 		}
   343 
   344 	TInt r;
   345 	if(!Fs.Handle())
   346 		{
   347 		r = Fs.Connect();
   348 		if(r!=KErrNone)
   349 			{
   350 			printf("Couldn't connect to file server. (%d)\n",r);
   351 			return Error();
   352 			}
   353 		}
   354 
   355 	TBuf8<KMaxFileName*2> fn = TPtrC8((TUint8*)name,nameLen);
   356 	r = File.Replace(Fs,fn.Expand(),EFileWrite);
   357 	if(r!=KErrNone)
   358 		{
   359 		printf("Couldn't create file: %s. (%d)\n",name,r);
   360 		return Error();
   361 		}
   362 
   363 	return 0;
   364 	}
   365 
   366 
   367 int Help()
   368 	{
   369 	printf("Usage: BTRACE [options] [filename]\n");
   370 	printf("\n");
   371 	printf("Options:\n");
   372 	printf("-fLIST   Set primary filter to a LIST of comma separated category numbers.\n");
   373 	printf("         This argument may be used more than once e.g. -f1,22,3 -f44 \n");
   374 	printf("-sLIST   Set secondary filter to a LIST of comma separated UID values.\n");
   375 	printf("         This argument may be used more than once e.g. -s1221,22,343243 -s3242344 \n");
   376 	printf("-mN      Set capture mode to value N (See RBTrace::TMode)\n");
   377 	printf("-bN      Set capture buffer size to N kBytes\n");
   378 	printf("-d       Dump contents of trace buffer to debug port\n");
   379 	printf("-tsNAME  Output a test measurement start trace with text NAME. This text\n");
   380 	printf("         may be between 0 and 80 non-whitespace characters.\n");
   381 	printf("-te      Output a test measurement end trace\n");
   382 	printf("-aLEVEL  Analyse trace buffer and produce a report. UNSUPPORTED!\n");
   383 	printf("-k       Keep old contents of trace buffer when enabling a new filter\n");
   384 	printf("filename File to dump contents of trace buffer to.\n");
   385 	getch();
   386 	return 0;
   387 	}
   388 
   389 
   390 int Main(int argc, char** argv)
   391 	{
   392 	if(argc<=1)
   393 		return Help();
   394 
   395 	TInt r = Trace.Open();
   396 	if(r!=KErrNone)
   397 		{
   398 		printf("Couldn't open BTrace driver. (Code %d)",r);
   399 		Error();
   400 		return r;
   401 		}
   402 
   403 	char* a;
   404 	while(--argc)
   405 		{
   406 		a = *++argv;
   407 		if(a[0]!='-')
   408 			{
   409 			CreateFile(a);
   410 			continue;
   411 			}
   412 
   413 		int r=0;
   414 		switch(a[1])
   415 			{
   416 		case 'f': PrimaryFilterArg = a+2; r = true; break;
   417 		case 's': r=ParseFilter2(a+2); break;
   418 		case 'm': r=ParseMode(a+2); break;
   419 		case 'b': r=ParseBufferSize(a+2); break;
   420 		case 'd': r=a[2]==0; DumpToDebugPort = true; break;
   421 		case 't': 
   422 			{
   423 			if (a[2]=='s') 
   424 				{
   425 				TUint cch=strlen(a);
   426 				if (cch > KMaxBTraceDataArray)
   427 					cch = KMaxBTraceDataArray;
   428 				int buff[KMaxBTraceDataArray/sizeof(int)];
   429 				a+=3;
   430 				memcpy(buff, a, cch);
   431 				BTraceContextN(BTrace::EMetaTrace, BTrace::EMetaTraceMeasurementStart, 0,0, buff, cch);
   432 				r=1;
   433 				}
   434 			else if (a[2]=='e')
   435 				{
   436 				BTraceContext8(BTrace::EMetaTrace, BTrace::EMetaTraceMeasurementEnd, 0,0);
   437 				r=1;
   438 				}
   439 			}
   440 			break;
   441 		case 'a': r=ParseAnalyse(a+2); break;
   442 		case 'k': DiscardOldData = false; r = true; break;
   443 		default: break;
   444 			}
   445 
   446 		if(!r)
   447 			{
   448 			printf("Bad command line argument: %s",a);
   449 			Error();
   450 			}
   451 		}
   452 
   453 	return DoCommand();
   454 	}
   455