os/kernelhwsrv/kerneltest/e32utils/analyse/analyse.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.
     1 // Copyright (c) 2000-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 //
    15 
    16 #include "analyse.h"
    17 #include "trace.h"
    18 #include "tracer.h"
    19 #include "distribution.h"
    20 #include "activity.h"
    21 #include "nonxip.h"
    22 
    23 #ifdef __MSVCDOTNET__
    24 #include <strstream>
    25 #include <iomanip>
    26 #else //!__MSVCDOTNET__
    27 #include <strstrea.h>
    28 #include <iomanip.h>
    29 #endif //__MSVCDOTNET__
    30 
    31 #include <ctype.h>
    32 
    33 Analyse::TAction Analyse::sAction;
    34 Analyse::TFormat Analyse::sFormat;
    35 Analyse::TPartition Analyse::sPartition;
    36 int Analyse::sOptions;
    37 std::vector<const char*> Analyse::sTraces;
    38 const char* Analyse::sRomFile;
    39 const char* Analyse::sThread;
    40 const char* Analyse::sDll;
    41 const char* Analyse::sFunction;
    42 unsigned Analyse::sBase;
    43 unsigned Analyse::sLim;
    44 unsigned Analyse::sBuckets = 100;
    45 unsigned Analyse::sBucketSize;
    46 double Analyse::sCutOff;
    47 unsigned Analyse::sBeginSample;
    48 unsigned Analyse::sEndSample = 0xffffffffu;
    49 
    50 
    51 NonXIP gNonXIP; // 
    52 
    53 //namespace {
    54 
    55 void PartitionByDll::File(const char* aName)
    56 	{
    57 	iCurrentFile = aName;
    58 	}
    59 
    60 bool PartitionByDll::Symbol(const char*, PC aPc, int)
    61 	{
    62 	bool is_added = false;
    63 	if (iCurrentFile)
    64 		{
    65 		if (iLastFile && Analyse::Match(iLastFile, iMatch))
    66 			{
    67 			Add(iLastFileAddress, aPc, iLastFile);
    68 			is_added = true;
    69 			}
    70 		iLastFile = iCurrentFile;
    71 		iLastFileAddress = aPc;
    72 		iCurrentFile = 0;
    73 		}
    74 	return is_added;
    75 	}
    76 
    77 
    78 PartitionByFunction::PartitionByFunction(const char* aFile, const char* aFunction)
    79 	:iFile(aFile), iFunction(aFunction), iActive(false)
    80 	{}
    81 
    82 void PartitionByFunction::File(const char* aName)
    83 	{
    84 	iActive = Analyse::Match(aName, iFile);
    85 	}
    86 
    87 bool PartitionByFunction::Symbol(const char* aSymbol, PC aPc, int aLength)
    88 	{
    89 	bool is_added = false;
    90 	if (iActive && Analyse::Match(aSymbol, iFunction))
    91 		{
    92 		Add(aPc, aPc + aLength, aSymbol);
    93 		is_added = true;
    94 		}
    95 	return is_added;
    96 	}
    97 
    98 
    99 
   100 class FindFunction : public SymbolFile::Parser
   101 	{
   102 public:
   103 	FindFunction(const char* aFile, const char* aFunction);
   104 	void File(const char* aName);
   105 	bool Symbol(const char* aName, PC aPc, int aLength);
   106 	void Done(PC aFirstPc=0, PC aLastPc=0, int aModuleId=0);
   107 private:
   108 	const char* iFile;
   109 	const char* iFunction;
   110 	bool iActive;
   111 public:
   112 	PC iPc;
   113 	int iLength;
   114 	};
   115 
   116 FindFunction::FindFunction(const char* aFile, const char* aFunction)
   117 	:iFile(aFile), iFunction(aFunction), iActive(false), iPc(0)
   118 	{}
   119 
   120 void FindFunction::File(const char* aName)
   121 	{
   122 	if (iPc == 0)
   123 		iActive = Analyse::Match(aName, iFile);
   124 	}
   125 
   126 bool FindFunction::Symbol(const char* aSymbol, PC aPc, int aLength)
   127 	{
   128 	bool is_added = false;
   129 	if (iPc == 0 && iActive && Analyse::Match(aSymbol, iFunction))
   130 		{
   131 		iPc = aPc;
   132 		iLength = aLength;
   133 		is_added = true;
   134 		}
   135 	return is_added;
   136 	}
   137 
   138 void FindFunction::Done(PC aFirstPc, PC aLastPc, int aModuleId)
   139 	{}
   140 
   141 //};	// local namepsace
   142 
   143 
   144 // entry point
   145 
   146 int main(int argc,char *argv[])
   147 	{
   148 	switch(Analyse::ProcessCommandLine(argc,argv))
   149 		{
   150 	case 1:
   151 		Analyse::ExplainUsage();
   152 		return 1;
   153 	case 2:
   154 		Analyse::ExplainConfigUsage();
   155 		return 1;
   156 		}
   157 	Analyse::Run();
   158 	return 0;
   159 	}
   160 
   161 // Class Analyse
   162 
   163 void Analyse::Information()
   164 	{
   165 	cout << "\nEPOC Profile Analyser   Version " << MajorVersion << '.' \
   166 		<< setw(2) << MinorVersion << "(build " << setw(3) << setfill('0') << Build \
   167 		<< ")\nCopyright (c) Symbian Limited 2000. All rights reserved.\n\n" << flush;
   168 	}
   169 
   170 void Analyse::ExplainUsage()
   171 	{
   172 	Information();
   173 	cout << "Usage:  Analyse [options] tracefile\n" \
   174 			" -h            display this information\n" \
   175 			" -l            generate a trace listing\n" \
   176 			" -p            generate a profile distribution\n" \
   177 			" -v            generate a activity trace\n" \
   178 			" -r <symbols>  supply a Rom symbol file\n" \
   179 			" -s<range>     restrict the profile to the samples specified\n" \
   180 			"               This is specified either as <start>-<end> or\n" \
   181 			"               as <start>+<length> in decimal\n" \
   182 			" -n            include NULL thread\n" \
   183 			" -t <thread>   profile threads matching the pattern\n" \
   184 			" -d <dll>      profile DLL (or EXE) matching the pattern\n" \
   185 			" -f <function> profile the function matching the pattern\n" \
   186 			" -a<range>     profile the address range specified\n" \
   187 			"               This is specified either as <start>-<end> or\n" \
   188 			"               as <start>+<length> in hexadecimal\n" \
   189 			" -bd           partition the profile by dll/exe\n" \
   190 			" -bf           partition the profile by function\n" \
   191 			" -bs<n>        partition the profile into buckets of size n\n" \
   192 			" -bn<n>        partition the profile into approx. n buckets\n" \
   193 			" -c<n>         set the cutoff value for discarding output\n" \
   194 			" -m...         setformat options:\n" \
   195 			"   p|s|x       use percentages/samples/excel for output\n" \
   196 			"   z           output zero values instead of blanks\n" \
   197 			"   t           do not show thread break-down\n" \
   198 			"   o           do not include the <other> bucket\n" \
   199 			" -z <rofs>     supply a ROFS symbol file\n" \
   200 			" -o <oby>      supply an OBY file\n" \
   201 			" -x <config>   supply a config file\n" \
   202 			" -h config     display an example of config file\n" \
   203 			<< flush;
   204 	}
   205 
   206 void Analyse::ExplainConfigUsage()
   207 	{
   208 	Information();
   209 	cout << "Example of config file:"		<< endl	<< endl;
   210 	cout << "[Common]"								<< endl;
   211 	cout << "TraceFile=PROFILER.DAT"				<< endl;
   212 	cout << "Mode=listing|profile|activity"			<< endl;
   213 	cout << "SymbolFile=core4r.bin.symbol"			<< endl;
   214 	cout << "Range=100-200 | 100+100"				<< endl;
   215 	cout << "IncludeNullThread=0|1"					<< endl;
   216 	cout << "[Profile]"								<< endl;
   217 	cout << "Thread="								<< endl;
   218 	cout << "Dll="									<< endl;
   219 	cout << "Function="								<< endl;
   220 	cout << "Range=1f1a+20 | 1f1a-1f3a"				<< endl;
   221 	cout << "[Partition]"							<< endl;
   222 	cout << "Mode=dll|function"						<< endl;
   223 	cout << "BucketSize="							<< endl;
   224 	cout << "NumberOfBuckets="						<< endl;
   225 	cout << "[Format]"								<< endl;
   226 	cout << "Mode=percentages|samples|excel"		<< endl;
   227 	cout << "ZeroValues=0|1"						<< endl;
   228 	cout << "NoOthers=0|1"							<< endl;
   229 	cout << "TotalOnly=0|1"							<< endl;
   230 	cout << "[NonXIP]"								<< endl;
   231 	cout << "ObyFile1=myrofs.oby"					<< endl;
   232 	cout << "RofsSymbolFile1=rofs.bin.symbol"		<< endl;
   233 	cout << flush;
   234 	}
   235 
   236 class Options
   237 	{
   238 	struct Entry
   239 		{
   240 		const char* iName;
   241 		int iOption;
   242 		};
   243 	const static Entry KOptions[];
   244 	static int Compare(const char* aLhs, const char* aRhs);
   245 public:
   246 	static int Get(istrstream& aStr);
   247 	};
   248 
   249 
   250 const Options::Entry Options::KOptions[] = 
   251 	{
   252 	{"activity",'v'},
   253 	{"address",	'a'},
   254 	{"by",		'b'},
   255 	{"cutoff",	'c'},
   256 	{"dll",		'd'},
   257 	{"excel",	'x'},
   258 	{"format",	'm'},
   259 	{"function",'f'},
   260 	{"help",	'h'},
   261 	{"listing",	'l'},
   262 	{"null",	'n'},
   263 	{"number",	'n'},
   264 	{"other",	'o'},
   265 	{"percent",	'p'},
   266 	{"profile",	'p'},
   267 	{"rom",		'r'},
   268 	{"samples",	's'},
   269 	{"size",	's'},
   270 	{"thread",	't'},
   271 	{"total",	't'},
   272 	{"zero",	'z'},
   273 	{"oby",		'o'},
   274 	{"rofs",	'z'},
   275 	{"config",	'x'},
   276 	};
   277 
   278 inline int min(int a, int b)
   279 	{
   280 	return a < b ? a : b;
   281 	}
   282 
   283 int Options::Compare(const char* aLhs, const char* aRhs)
   284 	{
   285 	int len = min(strlen(aLhs), strlen(aRhs));
   286 	return strnicmp(aLhs, aRhs, len);
   287 	}
   288 
   289 int Options::Get(istrstream& aStr)
   290 	{
   291 	int pos = aStr.tellg();
   292 	const char* s = aStr.str() + pos;
   293 
   294 	if (strlen(s) >= 3)
   295 		{
   296 		int l = 0, r = sizeof(KOptions)/sizeof(KOptions[0]);
   297 		do
   298 			{
   299 			int m = (l + r ) >> 1;
   300 			const Entry& e = KOptions[m];
   301 			int k = Compare(s, e.iName);
   302 			if (k < 0)
   303 				r = m;
   304 			else if (k > 0)
   305 				l = m + 1;
   306 			else
   307 				{
   308 				// found a match
   309 				aStr.ignore(strlen(e.iName));
   310 				return e.iOption;
   311 				}
   312 			} while (l < r);
   313 		}
   314 	// no match
   315 	return aStr.get();
   316 	}
   317 
   318 int Analyse::ProcessCommandLine(int argc, char ** argv)
   319 	{
   320 	int initial_argc = argc;
   321 	char ** initial_argv = argv;
   322 	// added 2-nd pass. on the 1-st just look for config file
   323 	for(int pass = 0;pass < 2;pass++)
   324 		{
   325 		argc = initial_argc;
   326 		argv = initial_argv;
   327 		while (--argc>0)
   328 			{
   329 			istrstream arg(*++argv);
   330 			int c = arg.get();
   331 			if (c != '/' && c != '-')
   332 				{
   333 				if (pass == 0) continue;
   334 				sTraces.clear();
   335 				sTraces.push_back(arg.str());
   336 				continue;
   337 				}
   338 			c = Options::Get(arg);
   339 			if (tolower(c) != 'x' && pass == 0)
   340 				continue;
   341 			switch (c)
   342 				{
   343 			case 'h': case 'H': case '?':
   344 				if (--argc > 0 && !stricmp(*++argv,"config")) 
   345 					return 2;
   346 				return 1;
   347 			case 'l': case 'L':
   348 				sAction = ETrace;
   349 				break;
   350 			case 'p': case 'P':
   351 				sAction = EProfile;
   352 				break;
   353 			case 'v': case 'V':
   354 				sAction = EActivity;
   355 				break;
   356 			case 'r': case 'R':
   357 				if (--argc == 0)
   358 					Abort("No symbol file specified for option '-r'");
   359 				sRomFile = *++argv;
   360 				break;
   361 			case 's': case 'S':
   362 				sOptions |= ERange;
   363 				arg >> sBeginSample;
   364 				c = arg.get();
   365 				arg >> sEndSample;
   366 				if (c == '+')
   367 					sEndSample += sBeginSample;
   368 				else if (c != '-')
   369 					return 1;
   370 				break;
   371 			case 'n': case 'N':
   372 				sOptions|=ENull;
   373 				break;
   374 			case 't': case 'T':
   375 				if (--argc == 0)
   376 					Abort("No thread name specified for option '-t'");
   377 				sThread = *++argv;
   378 				break;
   379 			case 'd': case 'D':
   380 				if (--argc == 0)
   381 					Abort("No DLL name specified for option '-d'");
   382 				sDll = *++argv;
   383 				break;
   384 			case 'f': case 'F':
   385 				if (--argc == 0)
   386 					Abort("No function name specified for option '-f'");
   387 				sFunction = *++argv;
   388 				break;
   389 			case 'a': case 'A':
   390 				sOptions |= EAddress;
   391 				arg >> hex >> sBase;
   392 				c = arg.get();
   393 				arg >> hex >> sLim;
   394 				if (c == '+')
   395 					sLim += sBase;
   396 				else if (c != '-')
   397 					return 1;
   398 				break;
   399 			case 'b': case 'B':
   400 				switch (c = Options::Get(arg))
   401 					{
   402 				case 'd': case 'D':
   403 					sPartition = EDll;
   404 					break;
   405 				case 'f': case 'F':
   406 					sPartition = EFunction;
   407 					break;
   408 				case 'n': case 'N':
   409 					sPartition = EBuckets;
   410 					arg >> dec >> sBuckets;
   411 					break;
   412 				case 's': case 'S':
   413 					sPartition = ESize;
   414 					arg >> dec >> sBucketSize;
   415 					break;
   416 					}
   417 				break;
   418 			case 'c': case 'C':
   419 				arg >> sCutOff;
   420 				break;
   421 			case 'm': case 'M':
   422 				while ((c = Options::Get(arg)) != EOF)
   423 					{
   424 					switch (c)
   425 						{
   426 					case 'p': case 'P':
   427 						sFormat = EPercent;
   428 						break;
   429 					case 's': case 'S':
   430 						sFormat = ESamples;
   431 						break;
   432 					case 'x': case 'X':
   433 						sFormat = EExcel;
   434 						break;
   435 					case 'z': case 'Z':
   436 						sOptions |= EZeros;
   437 						break;
   438 					case 'o': case 'O':
   439 						sOptions |= ENoOther;
   440 						break;
   441 					case 't': case 'T':
   442 						sOptions |= ETotalOnly;
   443 						break;
   444 					default:
   445 						arg.putback(c);
   446 						break;
   447 						}
   448 					}
   449 				break;
   450 			case 'o': case 'O':
   451 				if (--argc == 0)
   452 					Abort("No OBY file name specified for option '-o'");
   453 				gNonXIP.AddObyFile(*++argv);
   454 				break;
   455 			case 'z': case 'Z':
   456 				if (--argc == 0)
   457 					Abort("No ROFS symbol file name specified for option '-z'");
   458 				gNonXIP.AddSymbolFile(*++argv);
   459 				break;
   460 			case 'x': case 'X':
   461 				if (--argc == 0)
   462 					Abort("No config file name specified for option '-x'");
   463 				if (pass == 0)
   464 					{
   465 					switch(ProcessCfgFile(*++argv))
   466 						{
   467 					case ENoCfgFile:
   468 						Abort("Error no config file name specified for option '-x'");
   469 					case EErrorCfgFile:
   470 						Abort("Error in config file");
   471 						}
   472 					}
   473 				else
   474 					++argv;
   475 				break;
   476 			default:			// unrecognised option
   477 				arg.putback(c);
   478 				break;
   479 				}
   480 			if (!arg || arg.get() != EOF)
   481 				{
   482 				cerr << "Unrecognised option \'" << arg.str() << '\'' << endl;
   483 				Abort();
   484 				}
   485 			} // while
   486 		} // for(pass)
   487 	if (sTraces.empty())
   488 		Abort("No trace files specified");
   489 	return sTraces.size() != 1;
   490 	}
   491 
   492 CodeSpace* Analyse::CreateCodeSpace(SymbolFile* aSymbols, NonXIP *aNonXIP)
   493 	{
   494 	if (Option(EAddress))
   495 		{
   496 		unsigned size;
   497 		if (Partition() == ESize)
   498 			size = sBucketSize;
   499 		else
   500 			size = (sLim - sBase) / sBuckets;
   501 		return new AddressCodeSpace(sBase, sLim, size, AddressCodeSpace::EAbsolute);
   502 		}
   503 
   504 	MappedCodeSpace * mapped_code_space = 0;
   505 	if (aSymbols == 0)
   506 		{
   507 		MappedCodeSpace* mapped_code_space =  new MappedCodeSpace();
   508 		if (aNonXIP) 
   509 			aNonXIP->SetMappedCodeSpace(mapped_code_space);
   510 		return mapped_code_space;
   511 		}
   512 
   513 	for (;;)
   514 		{
   515 		switch (Partition())
   516 			{
   517 		case EDefault:
   518 			if (sFunction != 0)
   519 				{
   520 				sPartition = ESize;
   521 				sBucketSize = 4;
   522 				}
   523 			else if (sDll != 0)
   524 				sPartition = EFunction;
   525 			else
   526 				sPartition = EDll;
   527 			break;
   528 		case EDll:
   529 			{
   530 			PartitionByDll p(sDll);
   531 			mapped_code_space =  new MappedCodeSpace(*aSymbols,p);
   532 			if (aNonXIP) 
   533 				aNonXIP->SetMappedCodeSpace(mapped_code_space);
   534 			return mapped_code_space;
   535 			}
   536 		case EFunction:
   537 			{
   538 			PartitionByFunction p(sDll, sFunction);
   539 			mapped_code_space =  new MappedCodeSpace(*aSymbols,p);
   540 			if (aNonXIP) 
   541 				aNonXIP->SetMappedCodeSpace(mapped_code_space);
   542 			return mapped_code_space;
   543 			}
   544 		case ESize:
   545 		case EBuckets:
   546 			if (sFunction == 0)
   547 				sPartition = EFunction;
   548 			else
   549 				{
   550 				FindFunction f(sDll, sFunction);
   551 				aSymbols->Parse(f);
   552 				if (f.iPc == 0)
   553 					{
   554 					cerr << "Cannot find function '" << sFunction << '\'';
   555 					if (sDll)
   556 						cerr << " in '" << sDll << '\'';
   557 					cerr << endl;
   558 					Abort();
   559 					}
   560 				unsigned size = (Partition() == ESize) ? sBucketSize : f.iLength / sBuckets;
   561 				return new AddressCodeSpace(f.iPc, f.iPc + f.iLength, size, AddressCodeSpace::ERelative);
   562 				}
   563 			break;
   564 			}
   565 		}
   566 	}
   567 
   568 Sampler* Analyse::CreateSampler(SymbolFile* aSymbols, NonXIP *aNonXIP)
   569 	{
   570 	switch (Action())
   571 		{
   572 	case ETrace:
   573 		{
   574 		MappedCodeSpace * mapped_code_space = 0;
   575 		if (aSymbols == 0)
   576 			//return new Tracer(0);
   577 			mapped_code_space = new MappedCodeSpace();
   578 		else
   579 			{
   580 			PartitionByFunction p(0, 0);
   581 			mapped_code_space = new MappedCodeSpace(*aSymbols,p);
   582 			}
   583 		if (aNonXIP) aNonXIP->SetMappedCodeSpace(mapped_code_space);
   584 		return new Tracer(mapped_code_space);
   585 		}
   586 	case EProfile:
   587 		{
   588 		CodeSpace * code_space = CreateCodeSpace(aSymbols, aNonXIP);
   589 		return new Distribution(*code_space, sCutOff);
   590 		}
   591 	case EActivity:
   592 		return new Activity(Partition() == ESize ? sBucketSize : 100, sBeginSample, sCutOff);
   593 		}
   594 	return 0;
   595 	}
   596 
   597 void Analyse::Run()
   598 //
   599 // The main part of the program
   600 //
   601 	{
   602 	Information();
   603 	Trace trace;
   604 	trace.Load(sTraces[0], sBeginSample, sEndSample);
   605 	// create map of original/segment names
   606 	gNonXIP.CreateNamesMap();
   607 
   608 	SymbolFile* symbols = 0;
   609 	if (sRomFile)
   610 		symbols = new SymbolFile(sRomFile);
   611 	Sampler* sampler = CreateSampler(symbols, &gNonXIP);
   612 	trace.Decode(*sampler, &gNonXIP);
   613 	
   614 	// NonXIP footer messages
   615 	cout << endl << "Row buffer errors:" << gNonXIP.iRowBufferErrors;
   616 	cout << " Cook buffer errors:" << gNonXIP.iCookBufferErrors;
   617 	cout << "  Mode:";
   618 	if (gNonXIP.iReportMask & NonXIP::ENonXip)
   619 		cout << "NonXIP";
   620 	else
   621 		cout << "XIP only";
   622 	if (gNonXIP.iReportMask & NonXIP::ENodebugSupport)
   623 		cout << " No debug support from Kernel";
   624 
   625 	cout << endl;
   626 
   627 	cout << flush;
   628 	}
   629 
   630 bool Analyse::Match(const char* aString, const char* aMatch)
   631 //
   632 // Wildcard matching
   633 // If match string is 0, then matches everything
   634 //
   635 	{
   636 	if (aMatch == 0)
   637 		return true;
   638 
   639 	const char* star = strchr(aMatch, '*');
   640 	if (star == 0)
   641 		return (stricmp(aString, aMatch) == 0);
   642 
   643 	int mlen = star - aMatch;
   644 	if (strnicmp(aString, aMatch, mlen) != 0)
   645 		return false;
   646 
   647 	const char* end = aString + strlen(aString);
   648 
   649 	for (;;)
   650 		{
   651 		aString += mlen;
   652 		aMatch += mlen + 1;
   653 		star = strchr(aMatch, '*');
   654 		if (star == 0)
   655 			return (stricmp(end - strlen(aMatch), aMatch) == 0);
   656 		mlen = star - aMatch;
   657 		const char* lim = end - mlen;
   658 		for (;;)
   659 			{
   660 			if (aString > lim)
   661 				return false;
   662 			if (strnicmp(aString, aMatch, mlen) == 0)
   663 				break;
   664 			++aString;
   665 			}
   666 		}
   667 	}
   668 
   669 void Analyse::Abort(char const* aMessage)
   670 	{
   671 	cerr << aMessage << endl;
   672 	Abort();
   673 	}
   674 
   675 void Analyse::Abort()
   676 	{
   677 	exit(3);
   678 	}
   679 
   680 void Analyse::Corrupt(char const* aMessage)
   681 //
   682 // terminate after detecting a fatal corruption error
   683 //
   684 	{
   685 	cerr << "\nfatal error: " << aMessage << "\ncannot continue\n" << flush;
   686 	exit(2);
   687 	}
   688 
   689 ostream& Analyse::Error()
   690 	{
   691 	return cerr << "error: ";
   692 	}
   693 
   694 ostream& Analyse::Warning()
   695 	{
   696 	return cerr << "warning: ";
   697 	}
   698