blob: 4a11415a00c978bfe9be2d350d4e95644522c3dd [file] [log] [blame]
Jon Medhurstaaf37a32013-06-11 12:10:56 +01001/**
Jon Medhurst15ce78d2014-04-10 09:02:02 +01002 * Copyright (C) ARM Limited 2010-2014. All rights reserved.
Jon Medhurstaaf37a32013-06-11 12:10:56 +01003 *
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
Jon Medhurst15ce78d2014-04-10 09:02:02 +01009#include "CapturedXML.h"
10
Jon Medhurstaaf37a32013-06-11 12:10:56 +010011#include <stdlib.h>
12#include <string.h>
13#include <dirent.h>
Jon Medhurst15ce78d2014-04-10 09:02:02 +010014
Jon Medhurstaaf37a32013-06-11 12:10:56 +010015#include "SessionData.h"
Jon Medhurstaaf37a32013-06-11 12:10:56 +010016#include "Logging.h"
17#include "OlyUtility.h"
18
19CapturedXML::CapturedXML() {
20}
21
22CapturedXML::~CapturedXML() {
23}
24
25mxml_node_t* CapturedXML::getTree(bool includeTime) {
26 mxml_node_t *xml;
27 mxml_node_t *captured;
28 mxml_node_t *target;
29 int x;
30
31 xml = mxmlNewXML("1.0");
32
33 captured = mxmlNewElement(xml, "captured");
34 mxmlElementSetAttr(captured, "version", "1");
Jon Medhurst15ce78d2014-04-10 09:02:02 +010035 if (gSessionData->perf.isSetup()) {
Jon Medhurste31266f2014-08-04 15:47:44 +010036 mxmlElementSetAttr(captured, "type", "Perf");
Jon Medhurst15ce78d2014-04-10 09:02:02 +010037 }
Jon Medhurstaaf37a32013-06-11 12:10:56 +010038 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
42 }
43 }
44
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);
Jon Medhurst15ce78d2014-04-10 09:02:02 +010049 mxmlElementSetAttrf(target, "cpuid", "0x%x", gSessionData->mMaxCpuId);
Jon Medhurstaaf37a32013-06-11 12:10:56 +010050
Jon Medhurstd3698592013-10-10 16:48:56 +010051 if (!gSessionData->mOneShot && (gSessionData->mSampleRate > 0)) {
52 mxmlElementSetAttr(target, "supports_live", "yes");
53 }
54
55 if (gSessionData->mLocalCapture) {
56 mxmlElementSetAttr(target, "local_capture", "yes");
57 }
58
Jon Medhurstaaf37a32013-06-11 12:10:56 +010059 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");
65 }
66 mxml_node_t *const node = mxmlNewElement(counters, "counter");
67 mxmlElementSetAttrf(node, "key", "0x%x", counter.getKey());
68 mxmlElementSetAttr(node, "type", counter.getType());
Jon Medhurste31266f2014-08-04 15:47:44 +010069 if (counter.getEvent() != -1) {
70 mxmlElementSetAttrf(node, "event", "0x%x", counter.getEvent());
71 }
Jon Medhurstaaf37a32013-06-11 12:10:56 +010072 if (counter.getCount() > 0) {
73 mxmlElementSetAttrf(node, "count", "%d", counter.getCount());
74 }
Jon Medhurste31266f2014-08-04 15:47:44 +010075 if (counter.getCores() > 0) {
76 mxmlElementSetAttrf(node, "cores", "%d", counter.getCores());
77 }
Jon Medhurstaaf37a32013-06-11 12:10:56 +010078 }
79 }
80
81 return xml;
82}
83
84char* CapturedXML::getXML(bool includeTime) {
85 char* xml_string;
86 mxml_node_t *xml = getTree(includeTime);
87 xml_string = mxmlSaveAllocString(xml, mxmlWhitespaceCB);
88 mxmlDelete(xml);
89 return xml_string;
90}
91
92void CapturedXML::write(char* path) {
93 char file[PATH_MAX];
94
95 // Set full path
96 snprintf(file, PATH_MAX, "%s/captured.xml", path);
Jon Medhurste31266f2014-08-04 15:47:44 +010097
Jon Medhurstaaf37a32013-06-11 12:10:56 +010098 char* xml = getXML(true);
99 if (util->writeToDisk(file, xml) < 0) {
100 logg->logError(__FILE__, __LINE__, "Error writing %s\nPlease verify the path.", file);
101 handleException();
102 }
103
104 free(xml);
105}
106
107// whitespace callback utility function used with mini-xml
108const char * mxmlWhitespaceCB(mxml_node_t *node, int loc) {
109 const char *name;
110
111 name = mxmlGetElement(node);
112
113 if (loc == MXML_WS_BEFORE_OPEN) {
114 // Single indentation
115 if (!strcmp(name, "target") || !strcmp(name, "counters"))
116 return("\n ");
117
118 // Double indentation
119 if (!strcmp(name, "counter"))
120 return("\n ");
121
122 // Avoid a carriage return on the first line of the xml file
123 if (!strncmp(name, "?xml", 4))
124 return(NULL);
125
126 // Default - no indentation
127 return("\n");
128 }
129
130 if (loc == MXML_WS_BEFORE_CLOSE) {
131 // No indentation
132 if (!strcmp(name, "captured"))
133 return("\n");
134
135 // Single indentation
136 if (!strcmp(name, "counters"))
137 return("\n ");
138
139 // Default - no carriage return
140 return(NULL);
141 }
142
143 return(NULL);
144}