Fix tracecontext seek backward underflow
[lttv.git] / lttv / lttv / tracecontext.c
index 710cdecb0ad6f062bff12f9e68733035223f6078..a013b2e67518fb910263b39daed4b17569301d3c 100644 (file)
@@ -109,9 +109,8 @@ lttv_context_new_tracefile_context(LttvTracesetContext *self)
  *
  * Author : Xang Xiu Yang
  ***************************************************************************/
-static void lttv_traceset_context_compute_time_span(
-                                          LttvTracesetContext *self,
-                                                                 TimeInterval *time_span)
+void lttv_traceset_context_compute_time_span(LttvTracesetContext *self,
+       TimeInterval *time_span)
 {
   LttvTraceset * traceset = self->ts;
   int numTraces = lttv_traceset_number(traceset);
@@ -1287,7 +1286,10 @@ LttvTracefileContext *lttv_traceset_context_get_current_tfc(LttvTracesetContext
  */
 void lttv_process_traceset_synchronize_tracefiles(LttvTracesetContext *tsc)
 {
-  g_assert(lttv_process_traceset_seek_position(tsc, tsc->sync_position) == 0);
+  int retval;
+
+  retval= lttv_process_traceset_seek_position(tsc, tsc->sync_position);
+  g_assert_cmpint(retval, ==, 0);
 }
 
 
@@ -1414,6 +1416,7 @@ guint lttv_process_traceset_seek_n_backward(LttvTracesetContext *self,
   LttTime time_offset;
   struct seek_back_data sd;
   LttvHooks *hooks = lttv_hooks_new();
+  int retval;
   
   sd.first_event = 0;
   sd.events_found = 0;
@@ -1439,7 +1442,6 @@ guint lttv_process_traceset_seek_n_backward(LttvTracesetContext *self,
   if(ltt_time_compare(time, self->time_span.end_time) > 0) {
     time = self->time_span.end_time;
   }
-  asked_time = time;
   time_offset = first_offset;
  
   lttv_hooks_add(hooks, seek_back_event_hook, &sd, LTTV_PRIO_DEFAULT);
@@ -1447,17 +1449,16 @@ guint lttv_process_traceset_seek_n_backward(LttvTracesetContext *self,
   lttv_process_traceset_begin(self, NULL, NULL, NULL, hooks, NULL);
 
   while(1) {
-    /* stop criteria : - n events found
-     *                 - asked_time < beginning of trace */
-    if(ltt_time_compare(asked_time, self->time_span.start_time) < 0) break;
-
     lttv_traceset_context_position_copy(end_pos, next_iter_end_pos);
 
     /* We must seek the traceset back to time - time_offset */
     /* this time becomes the new reference time */
-    time = ltt_time_sub(time, time_offset);
+    if(ltt_time_compare(time, time_offset) > 0)
+      time = ltt_time_sub(time, time_offset);
+    else
+      time = self->time_span.start_time;
     asked_time = time;
-    
+
     time_seeker(self, time);
     lttv_traceset_context_position_save(self, next_iter_end_pos);
     /* Resync the time in case of a seek_closest */
@@ -1474,6 +1475,8 @@ guint lttv_process_traceset_seek_n_backward(LttvTracesetContext *self,
     lttv_process_traceset_middle(self, ltt_time_infinite,
         G_MAXUINT, end_pos);
 
+    /* stop criteria : - n events found
+     *                 - asked_time < beginning of trace */
     if(sd.events_found < n) {
       if(sd.first_event > 0) {
         /* Save the first position */
@@ -1490,9 +1493,19 @@ guint lttv_process_traceset_seek_n_backward(LttvTracesetContext *self,
       }
       g_ptr_array_set_size(sd.array, n-sd.events_found);
       sd.first_event = 0;
+
+      /*
+       * Did not fill our event list and started before the beginning of the
+       * trace. There is no hope to fill it then.
+       * It is OK to compare with trace start time here because we explicitely
+       * seeked by time (not by position), so we cannot miss multiple event
+       * happening exactly at trace start.
+       */
+      if(ltt_time_compare(asked_time, self->time_span.start_time) == 0)
+        break;
       
     } else break; /* Second end criterion : n events found */
-    
+
     time_offset = ltt_time_mul(time_offset, BACKWARD_SEEK_MUL);
   }
   
@@ -1506,11 +1519,13 @@ guint lttv_process_traceset_seek_n_backward(LttvTracesetContext *self,
     LttvTracesetContextPosition *pos =
       (LttvTracesetContextPosition*)g_ptr_array_index (sd.array,
                                                        sd.first_event);
-    g_assert(lttv_process_traceset_seek_position(self, pos) == 0);
+    retval= lttv_process_traceset_seek_position(self, pos);
+    g_assert_cmpint(retval, ==, 0);
   } else {
     /* Will seek to the last saved position : in the worst case, it will be the
      * original position (if events_found is 0) */
-    g_assert(lttv_process_traceset_seek_position(self, saved_pos) == 0);
+    retval= lttv_process_traceset_seek_position(self, saved_pos);
+    g_assert_cmpint(retval, ==, 0);
   }
   
   for(i=0;i<sd.array->len;i++) {
This page took 0.04131 seconds and 4 git commands to generate.