1 /**
2 * Copyright (C) ARM Limited 2010-2012. All rights reserved.
3 *
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.
7 */
9 #include <string.h>
10 #include <stdlib.h>
11 #include <dirent.h>
12 #include "ConfigurationXML.h"
13 #include "Logging.h"
14 #include "OlyUtility.h"
15 #include "SessionData.h"
17 extern void handleException();
19 static const char* ATTR_COUNTER = "counter";
20 static const char* ATTR_REVISION = "revision";
21 static const char* ATTR_TITLE = "title";
22 static const char* ATTR_NAME = "name";
23 static const char* ATTR_EVENT = "event";
24 static const char* ATTR_COLOR = "color";
25 static const char* ATTR_COUNT = "count";
26 static const char* ATTR_OPERATION = "operation";
27 static const char* ATTR_PER_CPU = "per_cpu";
28 static const char* ATTR_DESCRIPTION = "description";
29 static const char* ATTR_EBS = "event_based_sampling";
30 static const char* ATTR_LEVEL = "level";
31 static const char* ATTR_ALIAS = "alias";
32 static const char* ATTR_DISPLAY = "display";
33 static const char* ATTR_UNITS = "units";
34 static const char* ATTR_AVERAGE_SELECTION = "average_selection";
36 ConfigurationXML::ConfigurationXML() {
37 #include "configuration_xml.h" // defines and initializes char configuration_xml[] and int configuration_xml_len
38 mIndex = 0;
39 char* path = (char*)malloc(PATH_MAX);
41 if (gSessionData->mConfigurationXMLPath) {
42 strncpy(path, gSessionData->mConfigurationXMLPath, PATH_MAX);
43 } else {
44 if (util->getApplicationFullPath(path, PATH_MAX) != 0) {
45 logg->logMessage("Unable to determine the full path of gatord, the cwd will be used");
46 }
47 strncat(path, "configuration.xml", PATH_MAX - strlen(path) - 1);
48 }
49 mConfigurationXML = util->readFromDisk(path);
51 if (mConfigurationXML == NULL) {
52 logg->logMessage("Unable to locate configuration.xml, using default in binary");
53 // null-terminate configuration_xml
54 mConfigurationXML = (char*)malloc(configuration_xml_len + 1);
55 memcpy(mConfigurationXML, (const void*)configuration_xml, configuration_xml_len);
56 mConfigurationXML[configuration_xml_len] = 0;
57 }
59 // disable all counters prior to parsing the configuration xml
60 for (int i = 0; i < MAX_PERFORMANCE_COUNTERS; i++) {
61 gSessionData->mPerfCounterEnabled[i] = 0;
62 }
64 int ret = parse(mConfigurationXML);
65 if (ret == 1) {
66 // remove configuration.xml on disk to use the default
67 if (remove(path) != 0) {
68 logg->logError(__FILE__, __LINE__, "Invalid configuration.xml file detected and unable to delete it. To resolve, delete configuration.xml on disk");
69 handleException();
70 }
71 logg->logMessage("Invalid configuration.xml file detected and removed");
72 }
74 validate();
76 free(path);
77 }
79 ConfigurationXML::~ConfigurationXML() {
80 if (mConfigurationXML) {
81 free((void*)mConfigurationXML);
82 }
83 }
85 int ConfigurationXML::parse(const char* configurationXML) {
86 mxml_node_t *tree, *node;
87 int ret;
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);
98 while (node) {
99 if (mxmlGetType(node) != MXML_ELEMENT) {
100 node = mxmlWalkNext(node, tree, MXML_NO_DESCEND);
101 continue;
102 }
103 configurationTag(node);
104 node = mxmlWalkNext(node, tree, MXML_NO_DESCEND);
105 }
107 mxmlDelete(tree);
109 return ret;
110 }
112 void ConfigurationXML::validate(void) {
113 for (int i = 0; i < MAX_PERFORMANCE_COUNTERS; i++) {
114 if (gSessionData->mPerfCounterEnabled[i]) {
115 if (strcmp(gSessionData->mPerfCounterType[i], "") == 0) {
116 logg->logError(__FILE__, __LINE__, "Invalid required attribute in configuration.xml:\n counter=\"%s\"\n title=\"%s\"\n name=\"%s\"\n event=%d\n", gSessionData->mPerfCounterType[i], gSessionData->mPerfCounterTitle[i], gSessionData->mPerfCounterName[i], gSessionData->mPerfCounterEvent[i]);
117 handleException();
118 }
120 // iterate through the remaining enabled performance counters
121 for (int j = i + 1; j < MAX_PERFORMANCE_COUNTERS; j++) {
122 if (gSessionData->mPerfCounterEnabled[j]) {
123 // check if the types are the same
124 if (strcmp(gSessionData->mPerfCounterType[i], gSessionData->mPerfCounterType[j]) == 0) {
125 logg->logError(__FILE__, __LINE__, "Duplicate performance counter type in configuration.xml: %s", gSessionData->mPerfCounterType[i]);
126 handleException();
127 }
128 }
129 }
130 }
131 }
132 }
134 #define CONFIGURATION_REVISION 1
135 int ConfigurationXML::configurationsTag(mxml_node_t *node) {
136 const char* revision_string;
138 revision_string = mxmlElementGetAttr(node, ATTR_REVISION);
139 if (!revision_string) {
140 return 1; //revision issue;
141 }
143 int revision = strtol(revision_string, NULL, 10);
144 if (revision < CONFIGURATION_REVISION) {
145 return 1; // revision issue
146 }
148 return 0;
149 }
151 void ConfigurationXML::configurationTag(mxml_node_t *node) {
152 // handle all other performance counters
153 if (mIndex >= MAX_PERFORMANCE_COUNTERS) {
154 logg->logError(__FILE__, __LINE__, "Exceeded maximum number of %d performance counters", MAX_PERFORMANCE_COUNTERS);
155 handleException();
156 }
158 // read attributes
159 if (mxmlElementGetAttr(node, ATTR_COUNTER)) strncpy(gSessionData->mPerfCounterType[mIndex], mxmlElementGetAttr(node, ATTR_COUNTER), sizeof(gSessionData->mPerfCounterType[mIndex]));
160 if (mxmlElementGetAttr(node, ATTR_TITLE)) strncpy(gSessionData->mPerfCounterTitle[mIndex], mxmlElementGetAttr(node, ATTR_TITLE), sizeof(gSessionData->mPerfCounterTitle[mIndex]));
161 if (mxmlElementGetAttr(node, ATTR_NAME)) strncpy(gSessionData->mPerfCounterName[mIndex], mxmlElementGetAttr(node, ATTR_NAME), sizeof(gSessionData->mPerfCounterName[mIndex]));
162 if (mxmlElementGetAttr(node, ATTR_DESCRIPTION)) strncpy(gSessionData->mPerfCounterDescription[mIndex], mxmlElementGetAttr(node, ATTR_DESCRIPTION), sizeof(gSessionData->mPerfCounterDescription[mIndex]));
163 if (mxmlElementGetAttr(node, ATTR_EVENT)) gSessionData->mPerfCounterEvent[mIndex] = strtol(mxmlElementGetAttr(node, ATTR_EVENT), NULL, 16);
164 if (mxmlElementGetAttr(node, ATTR_COUNT)) gSessionData->mPerfCounterCount[mIndex] = strtol(mxmlElementGetAttr(node, ATTR_COUNT), NULL, 10);
165 if (mxmlElementGetAttr(node, ATTR_COLOR)) gSessionData->mPerfCounterColor[mIndex] = strtol(mxmlElementGetAttr(node, ATTR_COLOR), NULL, 16);
166 if (mxmlElementGetAttr(node, ATTR_PER_CPU)) gSessionData->mPerfCounterPerCPU[mIndex] = util->stringToBool(mxmlElementGetAttr(node, ATTR_PER_CPU), false);
167 if (mxmlElementGetAttr(node, ATTR_EBS)) gSessionData->mPerfCounterEBSCapable[mIndex] = util->stringToBool(mxmlElementGetAttr(node, ATTR_EBS), false);
168 if (mxmlElementGetAttr(node, ATTR_OPERATION)) strncpy(gSessionData->mPerfCounterOperation[mIndex], mxmlElementGetAttr(node, ATTR_OPERATION), sizeof(gSessionData->mPerfCounterOperation[mIndex]));
169 if (mxmlElementGetAttr(node, ATTR_LEVEL)) gSessionData->mPerfCounterLevel[mIndex] = util->stringToBool(mxmlElementGetAttr(node, ATTR_LEVEL), false);
170 if (mxmlElementGetAttr(node, ATTR_ALIAS)) strncpy(gSessionData->mPerfCounterAlias[mIndex], mxmlElementGetAttr(node, ATTR_ALIAS), sizeof(gSessionData->mPerfCounterAlias[mIndex]));
171 if (mxmlElementGetAttr(node, ATTR_DISPLAY)) strncpy(gSessionData->mPerfCounterDisplay[mIndex], mxmlElementGetAttr(node, ATTR_DISPLAY), sizeof(gSessionData->mPerfCounterDisplay[mIndex]));
172 if (mxmlElementGetAttr(node, ATTR_UNITS)) strncpy(gSessionData->mPerfCounterUnits[mIndex], mxmlElementGetAttr(node, ATTR_UNITS), sizeof(gSessionData->mPerfCounterUnits[mIndex]));
173 if (mxmlElementGetAttr(node, ATTR_AVERAGE_SELECTION)) gSessionData->mPerfCounterAverageSelection[mIndex] = util->stringToBool(mxmlElementGetAttr(node, ATTR_AVERAGE_SELECTION), false);
174 gSessionData->mPerfCounterEnabled[mIndex] = true;
176 // strncpy does not guarantee a null-termianted string
177 gSessionData->mPerfCounterType[mIndex][sizeof(gSessionData->mPerfCounterType[mIndex]) - 1] = 0;
178 gSessionData->mPerfCounterTitle[mIndex][sizeof(gSessionData->mPerfCounterTitle[mIndex]) - 1] = 0;
179 gSessionData->mPerfCounterName[mIndex][sizeof(gSessionData->mPerfCounterName[mIndex]) - 1] = 0;
180 gSessionData->mPerfCounterDescription[mIndex][sizeof(gSessionData->mPerfCounterDescription[mIndex]) - 1] = 0;
181 gSessionData->mPerfCounterOperation[mIndex][sizeof(gSessionData->mPerfCounterOperation[mIndex]) - 1] = 0;
182 gSessionData->mPerfCounterAlias[mIndex][sizeof(gSessionData->mPerfCounterAlias[mIndex]) - 1] = 0;
183 gSessionData->mPerfCounterDisplay[mIndex][sizeof(gSessionData->mPerfCounterDisplay[mIndex]) - 1] = 0;
184 gSessionData->mPerfCounterUnits[mIndex][sizeof(gSessionData->mPerfCounterUnits[mIndex]) - 1] = 0;
186 // update counter index
187 mIndex++;
188 }