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);
31 mConfigurationXML = util->readFromDisk(path);
33 for (int retryCount = 0; retryCount < 2; ++retryCount) {
34 if (mConfigurationXML == NULL) {
35 logg->logMessage("Unable to locate configuration.xml, using default in binary");
36 // null-terminate configuration_xml
37 mConfigurationXML = (char*)malloc(configuration_xml_len + 1);
38 memcpy(mConfigurationXML, (const void*)configuration_xml, configuration_xml_len);
39 mConfigurationXML[configuration_xml_len] = 0;
42 int ret = parse(mConfigurationXML);
46 // Free the current configuration and reload
47 free((void*)mConfigurationXML);
48 mConfigurationXML = NULL;
58 ConfigurationXML::~ConfigurationXML() {
59 if (mConfigurationXML) {
60 free((void*)mConfigurationXML);
64 int ConfigurationXML::parse(const char* configurationXML) {
65 mxml_node_t *tree, *node;
68 // clear counter overflow
69 gSessionData->mCounterOverflow = 0;
72 // disable all counters prior to parsing the configuration xml
73 for (int i = 0; i < MAX_PERFORMANCE_COUNTERS; i++) {
74 gSessionData->mCounters[i].setEnabled(false);
77 tree = mxmlLoadString(NULL, configurationXML, MXML_NO_CALLBACK);
79 node = mxmlGetFirstChild(tree);
80 while (node && mxmlGetType(node) != MXML_ELEMENT)
81 node = mxmlWalkNext(node, tree, MXML_NO_DESCEND);
83 ret = configurationsTag(node);
85 node = mxmlGetFirstChild(node);
87 if (mxmlGetType(node) != MXML_ELEMENT) {
88 node = mxmlWalkNext(node, tree, MXML_NO_DESCEND);
91 configurationTag(node);
92 node = mxmlWalkNext(node, tree, MXML_NO_DESCEND);
100 void ConfigurationXML::validate(void) {
101 for (int i = 0; i < MAX_PERFORMANCE_COUNTERS; i++) {
102 const Counter & counter = gSessionData->mCounters[i];
103 if (counter.isEnabled()) {
104 if (strcmp(counter.getType(), "") == 0) {
105 logg->logError(__FILE__, __LINE__, "Invalid required attribute in configuration.xml:\n counter=\"%s\"\n event=%d\n", counter.getType(), counter.getEvent());
109 // iterate through the remaining enabled performance counters
110 for (int j = i + 1; j < MAX_PERFORMANCE_COUNTERS; j++) {
111 const Counter & counter2 = gSessionData->mCounters[j];
112 if (counter2.isEnabled()) {
113 // check if the types are the same
114 if (strcmp(counter.getType(), counter2.getType()) == 0) {
115 logg->logError(__FILE__, __LINE__, "Duplicate performance counter type in configuration.xml: %s", counter.getType());
124 #define CONFIGURATION_REVISION 3
125 int ConfigurationXML::configurationsTag(mxml_node_t *node) {
126 const char* revision_string;
128 revision_string = mxmlElementGetAttr(node, ATTR_REVISION);
129 if (!revision_string) {
130 return 1; //revision issue;
133 int revision = strtol(revision_string, NULL, 10);
134 if (revision < CONFIGURATION_REVISION) {
135 return 1; // revision issue
138 // A revision >= CONFIGURATION_REVISION is okay
139 // Greater than can occur when Streamline is newer than gator
144 void ConfigurationXML::configurationTag(mxml_node_t *node) {
145 // handle all other performance counters
146 if (mIndex >= MAX_PERFORMANCE_COUNTERS) {
148 gSessionData->mCounterOverflow = mIndex;
153 Counter & counter = gSessionData->mCounters[mIndex];
155 if (mxmlElementGetAttr(node, ATTR_COUNTER)) counter.setType(mxmlElementGetAttr(node, ATTR_COUNTER));
156 if (mxmlElementGetAttr(node, ATTR_EVENT)) counter.setEvent(strtol(mxmlElementGetAttr(node, ATTR_EVENT), NULL, 16));
157 if (mxmlElementGetAttr(node, ATTR_COUNT)) counter.setCount(strtol(mxmlElementGetAttr(node, ATTR_COUNT), NULL, 10));
158 counter.setEnabled(true);
160 // Associate a driver with each counter
161 for (Driver *driver = Driver::getHead(); driver != NULL; driver = driver->getNext()) {
162 if (driver->claimCounter(counter)) {
163 if (counter.getDriver() != NULL) {
164 logg->logError(__FILE__, __LINE__, "More than one driver has claimed %s:%i", counter.getType(), counter.getEvent());
167 counter.setDriver(driver);
171 // If no driver is associated with the counter, disable it
172 if (counter.getDriver() == NULL) {
173 logg->logMessage("No driver has claimed %s:%i", counter.getType(), counter.getEvent());
174 counter.setEnabled(false);
177 if (counter.isEnabled()) {
178 // update counter index
183 void ConfigurationXML::getDefaultConfigurationXml(const char * & xml, unsigned int & len) {
184 #include "configuration_xml.h" // defines and initializes char configuration_xml[] and int configuration_xml_len
185 xml = (const char *)configuration_xml;
186 len = configuration_xml_len;
189 void ConfigurationXML::getPath(char* path) {
190 if (gSessionData->mConfigurationXMLPath) {
191 strncpy(path, gSessionData->mConfigurationXMLPath, PATH_MAX);
193 if (util->getApplicationFullPath(path, PATH_MAX) != 0) {
194 logg->logMessage("Unable to determine the full path of gatord, the cwd will be used");
196 strncat(path, "configuration.xml", PATH_MAX - strlen(path) - 1);
200 void ConfigurationXML::remove() {
204 if (::remove(path) != 0) {
205 logg->logError(__FILE__, __LINE__, "Invalid configuration.xml file detected and unable to delete it. To resolve, delete configuration.xml on disk");
208 logg->logMessage("Invalid configuration.xml file detected and removed");