2 * Copyright (C) ARM Limited 2010-2014. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #include "ConfigurationXML.h"
17 #include "OlyUtility.h"
18 #include "SessionData.h"
20 static const char* ATTR_COUNTER = "counter";
21 static const char* ATTR_REVISION = "revision";
22 static const char* ATTR_EVENT = "event";
23 static const char* ATTR_COUNT = "count";
25 ConfigurationXML::ConfigurationXML() {
26 const char * configuration_xml;
27 unsigned int configuration_xml_len;
28 getDefaultConfigurationXml(configuration_xml, configuration_xml_len);
33 mConfigurationXML = util->readFromDisk(path);
35 for (int retryCount = 0; retryCount < 2; ++retryCount) {
36 if (mConfigurationXML == NULL) {
37 logg->logMessage("Unable to locate configuration.xml, using default in binary");
38 // null-terminate configuration_xml
39 mConfigurationXML = (char*)malloc(configuration_xml_len + 1);
40 memcpy(mConfigurationXML, (const void*)configuration_xml, configuration_xml_len);
41 mConfigurationXML[configuration_xml_len] = 0;
44 int ret = parse(mConfigurationXML);
48 // Free the current configuration and reload
49 free((void*)mConfigurationXML);
50 mConfigurationXML = NULL;
60 ConfigurationXML::~ConfigurationXML() {
61 if (mConfigurationXML) {
62 free((void*)mConfigurationXML);
66 int ConfigurationXML::parse(const char* configurationXML) {
67 mxml_node_t *tree, *node;
70 // clear counter overflow
71 gSessionData->mCounterOverflow = 0;
72 gSessionData->mIsEBS = false;
75 // disable all counters prior to parsing the configuration xml
76 for (int i = 0; i < MAX_PERFORMANCE_COUNTERS; i++) {
77 gSessionData->mCounters[i].setEnabled(false);
80 tree = mxmlLoadString(NULL, configurationXML, MXML_NO_CALLBACK);
82 node = mxmlGetFirstChild(tree);
83 while (node && mxmlGetType(node) != MXML_ELEMENT)
84 node = mxmlWalkNext(node, tree, MXML_NO_DESCEND);
86 ret = configurationsTag(node);
88 node = mxmlGetFirstChild(node);
90 if (mxmlGetType(node) != MXML_ELEMENT) {
91 node = mxmlWalkNext(node, tree, MXML_NO_DESCEND);
94 configurationTag(node);
95 node = mxmlWalkNext(node, tree, MXML_NO_DESCEND);
103 void ConfigurationXML::validate(void) {
104 for (int i = 0; i < MAX_PERFORMANCE_COUNTERS; i++) {
105 const Counter & counter = gSessionData->mCounters[i];
106 if (counter.isEnabled()) {
107 if (strcmp(counter.getType(), "") == 0) {
108 logg->logError(__FILE__, __LINE__, "Invalid required attribute in configuration.xml:\n counter=\"%s\"\n event=%d\n", counter.getType(), counter.getEvent());
112 // iterate through the remaining enabled performance counters
113 for (int j = i + 1; j < MAX_PERFORMANCE_COUNTERS; j++) {
114 const Counter & counter2 = gSessionData->mCounters[j];
115 if (counter2.isEnabled()) {
116 // check if the types are the same
117 if (strcmp(counter.getType(), counter2.getType()) == 0) {
118 logg->logError(__FILE__, __LINE__, "Duplicate performance counter type in configuration.xml: %s", counter.getType());
127 #define CONFIGURATION_REVISION 3
128 int ConfigurationXML::configurationsTag(mxml_node_t *node) {
129 const char* revision_string;
131 revision_string = mxmlElementGetAttr(node, ATTR_REVISION);
132 if (!revision_string) {
133 return 1; //revision issue;
136 int revision = strtol(revision_string, NULL, 10);
137 if (revision < CONFIGURATION_REVISION) {
138 return 1; // revision issue
141 // A revision >= CONFIGURATION_REVISION is okay
142 // Greater than can occur when Streamline is newer than gator
147 void ConfigurationXML::configurationTag(mxml_node_t *node) {
148 // handle all other performance counters
149 if (mIndex >= MAX_PERFORMANCE_COUNTERS) {
151 gSessionData->mCounterOverflow = mIndex;
156 Counter & counter = gSessionData->mCounters[mIndex];
158 if (mxmlElementGetAttr(node, ATTR_COUNTER)) counter.setType(mxmlElementGetAttr(node, ATTR_COUNTER));
159 if (mxmlElementGetAttr(node, ATTR_EVENT)) counter.setEvent(strtol(mxmlElementGetAttr(node, ATTR_EVENT), NULL, 16));
160 if (mxmlElementGetAttr(node, ATTR_COUNT)) counter.setCount(strtol(mxmlElementGetAttr(node, ATTR_COUNT), NULL, 10));
161 if (counter.getCount() > 0) {
162 gSessionData->mIsEBS = true;
164 counter.setEnabled(true);
166 // Associate a driver with each counter
167 for (Driver *driver = Driver::getHead(); driver != NULL; driver = driver->getNext()) {
168 if (driver->claimCounter(counter)) {
169 if (counter.getDriver() != NULL) {
170 logg->logError(__FILE__, __LINE__, "More than one driver has claimed %s:%i", counter.getType(), counter.getEvent());
173 counter.setDriver(driver);
177 // If no driver is associated with the counter, disable it
178 if (counter.getDriver() == NULL) {
179 logg->logMessage("No driver has claimed %s:%i", counter.getType(), counter.getEvent());
180 counter.setEnabled(false);
183 if (counter.isEnabled()) {
184 // update counter index
189 void ConfigurationXML::getDefaultConfigurationXml(const char * & xml, unsigned int & len) {
190 #include "defaults_xml.h" // defines and initializes char defaults_xml[] and int defaults_xml_len
191 xml = (const char *)defaults_xml;
192 len = defaults_xml_len;
195 void ConfigurationXML::getPath(char* path) {
196 if (gSessionData->mConfigurationXMLPath) {
197 strncpy(path, gSessionData->mConfigurationXMLPath, PATH_MAX);
199 if (util->getApplicationFullPath(path, PATH_MAX) != 0) {
200 logg->logMessage("Unable to determine the full path of gatord, the cwd will be used");
202 strncat(path, "configuration.xml", PATH_MAX - strlen(path) - 1);
206 void ConfigurationXML::remove() {
210 if (::remove(path) != 0) {
211 logg->logError(__FILE__, __LINE__, "Invalid configuration.xml file detected and unable to delete it. To resolve, delete configuration.xml on disk");
214 logg->logMessage("Invalid configuration.xml file detected and removed");