From 92e7be0d225b5aec7ccf96775559b843496658cd Mon Sep 17 00:00:00 2001 From: Richard Hellwig Date: Thu, 4 Apr 2013 18:31:45 +0200 Subject: [PATCH] dump-trace: C++ implementation About 50 times faster than the Python version. Extended trace functionality still untested. Change-Id: I9e41b0283b7d3274c5ea9eba2164f78c04c1ad42 --- tools/CMakeLists.txt | 5 ++ tools/dump-trace/CMakeLists.txt | 7 +++ tools/dump-trace/DumpTrace.cc | 96 +++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 tools/dump-trace/CMakeLists.txt create mode 100644 tools/dump-trace/DumpTrace.cc diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index b24126e1..ac2e2096 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -1,3 +1,4 @@ +option(BUILD_DUMP_TRACE "Build the trace dump tool?" OFF) option(BUILD_IMPORT_TRACE "Build the trace import tool?" OFF) option(BUILD_PRUNE_TRACE "Build the trace prune tool?" OFF) @@ -13,3 +14,7 @@ endif(BUILD_IMPORT_TRACE) if(BUILD_PRUNE_TRACE) add_subdirectory(prune-trace) endif(BUILD_PRUNE_TRACE) + +if(BUILD_DUMP_TRACE) + add_subdirectory(dump-trace) +endif(BUILD_DUMP_TRACE) diff --git a/tools/dump-trace/CMakeLists.txt b/tools/dump-trace/CMakeLists.txt new file mode 100644 index 00000000..2f60608c --- /dev/null +++ b/tools/dump-trace/CMakeLists.txt @@ -0,0 +1,7 @@ +set(SRCS + DumpTrace.cc +) + +add_executable(dump-trace ${SRCS}) +target_link_libraries(dump-trace ${PROTOBUF_LIBRARY} fail-util fail-comm) +install(TARGETS dump-trace RUNTIME DESTINATION bin) diff --git a/tools/dump-trace/DumpTrace.cc b/tools/dump-trace/DumpTrace.cc new file mode 100644 index 00000000..3050d333 --- /dev/null +++ b/tools/dump-trace/DumpTrace.cc @@ -0,0 +1,96 @@ +#include +#include +#include +#include "comm/TracePlugin.pb.h" +#include "util/ProtoStream.hpp" +#include "../../src/core/util/Logger.hpp" +#include "../../src/core/util/gzstream/gzstream.h" + +using namespace fail; +using std::string; +using std::endl; +using std::cout; +using std::cerr; +using std::hex; +using std::dec; + +Logger log("dump-trace", true); + +std::istream& openStream(const char *input_file, + std::ifstream& normal_stream, igzstream& gz_stream) { + normal_stream.open(input_file); + if (!normal_stream) { + log << "couldn't open " << input_file << endl; + exit(-1); + } + unsigned char b1, b2; + normal_stream >> b1 >> b2; + + if (b1 == 0x1f && b2 == 0x8b) { + normal_stream.close(); + gz_stream.open(input_file); + if (!gz_stream) { + log << "couldn't open " << input_file << endl; + exit(-1); + } + //log << "opened file " << input_file << " in GZip mode" << endl; + return gz_stream; + } + + normal_stream.seekg(0); + + //log << "opened file " << input_file << " in normal mode" << endl; + return normal_stream; +} + +int main(int argc, char *argv[]) +{ + Trace_Event ev; + + if (argc != 2) { + cerr << "Usage: " << argv[0] << " tracefile.pb" << endl; + return 1; + } + + std::ifstream normal_stream; + igzstream gz_stream; + ProtoIStream ps(&openStream(argv[1], normal_stream, gz_stream)); + + while (ps.getNext(&ev)) { + if (!ev.has_memaddr()) { + cout << "IP " << hex << ev.ip() << "\n"; + } else { + string ext = ""; // FIXME: use stringstream? + if (ev.has_trace_ext()) { + const Trace_Event_Extended& temp_ext = ev.trace_ext(); + ext = " DATA "; + ext += temp_ext.data(); + int i; + for (i = 0 ; i < temp_ext.registers_size() ; i++) { + const Trace_Event_Extended_Registers& temp_reg = temp_ext.registers(i); + ext += "REG: " ; + ext += temp_reg.id(); + ext += " "; + ext += temp_reg.value(); + ext += "="; + ext += temp_reg.value_deref(); + } + if (temp_ext.stack_size() > 0 ) { + ext += " STACK: "; + for (i = 0 ; i< temp_ext.stack_size() ; i++) { + const Trace_Event_Extended_Stack& temp_stack = temp_ext.stack(i); + ext += temp_stack.value(); + } + } + } + cout << "MEM " + << (ev.accesstype() == Trace_Event_AccessType_READ ? "R" : "W") << " " + << ev.memaddr() + << dec << " width " << ev.width() + << hex << " IP " << ev.ip() + << ext << "\n"; + } + } + + return 0; +}