tracefw/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/project/GroupNames.java
branchRCL_3
changeset 21 52e343bb8f80
parent 20 ca8a1b6995f6
child 22 e26895079d7c
equal deleted inserted replaced
20:ca8a1b6995f6 21:52e343bb8f80
     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  * Default names for trace groups
       
    17  *
       
    18  */
       
    19 package com.nokia.tracecompiler.project;
       
    20 
       
    21 import java.io.BufferedReader;
       
    22 import java.io.File;
       
    23 import java.io.FileReader;
       
    24 import java.io.IOException;
       
    25 import java.util.Enumeration;
       
    26 import java.util.Vector;
       
    27 
       
    28 import com.nokia.tracecompiler.TraceCompilerLogger;
       
    29 import com.nokia.tracecompiler.TraceCompilerRootException;
       
    30 import com.nokia.tracecompiler.engine.TraceCompilerEngineGlobals;
       
    31 import com.nokia.tracecompiler.model.TraceCompilerException;
       
    32 
       
    33 /**
       
    34  * Default names for trace groups
       
    35  * 
       
    36  */
       
    37 public class GroupNames {
       
    38 
       
    39 	public static Vector<GroupNameIdPair> DEFAULT_GROUPS;
       
    40 
       
    41 	/**
       
    42 	 * epoc32 directory
       
    43 	 */
       
    44 	private static final String EPOC32_DIRECTORY = "epoc32"; //$NON-NLS-1$	
       
    45 	
       
    46 	/**
       
    47 	 * Start of user-defined groups
       
    48 	 */
       
    49 	public static int USER_GROUP_ID_FIRST = -1;
       
    50 
       
    51 	/**
       
    52 	 * end of user-defined groups
       
    53 	 */
       
    54 	public static int USER_GROUP_ID_LAST = -1;
       
    55 
       
    56 	/**
       
    57 	 * Constant to be used for EPOCROOT
       
    58 	 */
       
    59 	public static String EPOCROOT = "EPOCROOT"; //$NON-NLS-1$
       
    60 
       
    61 	/**
       
    62 	 * Constant to be used for the location of opensystemtrace_types.h
       
    63 	 */
       
    64 	public static String OST_TYPES = "epoc32" + File.separator + "include" + File.separator + "platform" + File.separator + "opensystemtrace_types.h"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
       
    65 
       
    66 	/**
       
    67 	 * Prefix for all default groups
       
    68 	 */
       
    69 	public static String DEFAULT_GROUP_PREFIX = "TRACE_"; //$NON-NLS-1$
       
    70 
       
    71 	/**
       
    72 	 * TRACE_BORDER group name
       
    73 	 */
       
    74 	public static final String TRACE_BORDER = "TRACE_BORDER"; //$NON-NLS-1$
       
    75 
       
    76 	/**
       
    77 	 * TRACE_API group name
       
    78 	 */
       
    79 	public static final String TRACE_API = "TRACE_API"; //$NON-NLS-1$
       
    80 
       
    81 	/**
       
    82 	 * TRACE_DUMP group name
       
    83 	 */
       
    84 	public static final String TRACE_DUMP = "TRACE_DUMP"; //$NON-NLS-1$
       
    85 
       
    86 	/**
       
    87 	 * TRACE_DEBUG group name
       
    88 	 */
       
    89 	public static final String TRACE_DEBUG = "TRACE_DEBUG"; //$NON-NLS-1$
       
    90 
       
    91 	/**
       
    92 	 * TRACE_DETAILED group name
       
    93 	 */
       
    94 	public static final String TRACE_DETAILED = "TRACE_DETAILED"; //$NON-NLS-1$
       
    95 
       
    96 	/**
       
    97 	 * TRACE_INTERNALS group name
       
    98 	 */
       
    99 	public static final String TRACE_INTERNALS = "TRACE_INTERNALS"; //$NON-NLS-1$
       
   100 
       
   101 	/**
       
   102 	 * Calculates location of opensystemtrace_types.h Calls getGroupName Returns
       
   103 	 * false if there have been any errors
       
   104 	 * 
       
   105 	 * @throws TraceCompilerRootException
       
   106 	 */
       
   107 	public static void initialiseGroupName() throws TraceCompilerRootException {
       
   108 
       
   109 		String e32 = System.getenv(EPOCROOT);
       
   110 		TraceCompilerLogger.printInfo(e32);
       
   111 		if (e32 == null || (e32.length() == 0)) {
       
   112 			String msg = Messages.getString("GroupNames.0"); //$NON-NLS-1$
       
   113 			throw new TraceCompilerRootException(msg, null);
       
   114 		}
       
   115 
       
   116 		// need to check that e32 ends in a file separator
       
   117 		if (!e32.endsWith(File.separator)) {
       
   118 			TraceCompilerLogger.printInfo(Messages.getString("GroupNames.2")); //$NON-NLS-1$
       
   119 			e32 += File.separator;
       
   120 		}
       
   121 
       
   122 		// Check that EPOCROOT is valid
       
   123 		File epocRoot = new File(e32 + EPOC32_DIRECTORY);
       
   124 		if (epocRoot.exists() == false) {
       
   125 			throw new TraceCompilerRootException(Messages.getString("GroupNames.invalidEpocRoot"), null); //$NON-NLS-1$
       
   126 		}
       
   127 		
       
   128 		// tests parsing the opensystemtrace_types.h for expected values
       
   129 		String ostHeaderFilename = e32 + OST_TYPES;
       
   130 
       
   131 		// add the predefined names / ids
       
   132 		// this was changed from the previous mechanism to break array indexing
       
   133 		// dependency
       
   134 		// for linking group names to group ids
       
   135 		DEFAULT_GROUPS = new Vector<GroupNameIdPair>();
       
   136 
       
   137 		// The testing ranges are being hardcoded as the values are linked to
       
   138 		// e32btrace.h
       
   139 		// The TC would have to be redesigned to read in the values from this
       
   140 		// file, which would impact performance
       
   141 
       
   142 		DEFAULT_GROUPS.add(new GroupNameIdPair("TRACE_TESTING1", 254)); //$NON-NLS-1$
       
   143 		TraceCompilerLogger.printInfo(Messages.getString("GroupNames.1")); //$NON-NLS-1$
       
   144 		DEFAULT_GROUPS.add(new GroupNameIdPair("TRACE_TESTING2", 255)); //$NON-NLS-1$
       
   145 		TraceCompilerLogger.printInfo(Messages.getString("GroupNames.3")); //$NON-NLS-1$
       
   146 		try {
       
   147 			getGroupName(ostHeaderFilename);
       
   148 		} catch (Exception e) {
       
   149 			throw new TraceCompilerRootException(
       
   150 					Messages.getString("GroupNames.failedToProcessOstHeaderText"), e); //$NON-NLS-1$
       
   151 		}
       
   152 	}
       
   153 
       
   154 	/**
       
   155 	 * Reads opensystemtrace_types.h and extracts the GroupIds
       
   156 	 * 
       
   157 	 * @throws IOException
       
   158 	 *             if fail to read the header file
       
   159 	 * @throws TraceCompilerException
       
   160 	 */
       
   161 
       
   162 	private static void getGroupName(String headerFilename) throws Exception {
       
   163 		// TODO this method is buggy as it could process group ids even if there
       
   164 		// are commented out
       
   165 		// should use regular expression or better parser.
       
   166 
       
   167 		TraceCompilerLogger
       
   168 				.printInfo(Messages.getString("GroupNames.8") + headerFilename); //$NON-NLS-1$
       
   169 
       
   170 		BufferedReader in = new BufferedReader(new FileReader(headerFilename));
       
   171 		try {
       
   172 			String str;
       
   173 			while ((str = in.readLine()) != null) {
       
   174 				if (str.length() != 0) {
       
   175 					if (((str.indexOf("EUserDefinedRangeFirst")) != -1) && (USER_GROUP_ID_FIRST == -1)) { //$NON-NLS-1$
       
   176 						int val = getGroupIdFromString(str);
       
   177 						if (val != -1) {
       
   178 							// set user group id first
       
   179 							USER_GROUP_ID_FIRST = val;
       
   180 							TraceCompilerLogger.printInfo(Messages
       
   181 									.getString("GroupNames.4") + val); //$NON-NLS-1$
       
   182 						}
       
   183 					}
       
   184 
       
   185 					if (((str.indexOf("EUserDefinedRangeLast")) != -1) && (USER_GROUP_ID_LAST == -1)) { //$NON-NLS-1$
       
   186 						int val = getGroupIdFromString(str);
       
   187 						if (val != -1) {
       
   188 							// set user group id last
       
   189 							USER_GROUP_ID_LAST = val;
       
   190 							TraceCompilerLogger.printInfo(Messages
       
   191 									.getString("GroupNames.12") + val); //$NON-NLS-1$
       
   192 						}
       
   193 					}
       
   194 
       
   195 					if ((str.indexOf("enum TGroupIdReserved")) != -1) { //$NON-NLS-1$
       
   196 						// search for reserved
       
   197 						while ((str.indexOf("};") < 1) && (str.indexOf("} ;") < 1)) { //$NON-NLS-1$//$NON-NLS-2$
       
   198 
       
   199 							str = in.readLine();
       
   200 							if (((str.indexOf("=")) != -1) && ((str.indexOf("*")) < 1)) { //$NON-NLS-1$ //$NON-NLS-2$
       
   201 								String groupName = str.substring(0, (str
       
   202 										.indexOf("="))); //$NON-NLS-1$
       
   203 								groupName = groupName.trim();
       
   204 								String substr = str
       
   205 										.substring(
       
   206 												(str.indexOf("=")) + 1, (str.indexOf(','))); //$NON-NLS-1$
       
   207 								int groupId = -1;
       
   208 								String trim = substr.trim();
       
   209 								try {
       
   210 									groupId = Integer.parseInt(trim);
       
   211 								} catch (NumberFormatException e) {
       
   212 									// not an Int so could be a renaming
       
   213 									groupId = getIdByName(trim);
       
   214 									if (groupId == 0) {
       
   215 										TraceCompilerEngineGlobals
       
   216 												.getEvents()
       
   217 												.postErrorMessage(
       
   218 														Messages.getString("GroupNames.canNotresolveGroupIdPrefix") + trim, null, true); //$NON-NLS-1$
       
   219 										// not resolved to an existing name
       
   220 										throw new TraceCompilerException(null,
       
   221 												true);
       
   222 									}
       
   223 								}
       
   224 								DEFAULT_GROUPS.add(new GroupNameIdPair(
       
   225 										groupName, groupId));
       
   226 								TraceCompilerLogger.printInfo(groupName
       
   227 										+ " = " + groupId); //$NON-NLS-1$
       
   228 							}
       
   229 						}
       
   230 					}
       
   231 				}
       
   232 			}
       
   233 		} catch (Exception e) {
       
   234 			TraceCompilerEngineGlobals.getEvents().postErrorMessage(
       
   235 					e.getMessage(), null, true);
       
   236 			throw e;
       
   237 		} finally {
       
   238 			in.close();
       
   239 		}
       
   240 	}
       
   241 
       
   242 	/**
       
   243 	 * Reads opensystemtrace_types.h and extracts the GroupIds value from a
       
   244 	 * string containing GroupId name and value. 
       
   245 	 * Returns GroupId value, returns -1 if not found or resolved in case of group renaming
       
   246 	 */
       
   247 
       
   248 	private static int getGroupIdFromString(String str) {
       
   249 		int groupId = -1;
       
   250 		if ((str.indexOf("=")) != -1) { //$NON-NLS-1$
       
   251 			String substr = str.substring(
       
   252 					(str.indexOf("=")) + 1, (str.indexOf(','))); //$NON-NLS-1$
       
   253 
       
   254 			try {
       
   255 				// try to read the id from the header file
       
   256 				groupId = Integer.parseInt(substr.trim());
       
   257 			} catch (NumberFormatException e) {
       
   258 				// it's probably a rename, so get the id for the old name if
       
   259 				// there is one
       
   260 				groupId = getIdByName(substr.trim());
       
   261 				if (groupId == 0) {
       
   262 					// return -1 because we could not resolve it
       
   263 					groupId = -1;
       
   264 				}
       
   265 			}
       
   266 		}
       
   267 		return groupId;
       
   268 	}
       
   269 
       
   270 	/**
       
   271 	 * Searches all known GroupIds for name and then returns value. 
       
   272 	 * Returns GroupId value, 0 if not found in DEFAULT_GROUPS
       
   273 	 */
       
   274 
       
   275 	public static int getIdByName(String name) {
       
   276 		TraceCompilerLogger
       
   277 				.printInfo(Messages.getString("GroupNames.23") + name); //$NON-NLS-1$
       
   278 		for (Enumeration<GroupNameIdPair> e = GroupNames.DEFAULT_GROUPS
       
   279 				.elements(); e.hasMoreElements();) {
       
   280 			GroupNameIdPair gnidp = e.nextElement();
       
   281 			if (name.equals(gnidp.Name())) {
       
   282 				TraceCompilerLogger.printInfo(Messages
       
   283 						.getString("GroupNames.24") + gnidp.Id()); //$NON-NLS-1$
       
   284 				return gnidp.Id();
       
   285 			}
       
   286 		}
       
   287 		// the caller should decide what to do with value 0
       
   288 		return 0;
       
   289 	}
       
   290 
       
   291 }