From 52cf61de975a8f44e623ab1b09a4265d542a072c Mon Sep 17 00:00:00 2001
From: Cedric Roux <cedric.roux@eurecom.fr>
Date: Wed, 2 May 2018 12:11:34 +0200
Subject: [PATCH] T tracer: don't depend on T_BUFFER_MAX anymore in the tracers

Whenever the value T_BUFFER_MAX changes, the tracers had to
be recompiled. The only reason why it was needed is because we
used some fixed size buffers to read events. This commit removes
this restriction.

Also, with the basic simulator, this value T_BUFFER_MAX now comes
with an #ifdef which would have required some special hackish-level
tricks in the tracers, which is not good.

Let's just allocate memory when needed.
---
 common/utils/T/tracer/enb.c                     |  6 +++---
 common/utils/T/tracer/event.c                   | 16 +++++++++++-----
 common/utils/T/tracer/event.h                   |  3 ++-
 common/utils/T/tracer/extract.c                 |  5 +++--
 common/utils/T/tracer/extract_config.c          | 11 ++++++++++-
 common/utils/T/tracer/extract_input_subframe.c  |  5 +++--
 common/utils/T/tracer/extract_output_subframe.c |  6 ++++--
 common/utils/T/tracer/hacks/dump_nack_signal.c  | 11 ++++++++---
 common/utils/T/tracer/hacks/time_meas.c         |  5 +++--
 common/utils/T/tracer/macpdu2wireshark.c        |  5 +++--
 common/utils/T/tracer/record.c                  | 10 +++++++++-
 common/utils/T/tracer/replay.c                  | 10 +++++++++-
 common/utils/T/tracer/textlog.c                 |  6 +++---
 common/utils/T/tracer/to_vcd.c                  |  5 +++--
 common/utils/T/tracer/ue.c                      |  6 +++---
 common/utils/T/tracer/vcd.c                     |  6 +++---
 16 files changed, 80 insertions(+), 36 deletions(-)

diff --git a/common/utils/T/tracer/enb.c b/common/utils/T/tracer/enb.c
index f607743d159..a30366faf1e 100644
--- a/common/utils/T/tracer/enb.c
+++ b/common/utils/T/tracer/enb.c
@@ -12,7 +12,6 @@
 #include "gui/gui.h"
 #include "filter/filter.h"
 #include "utils.h"
-#include "../T_defs.h"
 #include "event_selector.h"
 #include "openair_logo.h"
 #include "config.h"
@@ -875,6 +874,8 @@ int main(int n, char **v)
   if (pthread_mutex_init(&enb_data.lock, NULL)) abort();
   setup_event_selector(g, database, is_on, is_on_changed, &enb_data);
 
+  OBUF ebuf = { osize: 0, omaxsize: 0, obuf: NULL };
+
 restart:
   clear_remote_config();
   enb_data.socket = connect_to(ip, port);
@@ -884,9 +885,8 @@ restart:
 
   /* read messages */
   while (1) {
-    char v[T_BUFFER_MAX];
     event e;
-    e = get_event(enb_data.socket, v, database);
+    e = get_event(enb_data.socket, &ebuf, database);
     if (e.type == -1) goto restart;
     if (pthread_mutex_lock(&enb_data.lock)) abort();
     handle_event(h, e);
diff --git a/common/utils/T/tracer/event.c b/common/utils/T/tracer/event.c
index 0917c010b38..f5d12e472c0 100644
--- a/common/utils/T/tracer/event.c
+++ b/common/utils/T/tracer/event.c
@@ -6,7 +6,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-event get_event(int socket, char *event_buffer, void *database)
+event get_event(int socket, OBUF *event_buffer, void *database)
 {
 #ifdef T_SEND_TIME
   struct timespec t;
@@ -29,17 +29,23 @@ again:
 #endif
   if (fullread(socket, &type, sizeof(int)) == -1) goto read_error;
   length -= sizeof(int);
-  if (fullread(socket, event_buffer, length) == -1) goto read_error;
+  if (event_buffer->omaxsize < length) {
+    event_buffer->omaxsize = (length + 65535) & ~65535;
+    event_buffer->obuf = realloc(event_buffer->obuf, event_buffer->omaxsize);
+    if (event_buffer->obuf == NULL) { printf("out of memory\n"); exit(1); }
+  }
+  if (fullread(socket, event_buffer->obuf, length) == -1) goto read_error;
+  event_buffer->osize = length;
 
-  if (type == -1) append_received_config_chunk(event_buffer, length);
+  if (type == -1) append_received_config_chunk(event_buffer->obuf, length);
   if (type == -2) verify_config();
 
   if (type == -1 || type == -2) goto again;
 
 #ifdef T_SEND_TIME
-  return new_event(t, type, length, event_buffer, database);
+  return new_event(t, type, length, event_buffer->obuf, database);
 #else
-  return new_event(type, length, event_buffer, database);
+  return new_event(type, length, event_buffer->obuf, database);
 #endif
 
 read_error:
diff --git a/common/utils/T/tracer/event.h b/common/utils/T/tracer/event.h
index 6272baf1a8f..0170ab6680c 100644
--- a/common/utils/T/tracer/event.h
+++ b/common/utils/T/tracer/event.h
@@ -1,6 +1,7 @@
 #ifndef _EVENT_H_
 #define _EVENT_H_
 
+#include "utils.h"
 #include "../T_defs.h"
 #ifdef T_SEND_TIME
 #include <time.h>
@@ -37,7 +38,7 @@ typedef struct {
   int ecount;
 } event;
 
-event get_event(int s, char *v, void *d);
+event get_event(int s, OBUF *v, void *d);
 
 #ifdef T_SEND_TIME
 event new_event(struct timespec sending_time, int type,
diff --git a/common/utils/T/tracer/extract.c b/common/utils/T/tracer/extract.c
index b9fca959edd..2aec38831fe 100644
--- a/common/utils/T/tracer/extract.c
+++ b/common/utils/T/tracer/extract.c
@@ -99,10 +99,11 @@ int main(int n, char **v)
 
   found = 0;
 
+  OBUF ebuf = { osize: 0, omaxsize: 0, obuf: NULL };
+
   while (1) {
-    char v[T_BUFFER_MAX];
     event e;
-    e = get_event(fd, v, database);
+    e = get_event(fd, &ebuf, database);
     if (e.type == -1) break;
     if (e.type != input_event_id) continue;
     for (i = 0; i < filter_count; i++)
diff --git a/common/utils/T/tracer/extract_config.c b/common/utils/T/tracer/extract_config.c
index f35d765cec5..a26c52aeda2 100644
--- a/common/utils/T/tracer/extract_config.c
+++ b/common/utils/T/tracer/extract_config.c
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include "utils.h"
 #include "../T_defs.h"
 
 void usage(void)
@@ -35,14 +36,22 @@ int main(int n, char **v)
   in = fopen(input_filename, "r");
   if (in == NULL) { perror(input_filename); abort(); }
 
+  OBUF ebuf = { osize: 0, omaxsize: 0, obuf: NULL };
+
   while (1) {
     int type;
     int32_t length;
-    char v[T_BUFFER_MAX];
+    char *v;
     int vpos = 0;
 
     /* read event from file */
     if (fread(&length, 4, 1, in) != 1) break;
+    if (ebuf.omaxsize < length) {
+      ebuf.omaxsize = (length + 65535) & ~65535;
+      ebuf.obuf = realloc(ebuf.obuf, ebuf.omaxsize);
+      if (ebuf.obuf == NULL) { printf("out of memory\n"); exit(1); }
+    }
+    v = ebuf.obuf;
     memcpy(v+vpos, &length, 4);
     vpos += 4;
 #ifdef T_SEND_TIME
diff --git a/common/utils/T/tracer/extract_input_subframe.c b/common/utils/T/tracer/extract_input_subframe.c
index 02c5f3d5650..d7423182018 100644
--- a/common/utils/T/tracer/extract_input_subframe.c
+++ b/common/utils/T/tracer/extract_input_subframe.c
@@ -104,11 +104,12 @@ err:
   fd = open(file, O_RDONLY);
   if (fd == -1) { perror(file); exit(1); }
 
+  OBUF ebuf = { osize: 0, omaxsize: 0, obuf: NULL };
+
   /* get wanted frame/subframe */
   while (1) {
-    char v[T_BUFFER_MAX];
     event e;
-    e = get_event(fd, v, database);
+    e = get_event(fd, &ebuf, database);
     if (e.type == -1) break;
     if (e.type != input_event_id) continue;
     if (verbose)
diff --git a/common/utils/T/tracer/extract_output_subframe.c b/common/utils/T/tracer/extract_output_subframe.c
index b82ad0e7665..d24393553ce 100644
--- a/common/utils/T/tracer/extract_output_subframe.c
+++ b/common/utils/T/tracer/extract_output_subframe.c
@@ -100,10 +100,12 @@ err:
   int last_frame = -1;
   int last_subframe = -1;
   int subframe_written = 0;
+
+  OBUF ebuf = { osize: 0, omaxsize: 0, obuf: NULL };
+
   while (1) {
-    char v[T_BUFFER_MAX];
     event e;
-    e = get_event(fd, v, database);
+    e = get_event(fd, &ebuf, database);
     if (e.type == -1) break;
     if (e.type != output_event_id) continue;
     if (verbose)
diff --git a/common/utils/T/tracer/hacks/dump_nack_signal.c b/common/utils/T/tracer/hacks/dump_nack_signal.c
index 1627bdd14e9..bb8f1a9477b 100644
--- a/common/utils/T/tracer/hacks/dump_nack_signal.c
+++ b/common/utils/T/tracer/hacks/dump_nack_signal.c
@@ -73,17 +73,22 @@ int main(int n, char **v)
       socket_send(socket, is_on, number_of_events * sizeof(int)) == -1)
     abort();
 
+  OBUF ebuf = { osize: 0, omaxsize: 0, obuf: NULL };
+
   char dump[10][T_BUFFER_MAX];
   event dump_ev[10];
 FILE *z = fopen("/tmp/dd", "w"); if (z == NULL) abort();
   while (1) {
-    char v[T_BUFFER_MAX];
+    char *v;
     event e;
-    e = get_event(socket, v, database);
+    e = get_event(socket, &ebuf, database);
+    v = ebuf.obuf;
     if (e.type == -1) break;
     if (e.type == ev_input) {
       int sf = e.e[2].i;
-      memcpy(dump[sf], v, T_BUFFER_MAX);
+      if (ebuf.osize > T_BUFFER_MAX)
+        { printf("event size too big\n"); exit(1); }
+      memcpy(dump[sf], ebuf.obuf, ebuf.osize);
       dump_ev[sf] = e;
       printf("input %d/%d\n", e.e[1].i, sf);
 if (fwrite(dump_ev[sf].e[4].b, dump_ev[sf].e[4].bsize, 1, z) != 1) abort();
diff --git a/common/utils/T/tracer/hacks/time_meas.c b/common/utils/T/tracer/hacks/time_meas.c
index 04d4fa5acfc..6bd29503011 100644
--- a/common/utils/T/tracer/hacks/time_meas.c
+++ b/common/utils/T/tracer/hacks/time_meas.c
@@ -84,11 +84,12 @@ int main(int n, char **v)
       socket_send(socket, is_on, number_of_events * sizeof(int)) == -1)
     abort();
 
+  OBUF ebuf = { osize: 0, omaxsize: 0, obuf: NULL };
+
   while (1) {
-    char v[T_BUFFER_MAX];
     event e;
     int on_off;
-    e = get_event(socket, v, database);
+    e = get_event(socket, &ebuf, database);
     if (e.type == -1) break;
     if (e.type != ev_fun)
       { printf("unhandled event %d\n", e.type); continue; }
diff --git a/common/utils/T/tracer/macpdu2wireshark.c b/common/utils/T/tracer/macpdu2wireshark.c
index be0bbd38214..954f668aa42 100644
--- a/common/utils/T/tracer/macpdu2wireshark.c
+++ b/common/utils/T/tracer/macpdu2wireshark.c
@@ -246,11 +246,12 @@ int main(int n, char **v)
 
   new_thread(receiver, &d);
 
+  OBUF ebuf = { osize: 0, omaxsize: 0, obuf: NULL };
+
   /* read messages */
   while (1) {
-    char v[T_BUFFER_MAX];
     event e;
-    e = get_event(in, v, database);
+    e = get_event(in, &ebuf, database);
     if (e.type == -1) break;
     if (!(e.type == ul_id || e.type == dl_id)) continue;
     handle_event(h, e);
diff --git a/common/utils/T/tracer/record.c b/common/utils/T/tracer/record.c
index b1a1bc60de8..a565884e52b 100644
--- a/common/utils/T/tracer/record.c
+++ b/common/utils/T/tracer/record.c
@@ -123,14 +123,22 @@ int main(int n, char **v)
   if (signal(SIGINT, force_stop) == SIG_ERR) abort();
   if (signal(SIGTSTP, force_stop) == SIG_ERR) abort();
 
+  OBUF ebuf = { osize: 0, omaxsize: 0, obuf: NULL };
+
   /* read messages */
   while (run) {
     int type;
     int32_t length;
-    char v[T_BUFFER_MAX];
+    char *v;
     int vpos = 0;
 
     if (fullread(socket, &length, 4) == -1) goto read_error;
+    if (ebuf.omaxsize < length) {
+      ebuf.omaxsize = (length + 65535) & ~65535;
+      ebuf.obuf = realloc(ebuf.obuf, ebuf.omaxsize);
+      if (ebuf.obuf == NULL) { printf("out of memory\n"); exit(1); }
+    }
+    v = ebuf.obuf;
     memcpy(v+vpos, &length, 4);
     vpos += 4;
 #ifdef T_SEND_TIME
diff --git a/common/utils/T/tracer/replay.c b/common/utils/T/tracer/replay.c
index be7e8d60c69..c199a668d58 100644
--- a/common/utils/T/tracer/replay.c
+++ b/common/utils/T/tracer/replay.c
@@ -136,14 +136,22 @@ int main(int n, char **v)
 
   new_thread(get_message_thread, &socket);
 
+  OBUF ebuf = { osize: 0, omaxsize: 0, obuf: NULL };
+
   while (1) {
     int type;
     int32_t length;
-    char v[T_BUFFER_MAX];
+    char *v;
     int vpos = 0;
 
     /* read event from file */
     if (fread(&length, 4, 1, in) != 1) break;
+    if (ebuf.omaxsize < length) {
+      ebuf.omaxsize = (length + 65535) & ~65535;
+      ebuf.obuf = realloc(ebuf.obuf, ebuf.omaxsize);
+      if (ebuf.obuf == NULL) { printf("out of memory\n"); exit(1); }
+    }
+    v = ebuf.obuf;
     memcpy(v+vpos, &length, 4);
     vpos += 4;
 #ifdef T_SEND_TIME
diff --git a/common/utils/T/tracer/textlog.c b/common/utils/T/tracer/textlog.c
index 32bf1ed791c..bb773b42150 100644
--- a/common/utils/T/tracer/textlog.c
+++ b/common/utils/T/tracer/textlog.c
@@ -10,7 +10,6 @@
 #include "view/view.h"
 #include "gui/gui.h"
 #include "utils.h"
-#include "../T_defs.h"
 #include "event_selector.h"
 #include "config.h"
 
@@ -182,11 +181,12 @@ int main(int n, char **v)
   /* send the first message - activate selected traces */
   is_on_changed(&textlog_data);
 
+  OBUF ebuf = { osize: 0, omaxsize: 0, obuf: NULL };
+
   /* read messages */
   while (1) {
-    char v[T_BUFFER_MAX];
     event e;
-    e = get_event(textlog_data.socket, v, database);
+    e = get_event(textlog_data.socket, &ebuf, database);
     if (e.type == -1) abort();
     handle_event(h, e);
   }
diff --git a/common/utils/T/tracer/to_vcd.c b/common/utils/T/tracer/to_vcd.c
index 82cd3393d7e..097fdce9321 100644
--- a/common/utils/T/tracer/to_vcd.c
+++ b/common/utils/T/tracer/to_vcd.c
@@ -287,11 +287,12 @@ int main(int n, char **v)
   if (signal(SIGINT, force_stop) == SIG_ERR) abort();
   if (signal(SIGTSTP, force_stop) == SIG_ERR) abort();
 
+  OBUF ebuf = { osize: 0, omaxsize: 0, obuf: NULL };
+
   /* read messages */
   while (run) {
-    char v[T_BUFFER_MAX];
     event e;
-    e = get_event(socket, v, database);
+    e = get_event(socket, &ebuf, database);
     if (e.type == -1) { printf("disconnected? let's quit gently\n"); break; }
     handle_event(h, e);
   }
diff --git a/common/utils/T/tracer/ue.c b/common/utils/T/tracer/ue.c
index 5709a4e79e0..6a7d03c8973 100644
--- a/common/utils/T/tracer/ue.c
+++ b/common/utils/T/tracer/ue.c
@@ -12,7 +12,6 @@
 #include "gui/gui.h"
 #include "filter/filter.h"
 #include "utils.h"
-#include "../T_defs.h"
 #include "event_selector.h"
 #include "openair_logo.h"
 #include "config.h"
@@ -854,6 +853,8 @@ int main(int n, char **v)
   if (pthread_mutex_init(&ue_data.lock, NULL)) abort();
   setup_event_selector(g, database, is_on, is_on_changed, &ue_data);
 
+  OBUF ebuf = { osize: 0, omaxsize: 0, obuf: NULL };
+
 restart:
   clear_remote_config();
   ue_data.socket = connect_to(ip, port);
@@ -863,9 +864,8 @@ restart:
 
   /* read messages */
   while (1) {
-    char v[T_BUFFER_MAX];
     event e;
-    e = get_event(ue_data.socket, v, database);
+    e = get_event(ue_data.socket, &ebuf, database);
     if (e.type == -1) goto restart;
     if (pthread_mutex_lock(&ue_data.lock)) abort();
     handle_event(h, e);
diff --git a/common/utils/T/tracer/vcd.c b/common/utils/T/tracer/vcd.c
index 0ad2756c2a4..d4faa5fd847 100644
--- a/common/utils/T/tracer/vcd.c
+++ b/common/utils/T/tracer/vcd.c
@@ -10,7 +10,6 @@
 #include "view/view.h"
 #include "gui/gui.h"
 #include "utils.h"
-#include "../T_defs.h"
 #include "event_selector.h"
 #include "config.h"
 
@@ -188,11 +187,12 @@ int main(int n, char **v)
   /* send the first message - activate selected traces */
   is_on_changed(&vcd_data);
 
+  OBUF ebuf = { osize: 0, omaxsize: 0, obuf: NULL };
+
   /* read messages */
   while (1) {
-    char v[T_BUFFER_MAX];
     event e;
-    e = get_event(vcd_data.socket, v, database);
+    e = get_event(vcd_data.socket, &ebuf, database);
     if (e.type == -1) abort();
     handle_event(h, e);
   }
-- 
GitLab