summaryrefslogtreecommitdiffstats
path: root/tester/covoar/coverage_converter.cc
blob: a0313957dc25f0682324485c301715a89be012d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "CoverageFactory.h"
#include "CoverageMap.h"

/*
 *  Variables to control global behavior
 */
Coverage::CoverageReaderBase* coverageReader = NULL;
Coverage::CoverageWriterBase* coverageWriter = NULL;
uint32_t                      highAddress = 0xffffffff;
Coverage::CoverageFormats_t   inputFormat;
uint32_t                      lowAddress  = 0xffffffff;
char*                         progname;

/*
 *  Global variables for the program
 */
Coverage::CoverageMapBase    *CoverageMap    = NULL;

/*
 *  Convert string to int with status out
 */

bool stringToUint32(
  const char* s,
  int         base,
  uint32_t*   n
)
{
  long long result;

  if (!n)
    return false;

  errno = 0;
  *n    = 0;

  result = strtoll( s, NULL, base );

  if ((result == 0) && errno)
    return false;

  if ((result == LLONG_MAX) && (errno == ERANGE))
    return false;

  if ((result == LLONG_MIN) && (errno == ERANGE))
    return false;

  *n = (uint32_t)result;
  return true;
}

/*
 *  Print program usage message
 */
void usage()
{
  fprintf(
    stderr,
    "Usage: %s [-v] -l ADDRESS -h ADDRESS coverage_in coverage_out\n"
    "\n"
    "  -l low address   - low address of range to merge\n"
    "  -l high address  - high address of range to merge\n"
    "  -f format        - coverage files are in <format> (Qemu)\n"
    "  -v               - verbose at initialization\n"
    "\n",
    progname
  );
}

#define PrintableString(_s) \
       ((!(_s)) ? "NOT SET" : (_s))

int main(
  int    argc,
  char** argv
)
{
  char*       format = NULL;
  const char* coverageFile;
  const char* coverageIn;
  int         opt;

  //
  // Process command line options.
  //
  progname = argv[0];

  while ((opt = getopt(argc, argv, "f:h:l:v")) != -1) {
    switch (opt) {
      case 'v': Verbose = 1;       break;
      case 'f':
        inputFormat = Coverage::CoverageFormatToEnum(optarg);
        format = optarg;
        break;
      case 'l':
        if (!stringToUint32( optarg, 16, &lowAddress )) {
          fprintf( stderr, "ERROR: Low address is not a hexadecimal number\n" );
          usage();
          exit(-1);
        }
        break;
      case 'h':
        if (!stringToUint32( optarg, 16, &highAddress )) {
          fprintf( stderr, "ERROR: High address is not a hexadecimal number\n" );
          usage();
          exit(-1);
        }
        break;
      default: /* '?' */
        usage();
        exit( -1 );
    }
  }

  if ((argc - optind) != 2) {
    fprintf( stderr, "ERROR: Must provide input and output files\n" );
    exit(1);
  }

  coverageIn   = argv[optind];
  coverageFile = argv[optind + 1];

  if (Verbose) {
    fprintf( stderr, "Verbose       : %d\n", Verbose );
    fprintf( stderr, "Input Format  : %s\n", format );
    fprintf( stderr, "Input File    : %s\n", coverageIn );
    fprintf( stderr, "Output Format : %s\n", "RTEMS" );
    fprintf( stderr, "Output File   : %s\n", coverageFile );
    fprintf( stderr, "low address   : 0x%08x\n", lowAddress );
    fprintf( stderr, "high address  : 0x%08x\n", highAddress );
    fprintf( stderr, "\n" );
  }

  //
  // Validate inputs.
  //

  // Validate format.
  if (!format) {
    fprintf( stderr, "ERROR: input format must be given.\n\n" );
    usage();
    exit(-1);
  }

  // Validate address range
  if (lowAddress == 0xffffffff) {
    fprintf( stderr, "ERROR: Low address not specified.\n\n" );
    usage();
    exit(-1);
  }

  if (highAddress == 0xffffffff) {
    fprintf( stderr, "ERROR: High address not specified.\n\n" );
    usage();
    exit(-1);
  }

  if (lowAddress >= highAddress) {
    fprintf( stderr, "ERROR: Low address >= high address.\n\n" );
    usage();
    exit(-1);
  }

  //
  // Create data to support conversion.
  //

  // Create coverage map.
  CoverageMap = new Coverage::CoverageMap( lowAddress, highAddress );
  if (!CoverageMap) {
    fprintf( stderr, "ERROR: Unable to create coverage map.\n\n" );
    exit(-1);
  }

  // Create coverage writer.
  coverageWriter =
    Coverage::CreateCoverageWriter(Coverage::COVERAGE_FORMAT_RTEMS);
  if (!coverageWriter) {
    fprintf( stderr, "ERROR: Unable to create coverage file writer.\n\n" );
    exit(-1);
  }

  // Create coverage reader.
  coverageReader = CreateCoverageReader( inputFormat );
  if (!coverageReader) {
    fprintf( stderr, "ERROR: Unable to create input file reader.\n\n" );
    exit(-1);
  }

  // Now get to some real work.
  if (Verbose)
    fprintf( stderr, "Processing %s\n", coverageIn );
  coverageReader->processFile( coverageIn, CoverageMap );

  if (Verbose)
    fprintf(
      stderr, "Writing coverage file (%s)\n", coverageFile );
  coverageWriter->writeFile(
    coverageFile,
    CoverageMap,
    lowAddress,
    highAddress
  );

  return 0;
}