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 "CapturedXML.h"
15 #include "SessionData.h"
17 #include "OlyUtility.h"
19 CapturedXML::CapturedXML() {
22 CapturedXML::~CapturedXML() {
25 mxml_node_t* CapturedXML::getTree(bool includeTime) {
27 mxml_node_t *captured;
31 xml = mxmlNewXML("1.0");
33 captured = mxmlNewElement(xml, "captured");
34 mxmlElementSetAttr(captured, "version", "1");
35 if (gSessionData->perf.isSetup()) {
36 mxmlElementSetAttr(captured, "type", "Perf");
38 mxmlElementSetAttrf(captured, "protocol", "%d", PROTOCOL_VERSION);
39 if (includeTime) { // Send the following only after the capture is complete
40 if (time(NULL) > 1267000000) { // If the time is reasonable (after Feb 23, 2010)
41 mxmlElementSetAttrf(captured, "created", "%lu", time(NULL)); // Valid until the year 2038
45 target = mxmlNewElement(captured, "target");
46 mxmlElementSetAttr(target, "name", gSessionData->mCoreName);
47 mxmlElementSetAttrf(target, "sample_rate", "%d", gSessionData->mSampleRate);
48 mxmlElementSetAttrf(target, "cores", "%d", gSessionData->mCores);
49 mxmlElementSetAttrf(target, "cpuid", "0x%x", gSessionData->mMaxCpuId);
51 if (!gSessionData->mOneShot && (gSessionData->mSampleRate > 0)) {
52 mxmlElementSetAttr(target, "supports_live", "yes");
55 if (gSessionData->mLocalCapture) {
56 mxmlElementSetAttr(target, "local_capture", "yes");
59 mxml_node_t *counters = NULL;
60 for (x = 0; x < MAX_PERFORMANCE_COUNTERS; x++) {
61 const Counter & counter = gSessionData->mCounters[x];
62 if (counter.isEnabled()) {
63 if (counters == NULL) {
64 counters = mxmlNewElement(captured, "counters");
66 mxml_node_t *const node = mxmlNewElement(counters, "counter");
67 mxmlElementSetAttrf(node, "key", "0x%x", counter.getKey());
68 mxmlElementSetAttr(node, "type", counter.getType());
69 mxmlElementSetAttrf(node, "event", "0x%x", counter.getEvent());
70 if (counter.getCount() > 0) {
71 mxmlElementSetAttrf(node, "count", "%d", counter.getCount());
79 char* CapturedXML::getXML(bool includeTime) {
81 mxml_node_t *xml = getTree(includeTime);
82 xml_string = mxmlSaveAllocString(xml, mxmlWhitespaceCB);
87 void CapturedXML::write(char* path) {
91 snprintf(file, PATH_MAX, "%s/captured.xml", path);
93 char* xml = getXML(true);
94 if (util->writeToDisk(file, xml) < 0) {
95 logg->logError(__FILE__, __LINE__, "Error writing %s\nPlease verify the path.", file);
102 // whitespace callback utility function used with mini-xml
103 const char * mxmlWhitespaceCB(mxml_node_t *node, int loc) {
106 name = mxmlGetElement(node);
108 if (loc == MXML_WS_BEFORE_OPEN) {
109 // Single indentation
110 if (!strcmp(name, "target") || !strcmp(name, "counters"))
113 // Double indentation
114 if (!strcmp(name, "counter"))
117 // Avoid a carriage return on the first line of the xml file
118 if (!strncmp(name, "?xml", 4))
121 // Default - no indentation
125 if (loc == MXML_WS_BEFORE_CLOSE) {
127 if (!strcmp(name, "captured"))
130 // Single indentation
131 if (!strcmp(name, "counters"))
134 // Default - no carriage return