sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.instr/src/com/nokia/carbide/cpp/pi/instr/MapFile.java
changeset 2 b9ab3b238396
child 5 844b047e260d
equal deleted inserted replaced
1:1050670c6980 2:b9ab3b238396
       
     1 /*
       
     2  * Copyright (c) 2009 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 the License "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  */
       
    17 
       
    18 package com.nokia.carbide.cpp.pi.instr;
       
    19 
       
    20 import java.io.BufferedReader;
       
    21 import java.io.File;
       
    22 import java.io.FileInputStream;
       
    23 import java.io.FileNotFoundException;
       
    24 import java.io.IOException;
       
    25 import java.io.InputStreamReader;
       
    26 import java.util.ArrayList;
       
    27 import java.util.LinkedList;
       
    28 import java.util.regex.Matcher;
       
    29 import java.util.regex.Pattern;
       
    30 
       
    31 import com.nokia.carbide.cpp.internal.pi.model.Function;
       
    32 import com.nokia.carbide.cpp.pi.util.GeneralMessages;
       
    33 
       
    34 
       
    35 public class MapFile 
       
    36 {
       
    37 	private boolean parsedMapFile = false;
       
    38 	
       
    39 	private String name;
       
    40 	private File mapFile;
       
    41 	private String referencePath;
       
    42 	private long referenceLineNumber;
       
    43 	private LinkedList<Function> functionData;
       
    44 	private ArrayList<Function> sortedFunctionData;
       
    45 	private Long currentGccLibEndingOffset = new Long(0);
       
    46 	private Function lastGccFunction = null;
       
    47 	
       
    48 	// RVCT/RVDS map file line
       
    49 	private static final Pattern rvctLinePattern = Pattern.compile("\\p{Blank}*((?!\\d)\\S.+)\\p{Blank}+(0[x|X]\\p{XDigit}+|\\d+)\\p{Blank}+(?:ARM Code|Thumb Code)\\p{Blank}+(0x\\p{XDigit}+|\\d+)\\p{Blank}*.*");	//$NON-NLS-1$
       
    50 
       
    51 	// a GCC map file line looks like this:
       
    52 	// <%x|%d> <symbol name> for function symbols
       
    53 	// symbol name cannot be number (e.g. first non space is not a digit)
       
    54 	private static final Pattern gccFuncLinePattern  = Pattern.compile("\\p{Blank}*(0[x|X]\\p{XDigit}+|\\d+)\\p{Blank}+((?!\\d)\\S.+)");	//$NON-NLS-1$
       
    55 
       
    56 	// <section> <%x|%d> <%x|%d> <symbol name>	for whole library
       
    57 	// *fill* <%x|%d> <%x|%d> 00000000 for filler
       
    58 	private static final Pattern gccLibOrFillerLinePattern = Pattern.compile("\\p{Blank}*(?:\\S*)\\p{Blank}*(0[x|X]\\p{XDigit}+|\\d+)\\p{Blank}+(0[x|X]\\p{XDigit}+|\\d+)\\p{Blank}+(\\S.+)"); //$NON-NLS-1$
       
    59 
       
    60 
       
    61 	public MapFile(File file, String referencePath, long referenceLineNumber)
       
    62 	{
       
    63 		if (!file.exists())
       
    64 		{
       
    65 			  // .map not found
       
    66 			  if(file.getName().endsWith(".exe.map") || //$NON-NLS-1$
       
    67 					  file.getName().endsWith(".dll.map") || //$NON-NLS-1$
       
    68 					  file.getName().endsWith(".ldd.map") || //$NON-NLS-1$
       
    69 					  file.getName().endsWith(".pdd.map") || //$NON-NLS-1$
       
    70 					  file.getName().endsWith(".app.map"))  //$NON-NLS-1$
       
    71 			  {
       
    72 				  flagFileNotFound(file, referencePath, referenceLineNumber);
       
    73 			  }
       
    74 		}
       
    75 		//System.out.println("Creating MAP file "+file.getAbsolutePath());
       
    76 		
       
    77 		this.name = file.getName();
       
    78 		this.functionData = new LinkedList<Function>();
       
    79 		this.sortedFunctionData = new ArrayList<Function>();
       
    80 		
       
    81 		this.mapFile = file;
       
    82 		this.referencePath = referencePath;
       
    83 		this.referenceLineNumber = referenceLineNumber;
       
    84 		
       
    85 		/*
       
    86 		if (file.getName().indexOf("wserv.exe") != -1)
       
    87 		{
       
    88 			this.printSortedFunctions();
       
    89 			System.out.println("Map file "+file.getName()+" processed");
       
    90 		}*/
       
    91 	}
       
    92 	
       
    93 	public String getName()
       
    94 	{
       
    95 		return this.name;
       
    96 	}
       
    97 	
       
    98 	public String getFunctionNameForOffset(long offset)
       
    99 	{
       
   100 		if (!this.parsedMapFile) {
       
   101 			this.parseMapFile();
       
   102 			this.copyToSortedFunctionData();
       
   103 		}
       
   104 		
       
   105 		for (Function f : sortedFunctionData)
       
   106 		{
       
   107 			// is the asked offset within the area of this function
       
   108 			// test first is the offset equal or past the first
       
   109 			// instruction of the function
       
   110 			if (offset >= f.offsetFromBinaryStart)
       
   111 			{
       
   112 				// if so, make sure that the offset is less
       
   113 				// or equal to the last instruction within 
       
   114 				// this function
       
   115 				if (offset < (f.offsetFromBinaryStart+f.length))
       
   116 				{
       
   117 					return f.functionName;
       
   118 				}
       
   119 			}
       
   120 		}
       
   121 		
       
   122 		return Messages.getString("MapFile.functionWithOffsetNotFound1")+offset+Messages.getString("MapFile.functionWithOffsetNotFound2")+this.name+ //$NON-NLS-1$ //$NON-NLS-2$
       
   123 				Messages.getString("MapFile.functionWithOffsetNotFound3")+ //$NON-NLS-1$
       
   124 				(this.sortedFunctionData.get(this.sortedFunctionData.size() - 1)).offsetFromBinaryStart; 		 //$NON-NLS-1$
       
   125 	}
       
   126 	
       
   127 	public Function getFunctionForOffset(long offset)
       
   128 	{
       
   129 		if (!this.parsedMapFile) {
       
   130 			this.parseMapFile();
       
   131 			this.copyToSortedFunctionData();
       
   132 		}
       
   133 
       
   134 		for (Function f : sortedFunctionData)
       
   135 		{
       
   136 			// is the asked offset within the area of this function
       
   137 			// test first is the offset equal or past the first
       
   138 			// instruction of the function
       
   139 			if (offset >= f.offsetFromBinaryStart)
       
   140 			{
       
   141 				// if so, make sure that the offset is less
       
   142 				// or equal to the last instruction within 
       
   143 				// this function
       
   144 				if (offset < (f.offsetFromBinaryStart+f.length))
       
   145 				{
       
   146 					return f;
       
   147 				}
       
   148 			}
       
   149 		}
       
   150 		
       
   151 		return null; 		
       
   152 	}
       
   153 	
       
   154 	public long getFunctionLengthForOffset(long offset)
       
   155 	{
       
   156 		if (!this.parsedMapFile) {
       
   157 			this.parseMapFile();
       
   158 			this.copyToSortedFunctionData();
       
   159 		}
       
   160 
       
   161 		for (Function f : sortedFunctionData)
       
   162 		{
       
   163 			// is the asked offset within the area of this function
       
   164 			// test first is the offset equal or past the first
       
   165 			// instruction of the function
       
   166 			if (offset >= f.offsetFromBinaryStart)
       
   167 			{
       
   168 				// if so, make sure that the offset is less
       
   169 				// or equal to the last instruction within 
       
   170 				// this function
       
   171 				if (offset < (f.offsetFromBinaryStart+f.length))
       
   172 				{
       
   173 					return f.length;
       
   174 				}
       
   175 			}
       
   176 		}
       
   177 		return 0; 		
       
   178 	}
       
   179 	
       
   180 	public long getOffsetFromBinaryStartForFunction(String functionName)
       
   181 	{
       
   182 		if (!this.parsedMapFile) {
       
   183 			this.parseMapFile();
       
   184 			this.copyToSortedFunctionData();
       
   185 		}
       
   186 
       
   187 		for (Function f : sortedFunctionData)
       
   188 		{
       
   189 			if (f.functionName.equals(functionName))
       
   190 			{
       
   191 				return f.offsetFromBinaryStart;
       
   192 			}
       
   193 		}
       
   194 		return -1;
       
   195 	}
       
   196 	
       
   197 	private Function FunctionFromTokens(String funcNameToken, String funcOffsetToken, String funcLengthToken)
       
   198 	{	
       
   199 		Function f = new Function(funcNameToken,new Long(0),null);
       
   200 		// look for length, set it tentatively
       
   201 		// we may adjust it later
       
   202 		f.length = Long.decode(funcLengthToken);
       
   203 		f.offsetFromBinaryStart = Long.decode(funcOffsetToken);
       
   204 
       
   205 		return f;
       
   206 	}
       
   207 	
       
   208 	private void parseMapFile()
       
   209 	{
       
   210 		boolean isRVCT = false;
       
   211 		
       
   212 		System.out.println(Messages.getString("MapFile.parsingMapFile") + this.name); //$NON-NLS-1$
       
   213 		this.parsedMapFile = true;
       
   214 		
       
   215 		// read into the map and see if it's RVCT built
       
   216 		FileInputStream fis;
       
   217 		try {
       
   218 			fis = new FileInputStream(mapFile);
       
   219 			BufferedReader bufReader = new BufferedReader(new InputStreamReader(fis));
       
   220 			
       
   221 			String line = bufReader.readLine();
       
   222 			
       
   223 			if (line != null && line.length() > 0) {
       
   224 				if (line.toLowerCase().indexOf("arm linker,") != -1) //$NON-NLS-1$
       
   225 					isRVCT = true;
       
   226 				else
       
   227 					isRVCT = false;
       
   228 			} else {
       
   229 				// empty file, do nothing
       
   230 				return;
       
   231 			}
       
   232 			
       
   233 			bufReader.close();
       
   234 			fis.close();	
       
   235 			
       
   236 			fis = new FileInputStream(mapFile);		
       
   237 			bufReader = new BufferedReader(new InputStreamReader(fis));
       
   238 
       
   239 			if (isRVCT || mapFile.getAbsolutePath().toLowerCase().indexOf("armv5") != -1) //$NON-NLS-1$
       
   240 			{
       
   241 				parseRVCT(bufReader);
       
   242 			}
       
   243 			else
       
   244 			{
       
   245 				parseGCC(bufReader);
       
   246 			}
       
   247 
       
   248 			bufReader.close();
       
   249 			fis.close();	
       
   250 		} catch (FileNotFoundException e) {
       
   251 			flagFileNotFound(mapFile, referencePath, referenceLineNumber);
       
   252 		} catch (IOException e) {
       
   253 			flagIOException(mapFile, referencePath, referenceLineNumber);
       
   254 		}		
       
   255 	}
       
   256 	
       
   257 	private void parseGCC(BufferedReader bufReader) throws IOException
       
   258 	{
       
   259 		String line = bufReader.readLine();
       
   260 
       
   261 		while(line != null)
       
   262 		{
       
   263 			this.processLineGCC(line);
       
   264 			line = bufReader.readLine();
       
   265 		}
       
   266 	}
       
   267 	
       
   268 	private void parseRVCT(BufferedReader bufReader) throws IOException
       
   269 	{
       
   270 		String line = bufReader.readLine();
       
   271 
       
   272 		// find the global symbols section
       
   273 		//while(line.indexOf("Global Symbols") == -1)
       
   274 		// line = bufReader.readLine();		
       
   275 		
       
   276 		line = bufReader.readLine();
       
   277 		
       
   278 		while(line != null)
       
   279 		{
       
   280 			this.processLineRVCT(line);
       
   281 			line = bufReader.readLine();
       
   282 		}
       
   283 		bufReader.close();
       
   284 	}
       
   285 	
       
   286 	private void processLineRVCT(String line)
       
   287 	{
       
   288 		// a RVCT symbol line looks like this:
       
   289 		// <symbol name> <%x|%d> <ARM Code|Thumb Code> <%x|%d> <object>
       
   290 		// symbol name cannot be number (e.g. first non space is not a digit)
       
   291 		Matcher rvctLineMatcher = rvctLinePattern.matcher(line);
       
   292 		
       
   293 		if (rvctLineMatcher.matches())
       
   294 		{
       
   295 			String funcNameToken = rvctLineMatcher.group(1).trim();
       
   296 			// internal symbol, not a function
       
   297 			if (funcNameToken.indexOf(Messages.getString("MapFile.dollarSign")) != -1) //$NON-NLS-1$
       
   298 				return;
       
   299 			
       
   300 			String funcOffsetToken = rvctLineMatcher.group(2).trim();
       
   301 			
       
   302 			if (funcOffsetToken.equalsIgnoreCase("0x00000001") && line.contains("Thumb Code"))
       
   303 				return;
       
   304 			
       
   305 			String funcLengthToken = rvctLineMatcher.group(3).trim();
       
   306 			
       
   307 			Function f = FunctionFromTokens(funcNameToken, funcOffsetToken, funcLengthToken);
       
   308 			
       
   309 			this.insertToFunctionData(f);
       
   310 		}
       
   311 	}
       
   312 		
       
   313 	// our current parser picked up too many trash, let's try our best here
       
   314 	private boolean qualifyGCCSymbol(long address, String symbol) {
       
   315 		if (symbol == null || symbol.length() <= 0)
       
   316 			return false;
       
   317 		
       
   318 		// zero address on ARM, you must be kidding. This isn't a real program address
       
   319 		if (address <= 0) {
       
   320 			return false;
       
   321 		}
       
   322 		if (symbol.contains("(size before relaxing")) { //$NON-NLS-1$
       
   323 			return false;
       
   324 		}
       
   325 		if (symbol.contains("PROVIDE (")) { //$NON-NLS-1$
       
   326 			return false;
       
   327 		}
       
   328 		// you better be kidding if this is a C symbol, it's linker symbol
       
   329 		if (symbol.charAt(0) == '.') {
       
   330 			return false;
       
   331 		}
       
   332 		if (symbol.equals("_DYNAMIC")) { //$NON-NLS-1$
       
   333 			return false;
       
   334 		}
       
   335 		if (symbol.contains("vtable ") || symbol.contains("typeinfo ")) { //$NON-NLS-1$ //$NON-NLS-2$
       
   336 			return false;
       
   337 		}
       
   338 		if (symbol.contains("= .")) { //$NON-NLS-1$
       
   339 			return false;
       
   340 		}
       
   341 		return true;
       
   342 	}
       
   343 	
       
   344 	private void processLineGCC(String line)
       
   345 	{	
       
   346 		// a GCC symbol line looks like this:
       
   347 		// <%x|%d> <symbol name> for function symbols
       
   348 		// symbol name cannot be number (e.g. first non space is not a digit)
       
   349 		Matcher gccFuncLineMatcher  = gccFuncLinePattern.matcher(line);	//$NON-NLS-1$
       
   350 		// <section> <%x|%d> <%x|%d> <symbol name>	for whole library
       
   351 		// *fill* <%x|%d> <%x|%d> 00000000 for filler
       
   352 		Matcher gccLibOrFillerLineMatcher = gccLibOrFillerLinePattern.matcher(line); //$NON-NLS-1$
       
   353 		
       
   354 		Function f = null;
       
   355 		Long currentLineOffset = currentGccLibEndingOffset;
       
   356 		
       
   357 
       
   358 		if (gccFuncLineMatcher.matches())
       
   359 		{
       
   360 			String funcNameToken = gccFuncLineMatcher.group(2).trim();
       
   361 			String funcOffsetToken = gccFuncLineMatcher.group(1).trim();
       
   362 			String funcLengthToken = Messages.getString("MapFile.zero"); //$NON-NLS-1$
       
   363 			
       
   364 			f = FunctionFromTokens(funcNameToken, funcOffsetToken, funcLengthToken);
       
   365 			
       
   366 			// Some GCC symbol may be bogus
       
   367 			if (qualifyGCCSymbol(f.offsetFromBinaryStart, funcNameToken)) {
       
   368 				this.insertToFunctionData(f);
       
   369 			} 
       
   370 			
       
   371 			if (lastGccFunction != null){
       
   372 				// calculate size of last function with offset from current line
       
   373 				if (f.offsetFromBinaryStart > lastGccFunction.offsetFromBinaryStart &&
       
   374 						f.offsetFromBinaryStart < currentGccLibEndingOffset) {
       
   375 					currentLineOffset = f.offsetFromBinaryStart;
       
   376 				}
       
   377 			}
       
   378 			
       
   379 		} else if (gccLibOrFillerLineMatcher.matches()) {
       
   380 			String libOffsetToken = gccLibOrFillerLineMatcher.group(1).trim();
       
   381 			String libLengthToken = gccLibOrFillerLineMatcher.group(2).trim();
       
   382 			// next time around we will use the new library offset
       
   383 			currentGccLibEndingOffset = Long.decode(libLengthToken) + Long.decode(libOffsetToken);				
       
   384 		} else {
       
   385 			// next time around we will use the new library offset
       
   386 			currentGccLibEndingOffset = new Long(0);
       
   387 		}
       
   388 		
       
   389 		// update last function's size if needed
       
   390 		if (lastGccFunction != null)
       
   391 		{
       
   392 			if (currentLineOffset > lastGccFunction.offsetFromBinaryStart) {
       
   393 				lastGccFunction.length = currentLineOffset - lastGccFunction.offsetFromBinaryStart;
       
   394 			}
       
   395 		}
       
   396 		
       
   397 		// track function on this line as last function, or null if this line is not a function
       
   398 		lastGccFunction = f;
       
   399 
       
   400 	}
       
   401 	
       
   402 	private void insertToFunctionData(Function function)
       
   403 	{
       
   404 		if (functionData.size() == 0) 
       
   405 		{
       
   406 			functionData.addFirst(function);
       
   407 		}
       
   408 		else if ((functionData.getFirst()).offsetFromBinaryStart 
       
   409 					< function.offsetFromBinaryStart)
       
   410 		{
       
   411 			functionData.addFirst(function);
       
   412 		}
       
   413 		else if ((functionData.getLast()).offsetFromBinaryStart 
       
   414 					> function.offsetFromBinaryStart)
       
   415 		{
       
   416 			functionData.addLast(function);
       
   417 		}
       
   418 		else
       
   419 		{
       
   420 			for (int i=0;i<functionData.size();i++)
       
   421 			{
       
   422 				if ((functionData.get(i)).offsetFromBinaryStart 
       
   423 						< function.offsetFromBinaryStart)
       
   424 				{
       
   425 					functionData.add(i,function);
       
   426 					break;
       
   427 				}
       
   428 			}
       
   429 		}
       
   430 	}
       
   431 	
       
   432 	private void copyToSortedFunctionData()
       
   433 	{
       
   434 		if (this.functionData.size() <= 0) {
       
   435 			return;
       
   436 		}
       
   437 		this.sortedFunctionData.clear();
       
   438 		long start = (this.functionData.getLast()).offsetFromBinaryStart;
       
   439 		Function previous = null;
       
   440 		boolean reallyAdded = false;
       
   441 		
       
   442 		for (int i=this.functionData.size()-1;i>=0;i--)
       
   443 		{
       
   444 			//System.out.println(i);
       
   445 			Function f = this.functionData.get(i);
       
   446 			f.offsetFromBinaryStart = f.offsetFromBinaryStart - start;
       
   447 			
       
   448 			if (this.sortedFunctionData.size() == 0)
       
   449 			{
       
   450 				// add the function if the vector is empty
       
   451 				this.sortedFunctionData.add(f);
       
   452 				reallyAdded = true;
       
   453 			}
       
   454 			else if ( (this.sortedFunctionData.get(this.sortedFunctionData.size()-1)).offsetFromBinaryStart != f.offsetFromBinaryStart)
       
   455 			{
       
   456 				// add the function if the offset is not the same as with the previous line
       
   457 				this.sortedFunctionData.add(f);
       
   458 				reallyAdded = true;
       
   459 			}	
       
   460 			else if ( (this.sortedFunctionData.get(this.sortedFunctionData.size()-1)).functionName.startsWith("_")) //$NON-NLS-1$
       
   461 			{	
       
   462 				// if there is a key with this offset, discard the previous with prefix "_"
       
   463 				this.sortedFunctionData.remove(this.sortedFunctionData.get(this.sortedFunctionData.size()-1));
       
   464 				// add the new function with the same key
       
   465 				this.sortedFunctionData.add(f);
       
   466 				reallyAdded = true;
       
   467 			}
       
   468 			
       
   469 			// do this only if we really added the function to the sorted list
       
   470 			if (reallyAdded == true)
       
   471 			{
       
   472 				// store the length of the previous function
       
   473 				if (previous != null)
       
   474 					previous.length = f.offsetFromBinaryStart - previous.offsetFromBinaryStart;
       
   475 				previous = f;	
       
   476 				reallyAdded = false;
       
   477 			}
       
   478 			
       
   479 		}
       
   480 		this.functionData.clear();
       
   481 		this.functionData = null;
       
   482 	}
       
   483 	
       
   484 	/* internal test function: comment out so code coverage looks
       
   485 	 * good quantitatively
       
   486 	private void printSortedFunctions()
       
   487 	{ 
       
   488 		long totalLength = 0;
       
   489 		for (Function f : sortedFunctionData)
       
   490 		{	
       
   491 			System.out.println( f.offsetFromBinaryStart+Messages.getString("MapFile.openParenthesis")+totalLength+Messages.getString("MapFile.closeParenthesis")+Long.toHexString(f.length)+Messages.getString("MapFile.dashDash")+f.functionName); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
       
   492 			totalLength += f.length;
       
   493 		}
       
   494 	}
       
   495 	*/
       
   496 	
       
   497 	private void flagFileNotFound(File file, String referencePath, long referenceLineNumber) {
       
   498 		  String myMessage = Messages.getString("MapFile.map.file") +  file.getAbsoluteFile().getName() + Messages.getString("MapFile.not.found"); //$NON-NLS-1$ //$NON-NLS-2$
       
   499 		  if (referencePath != null && referencePath.length() > 0) {
       
   500 			  myMessage += Messages.getString("MapFile.referenced.by") + referencePath; //$NON-NLS-1$
       
   501 		  }
       
   502 		  if (referenceLineNumber > 0) {
       
   503 			  myMessage += Messages.getString("MapFile.line.number") + referenceLineNumber; //$NON-NLS-1$
       
   504 		  }
       
   505 
       
   506     	  GeneralMessages.PiLog(myMessage, GeneralMessages.ERROR);
       
   507 	}
       
   508 
       
   509 	private void flagIOException(File file, String referencePath, long referenceLineNumber) {
       
   510 		  String myMessage = Messages.getString("MapFile.map.file") + file.getAbsoluteFile().getName() + Messages.getString("MapFile.ioexception"); //$NON-NLS-1$ //$NON-NLS-2$
       
   511 		  if (referencePath != null && referencePath.length() > 0) {
       
   512 			  myMessage += Messages.getString("MapFile.referenced.by") + referencePath; //$NON-NLS-1$
       
   513 		  }
       
   514 		  if (referenceLineNumber > 0) {
       
   515 			  myMessage += Messages.getString("MapFile.line.number") + referenceLineNumber; //$NON-NLS-1$
       
   516 		  }
       
   517 
       
   518 		  GeneralMessages.showErrorMessage(myMessage);
       
   519 		  GeneralMessages.PiLog(myMessage, GeneralMessages.ERROR);
       
   520 	}
       
   521 }