blob: 32878bad6d35392b06cefe56803d203726f61314 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2018 Ecole Polytechnique de Montreal
*
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0 which
* accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.tracecompass.incubator.internal.ftrace.core.trace;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.tracecompass.incubator.internal.ftrace.core.Activator;
import org.eclipse.tracecompass.incubator.internal.ftrace.core.event.GenericFtraceField;
import org.eclipse.tracecompass.tmf.core.io.BufferedRandomAccessFile;
import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
/**
* Ftrace trace.
*
* @author Guillaume Champagne
* @author Alexis-Maurer Fortin
* @author Hugo Genesse
* @author Pierre-Yves Lajoie
* @author Eva Terriault
*/
public class FtraceTrace extends GenericFtrace {
private static final int MAX_LINES = 100;
private static final int MAX_CONFIDENCE = 100;
@Override
public IStatus validate(IProject project, String path) {
File file = new File(path);
if (!file.exists()) {
return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "File not found: " + path); //$NON-NLS-1$
}
if (!file.isFile()) {
return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Not a file. It's a directory: " + path); //$NON-NLS-1$
}
int confidence = 0;
try {
if (!TmfTraceUtils.isText(file)) {
int magicLength = TRACE_CMD_DAT_MAGIC.length;
if (file.length() > magicLength) {
try (FileInputStream fis = new FileInputStream(file)) {
byte[] start = new byte[magicLength];
int read = fis.read(start);
if (read == magicLength && Arrays.equals(TRACE_CMD_DAT_MAGIC, start)) {
// TODO: offer to do this programatically
return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "This file looks like a binary trace-cmd, try running 'trace-cmd report -R' on it."); //$NON-NLS-1$
}
}
}
return new TraceValidationStatus(confidence, Activator.PLUGIN_ID);
}
} catch (IOException e) {
Activator.getInstance().logError("Error validating file: " + path, e); //$NON-NLS-1$
return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "IOException validating file: " + path, e); //$NON-NLS-1$
}
try (BufferedRandomAccessFile rafile = new BufferedRandomAccessFile(path, "r")) { //$NON-NLS-1$
int lineCount = 0;
int matches = 0;
String line = rafile.readLine();
while ((line != null) && (lineCount++ < MAX_LINES)) {
try {
GenericFtraceField field = GenericFtraceField.parseLine(line);
if (field != null) {
matches++;
}
} catch (RuntimeException e) {
confidence = Integer.MIN_VALUE;
}
confidence = MAX_CONFIDENCE * matches / lineCount;
line = rafile.readLine();
}
if (matches == 0) {
return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Most assuredly NOT an fTrace text trace"); //$NON-NLS-1$
}
} catch (IOException e) {
Activator.getInstance().logError("Error validating file: " + path, e); //$NON-NLS-1$
return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "IOException validating file: " + path, e); //$NON-NLS-1$
}
return new TraceValidationStatus(confidence, Activator.PLUGIN_ID);
}
}