1 | /* |
---|
2 | * RTEMS Tools Project (http://www.rtems.org/) |
---|
3 | * Copyright 2014 OAR Corporation |
---|
4 | * All rights reserved. |
---|
5 | * |
---|
6 | * This file is part of the RTEMS Tools package in 'rtems-tools'. |
---|
7 | * |
---|
8 | * Redistribution and use in source and binary forms, with or without |
---|
9 | * modification, are permitted provided that the following conditions are met: |
---|
10 | * |
---|
11 | * 1. Redistributions of source code must retain the above copyright notice, |
---|
12 | * this list of conditions and the following disclaimer. |
---|
13 | * |
---|
14 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
15 | * this list of conditions and the following disclaimer in the documentation |
---|
16 | * and/or other materials provided with the distribution. |
---|
17 | * |
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
28 | * POSSIBILITY OF SUCH DAMAGE. |
---|
29 | */ |
---|
30 | /*! @file TraceWriterQEMU.cc |
---|
31 | * @brief TraceWriterQEMU Implementation |
---|
32 | * |
---|
33 | * This file contains the implementation of the functions supporting |
---|
34 | * reading the QEMU coverage data files. |
---|
35 | */ |
---|
36 | |
---|
37 | #include "covoar-config.h" |
---|
38 | |
---|
39 | #include <stdio.h> |
---|
40 | #include <stdlib.h> |
---|
41 | #include <sys/stat.h> |
---|
42 | |
---|
43 | #include "app_common.h" |
---|
44 | #include "TraceWriterQEMU.h" |
---|
45 | #include "ExecutableInfo.h" |
---|
46 | #include "CoverageMap.h" |
---|
47 | |
---|
48 | /* XXX really not always right */ |
---|
49 | typedef uint32_t target_ulong; |
---|
50 | |
---|
51 | #include "qemu-traces.h" |
---|
52 | |
---|
53 | #if HAVE_STAT64 |
---|
54 | #define STAT stat64 |
---|
55 | #else |
---|
56 | #define STAT stat |
---|
57 | #endif |
---|
58 | |
---|
59 | #if HAVE_OPEN64 |
---|
60 | #define OPEN fopen64 |
---|
61 | #else |
---|
62 | #define OPEN fopen |
---|
63 | #endif |
---|
64 | |
---|
65 | namespace Trace { |
---|
66 | |
---|
67 | TraceWriterQEMU::TraceWriterQEMU(): |
---|
68 | TraceWriterBase() |
---|
69 | { |
---|
70 | } |
---|
71 | |
---|
72 | TraceWriterQEMU::~TraceWriterQEMU() |
---|
73 | { |
---|
74 | } |
---|
75 | |
---|
76 | bool TraceWriterQEMU::writeFile( |
---|
77 | const char* const file, |
---|
78 | Trace::TraceReaderBase *log |
---|
79 | ) |
---|
80 | { |
---|
81 | struct trace_header header; |
---|
82 | int status; |
---|
83 | FILE* traceFile; |
---|
84 | uint8_t taken; |
---|
85 | uint8_t notTaken; |
---|
86 | |
---|
87 | taken = TargetInfo->qemuTakenBit(); |
---|
88 | notTaken = TargetInfo->qemuNotTakenBit(); |
---|
89 | |
---|
90 | // |
---|
91 | // Verify that the TraceList has a non-zero size. |
---|
92 | // |
---|
93 | if ( log->Trace.set.begin() == log->Trace.set.end() ){ |
---|
94 | fprintf( stderr, "ERROR: Empty TraceList\n" ); |
---|
95 | return false; |
---|
96 | } |
---|
97 | |
---|
98 | // |
---|
99 | // Open the trace file. |
---|
100 | // |
---|
101 | traceFile = OPEN( file, "w" ); |
---|
102 | if (!traceFile) { |
---|
103 | fprintf( stderr, "Unable to open %s\n", file ); |
---|
104 | return false; |
---|
105 | } |
---|
106 | |
---|
107 | // |
---|
108 | // Write the Header to the file |
---|
109 | // |
---|
110 | sprintf( header.magic, "%s", QEMU_TRACE_MAGIC ); |
---|
111 | header.version = QEMU_TRACE_VERSION; |
---|
112 | header.kind = QEMU_TRACE_KIND_RAW; // XXX ?? |
---|
113 | header.sizeof_target_pc = 32; |
---|
114 | header.big_endian = false; |
---|
115 | header.machine[0] = 0; // XXX ?? |
---|
116 | header.machine[1] = 0; // XXX ?? |
---|
117 | status = fwrite( &header, sizeof(trace_header), 1, traceFile ); |
---|
118 | if (status != 1) { |
---|
119 | fprintf( stderr, "Unable to write header to %s\n", file ); |
---|
120 | return false; |
---|
121 | } |
---|
122 | |
---|
123 | if (Verbose) |
---|
124 | fprintf( |
---|
125 | stderr, |
---|
126 | "magic = %s\n" |
---|
127 | "version = %d\n" |
---|
128 | "kind = %d\n" |
---|
129 | "sizeof_target_pc = %d\n" |
---|
130 | "big_endian = %d\n" |
---|
131 | "machine = %02x:%02x\n", |
---|
132 | header.magic, |
---|
133 | header.version, |
---|
134 | header.kind, |
---|
135 | header.sizeof_target_pc, |
---|
136 | header.big_endian, |
---|
137 | header.machine[0], header.machine[1] |
---|
138 | ); |
---|
139 | |
---|
140 | // |
---|
141 | // Loop through log and write each entry. |
---|
142 | // |
---|
143 | struct trace_entry32 entry; |
---|
144 | TraceList::ranges_t::iterator itr; |
---|
145 | |
---|
146 | for (itr = log->Trace.set.begin(); (itr != log->Trace.set.end()); itr++ ){ |
---|
147 | entry.pc = itr->lowAddress; |
---|
148 | entry.size = itr-> length; |
---|
149 | entry.op = TRACE_OP_BLOCK; |
---|
150 | switch (itr->exitReason) { |
---|
151 | case TraceList::EXIT_REASON_BRANCH_TAKEN: |
---|
152 | entry.op |= taken; |
---|
153 | break; |
---|
154 | case TraceList::EXIT_REASON_BRANCH_NOT_TAKEN: |
---|
155 | entry.op |= notTaken; |
---|
156 | break; |
---|
157 | case TraceList::EXIT_REASON_OTHER: |
---|
158 | break; |
---|
159 | default: |
---|
160 | fprintf(stderr, "Unknown exit Reason\n"); |
---|
161 | exit(1); |
---|
162 | break; |
---|
163 | } |
---|
164 | |
---|
165 | if ( Verbose ) |
---|
166 | fprintf(stderr, "%x %x %x\n", entry.pc, entry.size, entry.op); |
---|
167 | |
---|
168 | status = fwrite( &entry, sizeof(entry), 1, traceFile ); |
---|
169 | if (status != 1) { |
---|
170 | fprintf( stderr, "Unable to emtry to %s\n", file ); |
---|
171 | return false; |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | fclose( traceFile ); |
---|
176 | return true; |
---|
177 | } |
---|
178 | } |
---|