blob: af726df71d598c9ba0237e5702faffd5f6deb688 [file] [log] [blame]
Jon Medhurstaaf37a32013-06-11 12:10:56 +01001/**
2 * Copyright (C) ARM Limited 2010-2013. 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 */
8
9#include <stdlib.h>
10#include <string.h>
11#include <dirent.h>
12#include "SessionData.h"
13#include "CapturedXML.h"
14#include "Logging.h"
15#include "OlyUtility.h"
16
17CapturedXML::CapturedXML() {
18}
19
20CapturedXML::~CapturedXML() {
21}
22
23mxml_node_t* CapturedXML::getTree(bool includeTime) {
24 mxml_node_t *xml;
25 mxml_node_t *captured;
26 mxml_node_t *target;
27 int x;
28
29 xml = mxmlNewXML("1.0");
30
31 captured = mxmlNewElement(xml, "captured");
32 mxmlElementSetAttr(captured, "version", "1");
33 mxmlElementSetAttrf(captured, "protocol", "%d", PROTOCOL_VERSION);
34 if (includeTime) { // Send the following only after the capture is complete
35 if (time(NULL) > 1267000000) { // If the time is reasonable (after Feb 23, 2010)
36 mxmlElementSetAttrf(captured, "created", "%lu", time(NULL)); // Valid until the year 2038
37 }
38 }
39
40 target = mxmlNewElement(captured, "target");
41 mxmlElementSetAttr(target, "name", gSessionData->mCoreName);
42 mxmlElementSetAttrf(target, "sample_rate", "%d", gSessionData->mSampleRate);
43 mxmlElementSetAttrf(target, "cores", "%d", gSessionData->mCores);
44 mxmlElementSetAttrf(target, "cpuid", "0x%x", gSessionData->mCpuId);
45
46 mxml_node_t *counters = NULL;
47 for (x = 0; x < MAX_PERFORMANCE_COUNTERS; x++) {
48 const Counter & counter = gSessionData->mCounters[x];
49 if (counter.isEnabled()) {
50 if (counters == NULL) {
51 counters = mxmlNewElement(captured, "counters");
52 }
53 mxml_node_t *const node = mxmlNewElement(counters, "counter");
54 mxmlElementSetAttrf(node, "key", "0x%x", counter.getKey());
55 mxmlElementSetAttr(node, "type", counter.getType());
56 mxmlElementSetAttrf(node, "event", "0x%x", counter.getEvent());
57 if (counter.getCount() > 0) {
58 mxmlElementSetAttrf(node, "count", "%d", counter.getCount());
59 }
60 }
61 }
62
63 return xml;
64}
65
66char* CapturedXML::getXML(bool includeTime) {
67 char* xml_string;
68 mxml_node_t *xml = getTree(includeTime);
69 xml_string = mxmlSaveAllocString(xml, mxmlWhitespaceCB);
70 mxmlDelete(xml);
71 return xml_string;
72}
73
74void CapturedXML::write(char* path) {
75 char file[PATH_MAX];
76
77 // Set full path
78 snprintf(file, PATH_MAX, "%s/captured.xml", path);
79
80 char* xml = getXML(true);
81 if (util->writeToDisk(file, xml) < 0) {
82 logg->logError(__FILE__, __LINE__, "Error writing %s\nPlease verify the path.", file);
83 handleException();
84 }
85
86 free(xml);
87}
88
89// whitespace callback utility function used with mini-xml
90const char * mxmlWhitespaceCB(mxml_node_t *node, int loc) {
91 const char *name;
92
93 name = mxmlGetElement(node);
94
95 if (loc == MXML_WS_BEFORE_OPEN) {
96 // Single indentation
97 if (!strcmp(name, "target") || !strcmp(name, "counters"))
98 return("\n ");
99
100 // Double indentation
101 if (!strcmp(name, "counter"))
102 return("\n ");
103
104 // Avoid a carriage return on the first line of the xml file
105 if (!strncmp(name, "?xml", 4))
106 return(NULL);
107
108 // Default - no indentation
109 return("\n");
110 }
111
112 if (loc == MXML_WS_BEFORE_CLOSE) {
113 // No indentation
114 if (!strcmp(name, "captured"))
115 return("\n");
116
117 // Single indentation
118 if (!strcmp(name, "counters"))
119 return("\n ");
120
121 // Default - no carriage return
122 return(NULL);
123 }
124
125 return(NULL);
126}