tracesrv/tracecompiler/internal/scripts/convert_traces.pl
changeset 62 1c2bb2fc7c87
equal deleted inserted replaced
56:aa2539c91954 62:1c2bb2fc7c87
       
     1 #
       
     2 # Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of "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 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description: 
       
    15 #
       
    16 #!perl -w
       
    17 # 
       
    18 # 
       
    19 
       
    20 #*********************************************************************
       
    21 # convert_traces.pl
       
    22 # *********************************************************************
       
    23 #
       
    24 # VERSION     : 6      Draft         15-Feb-2010       Adrian Issott
       
    25 # REASON      : Added warnings about the tool being unsupported
       
    26 #
       
    27 # VERSION     : 5      Draft         21-Jan-2010       Adrian Issott
       
    28 # REASON      : Fixed issue with own macro support + added removal of 
       
    29 #               _L(...) around trace format strings
       
    30 #
       
    31 # VERSION     : 4      Draft         11-Nov-2009       Esa Karvanen
       
    32 # REASON      : Initial support for converting OST Function entry /
       
    33 #               exit traces to printfs.
       
    34 #
       
    35 # VERSION     : 3      Draft         20-Aug-2009       Esa Karvanen
       
    36 # REASON      : Added support to convert OST traces back to printf. 
       
    37 #               Doesn't work for Function entry / exit traces.
       
    38 #
       
    39 # VERSION     : 2      Draft         10-Apr-2008       Esa Karvanen
       
    40 # REASON      : Printf and own macro support
       
    41 #
       
    42 # VERSION     : 1      Draft         16-Oct-2007       Teemu Piiroinen
       
    43 # REASON      : Initial version
       
    44 
       
    45 use strict;
       
    46 use env;
       
    47 use FindBin '$Bin';
       
    48 
       
    49 warn "\n";
       
    50 warn "Warning: this script is not supported and the Dynamic Analysis Tools team\n";
       
    51 warn "does NOT promise to fix any bugs or add any functionality to it.\n";
       
    52 warn "\n";
       
    53 
       
    54 # Should we print debug prints
       
    55 my $DEBUG = 0;
       
    56 
       
    57 if (not defined $ARGV[0] or not -f $ARGV[0])
       
    58 {
       
    59 	print "Please give path to mmp file as parameter.";
       
    60 	
       
    61 	exit;
       
    62 }
       
    63 
       
    64 # Get group names
       
    65 my %map_trace_to_group;
       
    66 my %map_trace_to_text;
       
    67 my %map_trace_to_parameters;
       
    68 
       
    69 my $traces_folder;
       
    70 
       
    71 # Get source files
       
    72 my @sources = getSourceFiles();
       
    73 my @trace_id_names;
       
    74 my $CASWTraceCount = 0;
       
    75 
       
    76 my $printfMacro;
       
    77 
       
    78 
       
    79 # Ask selection from user
       
    80 my $selection = get_operation();
       
    81 
       
    82 # Check  if the selection is numeric
       
    83 if ($selection =~ /^-?\d/)
       
    84 {
       
    85 	# Symbian traces
       
    86 	if ($selection == 1)
       
    87 	{
       
    88 		$traces_folder = get_traces_folder();
       
    89 		convertSymbianTraces(@sources);
       
    90 	}
       
    91 
       
    92 	# Kern::Printf
       
    93 	elsif ($selection == 2)
       
    94 	{
       
    95 		$printfMacro = "kern::printf";
       
    96 		convertPrintf($printfMacro, @sources);
       
    97 	}
       
    98 
       
    99 	# RDebug::Printf
       
   100 	elsif ($selection == 3)
       
   101 	{
       
   102 		$printfMacro = "rdebug::printf";
       
   103 		convertPrintf($printfMacro, @sources);
       
   104 	}
       
   105 	
       
   106 	# User defined MACRO
       
   107 	elsif ($selection == 4)
       
   108 	{
       
   109 		$printfMacro = "rdebug::printf";
       
   110 		convertPrintf($printfMacro, @sources);
       
   111 	}
       
   112 	
       
   113 	# OST traces to Kern::Printf
       
   114 	elsif ($selection == 5)
       
   115 	{
       
   116 		$printfMacro = "Kern::Printf";
       
   117 		convertOstToPrintf($printfMacro, @sources);
       
   118 	}	
       
   119 	
       
   120 	# OST traces to RDebug::Printf
       
   121 	elsif ($selection == 6)
       
   122 	{
       
   123 		$printfMacro = "RDebug::Printf";
       
   124 		convertOstToPrintf($printfMacro, @sources);
       
   125 	}
       
   126 	else
       
   127 	{
       
   128 		print "Wrong selection!";
       
   129 	}
       
   130 }
       
   131 
       
   132 # Otherwise, it's user defined MACRO
       
   133 else
       
   134 {
       
   135 	$printfMacro = $selection;
       
   136 	convertPrintf($printfMacro, @sources);
       
   137 }
       
   138 
       
   139 
       
   140 #-------------------------------------------------------
       
   141 # Convert Symbian traces
       
   142 #-------------------------------------------------------
       
   143 sub convertSymbianTraces
       
   144 {
       
   145 	debug("\nConvertSymbianTraces starts");
       
   146 	my (@sources) = @_;
       
   147 	
       
   148 	# Go through all found source files
       
   149 	foreach my $source_file (@sources)
       
   150 	{
       
   151 		debug("Source file $source_file");
       
   152 		print $source_file . "\n";
       
   153 		
       
   154 		RemoveLineBreaksFromTracesInFile($source_file, 0);
       
   155 		
       
   156 		open FILE, "<$source_file" or die $!;
       
   157 		
       
   158 		my $new_file_content;
       
   159 		
       
   160 		my $traces_converted = 0;
       
   161 		my $file_name = "";
       
   162 		my $last_include_line = 0;
       
   163 		my $line_number = 0;
       
   164 	
       
   165 		if ($source_file =~ /.*\\(.+?)\..+/)
       
   166 		{
       
   167 			$file_name = $1;
       
   168 		}
       
   169 		
       
   170 		foreach my $line (<FILE>)
       
   171 		{
       
   172 			$line_number += 1;
       
   173 			
       
   174 			my $line_converted = 0;
       
   175 			
       
   176 			chomp $line;
       
   177 			
       
   178 			if ($line =~ /^\s*\#include/)
       
   179 			{
       
   180 				$last_include_line = $line_number; 
       
   181 			}
       
   182 			
       
   183 			if ($line =~ /Traces\.h/)
       
   184 			{
       
   185 				$line = "// " . $line;
       
   186 			}
       
   187 			
       
   188 			# FA_CORE_SERVICES traces
       
   189 			if ($line =~ /(BUILD_TRACE|ASSERT_TRACE|ASSERT_ALWAYS_TRACE|API_TRACE|INTERNAL_TRACE|DATA_DUMP_TRACE|ISIMSG_API_TRACE).*;/)
       
   190 			{
       
   191 				my $trace_name = $1;		
       
   192 				
       
   193 				if ($line =~ /.*?\/\/.*?$trace_name/)
       
   194 				{
       
   195 					next;
       
   196 				}
       
   197 				
       
   198 				my $spaces = "";				
       
   199 				if ($line =~ /^(\s+).*?$trace_name/)
       
   200 				{
       
   201 					$spaces = $1;
       
   202 				}
       
   203 				
       
   204 				my $prefix = "";				
       
   205 				if ($line =~ /^\s*(.*?)$trace_name/)
       
   206 				{
       
   207 					$prefix = $1;
       
   208 				}
       
   209 				
       
   210 				$line =~ s/^.*($trace_name.+;).*/$1/;
       
   211 				
       
   212 				if ($line =~ /$trace_name\s*\((.+)\)\s*;/)
       
   213 				{
       
   214 					$line_converted = 1;
       
   215 					$traces_converted = 1;
       
   216 					
       
   217 					my $parameters = $1;
       
   218 					
       
   219 					$new_file_content .= $spaces . "// " . $prefix . $line . "\n";
       
   220 				
       
   221 					# Get parameters without format string
       
   222 					my $parameters_without_format = $parameters;
       
   223 					if ($parameters =~ /\".*\"(.*)/)
       
   224 					{
       
   225 						$parameters_without_format = $1;
       
   226 					}
       
   227 					
       
   228 					# Calculate parameter count
       
   229 					my $parameter_count = -1;
       
   230 					foreach my $part (split(",", $parameters_without_format))
       
   231 					{
       
   232 						$parameter_count++;
       
   233 					}
       
   234 	
       
   235 					my $new_trace = GetNewCASWTraceName($trace_name, $parameter_count);
       
   236 					
       
   237 					if ($trace_name =~ /(BUILD_TRACE|ASSERT_TRACE|ASSERT_ALWAYS_TRACE|API_TRACE|INTERNAL_TRACE)/)
       
   238 					{
       
   239 						$new_trace .= "(" . $trace_name . ", TRACE_NAME_" . $CASWTraceCount++ . ", " . $parameters . ");";
       
   240 					}
       
   241 					else
       
   242 					{
       
   243 						$new_trace .= "(" . $trace_name . ", TRACE_NAME_" . $CASWTraceCount++ . ", ";
       
   244 						
       
   245 						if ($parameter_count == 0)
       
   246 						{
       
   247 							$new_trace .= "\"\", " . $parameters . ", <data_len>);";
       
   248 						}
       
   249 						else
       
   250 						{
       
   251 							$new_trace .= $parameters . ", <data_len>);";
       
   252 						}
       
   253 					}
       
   254 					
       
   255 					$new_file_content .= $spaces . $new_trace . "\n";
       
   256 				}
       
   257 			}
       
   258 			
       
   259 			# SymbianTraces
       
   260 			if ($line =~ /SymbianTrace([0-2]|Data[0-1]{0,1}|Thread[0-2]|ThreadData[0-1]{0,1}|IsTraceActive|Pc[0-2]|ThreadPc[0-2]|PcData[0-1]{0,1}|ThreadPcData[0-1]{0,1}|StartControlBlock|EndControlBlock|Ext[1-6]|ExtThread[1-6]).*;/)
       
   261 			{
       
   262 				my $trace_name = "SymbianTrace" . $1;
       
   263 	
       
   264 				if ($line =~ /.*?\/\/.*?SymbianTrace/)
       
   265 				{
       
   266 					next;
       
   267 				}						
       
   268 			
       
   269 				# print $trace_name . "\n";
       
   270 				
       
   271 				my $spaces = "";				
       
   272 				if ($line =~ /^(\s+).*?SymbianTrace/)
       
   273 				{
       
   274 					$spaces = $1;
       
   275 				}
       
   276 				
       
   277 				my $prefix = "";				
       
   278 				if ($line =~ /^\s*(.*?)SymbianTrace/)
       
   279 				{
       
   280 					$prefix = $1;
       
   281 				}
       
   282 				
       
   283 				$line =~ s/^.*(SymbianTrace.+;).*/$1/;
       
   284 				
       
   285 				#print $line . "\n";
       
   286 				
       
   287 				# Get trace id name
       
   288 				if ($line =~ /\(\s*(.+?)[\s\),]{1,2}(.*?)\)\s*;$/)
       
   289 				{
       
   290 					$line_converted = 1;
       
   291 					$traces_converted = 1;
       
   292 					
       
   293 					my $trace_id_name = $1;
       
   294 					my $rest_of_parameters = $2;
       
   295 					
       
   296 					my $new_trace_id_name = $trace_id_name;
       
   297 					
       
   298 					$new_file_content .= $spaces . "// " . $prefix . $line . "\n";
       
   299 									
       
   300 					#print $trace_id_name . "\n";
       
   301 					#print $rest_of_parameters . "\n";
       
   302 					
       
   303 					if ($trace_name ne "SymbianTraceIsTraceActive")
       
   304 					{
       
   305 						while (grep(/$new_trace_id_name/, @trace_id_names))
       
   306 						{
       
   307 							# Duplicate trace
       
   308 							$new_trace_id_name .= "_";
       
   309 						}
       
   310 						
       
   311 						push(@trace_id_names, $new_trace_id_name);
       
   312 					}
       
   313 								
       
   314 					my $trace_text = $map_trace_to_text{$trace_id_name};
       
   315 					
       
   316 					my @params = split(",", $rest_of_parameters);
       
   317 									
       
   318 					$trace_text .= GetFormatText($trace_id_name, @params);
       
   319 					
       
   320 					my $type_string = IsStringType($trace_id_name, @params);
       
   321 					
       
   322 					my $group_name = $map_trace_to_group{$trace_id_name};
       
   323 					if (not defined $group_name or $group_name eq "")
       
   324 					{
       
   325 						$group_name = "DEFAULT_GROUP";
       
   326 						$trace_text = "";
       
   327 					}
       
   328 					
       
   329 					my $new_trace = GetNewSymbianTraceName($trace_name, $type_string);
       
   330 					$new_trace .= "( " . $group_name . ", " . 
       
   331 												$new_trace_id_name . ", " .
       
   332 												"\"" . $trace_text . "\"";
       
   333 												
       
   334 					if ($rest_of_parameters ne "")
       
   335 					{
       
   336 						 $new_trace .= ", " . $rest_of_parameters;
       
   337 					}
       
   338 					
       
   339 					$new_trace .= ");";
       
   340 					
       
   341 					$new_file_content .= $spaces . $new_trace . "\n";
       
   342 					
       
   343 					#print $new_trace . "\n\n";
       
   344 				}
       
   345 				else
       
   346 				{
       
   347 					print STDERR "Cannot find trace id name. From line: $line\n";
       
   348 				}
       
   349 			}
       
   350 			
       
   351 			if (not $line_converted)
       
   352 			{
       
   353 				$new_file_content .= $line . "\n";
       
   354 			}
       
   355 			
       
   356 		}
       
   357 	
       
   358 		close FILE;	
       
   359 		
       
   360 		if ($traces_converted == 1)
       
   361 		{		
       
   362 			SaveNewSourceFile($source_file, $new_file_content, $last_include_line, $file_name, 1);
       
   363 		}
       
   364 	}
       
   365 
       
   366 CreateOSTTraceDefinitionsHeader();
       
   367 }
       
   368 
       
   369 
       
   370 #-------------------------------------------------------
       
   371 # Convert PRINTF traces to OST traces
       
   372 #-------------------------------------------------------
       
   373 sub convertPrintf
       
   374 {
       
   375 	my ($macro, @sources) = @_;
       
   376 	debug("\nConvertPrintf starts, macro is: $macro");
       
   377 	
       
   378 	# Go through all found source files
       
   379 	foreach my $source_file (@sources)
       
   380 	{
       
   381 		debug("Source file $source_file");
       
   382 		print $source_file . "\n";
       
   383 		
       
   384 		RemoveLineBreaksFromTracesInFile($source_file, 0);
       
   385 		
       
   386 		open FILE, "<$source_file" or die $!;
       
   387 		
       
   388 		my $new_file_content;
       
   389 		
       
   390 		my $traces_converted = 0;
       
   391 		my $file_name = "";
       
   392 		my $last_include_line = 0;
       
   393 		my $line_number = 0;
       
   394 		my $traceNumber = 0;
       
   395 	
       
   396 		if ($source_file =~ /.*\\(.+?)\..+/)
       
   397 		{
       
   398 			$file_name = $1;
       
   399 		}
       
   400 		
       
   401 		foreach my $line (<FILE>)
       
   402 		{
       
   403 			$line_number += 1;
       
   404 			
       
   405 			my $line_converted = 0;
       
   406 			
       
   407 			chomp $line;
       
   408 			
       
   409 			if ($line =~ /^\s*\#include/)
       
   410 			{
       
   411 				$last_include_line = $line_number; 
       
   412 			}
       
   413 			
       
   414 			if ($line =~ /Traces\.h/)
       
   415 			{
       
   416 				$line = "// " . $line;
       
   417 			}
       
   418 			
       
   419 			# Printf macro
       
   420 			if ($line =~ /^\s*$macro.*;/i)
       
   421 			{		
       
   422 				my $spaces;				
       
   423 				my $trace = "";
       
   424 				if ($line =~ /^(\s*)/)
       
   425 				{
       
   426 					$spaces = $1;
       
   427 				}
       
   428 				
       
   429 				$line =~ s/^\s*($macro.+;).*/$1/;
       
   430 				
       
   431 				$trace = $line;
       
   432 
       
   433 				# Remove spaces from the beginning
       
   434 				$trace =~ s/^\s*//g;
       
   435 
       
   436 				print("Trace: " . $line . "\n");
       
   437 				
       
   438 				if (GetBracketCount($line) % 2 == 0)
       
   439 				{
       
   440 					my $param_count = 0;
       
   441 					my $params = $line;
       
   442 					
       
   443 					while($params =~ s/(.*?),/$1/)
       
   444 					{
       
   445 						$param_count++;
       
   446 					}
       
   447 										
       
   448 					$line = $spaces . "// " . $trace ."\n";
       
   449 					$line .= $spaces;
       
   450 					
       
   451 					# Remove the macro from the trace
       
   452 					debug("Trace before removing MACRO $trace");
       
   453 					$trace =~ /\((.*)\)/;
       
   454 					$trace = $1;
       
   455 					debug("Trace after removing MACRO $trace");
       
   456 
       
   457     				# Remove use of _L(...)
       
   458     				if ($trace =~ s/\b_L\(//g) 
       
   459                     {
       
   460                         $trace =~ s/(.*")\)/$1/; # want to remove the last ") rather than the first one we come across
       
   461                     }
       
   462 
       
   463 					# Convert
       
   464 					# 0 parameters
       
   465 					debug("Convert trace with $param_count parameters");
       
   466 					if ($param_count == 0)
       
   467 					{
       
   468 						$line .= "OstTrace0(DEFAULT_GROUP, DEFAULT_TRACE" . $traceNumber . ", " . $trace . ");";
       
   469 					}
       
   470 					# 1 parameter
       
   471 					elsif	($param_count == 1)
       
   472 					{
       
   473 						$line .= "OstTrace1(DEFAULT_GROUP, DEFAULT_TRACE" . $traceNumber . ", " . $trace . ");";
       
   474 					}
       
   475 					# More than 1 parameters
       
   476 					else
       
   477 					{
       
   478 					$line .= "OstTraceExt" . $param_count . "(DEFAULT_GROUP, DEFAULT_TRACE" . $traceNumber . ", " . $trace . ");";	
       
   479 					}
       
   480 					
       
   481 					$new_file_content .= $line . "\n";
       
   482 					$line_converted = 1;
       
   483 					$traces_converted = 1;
       
   484 					$traceNumber++;
       
   485 				}
       
   486 			}
       
   487 			else
       
   488 			{
       
   489 				$new_file_content .= $line . "\n";
       
   490 			}
       
   491 		}
       
   492 	
       
   493 		close FILE;	
       
   494 		
       
   495 		if ($traces_converted == 1)
       
   496 		{	
       
   497 			debug("\n\nSave new source file");
       
   498 			SaveNewSourceFile($source_file, $new_file_content, $last_include_line, $file_name, 1);
       
   499 		}
       
   500 	}
       
   501 
       
   502 CreateOSTTraceDefinitionsHeader();
       
   503 	
       
   504 }
       
   505 
       
   506 
       
   507 #-------------------------------------------------------
       
   508 # Get bracket count
       
   509 #-------------------------------------------------------
       
   510 sub GetBracketCount
       
   511 {
       
   512 	my ($line) = @_;
       
   513 	
       
   514 	$line =~ s/\\\(//g;
       
   515 	$line =~ s/\\\)//g;
       
   516 	
       
   517 	my @brackets = ($line =~ /\(|\)/g);
       
   518 	
       
   519 	return scalar @brackets;
       
   520 }
       
   521 
       
   522 #-------------------------------------------------------
       
   523 # Save new source file
       
   524 #-------------------------------------------------------
       
   525 sub SaveNewSourceFile
       
   526 {
       
   527 	my ($source_file, $new_file_content, $last_include_line, $file_name, $add_ost_stuff) = @_;
       
   528 	
       
   529 	open FILE, ">$source_file" or die $!;
       
   530 	
       
   531 	my @lines = split("\n", $new_file_content);
       
   532 	
       
   533 	my $line_number = 0;
       
   534 	foreach my $line (@lines)
       
   535 	{	
       
   536 		$line_number++;
       
   537 		
       
   538 		print FILE $line . "\n";
       
   539 		
       
   540 		if ($line_number == $last_include_line && $add_ost_stuff == 1)
       
   541 		{
       
   542 			print FILE "\#include \"OstTraceDefinitions.h\"\n";
       
   543 			print FILE "\#ifdef OST_TRACE_COMPILER_IN_USE\n";
       
   544 			print FILE "\#include \"" . $file_name . "Traces.h\"\n";
       
   545 			print FILE "\#endif\n\n";
       
   546 		}
       
   547 	}
       
   548 	
       
   549 	close FILE;
       
   550 }
       
   551 
       
   552 
       
   553 #-------------------------------------------------------
       
   554 # Create OST Trace Definitions header file
       
   555 #-------------------------------------------------------
       
   556 sub CreateOSTTraceDefinitionsHeader
       
   557 {
       
   558 	debug("\nCreateOSTTraceDefinitionsHeader starts");
       
   559 	
       
   560 	# Get path to traces folder
       
   561 	my $mmp_file = $ARGV[0];
       
   562 	my $file_path = $mmp_file;
       
   563   $file_path =~ s/\\[^\\]+$/\\/i;
       
   564   $file_path =~ s/\\\\/\\/g;
       
   565   if ($file_path =~ m/\.mmp/i)
       
   566   {
       
   567   	debug("getGroupTraceData. MMP file doesn't have path. Use current dir.");
       
   568   	my $dir = "$Bin";
       
   569   	$file_path = $dir;
       
   570   	$file_path =~ s/\//\\/g;
       
   571   } 
       
   572 	my $trace_folder_path = concatenatePath($file_path, "..\\traces");
       
   573 	
       
   574 	debug("CreateOSTTraceDefinitionsHeader trace_folder_path: $trace_folder_path");
       
   575 	
       
   576 	mkdir($trace_folder_path);
       
   577 	
       
   578 	
       
   579 	open FILE, ">$trace_folder_path\\OstTraceDefinitions.h" or die $!;
       
   580 	
       
   581 	print FILE "\#ifndef __OSTTRACEDEFINITIONS_H__\n" .
       
   582 							"\#define __OSTTRACEDEFINITIONS_H__\n" .
       
   583 							"// OST_TRACE_COMPILER_IN_USE flag has been added by Trace Compiler\n" .
       
   584 							"// REMOVE BEFORE CHECK-IN TO VERSION CONTROL\n" .
       
   585 							"//\#define OST_TRACE_COMPILER_IN_USE\n" .
       
   586 							"\#include <OpenSystemTrace.h>\n" .
       
   587 							"\#endif\n";
       
   588 
       
   589 	
       
   590 	close FILE;
       
   591 }
       
   592 
       
   593 
       
   594 #-------------------------------------------------------
       
   595 # Get format text
       
   596 #-------------------------------------------------------
       
   597 sub GetFormatText
       
   598 {
       
   599 	my ($trace_id_name, @params) = @_;
       
   600 	
       
   601 	my $format_text = "";
       
   602 	
       
   603 	foreach my $param (@params)
       
   604 	{
       
   605 		if ($param =~ /(\w+)[\s\)]*$/)
       
   606 		{
       
   607 			$param = $1;
       
   608 		}
       
   609 		
       
   610 		my $type = $map_trace_to_parameters{$trace_id_name}{$param};
       
   611 		
       
   612 		if (not defined $type or $type eq "" or $type eq "-DEC")
       
   613 		{
       
   614 			$format_text .= " %d";
       
   615 		}
       
   616 		elsif ($type eq "DEC")
       
   617 		{
       
   618 			$format_text .= " %u";
       
   619 		}
       
   620 		elsif ($type eq "HEX")
       
   621 		{
       
   622 			$format_text .= " 0x%x";
       
   623 		}
       
   624 		elsif ($type eq "STR")
       
   625 		{
       
   626 			$format_text .= " %s";
       
   627 		}
       
   628 	}
       
   629 	
       
   630 	return $format_text;
       
   631 }
       
   632 
       
   633 #-------------------------------------------------------
       
   634 # Is string type
       
   635 #-------------------------------------------------------
       
   636 sub IsStringType
       
   637 {
       
   638 	my ($trace_id_name, @params) = @_;
       
   639 	
       
   640 	my $type_string = 0;
       
   641 	
       
   642 	foreach my $param (@params)
       
   643 	{
       
   644 		if ($param =~ /(\w+)[\s\)]*$/)
       
   645 		{
       
   646 			$param = $1;
       
   647 		}
       
   648 		
       
   649 		my $type = $map_trace_to_parameters{$trace_id_name}{$param};
       
   650 		
       
   651 		if (defined $type and $type eq "STR")
       
   652 		{
       
   653 			$type_string = 1;
       
   654 		}
       
   655 	}
       
   656 	
       
   657 	return $type_string;
       
   658 }
       
   659 
       
   660 #-------------------------------------------------------
       
   661 # Remove linebreaks from traces
       
   662 #-------------------------------------------------------
       
   663 sub RemoveLineBreaksFromTracesInFile
       
   664 {
       
   665 	my ($file, $convert_back_to_printf) = @_;
       
   666 	
       
   667 	my $file_changed = 0;
       
   668 	my $new_file_content;
       
   669 	my $previous_line_changed = 0;
       
   670 	my $convert_macro = "Kern::Printf";
       
   671 	
       
   672 	if ($convert_back_to_printf == 1)
       
   673 	{
       
   674 		$convert_macro = "OstTrace([01]|Ext|Data)";
       
   675 	}
       
   676 	
       
   677 	open FILE, "<$file" or die $!;
       
   678 	
       
   679 	foreach my $line (<FILE>)
       
   680 	{
       
   681 		chomp($line);
       
   682 			
       
   683 		if ($line =~ /SymbianTrace([0-2]|Data[0-1]{0,1}|Thread[0-2]|ThreadData[0-1]{0,1}|IsTraceActive|Pc[0-2]|ThreadPc[0-2]|PcData[0-1]{0,1}|ThreadPcData[0-1]{0,1}|StartControlBlock|EndControlBlock|Ext[1-6]|ExtThread[1-6])[^;]+$/ or
       
   684 				$line =~ /$convert_macro[^;]+$/ or
       
   685 				$line =~ /(BUILD_TRACE|ASSERT_TRACE|ASSERT_ALWAYS_TRACE|API_TRACE|INTERNAL_TRACE|DATA_DUMP_TRACE|ISIMSG_API_TRACE)[^;]+$/)
       
   686 		{
       
   687 			# We have a trace in multiple lines
       
   688 			$new_file_content .= $line;
       
   689 			
       
   690 			$file_changed = 1;
       
   691 			$previous_line_changed = 1;
       
   692 		}
       
   693 		else
       
   694 		{
       
   695 			if ($previous_line_changed == 1)
       
   696 			{
       
   697 				$line =~ s/\s*(.*)/ $1/;
       
   698 				
       
   699 				$new_file_content .= $line;
       
   700 			}
       
   701 			else
       
   702 			{
       
   703 				$new_file_content .= $line . "\n";
       
   704 			}
       
   705 			
       
   706 			if ($previous_line_changed and $line =~ /;/)
       
   707 			{
       
   708 				$new_file_content .= "\n";
       
   709 				$previous_line_changed = 0;
       
   710 			}
       
   711 		}
       
   712 	}
       
   713 	
       
   714 	close FILE;
       
   715 	
       
   716 	if ($file_changed == 1)
       
   717 	{
       
   718 		open FILE, ">$file" or die $!;
       
   719 	
       
   720 		print FILE $new_file_content;
       
   721 		
       
   722 		close FILE;
       
   723 	}
       
   724 }
       
   725 
       
   726 #-------------------------------------------------------
       
   727 # Get new CASW trace name
       
   728 #-------------------------------------------------------
       
   729 sub GetNewCASWTraceName
       
   730 {
       
   731 	my ($old_trace_name, $paramater_count) = @_;
       
   732 	
       
   733 	my $new_trace_name;
       
   734 	
       
   735 	if ($old_trace_name eq "BUILD_TRACE" or 
       
   736 			$old_trace_name eq "ASSERT_TRACE" or
       
   737 			$old_trace_name eq "ASSERT_ALWAYS_TRACE" or
       
   738 			$old_trace_name eq "API_TRACE" or
       
   739 			$old_trace_name eq "INTERNAL_TRACE")
       
   740 	{
       
   741 		if ($paramater_count <= 0)
       
   742 		{
       
   743 			$new_trace_name = "OstTrace0";
       
   744 		}
       
   745 		elsif ($paramater_count <= 5)
       
   746 		{
       
   747 			$new_trace_name = "OstTraceExt" . $paramater_count;
       
   748 		}
       
   749 		else
       
   750 		{
       
   751 			$new_trace_name = "// TODO: Cannot convert trace. Too much parameters.";
       
   752 		}
       
   753 	}
       
   754 	elsif ( $old_trace_name eq "DATA_DUMP_TRACE" or 
       
   755 					$old_trace_name eq "ISIMSG_API_TRACE")
       
   756 	{
       
   757 		$new_trace_name = "OstTraceData";
       
   758 	}
       
   759 	
       
   760 	return $new_trace_name;
       
   761 }
       
   762 
       
   763 #-------------------------------------------------------
       
   764 # Get new Symbian trace name
       
   765 #-------------------------------------------------------
       
   766 sub GetNewSymbianTraceName
       
   767 {
       
   768 	my ($old_trace_name, $type_string) = @_;
       
   769 	
       
   770 	my $new_trace_name;
       
   771 	
       
   772 	if ($old_trace_name eq "SymbianTrace0" or 
       
   773 			$old_trace_name eq "SymbianTraceThread0")
       
   774 	{
       
   775 		$new_trace_name = "OstTrace0";
       
   776 	}
       
   777 	elsif (	not $type_string and 
       
   778 					( $old_trace_name eq "SymbianTrace1" or 
       
   779 						$old_trace_name eq "SymbianTraceThread1" or
       
   780 						$old_trace_name eq "SymbianTraceExt1" or
       
   781 						$old_trace_name eq "SymbianTraceExtThread1"))
       
   782 	{
       
   783 		$new_trace_name = "OstTrace1";
       
   784 	}
       
   785 	elsif (	$type_string and 
       
   786 					( $old_trace_name eq "SymbianTrace1" or 
       
   787 						$old_trace_name eq "SymbianTraceThread1" or
       
   788 						$old_trace_name eq "SymbianTraceExt1" or
       
   789 						$old_trace_name eq "SymbianTraceExtThread1"))
       
   790 	{
       
   791 		$new_trace_name = "OstTraceExt1";
       
   792 	}
       
   793 	elsif (	$old_trace_name eq "SymbianTrace2" or 
       
   794 					$old_trace_name eq "SymbianTraceThread2" or
       
   795 					$old_trace_name eq "SymbianTraceExt2" or
       
   796 					$old_trace_name eq "SymbianTraceExtThread2")
       
   797 	{
       
   798 		$new_trace_name = "OstTraceExt2";
       
   799 	}
       
   800 	elsif (	$old_trace_name eq "SymbianTraceExt3" or
       
   801 					$old_trace_name eq "SymbianTraceExtThread3")
       
   802 	{
       
   803 		$new_trace_name = "OstTraceExt3";
       
   804 	}
       
   805 	elsif (	$old_trace_name eq "SymbianTraceExt4" or
       
   806 					$old_trace_name eq "SymbianTraceExtThread4")
       
   807 	{
       
   808 		$new_trace_name = "OstTraceExt4";
       
   809 	}
       
   810 	elsif (	$old_trace_name eq "SymbianTraceExt5" or
       
   811 					$old_trace_name eq "SymbianTraceExtThread5")
       
   812 	{
       
   813 		$new_trace_name = "OstTraceExt5";
       
   814 	}
       
   815 	elsif (	$old_trace_name eq "SymbianTraceExt6" or
       
   816 					$old_trace_name eq "SymbianTraceExtThread6")
       
   817 	{
       
   818 		$new_trace_name = "// TODO: Cannot convert SymbianTraceExt6";
       
   819 	}
       
   820 	elsif (	$old_trace_name eq "SymbianTraceExt7" or
       
   821 					$old_trace_name eq "SymbianTraceExtThread7")
       
   822 	{
       
   823 		$new_trace_name = "// TODO: Cannot convert SymbianTraceExt7";
       
   824 	}
       
   825 	elsif (	$old_trace_name eq "SymbianTraceData" or 
       
   826 					$old_trace_name eq "SymbianTraceData0" or 
       
   827 					$old_trace_name eq "SymbianTraceThreadData" or 
       
   828 					$old_trace_name eq "SymbianTraceThreadData0")
       
   829 	{
       
   830 		$new_trace_name = "OstTraceData";
       
   831 	}	
       
   832 	elsif ($old_trace_name eq "SymbianTraceData1")
       
   833 	{
       
   834 		$new_trace_name = "// TODO: Cannot convert SymbianTraceData1";
       
   835 	}
       
   836 	elsif ($old_trace_name eq "SymbianTraceIsTraceActive")
       
   837 	{
       
   838 		$new_trace_name = "// TODO: Cannot convert SymbianTraceIsTraceActive";
       
   839 	}
       
   840 	elsif (	$old_trace_name eq "SymbianTracePc0" or
       
   841 					$old_trace_name eq "SymbianTracePc1" or
       
   842 					$old_trace_name eq "SymbianTracePc2")
       
   843 	{
       
   844 		$new_trace_name = "// TODO: Cannot convert SymbianTracePc";
       
   845 	}
       
   846 	elsif (	$old_trace_name eq "SymbianTraceThreadPc0" or
       
   847 					$old_trace_name eq "SymbianTraceThreadPc1" or
       
   848 					$old_trace_name eq "SymbianTraceThreadPc2")
       
   849 	{
       
   850 		$new_trace_name = "// TODO: Cannot convert SymbianTraceThreadPc";
       
   851 	}
       
   852 	elsif (	$old_trace_name eq "SymbianTracePcData" or
       
   853 					$old_trace_name eq "SymbianTracePcData0" or
       
   854 					$old_trace_name eq "SymbianTracePcData1")
       
   855 	{
       
   856 		$new_trace_name = "// TODO: Cannot convert SymbianTracePcData";
       
   857 	}
       
   858 	elsif (	$old_trace_name eq "SymbianTraceThreadPcData" or
       
   859 					$old_trace_name eq "SymbianTraceThreadPcData0" or
       
   860 					$old_trace_name eq "SymbianTraceThreadPcData1")
       
   861 	{
       
   862 		$new_trace_name = "// TODO: Cannot convert SymbianTraceThreadPcData";
       
   863 	}
       
   864 	elsif (	$old_trace_name eq "SymbianTraceStartControlBlock")
       
   865 	{
       
   866 		$new_trace_name = "// TODO: Cannot convert SymbianTraceStartControlBlock";
       
   867 	}
       
   868 	elsif (	$old_trace_name eq "SymbianTraceEndControlBlock")
       
   869 	{
       
   870 		$new_trace_name = "// TODO: Cannot convert SymbianTraceEndControlBlock";
       
   871 	}
       
   872 	
       
   873 	return $new_trace_name;
       
   874 }
       
   875 
       
   876 #-------------------------------------------------------
       
   877 # Get group names
       
   878 #-------------------------------------------------------
       
   879 sub getGroupTraceData
       
   880 {
       
   881 	my ($map_trace_to_group, $map_trace_to_text, $map_trace_to_parameters, $traces_folder) = @_;
       
   882 	
       
   883 	debug("\n\ngetGroupTraceData starts with: $traces_folder");
       
   884 	
       
   885 	# Get path to traces folder
       
   886 	my $mmp_file = $ARGV[0];
       
   887 	my $file_path = $mmp_file;
       
   888   $file_path =~ s/\\[^\\]+$/\\/i;
       
   889   $file_path =~ s/\\\\/\\/g;
       
   890   if ($file_path =~ m/\.mmp/i)
       
   891   {
       
   892   	debug("getGroupTraceData file doesn't have path. Use current dir.");
       
   893   	my $dir = "$Bin";
       
   894   	$file_path = $dir;
       
   895   	$file_path =~ s/\//\\/g;
       
   896   } 
       
   897   
       
   898 	my $trace_folder_path = concatenatePath($file_path, $traces_folder);
       
   899 	
       
   900 	debug("getGroupTraceData MMP file: $mmp_file");
       
   901 	debug("getGroupTraceData folder path: $trace_folder_path");
       
   902 	
       
   903 	if (not -e $trace_folder_path)
       
   904 	{
       
   905 		return;
       
   906 	}
       
   907 	
       
   908 	# Get all header files in traces folder
       
   909 	my $dir_return = qx("dir $trace_folder_path\\*.h /b");
       
   910 	my @header_files = split("\n", $dir_return);
       
   911 	
       
   912 	my $found_header = 0;
       
   913 	
       
   914 	foreach my $header_file (@header_files)
       
   915 	{
       
   916 		debug("getGroupTraceData file: $header_file");
       
   917 		open FILE, "<$trace_folder_path\\$header_file" or die $!;
       
   918 		
       
   919 		debug("getGroupTraceData file: Found header!");
       
   920 		$found_header = 1;
       
   921 		
       
   922 		my $trace_id_name;
       
   923 		my $trace_group_name;
       
   924 		
       
   925 		foreach my $line (<FILE>)
       
   926 		{
       
   927 			if ($line =~ /\s*TRACE_GROUP_BEGIN\(\s*(.+?)[\s,]/)
       
   928 			{
       
   929 				$trace_group_name = $1;
       
   930 			}
       
   931 						
       
   932 			if ($line =~ /VALUE\s*\(\s*(-*)\d\d\s*,.+?,\s*(.+?),.*?,\s*(.+?)[\s\)]/)
       
   933 			{
       
   934 				my $sign = $1;
       
   935 				my $variable = $2;
       
   936 				my $type = $3;
       
   937 				
       
   938 				$map_trace_to_parameters{$trace_id_name}{$variable} = $1 . $type;
       
   939 				
       
   940 				#print $variable . " => " . $type . "\n";
       
   941 			}
       
   942 			
       
   943 			if ($line =~ /STRING_REF\s*\(\s*.+?\s*,.+?,\s*(.+?),.*?,\s*.+?[\s\)]/)
       
   944 			{
       
   945 				my $variable = $1;
       
   946 				
       
   947 				$map_trace_to_parameters{$trace_id_name}{$variable} = "STR";
       
   948 			}
       
   949 			
       
   950 			if ($line =~ /\s*TRACE_GROUP_END/)
       
   951 			{
       
   952 				$trace_group_name = "";
       
   953 			}
       
   954 
       
   955 			if ($line =~ /TRACE_BEGIN\(\s*(.+?)[\s,]+\"(.*)\"/)
       
   956 			{
       
   957 				$trace_id_name = $1;
       
   958 				my $trace_text = $2;
       
   959 				
       
   960 				$trace_text =~ s/\%//g;
       
   961 				
       
   962 				if ($trace_group_name eq "")
       
   963 				{
       
   964 					print STDERR "Invalid trace definition header file: $header_file\nMost likely bug in this script...";
       
   965 				}
       
   966 				else
       
   967 				{
       
   968 					#print "$trace_id_name => $trace_group_name\n";
       
   969 					#print "$trace_id_name => $trace_text\n";
       
   970 				
       
   971 					$map_trace_to_group{$trace_id_name} = $trace_group_name;
       
   972 					$map_trace_to_text{$trace_id_name} = $trace_text;
       
   973 				}
       
   974 			}
       
   975 		}
       
   976 		
       
   977 		close FILE;
       
   978 	}
       
   979 	
       
   980 	return $found_header;
       
   981 }
       
   982 
       
   983 #-------------------------------------------------------
       
   984 # Convert OST traces to PRINTF traces
       
   985 #-------------------------------------------------------
       
   986 sub convertOstToPrintf
       
   987 {
       
   988 	my ($macro, @sources) = @_;
       
   989 	debug("convertOstToPrintf starts, macro is: $macro");
       
   990 	
       
   991 	# Go through all found source files
       
   992 	foreach my $source_file (@sources)
       
   993 	{
       
   994 		debug("Source file $source_file");
       
   995 		print $source_file . "\n";
       
   996 		
       
   997 		RemoveLineBreaksFromTracesInFile($source_file, 1);
       
   998 		
       
   999 		open FILE, "<$source_file" or die $!;
       
  1000 		
       
  1001 		my $new_file_content;
       
  1002 		
       
  1003 		my $traces_converted = 0;
       
  1004 		my $file_name = "";
       
  1005 		my $last_include_line = 0;
       
  1006 		my $line_number = 0;
       
  1007 		my $traceNumber = 0;
       
  1008 	
       
  1009 		if ($source_file =~ /.*\\(.+?)\..+/)
       
  1010 		{
       
  1011 			$file_name = $1;
       
  1012 		}
       
  1013 		
       
  1014 		foreach my $line (<FILE>)
       
  1015 		{
       
  1016 			$line_number += 1;
       
  1017 			
       
  1018 			my $line_converted = 0;
       
  1019 			
       
  1020 			chomp $line;
       
  1021 			
       
  1022 			if ($line =~ /^\s*\#include/)
       
  1023 			{
       
  1024 				$last_include_line = $line_number; 
       
  1025 			}
       
  1026 			
       
  1027 			if ($line =~ /Traces\.h/)
       
  1028 			{
       
  1029 				$line = "// " . $line;
       
  1030 			}
       
  1031 			
       
  1032 			# Printf macro
       
  1033 			if ($line =~ /^\s*OstTrace.*;/i)
       
  1034 			{		
       
  1035 				my $spaces;				
       
  1036 				my $trace = "";
       
  1037 				if ($line =~ /^(\s*)/)
       
  1038 				{
       
  1039 					$spaces = $1;
       
  1040 				}
       
  1041 				
       
  1042 				$line =~ s/^\s*(OstTrace.+;).*/$1/;
       
  1043 				
       
  1044 				# Remove spaces from the beginning
       
  1045 				$trace = $line;
       
  1046 				$trace =~ s/^\s*//g;
       
  1047 				
       
  1048 				print("Trace: " . $line . "\n");
       
  1049 				
       
  1050 				if (GetBracketCount($line) % 2 == 0)
       
  1051 				{									
       
  1052 					$line = $spaces . "// " . $trace ."\n";
       
  1053 					$line .= $spaces;
       
  1054 					
       
  1055 					# Remove the macro from the trace
       
  1056 					debug("Trace before removing MACRO $trace");
       
  1057 					
       
  1058 					# Check if Function Entry or Exit trace
       
  1059 					if ($trace =~ /^\s*OstTraceFunction.*;/i)
       
  1060 					{
       
  1061 						$trace =~ /\(\s*(.*?)([,\s\)])/;
       
  1062 						$trace = "\"" . $1 . "\"";		
       
  1063 					}
       
  1064 					
       
  1065 					# Other OST traces
       
  1066 					else
       
  1067 					{
       
  1068 						$trace =~ /\((.*?,)(.*?,)\s?(.*)\)/;
       
  1069 						$trace = $3;
       
  1070 					}
       
  1071 					debug("Trace after removing MACRO $trace");
       
  1072 					
       
  1073 					# Convert
       
  1074 					$line .= $macro . "(" . $trace . ");";
       
  1075 					
       
  1076 					$new_file_content .= $line . "\n";
       
  1077 					$line_converted = 1;
       
  1078 					$traces_converted = 1;
       
  1079 					$traceNumber++;
       
  1080 				}
       
  1081 			}
       
  1082 			else
       
  1083 			{
       
  1084 				$new_file_content .= $line . "\n";
       
  1085 			}
       
  1086 		}
       
  1087 	
       
  1088 		close FILE;	
       
  1089 		
       
  1090 		if ($traces_converted == 1)
       
  1091 		{	
       
  1092 			debug("\n\nSave new source file");
       
  1093 			SaveNewSourceFile($source_file, $new_file_content, $last_include_line, $file_name, 0);
       
  1094 		}
       
  1095 	}	
       
  1096 }
       
  1097 
       
  1098 #-------------------------------------------------------
       
  1099 # Get source files
       
  1100 #-------------------------------------------------------
       
  1101 sub getSourceFiles
       
  1102 {
       
  1103 	debug("getSourceFiles starts");
       
  1104 	my @sources;
       
  1105 	
       
  1106 	# Open mmp file
       
  1107 	foreach my $mmp_file (@ARGV)
       
  1108 	{
       
  1109 		debug("getSourceFiles MMP file: $mmp_file");
       
  1110 		my $file_path = $mmp_file;
       
  1111 		$file_path =~ s/\\[^\\]+$/\\/i;
       
  1112 		$file_path =~ s/\\\\/\\/g;
       
  1113 	  if ($file_path =~ m/\.mmp/i)
       
  1114 	  {
       
  1115 	  	debug("getSourceFiles file doesn't have path. Use current dir");
       
  1116 	  	my $dir = "$Bin";
       
  1117 	  	$file_path = $dir;
       
  1118 	  	$file_path =~ s/\//\\/g;
       
  1119 	  } 
       
  1120 	
       
  1121 		# print "File path: $file_path\n";
       
  1122 		
       
  1123 		my $module_name = $mmp_file;
       
  1124 		$module_name =~ s/.*\\([^\.]*).*/$1/;
       
  1125 		
       
  1126 		# print "Module name: $module_name\n";
       
  1127 		
       
  1128 		my $uid = 0;
       
  1129 		my $current_src_path;
       
  1130 		
       
  1131 		if (-e $mmp_file)
       
  1132 		{
       
  1133 			debug("getSourceFiles MMP file exists");
       
  1134 			# Go through lines
       
  1135 			open FILE, "<$mmp_file" or die $!;
       
  1136 			foreach my $line (<FILE>)
       
  1137 			{
       
  1138 				$line =~ s/\//\\/g;
       
  1139 				
       
  1140 				# Find uid
       
  1141 				if ($line =~ /uid.+0x([a-fA-F0-9]+)?/i)
       
  1142 			  {
       
  1143 			  	$uid = $1;
       
  1144 			  	debug("getSourceFiles Found UID: $uid");
       
  1145 			  	# print "Uid: $uid\n";
       
  1146 			  }
       
  1147 			   
       
  1148 			  # Find source path 
       
  1149 			  if ($line =~ /sourcepath\s+([^\s]+)/i)
       
  1150 			  {
       
  1151 			  	my $src_path = $1;
       
  1152 			  	
       
  1153 			  	# Check absolute path
       
  1154 			  	if ($src_path =~ /^[^\\]/)
       
  1155 					{
       
  1156 			  		$current_src_path = concatenatePath($file_path, $src_path . "\\");
       
  1157 			  	}
       
  1158 			  	else
       
  1159 			  	{
       
  1160 			  		$current_src_path = substr($file_path, 0, 2) . $src_path;
       
  1161 			  	}
       
  1162 			  	
       
  1163 			  	debug("getSourceFiles current src path: $current_src_path");
       
  1164 			  	# print "Current src path: $current_src_path\n";
       
  1165 			  }
       
  1166 			  
       
  1167 			  while ($line =~ /source\s+([^\s]+)/i)
       
  1168 			  {
       
  1169 			  	my $src = $1;
       
  1170 			  	
       
  1171 					my $src_path = concatenatePath($current_src_path, $src);
       
  1172 			  	
       
  1173 			  	if (-e $src_path)
       
  1174 			  	{
       
  1175 						push (@sources, $src_path);
       
  1176 			  		debug("getSourceFiles found source: $src_path");
       
  1177 			  		# print "SRC: $src_path\n";
       
  1178 			  	}
       
  1179 			  	
       
  1180 			  	$line =~ s/\Q$src//;
       
  1181 			  }
       
  1182 			}
       
  1183 			close FILE;
       
  1184 			
       
  1185 		}
       
  1186 	}
       
  1187 	
       
  1188 	return @sources;
       
  1189 }
       
  1190 
       
  1191 #--------------------------------------------------------------
       
  1192 # Get operation
       
  1193 #--------------------------------------------------------------
       
  1194 sub get_operation
       
  1195 {
       
  1196 	print "\nSelect operation:\n";
       
  1197 	print "Convert Symbian traces to OST traces        (1)\n";
       
  1198 	print "Convert Kern::Printf traces to OST traces   (2)\n";
       
  1199 	print "Convert RDebug::Printf traces to OST traces (3)\n";
       
  1200 	print "Convert own MACRO to OST traces             (4)\n";
       
  1201 	print "Convert OST traces to Kern::Printf traces   (5)\n";
       
  1202 	print "Convert OST traces to RDebug::Printf traces (6)\n";
       
  1203 	  
       
  1204   my $selection = <STDIN>;
       
  1205   chomp $selection;
       
  1206   
       
  1207   if ($selection != 1 and
       
  1208   		$selection != 2 and
       
  1209   		$selection != 3 and
       
  1210   		$selection != 4 and
       
  1211   		$selection != 5 and
       
  1212   		$selection != 6)
       
  1213   {
       
  1214   	print STDERR "Invalid input!\n";
       
  1215   	die;		
       
  1216 	}
       
  1217 	
       
  1218 	if ($selection == 4)
       
  1219 	{
       
  1220 		print "\nGive the macro name (e.g. \"TRACE\" if your maco is like TRACE(\"Text\")\n";
       
  1221 		$selection = <STDIN>;
       
  1222 		chomp $selection;
       
  1223 	}
       
  1224   
       
  1225   return $selection;
       
  1226 }
       
  1227 
       
  1228 #--------------------------------------------------------------
       
  1229 # Get traces folder
       
  1230 #--------------------------------------------------------------
       
  1231 sub get_traces_folder
       
  1232 {
       
  1233 	my $traces_folder;
       
  1234 	
       
  1235 	if (not defined $traces_folder and getGroupTraceData(\%map_trace_to_group, \%map_trace_to_text, \%map_trace_to_parameters, "..\\..\\traces"))
       
  1236 	{
       
  1237 		$traces_folder = "..\\..\\traces";
       
  1238 	}
       
  1239 	
       
  1240 	if (not defined $traces_folder and getGroupTraceData(\%map_trace_to_group, \%map_trace_to_text, \%map_trace_to_parameters, "..\\traces"))
       
  1241 	{
       
  1242 		$traces_folder = "..\\traces";
       
  1243 	}
       
  1244 	
       
  1245 	if (not defined $traces_folder and getGroupTraceData(\%map_trace_to_group, \%map_trace_to_text, \%map_trace_to_parameters, "..\\..\\symbian_traces\\autogen"))
       
  1246 	{
       
  1247 		$traces_folder = "..\\..\\symbian_traces\\autogen";
       
  1248 	}
       
  1249 	
       
  1250 	if (not defined $traces_folder and getGroupTraceData(\%map_trace_to_group, \%map_trace_to_text, \%map_trace_to_parameters, "..\\..\\..\\symbian_traces\\autogen"))
       
  1251 	{
       
  1252 		$traces_folder = "..\\..\\..\\symbian_traces\\autogen";
       
  1253 	}
       
  1254 	
       
  1255 	if (not defined $traces_folder and getGroupTraceData(\%map_trace_to_group, \%map_trace_to_text, \%map_trace_to_parameters, "..\\..\\..\\..\\symbian_traces\\autogen"))
       
  1256 	{
       
  1257 		$traces_folder = "..\\..\\..\\..\\symbian_traces\\autogen";
       
  1258 	}
       
  1259 	
       
  1260 	if (not defined $traces_folder and getGroupTraceData(\%map_trace_to_group, \%map_trace_to_text, \%map_trace_to_parameters, "..\\..\\autogen"))
       
  1261 	{
       
  1262 		$traces_folder = "..\\..\\autogen";
       
  1263 	}
       
  1264 	
       
  1265 	if (not defined $traces_folder and getGroupTraceData(\%map_trace_to_group, \%map_trace_to_text, \%map_trace_to_parameters, "..\\..\\..\\autogen"))
       
  1266 	{
       
  1267 		$traces_folder = "..\\..\\..\\autogen";
       
  1268 	}
       
  1269 	
       
  1270 	if (not defined $traces_folder and getGroupTraceData(\%map_trace_to_group, \%map_trace_to_text, \%map_trace_to_parameters, "..\\..\\..\\..\\autogen"))
       
  1271 	{
       
  1272 		$traces_folder = "..\\..\\..\\..\\autogen";
       
  1273 	}
       
  1274 	
       
  1275 	if (not defined $traces_folder)
       
  1276 	{
       
  1277 		print STDERR "Cannot find traces folder...";
       
  1278 		exit;
       
  1279 	}
       
  1280 	return $traces_folder;
       
  1281 }
       
  1282 
       
  1283 
       
  1284 #-------------------------------------------------------
       
  1285 # Concatenate path
       
  1286 #-------------------------------------------------------
       
  1287 sub concatenatePath
       
  1288 {
       
  1289 	my ($concatenatePathBase, $concatenatePathFile) = @_;
       
  1290 	
       
  1291 	my $backCount = 0;
       
  1292 	
       
  1293 	# Find how many back references there are and remove them
       
  1294 	while ($concatenatePathFile =~ /\.\.\\/g) 
       
  1295 	{ 
       
  1296 		$backCount++ 
       
  1297 	}
       
  1298 	$concatenatePathFile =~ s/\.\.\\//g;
       
  1299 	
       
  1300 	# If there is \ in the end of the base remove it
       
  1301 	my $lastChar = chop($concatenatePathBase);
       
  1302 	if ($lastChar ne "\\")
       
  1303 	{
       
  1304 		$concatenatePathBase = "$concatenatePathBase$lastChar";
       
  1305 	}
       
  1306 	
       
  1307 	# Remove directories from the end of the path
       
  1308 	$concatenatePathBase = reverse($concatenatePathBase);
       
  1309 	for (my $i=0; $i<$backCount; $i++)
       
  1310 	{
       
  1311 		$concatenatePathBase =~ s/.*?\\//;
       
  1312 	}
       
  1313 	$concatenatePathBase = reverse($concatenatePathBase);
       
  1314 	
       
  1315 	my $concatenatePathFullFilePath = "$concatenatePathBase\\$concatenatePathFile";
       
  1316 	
       
  1317 	return $concatenatePathFullFilePath;	
       
  1318 }
       
  1319 
       
  1320 
       
  1321 #-------------------------------------------------------
       
  1322 # Debug print
       
  1323 #-------------------------------------------------------
       
  1324 sub debug
       
  1325 {
       
  1326 	my ($print) = @_;
       
  1327 	
       
  1328 	if ($DEBUG)
       
  1329 	{
       
  1330 		print $print . "\n";
       
  1331 	}
       
  1332 }
       
  1333 
       
  1334 warn "\n";
       
  1335 warn "Warning: this script is not supported and the Dynamic Analysis Tools team\n";
       
  1336 warn "does NOT promise to fix any bugs or add any functionality to it.\n";
       
  1337 warn "\n";