[PATCH rtems-tools v3 2/7] TraceReader: Convert to C++

Ryan Long ryan.long at oarcorp.com
Mon Sep 27 12:49:43 UTC 2021


---
 tester/covoar/ObjdumpProcessor.cc   |   4 +-
 tester/covoar/ObjdumpProcessor.h    |   2 +-
 tester/covoar/TargetBase.cc         |   6 +-
 tester/covoar/TargetBase.h          |   4 +-
 tester/covoar/TraceReaderBase.h     |   4 +-
 tester/covoar/TraceReaderLogQEMU.cc | 128 ++++++++++++++++--------------------
 tester/covoar/TraceReaderLogQEMU.h  |   2 +-
 tester/covoar/qemu-log.h            |   4 +-
 8 files changed, 68 insertions(+), 86 deletions(-)

diff --git a/tester/covoar/ObjdumpProcessor.cc b/tester/covoar/ObjdumpProcessor.cc
index 4d1e306..c910046 100644
--- a/tester/covoar/ObjdumpProcessor.cc
+++ b/tester/covoar/ObjdumpProcessor.cc
@@ -188,9 +188,7 @@ namespace Coverage {
     #undef METHOD
   }
 
-  bool ObjdumpProcessor::IsBranch(
-    const char *instruction
-  )
+  bool ObjdumpProcessor::IsBranch( const std::string& instruction )
   {
     if ( !targetInfo_m ) {
       fprintf(
diff --git a/tester/covoar/ObjdumpProcessor.h b/tester/covoar/ObjdumpProcessor.h
index 6a207dd..05a667f 100644
--- a/tester/covoar/ObjdumpProcessor.h
+++ b/tester/covoar/ObjdumpProcessor.h
@@ -143,7 +143,7 @@ namespace Coverage {
      *  an instruction that results in a code branch, otherwise
      *  it returns false.
      */
-    bool IsBranch( const char *instruction );
+    bool IsBranch( const std::string& instruction );
 
     /*!
      *  This method returns true if the instruction from
diff --git a/tester/covoar/TargetBase.cc b/tester/covoar/TargetBase.cc
index 4474fad..7ee45b5 100644
--- a/tester/covoar/TargetBase.cc
+++ b/tester/covoar/TargetBase.cc
@@ -60,9 +60,7 @@ namespace Target {
     return targetName_m.c_str();
   }
 
-  bool TargetBase::isBranch(
-      const char* const instruction
-  )
+  bool TargetBase::isBranch( const std::string& instruction )
   {
     std::list <std::string>::iterator i;
 
@@ -76,7 +74,7 @@ namespace Target {
     i = find(
       conditionalBranchInstructions.begin(),
       conditionalBranchInstructions.end(),
-      instruction
+      instruction.c_str()
     );
     if ( i  == conditionalBranchInstructions.end() )
       return false;
diff --git a/tester/covoar/TargetBase.h b/tester/covoar/TargetBase.h
index 6db08f2..e5c143e 100644
--- a/tester/covoar/TargetBase.h
+++ b/tester/covoar/TargetBase.h
@@ -98,9 +98,7 @@ namespace Target {
      *  This method determines if the specified line from an
      *  objdump file is a branch instruction.
      */
-    bool isBranch(
-      const char* const instruction
-    );
+    bool isBranch( const std::string& instruction );
 
     /*!
      *  This method returns the bit set by Qemu in the trace record
diff --git a/tester/covoar/TraceReaderBase.h b/tester/covoar/TraceReaderBase.h
index 3005c62..cbb9a62 100644
--- a/tester/covoar/TraceReaderBase.h
+++ b/tester/covoar/TraceReaderBase.h
@@ -7,6 +7,8 @@
 #ifndef __TRACE_READER_BASE_H__
 #define __TRACE_READER_BASE_H__
 
+#include <string>
+
 #include "TraceList.h"
 #include "ObjdumpProcessor.h"
 
@@ -47,7 +49,7 @@ namespace Trace {
      *  @return Returns TRUE if the method succeeded and FALSE if it failed.
      */
     virtual bool processFile(
-      const char* const           file,
+      const std::string&          file,
       Coverage::ObjdumpProcessor& objdumpProcessor
     ) = 0;
   };
diff --git a/tester/covoar/TraceReaderLogQEMU.cc b/tester/covoar/TraceReaderLogQEMU.cc
index 57bcaa1..d02b555 100644
--- a/tester/covoar/TraceReaderLogQEMU.cc
+++ b/tester/covoar/TraceReaderLogQEMU.cc
@@ -40,6 +40,9 @@
 #include <sys/stat.h>
 #include <string.h>
 
+#include <iostream>
+#include <fstream>
+
 #include "qemu-log.h"
 #include "TraceReaderBase.h"
 #include "TraceReaderLogQEMU.h"
@@ -48,32 +51,27 @@
 
 #include "rld-process.h"
 
-#if HAVE_STAT64
-#define STAT stat64
-#else
-#define STAT stat
-#endif
-
-#if HAVE_OPEN64
-#define OPEN fopen64
-#else
-#define OPEN fopen
-#endif
-
-#define MAX_LINE_LENGTH 512
-
-bool ReadUntilFound( FILE *file, const char *line )
+bool ReadUntilFound( std::ifstream& file, const char* line )
 {
-  char discardBuff[100];
-  size_t  len = strlen( line );
+  char   discardBuff[100] = {};
+  size_t len = strlen( line );
+
+  if ( len > sizeof( discardBuff ) ) {
+    std::cerr << "ReadUntilFound(): parameter 'line' is too long" << std::endl;
+    return false;
+  }
 
   do {
-    if ( !fgets( discardBuff, 99, file ) )
+    file.read( discardBuff, sizeof( discardBuff ) - 2 );
+    if ( file.fail() ) {
       return false;
+    }
 
-    if ( strncmp( discardBuff, line, len ) == 0 )
+    if ( strncmp( discardBuff, line, len ) == 0 ) {
       return true;
-  } while (1);
+    }
+
+  } while( true );
 }
 
 namespace Trace {
@@ -87,7 +85,7 @@ namespace Trace {
   }
 
   bool TraceReaderLogQEMU::processFile(
-    const char* const           file,
+    const std::string&          file,
     Coverage::ObjdumpProcessor& objdumpProcessor
   )
   {
@@ -96,42 +94,41 @@ namespace Trace {
     QEMU_LOG_IN_Block_t last          = { 0, "", "" };
     QEMU_LOG_IN_Block_t nextExecuted  = { 0, "", "" };
     uint32_t            nextlogical;
-    struct STAT         statbuf;
     int                 status;
-    FILE*               logFile;
+    std::ifstream       logFile;
     int                 result;
-    char                inputBuffer[MAX_LINE_LENGTH];
+    int                 fileSize;
+    char                ignore;
 
     //
     // Verify that the log file has a non-zero size.
     //
-    // NOTE: We prefer stat64 because some of the coverage files are HUGE!
-    status = STAT( file, &statbuf );
-    if (status == -1) {
-      fprintf( stderr, "Unable to stat %s\n", file );
+    logFile.open( file, std::ifstream::in | std::ifstream::binary );
+    if ( !logFile.is_open() ) {
+      std::cerr << "Unable to open " << file << std::endl;
       return false;
     }
 
-    if (statbuf.st_size == 0) {
-      fprintf( stderr, "%s is 0 bytes long\n", file );
+    logFile.seekg( 0, std::ios::end );
+    fileSize = logFile.tellg();
+
+    if ( fileSize == 0 ) {
+      std::cerr << file << " is 0 bytes long" << std::endl;
       return false;
     }
 
+    logFile.close();
+
     //
     // Open the coverage file and discard the header.
     //
-    logFile = OPEN( file, "r" );
-    if (!logFile) {
-      fprintf( stderr, "Unable to open %s\n", file );
-      return false;
-    }
+    logFile.open( file );
 
     //
     //  Discard Header section
     //
     if (! ReadUntilFound( logFile, QEMU_LOG_SECTION_END ) ) {
-      fprintf( stderr, "Unable to locate end of log file header\n" );
-      fclose( logFile );
+      std::cerr << "Unable to locate end of log file header" << std::endl;
       return false;
     }
 
@@ -139,25 +136,22 @@ namespace Trace {
     //  Find first IN block
     //
     if (! ReadUntilFound( logFile, QEMU_LOG_IN_KEY )){
-      fprintf(stderr,"Error: Unable to locate first IN: Block in Log file \n");
-      fclose( logFile );
+      std::cerr << "Error: Unable to locate first IN: Block in Log file"
+                << std::endl;
       return false;
     }
 
     //
     //  Read First Start Address
     //
-    fgets(inputBuffer, MAX_LINE_LENGTH, logFile );
-    result = sscanf(
-      inputBuffer,
-      "0x%08lx: %s %s\n",
-      &first.address,
-      first.instruction,
-      first.data
-    );
-    if ( result < 2 )
-    {
-      fprintf(stderr, "Error Unable to Read Initial First Block\n" );
+    logFile >> std::hex >> first.address >> std::dec
+            >> ignore
+            >> first.instruction
+            >> first.data;
+
+    if ( logFile.fail() ) {
+      std::cerr << "Error Unable to Read Initial First Block"
+                << std::endl;
       done = true;
     }
 
@@ -167,15 +161,11 @@ namespace Trace {
 
       // Read until we get to the last instruction in the block.
       do {
-        fgets(inputBuffer, MAX_LINE_LENGTH, logFile );
-        result = sscanf(
-          inputBuffer,
-          "0x%08lx: %s %s\n",
-          &last.address,
-          last.instruction,
-          last.data
-        );
-      } while( result > 1);
+        logFile >> std::hex >> last.address >> std::dec
+                >> ignore
+                >> last.instruction
+                >> last.data;
+      } while( !logFile.fail() );
 
       nextlogical = objdumpProcessor.getAddressAfter(last.address);
 
@@ -183,17 +173,13 @@ namespace Trace {
         done = true;
         nextExecuted = last;
       } else {
-        fgets(inputBuffer, MAX_LINE_LENGTH, logFile );
-        result = sscanf(
-          inputBuffer,
-          "0x%08lx: %s %s\n",
-          &nextExecuted.address,
-          nextExecuted.instruction,
-          nextExecuted.data
-        );
-        if ( result < 2 )
-        {
-          fprintf(stderr, "Error Unable to Read First Block\n" );
+        logFile >> std::hex >> nextExecuted.address >> std::dec
+                >> ignore
+                >> nextExecuted.instruction
+                >> nextExecuted.data;
+
+        if ( logFile.fail() ) {
+          std::cerr << "Error Unable to Read First Block" << std::endl;
         }
       }
 
@@ -213,7 +199,7 @@ namespace Trace {
       }
       first = nextExecuted;
     }
-    fclose( logFile );
+
     return true;
   }
 }
diff --git a/tester/covoar/TraceReaderLogQEMU.h b/tester/covoar/TraceReaderLogQEMU.h
index 8ee5651..59b5d23 100644
--- a/tester/covoar/TraceReaderLogQEMU.h
+++ b/tester/covoar/TraceReaderLogQEMU.h
@@ -43,7 +43,7 @@ namespace Trace {
      *  @return Returns TRUE if the method succeeded and FALSE if it failed.
      */
     virtual bool processFile(
-      const char* const           file,
+      const std::string&          file,
       Coverage::ObjdumpProcessor& objdumpProcessor
     );
   };
diff --git a/tester/covoar/qemu-log.h b/tester/covoar/qemu-log.h
index cbca05e..e9c89dc 100644
--- a/tester/covoar/qemu-log.h
+++ b/tester/covoar/qemu-log.h
@@ -15,8 +15,8 @@
  */
 typedef struct {
   unsigned long address;
-  char          instruction[10];
-  char          data[20];
+  std::string   instruction;
+  std::string   data;
 }  QEMU_LOG_IN_Block_t;
 
 
-- 
1.8.3.1



More information about the devel mailing list