plugin/tracing: merge full-tracing plugin into generic version

The full-tracing plugin was used in the DSN paper. It additionally
traces the data that was accessed/written on a memory access and the
contents of some CPU registers.

Change-Id: I61f5230699009ce523aba341985b98148160556d
This commit is contained in:
Christian Dietrich
2013-03-15 15:35:37 +01:00
committed by Gerrit Code Review
parent cffafa411c
commit 4e8098a636
7 changed files with 281 additions and 186 deletions

View File

@ -1,7 +1,9 @@
#include <iostream>
#include <assert.h>
#include "sal/SALInst.hpp"
#include "sal/Register.hpp"
#include "sal/Memory.hpp"
#include "sal/Listener.hpp"
#include "TracingPlugin.hpp"
@ -10,6 +12,8 @@ using namespace fail;
bool TracingPlugin::run()
{
MemoryManager& mm = simulator.getMemoryManager();
MemAccessListener ev_mem(ANY_ADDR);
BPSingleListener ev_step(ANY_ADDR);
BaseListener *ev;
@ -23,13 +27,24 @@ bool TracingPlugin::run()
if(m_protoStreamFile) {
ps = new ProtoOStream(m_protoStreamFile);
}
#ifdef BUILD_X86
size_t ids[] = {RID_CSP, RID_CBP, RID_FLAGS};
#else
size_t ids[] = {};
#endif
Register *regs[sizeof(ids)/sizeof(*ids)];
for (unsigned i = 0; i < sizeof(ids)/sizeof(*ids); ++i)
regs[i] = simulator.getCPU(0).getRegister(ids[i]);
while (true) {
ev = simulator.resume();
if (ev == &ev_step) {
simulator.addListener(&ev_step);
address_t ip = ev_step.getTriggerInstructionPointer();
if (m_ipMap && !m_ipMap->isMatching(ip)) {
continue;
@ -49,15 +64,15 @@ bool TracingPlugin::run()
address_t addr = ev_mem.getTriggerAddress();
size_t width = ev_mem.getTriggerWidth();
if ((m_ipMap && !m_ipMap->isMatching(ip)) ||
(m_memMap && !m_memMap->isMatching(addr, width))) {
(m_memMap && !m_memMap->isMatching(addr, width))) {
continue;
}
if (m_os)
*m_os << hex << "[Tracing] MEM "
<< ((ev_mem.getTriggerAccessType() &
MemAccessEvent::MEM_READ) ? "R " : "W ")
<< addr << " width " << width << " IP " << ip << "\n";
<< ((ev_mem.getTriggerAccessType() &
MemAccessEvent::MEM_READ) ? "R " : "W ")
<< addr << " width " << width << " IP " << ip << "\n";
if (m_protoStreamFile) {
Trace_Event e;
e.set_ip(ip);
@ -66,6 +81,29 @@ bool TracingPlugin::run()
e.set_accesstype(
(ev_mem.getTriggerAccessType() & MemAccessEvent::MEM_READ) ?
e.READ : e.WRITE);
/* When we're doing a full trace, we log more data in
the case of a memory event */
if (m_full_trace) {
Trace_Event_Extended &ext = *e.mutable_trace_ext();
// Read the accessed data
assert(width <= 8);
uint64_t data = 0;
mm.getBytes(addr, width, &data);
ext.set_data(data);
for (unsigned i = 0; i < sizeof(ids)/sizeof(*ids); ++i) {
Trace_Event_Extended_Registers *er = ext.add_registers();
er->set_id(ids[i]);
er->set_value(simulator.getCPU(0).getRegisterContent(regs[i]));
if (er->value() <= mm.getPoolSize() - width) {
uint32_t value_deref;
mm.getBytes(er->value(), 4, &value_deref);
er->set_value_deref(value_deref);
}
}
}
ps->writeMessage(&e);
}
} else {

View File

@ -21,7 +21,7 @@
* \brief Plugin to record instruction traces and memory accesses.
*
* This plugin is supposed to be instantiated, configured and run by
* experiments needing instruction or memory access traces. Tracing can be
* experiments needing instruction or memory access traces. Tracing can be
* restricted to a memory or instruction address map; the restrictions are
* applied together, i.e., a memory access is only logged if neither its
* instruction address nor its memory address is restricted.
@ -29,10 +29,10 @@
* TODO: document usage by example
* FIXME: handle configuration changes after tracing start properly
* FIXME: more explicit startup/shutdown; listener-based event interface needed?
* -> should simulator.removeFlow make sure all remaining active events
* are delivered?
* -> should simulator.removeFlow make sure all remaining active events
* are delivered?
* FIXME: trace a sequence of pb messages, not a giant single one (pb weren't
* made for huge messages)
* made for huge messages)
* FIXME: destructor -> removeFlow?
*/
class TracingPlugin : public fail::ExperimentFlow
@ -42,18 +42,19 @@ private:
fail::MemoryMap *m_ipMap; //!< instruction address restriction
bool m_memonly; //!< log instructions only if they are memory accesses
bool m_iponly; //!< log instruction addresses only
bool m_full_trace; //!< do a full trace (more information for the events)
std::ostream *m_protoStreamFile;
std::ostream *m_os; //!< ostream to write human-readable trace into
fail::ProtoOStream *ps;
public:
TracingPlugin()
TracingPlugin(bool full_trace = false)
: m_memMap(0), m_ipMap(0), m_memonly(false), m_iponly(false),
m_protoStreamFile(0), m_os(0) { }
m_full_trace(full_trace), m_protoStreamFile(0), m_os(0) { }
bool run();
/**
* Restricts tracing to memory addresses listed in this MemoryMap. An
* Restricts tracing to memory addresses listed in this MemoryMap. An
* access wider than 8 bit *is* logged if *one* of the bytes it
* reads/writes is listed.
*/
@ -75,6 +76,10 @@ public:
* If invoked with iponly=true, only instruction addresses are logged.
*/
void setLogIPOnly(bool iponly) { m_iponly = iponly; }
/**
* If invoked with fulltrace=true, a extended (full) trace is done.
*/
void setFullTrace(bool fulltrace) { m_full_trace = fulltrace; }
/**
* ostream to trace into (human-readable)
*/

View File

@ -1,47 +0,0 @@
#! /usr/bin/env python
# create python bindings before running:
# protoc --python_out=. trace.proto
import trace_pb2
import sys
import struct
if len(sys.argv) != 2:
print "Usage:", sys.argv[0], "tracefile.pb"
sys.exit(-1)
trace_event = trace_pb2.Trace_Event()
try:
f = open(sys.argv[1], "rb")
except IOError:
print sys.argv[1] + ": Could not open file."
sys.exit(-1)
while 1:
# Read trace length
try:
lengthNO = f.read(4)
if len(lengthNO) == 0:
break
except IOError:
print "Could not read data from file"
# Read Trace-Event
length = struct.unpack('!I', lengthNO)[0]
trace_event.ParseFromString(f.read(length))
# This works for any type of pb message:
#print trace_event
# More compact dump for traces:
if not trace_event.HasField("memaddr"):
print "IP {0:x}".format(trace_event.ip)
else:
print "MEM {0} {1:x} width {2:d} IP {3:x}".format(
"R" if trace_event.accesstype == trace_pb2.Trace_Event.READ else "W",
trace_event.memaddr, trace_event.width, trace_event.ip)
f.close()

View File

@ -1,3 +1,21 @@
message Trace_Event_Extended {
// data value read/written
optional uint64 data = 5;
// register contents
repeated group Registers = 6 {
// register ID
required uint32 id = 1;
// register value
optional uint64 value = 2;
// data register points to
optional uint32 value_deref = 3;
}
// selected stack content
repeated group Stack = 7 {
required uint32 value = 1;
}
}
message Trace_Event {
required uint64 ip = 1;
optional uint64 memaddr = 2;
@ -7,4 +25,7 @@ message Trace_Event {
WRITE = 2;
}
optional AccessType accesstype = 4;
optional Trace_Event_Extended trace_ext = 5;
}