blob: 41c6286bdd1df5906584814e67f61b4874c38bb5 [file] [log] [blame]
/* --COPYRIGHT--,EPL
* Copyright (c) 2008 Texas Instruments and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Texas Instruments - initial implementation
*
* --/COPYRIGHT--*/
/*
* ======== MetaData.java ========
*! Revision History
*! ================
*! 20-May-2009 cmcc Created
*/
package xdc.rta;
import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
import xdc.rov.*;
/*
* ======== MetaData ========
*/
public class MetaData implements IEventMetaData
{
private HashMap<String, Integer> modNameToId = null;
private HashMap<String, Integer> evtNameToId = null;
public int argSize = -1; // Target arg size in bytes
private int bitsPerChar = -1;
private int eventSize = -1;
private String targetName = "";
private TargetType.Endianess endian;
HashMap<Integer, String> modIdToName = null;
HashMap<Integer, String> evtIdToName = null;
HashMap<Integer, String> evtIdToMsg = null;
private HashMap<String, DiagConfig> modNameToDiags = null;
private HashMap<String, String> modNameToLogger = null;
private HashMap<String, Integer> loggerToId = null;
private String[] loggerNames = null;
private Vector<Logger> loggers = null;
/* Object file reader for looking up Log_print strings */
public IOFReader ofReader = null;
/*
* ======== getXMLFromExec ========
*/
public static String getXMLFromExec(String executable) throws Exception
{
return (Recap.locateRecap(executable, ".rta.xml"));
}
/*
* ======== parseFromExec ========
*/
public String parseFromExec(String executable) throws Exception
{
/*
* TODO - Read the parser from the executable using Vers so that
* you can use it to extract the embedded recap file.
*/
/* Retrieve the path to the XML file from the executable. */
String xmlFile = getXMLFromExec(executable);
/* Call parse and return the result. */
String result = parse(xmlFile, executable);
return (result);
}
/*!
* ======== Logger ========
* Contains the properties of a target logger instance.
*/
public class Logger {
public int id;
/* A name to associate with this logger instance. */
public String name;
/* The name of the logger module. */
public String type;
public Node metaArgs;
/* The list of modules which log to this logger. */
public Vector<String> modules;
public Logger()
{
modules = new Vector<String>();
}
}
/*
* ======== parse ========
*/
public String parse(String xmlFile, String executable)
throws java.lang.InstantiationException,
java.lang.ClassNotFoundException,
java.lang.IllegalAccessException
{
/* Modules */
modNameToId = new HashMap<String, Integer>();
modIdToName = new HashMap<Integer, String>();
modNameToDiags = new HashMap<String, DiagConfig>();
/* Events */
evtNameToId = new HashMap<String, Integer>();
evtIdToName = new HashMap<Integer, String>();
evtIdToMsg = new HashMap<Integer, String>();
/* Loggers */
loggers = new Vector<Logger>();
modNameToLogger = new HashMap<String, String>();
loggerToId = new HashMap<String, Integer>();
Document doc = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.parse(xmlFile);
}
catch (javax.xml.parsers.ParserConfigurationException e) {
return ("Can't create XML parser: " + e.toString());
}
catch (java.io.IOException e) {
return ("Can't read RTA XML file " + xmlFile + ": " + e.toString());
}
catch (org.xml.sax.SAXException e) {
return ("Can't parse RTA XML file " + xmlFile +": " + e.toString());
}
/* Retrieve the target-specific properties */
String res = parseTargetProps(doc);
if (res != "") {
return (res);
}
/* Retrieve the list of loggers and their properties. */
parseLoggers(doc);
/* Retrieve the mapping of modules to ids and the module properties. */
res = parseModules(doc, xmlFile);
if (res != "") {
return (res);
}
/* Retrieve the event information. */
res = parseEventMap(doc, xmlFile);
if (res != "") {
return (res);
}
if (executable != null) {
try {
/* Use Coff reader by default */
/* TODO - Read XML file for the object file parser to use. */
Class c = Class.forName("ti.targets.omf.cof.Coff");
ofReader = (IOFReader) c.newInstance();
String err = ofReader.parse(executable);
if (err != "") {
System.err.println("Failed to parse " + executable
+ ": " + err);
ofReader = null;
}
else {
/* Close the file. Reader will re-open it automatically to
* read strings.
*/
ofReader.close();
}
}
catch (IOException e) {
System.err.println("Failed to parse object file.");
e.printStackTrace();
}
/*
* Set the Formatter's OFReader so that it can perform String
* lookups (%s) when formatting printf strings.
*/
Formatter.setOFReader(ofReader);
}
return ("");
}
/*!
* ======== parseTargetProps ========
*/
private String parseTargetProps(Document doc)
{
this.targetName = doc.getElementsByTagName("targetName").item(0).getFirstChild().getNodeValue();
/* Get the target endianess. */
String endianStr = doc.getElementsByTagName("endian").item(0).getFirstChild().getNodeValue();
try {
this.endian = TargetType.strToEndianess(endianStr);
}
catch (Exception e) {
return (e.toString());
}
this.bitsPerChar = Integer.parseInt(doc.getElementsByTagName("bitsPerChar").item(0).getFirstChild().getNodeValue());
this.argSize = Integer.parseInt(
doc.getElementsByTagName("argSize").item(0).getFirstChild().getNodeValue());
this.eventSize = Integer.parseInt(
doc.getElementsByTagName("eventSize").item(0).getFirstChild().getNodeValue());
/* Convert sizes from MAUs to bytes */
this.argSize = this.argSize * (this.bitsPerChar / 8);
this.eventSize = this.eventSize * (this.bitsPerChar / 8);
return ("");
}
/*
* ======== parseLoggers ========
* Retrieve the list of loggers so that we have them in the same
* order as the target.
*/
private void parseLoggers(Document doc)
{
NodeList loggerList = doc.getElementsByTagName("loggers");
for (int i = 0; i < loggerList.getLength(); i++) {
Element elem = (Element) loggerList.item(i);
Logger logger = new Logger();
/* The log's id is its index in the list. */
logger.id = i;
/* Get all of the logger's properties. */
NodeList loggerProps = elem.getChildNodes();
/* Preserve compatibility. Older XML files just have logger name. */
if (loggerProps.getLength() == 0) {
logger.name = elem.getTextContent();
loggers.add(logger);
this.loggerToId.put(logger.name, i);
continue;
}
/* Retrieve all of the logger's properties */
for (int j = 0; j < loggerProps.getLength(); j++) {
Node n = loggerProps.item(j);
String nodeName = n.getNodeName();
/* Check for logger name */
if (nodeName.equals("name")) {
logger.name = n.getTextContent();
}
/* Check for logger type */
else if (nodeName.equals("type")) {
logger.type = n.getTextContent();
}
/* Check for logger buffer symbol */
else if (nodeName.equals("metaArgs")) {
logger.metaArgs = n;
}
}
/* Map id to logger and logger to id */
loggers.add(logger);
this.loggerToId.put(logger.name, i);
}
/* Create a String array of the logger names for getLoggerNames */
loggerNames = new String[loggers.size()];
for (int i = 0; i < loggers.size(); i++) {
loggerNames[i] = loggers.get(i).name;
}
}
/*!
* ======== parseModules ========
* Parse the modMap, which maps module names to their id, logger,
* and diags settings.
*/
private String parseModules(Document doc, String xmlFile)
{
NodeList modMap = doc.getElementsByTagName("modMap");
for (int i = 0; i < modMap.getLength(); i++) {
Element elem = (Element)modMap.item(i);
String modName = elem.getAttribute("key");
NodeList children = elem.getChildNodes();
for (int j = 0; j < children.getLength(); j++) {
Node n = children.item(j);
if (n.getNodeType() == 1) {
if (n.getNodeName().equals("id")) {
int id = Integer.parseInt(n.getTextContent());
this.modNameToId.put(modName, id);
this.modIdToName.put(id, modName);
}
else if (n.getNodeName().equals("logger")) {
String loggerName = n.getTextContent();
/* Don't add modules with null logger. */
if (!loggerName.equals("null")) {
/* Map module name to logger */
this.modNameToLogger.put(modName, loggerName);
/* Map logger to list of modules. */
int loggerId = lookupLoggerId(loggerName);
Logger logger = loggers.get(loggerId);
logger.modules.add(modName);
}
}
else if (n.getNodeName().equals("diagsMask")) {
String diagsMask = n.getTextContent();
DiagConfig diags = new DiagConfig(diagsMask);
this.modNameToDiags.put(modName, diags);
}
}
}
}
return ("");
}
/*
* ======== parseEventMap ========
*/
private String parseEventMap(Document doc, String xmlFile)
{
/*
* Parse the event map.
*/
NodeList evtMap = doc.getElementsByTagName("evtMap");
for (int i = 0; i < evtMap.getLength(); i++) {
Element elem = (Element)evtMap.item(i);
String name = elem.getAttribute("key");
int id = Integer.parseInt(
elem.getElementsByTagName("id").item(0).getFirstChild().getNodeValue());
this.evtNameToId.put(name, id);
this.evtIdToName.put(id, name);
String msg = elem.getElementsByTagName("msg").item(0).getFirstChild().getNodeValue();
try {
this.evtIdToMsg.put(id, java.net.URLDecoder.decode(msg, "UTF-8"));
}
catch (java.io.UnsupportedEncodingException e) {
return ("XMl file " + xmlFile + " contains unsupported encodings: " + e.toString());
}
}
return ("");
}
/*
* ======== getBitsPerChar ========
*/
public int getBitsPerChar()
{
return (this.bitsPerChar);
}
/*
* ======== getTargetEventRecSize ========
* Returns the record size in bytes (not MAUs).
*/
public int getTargetEventRecSize()
{
return (this.eventSize);
}
/*
* ======== getTargetArgSize ========
* Returns the target size of a record argument in bytes (not MAUs).
*/
public int getTargetArgSize()
{
return (this.argSize);
}
/*
* ======== lookupEventId ========
*/
public int lookupEventId(String eventName)
{
Integer res = this.evtNameToId.get(eventName);
return (res == null ? -1 : res);
}
/*
* ======== lookupEventName ========
*/
public String lookupEventName(int eventId) {
return (this.evtIdToName.get(eventId));
}
/*
* ======== lookupModuleId ========
*/
public int lookupModuleId(String moduleName)
{
Integer res = this.modNameToId.get(moduleName);
return (res == null ? -1 : res);
}
/*
* ======== lookupModuleName ========
*/
public String lookupModuleName(int modId) {
return (this.modIdToName.get(modId));
}
/*
* ======== lookupLoggerId ========
*/
public int lookupLoggerId(String loggerName)
{
Integer res = this.loggerToId.get(loggerName);
return (res == null ? -1 : res);
}
/*
* ======== lookupEventModule ========
* The event name contains the module name, e.g., xdc.runtime.Log.L_create
*/
public String lookupEventModule(String eventName)
{
return (eventName.substring(0, eventName.lastIndexOf(".")));
}
/*
* ======== lookupEventMessage ========
* Returns the format string for the message with the given name.
*/
public String lookupEventMessage(String eventName)
{
return (lookupEventMessage(this.lookupEventId(eventName)));
}
public String lookupEventMessage(int eventId) {
return (this.evtIdToMsg.get(eventId));
}
/*
* ======== getModuleNames ========
*/
public String[] getModuleNames()
{
Set<String> keys = modNameToId.keySet();
return (keys.toArray(new String [0]));
}
/*
* ======== getEventNames ========
*/
public String[] getEventNames()
{
Set<String> keys = evtNameToId.keySet();
return (keys.toArray(new String [0]));
}
/*
* ======== getLogger ========
*/
public Logger getLogger(int loggerId)
{
return (loggers.get(loggerId));
}
/*
* ======== getLoggerNames ========
* Lists all the loggers in the system.
*/
public String[] getLoggerNames()
{
return (loggerNames);
}
/*
* ======== getLoggerMetaArgs ========
* Returns the meta data associated with the specified logger instance.
*/
public Node getLoggerMetaArgs(int loggerId)
{
return (loggers.get(loggerId).metaArgs);
}
/*
* ======== getModuleDiagConfig ========
* Gets the initial diagnostics configuration for the given module.
*/
public DiagConfig getModuleDiagConfig(String moduleName) {
return (this.modNameToDiags.get(moduleName));
}
/*
* ======== getModuleLogger ========
* Returns the logger instance associated with the specified module.
*/
public String getModuleLogger(String moduleName) {
return (this.modNameToLogger.get(moduleName));
}
/*
* ======== getLoggersModules =========
* Returns the list of modules associated with a given logger instance.
*/
public String[] getLoggersModules(String loggerName)
{
int loggerId = lookupLoggerId(loggerName);
Logger logger = this.loggers.get(loggerId);
return (logger.modules.toArray(new String[0]));
}
/*
* ======== getTargetName ========
*/
public String getTargetName()
{
return (this.targetName);
}
/*
* ======== getEndianess ========
*/
public TargetType.Endianess getEndianess()
{
return (this.endian);
}
public String getDataTransportClass()
{
/* TODO - This should come from RTA XML file. */
return ("ti.rta.rtdx.DataTransport");
}
public String getControlTransportClass()
{
/* TODO - This should come from RTA XML file. */
return ("ti.rta.rtdx.ControlTransport");
}
public IOFReader getOFReader()
{
return (ofReader);
}
}