Read the 'magic' portion of 'struct blk_io_trace' first when reading the
tracefile and only if all magic checks succeed, read the rest of the
trace.

This is a preparation of supporting multiple trace protocol versions.

Signed-off-by: Johannes Thumshirn <[email protected]>
---
 blkparse.c | 44 ++++++++++++++++++++++++++++++++++----------
 1 file changed, 34 insertions(+), 10 deletions(-)

diff --git a/blkparse.c b/blkparse.c
index d58322c..5645c31 100644
--- a/blkparse.c
+++ b/blkparse.c
@@ -2438,14 +2438,13 @@ static int read_events(int fd, int always_block, int 
*fdblock)
                struct trace *t;
                int pdu_len, should_block, ret;
                __u32 magic;
-
-               bit = bit_alloc();
+               void *p;
 
                should_block = !events || always_block;
 
-               ret = read_data(fd, bit, sizeof(*bit), should_block, fdblock);
+               ret = read_data(fd, &magic, sizeof(magic), should_block,
+                               fdblock);
                if (ret) {
-                       bit_free(bit);
                        if (!events && ret < 0)
                                events = ret;
                        break;
@@ -2455,15 +2454,28 @@ static int read_events(int fd, int always_block, int 
*fdblock)
                 * look at first trace to check whether we need to convert
                 * data in the future
                 */
-               if (data_is_native == -1 && check_data_endianness(bit->magic))
+               if (data_is_native == -1 && check_data_endianness(magic))
                        break;
 
-               magic = get_magic(bit->magic);
+               magic = get_magic(magic);
                if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
                        fprintf(stderr, "Bad magic %x\n", magic);
                        break;
                }
 
+               bit = bit_alloc();
+               bit->magic = magic;
+               p = (void *) ((u8 *)bit + sizeof(magic));
+
+               ret = read_data(fd, p, sizeof(*bit) - sizeof(magic),
+                               should_block, fdblock);
+               if (ret) {
+                       bit_free(bit);
+                       if (!events && ret < 0)
+                               events = ret;
+                       break;
+               }
+
                pdu_len = get_pdulen(bit);
                if (pdu_len) {
                        void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
@@ -2596,20 +2608,30 @@ static int ms_prime(struct ms_stream *msp)
        int ret, pdu_len, ndone = 0;
 
        for (i = 0; !is_done() && pci->fd >= 0 && i < rb_batch; i++) {
-               bit = bit_alloc();
-               ret = read_data(pci->fd, bit, sizeof(*bit), 1, &pci->fdblock);
+               void *p;
+
+               ret = read_data(pci->fd, &magic, sizeof(magic), 1,
+                               &pci->fdblock);
                if (ret)
                        goto err;
 
-               if (data_is_native == -1 && check_data_endianness(bit->magic))
+               if (data_is_native == -1 && check_data_endianness(magic))
                        goto err;
 
-               magic = get_magic(bit->magic);
+               magic = get_magic(magic);
                if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
                        fprintf(stderr, "Bad magic %x\n", magic);
                        goto err;
 
                }
+               bit = bit_alloc();
+               bit->magic = magic;
+               p = (void *) ((u8 *)bit + sizeof(magic));
+
+               ret = read_data(pci->fd, p, sizeof(*bit) - sizeof(magic), 1,
+                               &pci->fdblock);
+               if (ret)
+                       goto err;
 
                pdu_len = get_pdulen(bit);
                if (pdu_len) {
@@ -2639,6 +2661,7 @@ static int ms_prime(struct ms_stream *msp)
                        handle_notify(bit);
                        output_binary(bit, sizeof(*bit) + bit->pdu_len);
                        bit_free(bit);
+                       bit = NULL;
 
                        i -= 1;
                        continue;
@@ -2659,6 +2682,7 @@ static int ms_prime(struct ms_stream *msp)
                }
 
                ndone++;
+               bit = NULL;
        }
 
        return ndone;
-- 
2.51.0


Reply via email to