Skip to content

Commit

Permalink
Print location of each line when CLI debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
fwsGonzo committed Oct 26, 2023
1 parent 81d8ff0 commit 0ebbf9a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
17 changes: 10 additions & 7 deletions lib/libriscv/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,6 @@ union UnderAlign32
}
};

#define INSTRUCTION_LOGGING() \
if (this->verbose_instructions) { \
const auto string = cpu.to_string(instruction) + "\n"; \
machine.print(string.c_str(), string.size()); \
}

template<int W>
void DebugMachine<W>::simulate(uint64_t max)
{
Expand Down Expand Up @@ -398,7 +392,16 @@ void DebugMachine<W>::simulate(uint64_t max)
// Instructions may be unaligned with C-extension
const rv32i_instruction instruction =
rv32i_instruction { *(UnderAlign32*) &exec_seg_data[pc] };
INSTRUCTION_LOGGING();
if (this->verbose_instructions) {
auto string = cpu.to_string(instruction) + " ";
machine.memory.print_backtrace([&] (auto view) {
if (string.size() < 48)
string.resize(48, ' ');
string.append(view);
}, false);
string.append("\n");
machine.print(string.c_str(), string.size());
}

// We can't use decoder cache when translator is enabled
constexpr bool enable_cache = !binary_translation_enabled;
Expand Down
34 changes: 21 additions & 13 deletions lib/libriscv/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,35 +533,43 @@ namespace riscv
}
template <int W>
void Memory<W>::print_backtrace(
std::function<void(std::string_view)> print_function)
std::function<void(std::string_view)> print_function, bool ra)
{
auto print_trace =
[this, print_function] (const int N, const address_type<W> addr) {
// get information about the callsite
const auto site = this->lookup(addr);
// write information directly to stdout
char buffer[8192];
int len;
int len = 0;
if (N >= 0) {
len = snprintf(&buffer[len], sizeof(buffer)-len,
"[%d] ", N);
}
if constexpr (W == 4) {
len = snprintf(buffer, sizeof(buffer),
"[%d] 0x%08" PRIx32 " + 0x%.3" PRIx32 ": %s",
N, site.address, site.offset, site.name.c_str());
len += snprintf(&buffer[len], sizeof(buffer)-len,
"0x%08" PRIx32 " + 0x%.3" PRIx32 ": %s",
site.address, site.offset, site.name.c_str());
} else if constexpr (W == 8) {
len = snprintf(buffer, sizeof(buffer),
"[%d] 0x%016" PRIX64 " + 0x%.3" PRIx32 ": %s",
N, site.address, site.offset, site.name.c_str());
len += snprintf(&buffer[len], sizeof(buffer)-len,
"0x%016" PRIX64 " + 0x%.3" PRIx32 ": %s",
site.address, site.offset, site.name.c_str());
} else if constexpr (W == 16) {
len = snprintf(buffer, sizeof(buffer),
"[%d] 0x%016" PRIx64 " + 0x%.3" PRIx32 ": %s",
N, (uint64_t)site.address, site.offset, site.name.c_str());
len += snprintf(&buffer[len], sizeof(buffer)-len,
"0x%016" PRIx64 " + 0x%.3" PRIx32 ": %s",
(uint64_t)site.address, site.offset, site.name.c_str());
}
if (len > 0)
print_function({buffer, (size_t)len});
else
print_function("Scuffed frame. Should not happen!");
};
print_trace(0, this->machine().cpu.pc());
print_trace(1, this->machine().cpu.reg(REG_RA));
if (ra) {
print_trace(0, this->machine().cpu.pc());
print_trace(1, this->machine().cpu.reg(REG_RA));
} else {
print_trace(-1, this->machine().cpu.pc());
}
}

template <int W>
Expand Down
2 changes: 1 addition & 1 deletion lib/libriscv/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ namespace riscv
size_t size = 0;
};
Callsite lookup(address_t) const;
void print_backtrace(std::function<void(std::string_view)>);
void print_backtrace(std::function<void(std::string_view)>, bool ra = true);

// Counts all the memory used by execute segments, pages, etc.
size_t memory_usage_total() const;
Expand Down

0 comments on commit 0ebbf9a

Please sign in to comment.