Skip to content

Commit

Permalink
Merge pull request #3 from pfpsim/debug-print
Browse files Browse the repository at this point in the history
Add support for debugger to print packet contents
  • Loading branch information
grodtron authored Jun 10, 2016
2 parents 1345cb4 + b8a0f40 commit e440128
Show file tree
Hide file tree
Showing 14 changed files with 284 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .travis/run_lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
# +y --> don't ignore category y
# Separate each by a comma with no space
# Example: -whitespace,+whitespace/braces --> ignores all 'whitespace' errors except the 'whitespace/braces' errors.
filters="-legal/copyright,-runtime/references,-build/include_subdir,-build/c++11"
filters="-runtime/references,-build/include_subdir,-build/c++11"

# Root Directory: Used to determine appropriate header guards
# The path must be relative to the location of the directory containing the .git file
Expand Down
2 changes: 2 additions & 0 deletions pfpsim/core/PFPObserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
#include <string>
#include <memory>

#include "TrType.h"

namespace pfp {
namespace core {

Expand Down
51 changes: 51 additions & 0 deletions pfpsim/core/TrType.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,53 @@

#include <iostream>
#include <string>
#include <memory>
#include <vector>

namespace pfp {
namespace core {

/**
* Class which is used to hold info about a packet that
* can be shown in the debugger.
*/
class DebugInfo {
public:
virtual ~DebugInfo() = default;

typedef std::vector<uint8_t> RawData;

struct Field {
const std::string name;
const RawData value;

Field(std::string n, RawData v)
: name(n), value(v) {}
};

struct Header {
const std::string name;
const std::vector<Field> fields;

Header(std::string n, std::vector<Field> f)
: name(n), fields(f) {}
};

// Get the raw unparsed data of a packet.
virtual RawData raw_data() const = 0;

virtual RawData field_value(const std::string & field_name) const = 0;

// Get the parsed representation of a packet. Makes the (reasonable)
// assumption that a packet is an ordered list of named headers,
// each of which is just an ordered list of named fields.
virtual std::vector<Header> parsed_data() const = 0;

// Check if this DebugInfo is still valid (refers to a packet which still
// exists inside the simulation)
virtual bool valid() const = 0;
};

class TrType {
public:
TrType() = default;
Expand All @@ -68,6 +111,14 @@ class TrType {
return false;
}

/**
* Get a pointer to an object describing the packet held by this TrType.
* This method will only be called if `debuggable` returns true.
*/
virtual std::shared_ptr<const DebugInfo> debug_info() const {
return nullptr;
}

private:
std::size_t id_; /*<! id for the transaction */
};
Expand Down
43 changes: 28 additions & 15 deletions pfpsim/core/debugger/DebugDataManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ void DebugDataManager::removeBreakpoint(int identifier) {

void DebugDataManager::enableBreakpoint(int id) {
std::lock_guard<std::mutex> guard(mutex_);
// TODO(gordon) Don't do a linear search!
for (auto bkpt = breakpoint_list.begin();
bkpt != breakpoint_list.end(); bkpt++) {
if (bkpt->getID() == id) {
Expand All @@ -116,6 +117,7 @@ void DebugDataManager::enableBreakpoint(int id) {

void DebugDataManager::disableBreakpoint(int id) {
std::lock_guard<std::mutex> guard(mutex_);
// TODO(gordon) Don't do a linear search!
for (auto bkpt = breakpoint_list.begin();
bkpt != breakpoint_list.end(); bkpt++) {
if (bkpt->getID() == id) {
Expand All @@ -125,22 +127,29 @@ void DebugDataManager::disableBreakpoint(int id) {
}
}

void DebugDataManager::updatePacket(int id, std::string module, double time_,
bool read) {
void DebugDataManager::updatePacket(int id,
std::shared_ptr<const DebugInfo> di,
std::string module, double time_,
bool read) {
std::lock_guard<std::mutex> guard(mutex_);
auto check_pk = packet_list.find(id);
if (check_pk == packet_list.end()) {
DebuggerPacket pk(id, module, time_);
packet_list[id] = pk;
}
DebuggerPacket& packet = packet_list.at(id);
if (read) {
packet.setTime(time_);
packet.setCurrentLocation(module);
packet.updateTraceReadTime(module, time_);
} else {
packet.updateTraceWriteTime(module, time_);
}

auto check_pk = packet_list.find(id);
if (check_pk == packet_list.end()) {
DebuggerPacket pk(id, module, time_);
packet_list[id] = pk;
}

DebuggerPacket& packet = packet_list.at(id);

packet.setDebugInfo(di);

if (read) {
packet.setTime(time_);
packet.setCurrentLocation(module);
packet.updateTraceReadTime(module, time_);
} else {
packet.updateTraceWriteTime(module, time_);
}
}

void DebugDataManager::removePacket(int id) {
Expand All @@ -150,6 +159,7 @@ void DebugDataManager::removePacket(int id) {

void DebugDataManager::addWatchpoint(Watchpoint wp) {
std::lock_guard<std::mutex> guard(mutex_);
// TODO(gordon) Don't do a linear search!
for (auto it = watchpoint_list.begin(); it != watchpoint_list.end(); it++) {
if (it->getCounterName() == wp.getCounterName()) {
return;
Expand All @@ -160,6 +170,7 @@ void DebugDataManager::addWatchpoint(Watchpoint wp) {

void DebugDataManager::removeWatchpoint(int id) {
std::lock_guard<std::mutex> guard(mutex_);
// TODO(gordon) Don't do a linear search!
for (auto it = watchpoint_list.begin(); it != watchpoint_list.end(); it++) {
if (it->getID() == id) {
watchpoint_list.erase(it);
Expand All @@ -170,6 +181,7 @@ void DebugDataManager::removeWatchpoint(int id) {

void DebugDataManager::enableWatchpoint(int id) {
std::lock_guard<std::mutex> guard(mutex_);
// TODO(gordon) Don't do a linear search!
for (auto it = watchpoint_list.begin(); it != watchpoint_list.end(); it++) {
if (it->getID() == id) {
it->disabled = false;
Expand All @@ -180,6 +192,7 @@ void DebugDataManager::enableWatchpoint(int id) {

void DebugDataManager::disableWatchpoint(int id) {
std::lock_guard<std::mutex> guard(mutex_);
// TODO(gordon) Don't do a linear search!
for (auto it = watchpoint_list.begin(); it != watchpoint_list.end(); it++) {
if (it->getID() == id) {
it->disabled = true;
Expand Down
4 changes: 3 additions & 1 deletion pfpsim/core/debugger/DebugDataManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,13 @@ class DebugDataManager {
/**
* Add a new packet or update an existing one.
* @param id ID of packet.
* @param di The DebugObject representing this packet.
* @param module Name of module the packet is currently in.
* @param time_ Time of update.
* @param read Indicates whether the update is for a read or a write. True = read, False = write.
*/
void updatePacket(int id, std::string module, double time_, bool read);
void updatePacket(int id, std::shared_ptr<const DebugInfo> di,
std::string module, double time_, bool read);

/**
* Remove a packet.
Expand Down
13 changes: 7 additions & 6 deletions pfpsim/core/debugger/DebugObserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ void DebugObserver::data_written(const std::string& from_module,
}
updateSimulationTime(simulation_time);

data_manager->updatePacket(data->id(), from_module, simulation_time, false);
data_manager->updatePacket(data->id(), data->debug_info(),
from_module, simulation_time, false);

if (!data_manager->checkIgnoreModules(from_module)) {
checkBreakpointHit(from_module, data->id(), simulation_time, false);
Expand All @@ -147,7 +148,8 @@ void DebugObserver::data_read(const std::string& to_module,
}

updateSimulationTime(simulation_time);
updatePacketList(data->id(), to_module, simulation_time);
data_manager->updatePacket(data->id(), data->debug_info(),
to_module, simulation_time, false);

if (!data_manager->checkIgnoreModules(to_module)) {
checkBreakpointHit(to_module, data->id(), simulation_time, true);
Expand All @@ -167,8 +169,11 @@ void DebugObserver::data_dropped(const std::string& in_module,
<< " in " << in_module << ":\nType: " << data->data_type()
<< "\nPacket ID: " << data->id() << "\nSource: " << std::endl;
}

updateSimulationTime(simulation_time);

data_manager->addDroppedPacket(data->id(), in_module, drop_reason);

if (data_manager->getBreakOnPacketDrop()) {
PacketDroppedMessage *msg
= new PacketDroppedMessage(data->id(), in_module, drop_reason);
Expand Down Expand Up @@ -255,10 +260,6 @@ void DebugObserver::updateWhoAmI(int packet_id) {
data_manager->set_whoami(packet_id);
}

void DebugObserver::updatePacketList(int id, std::string loc, double t) {
data_manager->updatePacket(id, loc, t, true);
}

void DebugObserver::checkBreakpointHit(std::string module, int packet_id,
double sim_time, bool read) {
bool break_hit = true;
Expand Down
9 changes: 0 additions & 9 deletions pfpsim/core/debugger/DebugObserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,6 @@ class DebugObserver : public PFPObserver {
*/
void updateWhoAmI(int packet_id);

/**
* Update the list of packets.
* This adds the packet if does not exists.
* @param id ID of packet.
* @param loc Module the packet is currently in.
* @param t Current simulation time in nanoseconds.
*/
void updatePacketList(int id, std::string loc, double t);

/**
* Check to see if the module, packet id or simulation time should cause a breakpoint hit.
* @param module Current module name.
Expand Down
71 changes: 70 additions & 1 deletion pfpsim/core/debugger/DebuggerIPCServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ PFPSimDebugger::DebugMsg* DebuggerIPCServer::recv() {
char *buf;
int bytes = nn_recv(socket, &buf, NN_MSG, 0);
if (bytes != -1) {
std::string message_string(buf);
std::string message_string(buf, bytes);
nn_freemsg(buf);

PFPSimDebugger::DebugMsg *message = new PFPSimDebugger::DebugMsg();
Expand Down Expand Up @@ -306,6 +306,27 @@ void DebuggerIPCServer::parseRequest(PFPSimDebugger::DebugMsg *request) {
handleGetTableEntries();
break;
}
case PFPSimDebugger::DebugMsg_Type_GetPacketField:
{
PFPSimDebugger::GetPacketFieldMsg msg;
msg.ParseFromString(request->message());
handleGetPacketField(msg.id(), msg.field_name());
break;
}
case PFPSimDebugger::DebugMsg_Type_GetRawPacket:
{
PFPSimDebugger::GetRawPacketMsg msg;
msg.ParseFromString(request->message());
handleGetRawPacket(msg.id());
break;
}
case PFPSimDebugger::DebugMsg_Type_GetParsedPacket:
{
PFPSimDebugger::GetParsedPacketMsg msg;
msg.ParseFromString(request->message());
handleGetParsedPacket(msg.id());
break;
}
default: {
sendRequestFailed();
}
Expand Down Expand Up @@ -561,6 +582,54 @@ void DebuggerIPCServer::handleBacktrace(PFPSimDebugger::BacktraceMsg msg) {
}
}

void DebuggerIPCServer::handleGetPacketField(int id, std::string field_name) {
DebuggerPacket *pk = data_manager->getPacket(id);
if (pk) {
auto dbg_info = pk->getDebugInfo();
if (dbg_info) {
auto raw_data = dbg_info->field_value(field_name);

PacketFieldValueMessage message(raw_data);

send(&message);
}
}

sendRequestFailed();
}

void DebuggerIPCServer::handleGetRawPacket(int id) {
DebuggerPacket *pk = data_manager->getPacket(id);
if (pk) {
auto dbg_info = pk->getDebugInfo();
if (dbg_info) {
auto raw_data = dbg_info->raw_data();

RawPacketValueMessage message(raw_data);

send(&message);
}
}

sendRequestFailed();
}

void DebuggerIPCServer::handleGetParsedPacket(int id) {
DebuggerPacket *pk = data_manager->getPacket(id);
if (pk) {
auto dbg_info = pk->getDebugInfo();
if (dbg_info) {
auto parsed_data = dbg_info->parsed_data();

ParsedPacketValueMessage message(parsed_data);

send(&message);
}
}

sendRequestFailed();
}

void DebuggerIPCServer::handleEnableDisableBreakpoint(
PFPSimDebugger::EnableDisableBreakpointMsg msg) {
int id = stoi(msg.id());
Expand Down
4 changes: 4 additions & 0 deletions pfpsim/core/debugger/DebuggerIPCServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ class DebuggerIPCServer {
void handleCPCommand(PFPSimDebugger::CPCommandMsg msg);
void handleGetTableEntries();

void handleGetParsedPacket(int id);
void handleGetRawPacket(int id);
void handleGetPacketField(int id, std::string field_name);

/**
* Send pfpdb a generic reply so that it may regain control.
*/
Expand Down
Loading

0 comments on commit e440128

Please sign in to comment.