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 <stdio.h> |
---|
38 | #include <stdlib.h> |
---|
39 | #include <sys/stat.h> |
---|
40 | |
---|
41 | #include "app_common.h" |
---|
42 | #include "TraceWriterQEMU.h" |
---|
43 | #include "ExecutableInfo.h" |
---|
44 | #include "CoverageMap.h" |
---|
45 | #include "qemu-traces.h" |
---|
46 | |
---|
47 | #include "rld-process.h" |
---|
48 | |
---|
49 | #if HAVE_STAT64 |
---|
50 | #define STAT stat64 |
---|
51 | #else |
---|
52 | #define STAT stat |
---|
53 | #endif |
---|
54 | |
---|
55 | #if HAVE_OPEN64 |
---|
56 | #define OPEN fopen64 |
---|
57 | #else |
---|
58 | #define OPEN fopen |
---|
59 | #endif |
---|
60 | |
---|
61 | namespace Trace { |
---|
62 | |
---|
63 | TraceWriterQEMU::TraceWriterQEMU(): |
---|
64 | TraceWriterBase() |
---|
65 | { |
---|
66 | } |
---|
67 | |
---|
68 | TraceWriterQEMU::~TraceWriterQEMU() |
---|
69 | { |
---|
70 | } |
---|
71 | |
---|
72 | bool TraceWriterQEMU::writeFile( |
---|
73 | const char* const file, |
---|
74 | Trace::TraceReaderBase *log |
---|
75 | ) |
---|
76 | { |
---|
77 | struct trace_header header; |
---|
78 | int status; |
---|
79 | FILE* traceFile; |
---|
80 | uint8_t taken; |
---|
81 | uint8_t notTaken; |
---|
82 | |
---|
83 | taken = TargetInfo->qemuTakenBit(); |
---|
84 | notTaken = TargetInfo->qemuNotTakenBit(); |
---|
85 | |
---|
86 | // |
---|
87 | // Verify that the TraceList has a non-zero size. |
---|
88 | // |
---|
89 | if ( log->Trace.set.begin() == log->Trace.set.end() ){ |
---|
90 | fprintf( stderr, "ERROR: Empty TraceList\n" ); |
---|
91 | return false; |
---|
92 | } |
---|
93 | |
---|
94 | // |
---|
95 | // Open the trace file. |
---|
96 | // |
---|
97 | traceFile = OPEN( file, "w" ); |
---|
98 | if (!traceFile) { |
---|
99 | fprintf( stderr, "Unable to open %s\n", file ); |
---|
100 | return false; |
---|
101 | } |
---|
102 | |
---|
103 | // |
---|
104 | // Write the Header to the file |
---|
105 | // |
---|
106 | sprintf( header.magic, "%s", QEMU_TRACE_MAGIC ); |
---|
107 | header.version = QEMU_TRACE_VERSION; |
---|
108 | header.kind = QEMU_TRACE_KIND_RAW; // XXX ?? |
---|
109 | header.sizeof_target_pc = 32; |
---|
110 | header.big_endian = false; |
---|
111 | header.machine[0] = 0; // XXX ?? |
---|
112 | header.machine[1] = 0; // XXX ?? |
---|
113 | status = fwrite( &header, sizeof(trace_header), 1, traceFile ); |
---|
114 | if (status != 1) { |
---|
115 | fprintf( stderr, "Unable to write header to %s\n", file ); |
---|
116 | return false; |
---|
117 | } |
---|
118 | |
---|
119 | if (Verbose) |
---|
120 | fprintf( |
---|
121 | stderr, |
---|
122 | "magic = %s\n" |
---|
123 | "version = %d\n" |
---|
124 | "kind = %d\n" |
---|
125 | "sizeof_target_pc = %d\n" |
---|
126 | "big_endian = %d\n" |
---|
127 | "machine = %02x:%02x\n", |
---|
128 | header.magic, |
---|
129 | header.version, |
---|
130 | header.kind, |
---|
131 | header.sizeof_target_pc, |
---|
132 | header.big_endian, |
---|
133 | header.machine[0], header.machine[1] |
---|
134 | ); |
---|
135 | |
---|
136 | // |
---|
137 | // Loop through log and write each entry. |
---|
138 | // |
---|
139 | struct trace_entry32 entry; |
---|
140 | TraceList::ranges_t::iterator itr; |
---|
141 | |
---|
142 | for (itr = log->Trace.set.begin(); (itr != log->Trace.set.end()); itr++ ){ |
---|
143 | entry.pc = itr->lowAddress; |
---|
144 | entry.size = itr-> length; |
---|
145 | entry.op = TRACE_OP_BLOCK; |
---|
146 | switch (itr->exitReason) { |
---|
147 | case TraceList::EXIT_REASON_BRANCH_TAKEN: |
---|
148 | entry.op |= taken; |
---|
149 | break; |
---|
150 | case TraceList::EXIT_REASON_BRANCH_NOT_TAKEN: |
---|
151 | entry.op |= notTaken; |
---|
152 | break; |
---|
153 | case TraceList::EXIT_REASON_OTHER: |
---|
154 | break; |
---|
155 | default: |
---|
156 | fprintf(stderr, "Unknown exit Reason\n"); |
---|
157 | exit(1); |
---|
158 | break; |
---|
159 | } |
---|
160 | |
---|
161 | if ( Verbose ) |
---|
162 | fprintf(stderr, "%x %x %x\n", entry.pc, entry.size, entry.op); |
---|
163 | |
---|
164 | status = fwrite( &entry, sizeof(entry), 1, traceFile ); |
---|
165 | if (status != 1) { |
---|
166 | fprintf( stderr, "Unable to write entry to %s\n", file ); |
---|
167 | return false; |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | fclose( traceFile ); |
---|
172 | return true; |
---|
173 | } |
---|
174 | } |
---|