Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 1 | /** |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame^] | 2 | * Copyright (C) ARM Limited 2010-2015. All rights reserved. |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 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 | |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 9 | #include "CapturedXML.h" |
| 10 | |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <dirent.h> |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 14 | |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 15 | #include "SessionData.h" |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 16 | #include "Logging.h" |
| 17 | #include "OlyUtility.h" |
| 18 | |
| 19 | CapturedXML::CapturedXML() { |
| 20 | } |
| 21 | |
| 22 | CapturedXML::~CapturedXML() { |
| 23 | } |
| 24 | |
| 25 | mxml_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 Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 35 | if (gSessionData->perf.isSetup()) { |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 36 | mxmlElementSetAttr(captured, "type", "Perf"); |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 37 | } |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 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 |
| 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 Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 49 | mxmlElementSetAttrf(target, "cpuid", "0x%x", gSessionData->mMaxCpuId); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 50 | |
Jon Medhurst | d369859 | 2013-10-10 16:48:56 +0100 | [diff] [blame] | 51 | 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 Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 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"); |
| 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 Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 69 | if (counter.getEvent() != -1) { |
| 70 | mxmlElementSetAttrf(node, "event", "0x%x", counter.getEvent()); |
| 71 | } |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 72 | if (counter.getCount() > 0) { |
| 73 | mxmlElementSetAttrf(node, "count", "%d", counter.getCount()); |
| 74 | } |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 75 | if (counter.getCores() > 0) { |
| 76 | mxmlElementSetAttrf(node, "cores", "%d", counter.getCores()); |
| 77 | } |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | return xml; |
| 82 | } |
| 83 | |
| 84 | char* 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 | |
| 92 | void CapturedXML::write(char* path) { |
| 93 | char file[PATH_MAX]; |
| 94 | |
| 95 | // Set full path |
| 96 | snprintf(file, PATH_MAX, "%s/captured.xml", path); |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 97 | |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 98 | char* xml = getXML(true); |
| 99 | if (util->writeToDisk(file, xml) < 0) { |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame^] | 100 | logg->logError("Error writing %s\nPlease verify the path.", file); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 101 | handleException(); |
| 102 | } |
| 103 | |
| 104 | free(xml); |
| 105 | } |
| 106 | |
| 107 | // whitespace callback utility function used with mini-xml |
| 108 | const 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")) |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 116 | return "\n "; |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 117 | |
| 118 | // Double indentation |
| 119 | if (!strcmp(name, "counter")) |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 120 | return "\n "; |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 121 | |
| 122 | // Avoid a carriage return on the first line of the xml file |
| 123 | if (!strncmp(name, "?xml", 4)) |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 124 | return NULL; |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 125 | |
| 126 | // Default - no indentation |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 127 | return "\n"; |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | if (loc == MXML_WS_BEFORE_CLOSE) { |
| 131 | // No indentation |
| 132 | if (!strcmp(name, "captured")) |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 133 | return "\n"; |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 134 | |
| 135 | // Single indentation |
| 136 | if (!strcmp(name, "counters")) |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 137 | return "\n "; |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 138 | |
| 139 | // Default - no carriage return |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 140 | return NULL; |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 143 | return NULL; |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 144 | } |