Compare commits

...

1 Commits

Author SHA1 Message Date
Benjamin Doherty
031cd302dd Capture command callstacks for debugging 2023-10-30 14:42:24 -07:00
4 changed files with 34 additions and 6 deletions

View File

@@ -73,14 +73,32 @@ public:
// a cost here (writing and reading the stack at each iteration), in the end it's
// probably better to pay the cost at just one location.
intptr_t next;
driver.mCurrentExecutingCommand = this;
mExecute(driver, this, &next);
return reinterpret_cast<CommandBase*>(reinterpret_cast<intptr_t>(this) + next);
}
inline void captureCallstack() noexcept {
auto c = utils::CallStack::unwind(4);
size_t i = 0;
for (; i < c.getFrameCount() && i < 16; i++) {
mCallstack[i] = c[i];
}
for (; i < 16; i++) {
mCallstack[i] = 0;
}
}
void printCallstack() noexcept {
auto c = utils::CallStack(mCallstack);
utils::slog.d << c << utils::io::endl;
}
inline ~CommandBase() noexcept = default;
private:
Execute mExecute;
std::array<intptr_t, 16> mCallstack = {0};
};
// ------------------------------------------------------------------------------------------------
@@ -218,6 +236,7 @@ public:
using Cmd = COMMAND_TYPE(methodName); \
void* const p = allocateCommand(CommandBase::align(sizeof(Cmd))); \
new(p) Cmd(mDispatcher.methodName##_, APPLY(std::move, params)); \
((Cmd*)p)->captureCallstack(); \
DEBUG_COMMAND_END(methodName, false); \
}
@@ -237,6 +256,7 @@ public:
using Cmd = COMMAND_TYPE(methodName##R); \
void* const p = allocateCommand(CommandBase::align(sizeof(Cmd))); \
new(p) Cmd(mDispatcher.methodName##_, RetType(result), APPLY(std::move, params)); \
((Cmd*)p)->captureCallstack(); \
DEBUG_COMMAND_END(methodName, false); \
return result; \
}

View File

@@ -53,6 +53,7 @@ template<typename T>
class ConcreteDispatcher;
class Dispatcher;
class CommandStream;
class CommandBase;
class Driver {
public:
@@ -83,6 +84,8 @@ public:
virtual void debugCommandEnd(CommandStream* cmds,
bool synchronous, const char* methodName) noexcept = 0;
CommandBase* mCurrentExecutingCommand = nullptr;
/*
* Asynchronous calls here only to provide a type to CommandStream. They must be non-virtual
* so that calling the concrete implementation won't go through a vtable.

View File

@@ -36,6 +36,13 @@ public:
* @see CallStack::capture()
*/
CallStack() = default;
template <unsigned long N>
explicit CallStack(const std::array<intptr_t, N>& symbols)
: m_frame_count(N) {
for (size_t i = 0; i < N; i++) {
m_stack[i] = symbols[i];
}
}
CallStack(const CallStack&) = default;
~CallStack() = default;
@@ -114,12 +121,10 @@ private:
static constexpr size_t NUM_FRAMES = 20;
struct StackFrameInfo {
intptr_t pc;
};
using StackFrameInfo = intptr_t;
size_t m_frame_count = 0;
StackFrameInfo m_stack[NUM_FRAMES];
StackFrameInfo m_stack[NUM_FRAMES] = {0};
};
} // namespace utils

View File

@@ -70,7 +70,7 @@ intptr_t CallStack::operator[](size_t index) const {
#endif
std::abort();
}
return m_stack[index].pc;
return m_stack[index];
}
size_t CallStack::getFrameCount() const noexcept {
@@ -91,7 +91,7 @@ void CallStack::update_gcc(size_t ignore) noexcept {
size -= ignore;
#endif
for (ssize_t i = 0; i < size; i++) {
m_stack[i].pc = intptr_t(array[ignore + i]);
m_stack[i] = intptr_t(array[ignore + i]);
}
size--; // the last one seems to always be 0x0