• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * \file       snapshot_reader.cpp
3  * \brief      OpenCSD :
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 
35 #include "snapshot_reader.h"
36 #include "snapshot_parser.h"
37 #include "device_parser.h"
38 
39 #include "opencsd.h"
40 
41 #include <fstream>
42 #include <iterator>
43 #include <ios>
44 #include <iostream>
45 
46 #ifdef WIN32
47 #define DIR_CHAR '\\'
48 #else
49 #define DIR_CHAR '/'
50 #endif
51 
52 using namespace Parser;
53 
SnapShotReader()54 SnapShotReader::SnapShotReader() :
55     m_snapshotPath(""),
56     m_snapshot_found(false),
57     m_i_err_log(0),
58     m_errlog_handle(0),
59     m_verbose(true),
60     m_read_ok(false)
61 {
62     checkPath();    // see if default will work.
63 }
64 
~SnapShotReader()65 SnapShotReader::~SnapShotReader()
66 {
67 }
68 
setSnapshotDir(const std::string & dir)69 void SnapShotReader::setSnapshotDir(const std::string &dir)
70 {
71     m_snapshotPath = dir;
72     if(dir.size() > 0)
73     {
74         if(dir.at(dir.size()-1) != DIR_CHAR)
75             m_snapshotPath += DIR_CHAR;
76     }
77     m_read_ok = false;
78     checkPath();
79 }
80 
readSnapShot()81 const bool SnapShotReader::readSnapShot()
82 {
83     bool bRead = true;
84 
85     std::string iniFile = m_snapshotPath + SnapshotINIFilename;
86     std::ifstream in(iniFile.c_str());
87     std::ostringstream oss;
88 
89     if(in.is_open())
90     {
91         Parser::SetVerboseLogging(m_verbose);
92         ModernSnapshotParser parser(in);
93         in.close();
94 
95         if(m_verbose)
96         {
97             oss.str("");
98             oss << "Parsed snapshot.ini." << std::endl;
99             oss << "Found " << parser.getDeviceCount() << " devices." << std::endl;
100             LogInfo(oss.str());
101         }
102 
103         std::vector<int> device_list;
104         parser.getDeviceList(device_list);
105 
106         ModernSnapshotParser::DevPtr device;
107 
108         for(size_t i = 0; i < device_list.size(); i++)
109         {
110             device = parser.getDevice(device_list[i]);
111             if(m_verbose)
112             {
113                 oss.str("");
114                 oss << "Device " << device.get()->getID() << ": Ini file = " << device.get()->getIniFile() << "; Name = " << device.get()->getName() << std::endl;
115                 LogInfo(oss.str());
116             }
117             iniFile = m_snapshotPath + device.get()->getIniFile();
118             in.open(iniFile.c_str());
119             if(in.is_open())
120             {
121                 Parser::Parsed pdev = Parser::ParseSingleDevice(in);
122                 m_parsed_device_list[pdev.deviceName] = pdev;   // map devices by name
123                 in.close();
124 
125             }
126             else
127             {
128                 oss.str("");
129                 oss << "Failed to open device file : " << iniFile << std::endl;
130                 LogError(oss.str());
131             }
132         }
133 
134         if(parser.getTraceMetadataFile().length() > 0)
135         {
136             if(m_verbose)
137             {
138                 oss.str("");
139                 oss << "Trace Metadata ini file found : " << parser.getTraceMetadataFile() << std::endl;
140                 LogInfo(oss.str());
141             }
142 
143             iniFile = m_snapshotPath + parser.getTraceMetadataFile();
144             in.open(iniFile.c_str());
145             if(in.is_open())
146             {
147                 m_parsed_trace = Parser::ParseTraceMetaData(in);
148                 in.close();
149                 if(m_parsed_trace.trace_buffers.size()) // found one or more buffers
150                 {
151                     std::vector<std::string> bufferNames = GetBufferNameList(m_parsed_trace);
152                     std::vector<std::string>::iterator bnit = bufferNames.begin();
153                     while(bnit != bufferNames.end())
154                     {
155                         Parser::TraceBufferSourceTree tbst;
156                         if(Parser::ExtractSourceTree(*bnit,m_parsed_trace,tbst))
157                             m_source_trees[*bnit] = tbst;
158                         bnit++;
159                     }
160                 }
161 
162             }
163             else
164             {
165                 oss.str("");
166                 oss << "Failed to trace ini file : " << iniFile << std::endl;
167                 LogError(oss.str());
168             }
169         }
170         else
171         {
172             oss.str("");
173             oss << "Trace Metadata ini file not found." << std::endl;
174             LogError(oss.str());
175         }
176 
177         if(m_verbose)
178         {
179             oss.str("");
180             oss << "Done." << std::endl;
181             LogInfo(oss.str());
182         }
183     }
184     else
185     {
186         oss.str("");
187         oss << "Read Error : Failed to open " << iniFile << "." << std::endl;
188         LogError(oss.str());
189         bRead = false;
190     }
191     m_read_ok = bRead;
192     return bRead;
193 }
194 
checkPath()195 void SnapShotReader::checkPath()
196 {
197     std::string iniFile = m_snapshotPath + SnapshotINIFilename;
198     std::ifstream in(iniFile.c_str());
199     m_snapshot_found = false;
200 
201     if(in.is_open())
202     {
203         in.close();
204         m_snapshot_found = true;
205     }
206 }
207 
setErrorLogger(ITraceErrorLog * err_log)208 void SnapShotReader::setErrorLogger(ITraceErrorLog *err_log)
209 {
210     if(err_log)
211     {
212         m_i_err_log = err_log;
213         m_errlog_handle = m_i_err_log->RegisterErrorSource("snapshot_reader");
214         Parser::SetIErrorLogger(m_i_err_log);
215     }
216 }
217 
LogInfo(const std::string & msg)218 void SnapShotReader::LogInfo(const std::string &msg)
219 {
220     if(m_i_err_log)
221         m_i_err_log->LogMessage(m_errlog_handle, OCSD_ERR_SEV_INFO, msg);
222 }
223 
LogError(const std::string & msg)224 void SnapShotReader::LogError(const std::string &msg)
225 {
226     if(m_i_err_log)
227     {
228         ocsdError err(OCSD_ERR_SEV_ERROR,OCSD_ERR_TEST_SNAPSHOT_READ,msg);
229         m_i_err_log->LogError(m_errlog_handle,&err);
230     }
231 }
232 
getSourceBufferNameList(std::vector<std::string> & nameList)233 bool SnapShotReader::getSourceBufferNameList(std::vector<std::string> &nameList)
234 {
235     nameList.clear();
236     if(snapshotFound())
237     {
238         nameList = GetBufferNameList(m_parsed_trace);
239     }
240     return (bool)(nameList.size() > 0);
241 }
242 
getTraceBufferSourceTree(const std::string & traceBufferName,Parser::TraceBufferSourceTree & sourceTree)243 bool SnapShotReader::getTraceBufferSourceTree(const std::string &traceBufferName, Parser::TraceBufferSourceTree &sourceTree)
244 {
245     bool found = false;
246     std::map<std::string, Parser::TraceBufferSourceTree>::iterator it;
247     it = m_source_trees.find(traceBufferName);
248     if(it != m_source_trees.end())
249     {
250         sourceTree = it->second;
251         found = true;
252     }
253     return found;
254 }
255 
getDeviceData(const std::string & deviceName,Parser::Parsed ** devData)256 bool SnapShotReader::getDeviceData(const std::string &deviceName, Parser::Parsed **devData)
257 {
258     std::map<std::string, Parser::Parsed>::iterator it;
259 
260     *devData = 0;
261     it = m_parsed_device_list.find(deviceName);
262     if(it != m_parsed_device_list.end())
263     {
264         *devData = &(it->second);
265     }
266     return (*devData != 0);
267 }
268 
269 
270 /* End of File snapshot_reader.cpp */
271