summaryrefslogtreecommitdiffstats
path: root/tester/covoar/TraceConverter.cc
blob: 7770b110b6480b63dadb8f10ea2649a1212f7607 (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
/*! @file TraceReaderLogQEMU.cc
 *  @brief TraceReaderLogQEMU Implementation
 *
 *  This file contains the implementation of the functions supporting
 *  reading the QEMU coverage data files.
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <getopt.h>
#include <signal.h>
#include <unistd.h>

#include <rld.h>
#include <rld-process.h>

#include "qemu-log.h"
#include "TraceReaderLogQEMU.h"
#include "TraceWriterQEMU.h"
#include "TraceList.h"
#include "ObjdumpProcessor.h"
#include "TargetFactory.h"

#if defined(_WIN32) || defined(__CYGWIN__)
  #define kill(p,s) raise(s)
#endif

char*       progname;

void usage()
{
  fprintf(
    stderr,
    "Usage: %s [-v] -c CPU -e executable -t tracefile [-E logfile]\n",
    progname
  );
  exit(1);
}

static void
fatal_signal( int signum )
{
  signal( signum, SIG_DFL );

  rld::process::temporaries_clean_up();

  /*
   * Get the same signal again, this time not handled, so its normal effect
   * occurs.
   */
  kill( getpid(), signum );
}

static void
setup_signals( void )
{
  if ( signal (SIGINT, SIG_IGN) != SIG_IGN )
    signal( SIGINT, fatal_signal );
#ifdef SIGHUP
  if ( signal( SIGHUP, SIG_IGN ) != SIG_IGN )
    signal( SIGHUP, fatal_signal );
#endif
  if ( signal( SIGTERM, SIG_IGN ) != SIG_IGN )
    signal( SIGTERM, fatal_signal );
#ifdef SIGPIPE
  if ( signal( SIGPIPE, SIG_IGN ) != SIG_IGN )
    signal( SIGPIPE, fatal_signal );
#endif
#ifdef SIGCHLD
  signal( SIGCHLD, SIG_DFL );
#endif
}

int main(
  int    argc,
  char** argv
)
{
  int                                 opt;
  Trace::TraceReaderLogQEMU           log;
  Trace::TraceWriterQEMU              trace;
  const char                         *cpuname    = "";
  const char                         *executable = "";
  const char                         *tracefile  =  "";
  const char                         *logname = "/tmp/qemu.log";
  Coverage::ExecutableInfo*           executableInfo;
  rld::process::tempfile              objdumpFile( ".dmp" );
  rld::process::tempfile              err( ".err" );
  Coverage::DesiredSymbols            symbolsToAnalyze;
  bool                                verbose = false;
  std::string                         dynamicLibrary;
  int                                 ec = 0;
  std::shared_ptr<Target::TargetBase> targetInfo;

  setup_signals();

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

  while ((opt = getopt(argc, argv, "c:e:l:L:t:v")) != -1) {
    switch (opt) {
      case 'c': cpuname = optarg;        break;
      case 'e': executable = optarg;     break;
      case 'l': logname = optarg;        break;
      case 'L': dynamicLibrary = optarg; break;
      case 't': tracefile = optarg;      break;
      case 'v': verbose = true;          break;
      default:  usage();
    }
  }

  // Make sure we have all the required parameters
  if ( !cpuname ) {
    fprintf( stderr, "cpuname not specified\n" );
    usage();
  }

  if ( !executable ) {
    fprintf( stderr, "executable not specified\n" );
    usage();
  }

  if ( !tracefile ) {
    fprintf( stderr, "output trace file not specified\n" );
    usage();
  }


  // Create toolnames.
  try
  {
    targetInfo.reset( Target::TargetFactory( cpuname ) );
  }
  catch ( rld::error re )
  {
    std::cerr << "error: "
              << re.where << ": " << re.what
              << std::endl;
    ec = 10;

    return ec;
  }

  Coverage::ObjdumpProcessor objdumpProcessor( symbolsToAnalyze, targetInfo );

  if ( !dynamicLibrary.empty() )
    executableInfo = new Coverage::ExecutableInfo(
      executable,
      dynamicLibrary,
      false,
      symbolsToAnalyze
    );
  else {
    try
    {
      executableInfo = new Coverage::ExecutableInfo(
        executable,
        "",
        false,
        symbolsToAnalyze
      );
    }
    catch ( rld::error re )
    {
      std::cerr << "error: "
                << re.where << ": " << re.what
                << std::endl;
      ec = 10;
    }
  }

  // If a dynamic library was specified, determine the load address.
  if ( !dynamicLibrary.empty() ) {
    try
    {
      executableInfo->setLoadAddress(
        objdumpProcessor.determineLoadAddress( executableInfo )
      );
    }
    catch ( rld::error re )
    {
      std::cerr << "error: "
                << re.where << ": " << re.what
                << std::endl;
      ec = 10;

      return ec;
    }
  }

  objdumpProcessor.loadAddressTable( executableInfo, objdumpFile, err );
  log.processFile( logname, objdumpProcessor );
  trace.writeFile( tracefile, &log, verbose );

  return ec;
}