cs-etm: account for each trace buffer in the queue
[firefly-linux-kernel-4.4.55.git] / tools / perf / util / cs-etm.c
1 /*
2  * Copyright(C) 2016 Linaro Limited. All rights reserved.
3  * Author: Tor Jeremiassen <tor.jeremiassen@linaro.org>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <linux/kernel.h>
19 #include <linux/types.h>
20 #include <linux/bitops.h>
21 #include <linux/log2.h>
22
23 #include "perf.h"
24 #include "thread_map.h"
25 #include "thread.h"
26 #include "thread-stack.h"
27 #include "callchain.h"
28 #include "auxtrace.h"
29 #include "evlist.h"
30 #include "machine.h"
31 #include "util.h"
32 #include "color.h"
33 #include "cs-etm.h"
34 #include "cs-etm-decoder/cs-etm-decoder.h"
35 #include "debug.h"
36
37 #include <stdlib.h>
38
39 #define KiB(x) ((x) * 1024)
40 #define MiB(x) ((x) * 1024 * 1024)
41 #define MAX_TIMESTAMP (~0ULL)
42
43 struct cs_etm_auxtrace {
44         struct auxtrace         auxtrace;
45         struct auxtrace_queues  queues;
46         struct auxtrace_heap    heap;
47         u64                    **metadata;
48         u32                     auxtrace_type;
49         struct perf_session    *session;
50         struct machine         *machine;
51         struct perf_evsel      *switch_evsel;
52         struct thread          *unknown_thread;
53         uint32_t                num_cpu;
54         bool                    timeless_decoding;
55         bool                    sampling_mode;
56         bool                    snapshot_mode;
57         bool                    data_queued;
58         bool                    sync_switch;
59         bool                    synth_needs_swap;
60         int                     have_sched_switch;
61
62         bool                    sample_instructions;
63         u64                     instructions_sample_type;
64         u64                     instructions_sample_period;
65         u64                     instructions_id;
66         struct itrace_synth_opts synth_opts;
67         unsigned                pmu_type;
68 };
69
70 struct cs_etm_queue {
71         struct cs_etm_auxtrace *etm;
72         unsigned                queue_nr;
73         struct auxtrace_buffer *buffer;
74         const struct           cs_etm_state *state;
75         struct ip_callchain    *chain;
76         union perf_event       *event_buf;
77         bool                    on_heap;
78         bool                    step_through_buffers;
79         bool                    use_buffer_pid_tid;
80         pid_t                   pid, tid;
81         int                     cpu;
82         struct thread          *thread;
83         u64                     time;
84         u64                     timestamp;
85         bool                    stop;
86         bool                    have_sample;
87         struct cs_etm_decoder  *decoder;
88         u64                     offset;
89         bool                    eot;
90         bool                    kernel_mapped;
91 };
92
93 static int cs_etm__get_trace(struct cs_etm_buffer *buff, struct cs_etm_queue *etmq);
94 static int cs_etm__update_queues(struct cs_etm_auxtrace *);
95 static int cs_etm__process_queues(struct cs_etm_auxtrace *, u64);
96 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *, pid_t, u64);
97 static uint32_t cs_etm__mem_access(struct cs_etm_queue *, uint64_t , size_t , uint8_t *);
98
99 static void cs_etm__packet_dump(const char *pkt_string)
100 {
101         const char *color = PERF_COLOR_BLUE;
102
103         color_fprintf(stdout,color, "  %s\n", pkt_string);
104         fflush(stdout);
105 }
106
107 static void cs_etm__dump_event(struct cs_etm_auxtrace *etm,
108                               struct auxtrace_buffer *buffer)
109 {
110         const char *color = PERF_COLOR_BLUE;
111         struct cs_etm_decoder_params d_params;
112         struct cs_etm_trace_params *t_params;
113         struct cs_etm_decoder *decoder;
114         size_t buffer_used = 0;
115         size_t i;
116
117         fprintf(stdout,"\n");
118         color_fprintf(stdout, color,
119                      ". ... CoreSight ETM Trace data: size %zu bytes\n",
120                      buffer->size);
121
122         t_params = zalloc(sizeof(struct cs_etm_trace_params) * etm->num_cpu);
123         for (i = 0; i < etm->num_cpu; ++i) {
124                 t_params[i].protocol = CS_ETM_PROTO_ETMV4i;
125                 t_params[i].reg_idr0 = etm->metadata[i][CS_ETMV4_TRCIDR0];
126                 t_params[i].reg_idr1 = etm->metadata[i][CS_ETMV4_TRCIDR1];
127                 t_params[i].reg_idr2 = etm->metadata[i][CS_ETMV4_TRCIDR2];
128                 t_params[i].reg_idr8 = etm->metadata[i][CS_ETMV4_TRCIDR8];
129                 t_params[i].reg_configr = etm->metadata[i][CS_ETMV4_TRCCONFIGR];
130                 t_params[i].reg_traceidr = etm->metadata[i][CS_ETMV4_TRCTRACEIDR];
131   //[CS_ETMV4_TRCAUTHSTATUS] = "   TRCAUTHSTATUS                  %"PRIx64"\n",
132         }
133         d_params.packet_printer = cs_etm__packet_dump;
134         d_params.operation = CS_ETM_OPERATION_PRINT;
135         d_params.formatted = true;
136         d_params.fsyncs = false;
137         d_params.hsyncs = false;
138         d_params.frame_aligned = true;
139
140         decoder = cs_etm_decoder__new(etm->num_cpu,&d_params, t_params);
141
142         zfree(&t_params);
143
144         if (decoder == NULL) {
145                 return; 
146         }
147         do {
148             size_t consumed;
149             cs_etm_decoder__process_data_block(decoder,buffer->offset,&(((uint8_t *)buffer->data)[buffer_used]),buffer->size - buffer_used, &consumed);
150             buffer_used += consumed;
151         } while(buffer_used < buffer->size);
152         cs_etm_decoder__free(decoder);
153 }
154                               
155 static int cs_etm__flush_events(struct perf_session *session, struct perf_tool *tool){
156         struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
157                                                    struct cs_etm_auxtrace,
158                                                    auxtrace);
159
160         int ret;
161
162         if (dump_trace)
163                 return 0;
164
165         if (!tool->ordered_events)
166                 return -EINVAL;
167
168         ret = cs_etm__update_queues(etm);
169
170         if (ret < 0) 
171                 return ret;
172
173         if (etm->timeless_decoding)
174                 return cs_etm__process_timeless_queues(etm,-1,MAX_TIMESTAMP - 1);
175
176         return cs_etm__process_queues(etm, MAX_TIMESTAMP);
177 }
178
179 static void  cs_etm__set_pid_tid_cpu(struct cs_etm_auxtrace *etm,
180                                     struct auxtrace_queue *queue)
181 {
182         struct cs_etm_queue *etmq = queue->priv;
183
184         if ((queue->tid == -1) || (etm->have_sched_switch)) {
185                 etmq->tid = machine__get_current_tid(etm->machine, etmq->cpu);
186                 thread__zput(etmq->thread);
187         }
188
189         if ((!etmq->thread) && (etmq->tid != -1)) {
190                 etmq->thread = machine__find_thread(etm->machine,-1,etmq->tid);
191         }
192
193         if (etmq->thread) {
194                 etmq->pid = etmq->thread->pid_;
195                 if (queue->cpu == -1) {
196                         etmq->cpu = etmq->thread->cpu;
197                 }
198         }
199 }
200
201 static void cs_etm__free_queue(void *priv)
202 {
203         struct cs_etm_queue *etmq = priv;
204
205         if (!etmq)
206                 return;
207
208         thread__zput(etmq->thread);
209         cs_etm_decoder__free(etmq->decoder);
210         zfree(&etmq->event_buf);
211         zfree(&etmq->chain);
212         free(etmq);
213 }
214
215 static void cs_etm__free_events(struct perf_session *session)
216 {
217         struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
218                                                    struct cs_etm_auxtrace,
219                                                    auxtrace);
220
221         struct auxtrace_queues *queues = &(aux->queues);
222
223         unsigned i;
224
225         for (i = 0; i < queues->nr_queues; ++i) {
226                 cs_etm__free_queue(queues->queue_array[i].priv);
227                 queues->queue_array[i].priv = 0;
228         }
229
230         auxtrace_queues__free(queues);
231
232 }
233
234 static void cs_etm__free(struct perf_session *session)
235 {
236
237         size_t i;
238         struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
239                                                    struct cs_etm_auxtrace,
240                                                    auxtrace);
241         auxtrace_heap__free(&aux->heap);
242         cs_etm__free_events(session);
243         session->auxtrace = NULL;
244         //thread__delete(aux->unknown_thread);
245         for (i = 0; i < aux->num_cpu; ++i) {
246                 zfree(&aux->metadata[i]);
247         }
248         zfree(&aux->metadata);
249         free(aux);
250 }
251
252 static void cs_etm__use_buffer_pid_tid(struct cs_etm_queue *etmq,
253                                       struct auxtrace_queue *queue,
254                                       struct auxtrace_buffer *buffer)
255 {
256         if ((queue->cpu == -1) && (buffer->cpu != -1)) {
257                 etmq->cpu = buffer->cpu;
258         }
259
260         etmq->pid = buffer->pid;
261         etmq->tid = buffer->tid;
262
263         thread__zput(etmq->thread);
264
265         if (etmq->tid != -1) {
266                 if (etmq->pid != -1) {
267                         etmq->thread = machine__findnew_thread(etmq->etm->machine,
268                                                                etmq->pid,
269                                                                etmq->tid);
270                 } else {
271                         etmq->thread = machine__findnew_thread(etmq->etm->machine,
272                                                                -1,
273                                                                etmq->tid);
274                 }
275         }
276 }
277
278
279 static int cs_etm__get_trace(struct cs_etm_buffer *buff, struct cs_etm_queue *etmq)
280 {
281         struct auxtrace_buffer *aux_buffer = etmq->buffer;
282         struct auxtrace_buffer *old_buffer = aux_buffer;
283         struct auxtrace_queue *queue;
284
285         if (etmq->stop) {
286                 buff->len = 0;
287                 return 0;
288         }
289
290         queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
291
292         aux_buffer = auxtrace_buffer__next(queue,aux_buffer);
293
294         if (!aux_buffer) {
295                 if (old_buffer) {
296                         auxtrace_buffer__drop_data(old_buffer);
297                 }
298                 buff->len = 0;
299                 return 0;
300         }
301
302         etmq->buffer = aux_buffer;
303
304         if (!aux_buffer->data) {
305                 int fd = perf_data_file__fd(etmq->etm->session->file);
306
307                 aux_buffer->data = auxtrace_buffer__get_data(aux_buffer, fd);
308                 if (!aux_buffer->data)
309                         return -ENOMEM;
310         }
311
312         if (old_buffer)
313                 auxtrace_buffer__drop_data(old_buffer);
314
315         if (aux_buffer->use_data) {
316                 buff->offset = aux_buffer->offset;
317                 buff->len = aux_buffer->use_size;
318                 buff->buf = aux_buffer->use_data;
319         } else {
320                 buff->offset = aux_buffer->offset;
321                 buff->len = aux_buffer->size;
322                 buff->buf = aux_buffer->data;
323         }
324         /*
325         buff->offset = 0;
326         buff->len = sizeof(cstrace);
327         buff->buf = cstrace;
328         */
329
330         buff->ref_timestamp = aux_buffer->reference;
331
332         if (etmq->use_buffer_pid_tid && 
333             ((etmq->pid != aux_buffer->pid) || 
334              (etmq->tid != aux_buffer->tid))) {
335                 cs_etm__use_buffer_pid_tid(etmq,queue,aux_buffer);
336         }
337
338         if (etmq->step_through_buffers)
339                 etmq->stop = true;
340
341         return buff->len;
342 }
343
344 static struct cs_etm_queue *cs_etm__alloc_queue(struct cs_etm_auxtrace *etm,
345                                                unsigned int queue_nr)
346 {
347         struct cs_etm_decoder_params d_params;
348         struct cs_etm_trace_params   *t_params;
349         struct cs_etm_queue *etmq;
350         size_t i;
351
352         etmq = zalloc(sizeof(struct cs_etm_queue));
353         if (!etmq)
354                 return NULL;
355
356         if (etm->synth_opts.callchain) {
357                 size_t sz = sizeof(struct ip_callchain);
358
359                 sz += etm->synth_opts.callchain_sz * sizeof(u64);
360                 etmq->chain = zalloc(sz);
361                 if (!etmq->chain)
362                         goto out_free;
363         } else {
364                 etmq->chain = NULL;
365         }
366
367         etmq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
368         if (!etmq->event_buf)
369                 goto out_free;
370
371         etmq->etm = etm;
372         etmq->queue_nr = queue_nr;
373         etmq->pid = -1;
374         etmq->tid = -1;
375         etmq->cpu = -1;
376         etmq->stop = false;
377         etmq->kernel_mapped = false;
378
379         t_params = zalloc(sizeof(struct cs_etm_trace_params)*etm->num_cpu);
380
381         for (i = 0; i < etm->num_cpu; ++i) {
382                 t_params[i].reg_idr0 = etm->metadata[i][CS_ETMV4_TRCIDR0];
383                 t_params[i].reg_idr1 = etm->metadata[i][CS_ETMV4_TRCIDR1];
384                 t_params[i].reg_idr2 = etm->metadata[i][CS_ETMV4_TRCIDR2];
385                 t_params[i].reg_idr8 = etm->metadata[i][CS_ETMV4_TRCIDR8];
386                 t_params[i].reg_configr = etm->metadata[i][CS_ETMV4_TRCCONFIGR];
387                 t_params[i].reg_traceidr = etm->metadata[i][CS_ETMV4_TRCTRACEIDR];
388                 t_params[i].protocol = CS_ETM_PROTO_ETMV4i;
389         }
390         d_params.packet_printer = cs_etm__packet_dump;
391         d_params.operation = CS_ETM_OPERATION_DECODE;    
392         d_params.formatted = true;
393         d_params.fsyncs = false;
394         d_params.hsyncs = false;
395         d_params.frame_aligned = true;
396         d_params.data = etmq;
397
398         etmq->decoder = cs_etm_decoder__new(etm->num_cpu,&d_params,t_params);
399
400
401         zfree(&t_params);
402
403         if (!etmq->decoder)
404                 goto out_free;
405
406         etmq->offset = 0;
407         etmq->eot = false;
408
409         return etmq;
410
411 out_free:
412         zfree(&etmq->event_buf);
413         zfree(&etmq->chain);
414         free(etmq);
415         return NULL;
416 }
417
418 static int cs_etm__setup_queue(struct cs_etm_auxtrace *etm, 
419                               struct auxtrace_queue *queue,
420                               unsigned int queue_nr)
421 {
422         struct cs_etm_queue *etmq = queue->priv;
423
424         if (list_empty(&(queue->head))) 
425                 return 0;
426
427         if (etmq == NULL) {
428                 etmq = cs_etm__alloc_queue(etm,queue_nr);
429
430                 if (etmq == NULL) {
431                         return -ENOMEM;
432                 }
433
434                 queue->priv = etmq;
435
436                 if (queue->cpu != -1) {
437                         etmq->cpu = queue->cpu;
438                 }
439
440                 etmq->tid = queue->tid;
441
442                 if (etm->sampling_mode) {
443                         if (etm->timeless_decoding)
444                                 etmq->step_through_buffers = true;
445                         if (etm->timeless_decoding || !etm->have_sched_switch)
446                                 etmq->use_buffer_pid_tid = true;
447                 }
448         }
449         
450         if (!etmq->on_heap && 
451             (!etm->sync_switch)) {
452                 const struct cs_etm_state *state;
453                 int ret = 0;
454
455                 if (etm->timeless_decoding)
456                         return ret;
457
458                 //cs_etm__log("queue %u getting timestamp\n",queue_nr);
459                 //cs_etm__log("queue %u decoding cpu %d pid %d tid %d\n",
460                            //queue_nr, etmq->cpu, etmq->pid, etmq->tid);
461                 (void) state;
462                 return ret;
463                 /*
464                 while (1) {
465                         state = cs_etm_decoder__decode(etmq->decoder);
466                         if (state->err) {
467                                 if (state->err == CS_ETM_ERR_NODATA) {
468                                         //cs_etm__log("queue %u has no timestamp\n",
469                                                    //queue_nr);
470                                         return 0;
471                                 }
472                                 continue;
473                         }
474                         if (state->timestamp)
475                                 break;
476                 }
477
478                 etmq->timestamp = state->timestamp;
479                 //cs_etm__log("queue %u timestamp 0x%"PRIx64 "\n",
480                            //queue_nr, etmq->timestamp);
481                 etmq->state = state;
482                 etmq->have_sample = true;
483                 //cs_etm__sample_flags(etmq);
484                 ret = auxtrace_heap__add(&etm->heap, queue_nr, etmq->timestamp);
485                 if (ret)
486                         return ret;
487                 etmq->on_heap = true;
488                 */
489         }
490         
491         return 0;
492 }
493
494
495 static int cs_etm__setup_queues(struct cs_etm_auxtrace *etm)
496 {
497         unsigned int i;
498         int ret;
499
500         for (i = 0; i < etm->queues.nr_queues; i++) {
501                 ret = cs_etm__setup_queue(etm, &(etm->queues.queue_array[i]),i);
502                 if (ret)
503                         return ret;
504         }
505         return 0;
506 }
507
508 #if 0
509 struct cs_etm_cache_entry {
510         struct auxtrace_cache_entry     entry;
511         uint64_t                        icount;
512         uint64_t                        bcount;
513 };
514
515 static size_t cs_etm__cache_divisor(void)
516 {
517         static size_t d = 64;
518
519         return d;
520 }
521
522 static size_t cs_etm__cache_size(struct dso *dso,
523                                 struct machine *machine)
524 {
525         off_t size;
526
527         size = dso__data_size(dso,machine);
528         size /= cs_etm__cache_divisor();
529
530         if (size < 1000) 
531                 return 10;
532
533         if (size > (1 << 21)) 
534                 return 21;
535
536         return 32 - __builtin_clz(size);
537 }
538
539 static struct auxtrace_cache *cs_etm__cache(struct dso *dso,
540                                            struct machine *machine)
541 {
542         struct auxtrace_cache *c;
543         size_t bits;
544
545         if (dso->auxtrace_cache)
546                 return dso->auxtrace_cache;
547
548         bits = cs_etm__cache_size(dso,machine);
549
550         c = auxtrace_cache__new(bits, sizeof(struct cs_etm_cache_entry), 200);
551
552         dso->auxtrace_cache = c;
553
554         return c;
555 }
556
557 static int cs_etm__cache_add(struct dso *dso, struct machine *machine,
558                             uint64_t offset, uint64_t icount, uint64_t bcount)
559 {
560         struct auxtrace_cache *c = cs_etm__cache(dso, machine);
561         struct cs_etm_cache_entry *e;
562         int err;
563
564         if (!c)
565                 return -ENOMEM;
566
567         e = auxtrace_cache__alloc_entry(c);
568         if (!e)
569                 return -ENOMEM;
570
571         e->icount = icount;
572         e->bcount = bcount;
573
574         err = auxtrace_cache__add(c, offset, &e->entry);
575
576         if (err)
577                 auxtrace_cache__free_entry(c, e);
578
579         return err;
580 }
581
582 static struct cs_etm_cache_entry *cs_etm__cache_lookup(struct dso *dso,
583                                                       struct machine *machine,
584                                                       uint64_t offset)
585 {
586         struct auxtrace_cache *c = cs_etm__cache(dso, machine);
587
588         if (!c)
589                 return NULL;
590
591         return auxtrace_cache__lookup(dso->auxtrace_cache, offset);
592 }
593 #endif
594
595 static int cs_etm__synth_instruction_sample(struct cs_etm_queue *etmq,
596                                            struct cs_etm_packet *packet)
597 {
598         int ret = 0;
599         struct cs_etm_auxtrace *etm = etmq->etm;
600         union perf_event *event = etmq->event_buf;
601         struct perf_sample sample = {.ip = 0,};
602         uint64_t start_addr = packet->start_addr;
603         uint64_t end_addr = packet->end_addr;
604
605         event->sample.header.type = PERF_RECORD_SAMPLE;
606         event->sample.header.misc = PERF_RECORD_MISC_USER;
607         event->sample.header.size = sizeof(struct perf_event_header);
608
609
610         sample.ip = start_addr;
611         sample.pid = etmq->pid;
612         sample.tid = etmq->tid;
613         sample.addr = end_addr;
614         sample.id = etmq->etm->instructions_id;
615         sample.stream_id = etmq->etm->instructions_id;
616         sample.period = (end_addr - start_addr) >> 2; 
617         sample.cpu = etmq->cpu;
618         sample.flags = 0; // etmq->flags;
619         sample.insn_len = 1; // etmq->insn_len;
620
621         //etmq->last_insn_cnt = etmq->state->tot_insn_cnt;
622
623 #if 0
624         {
625                 struct   addr_location al;
626                 uint64_t offset;
627                 struct   thread *thread;
628                 struct   machine *machine = etmq->etm->machine;
629                 uint8_t  cpumode;
630                 struct   cs_etm_cache_entry *e;
631                 uint8_t  buf[256];
632                 size_t   bufsz;
633
634                 thread = etmq->thread;
635
636                 if (!thread) {
637                         thread = etmq->etm->unknown_thread;
638                 }
639
640                 if (start_addr > 0xffffffc000000000UL) {
641                         cpumode = PERF_RECORD_MISC_KERNEL;
642                 } else {
643                         cpumode = PERF_RECORD_MISC_USER;
644                 }
645
646                 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, start_addr,&al);
647                 if (!al.map || !al.map->dso) {
648                         goto endTest;
649                 }
650                 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
651                     dso__data_status_seen(al.map->dso,DSO_DATA_STATUS_SEEN_ITRACE)) {
652                         goto endTest;
653                 }
654
655                 offset = al.map->map_ip(al.map,start_addr);
656
657
658                 e = cs_etm__cache_lookup(al.map->dso, machine, offset);
659
660                 if (e) {
661                   (void) e;
662                 } else {
663                         int len;
664                         map__load(al.map, machine->symbol_filter);
665
666                         bufsz = sizeof(buf);
667                         len = dso__data_read_offset(al.map->dso, machine,
668                                                     offset, buf, bufsz);
669
670                         if (len <= 0) {
671                                 goto endTest;
672                         }
673
674                         cs_etm__cache_add(al.map->dso, machine, offset, (end_addr - start_addr) >> 2, end_addr - start_addr);
675
676                 }
677 endTest:
678                 (void) offset;
679         }
680 #endif
681
682         ret = perf_session__deliver_synth_event(etm->session,event, &sample);
683
684         if (ret) {
685                 pr_err("CS ETM Trace: failed to deliver instruction event, error %d\n", ret);
686
687         }
688         return ret;
689 }
690
691 struct cs_etm_synth {
692         struct perf_tool dummy_tool;
693         struct perf_session *session;
694 };
695
696
697 static int cs_etm__event_synth(struct perf_tool *tool,
698                               union perf_event *event,
699                               struct perf_sample *sample,
700                               struct machine *machine)
701 {
702         struct cs_etm_synth *cs_etm_synth =
703                       container_of(tool, struct cs_etm_synth, dummy_tool);
704
705         (void) sample;
706         (void) machine;
707
708         return perf_session__deliver_synth_event(cs_etm_synth->session, event, NULL);
709
710 }
711
712
713 static int cs_etm__synth_event(struct perf_session *session,
714                               struct perf_event_attr *attr, u64 id)
715 {
716         struct cs_etm_synth cs_etm_synth;
717
718         memset(&cs_etm_synth, 0, sizeof(struct cs_etm_synth));
719         cs_etm_synth.session = session;
720
721         return perf_event__synthesize_attr(&cs_etm_synth.dummy_tool, attr, 1,
722                                            &id, cs_etm__event_synth);
723 }
724
725 static int cs_etm__synth_events(struct cs_etm_auxtrace *etm, 
726                                struct perf_session *session)
727 {
728         struct perf_evlist *evlist = session->evlist;
729         struct perf_evsel *evsel;
730         struct perf_event_attr attr;
731         bool found = false;
732         u64 id;
733         int err;
734
735         evlist__for_each(evlist, evsel) {
736
737                 if (evsel->attr.type == etm->pmu_type) {
738                         found = true;
739                         break;
740                 }
741         }
742
743         if (!found) {
744                 pr_debug("There are no selected events with Core Sight Trace data\n");
745                 return 0;
746         }
747
748         memset(&attr, 0, sizeof(struct perf_event_attr));
749         attr.size = sizeof(struct perf_event_attr);
750         attr.type = PERF_TYPE_HARDWARE;
751         attr.sample_type = evsel->attr.sample_type & PERF_SAMPLE_MASK;
752         attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
753                             PERF_SAMPLE_PERIOD;
754         if (etm->timeless_decoding) 
755                 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
756         else
757                 attr.sample_type |= PERF_SAMPLE_TIME;
758
759         attr.exclude_user = evsel->attr.exclude_user;
760         attr.exclude_kernel = evsel->attr.exclude_kernel;
761         attr.exclude_hv = evsel->attr.exclude_hv;
762         attr.exclude_host = evsel->attr.exclude_host;
763         attr.exclude_guest = evsel->attr.exclude_guest;
764         attr.sample_id_all = evsel->attr.sample_id_all;
765         attr.read_format = evsel->attr.read_format;
766
767         id = evsel->id[0] + 1000000000;
768
769         if (!id)
770                 id = 1;
771
772         if (etm->synth_opts.instructions) {
773                 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
774                 attr.sample_period = etm->synth_opts.period;
775                 etm->instructions_sample_period = attr.sample_period;
776                 err = cs_etm__synth_event(session, &attr, id);
777
778                 if (err) {
779                         pr_err("%s: failed to synthesize 'instructions' event type\n",
780                                __func__);
781                         return err;
782                 }
783                 etm->sample_instructions = true;
784                 etm->instructions_sample_type = attr.sample_type;
785                 etm->instructions_id = id;
786                 id += 1;
787         }
788
789         etm->synth_needs_swap = evsel->needs_swap;
790         return 0;
791 }
792
793 static int cs_etm__sample(struct cs_etm_queue *etmq)
794 {
795         //const struct cs_etm_state *state = etmq->state;
796         struct cs_etm_packet packet;
797         //struct cs_etm_auxtrace *etm = etmq->etm;
798         int err;
799
800         if (!etmq->have_sample)
801                 return 0;
802
803         etmq->have_sample = false;
804
805         err = cs_etm_decoder__get_packet(etmq->decoder,&packet);
806         // if there is no sample, it returns err = -1, no real error
807
808         if (!err && packet.sample_type & CS_ETM_RANGE) {
809                 err = cs_etm__synth_instruction_sample(etmq,&packet);
810                 if (err)
811                         return err;
812         }
813         return 0;
814 }
815
816 static int cs_etm__run_decoder(struct cs_etm_queue *etmq, u64 *timestamp)
817 {
818         struct cs_etm_buffer buffer;
819         size_t buffer_used;
820         int err = 0;
821
822         /* Go through each buffer in the queue and decode them one by one */
823 more:
824         buffer_used = 0;
825         memset(&buffer, 0, sizeof(buffer));
826         err = cs_etm__get_trace(&buffer,etmq);
827         if (err <= 0)
828                 return err;
829
830         do {
831             size_t processed = 0;
832             etmq->state = cs_etm_decoder__process_data_block(etmq->decoder,
833                                                etmq->offset,
834                                                &buffer.buf[buffer_used],
835                                                buffer.len-buffer_used,
836                                                &processed);
837             err = etmq->state->err;
838             etmq->offset += processed;
839             buffer_used += processed;
840             if (!err) {
841                 etmq->have_sample = true;
842                 cs_etm__sample(etmq);
843             }
844         } while (!etmq->eot && (buffer.len > buffer_used));
845 goto more;
846
847         (void) timestamp;
848
849         return err;
850 }
851
852 static int cs_etm__update_queues(struct cs_etm_auxtrace *etm)
853 {
854   if (etm->queues.new_data) {
855         etm->queues.new_data = false;
856         return cs_etm__setup_queues(etm);
857   }
858   return 0;
859 }
860
861 static int cs_etm__process_queues(struct cs_etm_auxtrace *etm, u64 timestamp)
862 {
863         unsigned int queue_nr;
864         u64 ts;
865         int ret;
866
867         while (1) {
868                 struct auxtrace_queue *queue;
869                 struct cs_etm_queue *etmq;
870         
871                 if (!etm->heap.heap_cnt)
872                         return 0;
873         
874                 if (etm->heap.heap_array[0].ordinal >= timestamp)
875                         return 0;
876         
877                 queue_nr = etm->heap.heap_array[0].queue_nr;
878                 queue = &etm->queues.queue_array[queue_nr];
879                 etmq = queue->priv;
880         
881                 //cs_etm__log("queue %u processing 0x%" PRIx64 " to 0x%" PRIx64 "\n",
882                            //queue_nr, etm->heap.heap_array[0].ordinal,
883                            //timestamp);
884
885                 auxtrace_heap__pop(&etm->heap);
886
887                 if (etm->heap.heap_cnt) {
888                         ts = etm->heap.heap_array[0].ordinal + 1;
889                         if (ts > timestamp)
890                                 ts = timestamp;
891                 } else {
892                         ts = timestamp;
893                 }
894
895                 cs_etm__set_pid_tid_cpu(etm, queue);
896
897                 ret = cs_etm__run_decoder(etmq, &ts);
898
899                 if (ret < 0) {
900                         auxtrace_heap__add(&etm->heap, queue_nr, ts);
901                         return ret;
902                 }
903
904                 if (!ret) {
905                         ret = auxtrace_heap__add(&etm->heap, queue_nr, ts);
906                         if (ret < 0)
907                                 return ret;
908                 } else {
909                         etmq->on_heap = false;
910                 }
911         }
912         return 0;
913 }
914
915 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
916                                           pid_t tid,
917                                           u64 time_)
918 {
919         struct auxtrace_queues *queues = &etm->queues;
920         unsigned int i;
921         u64 ts = 0;
922         
923         for (i = 0; i < queues->nr_queues; ++i) {
924                 struct auxtrace_queue *queue = &(etm->queues.queue_array[i]);
925                 struct cs_etm_queue *etmq = queue->priv;
926
927                 if (etmq && ((tid == -1) || (etmq->tid == tid))) {
928                         etmq->time = time_;
929                         cs_etm__set_pid_tid_cpu(etm, queue);
930                         cs_etm__run_decoder(etmq,&ts);
931
932                 }
933         }
934         return 0;
935 }
936
937 static struct cs_etm_queue *cs_etm__cpu_to_etmq(struct cs_etm_auxtrace *etm, 
938                                                int cpu)
939 {
940         unsigned q,j;
941
942         if (etm->queues.nr_queues == 0)
943                 return NULL;
944
945         if (cpu < 0)
946                 q = 0;
947         else if ((unsigned) cpu >= etm->queues.nr_queues)
948                 q = etm->queues.nr_queues - 1;
949         else 
950                 q = cpu;
951
952         if (etm->queues.queue_array[q].cpu == cpu)
953                 return etm->queues.queue_array[q].priv;
954
955         for (j = 0; q > 0; j++) {
956                 if (etm->queues.queue_array[--q].cpu == cpu)
957                         return etm->queues.queue_array[q].priv;
958         }
959
960         for (; j < etm->queues.nr_queues; j++) {
961                 if (etm->queues.queue_array[j].cpu == cpu)
962                         return etm->queues.queue_array[j].priv;
963
964         }
965
966         return NULL;
967 }
968
969 static uint32_t cs_etm__mem_access(struct cs_etm_queue *etmq, uint64_t address, size_t size, uint8_t *buffer)
970 {
971         struct   addr_location al;
972         uint64_t offset;
973         struct   thread *thread;
974         struct   machine *machine;
975         uint8_t  cpumode;
976         int len;
977
978         if (etmq == NULL)
979                 return -1;
980
981         machine = etmq->etm->machine;
982         thread = etmq->thread;
983         if (address > 0xffffffc000000000UL) {
984                 cpumode = PERF_RECORD_MISC_KERNEL;
985         } else {
986                 cpumode = PERF_RECORD_MISC_USER;
987         }
988
989         thread__find_addr_map(thread, cpumode, MAP__FUNCTION, address,&al);
990
991         if (!al.map || !al.map->dso) {
992                 return 0;
993         }
994
995         if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
996             dso__data_status_seen(al.map->dso,DSO_DATA_STATUS_SEEN_ITRACE)) {
997                 return 0;
998         }
999
1000         offset = al.map->map_ip(al.map,address);
1001
1002         map__load(al.map, machine->symbol_filter);
1003
1004         len = dso__data_read_offset(al.map->dso, machine,
1005                                     offset, buffer, size);
1006
1007         if (len <= 0) {
1008                 return 0;
1009         }
1010
1011         return len;
1012 }
1013
1014 static bool check_need_swap(int file_endian)
1015 {
1016         const int data = 1;
1017         u8 *check = (u8 *)&data;
1018         int host_endian;
1019
1020         if (check[0] == 1)
1021                 host_endian = ELFDATA2LSB;
1022         else
1023                 host_endian = ELFDATA2MSB;
1024
1025         return host_endian != file_endian;
1026 }
1027
1028 static int cs_etm__read_elf_info(const char *fname, uint64_t *foffset, uint64_t *fstart, uint64_t *fsize)
1029 {
1030         FILE *fp;
1031         u8 e_ident[EI_NIDENT];
1032         int ret = -1;
1033         bool need_swap = false;
1034         size_t buf_size;
1035         void *buf;
1036         int i;
1037
1038         fp = fopen(fname, "r");
1039         if (fp == NULL)
1040                 return -1;
1041
1042         if (fread(e_ident, sizeof(e_ident), 1, fp) != 1)
1043                 goto out;
1044
1045         if (memcmp(e_ident, ELFMAG, SELFMAG) ||
1046             e_ident[EI_VERSION] != EV_CURRENT)
1047                 goto out;
1048
1049         need_swap = check_need_swap(e_ident[EI_DATA]);
1050
1051         /* for simplicity */
1052         fseek(fp, 0, SEEK_SET);
1053
1054         if (e_ident[EI_CLASS] == ELFCLASS32) {
1055                 Elf32_Ehdr ehdr;
1056                 Elf32_Phdr *phdr;
1057
1058                 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
1059                         goto out;
1060
1061                 if (need_swap) {
1062                         ehdr.e_phoff = bswap_32(ehdr.e_phoff);
1063                         ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
1064                         ehdr.e_phnum = bswap_16(ehdr.e_phnum);
1065                 }
1066
1067                 buf_size = ehdr.e_phentsize * ehdr.e_phnum;
1068                 buf = malloc(buf_size);
1069                 if (buf == NULL)
1070                         goto out;
1071
1072                 fseek(fp, ehdr.e_phoff, SEEK_SET);
1073                 if (fread(buf, buf_size, 1, fp) != 1)
1074                         goto out_free;
1075
1076                 for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
1077
1078                         if (need_swap) {
1079                                 phdr->p_type = bswap_32(phdr->p_type);
1080                                 phdr->p_offset = bswap_32(phdr->p_offset);
1081                                 phdr->p_filesz = bswap_32(phdr->p_filesz);
1082                         }
1083
1084                         if (phdr->p_type != PT_LOAD)
1085                                 continue;
1086
1087                         *foffset = phdr->p_offset;
1088                         *fstart = phdr->p_vaddr;
1089                         *fsize = phdr->p_filesz;
1090                         ret = 0;
1091                         break;
1092                 }
1093         } else {
1094                 Elf64_Ehdr ehdr;
1095                 Elf64_Phdr *phdr;
1096
1097                 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
1098                         goto out;
1099
1100                 if (need_swap) {
1101                         ehdr.e_phoff = bswap_64(ehdr.e_phoff);
1102                         ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
1103                         ehdr.e_phnum = bswap_16(ehdr.e_phnum);
1104                 }
1105
1106                 buf_size = ehdr.e_phentsize * ehdr.e_phnum;
1107                 buf = malloc(buf_size);
1108                 if (buf == NULL)
1109                         goto out;
1110
1111                 fseek(fp, ehdr.e_phoff, SEEK_SET);
1112                 if (fread(buf, buf_size, 1, fp) != 1)
1113                         goto out_free;
1114
1115                 for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
1116
1117                         if (need_swap) {
1118                                 phdr->p_type = bswap_32(phdr->p_type);
1119                                 phdr->p_offset = bswap_64(phdr->p_offset);
1120                                 phdr->p_filesz = bswap_64(phdr->p_filesz);
1121                         }
1122
1123                         if (phdr->p_type != PT_LOAD)
1124                                 continue;
1125
1126                         *foffset = phdr->p_offset;
1127                         *fstart = phdr->p_vaddr;
1128                         *fsize = phdr->p_filesz;
1129                         ret = 0;
1130                         break;
1131                 }
1132         }
1133 out_free:
1134         free(buf);
1135 out:
1136         fclose(fp);
1137         return ret;
1138 }
1139
1140 static int cs_etm__process_event(struct perf_session *session,
1141                                 union perf_event *event,
1142                                 struct perf_sample *sample,
1143                                 struct perf_tool *tool)
1144 {
1145         struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
1146                                                    struct cs_etm_auxtrace,
1147                                                    auxtrace);
1148
1149         u64 timestamp;
1150         int err = 0;
1151
1152         if (dump_trace) 
1153                 return 0;
1154
1155         if (!tool->ordered_events) {
1156                 pr_err("CoreSight ETM Trace requires ordered events\n");
1157                 return -EINVAL;
1158         }
1159
1160         if (sample->time && (sample->time != (u64)-1))
1161                 timestamp = sample->time;
1162         else
1163                 timestamp = 0;
1164
1165         if (timestamp || etm->timeless_decoding) {
1166                 err = cs_etm__update_queues(etm);
1167                 if (err)
1168                         return err;
1169
1170         }
1171
1172         if (event->header.type == PERF_RECORD_MMAP2) {
1173                 struct dso *dso;
1174                 int cpu;
1175                 struct cs_etm_queue *etmq;
1176
1177                 cpu = sample->cpu;
1178
1179                 etmq = cs_etm__cpu_to_etmq(etm,cpu);
1180
1181                 if (!etmq) {
1182                         return -1;
1183                 }
1184
1185                 dso = dsos__find(&(etm->machine->dsos),event->mmap2.filename,false);
1186                 if (NULL != dso) {
1187                         err = cs_etm_decoder__add_mem_access_cb(
1188                             etmq->decoder,
1189                             event->mmap2.start, 
1190                             event->mmap2.len, 
1191                             cs_etm__mem_access);
1192                 }
1193
1194                 if ((symbol_conf.vmlinux_name != NULL) && (!etmq->kernel_mapped)) {
1195                         uint64_t foffset;
1196                         uint64_t fstart;
1197                         uint64_t fsize;
1198
1199                         err = cs_etm__read_elf_info(symbol_conf.vmlinux_name,
1200                                                       &foffset,&fstart,&fsize);
1201
1202                         if (!err) {
1203                                 cs_etm_decoder__add_bin_file(
1204                                         etmq->decoder,
1205                                         foffset,
1206                                         fstart,
1207                                         fsize & ~0x1ULL,
1208                                         symbol_conf.vmlinux_name);
1209
1210                                 etmq->kernel_mapped = true;
1211                         }
1212                 }
1213
1214         }
1215
1216         if (etm->timeless_decoding) {
1217                 if (event->header.type == PERF_RECORD_EXIT) {
1218                         err = cs_etm__process_timeless_queues(etm,
1219                                                              event->fork.tid,
1220                                                              sample->time);
1221                 }
1222         } else if (timestamp) {
1223                 err = cs_etm__process_queues(etm, timestamp);
1224         }
1225
1226         //cs_etm__log("event %s (%u): cpu %d time%"PRIu64" tsc %#"PRIx64"\n",
1227                    //perf_event__name(event->header.type), event->header.type,
1228                    //sample->cpu, sample->time, timestamp);
1229         return err;
1230 }
1231
1232 static int cs_etm__process_auxtrace_event(struct perf_session *session,
1233                                   union perf_event *event,
1234                                   struct perf_tool *tool)
1235 {
1236         struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
1237                                                    struct cs_etm_auxtrace,
1238                                                    auxtrace);
1239
1240         (void) tool;
1241
1242         if (!etm->data_queued) {
1243                 struct auxtrace_buffer *buffer;
1244                 off_t  data_offset;
1245                 int fd = perf_data_file__fd(session->file);
1246                 bool is_pipe = perf_data_file__is_pipe(session->file);
1247                 int err;
1248
1249                 if (is_pipe) {
1250                         data_offset = 0;
1251                 } else {
1252                         data_offset = lseek(fd, 0, SEEK_CUR);
1253                         if (data_offset == -1) {
1254                                 return -errno;
1255                         }
1256                 }
1257
1258                 err = auxtrace_queues__add_event(&etm->queues,
1259                                                  session,
1260                                                  event,
1261                                                  data_offset,
1262                                                  &buffer);
1263                 if (err)
1264                         return err;
1265
1266                 if (dump_trace)
1267                 {
1268                         if (auxtrace_buffer__get_data(buffer,fd)) {
1269                                 cs_etm__dump_event(etm,buffer);
1270                                 auxtrace_buffer__put_data(buffer);
1271                         }
1272                 }
1273         } 
1274
1275         return 0;
1276
1277 }
1278
1279 static const char * const cs_etm_global_header_fmts[] = {
1280   [CS_HEADER_VERSION_0]    = "   Header version                 %"PRIx64"\n",
1281   [CS_PMU_TYPE_CPUS]       = "   PMU type/num cpus              %"PRIx64"\n",
1282   [CS_ETM_SNAPSHOT]        = "   Snapshot                       %"PRIx64"\n",
1283 };
1284
1285 static const char * const cs_etm_priv_fmts[] = {
1286   [CS_ETM_MAGIC]           = "   Magic number                   %"PRIx64"\n",
1287   [CS_ETM_CPU]             = "   CPU                            %"PRIx64"\n",
1288   [CS_ETM_ETMCR]           = "   ETMCR                          %"PRIx64"\n",
1289   [CS_ETM_ETMTRACEIDR]     = "   ETMTRACEIDR                    %"PRIx64"\n",
1290   [CS_ETM_ETMCCER]         = "   ETMCCER                        %"PRIx64"\n",
1291   [CS_ETM_ETMIDR]          = "   ETMIDR                         %"PRIx64"\n",
1292 };
1293
1294 static const char * const cs_etmv4_priv_fmts[] = {
1295   [CS_ETM_MAGIC]           = "   Magic number                   %"PRIx64"\n",
1296   [CS_ETM_CPU]             = "   CPU                            %"PRIx64"\n",
1297   [CS_ETMV4_TRCCONFIGR]    = "   TRCCONFIGR                     %"PRIx64"\n",
1298   [CS_ETMV4_TRCTRACEIDR]   = "   TRCTRACEIDR                    %"PRIx64"\n",
1299   [CS_ETMV4_TRCIDR0]       = "   TRCIDR0                        %"PRIx64"\n",
1300   [CS_ETMV4_TRCIDR1]       = "   TRCIDR1                        %"PRIx64"\n",
1301   [CS_ETMV4_TRCIDR2]       = "   TRCIDR2                        %"PRIx64"\n",
1302   [CS_ETMV4_TRCIDR8]       = "   TRCIDR8                        %"PRIx64"\n",
1303   [CS_ETMV4_TRCAUTHSTATUS] = "   TRCAUTHSTATUS                  %"PRIx64"\n",
1304 };
1305
1306 static void cs_etm__print_auxtrace_info(u64 *val, size_t num)
1307 {
1308         unsigned i,j,cpu;
1309
1310         for (i = 0, cpu = 0; cpu < num; ++cpu) {
1311
1312                 if (val[i] == __perf_cs_etmv3_magic) {
1313                         for (j = 0; j < CS_ETM_PRIV_MAX; ++j, ++i) {
1314                                 fprintf(stdout,cs_etm_priv_fmts[j],val[i]);
1315                         }
1316                 } else if (val[i] == __perf_cs_etmv4_magic) {
1317                         for (j = 0; j < CS_ETMV4_PRIV_MAX; ++j, ++i) {
1318                                 fprintf(stdout,cs_etmv4_priv_fmts[j],val[i]);
1319                         }
1320                 } else {
1321                         // failure.. return
1322                         return;
1323                 }
1324         }
1325 }
1326
1327 int cs_etm__process_auxtrace_info(union perf_event *event,
1328                                  struct perf_session *session)
1329 {
1330         struct auxtrace_info_event *auxtrace_info = &(event->auxtrace_info);
1331         size_t event_header_size = sizeof(struct perf_event_header);
1332         size_t info_header_size = 8;
1333         size_t total_size = auxtrace_info->header.size;
1334         size_t priv_size = 0;
1335         size_t num_cpu;
1336         struct cs_etm_auxtrace *etm = 0;
1337         int err = 0;
1338         u64 *ptr;
1339         u64 *hdr = NULL;
1340         u64 **metadata = NULL;
1341         size_t i,j,k;
1342         unsigned pmu_type;
1343
1344         if (total_size < (event_header_size + info_header_size))
1345                 return -EINVAL;
1346
1347         priv_size = total_size - event_header_size - info_header_size;
1348
1349         // First the global part
1350
1351         ptr = (u64 *) auxtrace_info->priv;
1352         if (ptr[0] == 0) {
1353                 hdr = zalloc(sizeof(u64 *) * CS_HEADER_VERSION_0_MAX);
1354                 if (hdr == NULL) {
1355                         return -EINVAL;
1356                 }
1357                 for (i = 0; i < CS_HEADER_VERSION_0_MAX; ++i) {
1358                         hdr[i] = ptr[i];
1359                 }
1360                 num_cpu = hdr[CS_PMU_TYPE_CPUS] & 0xffffffff;
1361                 pmu_type = (unsigned) ((hdr[CS_PMU_TYPE_CPUS] >> 32) & 0xffffffff);
1362         } else {
1363                 return -EINVAL;
1364         }
1365
1366         metadata = zalloc(sizeof(u64 *) * num_cpu);
1367
1368         if (metadata == NULL) {
1369                 return -EINVAL;
1370         }
1371
1372         for (j = 0; j < num_cpu; ++j) {
1373                 if (ptr[i] == __perf_cs_etmv3_magic) {
1374                         metadata[j] = zalloc(sizeof(u64)*CS_ETM_PRIV_MAX);
1375                         if (metadata == NULL)
1376                                 return -EINVAL;
1377                         for (k = 0; k < CS_ETM_PRIV_MAX; k++) {
1378                                 metadata[j][k] = ptr[i+k];
1379                         }
1380                         i += CS_ETM_PRIV_MAX;
1381                 } else if (ptr[i] == __perf_cs_etmv4_magic) {
1382                         metadata[j] = zalloc(sizeof(u64)*CS_ETMV4_PRIV_MAX);
1383                         if (metadata == NULL)
1384                                 return -EINVAL;
1385                         for (k = 0; k < CS_ETMV4_PRIV_MAX; k++) {
1386                                 metadata[j][k] = ptr[i+k];
1387                         }
1388                         i += CS_ETMV4_PRIV_MAX;
1389                 }
1390         }
1391
1392         if (i*8 != priv_size)
1393                 return -EINVAL;
1394
1395         if (dump_trace)
1396                 cs_etm__print_auxtrace_info(auxtrace_info->priv,num_cpu);
1397
1398         etm = zalloc(sizeof(struct cs_etm_auxtrace));
1399
1400         etm->num_cpu = num_cpu;
1401         etm->pmu_type = pmu_type;
1402         etm->snapshot_mode = (hdr[CS_ETM_SNAPSHOT] != 0);
1403
1404         if (!etm)
1405                 return -ENOMEM;
1406
1407
1408         err = auxtrace_queues__init(&etm->queues);
1409         if (err)
1410                 goto err_free;
1411
1412         etm->unknown_thread = thread__new(999999999,999999999);
1413         if (etm->unknown_thread == NULL) {
1414                 err = -ENOMEM;
1415                 goto err_free_queues;
1416         }
1417         err = thread__set_comm(etm->unknown_thread, "unknown", 0);
1418         if (err) {
1419                 goto err_delete_thread;
1420         }
1421
1422         if (thread__init_map_groups(etm->unknown_thread,
1423                                     etm->machine)) {
1424                 err = -ENOMEM;
1425                 goto err_delete_thread;
1426         }
1427
1428         etm->timeless_decoding = true;
1429         etm->sampling_mode = false;
1430         etm->metadata = metadata;
1431         etm->session = session;
1432         etm->machine = &session->machines.host;
1433         etm->auxtrace_type = auxtrace_info->type;
1434
1435         etm->auxtrace.process_event = cs_etm__process_event;
1436         etm->auxtrace.process_auxtrace_event = cs_etm__process_auxtrace_event;
1437         etm->auxtrace.flush_events = cs_etm__flush_events;
1438         etm->auxtrace.free_events  = cs_etm__free_events;
1439         etm->auxtrace.free         = cs_etm__free;
1440         session->auxtrace = &(etm->auxtrace);
1441
1442         if (dump_trace)
1443                 return 0;
1444
1445         if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
1446                 etm->synth_opts = *session->itrace_synth_opts;
1447         } else {
1448                 itrace_synth_opts__set_default(&etm->synth_opts);
1449         }
1450         etm->synth_opts.branches = false;
1451         etm->synth_opts.callchain = false;
1452         etm->synth_opts.calls = false;
1453         etm->synth_opts.returns = false;
1454
1455         err = cs_etm__synth_events(etm, session);
1456         if (err)
1457                 goto err_delete_thread;
1458
1459         err = auxtrace_queues__process_index(&etm->queues, session);
1460         if (err)
1461                 goto err_delete_thread;
1462
1463         etm->data_queued = etm->queues.populated;
1464
1465         return 0;
1466
1467 err_delete_thread:
1468         thread__delete(etm->unknown_thread);
1469 err_free_queues:
1470         auxtrace_queues__free(&etm->queues);
1471         session->auxtrace = NULL;
1472 err_free:
1473         free(etm);
1474         return err;
1475 }