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