ProfMerge_main.cc 6.72 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
/******************************************************************************
balaskoa's avatar
balaskoa committed
2
 * Copyright (c) 2000-2018 Ericsson Telecom AB
Elemer Lelik's avatar
Elemer Lelik committed
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v2.0
Elemer Lelik's avatar
Elemer Lelik committed
5
 * which accompanies this distribution, and is available at
6
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
Elemer Lelik's avatar
Elemer Lelik committed
7
8
9
10
11
12
 *
 * Contributors:
 *   Balasko, Jeno
 *   Baranyi, Botond
 *
 ******************************************************************************/
Elemer Lelik's avatar
Elemer Lelik committed
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

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <getopt.h>
#include <string.h>
#include "ProfilerTools.hh"
#include "version_internal.h"

#ifdef LICENSE
#include "license.h"
#endif

/** the name of the executable */
const char* program_name;

/** error flag, set automatically when an error occurs */
boolean erroneous = FALSE;

/** prints usage information */
void usage()
{
  fprintf(stderr,
    "usage:	%s [-pc] [-o file] [-s file] [-f filter] db_file1 [db_file2 ...]\n"
    "	or %s -v\n\n"
    "options:\n"
    "	-p:		discard profiling data\n"
    "	-c:		discard code coverage data\n"
    "	-o file:	write merged database into file\n"
    "	-s file:	generate statistics file from merged database\n"
    "	-f filter:	filter the generated statistics file (the filter is a hexadecimal number)\n"
    "	-v:		show version\n", program_name, program_name);
}

/** displays an error message and sets the erroneous flag */
void error(const char *fmt, ...)
{
  fprintf(stderr, "%s: error: ", program_name);
  va_list parameters;
  va_start(parameters, fmt);
  vfprintf(stderr, fmt, parameters);
  va_end(parameters);
  putc('\n', stderr);
  fflush(stderr);
  erroneous = TRUE;
}

/** checks if the specified file exists */
boolean file_exists(const char* p_filename)
{
  FILE* file = fopen(p_filename, "r");
  if (NULL != file) {
    fclose(file);
    return TRUE;
  }
  return FALSE;
}


int main(int argc, char* argv[])
{
  // store the executable name
  program_name = argv[0];
  
  // initialize variables for command line options
  const char* out_file = NULL;
  const char* stats_file = NULL;
  boolean disable_profiler = FALSE;
  boolean disable_coverage = FALSE;
  unsigned int stats_flags = Profiler_Tools::STATS_ALL;
  boolean has_stats_flag = FALSE;
  boolean print_version = FALSE;
  
  if (1 == argc) {
    // no command line options
    usage();
    return EXIT_FAILURE;
  }
  
  for (;;) {
    // read the next command line option (and its argument)
    int c = getopt(argc, argv, "o:s:pcf:v");
    if (-1 == c) {
      break;
    }
    switch (c) {
    case 'o': // output database file
      out_file = optarg;
      break;
    case 's': // statistics file
      stats_file = optarg;
      break;
    case 'p':
      disable_profiler = TRUE;
      break;
    case 'c':
      disable_coverage = TRUE;
      break;
    case 'f': { // statistics filter (hex number)
      has_stats_flag = TRUE;
      size_t len = strlen(optarg);
      size_t start = 0;
      if (len > STATS_MAX_HEX_DIGITS) {
        // the rest of the bits are not needed, and stats_flags might run out of bits
        start = len - STATS_MAX_HEX_DIGITS;
      }
      stats_flags = 0;
      // extract the hex digits from the argument
      for (size_t i = start; i < len; ++i) {
        stats_flags *= 16;
        if ('0' <= optarg[i] && '9' >= optarg[i]) {
          stats_flags += optarg[i] - '0';
        }
        else if ('a' <= optarg[i] && 'f' >= optarg[i]) {
          stats_flags += optarg[i] - 'a' + 10;
        }
        else if ('A' <= optarg[i] && 'F' >= optarg[i]) {
          stats_flags += optarg[i] - 'A' + 10;
        }
        else {
          error("Invalid statistics filter. Expected hexadecimal value.");
          return EXIT_FAILURE;
        }
      }
      break; }
    case 'v':
      print_version = TRUE;
      break;
    default:
      usage();
      return EXIT_FAILURE;
    }
  }
  
  if (print_version) {
    // no other flags are allowed when printing version info
    if (disable_profiler || disable_coverage || has_stats_flag ||
        NULL != out_file || NULL != stats_file) {
      usage();
      return EXIT_FAILURE;
    }
    else {
      fputs("Profiler and Code Coverage Merge Tool for the TTCN-3 Test Executor\n"
	    "Product number: " PRODUCT_NUMBER "\n"
157
	    "Version: " VERSION_STRING "\n"
Elemer Lelik's avatar
Elemer Lelik committed
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
	    "Build date: " __DATE__ " " __TIME__ "\n"
	    "Compiled with: " C_COMPILER_VERSION "\n\n"
	    COPYRIGHT_STRING "\n\n", stderr);
#ifdef LICENSE
      print_license_info();
#endif
      return EXIT_SUCCESS;
    }
  }
  
  if (optind == argc) {
    error("No input files specified.");
    usage();
    return EXIT_FAILURE;
  }
  
  if (disable_profiler && disable_coverage) {
    error("Both profiling and code coverage data are discarded, nothing to do.");
    return EXIT_FAILURE;
  }
  
  if (NULL == out_file && NULL == stats_file) {
    error("No output files specified (either the output database file or the "
      "statistics file must be set).");
    usage();
    return EXIT_FAILURE;
  }
  
  if (has_stats_flag && NULL == stats_file) {
    fprintf(stderr, "Notify: No statistics file specified, the statistics filter "
      "will be ignored.");
  }
  
  // create the local database
  Profiler_Tools::profiler_db_t profiler_db;
  
  for (int i = optind; i < argc; i++) {
    // import each input file's contents into the local database
    fprintf(stderr, "Notify: Importing database file '%s'...\n", argv[i]);
Elemer Lelik's avatar
Elemer Lelik committed
197
    Profiler_Tools::import_data(profiler_db, argv[i], error);
Elemer Lelik's avatar
Elemer Lelik committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
    if (erroneous) {
      // an import failed -> exit
      return EXIT_FAILURE;
    }
  }

  boolean out_file_success = TRUE;
  if (NULL != out_file) {
    // print the local database into the output file
    boolean update = file_exists(out_file);
    Profiler_Tools::export_data(profiler_db, out_file, disable_profiler,
      disable_coverage, error);
    out_file_success = !erroneous;
    if (out_file_success) {
      fprintf(stderr, "Notify: Database file '%s' was %s.\n", out_file,
        update ? "updated" : "generated");
    }
  }
  
  boolean stats_file_success = TRUE;
  if (NULL != stats_file) {
    // reset the error flag, in case export_data failed
    erroneous = FALSE;
    // print the statistics into the designated file
    boolean update = file_exists(stats_file);
    Profiler_Tools::print_stats(profiler_db, stats_file, disable_profiler,
      disable_coverage, stats_flags, error);
    stats_file_success = !erroneous;
    if (stats_file_success) {
      fprintf(stderr, "Notify: Statistics file '%s' was %s.\n", stats_file,
        update ? "updated" : "generated");
    }
  }
  
  // return an error code if printing either output file failed
  return (out_file_success && stats_file_success) ? EXIT_SUCCESS : EXIT_FAILURE;
}

// this is needed by version.h
reffer::reffer(const char*) {}