Skip to content

Commit

Permalink
tracing: adding protocol serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
janweinstock committed Jun 17, 2024
1 parent ee8ff13 commit c102a23
Showing 1 changed file with 248 additions and 5 deletions.
253 changes: 248 additions & 5 deletions src/vcml/tracing/tracer_inscight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,14 @@ string serialize(const PAYLOAD& tx) {

template <>
string serialize(const tlm_generic_payload& tx) {
std::ostringstream os;
os << std::hex << std::showbase;
ostringstream os;

os << "{";
os << "\"address\":\"" << tx.get_address() << "\",";
os << "\"address\":" << tx.get_address() << ",";
os << "\"data\":[";
unsigned char* data_ptr = tx.get_data_ptr();
for (unsigned int i = 0; i < tx.get_data_length(); ++i) {
os << "\"" << (u32)data_ptr[i] << "\"";
os << (u32)data_ptr[i];
if (i < tx.get_data_length() - 1)
os << ",";
}
Expand All @@ -98,7 +97,7 @@ string serialize(const tlm_generic_payload& tx) {
os << "\"byte_enable\":[";
unsigned char* byte_enable_ptr = tx.get_byte_enable_ptr();
for (unsigned int i = 0; i < tx.get_byte_enable_length(); ++i) {
os << "\"" << (u32)byte_enable_ptr[i] << "\"";
os << (u32)byte_enable_ptr[i];
if (i < tx.get_byte_enable_length() - 1)
os << ",";
}
Expand Down Expand Up @@ -129,6 +128,250 @@ string serialize(const tlm_generic_payload& tx) {
return os.str();
}

template <>
string serialize(const gpio_payload& tx) {
ostringstream os;
os << "{\"state\":" << (tx.state ? "true" : "false");

if (tx.vector != GPIO_NO_VECTOR)
os << ",\"vector\":" << tx.vector;

os << "}";
return os.str();
}

template <>
string serialize(const clk_payload& tx) {
ostringstream os;
os << "{";
os << "\"newhz\":" << tx.newhz << ",";
os << "\"oldhz\":" << tx.oldhz;
os << "}";
return os.str();
}

template <>
string serialize(const pci_payload& tx) {
ostringstream os;
os << "{";
os << "\"command\":\"" << pci_command_str(tx.command) << "\",";
os << "\"response\":\"" << pci_response_str(tx.response) << "\",";
os << "\"address_space\":\"" << pci_address_space_str(tx.space) << "\",";
os << "\"address\":" << tx.addr << ",";

os << "\"data\":[";
if (tx.size > 0) {
u32 i, size = min<u32>(tx.size, sizeof(tx.data));
for (i = 0; i < size - 1; i++)
os << ((tx.data >> (i * 8)) & 0xff) << ",";
os << ((tx.data >> (i * 8)) & 0xff);
}
os << "],";

os << "\"debug\":" << (tx.debug ? "true" : "false");
os << "}";
return os.str();
}

template <>
string serialize(const i2c_payload& tx) {
ostringstream os;
os << "{";
os << "\"command\":\"" << i2c_command_str(tx.cmd) << "\",";
os << "\"response\":\"" << i2c_response_str(tx.resp) << "\",";
os << "\"data\":" << (int)tx.data;
os << "}";
return os.str();
}

template <>
string serialize(const spi_payload& tx) {
ostringstream os;
os << "{";
os << "\"miso\":" << (int)tx.miso << ",";
os << "\"mosi\":" << (int)tx.mosi;
os << "}";
return os.str();
}

template <>
string serialize(const sd_command& tx) {
ostringstream os;
os << "{";

if (tx.appcmd)
os << "\"command\":\"SD_APP_CMD\",";
else
os << "\"command\":\"SD_CMD\",";

os << "\"opcode\":\"" << sd_opcode_str(tx.opcode, tx.appcmd) << "\",";
os << "\"argument\":" << tx.argument << ",";
os << "\"crc\":" << (int)tx.crc << ",";
os << "\"spi\":" << (tx.spi ? "true" : "false") << ",";
os << "\"response\":[";

size_t len = min<size_t>(tx.resp_len, MWR_ARRAY_SIZE(tx.response));
if (len > 0) {
for (size_t i = 0; i < len - 1; i++)
os << (int)tx.response[i] << ",";
os << (int)tx.response[len - 1];
}

os << "],";
os << "\"status:\":\"" << sd_status_str(tx.status) << "\"";
os << "}";
return os.str();
}

template <>
string serialize(const sd_data& tx) {
ostringstream os;
os << "{";

switch (tx.mode) {
case SD_READ:
os << "\"command\":\"SD_DATA_READ\",";
if (success(tx))
os << "\"data\":" << (int)tx.data << ",";
os << "\"status\":\"" << sd_status_str(tx.status.read) << "\"";
break;

case SD_WRITE:
os << "\"command\":\"SD_DATA_WRITE\",";
os << "\"data\":" << (int)tx.data << ",";
os << "\"status\":\"" << sd_status_str(tx.status.write) << "\"";
break;

default:
break;
}

os << "}";
return os.str();
}

template <>
string serialize(const vq_message& tx) {
ostringstream os;
os << "{";

os << "\"index\":" << tx.index << ",";

os << "\"input_buffers\":[";
for (size_t i = 0; i < tx.in.size(); i++) {
os << "{";
os << "\"addr\":" << tx.in[i].addr << ",\"size\":" << tx.in[i].size;
os << "}";
if (i < tx.in.size() - 1)
os << ",";
}
os << "],";

os << "\"output_buffers\":[";
for (size_t i = 0; i < tx.out.size(); i++) {
os << "{";
os << "\"addr\":" << tx.out[i].addr << ",\"size\":" << tx.out[i].size;
os << "}";
if (i < tx.out.size() - 1)
os << ",";
}
os << "],";

os << "\"status\":\"" << virtio_status_str(tx.status) << "\"";

os << "}";
return os.str();
}

template <>
string serialize(const serial_payload& tx) {
ostringstream os;
os << "{";
os << "\"data\":" << (tx.data & tx.mask) << ",";
os << "\"bits\":" << tx.width << ",";
os << "\"baud\":" << tx.baud << ",";
os << "\"parity\":\"" << serial_parity_str(tx.parity) << "\",";
os << "\"stop\":\"" << serial_stop_str(tx.stop) << "\"";
os << "}";
return os.str();
}

template <>
string serialize(const eth_frame& tx) {
ostringstream os;
os << "{";

os << "\"type\":\"" << tx.identify() << "\"";

if (!tx.valid()) {
os << "}";
return os.str();
}

os << ",";

os << "\"sourceaddr\":\"" << tx.source() << "\",";
os << "\"destaddr\":\"" << tx.destination() << "\",";

os << "\"data\":[";
for (size_t i = 0; i < tx.size(); i++) {
os << (int)tx[i];
if (i < tx.size() - 1)
os << ",";
}
os << "]";

os << "}";
return os.str();
}

template <>
string serialize(const can_frame& tx) {
ostringstream os;
os << "{";

if (tx.is_fdf())
os << "\"type\":\"CAN_FD\",";
else
os << "\"type\":\"CAN\",";

os << "\"flags\":" << (int)tx.flags << ",";
os << "\"rtr\":" << (tx.is_rtr() ? "true" : "false") << ",";
os << "\"err\":" << (tx.is_err() ? "true" : "false") << ",";
os << "\"brs\":" << (tx.is_brs() ? "true" : "false") << ",";
os << "\"esi\":" << (tx.is_esi() ? "true" : "false") << ",";

os << "\"data\":[";
for (size_t i = 0; i < tx.length(); i++) {
os << (int)tx.data[i];
if (i < tx.length() - 1)
os << ",";
}
os << "]";

os << "}";
return os.str();
}

template <>
string serialize(const usb_packet& tx) {
ostringstream os;
os << "{";
os << "\"token\":\"" << usb_token_str(tx.token) << "\",";
os << "\"addr\":" << tx.addr << ",";
os << "\"endpoint\":" << tx.epno << ",";
os << "\"data\":[";
for (size_t i = 0; i < tx.length; i++) {
os << (int)tx.data[i];
if (i < tx.length - 1)
os << ",";
}
os << "],";
os << "\"status\":\"" << usb_result_str(tx.result) << "\"";
os << "}";
return os.str();
}

template <typename PAYLOAD>
void tracer_inscight::do_trace(const activity<PAYLOAD>& msg) {
auto json = serialize(msg.payload);
Expand Down

0 comments on commit c102a23

Please sign in to comment.