2 * Copyright (C) ARM Limited 2010-2013. 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.
12 #include "ConfigurationXML.h"
15 #include "OlyUtility.h"
16 #include "SessionData.h"
18 static const char* ATTR_COUNTER = "counter";
19 static const char* ATTR_REVISION = "revision";
20 static const char* ATTR_EVENT = "event";
21 static const char* ATTR_COUNT = "count";
23 ConfigurationXML::ConfigurationXML() {
24 const char * configuration_xml;
25 unsigned int configuration_xml_len;
26 getDefaultConfigurationXml(configuration_xml, configuration_xml_len);
30 if (gSessionData->mConfigurationXMLPath) {
31 strncpy(path, gSessionData->mConfigurationXMLPath, PATH_MAX);
33 if (util->getApplicationFullPath(path, PATH_MAX) != 0) {
34 logg->logMessage("Unable to determine the full path of gatord, the cwd will be used");
36 strncat(path, "configuration.xml", PATH_MAX - strlen(path) - 1);
38 mConfigurationXML = util->readFromDisk(path);
40 for (int retryCount = 0; retryCount < 2; ++retryCount) {
41 if (mConfigurationXML == NULL) {
42 logg->logMessage("Unable to locate configuration.xml, using default in binary");
43 // null-terminate configuration_xml
44 mConfigurationXML = (char*)malloc(configuration_xml_len + 1);
45 memcpy(mConfigurationXML, (const void*)configuration_xml, configuration_xml_len);
46 mConfigurationXML[configuration_xml_len] = 0;
49 int ret = parse(mConfigurationXML);
51 // remove configuration.xml on disk to use the default
52 if (remove(path) != 0) {
53 logg->logError(__FILE__, __LINE__, "Invalid configuration.xml file detected and unable to delete it. To resolve, delete configuration.xml on disk");
56 logg->logMessage("Invalid configuration.xml file detected and removed");
58 // Free the current configuration and reload
59 free((void*)mConfigurationXML);
60 mConfigurationXML = NULL;
70 ConfigurationXML::~ConfigurationXML() {
71 if (mConfigurationXML) {
72 free((void*)mConfigurationXML);
76 int ConfigurationXML::parse(const char* configurationXML) {
77 mxml_node_t *tree, *node;
80 // clear counter overflow
81 gSessionData->mCounterOverflow = 0;
84 // disable all counters prior to parsing the configuration xml
85 for (int i = 0; i < MAX_PERFORMANCE_COUNTERS; i++) {
86 gSessionData->mCounters[i].setEnabled(false);
89 tree = mxmlLoadString(NULL, configurationXML, MXML_NO_CALLBACK);
91 node = mxmlGetFirstChild(tree);
92 while (node && mxmlGetType(node) != MXML_ELEMENT)
93 node = mxmlWalkNext(node, tree, MXML_NO_DESCEND);
95 ret = configurationsTag(node);
97 node = mxmlGetFirstChild(node);
99 if (mxmlGetType(node) != MXML_ELEMENT) {
100 node = mxmlWalkNext(node, tree, MXML_NO_DESCEND);
103 configurationTag(node);
104 node = mxmlWalkNext(node, tree, MXML_NO_DESCEND);
112 void ConfigurationXML::validate(void) {
113 for (int i = 0; i < MAX_PERFORMANCE_COUNTERS; i++) {
114 const Counter & counter = gSessionData->mCounters[i];
115 if (counter.isEnabled()) {
116 if (strcmp(counter.getType(), "") == 0) {
117 logg->logError(__FILE__, __LINE__, "Invalid required attribute in configuration.xml:\n counter=\"%s\"\n event=%d\n", counter.getType(), counter.getEvent());
121 // iterate through the remaining enabled performance counters
122 for (int j = i + 1; j < MAX_PERFORMANCE_COUNTERS; j++) {
123 const Counter & counter2 = gSessionData->mCounters[j];
124 if (counter2.isEnabled()) {
125 // check if the types are the same
126 if (strcmp(counter.getType(), counter2.getType()) == 0) {
127 logg->logError(__FILE__, __LINE__, "Duplicate performance counter type in configuration.xml: %s", counter.getType());
136 #define CONFIGURATION_REVISION 3
137 int ConfigurationXML::configurationsTag(mxml_node_t *node) {
138 const char* revision_string;
140 revision_string = mxmlElementGetAttr(node, ATTR_REVISION);
141 if (!revision_string) {
142 return 1; //revision issue;
145 int revision = strtol(revision_string, NULL, 10);
146 if (revision < CONFIGURATION_REVISION) {
147 return 1; // revision issue
150 // A revision >= CONFIGURATION_REVISION is okay
151 // Greater than can occur when Streamline is newer than gator
156 void ConfigurationXML::configurationTag(mxml_node_t *node) {
157 // handle all other performance counters
158 if (mIndex >= MAX_PERFORMANCE_COUNTERS) {
160 gSessionData->mCounterOverflow = mIndex;
165 Counter & counter = gSessionData->mCounters[mIndex];
167 if (mxmlElementGetAttr(node, ATTR_COUNTER)) counter.setType(mxmlElementGetAttr(node, ATTR_COUNTER));
168 if (mxmlElementGetAttr(node, ATTR_EVENT)) counter.setEvent(strtol(mxmlElementGetAttr(node, ATTR_EVENT), NULL, 16));
169 if (mxmlElementGetAttr(node, ATTR_COUNT)) counter.setCount(strtol(mxmlElementGetAttr(node, ATTR_COUNT), NULL, 10));
170 counter.setEnabled(true);
172 // Associate a driver with each counter
173 for (Driver *driver = Driver::getHead(); driver != NULL; driver = driver->getNext()) {
174 if (driver->claimCounter(counter)) {
175 if (counter.getDriver() != NULL) {
176 logg->logError(__FILE__, __LINE__, "More than one driver has claimed %s:%i", counter.getType(), counter.getEvent());
179 counter.setDriver(driver);
183 // If no driver is associated with the counter, disable it
184 if (counter.getDriver() == NULL) {
185 logg->logMessage("No driver has claimed %s:%i", counter.getType(), counter.getEvent());
186 counter.setEnabled(false);
189 if (counter.isEnabled()) {
190 // update counter index
195 void ConfigurationXML::getDefaultConfigurationXml(const char * & xml, unsigned int & len) {
196 #include "configuration_xml.h" // defines and initializes char configuration_xml[] and int configuration_xml_len
197 xml = (const char *)configuration_xml;
198 len = configuration_xml_len;