Skip to content

Commit

Permalink
Use the function name info from the analysis to augment the stack tra…
Browse files Browse the repository at this point in the history
…ce. Fix Vector35#353
  • Loading branch information
xusheng6 committed Oct 11, 2024
1 parent cd136d5 commit d1f0e0e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
42 changes: 41 additions & 1 deletion core/debuggerstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,38 @@ void DebuggerThreads::MarkDirty()
}


void DebuggerThreads::SymbolizeFrames(std::vector<DebugFrame>& frames)
{
if (!m_state || !m_state->GetController())
return;

auto data = m_state->GetController()->GetData();
if (!data)
return;

for (DebugFrame& frame: frames)
{
// Try to find a better symbol than the one provided by the debugger backend
auto funcs = data->GetAnalysisFunctionsContainingAddress(frame.m_pc);
if (!funcs.empty())
{
auto func = funcs[0];
if (!func)
continue;

frame.m_functionStart = func->GetStart();
auto symbol = func->GetSymbol();
if (symbol)
frame.m_functionName = symbol->GetShortName();
else
frame.m_functionName = fmt::format("sub_{:x}", func->GetStart());

continue;
}
}
}


void DebuggerThreads::Update()
{
if (!m_state)
Expand All @@ -166,7 +198,9 @@ void DebuggerThreads::Update()
std::vector<DebugThread> newThreads = adapter->GetThreadList();
for (auto thread = newThreads.begin(); thread != newThreads.end(); thread++)
{
m_frames[thread->m_tid] = adapter->GetFramesOfThread(thread->m_tid);
auto frames = adapter->GetFramesOfThread(thread->m_tid);
SymbolizeFrames(frames);
m_frames[thread->m_tid] = frames;

// update thread states in new thread list
auto oldThread = std::find_if(m_threads.begin(), m_threads.end(), [&](DebugThread const& t) {
Expand Down Expand Up @@ -957,6 +991,12 @@ void DebuggerState::SetInputFile(const std::string& path)
}


std::string DebuggerState::GetInputFile()
{
return m_inputFile;
}


void DebuggerState::SetWorkingDirectory(const std::string& directory)
{
m_workingDirectory = directory;
Expand Down
2 changes: 2 additions & 0 deletions core/debuggerstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ namespace BinaryNinjaDebugger {
std::vector<DebugFrame> GetFramesOfThread(uint32_t tid);
bool SuspendThread(std::uint32_t tid);
bool ResumeThread(std::uint32_t tid);
void SymbolizeFrames(std::vector<DebugFrame>& frames);
};

enum MemoryByteCacheStatus
Expand Down Expand Up @@ -212,6 +213,7 @@ namespace BinaryNinjaDebugger {
void SetAdapterType(const std::string& adapter);
void SetExecutablePath(const std::string& path);
void SetInputFile(const std::string& path);
std::string GetInputFile();
void SetWorkingDirectory(const std::string& directory);
void SetCommandLineArguments(const std::string& arguments);
void SetRemoteHost(const std::string& host);
Expand Down
15 changes: 12 additions & 3 deletions ui/threadframes.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ limitations under the License.
#include "debuggerapi.h"
#include "inttypes.h"
#include "ui.h"
#include "fmt/format.h"


using namespace BinaryNinjaDebuggerAPI;
Expand All @@ -49,10 +50,18 @@ class FrameItem
m_isFrame(true), m_tid(thread.m_tid), m_threadPc(thread.m_rip), m_frameIndex(frame.m_index),
m_module(frame.m_module), m_framePc(frame.m_pc), m_sp(frame.m_sp), m_fp(frame.m_fp), m_parentItem(parentItem)
{
uint64_t offset = frame.m_pc - frame.m_functionStart;
QString funcName = QString::asprintf("%s + 0x%" PRIx64, frame.m_functionName.c_str(), offset);
// WinDbg always reports the function name with the module prefix, we remove it for conciseness
auto trimmedFunctionName = frame.m_functionName;
auto prefix = m_module + '!';
if (trimmedFunctionName.compare(0, prefix.size(), prefix) == 0)
trimmedFunctionName.erase(0, prefix.size());

m_function = funcName.toStdString();
// Only show the offset if it is not 0x0
uint64_t offset = frame.m_pc - frame.m_functionStart;
if (offset != 0)
m_function = fmt::format("{} + {:#x}", trimmedFunctionName, offset);
else
m_function = trimmedFunctionName;
}

~FrameItem();
Expand Down

0 comments on commit d1f0e0e

Please sign in to comment.