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