1 /*
2  * \file       trc_gen_elem.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 "common/trc_gen_elem.h"
36 
37 #include <string>
38 #include <sstream>
39 #include <iomanip>
40 
41 static const char *s_elem_descs[][2] =
42 {
43     {"OCSD_GEN_TRC_ELEM_UNKNOWN","Unknown trace element - default value or indicate error in stream to client."},
44     {"OCSD_GEN_TRC_ELEM_NO_SYNC","Waiting for sync - either at start of decode, or after overflow / bad packet"},
45     {"OCSD_GEN_TRC_ELEM_TRACE_ON","Start of trace - beginning of elements or restart after discontinuity (overflow, trace filtering)."},
46     {"OCSD_GEN_TRC_ELEM_EO_TRACE","End of the available trace in the buffer."},
47     {"OCSD_GEN_TRC_ELEM_PE_CONTEXT","PE status update / change (arch, ctxtid, vmid etc)."},
48     {"OCSD_GEN_TRC_ELEM_INSTR_RANGE","Traced N consecutive instructions from addr (no intervening events or data elements), may have data assoc key"},
49     {"OCSD_GEN_TRC_ELEM_I_RANGE_NOPATH","Traced N instructions in a range, but incomplete information as to program execution path from start to end of range"},
50     {"OCSD_GEN_TRC_ELEM_ADDR_NACC","Tracing in inaccessible memory area."},
51     {"OCSD_GEN_TRC_ELEM_ADDR_UNKNOWN","Tracing unknown address area."},
52     {"OCSD_GEN_TRC_ELEM_EXCEPTION","Exception"},
53     {"OCSD_GEN_TRC_ELEM_EXCEPTION_RET","Exception return"},
54     {"OCSD_GEN_TRC_ELEM_TIMESTAMP","Timestamp - preceding elements happeded before this time."},
55     {"OCSD_GEN_TRC_ELEM_CYCLE_COUNT","Cycle count - cycles since last cycle count value - associated with a preceding instruction range."},
56     {"OCSD_GEN_TRC_ELEM_EVENT","Event - numbered event or trigger"},
57     {"OCSD_GEN_TRC_ELEM_SWTRACE","Software trace packet - may contain data payload."},
58     {"OCSD_GEN_TRC_ELEM_CUSTOM","Fully custom packet type."}
59 };
60 
61 static const char *instr_type[] = {
62     "--- ",
63     "BR  ",
64     "iBR ",
65     "ISB ",
66     "DSB.DMB",
67     "WFI.WFE"
68 };
69 
70 #define T_SIZE (sizeof(instr_type) / sizeof(const char *))
71 
72 static const char *instr_sub_type[] = {
73     "--- ",
74     "b+link ",
75     "A64:ret ",
76     "A64:eret ",
77     "V7:impl ret",
78 };
79 
80 #define ST_SIZE (sizeof(instr_sub_type) / sizeof(const char *))
81 
82 static const char *s_trace_on_reason[] = {
83     "begin or filter",
84     "overflow",
85     "debug restart"
86 };
87 
88 
89 static const char *s_isa_str[] = {
90    "A32",      /**< V7 ARM 32, V8 AArch32 */
91    "T32",          /**< Thumb2 -> 16/32 bit instructions */
92    "A64",      /**< V8 AArch64 */
93    "TEE",          /**< Thumb EE - unsupported */
94    "Jaz",      /**< Jazelle - unsupported in trace */
95    "Cst",      /**< ISA custom */
96    "Unk"       /**< ISA not yet known */
97 };
98 
99 static const char *s_unsync_reason[] = {
100     "undefined",            // UNSYNC_UNKNOWN - unknown /undefined
101     "init-decoder",         // UNSYNC_INIT_DECODER - decoder intialisation - start of trace.
102     "reset-decoder",        // UNSYNC_RESET_DECODER - decoder reset.
103     "overflow",             // UNSYNC_OVERFLOW - overflow packet - need to re-sync
104     "discard",              // UNSYNC_DISCARD - specl trace discard - need to re-sync
105     "bad-packet",           // UNSYNC_BAD_PACKET - bad packet at input - resync to restart.
106     "end-of-trace",         // UNSYNC_EOT - end of trace info.
107 };
108 
109 void OcsdTraceElement::toString(std::string &str) const
110 {
111     std::ostringstream oss;
112     int num_str = sizeof(s_elem_descs) / sizeof(s_elem_descs[0]);
113     int typeIdx = (int)this->elem_type;
114     if(typeIdx < num_str)
115     {
116         oss << s_elem_descs[typeIdx][0] << "(";
117         switch(elem_type)
118         {
119         case OCSD_GEN_TRC_ELEM_INSTR_RANGE:
120             oss << "exec range=0x" << std::hex << st_addr << ":[0x" << en_addr << "] ";
121             oss << "num_i(" << std::dec << num_instr_range << ") ";
122             oss << "last_sz(" << last_instr_sz << ") ";
123             oss << "(ISA=" << s_isa_str[(int)isa] << ") ";
124             oss << ((last_instr_exec == 1) ? "E " : "N ");
125             if((int)last_i_type < T_SIZE)
126                 oss << instr_type[last_i_type];
127             if((last_i_subtype != OCSD_S_INSTR_NONE) && ((int)last_i_subtype < ST_SIZE))
128                 oss << instr_sub_type[last_i_subtype];
129             if (last_instr_cond)
130                 oss << " <cond>";
131             break;
132 
133         case OCSD_GEN_TRC_ELEM_ADDR_NACC:
134             oss << " 0x" << std::hex << st_addr << " ";
135             break;
136 
137         case OCSD_GEN_TRC_ELEM_I_RANGE_NOPATH:
138             oss << "first 0x" << std::hex << st_addr << ":[next 0x" << en_addr << "] ";
139             oss << "num_i(" << std::dec << num_instr_range << ") ";
140             break;
141 
142         case OCSD_GEN_TRC_ELEM_EXCEPTION:
143             if (excep_ret_addr == 1)
144             {
145                 oss << "pref ret addr:0x" << std::hex << en_addr;
146                 if (excep_ret_addr_br_tgt)
147                 {
148                     oss << " [addr also prev br tgt]";
149                 }
150                 oss << "; ";
151             }
152             oss << "excep num (0x" << std::setfill('0') << std::setw(2) << std::hex << exception_number << ") ";
153             break;
154 
155         case OCSD_GEN_TRC_ELEM_PE_CONTEXT:
156             oss << "(ISA=" << s_isa_str[(int)isa] << ") ";
157             if((context.exception_level > ocsd_EL_unknown) && (context.el_valid))
158             {
159                 oss << "EL" << std::dec << (int)(context.exception_level);
160             }
161             oss << (context.security_level == ocsd_sec_secure ? "S; " : "N; ") << (context.bits64 ? "64-bit; " : "32-bit; ");
162             if(context.vmid_valid)
163                 oss << "VMID=0x" << std::hex << context.vmid << "; ";
164             if(context.ctxt_id_valid)
165                 oss << "CTXTID=0x" << std::hex << context.context_id << "; ";
166             break;
167 
168         case  OCSD_GEN_TRC_ELEM_TRACE_ON:
169             oss << " [" << s_trace_on_reason[trace_on_reason] << "]";
170             break;
171 
172         case OCSD_GEN_TRC_ELEM_TIMESTAMP:
173             oss << " [ TS=0x" << std::setfill('0') << std::setw(12) << std::hex << timestamp << "]; ";
174             break;
175 
176         case OCSD_GEN_TRC_ELEM_SWTRACE:
177             printSWInfoPkt(oss);
178             break;
179 
180         case OCSD_GEN_TRC_ELEM_EVENT:
181             if(trace_event.ev_type == EVENT_TRIGGER)
182                 oss << " Trigger; ";
183             else if(trace_event.ev_type == EVENT_NUMBERED)
184                 oss << " Numbered:" << std::dec << trace_event.ev_number << "; ";
185             break;
186 
187         case OCSD_GEN_TRC_ELEM_EO_TRACE:
188         case OCSD_GEN_TRC_ELEM_NO_SYNC:
189             if (unsync_eot_info <= UNSYNC_EOT)
190                 oss << " [" << s_unsync_reason[unsync_eot_info] << "]";
191             break;
192 
193         default: break;
194         }
195         if(has_cc)
196             oss << std::dec << " [CC=" << cycle_count << "]; ";
197         oss << ")";
198     }
199     else
200     {
201         oss << "OCSD_GEN_TRC_ELEM??: index out of range.";
202     }
203     str = oss.str();
204 }
205 
206 OcsdTraceElement &OcsdTraceElement::operator =(const ocsd_generic_trace_elem* p_elem)
207 {
208     *dynamic_cast<ocsd_generic_trace_elem*>(this) = *p_elem;
209     return *this;
210 }
211 
212 
213 void OcsdTraceElement::printSWInfoPkt(std::ostringstream & oss) const
214 {
215     if (!sw_trace_info.swt_global_err)
216     {
217         if (sw_trace_info.swt_id_valid)
218         {
219             oss << " (Ma:0x" << std::setfill('0') << std::setw(2) << std::hex << sw_trace_info.swt_master_id << "; ";
220             oss << "Ch:0x" << std::setfill('0') << std::setw(2) << std::hex << sw_trace_info.swt_channel_id << ") ";
221         }
222         else
223             oss << "(Ma:0x??; Ch:0x??" << ") ";
224 
225         if (sw_trace_info.swt_payload_pkt_bitsize > 0)
226         {
227             oss << "0x" << std::setfill('0') << std::hex;
228             if (sw_trace_info.swt_payload_pkt_bitsize == 4)
229             {
230                 oss << std::setw(1);
231                 oss << (uint16_t)(((uint8_t *)ptr_extended_data)[0] & 0xF);
232             }
233             else
234             {
235                 switch (sw_trace_info.swt_payload_pkt_bitsize)
236                 {
237                 case 8:
238                     // force uint8 to uint16 so oss 'sees' them as something to be stringised, rather than absolute char values
239                     oss << std::setw(2) << (uint16_t)((uint8_t *)ptr_extended_data)[0];
240                     break;
241                 case 16:
242                     oss << std::setw(4) << ((uint16_t *)ptr_extended_data)[0];
243                     break;
244                 case 32:
245                     oss << std::setw(8) << ((uint32_t *)ptr_extended_data)[0];
246                     break;
247                 case 64:
248                     oss << std::setw(16) << ((uint64_t *)ptr_extended_data)[0];
249                     break;
250                 default:
251                     oss << "{Data Error : unsupported bit width.}";
252                     break;
253                 }
254             }
255             oss << "; ";
256         }
257         if (sw_trace_info.swt_marker_packet)
258             oss << "+Mrk ";
259         if (sw_trace_info.swt_trigger_event)
260             oss << "Trig ";
261         if (sw_trace_info.swt_has_timestamp)
262             oss << " [ TS=0x" << std::setfill('0') << std::setw(12) << std::hex << timestamp << "]; ";
263         if (sw_trace_info.swt_frequency)
264             oss << "Freq";
265         if (sw_trace_info.swt_master_err)
266             oss << "{Master Error.}";
267     }
268     else
269     {
270         oss << "{Global Error.}";
271     }
272 }
273 
274 /*
275 void OcsdTraceElement::toString(const ocsd_generic_trace_elem *p_elem, std::string &str)
276 {
277     OcsdTraceElement elem;
278     elem = p_elem;
279     elem.toString(str);
280 }
281 */
282 /* End of File trc_gen_elem.cpp */
283