e1bec0fd4fa89ddb9118355827a3f575f8cfd26a
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_VERSION = "version";
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";
31 ConfigurationXML::ConfigurationXML() {
32 #include "configuration_xml.h" // defines and initializes char configuration_xml[] and int configuration_xml_len
33 index = 0;
34 char* path = (char *)malloc(PATH_MAX);
36 if (gSessionData->configurationXMLPath) {
37 strncpy(path, gSessionData->configurationXMLPath, PATH_MAX);
38 } else {
39 if (util->getApplicationFullPath(path, PATH_MAX) != 0) {
40 logg->logMessage("Unable to determine the full path of gatord, the cwd will be used");
41 }
42 strncat(path, "configuration.xml", PATH_MAX - strlen(path) - 1);
43 }
44 mConfigurationXML = util->readFromDisk(path);
46 if (mConfigurationXML == NULL) {
47 logg->logMessage("Unable to locate configuration.xml, using default in binary");
48 // null-terminate configuration_xml
49 mConfigurationXML = (char*)malloc(configuration_xml_len + 1);
50 memcpy(mConfigurationXML, (const void*)configuration_xml, configuration_xml_len);
51 mConfigurationXML[configuration_xml_len] = 0;
52 }
54 // disable all counters prior to parsing the configuration xml
55 for (int i = 0; i < MAX_PERFORMANCE_COUNTERS; i++) {
56 gSessionData->mPerfCounterEnabled[i] = 0;
57 }
59 int ret = parse(mConfigurationXML);
60 if (ret == 1) {
61 // remove configuration.xml on disk to use the default
62 if (remove(path) != 0) {
63 logg->logError(__FILE__, __LINE__, "Invalid configuration.xml file detected and unable to delete it. To resolve, delete configuration.xml on disk");
64 handleException();
65 }
66 } else if (ret < 0 || isValid() == false) {
67 logg->logError(__FILE__, __LINE__, "Parsing of the configuration.xml file failed. Please verify configuration.xml on the target filesystem is valid or delete it to use the default.");
68 handleException();
69 }
71 free(path);
72 }
74 ConfigurationXML::~ConfigurationXML() {
75 if (mConfigurationXML) {
76 free((void*)mConfigurationXML);
77 }
78 }
80 int ConfigurationXML::parse(const char* configurationXML) {
81 int ret = 0;
82 XMLReader reader(configurationXML);
83 char * tag = reader.nextTag();
84 while(tag != 0 && ret == 0) {
85 if (strcmp(tag, "configurations") == 0) {
86 ret = configurationsTag(&reader);
87 } else if (strcmp(tag, "configuration") == 0) {
88 ret = configurationTag(&reader);
89 }
90 tag = reader.nextTag();
91 }
93 return ret;
94 }
96 bool ConfigurationXML::isValid(void) {
97 for (int i = 0; i < MAX_PERFORMANCE_COUNTERS; i++) {
98 if (gSessionData->mPerfCounterEnabled[i]) {
99 if (strcmp(gSessionData->mPerfCounterType[i], "") == 0 ||
100 strcmp(gSessionData->mPerfCounterTitle[i], "") == 0 ||
101 strcmp(gSessionData->mPerfCounterName[i], "") == 0) {
102 logg->logMessage("Invalid required attribute\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]);
103 return false; // failure
104 }
106 // iterate through the remaining enabled performance counters
107 for (int j = i + 1; j < MAX_PERFORMANCE_COUNTERS; j++) {
108 if (gSessionData->mPerfCounterEnabled[j]) {
109 // check if the type or device are the same
110 if (strcmp(gSessionData->mPerfCounterType[i], gSessionData->mPerfCounterType[j]) == 0) {
111 logg->logMessage("Duplicate performance counter type: %s", gSessionData->mPerfCounterType[i]);
112 return false; // failure
113 }
114 }
115 }
116 }
117 }
119 return true; // success
120 }
122 #define CONFIGURATION_VERSION 1
123 int ConfigurationXML::configurationsTag(XMLReader *in) {
124 int version = in->getAttributeAsInteger(ATTR_VERSION, 0);
125 if (version != CONFIGURATION_VERSION) {
126 logg->logMessage("Incompatible configuration.xml version (%d) detected. The version needs to be %d.", version, CONFIGURATION_VERSION);
127 return 1; // version issue
128 }
129 return 0;
130 }
132 int ConfigurationXML::configurationTag(XMLReader* in) {
133 // handle all other performance counters
134 if (index >= MAX_PERFORMANCE_COUNTERS) {
135 logg->logMessage("Invalid performance counter index: %d", index);
136 return -1; // failure
137 }
139 // read attributes
140 in->getAttribute(ATTR_COUNTER, gSessionData->mPerfCounterType[index], sizeof(gSessionData->mPerfCounterType[index]), "");
141 in->getAttribute(ATTR_TITLE, gSessionData->mPerfCounterTitle[index], sizeof(gSessionData->mPerfCounterTitle[index]), "");
142 in->getAttribute(ATTR_NAME, gSessionData->mPerfCounterName[index], sizeof(gSessionData->mPerfCounterName[index]), "");
143 in->getAttribute(ATTR_DESCRIPTION, gSessionData->mPerfCounterDescription[index], sizeof(gSessionData->mPerfCounterDescription[index]), "");
144 gSessionData->mPerfCounterEvent[index] = in->getAttributeAsInteger(ATTR_EVENT, 0);
145 gSessionData->mPerfCounterCount[index] = in->getAttributeAsInteger(ATTR_COUNT, 0);
146 gSessionData->mPerfCounterColor[index] = in->getAttributeAsInteger(ATTR_COLOR, 0);
147 gSessionData->mPerfCounterPerCPU[index] = in->getAttributeAsBoolean(ATTR_PER_CPU, false);
148 gSessionData->mPerfCounterEBSCapable[index] = in->getAttributeAsBoolean(ATTR_EBS, false);
149 in->getAttribute(ATTR_OPERATION, gSessionData->mPerfCounterOperation[index], sizeof(gSessionData->mPerfCounterOperation[index]), "");
150 gSessionData->mPerfCounterEnabled[index] = true;
152 // update counter index
153 index++;
155 return 0; // success
156 }