1 /*
2 * \file device_parser.cpp
3 * \brief OpenCSD : Snapshot Parser Library
4 *
5 * \copyright Copyright (c) 2015, ARM Limited. All Rights Reserved.
6 */
7
8 /*
9 * Redistribution and use in source and binary forms, with or without modification,
10 * are permitted provided that the following conditions are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of the copyright holder nor the names of its contributors
20 * may be used to endorse or promote products derived from this software without
21 * specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34 #include "device_parser.h"
35
36 #include "snapshot_parser.h"
37
38 #include <sstream>
39 #include <iostream>
40 #include <string>
41 #include <memory>
42 #include <cstdio>
43
44 #include "common/ocsd_error.h"
45
46 using namespace std;
47
ModernSnapshotParser()48 ModernSnapshotParser::ModernSnapshotParser() : initialised(false), traceMetaDataIni("")
49 {
50 }
51
ModernSnapshotParser(std::istream & iss)52 ModernSnapshotParser::ModernSnapshotParser( std::istream &iss) : initialised(false), traceMetaDataIni("")
53 {
54 Parser::ParsedDevices pd = Parser::ParseDeviceList(iss);
55 std::map<std::string, std::string> rawList = pd.deviceList;
56 std::map<std::string, std::string>::iterator it;
57 for (it = rawList.begin(); it != rawList.end(); ++it )
58 {
59 addDevice(it->first, it->second);
60 }
61 snaphotInfo = pd.snapshotInfo;
62 this->traceMetaDataIni = pd.traceMetaDataName;
63 initialised = true;
64 }
65
~ModernSnapshotParser()66 ModernSnapshotParser::~ModernSnapshotParser()
67 {
68 }
69
addDevice(std::string rawName,std::string path)70 void ModernSnapshotParser::addDevice(std::string rawName, std::string path)
71 {
72 uint32_t deviceID = getUniqueDeviceID(rawName);
73 deviceMap[deviceID] = DevPtr(new DeviceInfo(deviceID, path));
74 }
75
getUniqueDeviceID(std::string & rawName)76 uint32_t ModernSnapshotParser::getUniqueDeviceID(std::string &rawName)
77 {
78 size_t pos = rawName.find_first_of("0123456789");
79 uint32_t candidate = 1;
80 if (pos != std::string::npos)
81 {
82 std::string numbers = rawName.substr(pos);
83 istringstream ist(numbers);
84 ist >> candidate;
85 }
86 while (true)
87 {
88 try
89 {
90 getDevice(candidate);
91 // didn't throw means already in list, so bad candidate
92 candidate++;
93 }
94 catch (ocsdError & /*e*/)
95 {
96 // threw, so unique ID, so this is good, leave loop
97 break;
98 }
99 }
100 return candidate;
101 }
102
isInitialised() const103 bool ModernSnapshotParser::isInitialised() const
104 {
105 return initialised;
106 }
107
getDeviceCount() const108 size_t ModernSnapshotParser::getDeviceCount() const
109 {
110 unsigned int deviceCount = 0;
111 if (isInitialised())
112 deviceCount = deviceMap.size();
113 return deviceCount;
114 }
115
getDevice(int id)116 ModernSnapshotParser::DevPtr ModernSnapshotParser::getDevice(int id)
117 {
118 map<int, DevPtr>::const_iterator it(deviceMap.find(id));
119 if (it == deviceMap.end())
120 {
121 std::ostringstream ost;
122 ost << "Unknown device:" << id;
123 throw ocsdError(OCSD_ERR_SEV_ERROR, OCSD_ERR_TEST_SNAPSHOT_PARSE, ost.str());
124 }
125 return it->second;
126 }
127
addSnapshotInfo(SnapshotInfo snap)128 void ModernSnapshotParser::addSnapshotInfo(SnapshotInfo snap)
129 {
130 snaphotInfo = snap;
131 }
132
getDeviceList(std::vector<int> & devices) const133 void ModernSnapshotParser::getDeviceList(std::vector<int> &devices) const
134 {
135 map<int, DevPtr>::const_iterator it;
136 //devices.clear();
137 for (it = deviceMap.begin(); it != deviceMap.end(); ++it)
138 devices.push_back(it->first);
139 }
140
getDescription() const141 std::string ModernSnapshotParser::getDescription() const
142 {
143 return snaphotInfo.description;
144 }
145
getVersion() const146 std::string ModernSnapshotParser::getVersion() const
147 {
148 return snaphotInfo.version;
149 }
150
151
getTraceMetadataFile() const152 std::string ModernSnapshotParser::getTraceMetadataFile() const
153 {
154 return traceMetaDataIni;
155 }
156
157 /* End of File device_parser.cpp */
158