• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SYNC_MARKER","Synchronisation marker - marks position in stream of an element that is output later."},
59     {"OCSD_GEN_TRC_ELEM_MEMTRANS","Trace indication of transactional memory operations."},
60     {"OCSD_GEN_TRC_ELEM_CUSTOM","Fully custom packet type."}
61 };
62 
63 static const char *instr_type[] = {
64     "--- ",
65     "BR  ",
66     "iBR ",
67     "ISB ",
68     "DSB.DMB",
69     "WFI.WFE",
70     "TSTART"
71 };
72 
73 #define T_SIZE (sizeof(instr_type) / sizeof(const char *))
74 
75 static const char *instr_sub_type[] = {
76     "--- ",
77     "b+link ",
78     "A64:ret ",
79     "A64:eret ",
80     "V7:impl ret",
81 };
82 
83 #define ST_SIZE (sizeof(instr_sub_type) / sizeof(const char *))
84 
85 static const char *s_trace_on_reason[] = {
86     "begin or filter",
87     "overflow",
88     "debug restart"
89 };
90 
91 
92 static const char *s_isa_str[] = {
93    "A32",      /**< V7 ARM 32, V8 AArch32 */
94    "T32",          /**< Thumb2 -> 16/32 bit instructions */
95    "A64",      /**< V8 AArch64 */
96    "TEE",          /**< Thumb EE - unsupported */
97    "Jaz",      /**< Jazelle - unsupported in trace */
98    "Cst",      /**< ISA custom */
99    "Unk"       /**< ISA not yet known */
100 };
101 
102 static const char *s_unsync_reason[] = {
103     "undefined",            // UNSYNC_UNKNOWN - unknown /undefined
104     "init-decoder",         // UNSYNC_INIT_DECODER - decoder intialisation - start of trace.
105     "reset-decoder",        // UNSYNC_RESET_DECODER - decoder reset.
106     "overflow",             // UNSYNC_OVERFLOW - overflow packet - need to re-sync
107     "discard",              // UNSYNC_DISCARD - specl trace discard - need to re-sync
108     "bad-packet",           // UNSYNC_BAD_PACKET - bad packet at input - resync to restart.
109     "end-of-trace",         // UNSYNC_EOT - end of trace info.
110 };
111 static const char *s_transaction_type[] = {
112 	"Init",
113     "Start",
114     "Commit",
115     "Fail"
116 };
117 
118 static const char *s_marker_t[] = {
119   "Timestamp marker",  //  ELEM_MARKER_TS
120 };
121 
toString(std::string & str) const122 void OcsdTraceElement::toString(std::string &str) const
123 {
124     std::ostringstream oss;
125     int num_str = sizeof(s_elem_descs) / sizeof(s_elem_descs[0]);
126     int typeIdx = (int)this->elem_type;
127     if(typeIdx < num_str)
128     {
129         oss << s_elem_descs[typeIdx][0] << "(";
130         switch(elem_type)
131         {
132         case OCSD_GEN_TRC_ELEM_INSTR_RANGE:
133             oss << "exec range=0x" << std::hex << st_addr << ":[0x" << en_addr << "] ";
134             oss << "num_i(" << std::dec << num_instr_range << ") ";
135             oss << "last_sz(" << last_instr_sz << ") ";
136             oss << "(ISA=" << s_isa_str[(int)isa] << ") ";
137             oss << ((last_instr_exec == 1) ? "E " : "N ");
138             if((int)last_i_type < T_SIZE)
139                 oss << instr_type[last_i_type];
140             if((last_i_subtype != OCSD_S_INSTR_NONE) && ((int)last_i_subtype < ST_SIZE))
141                 oss << instr_sub_type[last_i_subtype];
142             if (last_instr_cond)
143                 oss << " <cond>";
144             break;
145 
146         case OCSD_GEN_TRC_ELEM_ADDR_NACC:
147             oss << " 0x" << std::hex << st_addr << " ";
148             break;
149 
150         case OCSD_GEN_TRC_ELEM_I_RANGE_NOPATH:
151             oss << "first 0x" << std::hex << st_addr << ":[next 0x" << en_addr << "] ";
152             oss << "num_i(" << std::dec << num_instr_range << ") ";
153             break;
154 
155         case OCSD_GEN_TRC_ELEM_EXCEPTION:
156             if (excep_ret_addr == 1)
157             {
158                 oss << "pref ret addr:0x" << std::hex << en_addr;
159                 if (excep_ret_addr_br_tgt)
160                 {
161                     oss << " [addr also prev br tgt]";
162                 }
163                 oss << "; ";
164             }
165             oss << "excep num (0x" << std::setfill('0') << std::setw(2) << std::hex << exception_number << ") ";
166             break;
167 
168         case OCSD_GEN_TRC_ELEM_PE_CONTEXT:
169             oss << "(ISA=" << s_isa_str[(int)isa] << ") ";
170             if((context.exception_level > ocsd_EL_unknown) && (context.el_valid))
171             {
172                 oss << "EL" << std::dec << (int)(context.exception_level);
173             }
174             oss << (context.security_level == ocsd_sec_secure ? "S; " : "N; ") << (context.bits64 ? "64-bit; " : "32-bit; ");
175             if(context.vmid_valid)
176                 oss << "VMID=0x" << std::hex << context.vmid << "; ";
177             if(context.ctxt_id_valid)
178                 oss << "CTXTID=0x" << std::hex << context.context_id << "; ";
179             break;
180 
181         case  OCSD_GEN_TRC_ELEM_TRACE_ON:
182             oss << " [" << s_trace_on_reason[trace_on_reason] << "]";
183             break;
184 
185         case OCSD_GEN_TRC_ELEM_TIMESTAMP:
186             oss << " [ TS=0x" << std::setfill('0') << std::setw(12) << std::hex << timestamp << "]; ";
187             break;
188 
189         case OCSD_GEN_TRC_ELEM_SWTRACE:
190             printSWInfoPkt(oss);
191             break;
192 
193         case OCSD_GEN_TRC_ELEM_EVENT:
194             if(trace_event.ev_type == EVENT_TRIGGER)
195                 oss << " Trigger; ";
196             else if(trace_event.ev_type == EVENT_NUMBERED)
197                 oss << " Numbered:" << std::dec << trace_event.ev_number << "; ";
198             break;
199 
200         case OCSD_GEN_TRC_ELEM_EO_TRACE:
201         case OCSD_GEN_TRC_ELEM_NO_SYNC:
202             if (unsync_eot_info <= UNSYNC_EOT)
203                 oss << " [" << s_unsync_reason[unsync_eot_info] << "]";
204             break;
205 
206         case OCSD_GEN_TRC_ELEM_SYNC_MARKER:
207             oss << " [" << s_marker_t[sync_marker.type] << "(0x" << std::setfill('0') << std::setw(8) << std::hex << sync_marker.value << ")]";
208             break;
209 
210         case OCSD_GEN_TRC_ELEM_MEMTRANS:
211             if (mem_trans <= OCSD_MEM_TRANS_FAIL)
212                 oss << s_transaction_type[mem_trans];
213             break;
214 
215         default: break;
216         }
217         if(has_cc)
218             oss << std::dec << " [CC=" << cycle_count << "]; ";
219         oss << ")";
220     }
221     else
222     {
223         oss << "OCSD_GEN_TRC_ELEM??: index out of range.";
224     }
225     str = oss.str();
226 }
227 
operator =(const ocsd_generic_trace_elem * p_elem)228 OcsdTraceElement &OcsdTraceElement::operator =(const ocsd_generic_trace_elem* p_elem)
229 {
230     *dynamic_cast<ocsd_generic_trace_elem*>(this) = *p_elem;
231     return *this;
232 }
233 
234 
printSWInfoPkt(std::ostringstream & oss) const235 void OcsdTraceElement::printSWInfoPkt(std::ostringstream & oss) const
236 {
237     if (!sw_trace_info.swt_global_err)
238     {
239         if (sw_trace_info.swt_id_valid)
240         {
241             oss << " (Ma:0x" << std::setfill('0') << std::setw(2) << std::hex << sw_trace_info.swt_master_id << "; ";
242             oss << "Ch:0x" << std::setfill('0') << std::setw(2) << std::hex << sw_trace_info.swt_channel_id << ") ";
243         }
244         else
245             oss << "(Ma:0x??; Ch:0x??" << ") ";
246 
247         if (sw_trace_info.swt_payload_pkt_bitsize > 0)
248         {
249             oss << "0x" << std::setfill('0') << std::hex;
250             if (sw_trace_info.swt_payload_pkt_bitsize == 4)
251             {
252                 oss << std::setw(1);
253                 oss << (uint16_t)(((uint8_t *)ptr_extended_data)[0] & 0xF);
254             }
255             else
256             {
257                 switch (sw_trace_info.swt_payload_pkt_bitsize)
258                 {
259                 case 8:
260                     // force uint8 to uint16 so oss 'sees' them as something to be stringised, rather than absolute char values
261                     oss << std::setw(2) << (uint16_t)((uint8_t *)ptr_extended_data)[0];
262                     break;
263                 case 16:
264                     oss << std::setw(4) << ((uint16_t *)ptr_extended_data)[0];
265                     break;
266                 case 32:
267                     oss << std::setw(8) << ((uint32_t *)ptr_extended_data)[0];
268                     break;
269                 case 64:
270                     oss << std::setw(16) << ((uint64_t *)ptr_extended_data)[0];
271                     break;
272                 default:
273                     oss << "{Data Error : unsupported bit width.}";
274                     break;
275                 }
276             }
277             oss << "; ";
278         }
279         if (sw_trace_info.swt_marker_packet)
280             oss << "+Mrk ";
281         if (sw_trace_info.swt_trigger_event)
282             oss << "Trig ";
283         if (sw_trace_info.swt_has_timestamp)
284             oss << " [ TS=0x" << std::setfill('0') << std::setw(12) << std::hex << timestamp << "]; ";
285         if (sw_trace_info.swt_frequency)
286             oss << "Freq";
287         if (sw_trace_info.swt_master_err)
288             oss << "{Master Error.}";
289     }
290     else
291     {
292         oss << "{Global Error.}";
293     }
294 }
295 
296 /*
297 void OcsdTraceElement::toString(const ocsd_generic_trace_elem *p_elem, std::string &str)
298 {
299     OcsdTraceElement elem;
300     elem = p_elem;
301     elem.toString(str);
302 }
303 */
304 /* End of File trc_gen_elem.cpp */
305