1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright(C) 2015-2018 Linaro Limited.
5 * Author: Tor Jeremiassen <tor@ti.com>
6 * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
9 #include <linux/bitops.h>
10 #include <linux/err.h>
11 #include <linux/kernel.h>
12 #include <linux/log2.h>
13 #include <linux/types.h>
14 #include <linux/zalloc.h>
16 #include <opencsd/ocsd_if_types.h>
22 #include "cs-etm-decoder/cs-etm-decoder.h"
33 #include "thread_map.h"
34 #include "thread-stack.h"
35 #include <tools/libc_compat.h>
38 #define MAX_TIMESTAMP (~0ULL)
40 struct cs_etm_auxtrace {
41 struct auxtrace auxtrace;
42 struct auxtrace_queues queues;
43 struct auxtrace_heap heap;
44 struct itrace_synth_opts synth_opts;
45 struct perf_session *session;
46 struct machine *machine;
47 struct thread *unknown_thread;
53 u8 sample_instructions;
57 u64 branches_sample_type;
59 u64 instructions_sample_type;
60 u64 instructions_sample_period;
64 unsigned int pmu_type;
67 struct cs_etm_traceid_queue {
70 u64 period_instructions;
71 size_t last_branch_pos;
72 union perf_event *event_buf;
73 struct thread *thread;
74 struct branch_stack *last_branch;
75 struct branch_stack *last_branch_rb;
76 struct cs_etm_packet *prev_packet;
77 struct cs_etm_packet *packet;
78 struct cs_etm_packet_queue packet_queue;
82 struct cs_etm_auxtrace *etm;
83 struct cs_etm_decoder *decoder;
84 struct auxtrace_buffer *buffer;
85 unsigned int queue_nr;
88 const unsigned char *buf;
89 size_t buf_len, buf_used;
90 /* Conversion between traceID and index in traceid_queues array */
91 struct intlist *traceid_queues_list;
92 struct cs_etm_traceid_queue **traceid_queues;
95 static int cs_etm__update_queues(struct cs_etm_auxtrace *etm);
96 static int cs_etm__process_queues(struct cs_etm_auxtrace *etm);
97 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
99 static int cs_etm__get_data_block(struct cs_etm_queue *etmq);
100 static int cs_etm__decode_data_block(struct cs_etm_queue *etmq);
102 /* PTMs ETMIDR [11:8] set to b0011 */
103 #define ETMIDR_PTM_VERSION 0x00000300
106 * A struct auxtrace_heap_item only has a queue_nr and a timestamp to
107 * work with. One option is to modify to auxtrace_heap_XYZ() API or simply
108 * encode the etm queue number as the upper 16 bit and the channel as
111 #define TO_CS_QUEUE_NR(queue_nr, trace_id_chan) \
112 (queue_nr << 16 | trace_chan_id)
113 #define TO_QUEUE_NR(cs_queue_nr) (cs_queue_nr >> 16)
114 #define TO_TRACE_CHAN_ID(cs_queue_nr) (cs_queue_nr & 0x0000ffff)
116 static u32 cs_etm__get_v7_protocol_version(u32 etmidr)
118 etmidr &= ETMIDR_PTM_VERSION;
120 if (etmidr == ETMIDR_PTM_VERSION)
121 return CS_ETM_PROTO_PTM;
123 return CS_ETM_PROTO_ETMV3;
126 static int cs_etm__get_magic(u8 trace_chan_id, u64 *magic)
128 struct int_node *inode;
131 inode = intlist__find(traceid_list, trace_chan_id);
135 metadata = inode->priv;
136 *magic = metadata[CS_ETM_MAGIC];
140 int cs_etm__get_cpu(u8 trace_chan_id, int *cpu)
142 struct int_node *inode;
145 inode = intlist__find(traceid_list, trace_chan_id);
149 metadata = inode->priv;
150 *cpu = (int)metadata[CS_ETM_CPU];
154 void cs_etm__etmq_set_traceid_queue_timestamp(struct cs_etm_queue *etmq,
158 * Wnen a timestamp packet is encountered the backend code
159 * is stopped so that the front end has time to process packets
160 * that were accumulated in the traceID queue. Since there can
161 * be more than one channel per cs_etm_queue, we need to specify
162 * what traceID queue needs servicing.
164 etmq->pending_timestamp = trace_chan_id;
167 static u64 cs_etm__etmq_get_timestamp(struct cs_etm_queue *etmq,
170 struct cs_etm_packet_queue *packet_queue;
172 if (!etmq->pending_timestamp)
176 *trace_chan_id = etmq->pending_timestamp;
178 packet_queue = cs_etm__etmq_get_packet_queue(etmq,
179 etmq->pending_timestamp);
183 /* Acknowledge pending status */
184 etmq->pending_timestamp = 0;
186 /* See function cs_etm_decoder__do_{hard|soft}_timestamp() */
187 return packet_queue->timestamp;
190 static void cs_etm__clear_packet_queue(struct cs_etm_packet_queue *queue)
196 queue->packet_count = 0;
197 for (i = 0; i < CS_ETM_PACKET_MAX_BUFFER; i++) {
198 queue->packet_buffer[i].isa = CS_ETM_ISA_UNKNOWN;
199 queue->packet_buffer[i].start_addr = CS_ETM_INVAL_ADDR;
200 queue->packet_buffer[i].end_addr = CS_ETM_INVAL_ADDR;
201 queue->packet_buffer[i].instr_count = 0;
202 queue->packet_buffer[i].last_instr_taken_branch = false;
203 queue->packet_buffer[i].last_instr_size = 0;
204 queue->packet_buffer[i].last_instr_type = 0;
205 queue->packet_buffer[i].last_instr_subtype = 0;
206 queue->packet_buffer[i].last_instr_cond = 0;
207 queue->packet_buffer[i].flags = 0;
208 queue->packet_buffer[i].exception_number = UINT32_MAX;
209 queue->packet_buffer[i].trace_chan_id = UINT8_MAX;
210 queue->packet_buffer[i].cpu = INT_MIN;
214 static void cs_etm__clear_all_packet_queues(struct cs_etm_queue *etmq)
217 struct int_node *inode;
218 struct cs_etm_traceid_queue *tidq;
219 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
221 intlist__for_each_entry(inode, traceid_queues_list) {
222 idx = (int)(intptr_t)inode->priv;
223 tidq = etmq->traceid_queues[idx];
224 cs_etm__clear_packet_queue(&tidq->packet_queue);
228 static int cs_etm__init_traceid_queue(struct cs_etm_queue *etmq,
229 struct cs_etm_traceid_queue *tidq,
233 struct auxtrace_queue *queue;
234 struct cs_etm_auxtrace *etm = etmq->etm;
236 cs_etm__clear_packet_queue(&tidq->packet_queue);
238 queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
239 tidq->tid = queue->tid;
241 tidq->trace_chan_id = trace_chan_id;
243 tidq->packet = zalloc(sizeof(struct cs_etm_packet));
247 tidq->prev_packet = zalloc(sizeof(struct cs_etm_packet));
248 if (!tidq->prev_packet)
251 if (etm->synth_opts.last_branch) {
252 size_t sz = sizeof(struct branch_stack);
254 sz += etm->synth_opts.last_branch_sz *
255 sizeof(struct branch_entry);
256 tidq->last_branch = zalloc(sz);
257 if (!tidq->last_branch)
259 tidq->last_branch_rb = zalloc(sz);
260 if (!tidq->last_branch_rb)
264 tidq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
265 if (!tidq->event_buf)
271 zfree(&tidq->last_branch_rb);
272 zfree(&tidq->last_branch);
273 zfree(&tidq->prev_packet);
274 zfree(&tidq->packet);
279 static struct cs_etm_traceid_queue
280 *cs_etm__etmq_get_traceid_queue(struct cs_etm_queue *etmq, u8 trace_chan_id)
283 struct int_node *inode;
284 struct intlist *traceid_queues_list;
285 struct cs_etm_traceid_queue *tidq, **traceid_queues;
286 struct cs_etm_auxtrace *etm = etmq->etm;
288 if (etm->timeless_decoding)
289 trace_chan_id = CS_ETM_PER_THREAD_TRACEID;
291 traceid_queues_list = etmq->traceid_queues_list;
294 * Check if the traceid_queue exist for this traceID by looking
297 inode = intlist__find(traceid_queues_list, trace_chan_id);
299 idx = (int)(intptr_t)inode->priv;
300 return etmq->traceid_queues[idx];
303 /* We couldn't find a traceid_queue for this traceID, allocate one */
304 tidq = malloc(sizeof(*tidq));
308 memset(tidq, 0, sizeof(*tidq));
310 /* Get a valid index for the new traceid_queue */
311 idx = intlist__nr_entries(traceid_queues_list);
312 /* Memory for the inode is free'ed in cs_etm_free_traceid_queues () */
313 inode = intlist__findnew(traceid_queues_list, trace_chan_id);
317 /* Associate this traceID with this index */
318 inode->priv = (void *)(intptr_t)idx;
320 if (cs_etm__init_traceid_queue(etmq, tidq, trace_chan_id))
323 /* Grow the traceid_queues array by one unit */
324 traceid_queues = etmq->traceid_queues;
325 traceid_queues = reallocarray(traceid_queues,
327 sizeof(*traceid_queues));
330 * On failure reallocarray() returns NULL and the original block of
331 * memory is left untouched.
336 traceid_queues[idx] = tidq;
337 etmq->traceid_queues = traceid_queues;
339 return etmq->traceid_queues[idx];
343 * Function intlist__remove() removes the inode from the list
344 * and delete the memory associated to it.
346 intlist__remove(traceid_queues_list, inode);
352 struct cs_etm_packet_queue
353 *cs_etm__etmq_get_packet_queue(struct cs_etm_queue *etmq, u8 trace_chan_id)
355 struct cs_etm_traceid_queue *tidq;
357 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
359 return &tidq->packet_queue;
364 static void cs_etm__packet_dump(const char *pkt_string)
366 const char *color = PERF_COLOR_BLUE;
367 int len = strlen(pkt_string);
369 if (len && (pkt_string[len-1] == '\n'))
370 color_fprintf(stdout, color, " %s", pkt_string);
372 color_fprintf(stdout, color, " %s\n", pkt_string);
377 static void cs_etm__set_trace_param_etmv3(struct cs_etm_trace_params *t_params,
378 struct cs_etm_auxtrace *etm, int idx,
381 u64 **metadata = etm->metadata;
383 t_params[idx].protocol = cs_etm__get_v7_protocol_version(etmidr);
384 t_params[idx].etmv3.reg_ctrl = metadata[idx][CS_ETM_ETMCR];
385 t_params[idx].etmv3.reg_trc_id = metadata[idx][CS_ETM_ETMTRACEIDR];
388 static void cs_etm__set_trace_param_etmv4(struct cs_etm_trace_params *t_params,
389 struct cs_etm_auxtrace *etm, int idx)
391 u64 **metadata = etm->metadata;
393 t_params[idx].protocol = CS_ETM_PROTO_ETMV4i;
394 t_params[idx].etmv4.reg_idr0 = metadata[idx][CS_ETMV4_TRCIDR0];
395 t_params[idx].etmv4.reg_idr1 = metadata[idx][CS_ETMV4_TRCIDR1];
396 t_params[idx].etmv4.reg_idr2 = metadata[idx][CS_ETMV4_TRCIDR2];
397 t_params[idx].etmv4.reg_idr8 = metadata[idx][CS_ETMV4_TRCIDR8];
398 t_params[idx].etmv4.reg_configr = metadata[idx][CS_ETMV4_TRCCONFIGR];
399 t_params[idx].etmv4.reg_traceidr = metadata[idx][CS_ETMV4_TRCTRACEIDR];
402 static int cs_etm__init_trace_params(struct cs_etm_trace_params *t_params,
403 struct cs_etm_auxtrace *etm)
409 for (i = 0; i < etm->num_cpu; i++) {
410 architecture = etm->metadata[i][CS_ETM_MAGIC];
412 switch (architecture) {
413 case __perf_cs_etmv3_magic:
414 etmidr = etm->metadata[i][CS_ETM_ETMIDR];
415 cs_etm__set_trace_param_etmv3(t_params, etm, i, etmidr);
417 case __perf_cs_etmv4_magic:
418 cs_etm__set_trace_param_etmv4(t_params, etm, i);
428 static int cs_etm__init_decoder_params(struct cs_etm_decoder_params *d_params,
429 struct cs_etm_queue *etmq,
430 enum cs_etm_decoder_operation mode)
434 if (!(mode < CS_ETM_OPERATION_MAX))
437 d_params->packet_printer = cs_etm__packet_dump;
438 d_params->operation = mode;
439 d_params->data = etmq;
440 d_params->formatted = true;
441 d_params->fsyncs = false;
442 d_params->hsyncs = false;
443 d_params->frame_aligned = true;
450 static void cs_etm__dump_event(struct cs_etm_auxtrace *etm,
451 struct auxtrace_buffer *buffer)
454 const char *color = PERF_COLOR_BLUE;
455 struct cs_etm_decoder_params d_params;
456 struct cs_etm_trace_params *t_params;
457 struct cs_etm_decoder *decoder;
458 size_t buffer_used = 0;
460 fprintf(stdout, "\n");
461 color_fprintf(stdout, color,
462 ". ... CoreSight ETM Trace data: size %zu bytes\n",
465 /* Use metadata to fill in trace parameters for trace decoder */
466 t_params = zalloc(sizeof(*t_params) * etm->num_cpu);
471 if (cs_etm__init_trace_params(t_params, etm))
474 /* Set decoder parameters to simply print the trace packets */
475 if (cs_etm__init_decoder_params(&d_params, NULL,
476 CS_ETM_OPERATION_PRINT))
479 decoder = cs_etm_decoder__new(etm->num_cpu, &d_params, t_params);
486 ret = cs_etm_decoder__process_data_block(
487 decoder, buffer->offset,
488 &((u8 *)buffer->data)[buffer_used],
489 buffer->size - buffer_used, &consumed);
493 buffer_used += consumed;
494 } while (buffer_used < buffer->size);
496 cs_etm_decoder__free(decoder);
502 static int cs_etm__flush_events(struct perf_session *session,
503 struct perf_tool *tool)
506 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
507 struct cs_etm_auxtrace,
512 if (!tool->ordered_events)
515 ret = cs_etm__update_queues(etm);
520 if (etm->timeless_decoding)
521 return cs_etm__process_timeless_queues(etm, -1);
523 return cs_etm__process_queues(etm);
526 static void cs_etm__free_traceid_queues(struct cs_etm_queue *etmq)
530 struct int_node *inode, *tmp;
531 struct cs_etm_traceid_queue *tidq;
532 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
534 intlist__for_each_entry_safe(inode, tmp, traceid_queues_list) {
535 priv = (uintptr_t)inode->priv;
538 /* Free this traceid_queue from the array */
539 tidq = etmq->traceid_queues[idx];
540 thread__zput(tidq->thread);
541 zfree(&tidq->event_buf);
542 zfree(&tidq->last_branch);
543 zfree(&tidq->last_branch_rb);
544 zfree(&tidq->prev_packet);
545 zfree(&tidq->packet);
549 * Function intlist__remove() removes the inode from the list
550 * and delete the memory associated to it.
552 intlist__remove(traceid_queues_list, inode);
555 /* Then the RB tree itself */
556 intlist__delete(traceid_queues_list);
557 etmq->traceid_queues_list = NULL;
559 /* finally free the traceid_queues array */
560 zfree(&etmq->traceid_queues);
563 static void cs_etm__free_queue(void *priv)
565 struct cs_etm_queue *etmq = priv;
570 cs_etm_decoder__free(etmq->decoder);
571 cs_etm__free_traceid_queues(etmq);
575 static void cs_etm__free_events(struct perf_session *session)
578 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
579 struct cs_etm_auxtrace,
581 struct auxtrace_queues *queues = &aux->queues;
583 for (i = 0; i < queues->nr_queues; i++) {
584 cs_etm__free_queue(queues->queue_array[i].priv);
585 queues->queue_array[i].priv = NULL;
588 auxtrace_queues__free(queues);
591 static void cs_etm__free(struct perf_session *session)
594 struct int_node *inode, *tmp;
595 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
596 struct cs_etm_auxtrace,
598 cs_etm__free_events(session);
599 session->auxtrace = NULL;
601 /* First remove all traceID/metadata nodes for the RB tree */
602 intlist__for_each_entry_safe(inode, tmp, traceid_list)
603 intlist__remove(traceid_list, inode);
604 /* Then the RB tree itself */
605 intlist__delete(traceid_list);
607 for (i = 0; i < aux->num_cpu; i++)
608 zfree(&aux->metadata[i]);
610 thread__zput(aux->unknown_thread);
611 zfree(&aux->metadata);
615 static u8 cs_etm__cpu_mode(struct cs_etm_queue *etmq, u64 address)
617 struct machine *machine;
619 machine = etmq->etm->machine;
621 if (address >= etmq->etm->kernel_start) {
622 if (machine__is_host(machine))
623 return PERF_RECORD_MISC_KERNEL;
625 return PERF_RECORD_MISC_GUEST_KERNEL;
627 if (machine__is_host(machine))
628 return PERF_RECORD_MISC_USER;
630 return PERF_RECORD_MISC_GUEST_USER;
632 return PERF_RECORD_MISC_HYPERVISOR;
636 static u32 cs_etm__mem_access(struct cs_etm_queue *etmq, u8 trace_chan_id,
637 u64 address, size_t size, u8 *buffer)
642 struct thread *thread;
643 struct machine *machine;
644 struct addr_location al;
645 struct cs_etm_traceid_queue *tidq;
650 machine = etmq->etm->machine;
651 cpumode = cs_etm__cpu_mode(etmq, address);
652 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
656 thread = tidq->thread;
658 if (cpumode != PERF_RECORD_MISC_KERNEL)
660 thread = etmq->etm->unknown_thread;
663 if (!thread__find_map(thread, cpumode, address, &al) || !al.map->dso)
666 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
667 dso__data_status_seen(al.map->dso, DSO_DATA_STATUS_SEEN_ITRACE))
670 offset = al.map->map_ip(al.map, address);
674 len = dso__data_read_offset(al.map->dso, machine, offset, buffer, size);
682 static struct cs_etm_queue *cs_etm__alloc_queue(struct cs_etm_auxtrace *etm)
684 struct cs_etm_decoder_params d_params;
685 struct cs_etm_trace_params *t_params = NULL;
686 struct cs_etm_queue *etmq;
688 etmq = zalloc(sizeof(*etmq));
692 etmq->traceid_queues_list = intlist__new(NULL);
693 if (!etmq->traceid_queues_list)
696 /* Use metadata to fill in trace parameters for trace decoder */
697 t_params = zalloc(sizeof(*t_params) * etm->num_cpu);
702 if (cs_etm__init_trace_params(t_params, etm))
705 /* Set decoder parameters to decode trace packets */
706 if (cs_etm__init_decoder_params(&d_params, etmq,
707 CS_ETM_OPERATION_DECODE))
710 etmq->decoder = cs_etm_decoder__new(etm->num_cpu, &d_params, t_params);
716 * Register a function to handle all memory accesses required by
717 * the trace decoder library.
719 if (cs_etm_decoder__add_mem_access_cb(etmq->decoder,
722 goto out_free_decoder;
728 cs_etm_decoder__free(etmq->decoder);
730 intlist__delete(etmq->traceid_queues_list);
736 static int cs_etm__setup_queue(struct cs_etm_auxtrace *etm,
737 struct auxtrace_queue *queue,
738 unsigned int queue_nr)
741 unsigned int cs_queue_nr;
744 struct cs_etm_queue *etmq = queue->priv;
746 if (list_empty(&queue->head) || etmq)
749 etmq = cs_etm__alloc_queue(etm);
758 etmq->queue_nr = queue_nr;
761 if (etm->timeless_decoding)
765 * We are under a CPU-wide trace scenario. As such we need to know
766 * when the code that generated the traces started to execute so that
767 * it can be correlated with execution on other CPUs. So we get a
768 * handle on the beginning of traces and decode until we find a
769 * timestamp. The timestamp is then added to the auxtrace min heap
770 * in order to know what nibble (of all the etmqs) to decode first.
774 * Fetch an aux_buffer from this etmq. Bail if no more
775 * blocks or an error has been encountered.
777 ret = cs_etm__get_data_block(etmq);
782 * Run decoder on the trace block. The decoder will stop when
783 * encountering a timestamp, a full packet queue or the end of
784 * trace for that block.
786 ret = cs_etm__decode_data_block(etmq);
791 * Function cs_etm_decoder__do_{hard|soft}_timestamp() does all
792 * the timestamp calculation for us.
794 timestamp = cs_etm__etmq_get_timestamp(etmq, &trace_chan_id);
796 /* We found a timestamp, no need to continue. */
801 * We didn't find a timestamp so empty all the traceid packet
802 * queues before looking for another timestamp packet, either
803 * in the current data block or a new one. Packets that were
804 * just decoded are useless since no timestamp has been
805 * associated with them. As such simply discard them.
807 cs_etm__clear_all_packet_queues(etmq);
811 * We have a timestamp. Add it to the min heap to reflect when
812 * instructions conveyed by the range packets of this traceID queue
813 * started to execute. Once the same has been done for all the traceID
814 * queues of each etmq, redenring and decoding can start in
815 * chronological order.
817 * Note that packets decoded above are still in the traceID's packet
818 * queue and will be processed in cs_etm__process_queues().
820 cs_queue_nr = TO_CS_QUEUE_NR(queue_nr, trace_id_chan);
821 ret = auxtrace_heap__add(&etm->heap, cs_queue_nr, timestamp);
826 static int cs_etm__setup_queues(struct cs_etm_auxtrace *etm)
831 if (!etm->kernel_start)
832 etm->kernel_start = machine__kernel_start(etm->machine);
834 for (i = 0; i < etm->queues.nr_queues; i++) {
835 ret = cs_etm__setup_queue(etm, &etm->queues.queue_array[i], i);
843 static int cs_etm__update_queues(struct cs_etm_auxtrace *etm)
845 if (etm->queues.new_data) {
846 etm->queues.new_data = false;
847 return cs_etm__setup_queues(etm);
854 void cs_etm__copy_last_branch_rb(struct cs_etm_queue *etmq,
855 struct cs_etm_traceid_queue *tidq)
857 struct branch_stack *bs_src = tidq->last_branch_rb;
858 struct branch_stack *bs_dst = tidq->last_branch;
862 * Set the number of records before early exit: ->nr is used to
863 * determine how many branches to copy from ->entries.
865 bs_dst->nr = bs_src->nr;
868 * Early exit when there is nothing to copy.
874 * As bs_src->entries is a circular buffer, we need to copy from it in
875 * two steps. First, copy the branches from the most recently inserted
876 * branch ->last_branch_pos until the end of bs_src->entries buffer.
878 nr = etmq->etm->synth_opts.last_branch_sz - tidq->last_branch_pos;
879 memcpy(&bs_dst->entries[0],
880 &bs_src->entries[tidq->last_branch_pos],
881 sizeof(struct branch_entry) * nr);
884 * If we wrapped around at least once, the branches from the beginning
885 * of the bs_src->entries buffer and until the ->last_branch_pos element
886 * are older valid branches: copy them over. The total number of
887 * branches copied over will be equal to the number of branches asked by
888 * the user in last_branch_sz.
890 if (bs_src->nr >= etmq->etm->synth_opts.last_branch_sz) {
891 memcpy(&bs_dst->entries[nr],
893 sizeof(struct branch_entry) * tidq->last_branch_pos);
898 void cs_etm__reset_last_branch_rb(struct cs_etm_traceid_queue *tidq)
900 tidq->last_branch_pos = 0;
901 tidq->last_branch_rb->nr = 0;
904 static inline int cs_etm__t32_instr_size(struct cs_etm_queue *etmq,
905 u8 trace_chan_id, u64 addr)
909 cs_etm__mem_access(etmq, trace_chan_id, addr,
910 ARRAY_SIZE(instrBytes), instrBytes);
912 * T32 instruction size is indicated by bits[15:11] of the first
913 * 16-bit word of the instruction: 0b11101, 0b11110 and 0b11111
914 * denote a 32-bit instruction.
916 return ((instrBytes[1] & 0xF8) >= 0xE8) ? 4 : 2;
919 static inline u64 cs_etm__first_executed_instr(struct cs_etm_packet *packet)
921 /* Returns 0 for the CS_ETM_DISCONTINUITY packet */
922 if (packet->sample_type == CS_ETM_DISCONTINUITY)
925 return packet->start_addr;
929 u64 cs_etm__last_executed_instr(const struct cs_etm_packet *packet)
931 /* Returns 0 for the CS_ETM_DISCONTINUITY packet */
932 if (packet->sample_type == CS_ETM_DISCONTINUITY)
935 return packet->end_addr - packet->last_instr_size;
938 static inline u64 cs_etm__instr_addr(struct cs_etm_queue *etmq,
940 const struct cs_etm_packet *packet,
943 if (packet->isa == CS_ETM_ISA_T32) {
944 u64 addr = packet->start_addr;
947 addr += cs_etm__t32_instr_size(etmq,
948 trace_chan_id, addr);
954 /* Assume a 4 byte instruction size (A32/A64) */
955 return packet->start_addr + offset * 4;
958 static void cs_etm__update_last_branch_rb(struct cs_etm_queue *etmq,
959 struct cs_etm_traceid_queue *tidq)
961 struct branch_stack *bs = tidq->last_branch_rb;
962 struct branch_entry *be;
965 * The branches are recorded in a circular buffer in reverse
966 * chronological order: we start recording from the last element of the
967 * buffer down. After writing the first element of the stack, move the
968 * insert position back to the end of the buffer.
970 if (!tidq->last_branch_pos)
971 tidq->last_branch_pos = etmq->etm->synth_opts.last_branch_sz;
973 tidq->last_branch_pos -= 1;
975 be = &bs->entries[tidq->last_branch_pos];
976 be->from = cs_etm__last_executed_instr(tidq->prev_packet);
977 be->to = cs_etm__first_executed_instr(tidq->packet);
978 /* No support for mispredict */
979 be->flags.mispred = 0;
980 be->flags.predicted = 1;
983 * Increment bs->nr until reaching the number of last branches asked by
984 * the user on the command line.
986 if (bs->nr < etmq->etm->synth_opts.last_branch_sz)
990 static int cs_etm__inject_event(union perf_event *event,
991 struct perf_sample *sample, u64 type)
993 event->header.size = perf_event__sample_event_size(sample, type, 0);
994 return perf_event__synthesize_sample(event, type, 0, sample);
999 cs_etm__get_trace(struct cs_etm_queue *etmq)
1001 struct auxtrace_buffer *aux_buffer = etmq->buffer;
1002 struct auxtrace_buffer *old_buffer = aux_buffer;
1003 struct auxtrace_queue *queue;
1005 queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
1007 aux_buffer = auxtrace_buffer__next(queue, aux_buffer);
1009 /* If no more data, drop the previous auxtrace_buffer and return */
1012 auxtrace_buffer__drop_data(old_buffer);
1017 etmq->buffer = aux_buffer;
1019 /* If the aux_buffer doesn't have data associated, try to load it */
1020 if (!aux_buffer->data) {
1021 /* get the file desc associated with the perf data file */
1022 int fd = perf_data__fd(etmq->etm->session->data);
1024 aux_buffer->data = auxtrace_buffer__get_data(aux_buffer, fd);
1025 if (!aux_buffer->data)
1029 /* If valid, drop the previous buffer */
1031 auxtrace_buffer__drop_data(old_buffer);
1034 etmq->buf_len = aux_buffer->size;
1035 etmq->buf = aux_buffer->data;
1037 return etmq->buf_len;
1040 static void cs_etm__set_pid_tid_cpu(struct cs_etm_auxtrace *etm,
1041 struct cs_etm_traceid_queue *tidq)
1043 if ((!tidq->thread) && (tidq->tid != -1))
1044 tidq->thread = machine__find_thread(etm->machine, -1,
1048 tidq->pid = tidq->thread->pid_;
1051 int cs_etm__etmq_set_tid(struct cs_etm_queue *etmq,
1052 pid_t tid, u8 trace_chan_id)
1054 int cpu, err = -EINVAL;
1055 struct cs_etm_auxtrace *etm = etmq->etm;
1056 struct cs_etm_traceid_queue *tidq;
1058 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
1062 if (cs_etm__get_cpu(trace_chan_id, &cpu) < 0)
1065 err = machine__set_current_tid(etm->machine, cpu, tid, tid);
1070 thread__zput(tidq->thread);
1072 cs_etm__set_pid_tid_cpu(etm, tidq);
1076 bool cs_etm__etmq_is_timeless(struct cs_etm_queue *etmq)
1078 return !!etmq->etm->timeless_decoding;
1081 static void cs_etm__copy_insn(struct cs_etm_queue *etmq,
1083 const struct cs_etm_packet *packet,
1084 struct perf_sample *sample)
1087 * It's pointless to read instructions for the CS_ETM_DISCONTINUITY
1088 * packet, so directly bail out with 'insn_len' = 0.
1090 if (packet->sample_type == CS_ETM_DISCONTINUITY) {
1091 sample->insn_len = 0;
1096 * T32 instruction size might be 32-bit or 16-bit, decide by calling
1097 * cs_etm__t32_instr_size().
1099 if (packet->isa == CS_ETM_ISA_T32)
1100 sample->insn_len = cs_etm__t32_instr_size(etmq, trace_chan_id,
1102 /* Otherwise, A64 and A32 instruction size are always 32-bit. */
1104 sample->insn_len = 4;
1106 cs_etm__mem_access(etmq, trace_chan_id, sample->ip,
1107 sample->insn_len, (void *)sample->insn);
1110 static int cs_etm__synth_instruction_sample(struct cs_etm_queue *etmq,
1111 struct cs_etm_traceid_queue *tidq,
1112 u64 addr, u64 period)
1115 struct cs_etm_auxtrace *etm = etmq->etm;
1116 union perf_event *event = tidq->event_buf;
1117 struct perf_sample sample = {.ip = 0,};
1119 event->sample.header.type = PERF_RECORD_SAMPLE;
1120 event->sample.header.misc = cs_etm__cpu_mode(etmq, addr);
1121 event->sample.header.size = sizeof(struct perf_event_header);
1124 sample.pid = tidq->pid;
1125 sample.tid = tidq->tid;
1126 sample.id = etmq->etm->instructions_id;
1127 sample.stream_id = etmq->etm->instructions_id;
1128 sample.period = period;
1129 sample.cpu = tidq->packet->cpu;
1130 sample.flags = tidq->prev_packet->flags;
1131 sample.cpumode = event->sample.header.misc;
1133 cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->packet, &sample);
1135 if (etm->synth_opts.last_branch) {
1136 cs_etm__copy_last_branch_rb(etmq, tidq);
1137 sample.branch_stack = tidq->last_branch;
1140 if (etm->synth_opts.inject) {
1141 ret = cs_etm__inject_event(event, &sample,
1142 etm->instructions_sample_type);
1147 ret = perf_session__deliver_synth_event(etm->session, event, &sample);
1151 "CS ETM Trace: failed to deliver instruction event, error %d\n",
1154 if (etm->synth_opts.last_branch)
1155 cs_etm__reset_last_branch_rb(tidq);
1161 * The cs etm packet encodes an instruction range between a branch target
1162 * and the next taken branch. Generate sample accordingly.
1164 static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq,
1165 struct cs_etm_traceid_queue *tidq)
1168 struct cs_etm_auxtrace *etm = etmq->etm;
1169 struct perf_sample sample = {.ip = 0,};
1170 union perf_event *event = tidq->event_buf;
1171 struct dummy_branch_stack {
1173 struct branch_entry entries;
1177 ip = cs_etm__last_executed_instr(tidq->prev_packet);
1179 event->sample.header.type = PERF_RECORD_SAMPLE;
1180 event->sample.header.misc = cs_etm__cpu_mode(etmq, ip);
1181 event->sample.header.size = sizeof(struct perf_event_header);
1184 sample.pid = tidq->pid;
1185 sample.tid = tidq->tid;
1186 sample.addr = cs_etm__first_executed_instr(tidq->packet);
1187 sample.id = etmq->etm->branches_id;
1188 sample.stream_id = etmq->etm->branches_id;
1190 sample.cpu = tidq->packet->cpu;
1191 sample.flags = tidq->prev_packet->flags;
1192 sample.cpumode = event->sample.header.misc;
1194 cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->prev_packet,
1198 * perf report cannot handle events without a branch stack
1200 if (etm->synth_opts.last_branch) {
1201 dummy_bs = (struct dummy_branch_stack){
1208 sample.branch_stack = (struct branch_stack *)&dummy_bs;
1211 if (etm->synth_opts.inject) {
1212 ret = cs_etm__inject_event(event, &sample,
1213 etm->branches_sample_type);
1218 ret = perf_session__deliver_synth_event(etm->session, event, &sample);
1222 "CS ETM Trace: failed to deliver instruction event, error %d\n",
1228 struct cs_etm_synth {
1229 struct perf_tool dummy_tool;
1230 struct perf_session *session;
1233 static int cs_etm__event_synth(struct perf_tool *tool,
1234 union perf_event *event,
1235 struct perf_sample *sample __maybe_unused,
1236 struct machine *machine __maybe_unused)
1238 struct cs_etm_synth *cs_etm_synth =
1239 container_of(tool, struct cs_etm_synth, dummy_tool);
1241 return perf_session__deliver_synth_event(cs_etm_synth->session,
1245 static int cs_etm__synth_event(struct perf_session *session,
1246 struct perf_event_attr *attr, u64 id)
1248 struct cs_etm_synth cs_etm_synth;
1250 memset(&cs_etm_synth, 0, sizeof(struct cs_etm_synth));
1251 cs_etm_synth.session = session;
1253 return perf_event__synthesize_attr(&cs_etm_synth.dummy_tool, attr, 1,
1254 &id, cs_etm__event_synth);
1257 static int cs_etm__synth_events(struct cs_etm_auxtrace *etm,
1258 struct perf_session *session)
1260 struct evlist *evlist = session->evlist;
1261 struct evsel *evsel;
1262 struct perf_event_attr attr;
1267 evlist__for_each_entry(evlist, evsel) {
1268 if (evsel->core.attr.type == etm->pmu_type) {
1275 pr_debug("No selected events with CoreSight Trace data\n");
1279 memset(&attr, 0, sizeof(struct perf_event_attr));
1280 attr.size = sizeof(struct perf_event_attr);
1281 attr.type = PERF_TYPE_HARDWARE;
1282 attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
1283 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
1285 if (etm->timeless_decoding)
1286 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
1288 attr.sample_type |= PERF_SAMPLE_TIME;
1290 attr.exclude_user = evsel->core.attr.exclude_user;
1291 attr.exclude_kernel = evsel->core.attr.exclude_kernel;
1292 attr.exclude_hv = evsel->core.attr.exclude_hv;
1293 attr.exclude_host = evsel->core.attr.exclude_host;
1294 attr.exclude_guest = evsel->core.attr.exclude_guest;
1295 attr.sample_id_all = evsel->core.attr.sample_id_all;
1296 attr.read_format = evsel->core.attr.read_format;
1298 /* create new id val to be a fixed offset from evsel id */
1299 id = evsel->id[0] + 1000000000;
1304 if (etm->synth_opts.branches) {
1305 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
1306 attr.sample_period = 1;
1307 attr.sample_type |= PERF_SAMPLE_ADDR;
1308 err = cs_etm__synth_event(session, &attr, id);
1311 etm->sample_branches = true;
1312 etm->branches_sample_type = attr.sample_type;
1313 etm->branches_id = id;
1315 attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR;
1318 if (etm->synth_opts.last_branch)
1319 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
1321 if (etm->synth_opts.instructions) {
1322 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
1323 attr.sample_period = etm->synth_opts.period;
1324 etm->instructions_sample_period = attr.sample_period;
1325 err = cs_etm__synth_event(session, &attr, id);
1328 etm->sample_instructions = true;
1329 etm->instructions_sample_type = attr.sample_type;
1330 etm->instructions_id = id;
1337 static int cs_etm__sample(struct cs_etm_queue *etmq,
1338 struct cs_etm_traceid_queue *tidq)
1340 struct cs_etm_auxtrace *etm = etmq->etm;
1341 struct cs_etm_packet *tmp;
1343 u8 trace_chan_id = tidq->trace_chan_id;
1344 u64 instrs_executed = tidq->packet->instr_count;
1346 tidq->period_instructions += instrs_executed;
1349 * Record a branch when the last instruction in
1350 * PREV_PACKET is a branch.
1352 if (etm->synth_opts.last_branch &&
1353 tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1354 tidq->prev_packet->last_instr_taken_branch)
1355 cs_etm__update_last_branch_rb(etmq, tidq);
1357 if (etm->sample_instructions &&
1358 tidq->period_instructions >= etm->instructions_sample_period) {
1360 * Emit instruction sample periodically
1361 * TODO: allow period to be defined in cycles and clock time
1364 /* Get number of instructions executed after the sample point */
1365 u64 instrs_over = tidq->period_instructions -
1366 etm->instructions_sample_period;
1369 * Calculate the address of the sampled instruction (-1 as
1370 * sample is reported as though instruction has just been
1371 * executed, but PC has not advanced to next instruction)
1373 u64 offset = (instrs_executed - instrs_over - 1);
1374 u64 addr = cs_etm__instr_addr(etmq, trace_chan_id,
1375 tidq->packet, offset);
1377 ret = cs_etm__synth_instruction_sample(
1378 etmq, tidq, addr, etm->instructions_sample_period);
1382 /* Carry remaining instructions into next sample period */
1383 tidq->period_instructions = instrs_over;
1386 if (etm->sample_branches) {
1387 bool generate_sample = false;
1389 /* Generate sample for tracing on packet */
1390 if (tidq->prev_packet->sample_type == CS_ETM_DISCONTINUITY)
1391 generate_sample = true;
1393 /* Generate sample for branch taken packet */
1394 if (tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1395 tidq->prev_packet->last_instr_taken_branch)
1396 generate_sample = true;
1398 if (generate_sample) {
1399 ret = cs_etm__synth_branch_sample(etmq, tidq);
1405 if (etm->sample_branches || etm->synth_opts.last_branch) {
1407 * Swap PACKET with PREV_PACKET: PACKET becomes PREV_PACKET for
1408 * the next incoming packet.
1411 tidq->packet = tidq->prev_packet;
1412 tidq->prev_packet = tmp;
1418 static int cs_etm__exception(struct cs_etm_traceid_queue *tidq)
1421 * When the exception packet is inserted, whether the last instruction
1422 * in previous range packet is taken branch or not, we need to force
1423 * to set 'prev_packet->last_instr_taken_branch' to true. This ensures
1424 * to generate branch sample for the instruction range before the
1425 * exception is trapped to kernel or before the exception returning.
1427 * The exception packet includes the dummy address values, so don't
1428 * swap PACKET with PREV_PACKET. This keeps PREV_PACKET to be useful
1429 * for generating instruction and branch samples.
1431 if (tidq->prev_packet->sample_type == CS_ETM_RANGE)
1432 tidq->prev_packet->last_instr_taken_branch = true;
1437 static int cs_etm__flush(struct cs_etm_queue *etmq,
1438 struct cs_etm_traceid_queue *tidq)
1441 struct cs_etm_auxtrace *etm = etmq->etm;
1442 struct cs_etm_packet *tmp;
1444 /* Handle start tracing packet */
1445 if (tidq->prev_packet->sample_type == CS_ETM_EMPTY)
1448 if (etmq->etm->synth_opts.last_branch &&
1449 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1451 * Generate a last branch event for the branches left in the
1452 * circular buffer at the end of the trace.
1454 * Use the address of the end of the last reported execution
1457 u64 addr = cs_etm__last_executed_instr(tidq->prev_packet);
1459 err = cs_etm__synth_instruction_sample(
1461 tidq->period_instructions);
1465 tidq->period_instructions = 0;
1469 if (etm->sample_branches &&
1470 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1471 err = cs_etm__synth_branch_sample(etmq, tidq);
1477 if (etm->sample_branches || etm->synth_opts.last_branch) {
1479 * Swap PACKET with PREV_PACKET: PACKET becomes PREV_PACKET for
1480 * the next incoming packet.
1483 tidq->packet = tidq->prev_packet;
1484 tidq->prev_packet = tmp;
1490 static int cs_etm__end_block(struct cs_etm_queue *etmq,
1491 struct cs_etm_traceid_queue *tidq)
1496 * It has no new packet coming and 'etmq->packet' contains the stale
1497 * packet which was set at the previous time with packets swapping;
1498 * so skip to generate branch sample to avoid stale packet.
1500 * For this case only flush branch stack and generate a last branch
1501 * event for the branches left in the circular buffer at the end of
1504 if (etmq->etm->synth_opts.last_branch &&
1505 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1507 * Use the address of the end of the last reported execution
1510 u64 addr = cs_etm__last_executed_instr(tidq->prev_packet);
1512 err = cs_etm__synth_instruction_sample(
1514 tidq->period_instructions);
1518 tidq->period_instructions = 0;
1524 * cs_etm__get_data_block: Fetch a block from the auxtrace_buffer queue
1526 * Returns: < 0 if error
1527 * = 0 if no more auxtrace_buffer to read
1528 * > 0 if the current buffer isn't empty yet
1530 static int cs_etm__get_data_block(struct cs_etm_queue *etmq)
1534 if (!etmq->buf_len) {
1535 ret = cs_etm__get_trace(etmq);
1539 * We cannot assume consecutive blocks in the data file
1540 * are contiguous, reset the decoder to force re-sync.
1542 ret = cs_etm_decoder__reset(etmq->decoder);
1547 return etmq->buf_len;
1550 static bool cs_etm__is_svc_instr(struct cs_etm_queue *etmq, u8 trace_chan_id,
1551 struct cs_etm_packet *packet,
1554 /* Initialise to keep compiler happy */
1559 switch (packet->isa) {
1560 case CS_ETM_ISA_T32:
1562 * The SVC of T32 is defined in ARM DDI 0487D.a, F5.1.247:
1565 * +-----------------+--------+
1566 * | 1 1 0 1 1 1 1 1 | imm8 |
1567 * +-----------------+--------+
1569 * According to the specifiction, it only defines SVC for T32
1570 * with 16 bits instruction and has no definition for 32bits;
1571 * so below only read 2 bytes as instruction size for T32.
1573 addr = end_addr - 2;
1574 cs_etm__mem_access(etmq, trace_chan_id, addr,
1575 sizeof(instr16), (u8 *)&instr16);
1576 if ((instr16 & 0xFF00) == 0xDF00)
1580 case CS_ETM_ISA_A32:
1582 * The SVC of A32 is defined in ARM DDI 0487D.a, F5.1.247:
1584 * b'31 b'28 b'27 b'24
1585 * +---------+---------+-------------------------+
1586 * | !1111 | 1 1 1 1 | imm24 |
1587 * +---------+---------+-------------------------+
1589 addr = end_addr - 4;
1590 cs_etm__mem_access(etmq, trace_chan_id, addr,
1591 sizeof(instr32), (u8 *)&instr32);
1592 if ((instr32 & 0x0F000000) == 0x0F000000 &&
1593 (instr32 & 0xF0000000) != 0xF0000000)
1597 case CS_ETM_ISA_A64:
1599 * The SVC of A64 is defined in ARM DDI 0487D.a, C6.2.294:
1602 * +-----------------------+---------+-----------+
1603 * | 1 1 0 1 0 1 0 0 0 0 0 | imm16 | 0 0 0 0 1 |
1604 * +-----------------------+---------+-----------+
1606 addr = end_addr - 4;
1607 cs_etm__mem_access(etmq, trace_chan_id, addr,
1608 sizeof(instr32), (u8 *)&instr32);
1609 if ((instr32 & 0xFFE0001F) == 0xd4000001)
1613 case CS_ETM_ISA_UNKNOWN:
1621 static bool cs_etm__is_syscall(struct cs_etm_queue *etmq,
1622 struct cs_etm_traceid_queue *tidq, u64 magic)
1624 u8 trace_chan_id = tidq->trace_chan_id;
1625 struct cs_etm_packet *packet = tidq->packet;
1626 struct cs_etm_packet *prev_packet = tidq->prev_packet;
1628 if (magic == __perf_cs_etmv3_magic)
1629 if (packet->exception_number == CS_ETMV3_EXC_SVC)
1633 * ETMv4 exception type CS_ETMV4_EXC_CALL covers SVC, SMC and
1634 * HVC cases; need to check if it's SVC instruction based on
1637 if (magic == __perf_cs_etmv4_magic) {
1638 if (packet->exception_number == CS_ETMV4_EXC_CALL &&
1639 cs_etm__is_svc_instr(etmq, trace_chan_id, prev_packet,
1640 prev_packet->end_addr))
1647 static bool cs_etm__is_async_exception(struct cs_etm_traceid_queue *tidq,
1650 struct cs_etm_packet *packet = tidq->packet;
1652 if (magic == __perf_cs_etmv3_magic)
1653 if (packet->exception_number == CS_ETMV3_EXC_DEBUG_HALT ||
1654 packet->exception_number == CS_ETMV3_EXC_ASYNC_DATA_ABORT ||
1655 packet->exception_number == CS_ETMV3_EXC_PE_RESET ||
1656 packet->exception_number == CS_ETMV3_EXC_IRQ ||
1657 packet->exception_number == CS_ETMV3_EXC_FIQ)
1660 if (magic == __perf_cs_etmv4_magic)
1661 if (packet->exception_number == CS_ETMV4_EXC_RESET ||
1662 packet->exception_number == CS_ETMV4_EXC_DEBUG_HALT ||
1663 packet->exception_number == CS_ETMV4_EXC_SYSTEM_ERROR ||
1664 packet->exception_number == CS_ETMV4_EXC_INST_DEBUG ||
1665 packet->exception_number == CS_ETMV4_EXC_DATA_DEBUG ||
1666 packet->exception_number == CS_ETMV4_EXC_IRQ ||
1667 packet->exception_number == CS_ETMV4_EXC_FIQ)
1673 static bool cs_etm__is_sync_exception(struct cs_etm_queue *etmq,
1674 struct cs_etm_traceid_queue *tidq,
1677 u8 trace_chan_id = tidq->trace_chan_id;
1678 struct cs_etm_packet *packet = tidq->packet;
1679 struct cs_etm_packet *prev_packet = tidq->prev_packet;
1681 if (magic == __perf_cs_etmv3_magic)
1682 if (packet->exception_number == CS_ETMV3_EXC_SMC ||
1683 packet->exception_number == CS_ETMV3_EXC_HYP ||
1684 packet->exception_number == CS_ETMV3_EXC_JAZELLE_THUMBEE ||
1685 packet->exception_number == CS_ETMV3_EXC_UNDEFINED_INSTR ||
1686 packet->exception_number == CS_ETMV3_EXC_PREFETCH_ABORT ||
1687 packet->exception_number == CS_ETMV3_EXC_DATA_FAULT ||
1688 packet->exception_number == CS_ETMV3_EXC_GENERIC)
1691 if (magic == __perf_cs_etmv4_magic) {
1692 if (packet->exception_number == CS_ETMV4_EXC_TRAP ||
1693 packet->exception_number == CS_ETMV4_EXC_ALIGNMENT ||
1694 packet->exception_number == CS_ETMV4_EXC_INST_FAULT ||
1695 packet->exception_number == CS_ETMV4_EXC_DATA_FAULT)
1699 * For CS_ETMV4_EXC_CALL, except SVC other instructions
1700 * (SMC, HVC) are taken as sync exceptions.
1702 if (packet->exception_number == CS_ETMV4_EXC_CALL &&
1703 !cs_etm__is_svc_instr(etmq, trace_chan_id, prev_packet,
1704 prev_packet->end_addr))
1708 * ETMv4 has 5 bits for exception number; if the numbers
1709 * are in the range ( CS_ETMV4_EXC_FIQ, CS_ETMV4_EXC_END ]
1710 * they are implementation defined exceptions.
1712 * For this case, simply take it as sync exception.
1714 if (packet->exception_number > CS_ETMV4_EXC_FIQ &&
1715 packet->exception_number <= CS_ETMV4_EXC_END)
1722 static int cs_etm__set_sample_flags(struct cs_etm_queue *etmq,
1723 struct cs_etm_traceid_queue *tidq)
1725 struct cs_etm_packet *packet = tidq->packet;
1726 struct cs_etm_packet *prev_packet = tidq->prev_packet;
1727 u8 trace_chan_id = tidq->trace_chan_id;
1731 switch (packet->sample_type) {
1734 * Immediate branch instruction without neither link nor
1735 * return flag, it's normal branch instruction within
1738 if (packet->last_instr_type == OCSD_INSTR_BR &&
1739 packet->last_instr_subtype == OCSD_S_INSTR_NONE) {
1740 packet->flags = PERF_IP_FLAG_BRANCH;
1742 if (packet->last_instr_cond)
1743 packet->flags |= PERF_IP_FLAG_CONDITIONAL;
1747 * Immediate branch instruction with link (e.g. BL), this is
1748 * branch instruction for function call.
1750 if (packet->last_instr_type == OCSD_INSTR_BR &&
1751 packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
1752 packet->flags = PERF_IP_FLAG_BRANCH |
1756 * Indirect branch instruction with link (e.g. BLR), this is
1757 * branch instruction for function call.
1759 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1760 packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
1761 packet->flags = PERF_IP_FLAG_BRANCH |
1765 * Indirect branch instruction with subtype of
1766 * OCSD_S_INSTR_V7_IMPLIED_RET, this is explicit hint for
1767 * function return for A32/T32.
1769 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1770 packet->last_instr_subtype == OCSD_S_INSTR_V7_IMPLIED_RET)
1771 packet->flags = PERF_IP_FLAG_BRANCH |
1772 PERF_IP_FLAG_RETURN;
1775 * Indirect branch instruction without link (e.g. BR), usually
1776 * this is used for function return, especially for functions
1777 * within dynamic link lib.
1779 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1780 packet->last_instr_subtype == OCSD_S_INSTR_NONE)
1781 packet->flags = PERF_IP_FLAG_BRANCH |
1782 PERF_IP_FLAG_RETURN;
1784 /* Return instruction for function return. */
1785 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1786 packet->last_instr_subtype == OCSD_S_INSTR_V8_RET)
1787 packet->flags = PERF_IP_FLAG_BRANCH |
1788 PERF_IP_FLAG_RETURN;
1791 * Decoder might insert a discontinuity in the middle of
1792 * instruction packets, fixup prev_packet with flag
1793 * PERF_IP_FLAG_TRACE_BEGIN to indicate restarting trace.
1795 if (prev_packet->sample_type == CS_ETM_DISCONTINUITY)
1796 prev_packet->flags |= PERF_IP_FLAG_BRANCH |
1797 PERF_IP_FLAG_TRACE_BEGIN;
1800 * If the previous packet is an exception return packet
1801 * and the return address just follows SVC instuction,
1802 * it needs to calibrate the previous packet sample flags
1803 * as PERF_IP_FLAG_SYSCALLRET.
1805 if (prev_packet->flags == (PERF_IP_FLAG_BRANCH |
1806 PERF_IP_FLAG_RETURN |
1807 PERF_IP_FLAG_INTERRUPT) &&
1808 cs_etm__is_svc_instr(etmq, trace_chan_id,
1809 packet, packet->start_addr))
1810 prev_packet->flags = PERF_IP_FLAG_BRANCH |
1811 PERF_IP_FLAG_RETURN |
1812 PERF_IP_FLAG_SYSCALLRET;
1814 case CS_ETM_DISCONTINUITY:
1816 * The trace is discontinuous, if the previous packet is
1817 * instruction packet, set flag PERF_IP_FLAG_TRACE_END
1818 * for previous packet.
1820 if (prev_packet->sample_type == CS_ETM_RANGE)
1821 prev_packet->flags |= PERF_IP_FLAG_BRANCH |
1822 PERF_IP_FLAG_TRACE_END;
1824 case CS_ETM_EXCEPTION:
1825 ret = cs_etm__get_magic(packet->trace_chan_id, &magic);
1829 /* The exception is for system call. */
1830 if (cs_etm__is_syscall(etmq, tidq, magic))
1831 packet->flags = PERF_IP_FLAG_BRANCH |
1833 PERF_IP_FLAG_SYSCALLRET;
1835 * The exceptions are triggered by external signals from bus,
1836 * interrupt controller, debug module, PE reset or halt.
1838 else if (cs_etm__is_async_exception(tidq, magic))
1839 packet->flags = PERF_IP_FLAG_BRANCH |
1841 PERF_IP_FLAG_ASYNC |
1842 PERF_IP_FLAG_INTERRUPT;
1844 * Otherwise, exception is caused by trap, instruction &
1845 * data fault, or alignment errors.
1847 else if (cs_etm__is_sync_exception(etmq, tidq, magic))
1848 packet->flags = PERF_IP_FLAG_BRANCH |
1850 PERF_IP_FLAG_INTERRUPT;
1853 * When the exception packet is inserted, since exception
1854 * packet is not used standalone for generating samples
1855 * and it's affiliation to the previous instruction range
1856 * packet; so set previous range packet flags to tell perf
1857 * it is an exception taken branch.
1859 if (prev_packet->sample_type == CS_ETM_RANGE)
1860 prev_packet->flags = packet->flags;
1862 case CS_ETM_EXCEPTION_RET:
1864 * When the exception return packet is inserted, since
1865 * exception return packet is not used standalone for
1866 * generating samples and it's affiliation to the previous
1867 * instruction range packet; so set previous range packet
1868 * flags to tell perf it is an exception return branch.
1870 * The exception return can be for either system call or
1871 * other exception types; unfortunately the packet doesn't
1872 * contain exception type related info so we cannot decide
1873 * the exception type purely based on exception return packet.
1874 * If we record the exception number from exception packet and
1875 * reuse it for excpetion return packet, this is not reliable
1876 * due the trace can be discontinuity or the interrupt can
1877 * be nested, thus the recorded exception number cannot be
1878 * used for exception return packet for these two cases.
1880 * For exception return packet, we only need to distinguish the
1881 * packet is for system call or for other types. Thus the
1882 * decision can be deferred when receive the next packet which
1883 * contains the return address, based on the return address we
1884 * can read out the previous instruction and check if it's a
1885 * system call instruction and then calibrate the sample flag
1888 if (prev_packet->sample_type == CS_ETM_RANGE)
1889 prev_packet->flags = PERF_IP_FLAG_BRANCH |
1890 PERF_IP_FLAG_RETURN |
1891 PERF_IP_FLAG_INTERRUPT;
1901 static int cs_etm__decode_data_block(struct cs_etm_queue *etmq)
1904 size_t processed = 0;
1907 * Packets are decoded and added to the decoder's packet queue
1908 * until the decoder packet processing callback has requested that
1909 * processing stops or there is nothing left in the buffer. Normal
1910 * operations that stop processing are a timestamp packet or a full
1911 * decoder buffer queue.
1913 ret = cs_etm_decoder__process_data_block(etmq->decoder,
1915 &etmq->buf[etmq->buf_used],
1921 etmq->offset += processed;
1922 etmq->buf_used += processed;
1923 etmq->buf_len -= processed;
1929 static int cs_etm__process_traceid_queue(struct cs_etm_queue *etmq,
1930 struct cs_etm_traceid_queue *tidq)
1933 struct cs_etm_packet_queue *packet_queue;
1935 packet_queue = &tidq->packet_queue;
1937 /* Process each packet in this chunk */
1939 ret = cs_etm_decoder__get_packet(packet_queue,
1943 * Stop processing this chunk on
1944 * end of data or error
1949 * Since packet addresses are swapped in packet
1950 * handling within below switch() statements,
1951 * thus setting sample flags must be called
1952 * prior to switch() statement to use address
1953 * information before packets swapping.
1955 ret = cs_etm__set_sample_flags(etmq, tidq);
1959 switch (tidq->packet->sample_type) {
1962 * If the packet contains an instruction
1963 * range, generate instruction sequence
1966 cs_etm__sample(etmq, tidq);
1968 case CS_ETM_EXCEPTION:
1969 case CS_ETM_EXCEPTION_RET:
1971 * If the exception packet is coming,
1972 * make sure the previous instruction
1973 * range packet to be handled properly.
1975 cs_etm__exception(tidq);
1977 case CS_ETM_DISCONTINUITY:
1979 * Discontinuity in trace, flush
1980 * previous branch stack
1982 cs_etm__flush(etmq, tidq);
1986 * Should not receive empty packet,
1989 pr_err("CS ETM Trace: empty packet\n");
1999 static void cs_etm__clear_all_traceid_queues(struct cs_etm_queue *etmq)
2002 struct int_node *inode;
2003 struct cs_etm_traceid_queue *tidq;
2004 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
2006 intlist__for_each_entry(inode, traceid_queues_list) {
2007 idx = (int)(intptr_t)inode->priv;
2008 tidq = etmq->traceid_queues[idx];
2010 /* Ignore return value */
2011 cs_etm__process_traceid_queue(etmq, tidq);
2014 * Generate an instruction sample with the remaining
2015 * branchstack entries.
2017 cs_etm__flush(etmq, tidq);
2021 static int cs_etm__run_decoder(struct cs_etm_queue *etmq)
2024 struct cs_etm_traceid_queue *tidq;
2026 tidq = cs_etm__etmq_get_traceid_queue(etmq, CS_ETM_PER_THREAD_TRACEID);
2030 /* Go through each buffer in the queue and decode them one by one */
2032 err = cs_etm__get_data_block(etmq);
2036 /* Run trace decoder until buffer consumed or end of trace */
2038 err = cs_etm__decode_data_block(etmq);
2043 * Process each packet in this chunk, nothing to do if
2044 * an error occurs other than hoping the next one will
2047 err = cs_etm__process_traceid_queue(etmq, tidq);
2049 } while (etmq->buf_len);
2052 /* Flush any remaining branch stack entries */
2053 err = cs_etm__end_block(etmq, tidq);
2059 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
2063 struct auxtrace_queues *queues = &etm->queues;
2065 for (i = 0; i < queues->nr_queues; i++) {
2066 struct auxtrace_queue *queue = &etm->queues.queue_array[i];
2067 struct cs_etm_queue *etmq = queue->priv;
2068 struct cs_etm_traceid_queue *tidq;
2073 tidq = cs_etm__etmq_get_traceid_queue(etmq,
2074 CS_ETM_PER_THREAD_TRACEID);
2079 if ((tid == -1) || (tidq->tid == tid)) {
2080 cs_etm__set_pid_tid_cpu(etm, tidq);
2081 cs_etm__run_decoder(etmq);
2088 static int cs_etm__process_queues(struct cs_etm_auxtrace *etm)
2091 unsigned int cs_queue_nr, queue_nr;
2094 struct auxtrace_queue *queue;
2095 struct cs_etm_queue *etmq;
2096 struct cs_etm_traceid_queue *tidq;
2099 if (!etm->heap.heap_cnt)
2102 /* Take the entry at the top of the min heap */
2103 cs_queue_nr = etm->heap.heap_array[0].queue_nr;
2104 queue_nr = TO_QUEUE_NR(cs_queue_nr);
2105 trace_chan_id = TO_TRACE_CHAN_ID(cs_queue_nr);
2106 queue = &etm->queues.queue_array[queue_nr];
2110 * Remove the top entry from the heap since we are about
2113 auxtrace_heap__pop(&etm->heap);
2115 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
2118 * No traceID queue has been allocated for this traceID,
2119 * which means something somewhere went very wrong. No
2120 * other choice than simply exit.
2127 * Packets associated with this timestamp are already in
2128 * the etmq's traceID queue, so process them.
2130 ret = cs_etm__process_traceid_queue(etmq, tidq);
2135 * Packets for this timestamp have been processed, time to
2136 * move on to the next timestamp, fetching a new auxtrace_buffer
2140 ret = cs_etm__get_data_block(etmq);
2145 * No more auxtrace_buffers to process in this etmq, simply
2146 * move on to another entry in the auxtrace_heap.
2151 ret = cs_etm__decode_data_block(etmq);
2155 timestamp = cs_etm__etmq_get_timestamp(etmq, &trace_chan_id);
2159 * Function cs_etm__decode_data_block() returns when
2160 * there is no more traces to decode in the current
2161 * auxtrace_buffer OR when a timestamp has been
2162 * encountered on any of the traceID queues. Since we
2163 * did not get a timestamp, there is no more traces to
2164 * process in this auxtrace_buffer. As such empty and
2165 * flush all traceID queues.
2167 cs_etm__clear_all_traceid_queues(etmq);
2169 /* Fetch another auxtrace_buffer for this etmq */
2174 * Add to the min heap the timestamp for packets that have
2175 * just been decoded. They will be processed and synthesized
2176 * during the next call to cs_etm__process_traceid_queue() for
2177 * this queue/traceID.
2179 cs_queue_nr = TO_CS_QUEUE_NR(queue_nr, trace_chan_id);
2180 ret = auxtrace_heap__add(&etm->heap, cs_queue_nr, timestamp);
2187 static int cs_etm__process_itrace_start(struct cs_etm_auxtrace *etm,
2188 union perf_event *event)
2192 if (etm->timeless_decoding)
2196 * Add the tid/pid to the log so that we can get a match when
2197 * we get a contextID from the decoder.
2199 th = machine__findnew_thread(etm->machine,
2200 event->itrace_start.pid,
2201 event->itrace_start.tid);
2210 static int cs_etm__process_switch_cpu_wide(struct cs_etm_auxtrace *etm,
2211 union perf_event *event)
2214 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
2217 * Context switch in per-thread mode are irrelevant since perf
2218 * will start/stop tracing as the process is scheduled.
2220 if (etm->timeless_decoding)
2224 * SWITCH_IN events carry the next process to be switched out while
2225 * SWITCH_OUT events carry the process to be switched in. As such
2226 * we don't care about IN events.
2232 * Add the tid/pid to the log so that we can get a match when
2233 * we get a contextID from the decoder.
2235 th = machine__findnew_thread(etm->machine,
2236 event->context_switch.next_prev_pid,
2237 event->context_switch.next_prev_tid);
2246 static int cs_etm__process_event(struct perf_session *session,
2247 union perf_event *event,
2248 struct perf_sample *sample,
2249 struct perf_tool *tool)
2253 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2254 struct cs_etm_auxtrace,
2260 if (!tool->ordered_events) {
2261 pr_err("CoreSight ETM Trace requires ordered events\n");
2265 if (sample->time && (sample->time != (u64) -1))
2266 timestamp = sample->time;
2270 if (timestamp || etm->timeless_decoding) {
2271 err = cs_etm__update_queues(etm);
2276 if (etm->timeless_decoding &&
2277 event->header.type == PERF_RECORD_EXIT)
2278 return cs_etm__process_timeless_queues(etm,
2281 if (event->header.type == PERF_RECORD_ITRACE_START)
2282 return cs_etm__process_itrace_start(etm, event);
2283 else if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)
2284 return cs_etm__process_switch_cpu_wide(etm, event);
2286 if (!etm->timeless_decoding &&
2287 event->header.type == PERF_RECORD_AUX)
2288 return cs_etm__process_queues(etm);
2293 static int cs_etm__process_auxtrace_event(struct perf_session *session,
2294 union perf_event *event,
2295 struct perf_tool *tool __maybe_unused)
2297 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2298 struct cs_etm_auxtrace,
2300 if (!etm->data_queued) {
2301 struct auxtrace_buffer *buffer;
2303 int fd = perf_data__fd(session->data);
2304 bool is_pipe = perf_data__is_pipe(session->data);
2310 data_offset = lseek(fd, 0, SEEK_CUR);
2311 if (data_offset == -1)
2315 err = auxtrace_queues__add_event(&etm->queues, session,
2316 event, data_offset, &buffer);
2321 if (auxtrace_buffer__get_data(buffer, fd)) {
2322 cs_etm__dump_event(etm, buffer);
2323 auxtrace_buffer__put_data(buffer);
2330 static bool cs_etm__is_timeless_decoding(struct cs_etm_auxtrace *etm)
2332 struct evsel *evsel;
2333 struct evlist *evlist = etm->session->evlist;
2334 bool timeless_decoding = true;
2337 * Circle through the list of event and complain if we find one
2338 * with the time bit set.
2340 evlist__for_each_entry(evlist, evsel) {
2341 if ((evsel->core.attr.sample_type & PERF_SAMPLE_TIME))
2342 timeless_decoding = false;
2345 return timeless_decoding;
2348 static const char * const cs_etm_global_header_fmts[] = {
2349 [CS_HEADER_VERSION_0] = " Header version %llx\n",
2350 [CS_PMU_TYPE_CPUS] = " PMU type/num cpus %llx\n",
2351 [CS_ETM_SNAPSHOT] = " Snapshot %llx\n",
2354 static const char * const cs_etm_priv_fmts[] = {
2355 [CS_ETM_MAGIC] = " Magic number %llx\n",
2356 [CS_ETM_CPU] = " CPU %lld\n",
2357 [CS_ETM_ETMCR] = " ETMCR %llx\n",
2358 [CS_ETM_ETMTRACEIDR] = " ETMTRACEIDR %llx\n",
2359 [CS_ETM_ETMCCER] = " ETMCCER %llx\n",
2360 [CS_ETM_ETMIDR] = " ETMIDR %llx\n",
2363 static const char * const cs_etmv4_priv_fmts[] = {
2364 [CS_ETM_MAGIC] = " Magic number %llx\n",
2365 [CS_ETM_CPU] = " CPU %lld\n",
2366 [CS_ETMV4_TRCCONFIGR] = " TRCCONFIGR %llx\n",
2367 [CS_ETMV4_TRCTRACEIDR] = " TRCTRACEIDR %llx\n",
2368 [CS_ETMV4_TRCIDR0] = " TRCIDR0 %llx\n",
2369 [CS_ETMV4_TRCIDR1] = " TRCIDR1 %llx\n",
2370 [CS_ETMV4_TRCIDR2] = " TRCIDR2 %llx\n",
2371 [CS_ETMV4_TRCIDR8] = " TRCIDR8 %llx\n",
2372 [CS_ETMV4_TRCAUTHSTATUS] = " TRCAUTHSTATUS %llx\n",
2375 static void cs_etm__print_auxtrace_info(__u64 *val, int num)
2379 for (i = 0; i < CS_HEADER_VERSION_0_MAX; i++)
2380 fprintf(stdout, cs_etm_global_header_fmts[i], val[i]);
2382 for (i = CS_HEADER_VERSION_0_MAX; cpu < num; cpu++) {
2383 if (val[i] == __perf_cs_etmv3_magic)
2384 for (j = 0; j < CS_ETM_PRIV_MAX; j++, i++)
2385 fprintf(stdout, cs_etm_priv_fmts[j], val[i]);
2386 else if (val[i] == __perf_cs_etmv4_magic)
2387 for (j = 0; j < CS_ETMV4_PRIV_MAX; j++, i++)
2388 fprintf(stdout, cs_etmv4_priv_fmts[j], val[i]);
2390 /* failure.. return */
2395 int cs_etm__process_auxtrace_info(union perf_event *event,
2396 struct perf_session *session)
2398 struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
2399 struct cs_etm_auxtrace *etm = NULL;
2400 struct int_node *inode;
2401 unsigned int pmu_type;
2402 int event_header_size = sizeof(struct perf_event_header);
2403 int info_header_size;
2404 int total_size = auxtrace_info->header.size;
2407 int err = 0, idx = -1;
2409 u64 *ptr, *hdr = NULL;
2410 u64 **metadata = NULL;
2413 * sizeof(auxtrace_info_event::type) +
2414 * sizeof(auxtrace_info_event::reserved) == 8
2416 info_header_size = 8;
2418 if (total_size < (event_header_size + info_header_size))
2421 priv_size = total_size - event_header_size - info_header_size;
2423 /* First the global part */
2424 ptr = (u64 *) auxtrace_info->priv;
2426 /* Look for version '0' of the header */
2430 hdr = zalloc(sizeof(*hdr) * CS_HEADER_VERSION_0_MAX);
2434 /* Extract header information - see cs-etm.h for format */
2435 for (i = 0; i < CS_HEADER_VERSION_0_MAX; i++)
2437 num_cpu = hdr[CS_PMU_TYPE_CPUS] & 0xffffffff;
2438 pmu_type = (unsigned int) ((hdr[CS_PMU_TYPE_CPUS] >> 32) &
2442 * Create an RB tree for traceID-metadata tuple. Since the conversion
2443 * has to be made for each packet that gets decoded, optimizing access
2444 * in anything other than a sequential array is worth doing.
2446 traceid_list = intlist__new(NULL);
2447 if (!traceid_list) {
2452 metadata = zalloc(sizeof(*metadata) * num_cpu);
2455 goto err_free_traceid_list;
2459 * The metadata is stored in the auxtrace_info section and encodes
2460 * the configuration of the ARM embedded trace macrocell which is
2461 * required by the trace decoder to properly decode the trace due
2462 * to its highly compressed nature.
2464 for (j = 0; j < num_cpu; j++) {
2465 if (ptr[i] == __perf_cs_etmv3_magic) {
2466 metadata[j] = zalloc(sizeof(*metadata[j]) *
2470 goto err_free_metadata;
2472 for (k = 0; k < CS_ETM_PRIV_MAX; k++)
2473 metadata[j][k] = ptr[i + k];
2475 /* The traceID is our handle */
2476 idx = metadata[j][CS_ETM_ETMTRACEIDR];
2477 i += CS_ETM_PRIV_MAX;
2478 } else if (ptr[i] == __perf_cs_etmv4_magic) {
2479 metadata[j] = zalloc(sizeof(*metadata[j]) *
2483 goto err_free_metadata;
2485 for (k = 0; k < CS_ETMV4_PRIV_MAX; k++)
2486 metadata[j][k] = ptr[i + k];
2488 /* The traceID is our handle */
2489 idx = metadata[j][CS_ETMV4_TRCTRACEIDR];
2490 i += CS_ETMV4_PRIV_MAX;
2493 /* Get an RB node for this CPU */
2494 inode = intlist__findnew(traceid_list, idx);
2496 /* Something went wrong, no need to continue */
2499 goto err_free_metadata;
2503 * The node for that CPU should not be taken.
2504 * Back out if that's the case.
2508 goto err_free_metadata;
2510 /* All good, associate the traceID with the metadata pointer */
2511 inode->priv = metadata[j];
2515 * Each of CS_HEADER_VERSION_0_MAX, CS_ETM_PRIV_MAX and
2516 * CS_ETMV4_PRIV_MAX mark how many double words are in the
2517 * global metadata, and each cpu's metadata respectively.
2518 * The following tests if the correct number of double words was
2519 * present in the auxtrace info section.
2521 if (i * 8 != priv_size) {
2523 goto err_free_metadata;
2526 etm = zalloc(sizeof(*etm));
2530 goto err_free_metadata;
2533 err = auxtrace_queues__init(&etm->queues);
2537 etm->session = session;
2538 etm->machine = &session->machines.host;
2540 etm->num_cpu = num_cpu;
2541 etm->pmu_type = pmu_type;
2542 etm->snapshot_mode = (hdr[CS_ETM_SNAPSHOT] != 0);
2543 etm->metadata = metadata;
2544 etm->auxtrace_type = auxtrace_info->type;
2545 etm->timeless_decoding = cs_etm__is_timeless_decoding(etm);
2547 etm->auxtrace.process_event = cs_etm__process_event;
2548 etm->auxtrace.process_auxtrace_event = cs_etm__process_auxtrace_event;
2549 etm->auxtrace.flush_events = cs_etm__flush_events;
2550 etm->auxtrace.free_events = cs_etm__free_events;
2551 etm->auxtrace.free = cs_etm__free;
2552 session->auxtrace = &etm->auxtrace;
2554 etm->unknown_thread = thread__new(999999999, 999999999);
2555 if (!etm->unknown_thread) {
2557 goto err_free_queues;
2561 * Initialize list node so that at thread__zput() we can avoid
2562 * segmentation fault at list_del_init().
2564 INIT_LIST_HEAD(&etm->unknown_thread->node);
2566 err = thread__set_comm(etm->unknown_thread, "unknown", 0);
2568 goto err_delete_thread;
2570 if (thread__init_map_groups(etm->unknown_thread, etm->machine)) {
2572 goto err_delete_thread;
2576 cs_etm__print_auxtrace_info(auxtrace_info->priv, num_cpu);
2580 if (session->itrace_synth_opts->set) {
2581 etm->synth_opts = *session->itrace_synth_opts;
2583 itrace_synth_opts__set_default(&etm->synth_opts,
2584 session->itrace_synth_opts->default_no_sample);
2585 etm->synth_opts.callchain = false;
2588 err = cs_etm__synth_events(etm, session);
2590 goto err_delete_thread;
2592 err = auxtrace_queues__process_index(&etm->queues, session);
2594 goto err_delete_thread;
2596 etm->data_queued = etm->queues.populated;
2601 thread__zput(etm->unknown_thread);
2603 auxtrace_queues__free(&etm->queues);
2604 session->auxtrace = NULL;
2608 /* No need to check @metadata[j], free(NULL) is supported */
2609 for (j = 0; j < num_cpu; j++)
2610 zfree(&metadata[j]);
2612 err_free_traceid_list:
2613 intlist__delete(traceid_list);