static int cs_etm__set_sample_flags(struct cs_etm_queue *etmq)
 {
        struct cs_etm_packet *packet = etmq->packet;
+       struct cs_etm_packet *prev_packet = etmq->prev_packet;
 
        switch (packet->sample_type) {
        case CS_ETM_RANGE:
                    packet->last_instr_subtype == OCSD_S_INSTR_V8_RET)
                        packet->flags = PERF_IP_FLAG_BRANCH |
                                        PERF_IP_FLAG_RETURN;
+
+               /*
+                * Decoder might insert a discontinuity in the middle of
+                * instruction packets, fixup prev_packet with flag
+                * PERF_IP_FLAG_TRACE_BEGIN to indicate restarting trace.
+                */
+               if (prev_packet->sample_type == CS_ETM_DISCONTINUITY)
+                       prev_packet->flags |= PERF_IP_FLAG_BRANCH |
+                                             PERF_IP_FLAG_TRACE_BEGIN;
                break;
        case CS_ETM_DISCONTINUITY:
+               /*
+                * The trace is discontinuous, if the previous packet is
+                * instruction packet, set flag PERF_IP_FLAG_TRACE_END
+                * for previous packet.
+                */
+               if (prev_packet->sample_type == CS_ETM_RANGE)
+                       prev_packet->flags |= PERF_IP_FLAG_BRANCH |
+                                             PERF_IP_FLAG_TRACE_END;
+               break;
        case CS_ETM_EXCEPTION:
        case CS_ETM_EXCEPTION_RET:
        case CS_ETM_EMPTY: