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");
37 mxmlElementSetAttr(captured, "perf_beta", "yes");
39 mxmlElementSetAttrf(captured, "protocol", "%d", PROTOCOL_VERSION);
40 if (includeTime) { // Send the following only after the capture is complete
41 if (time(NULL) > 1267000000) { // If the time is reasonable (after Feb 23, 2010)
42 mxmlElementSetAttrf(captured, "created", "%lu", time(NULL)); // Valid until the year 2038
46 target = mxmlNewElement(captured, "target");
47 mxmlElementSetAttr(target, "name", gSessionData->mCoreName);
48 mxmlElementSetAttrf(target, "sample_rate", "%d", gSessionData->mSampleRate);
49 mxmlElementSetAttrf(target, "cores", "%d", gSessionData->mCores);
50 mxmlElementSetAttrf(target, "cpuid", "0x%x", gSessionData->mMaxCpuId);
52 if (!gSessionData->mOneShot && (gSessionData->mSampleRate > 0)) {
53 mxmlElementSetAttr(target, "supports_live", "yes");
56 if (gSessionData->mLocalCapture) {
57 mxmlElementSetAttr(target, "local_capture", "yes");
60 mxml_node_t *counters = NULL;
61 for (x = 0; x < MAX_PERFORMANCE_COUNTERS; x++) {
62 const Counter & counter = gSessionData->mCounters[x];
63 if (counter.isEnabled()) {
64 if (counters == NULL) {
65 counters = mxmlNewElement(captured, "counters");
67 mxml_node_t *const node = mxmlNewElement(counters, "counter");
68 mxmlElementSetAttrf(node, "key", "0x%x", counter.getKey());
69 mxmlElementSetAttr(node, "type", counter.getType());
70 if (counter.getEvent() != -1) {
71 mxmlElementSetAttrf(node, "event", "0x%x", counter.getEvent());
73 if (counter.getCount() > 0) {
74 mxmlElementSetAttrf(node, "count", "%d", counter.getCount());
76 if (counter.getCores() > 0) {
77 mxmlElementSetAttrf(node, "cores", "%d", counter.getCores());
85 char* CapturedXML::getXML(bool includeTime) {
87 mxml_node_t *xml = getTree(includeTime);
88 xml_string = mxmlSaveAllocString(xml, mxmlWhitespaceCB);
93 void CapturedXML::write(char* path) {
97 snprintf(file, PATH_MAX, "%s/captured.xml", path);
99 char* xml = getXML(true);
100 if (util->writeToDisk(file, xml) < 0) {
101 logg->logError(__FILE__, __LINE__, "Error writing %s\nPlease verify the path.", file);
108 // whitespace callback utility function used with mini-xml
109 const char * mxmlWhitespaceCB(mxml_node_t *node, int loc) {
112 name = mxmlGetElement(node);
114 if (loc == MXML_WS_BEFORE_OPEN) {
115 // Single indentation
116 if (!strcmp(name, "target") || !strcmp(name, "counters"))
119 // Double indentation
120 if (!strcmp(name, "counter"))
123 // Avoid a carriage return on the first line of the xml file
124 if (!strncmp(name, "?xml", 4))
127 // Default - no indentation
131 if (loc == MXML_WS_BEFORE_CLOSE) {
133 if (!strcmp(name, "captured"))
136 // Single indentation
137 if (!strcmp(name, "counters"))
140 // Default - no carriage return