gem5  v21.1.0.2
inst_pb_trace.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are
16  * met: redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer;
18  * redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution;
21  * neither the name of the copyright holders nor the names of its
22  * contributors may be used to endorse or promote products derived from
23  * this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include "cpu/inst_pb_trace.hh"
39 
40 #include "base/callback.hh"
41 #include "base/output.hh"
42 #include "config/the_isa.hh"
43 #include "cpu/static_inst.hh"
44 #include "cpu/thread_context.hh"
45 #include "debug/ExecEnable.hh"
46 #include "params/InstPBTrace.hh"
47 #include "proto/inst.pb.h"
48 #include "sim/core.hh"
49 #include "sim/cur_tick.hh"
50 
51 namespace gem5
52 {
53 
54 namespace Trace {
55 
57 
58 void
60 {
61  // We're trying to build an instruction trace so we just want macro-ops and
62  // instructions that aren't macro-oped
64  !staticInst->isMicroop()) {
66  }
67 
68  // If this instruction accessed memory lets record it
69  if (getMemValid())
71 }
72 
73 InstPBTrace::InstPBTrace(const InstPBTraceParams &p)
74  : InstTracer(p), buf(nullptr), bufSize(0), curMsg(nullptr)
75 {
76  // Create our output file
77  createTraceFile(p.file_name);
78 }
79 
80 void
81 InstPBTrace::createTraceFile(std::string filename)
82 {
83  // Since there is only one output file for all tracers check if it exists
84  if (traceStream)
85  return;
86 
88 
89  // Output the header
90  ProtoMessage::InstHeader header_msg;
91  header_msg.set_obj_id("gem5 generated instruction trace");
92  header_msg.set_ver(0);
93  header_msg.set_tick_freq(sim_clock::Frequency);
94  header_msg.set_has_mem(true);
95  traceStream->write(header_msg);
96 
97  // get a callback when we exit so we can close the file
98  registerExitCallback([this]() { closeStreams(); });
99 }
100 
101 void
103 {
104  if (curMsg) {
106  delete curMsg;
107  curMsg = NULL;
108  }
109 
110  if (!traceStream)
111  return;
112 
113  delete traceStream;
114  traceStream = NULL;
115 }
116 
118 {
119  closeStreams();
120 }
121 
125 {
126  // Only record the trace if Exec debugging is enabled
127  if (!debug::ExecEnable)
128  return NULL;
129 
130  return new InstPBTraceRecord(*this, when, tc, si, pc, mi);
131 
132 }
133 
134 void
136 {
137  if (curMsg) {
140  delete curMsg;
141  curMsg = NULL;
142  }
143 
144  size_t instSize = si->asBytes(buf.get(), bufSize);
145  if (instSize > bufSize) {
146  bufSize = instSize;
147  buf.reset(new uint8_t[bufSize]);
148  instSize = si->asBytes(buf.get(), bufSize);
149  }
150 
151  // Create a new instruction message and fill out the fields
152  curMsg = new ProtoMessage::Inst;
153  curMsg->set_pc(pc.pc());
154  if (instSize == sizeof(uint32_t)) {
155  curMsg->set_inst(letoh(*reinterpret_cast<uint32_t *>(buf.get())));
156  } else if (instSize) {
157  curMsg->set_inst_bytes(
158  std::string(reinterpret_cast<const char *>(buf.get()), bufSize));
159  }
160  curMsg->set_cpuid(tc->cpuId());
161  curMsg->set_tick(curTick());
162  curMsg->set_type(static_cast<ProtoMessage::Inst_InstType>(si->opClass()));
163 }
164 
165 void
167 {
168  panic_if(!curMsg, "Memory access w/o msg?!");
169 
170  // We do a poor job identifying macro-ops that are load/stores
171  curMsg->set_type(static_cast<ProtoMessage::Inst_InstType>(si->opClass()));
172 
173  ProtoMessage::Inst::MemAccess *mem_msg = curMsg->add_mem_access();
174  mem_msg->set_addr(a);
175  mem_msg->set_size(s);
176  mem_msg->set_mem_flags(f);
177 
178 }
179 
180 } // namespace Trace
181 } // namespace gem5
gem5::curTick
Tick curTick()
The universal simulation clock.
Definition: cur_tick.hh:46
ProtoOutputStream::write
void write(const google::protobuf::Message &msg)
Write a message to the stream, preprending it with the message size.
Definition: protoio.cc:82
gem5::Trace::InstRecord::getAddr
Addr getAddr() const
Definition: insttracer.hh:241
gem5::StaticInst::isMicroop
bool isMicroop() const
Definition: static_inst.hh:208
gem5::Trace::InstPBTraceRecord
This in an instruction tracer that records the flow of instructions through multiple cpus and systems...
Definition: inst_pb_trace.hh:66
gem5::Trace::InstPBTraceRecord::dump
void dump() override
called by the cpu when the instruction commits.
Definition: inst_pb_trace.cc:59
gem5::Trace::InstRecord::getSize
Addr getSize() const
Definition: insttracer.hh:242
gem5::ArmISA::f
Bitfield< 6 > f
Definition: misc_types.hh:67
gem5::Trace::InstPBTrace::bufSize
size_t bufSize
Definition: inst_pb_trace.hh:98
cur_tick.hh
gem5::simout
OutputDirectory simout
Definition: output.cc:62
gem5::sim_clock::Frequency
Tick Frequency
The simulated frequency of curTick(). (In ticks per second)
Definition: core.cc:48
gem5::ArmISA::a
Bitfield< 8 > a
Definition: misc_types.hh:65
gem5::Trace::InstRecord::thread
ThreadContext * thread
Definition: insttracer.hh:65
gem5::Trace::InstPBTrace::getInstRecord
InstPBTraceRecord * getInstRecord(Tick when, ThreadContext *tc, const StaticInstPtr si, TheISA::PCState pc, const StaticInstPtr mi=NULL) override
Definition: inst_pb_trace.cc:123
gem5::StaticInst::isFirstMicroop
bool isFirstMicroop() const
Definition: static_inst.hh:211
gem5::Trace::InstPBTrace::buf
std::unique_ptr< uint8_t[]> buf
Definition: inst_pb_trace.hh:97
gem5::ThreadContext::cpuId
virtual int cpuId() const =0
gem5::Trace::InstPBTrace::traceStream
static ProtoOutputStream * traceStream
One output stream for the entire simulation.
Definition: inst_pb_trace.hh:103
output.hh
gem5::RefCountingPtr< StaticInst >
gem5::letoh
T letoh(T value)
Definition: byteswap.hh:173
gem5::ArmISA::mi
Bitfield< 14 > mi
Definition: misc_types.hh:765
gem5::ThreadContext
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Definition: thread_context.hh:93
gem5::Trace::InstRecord::getMemValid
bool getMemValid() const
Definition: insttracer.hh:244
gem5::Trace::InstRecord::pc
TheISA::PCState pc
Definition: insttracer.hh:69
gem5::Trace::InstPBTrace::createTraceFile
void createTraceFile(std::string filename)
Create the output file and write the header into it.
Definition: inst_pb_trace.cc:81
gem5::Trace::InstPBTrace::curMsg
ProtoMessage::Inst * curMsg
This is the message were working on writing.
Definition: inst_pb_trace.hh:109
gem5::Trace::InstRecord::staticInst
StaticInstPtr staticInst
Definition: insttracer.hh:68
gem5::MipsISA::PCState
GenericISA::DelaySlotPCState< 4 > PCState
Definition: pcstate.hh:40
gem5::MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:326
gem5::Trace::InstPBTrace::~InstPBTrace
virtual ~InstPBTrace()
Definition: inst_pb_trace.cc:117
gem5::Trace::InstPBTrace::traceMem
void traceMem(StaticInstPtr si, Addr a, Addr s, unsigned f)
Write a memory request to the trace file as part of the cur instruction.
Definition: inst_pb_trace.cc:166
gem5::Tick
uint64_t Tick
Tick count type.
Definition: types.hh:58
gem5::ArmISA::s
Bitfield< 4 > s
Definition: misc_types.hh:561
gem5::OutputDirectory::resolve
std::string resolve(const std::string &name) const
Returns relative file names prepended with name of this directory.
Definition: output.cc:204
gem5::Trace::InstTracer
Definition: insttracer.hh:259
gem5::Trace::InstPBTrace::InstPBTraceRecord
friend class InstPBTraceRecord
Definition: inst_pb_trace.hh:136
static_inst.hh
gem5::Trace::InstPBTraceRecord::tracer
InstPBTrace & tracer
Definition: inst_pb_trace.hh:82
core.hh
gem5::ArmISA::si
Bitfield< 6 > si
Definition: misc_types.hh:772
gem5::Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
gem5::Trace::InstPBTrace::InstPBTrace
InstPBTrace(const InstPBTraceParams &p)
Definition: inst_pb_trace.cc:73
inst_pb_trace.hh
panic_if
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:203
gem5::MipsISA::pc
Bitfield< 4 > pc
Definition: pra_constants.hh:243
gem5::Trace::InstPBTrace::traceInst
void traceInst(ThreadContext *tc, StaticInstPtr si, TheISA::PCState pc)
Write an instruction to the trace file.
Definition: inst_pb_trace.cc:135
gem5::Trace::InstPBTrace::closeStreams
void closeStreams()
If there is a pending message still write it out and then close the file.
Definition: inst_pb_trace.cc:102
gem5::registerExitCallback
void registerExitCallback(const std::function< void()> &callback)
Register an exit callback.
Definition: core.cc:146
gem5::Trace::InstRecord::macroStaticInst
StaticInstPtr macroStaticInst
Definition: insttracer.hh:70
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
ProtoOutputStream
A ProtoOutputStream wraps a coded stream, potentially with compression, based on looking at the file ...
Definition: protoio.hh:90
thread_context.hh
callback.hh
gem5::Trace::InstRecord::getFlags
unsigned getFlags() const
Definition: insttracer.hh:243

Generated on Tue Sep 21 2021 12:25:00 for gem5 by doxygen 1.8.17