sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.instr/src/com/nokia/carbide/cpp/pi/instr/BinaryReader122.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.EOFException;
       
    22 import java.io.File;
       
    23 import java.io.FileInputStream;
       
    24 import java.io.FileReader;
       
    25 import java.io.IOException;
       
    26 import java.io.InputStreamReader;
       
    27 import java.util.Hashtable;
       
    28 import java.util.regex.Matcher;
       
    29 import java.util.regex.Pattern;
       
    30 
       
    31 import com.nokia.carbide.cpp.internal.pi.model.Binary;
       
    32 import com.nokia.carbide.cpp.pi.importer.SampleImporter;
       
    33 import com.nokia.carbide.cpp.pi.importer.SampleImporter.PkgObyFile;
       
    34 import com.nokia.carbide.cpp.pi.util.GeneralMessages;
       
    35 import com.nokia.carbide.cpp.pi.util.GuessAndFixPath;
       
    36 
       
    37 
       
    38 /*
       
    39  * This class is a reader of OBY/PKG file for building two hashes
       
    40  * with the support of IttTrace22(the real trace reader)
       
    41  * 1. binary(host side) => map files hash for using map file
       
    42  * 2. string(host side) => binary hash for using ROFS symbol file
       
    43  * 
       
    44  * it reads OBY/PKG line by line and for host/target reference and 
       
    45  * update records of "Binary" from dynamic binary trace.
       
    46  * 
       
    47  */
       
    48 
       
    49 public class BinaryReader122
       
    50 {
       
    51 	private boolean debug = false;
       
    52 	private IttTrace122 ittTrace122;
       
    53 	
       
    54 	private Hashtable<Binary, MapFile> binaryToMapFile;
       
    55 	private Hashtable<String, Binary> hostNameToBinary;	// so we can adjust ROFS symbol
       
    56 	
       
    57 	private Pattern stopPattern     = Pattern.compile("(?:\\p{Blank}*)(?:[S|s][T|t][O|o][P|p])(?:\\p{Blank}+(?!\\p{Blank}).*|\\p{Blank}*)"); //$NON-NLS-1$
       
    58 	private Pattern commentPattern  = Pattern.compile("(?:\\p{Blank}*)(?:[R|r][E|e][M|m])(?:\\p{Blank}+(?!\\p{Blank}).*|\\p{Blank}*)"); //$NON-NLS-1$
       
    59 	private Pattern fileSpecPattern = Pattern.compile("(?:\\p{Blank}*)(?:data|file|primary|secondary|variant|device|extension|dll|filecompress|fileuncompress)(?:(?:\\[.*?\\])?)(?:\\p{Blank}*=\\p{Blank}*)((?:\\S|[ ])+(?:\\S))(?:\\p{Blank}+)(?:\")((?:\\S|[ ])+(?:\\S))(?:\")(?:.*)"); //$NON-NLS-1$
       
    60 	
       
    61 	public BinaryReader122(IttTrace122 ittTrace122)
       
    62 	{
       
    63 		binaryToMapFile = new Hashtable<Binary, MapFile>();
       
    64 		setHostNameToBinary(new Hashtable<String,Binary>());
       
    65 		
       
    66 		this.ittTrace122 = ittTrace122;
       
    67 		
       
    68 		
       
    69 		for(PkgObyFile currentFile : SampleImporter.getInstance().getPkgObyFilesList()) {
       
    70 			String fileName = currentFile.fileAbsolutePath;
       
    71 			
       
    72 			if (fileName.toLowerCase().endsWith(".oby") || fileName.toLowerCase().endsWith(".iby")) //$NON-NLS-1$ //$NON-NLS-2$
       
    73 				parseAndProcessObyFile(fileName);
       
    74 			else if (fileName.toLowerCase().endsWith(".pkg")) //$NON-NLS-1$
       
    75 			{
       
    76 				addFilesInPkg(fileName);
       
    77 			}
       
    78 			else
       
    79 			{
       
    80 		    	GeneralMessages.showErrorMessage(Messages.getString("BinaryReader122.wrongEKA2FileType1")+fileName+Messages.getString("BinaryReader122.wrongEKA2FileType2")); //$NON-NLS-1$ //$NON-NLS-2$
       
    81 			}
       
    82 		}		
       
    83 	}
       
    84 	
       
    85 	public MapFile getMapFileForBinary(Binary b)
       
    86 	{
       
    87 		return this.binaryToMapFile.get(b);
       
    88 	}
       
    89 	
       
    90 	public Binary getBinaryForHostName(String s)
       
    91 	{
       
    92 		return this.getHostNameToBinary().get(s);
       
    93 	}
       
    94 	
       
    95 	  public void addFilesInPkg(String pkgName)
       
    96 	  {
       
    97 		  long lineNumber = 0;
       
    98 		  File pkgFile = new File(pkgName);
       
    99 		  if (pkgFile.exists() && !pkgFile.isDirectory())
       
   100 		  {
       
   101 			  String localFile = ""; //$NON-NLS-1$
       
   102 			  String mapName = ""; //$NON-NLS-1$
       
   103 			  try
       
   104 			  {
       
   105 				  BufferedReader br = new BufferedReader(new FileReader(pkgFile));
       
   106 				  String line;
       
   107 				  
       
   108 				  while((line = br.readLine()) != null)
       
   109 				  {
       
   110 					  ++lineNumber;
       
   111 					  
       
   112 					  // find comment lines, which are started with a ';'
       
   113 					  int first = line.indexOf(";"); //$NON-NLS-1$
       
   114 					  
       
   115 					  if (first != -1) {
       
   116 						  int i = 0;
       
   117 						  for (; i < first; i++)
       
   118 						  {
       
   119 							  if (line.charAt(i) != ' ' && line.charAt(i) != '\t')
       
   120 								  break;
       
   121 						  }
       
   122 						  if (i == first)
       
   123 							  continue;
       
   124 					  }
       
   125 					  
       
   126 					  //System.out.println(line);
       
   127 					  first = line.indexOf("\""); //$NON-NLS-1$
       
   128 					  int second = -1;
       
   129 					  int third = -1;
       
   130 					  int fourth = -1;
       
   131 					  
       
   132 					  if (first != -1)
       
   133 						  second = line.indexOf("\"",first+1); //$NON-NLS-1$
       
   134 					  
       
   135 					  if (second != -1)
       
   136 						  third = line.indexOf("\"!:",second+1); //$NON-NLS-1$
       
   137 					  
       
   138 					  if (third != -1)
       
   139 						  fourth = line.indexOf("\"",third+1); //$NON-NLS-1$
       
   140 						  
       
   141 					  //System.out.println(first+" "+second+" "+third+" "+fourth);
       
   142 					  if (fourth != -1)
       
   143 					  {
       
   144 						  Binary binary = null;
       
   145 						  
       
   146 						  localFile = line.substring(first+1,second);
       
   147 						  String remoteFile = line.substring(third+3,fourth);
       
   148 						  
       
   149 						  // .PKG is referring everything as root relative,
       
   150 						  // sometime there is reference to $(EPOCROOT) too
       
   151 						  // let's try to guess what it was. I hate windows drive letter
       
   152 						  if (localFile.charAt(0) == '\\') {
       
   153 							  localFile = GuessAndFixPath.fixPath(localFile, "", pkgName);	//$NON-NLS-1$
       
   154 						  } else if (localFile.charAt(1) != ':') {
       
   155 							  // not absolute drive, resolve relative to PKG file
       
   156 							  File relativeMapFile = new File(pkgFile.getParent(), localFile);
       
   157 							  if (relativeMapFile.exists()) {
       
   158 								  localFile = relativeMapFile.getCanonicalPath();
       
   159 							  }
       
   160 						  }
       
   161 						  
       
   162 						  String lowerLocal = localFile.toLowerCase();
       
   163 						  if (BinaryReader.checkFileExtension(lowerLocal))
       
   164 						  {
       
   165 							  if (debug)System.out.println(Messages.getString("BinaryReader122.addingPkgFile")+lowerLocal); //$NON-NLS-1$
       
   166 			        		  
       
   167 				        	  binary = this.ittTrace122.getBinaryForFileName(remoteFile);
       
   168 				        	  if (debug)System.out.println(Messages.getString("BinaryReader122.gotBinaryPkgFile")+lowerLocal); //$NON-NLS-1$
       
   169 				        	  
       
   170 					          if (binary != null)
       
   171 					          {
       
   172 					        	  if (debug)System.out.println(Messages.getString("BinaryReader122.foundPkgBinary1")+binary.binaryName+Messages.getString("BinaryReader122.foundPkgBinary2")+localFile); //$NON-NLS-1$ //$NON-NLS-2$
       
   173 				        		  binary.binaryName = localFile;
       
   174 				        		  getHostNameToBinary().put(binary.binaryName, binary);
       
   175 					          }
       
   176 							  
       
   177 					          mapName = localFile+".map"; //$NON-NLS-1$
       
   178 					          File mapFile = new File(mapName);
       
   179 					          MapFile mf = new MapFile(mapFile, pkgFile.toString(), lineNumber);
       
   180 							  if (mapFile.exists() && binary != null)
       
   181 			        		  {
       
   182 			        			  try
       
   183 			        			  {
       
   184 			        				  if (binary != null)
       
   185 			        				  {
       
   186 							        	  if (debug)System.out.println(Messages.getString("BinaryReader122.canGetBinaryFrom1")+binary.binaryName+Messages.getString("BinaryReader122.canGetBinaryFrom2")+localFile); //$NON-NLS-1$ //$NON-NLS-2$
       
   187 			        					  binaryToMapFile.put(binary,mf);
       
   188 			        				  }
       
   189 			        				  else
       
   190 			        				  {
       
   191 			        					  if (debug)System.out.println(Messages.getString("BinaryReader122.cannotCreatePkgFileFrom") + mapName); //$NON-NLS-1$
       
   192 			        				  }
       
   193 			        			  }
       
   194 			        			  catch (Exception e)
       
   195 			        			  {
       
   196 			        				  if (debug)System.out.println(Messages.getString("BinaryReader122.pkgNotOK")+remoteFile); //$NON-NLS-1$
       
   197 			        			  }
       
   198 			        		  }
       
   199 						  }
       
   200 					  }
       
   201 				  } // while((line = br.readLine()) != null)
       
   202 			  } catch (Exception e) {
       
   203 				  // either malformed PKG or we are not robust enough
       
   204 				  GeneralMessages.showErrorMessage (Messages.getString("BinaryReader122.pkgMalform")); //$NON-NLS-1$
       
   205 			  }
       
   206 		  }
       
   207 	  }
       
   208 	
       
   209 	public void parseAndProcessObyFile(String obyFile) {
       
   210 		File f = new File (obyFile);
       
   211 		
       
   212 		try {
       
   213 			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
       
   214 			String line = br.readLine();
       
   215 			long lineNumber = 1;
       
   216 			
       
   217 			while (line != null) {
       
   218 				Matcher stopMatcher = stopPattern.matcher(line);
       
   219 				if (stopMatcher.matches()) {
       
   220 					break;
       
   221 				}
       
   222 				Matcher commentMatcher = commentPattern.matcher(line); //$NON-NLS-1$
       
   223 				if (!commentMatcher.matches()) {
       
   224 					// case is sensitive in OBY keywords
       
   225 					// only read file spec from OBY
       
   226 					Matcher fileSpecMatcher = fileSpecPattern.matcher(line); //").matcher(line); //$NON-NLS-1$
       
   227 					if (fileSpecMatcher.matches()) {
       
   228 						Binary binary = ittTrace122.getBinaryForFileName(fileSpecMatcher.group(2).trim());
       
   229 						if (binary != null) {
       
   230 							binary.binaryName = fileSpecMatcher.group(1).trim();
       
   231 							getHostNameToBinary().put(binary.binaryName, binary);
       
   232 							
       
   233 							String pcFileName = fileSpecMatcher.group(1).trim();
       
   234 							// .OBY is referring everything as root relative,
       
   235 							// sometime there is reference to $(EPOCROOT) too
       
   236 							// let's try to guess what it was. I hate windows drive letter
       
   237 							if (pcFileName.charAt(0) == '\\') {
       
   238 								pcFileName = GuessAndFixPath.fixPath(pcFileName, SampleImporter.getInstance().getRomEpocroot(), ""); //$NON-NLS-1$ //$NON-NLS-2$
       
   239 							}
       
   240 							if (pcFileName.endsWith("\"")) //$NON-NLS-1$
       
   241 								pcFileName = pcFileName.substring(0, pcFileName.length() - 1);
       
   242 							
       
   243 							pcFileName += ".map"; //$NON-NLS-1$
       
   244 							File pcFile = new File(pcFileName);
       
   245 							if (pcFile.exists())
       
   246 							{
       
   247 								MapFile mf = new MapFile(pcFile, obyFile, lineNumber);
       
   248 								binaryToMapFile.put(binary,mf);
       
   249 							} else {
       
   250 								  // .map not found
       
   251 								  if(pcFileName.endsWith(".exe.map") || //$NON-NLS-1$
       
   252 										  pcFileName.endsWith(".dll.map") || //$NON-NLS-1$
       
   253 										  pcFileName.endsWith(".ldd.map") || //$NON-NLS-1$
       
   254 										  pcFileName.endsWith(".pdd.map") || //$NON-NLS-1$
       
   255 										  pcFileName.endsWith(".app.map"))  //$NON-NLS-1$
       
   256 								  {
       
   257 									  String myMessage = Messages.getString("BinaryReader122.map.file") +  pcFileName + Messages.getString("BinaryReader122.not.found"); //$NON-NLS-1$ //$NON-NLS-2$
       
   258 									  if (obyFile != null && obyFile.length() > 0) {
       
   259 										  myMessage += Messages.getString("BinaryReader122.referenced.by") + obyFile; //$NON-NLS-1$
       
   260 									  }
       
   261 									  if (lineNumber > 0) {
       
   262 										  myMessage += Messages.getString("BinaryReader122.line.number") + lineNumber; //$NON-NLS-1$
       
   263 									  }
       
   264 
       
   265 									  // if it is code binary...
       
   266 								      GeneralMessages.PiLog(myMessage, GeneralMessages.WARNING);
       
   267 								  }
       
   268 							}
       
   269 						}
       
   270 						else if (debug) {
       
   271 							System.out.print(fileSpecMatcher.group(1));
       
   272 							System.out.print("|"); //$NON-NLS-1$
       
   273 							System.out.print(fileSpecMatcher.group(2));
       
   274 							System.out.println(""); //$NON-NLS-1$
       
   275 						}
       
   276 					}
       
   277 				}
       
   278 				
       
   279 				line = br.readLine();
       
   280 				++lineNumber;
       
   281 			}
       
   282 		} catch (EOFException e) {
       
   283 			// good, that's the end of file, bail out peacefully
       
   284 		} catch (IOException e) {
       
   285 			GeneralMessages.PiLog(Messages.getString("BinaryReader122.IOException.on") + obyFile, GeneralMessages.ERROR, e); //$NON-NLS-1$
       
   286 		}
       
   287 	}
       
   288 	
       
   289 	public void setHostNameToBinary(Hashtable<String, Binary> hostNameToBinary) {
       
   290 		this.hostNameToBinary = hostNameToBinary;
       
   291 	}
       
   292 
       
   293 	public Hashtable<String, Binary> getHostNameToBinary() {
       
   294 		return hostNameToBinary;
       
   295 	}
       
   296 	
       
   297 	public int parsedMapFileCount() {
       
   298 		return this.binaryToMapFile.size();
       
   299 	}
       
   300 }