crashanalysis/crashanalyser/com.nokia.s60tools.crashanalyser.corecomponents/src/com/nokia/s60tools/crashanalyser/corecomponents/interfaces/CommandLineManager.java
changeset 0 5ad7ad99af01
child 4 615035072f7e
equal deleted inserted replaced
-1:000000000000 0:5ad7ad99af01
       
     1 /*
       
     2  * Copyright (c) 2008 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  */
       
    17 
       
    18 package com.nokia.s60tools.crashanalyser.corecomponents.interfaces;
       
    19 
       
    20 import com.nokia.s60tools.crashanalyser.corecomponents.model.*;
       
    21 import com.nokia.s60tools.crashanalyser.corecomponents.model.InputXmlGenerator.XMLGeneratorAction;
       
    22 import com.nokia.s60tools.crashanalyser.corecomponents.plugin.*;
       
    23 import java.io.*;
       
    24 import java.util.*;
       
    25 import org.eclipse.core.runtime.IProgressMonitor;
       
    26 
       
    27 /**
       
    28  * This class is used for running CrashAnalyser.exe
       
    29  * 
       
    30  */
       
    31 public final class CommandLineManager {
       
    32 
       
    33 	private static final String ACTION_START_TAG = "} START - ";
       
    34 
       
    35 	/**
       
    36 	 * Verbose output
       
    37 	 */
       
    38 	static final boolean verbose = false;
       
    39 
       
    40 	/**
       
    41 	 * Progress detailed output
       
    42 	 */
       
    43 	static final boolean progress_detailed = false;
       
    44 
       
    45 	static final String PARAMETERS_XML = "parameters.xml";
       
    46 	static final String EXE_FOLDER = "Binaries";
       
    47 	static final String COMMAND_LINE_COMMAND = "CrashAnalyserConsole.exe\" -plugin CRASH_ANALYSIS -input ";
       
    48 	static final String PROGRESS_PARAMETER = " -PROGRESS";
       
    49 	static final String PROGRESS_DETAILS_PARAMETER = " -PROGRESS_DETAILS";
       
    50 	static final String VERBOSE_PARAMETER = " -V";
       
    51 
       
    52 	private CommandLineManager() {
       
    53 		// not meant to be implemented
       
    54 	}
       
    55 
       
    56 	/**
       
    57 	 * Executes CrashAnalyser.exe with Full data (i.e. creates an XML file from
       
    58 	 * e.g. MobileCrash.bin witht symbol etc. information)
       
    59 	 * 
       
    60 	 * @param outputFolder
       
    61 	 *            where output files are created
       
    62 	 * @param crashFiles
       
    63 	 *            files to be decoded
       
    64 	 * @param symbolFiles
       
    65 	 *            paths to symbol files (or null)
       
    66 	 * @param mapFilesFolder
       
    67 	 *            folder which contains map files (or null)
       
    68 	 * @param imageFiles
       
    69 	 *            paths to image files (or null)
       
    70 	 * @param fileExtension
       
    71 	 *            E.g. crashxml (not .crashxml)
       
    72 	 * @param failedFileExtension
       
    73 	 *            extension for failed files
       
    74 	 * @param selgeEventIniFile
       
    75 	 *            location of selge_event.ini
       
    76 	 * @param monitor
       
    77 	 *            for progress bar
       
    78 	 * @return true if success, false if not
       
    79 	 */
       
    80 	public static boolean executeFullAnalysis(String outputFolder,
       
    81 			String[] crashFiles, String[] symbolFiles, String mapFilesFolder,
       
    82 			String[] imageFiles, String fileExtension,
       
    83 			String failedFileExtension, String selgeEventIniFile,
       
    84 			IProgressMonitor monitor) {
       
    85 		List<String> debugMetadataFiles = new ArrayList<String>();
       
    86 
       
    87 		// collect symbol files if any provided
       
    88 		if (symbolFiles != null && symbolFiles.length > 0) {
       
    89 			for (int i = 0; i < symbolFiles.length; i++)
       
    90 				debugMetadataFiles.add(symbolFiles[i]);
       
    91 		}
       
    92 
       
    93 		// collect image files if any provided
       
    94 		if (imageFiles != null && imageFiles.length > 0) {
       
    95 			for (int i = 0; i < imageFiles.length; i++)
       
    96 				debugMetadataFiles.add(imageFiles[i]);
       
    97 		}
       
    98 
       
    99 		String[] debugMetadata = null;
       
   100 		if (!debugMetadataFiles.isEmpty())
       
   101 			debugMetadata = debugMetadataFiles
       
   102 					.toArray(new String[debugMetadataFiles.size()]);
       
   103 
       
   104 		String mapFolder = mapFilesFolder;
       
   105 		if (mapFilesFolder != null && "".equals(mapFilesFolder))
       
   106 			mapFolder = null;
       
   107 
       
   108 		InputXmlGenerator xml = new InputXmlGenerator(
       
   109 				XMLGeneratorAction.ANALYSE_CRASH_FULL, crashFiles, null,
       
   110 				debugMetadata, mapFolder, null, null, outputFolder, "."
       
   111 						+ fileExtension, "." + failedFileExtension,
       
   112 				selgeEventIniFile);
       
   113 		return createAndExecute(xml, monitor);
       
   114 	}
       
   115 
       
   116 	/**
       
   117 	 * Executes CrashAnalyser.exe with Summary data (i.e. creates quickly an XML
       
   118 	 * file from e.g. MobileCrash.bin without symbol etc. information)
       
   119 	 * 
       
   120 	 * @param outputFolder
       
   121 	 *            where output files are created
       
   122 	 * @param fileOrFolder
       
   123 	 *            file to be "decoded" or directory from which files are to be
       
   124 	 *            decoded
       
   125 	 * @param fileExtension
       
   126 	 *            E.g. xml (not .xml)
       
   127 	 * @param monitor
       
   128 	 *            Progress monitor
       
   129 	 * @return true if success, false if not
       
   130 	 */
       
   131 	public static boolean executeSummary(String outputFolder,
       
   132 			String fileOrFolder, String fileExtension, IProgressMonitor monitor) {
       
   133 		File file = new File(fileOrFolder);
       
   134 		// directory was given
       
   135 		if (file.isDirectory()) {
       
   136 			InputXmlGenerator xml = new InputXmlGenerator(
       
   137 					XMLGeneratorAction.ANALYSE_CRASH_SUMMARY, null,
       
   138 					fileOrFolder, null, null, null, null, outputFolder, "."
       
   139 							+ fileExtension, null, "");
       
   140 			return createAndExecute(xml, monitor);
       
   141 			// a single file was given
       
   142 		} else if (file.isFile()) {
       
   143 			InputXmlGenerator xml = new InputXmlGenerator(
       
   144 					XMLGeneratorAction.ANALYSE_CRASH_SUMMARY,
       
   145 					new String[] { fileOrFolder }, null, null, null, null,
       
   146 					null, outputFolder, "." + fileExtension, null, "");
       
   147 			return createAndExecute(xml, monitor);
       
   148 		}
       
   149 		return false;
       
   150 	}
       
   151 
       
   152 	/**
       
   153 	 * creates an input.xml for CrashAnalyser.exe and executes CrashAnalyser.exe
       
   154 	 * 
       
   155 	 * @param xml
       
   156 	 *            initialized xml generator
       
   157 	 * @param monitor
       
   158 	 *            Progress monitor
       
   159 	 * @return true if success, false if not
       
   160 	 */
       
   161 	private static boolean createAndExecute(InputXmlGenerator xml,
       
   162 			IProgressMonitor monitor) {
       
   163 		String workingDirectory = getCrashAnalyserPath();
       
   164 		String fileName = workingDirectory + PARAMETERS_XML;
       
   165 		File filename = new File(fileName);
       
   166 		BufferedWriter writer = null;
       
   167 		try {
       
   168 			writer = new BufferedWriter(new FileWriter(filename));
       
   169 			// create input.xml for CrashAnalyser.exe
       
   170 			xml.GenerateXML(writer);
       
   171 
       
   172 		} catch (IOException e) {
       
   173 			e.printStackTrace();
       
   174 			return false;
       
   175 		} finally {
       
   176 			try {
       
   177 				if (writer != null) {
       
   178 					writer.flush();
       
   179 					writer.close();
       
   180 				}
       
   181 			} catch (IOException ex) {
       
   182 				ex.printStackTrace();
       
   183 			}
       
   184 		}
       
   185 
       
   186 		try {
       
   187 			String commandLineCommand = "\"" + workingDirectory
       
   188 					+ COMMAND_LINE_COMMAND + PARAMETERS_XML;
       
   189 			if (monitor != null)
       
   190 				if (progress_detailed) {
       
   191 					commandLineCommand += PROGRESS_DETAILS_PARAMETER;
       
   192 				} else {
       
   193 					commandLineCommand += PROGRESS_PARAMETER;
       
   194 				}
       
   195 
       
   196 			if (verbose)
       
   197 				commandLineCommand += VERBOSE_PARAMETER;
       
   198 
       
   199 			// execute CrashAnalyser.exe
       
   200 			Process p = Runtime.getRuntime().exec(commandLineCommand, null,
       
   201 					new File(workingDirectory));
       
   202 			// Get the input stream and read from it
       
   203 
       
   204 			FileWriter fstream = new FileWriter(workingDirectory + "log.txt");
       
   205 			BufferedWriter out = new BufferedWriter(fstream);
       
   206 			int c;
       
   207 			String line = "";
       
   208 			InputStream in = p.getInputStream();
       
   209 			boolean monitorStarted = false;
       
   210 			while ((c = in.read()) != -1) {
       
   211 				if ((char) c == '\n' || (char) c == '\r') {
       
   212 					if (line.contains(ACTION_START_TAG)) {
       
   213 						int beginIndex = line.indexOf(ACTION_START_TAG)
       
   214 								+ ACTION_START_TAG.length();
       
   215 						String action = line.substring(beginIndex);
       
   216 						if (!line.contains("/???") && !monitorStarted) {
       
   217 							int beginOfIndexOfActionsNbr = line.indexOf("/") + 1;
       
   218 							int endOfIndexOfActionsNbr = line.indexOf("}",
       
   219 									beginOfIndexOfActionsNbr);
       
   220 							Integer nbrOfActions = 0;
       
   221 							try {
       
   222 								nbrOfActions = Integer.valueOf((line.substring(
       
   223 										beginOfIndexOfActionsNbr,
       
   224 										endOfIndexOfActionsNbr)));
       
   225 							} catch (NumberFormatException e) {
       
   226 								nbrOfActions = -1;
       
   227 							}
       
   228 							if (nbrOfActions == -1) {
       
   229 								monitor.beginTask(action,
       
   230 										IProgressMonitor.UNKNOWN);
       
   231 							} else {
       
   232 								monitor.beginTask(action, nbrOfActions
       
   233 										.intValue());
       
   234 							}
       
   235 							monitorStarted = true;
       
   236 							monitor.worked(1);
       
   237 						} else {
       
   238 							if (monitorStarted) {
       
   239 								monitor.setTaskName(action);
       
   240 								monitor.worked(1);
       
   241 							}
       
   242 						}
       
   243 					}
       
   244 					line = "";
       
   245 				} else {
       
   246 					line += (char) c;
       
   247 				}
       
   248 				out.write((char) c);
       
   249 			}
       
   250 			in.close();
       
   251 			out.close();
       
   252 			monitor.done();
       
   253 		} catch (Exception e) {
       
   254 			e.printStackTrace();
       
   255 			return false;
       
   256 		}
       
   257 
       
   258 		return true;
       
   259 
       
   260 	}
       
   261 
       
   262 	/**
       
   263 	 * Gets the path where CrashAnalyser.exe is located
       
   264 	 * 
       
   265 	 * @return the path where CrashAnalyser.exe is located
       
   266 	 */
       
   267 	private static String getCrashAnalyserPath() {
       
   268 		String crashAnalyserExePath = CrashAnalyserCoreComponentsPlugin
       
   269 				.getPluginInstallPath();
       
   270 		if (!crashAnalyserExePath.endsWith(File.separator))
       
   271 			crashAnalyserExePath += File.separator;
       
   272 		crashAnalyserExePath += EXE_FOLDER + File.separator;
       
   273 		return crashAnalyserExePath;
       
   274 	}
       
   275 }