1 /*
2  * \file       ocsd_error.cpp
3  * \brief      OpenCSD : Library error class.
4  *
5  * \copyright  Copyright (c) 2015, ARM Limited. All Rights Reserved.
6  */
7 
8 
9 /*
10  * Redistribution and use in source and binary forms, with or without modification,
11  * are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  * this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright notice,
17  * this list of conditions and the following disclaimer in the documentation
18  * and/or other materials provided with the distribution.
19  *
20  * 3. Neither the name of the copyright holder nor the names of its contributors
21  * may be used to endorse or promote products derived from this software without
22  * specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include "common/ocsd_error.h"
37 #include <sstream>
38 #include <iomanip>
39 
40 static const char *s_errorCodeDescs[][2] = {
41     /* general return errors */
42     {"OCSD_OK", "No Error."},
43     {"OCSD_ERR_FAIL","General failure."},
44     {"OCSD_ERR_MEM","Internal memory allocation error."},
45     {"OCSD_ERR_NOT_INIT","Component not initialised."},
46     {"OCSD_ERR_INVALID_ID","Invalid CoreSight Trace Source ID."},
47     {"OCSD_ERR_BAD_HANDLE","Invalid handle passed to component."},
48     {"OCSD_ERR_INVALID_PARAM_VAL","Invalid value parameter passed to component."},
49     {"OCSD_ERR_INVALID_PARAM_TYPE","Type mismatch on abstract interface."},
50     {"OCSD_ERR_FILE_ERROR","File access error"},
51     {"OCSD_ERR_NO_PROTOCOL","Trace protocol unsupported"},
52     /* attachment point errors */
53     {"OCSD_ERR_ATTACH_TOO_MANY","Cannot attach - attach device limit reached."},
54     {"OCSD_ERR_ATTACH_INVALID_PARAM"," Cannot attach - invalid parameter."},
55     {"OCSD_ERR_ATTACH_COMP_NOT_FOUND","Cannot detach - component not found."},
56     /* source reader errors */
57     {"OCSD_ERR_RDR_FILE_NOT_FOUND","source reader - file not found."},
58     {"OCSD_ERR_RDR_INVALID_INIT",  "source reader - invalid initialisation parameter."},
59     {"OCSD_ERR_RDR_NO_DECODER", "source reader - not trace decoder set."},
60     /* data path errors */
61     {"OCSD_ERR_DATA_DECODE_FATAL", "A decoder in the data path has returned a fatal error."},
62     /* frame deformatter errors */
63     {"OCSD_ERR_DFMTR_NOTCONTTRACE", "Trace input to deformatter none-continuous"},
64     /* packet processor errors - protocol issues etc */
65     {"OCSD_ERR_BAD_PACKET_SEQ","Bad packet sequence"},
66     {"OCSD_ERR_INVALID_PCKT_HDR","Invalid packet header"},
67     {"OCSD_ERR_PKT_INTERP_FAIL","Interpreter failed - cannot recover - bad data or sequence"},
68     /* packet decoder errors */
69     {"OCSD_ERR_UNSUPPORTED_ISA","ISA not supported in decoder"},
70     {"OCSD_ERR_HW_CFG_UNSUPP","Programmed trace configuration not supported by decodUer."},
71     {"OCSD_ERR_UNSUPP_DECODE_PKT","Packet not supported in decoder"},
72     {"OCSD_ERR_BAD_DECODE_PKT","Reserved or unknown packet in decoder."},
73     {"OCSD_ERR_COMMIT_PKT_OVERRUN","Overrun in commit packet stack - tried to commit more than available"},
74     {"OCSD_ERR_MEM_NACC","Unable to access required memory address."},
75     {"OCSD_ERR_RET_STACK_OVERFLOW","Internal return stack overflow checks failed - popped more than we pushed."},
76     /* decode tree errors */
77     {"OCSD_ERR_DCDT_NO_FORMATTER","No formatter in use - operation not valid."},
78     /* target memory access errors */
79     {"OCSD_ERR_MEM_ACC_OVERLAP","Attempted to set an overlapping range in memory access map."},
80     {"OCSD_ERR_MEM_ACC_FILE_NOT_FOUND","Memory access file could not be opened."},
81     {"OCSD_ERR_MEM_ACC_FILE_DIFF_RANGE","Attempt to re-use the same memory access file for a different address range."},
82     {"OCSD_ERR_MEM_ACC_RANGE_INVALID","Address range in accessor set to invalid values."},
83     /* test errors - errors generated only by the test code, not the library */
84     {"OCSD_ERR_TEST_SNAPSHOT_PARSE", "Test snapshot file parse error"},
85     {"OCSD_ERR_TEST_SNAPSHOT_PARSE_INFO", "Test snapshot file parse information"},
86     {"OCSD_ERR_TEST_SNAPSHOT_READ","test snapshot reader error"},
87     {"OCSD_ERR_TEST_SS_TO_DECODER","test snapshot to decode tree conversion error"},
88     /* decoder registration */
89     {"OCSD_ERR_DCDREG_NAME_REPEAT","Attempted to register a decoder with the same name as another one."},
90     {"OCSD_ERR_DCDREG_NAME_UNKNOWN","Attempted to find a decoder with a name that is not known in the library."},
91     {"OCSD_ERR_DCDREG_TYPE_UNKNOWN","Attempted to find a decoder with a type that is not known in the library."},
92     /* decoder config */
93     {"OCSD_ERR_DCD_INTERFACE_UNUSED","Attempt to connect or use and inteface not supported by this decoder."},
94     /* end marker*/
95     {"OCSD_ERR_LAST", "No error - error code end marker"}
96 };
97 
98 ocsdError::ocsdError(const ocsd_err_severity_t sev_type, const ocsd_err_t code) :
99     m_error_code(code),
100     m_sev(sev_type),
101     m_idx(OCSD_BAD_TRC_INDEX),
102     m_chan_ID(OCSD_BAD_CS_SRC_ID)
103 {
104 }
105 
106 ocsdError::ocsdError(const ocsd_err_severity_t sev_type, const ocsd_err_t code, const ocsd_trc_index_t idx) :
107     m_error_code(code),
108     m_sev(sev_type),
109     m_idx(idx),
110     m_chan_ID(OCSD_BAD_CS_SRC_ID)
111 {
112 }
113 
114 ocsdError::ocsdError(const ocsd_err_severity_t sev_type, const ocsd_err_t code, const ocsd_trc_index_t idx, const uint8_t chan_id) :
115     m_error_code(code),
116     m_sev(sev_type),
117     m_idx(idx),
118     m_chan_ID(chan_id)
119 {
120 }
121 
122 ocsdError::ocsdError(const ocsd_err_severity_t sev_type, const ocsd_err_t code, const std::string &msg) :
123     m_error_code(code),
124     m_sev(sev_type),
125     m_idx(OCSD_BAD_TRC_INDEX),
126     m_chan_ID(OCSD_BAD_CS_SRC_ID),
127     m_err_message(msg)
128 {
129 }
130 
131 ocsdError::ocsdError(const ocsd_err_severity_t sev_type, const ocsd_err_t code, const ocsd_trc_index_t idx, const std::string &msg) :
132     m_error_code(code),
133     m_sev(sev_type),
134     m_idx(idx),
135     m_chan_ID(OCSD_BAD_CS_SRC_ID),
136     m_err_message(msg)
137 {
138 }
139 
140 ocsdError::ocsdError(const ocsd_err_severity_t sev_type, const ocsd_err_t code, const ocsd_trc_index_t idx, const uint8_t chan_id, const std::string &msg) :
141     m_error_code(code),
142     m_sev(sev_type),
143     m_idx(idx),
144     m_chan_ID(chan_id),
145     m_err_message(msg)
146 {
147 }
148 
149 
150 ocsdError::ocsdError(const ocsdError *pError) :
151     m_error_code(pError->getErrorCode()),
152     m_sev(pError->getErrorSeverity()),
153     m_idx(pError->getErrorIndex()),
154     m_chan_ID(pError->getErrorChanID())
155 {
156     setMessage(pError->getMessage());
157 }
158 
159 ocsdError::ocsdError(const ocsdError &Error) :
160     m_error_code(Error.getErrorCode()),
161     m_sev(Error.getErrorSeverity()),
162     m_idx(Error.getErrorIndex()),
163     m_chan_ID(Error.getErrorChanID())
164 {
165     setMessage(Error.getMessage());
166 }
167 
168 ocsdError::ocsdError():
169     m_error_code(OCSD_ERR_LAST),
170     m_sev(OCSD_ERR_SEV_NONE),
171     m_idx(OCSD_BAD_TRC_INDEX),
172     m_chan_ID(OCSD_BAD_CS_SRC_ID)
173 {
174 }
175 
176 ocsdError::~ocsdError()
177 {
178 }
179 
180 const std::string ocsdError::getErrorString(const ocsdError &error)
181 {
182     std::string szErrStr = "LIBRARY INTERNAL ERROR: Invalid Error Object";
183     const char *sev_type_sz[] = {
184         "NONE ",
185         "ERROR:",
186         "WARN :",
187         "INFO :"
188     };
189 
190     switch(error.getErrorSeverity())
191     {
192     default:
193     case OCSD_ERR_SEV_NONE:
194         break;
195 
196     case OCSD_ERR_SEV_ERROR:
197     case OCSD_ERR_SEV_WARN:
198     case OCSD_ERR_SEV_INFO:
199         szErrStr = sev_type_sz[(int)error.getErrorSeverity()];
200         appendErrorDetails(szErrStr,error);
201         break;
202     }
203     return szErrStr;
204 }
205 
206 void ocsdError::appendErrorDetails(std::string &errStr, const ocsdError &error)
207 {
208     int numerrstr = ((sizeof(s_errorCodeDescs) / sizeof(const char *)) / 2);
209     int code = (int)error.getErrorCode();
210     ocsd_trc_index_t idx = error.getErrorIndex();
211     uint8_t chan_ID = error.getErrorChanID();
212     std::ostringstream oss;
213 
214     oss << "0x" << std::hex << std::setfill('0') << std::setw(4) << code;
215     if(code < numerrstr)
216         oss << " (" << s_errorCodeDescs[code][0] << ") [" << s_errorCodeDescs[code][1] << "]; ";
217     else
218         oss << " (unknown); ";
219 
220     if(idx != OCSD_BAD_TRC_INDEX)
221         oss << "TrcIdx=" << std::dec << idx << "; ";
222 
223     if(chan_ID != OCSD_BAD_CS_SRC_ID)
224         oss << "CS ID=" << std::hex << std::setfill('0') << std::setw(2) << (uint16_t)chan_ID << "; ";
225 
226     oss << error.getMessage();
227     errStr = oss.str();
228 }
229 
230 /* End of File ocsd_error.cpp */
231