From f8c88a75df041348be8426c6dc2c0ba99e571ad4 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Mon, 1 Jan 2007 03:40:29 +0000 Subject: [PATCH] implemented debugger --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40251 --- core/vm/sp_vm_basecontext.cpp | 3 +- core/vm/sp_vm_engine.cpp | 60 +++++++++++++++++++++-------------- core/vm/sp_vm_engine.h | 5 +-- 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/core/vm/sp_vm_basecontext.cpp b/core/vm/sp_vm_basecontext.cpp index 2a496b565..76b97240d 100644 --- a/core/vm/sp_vm_basecontext.cpp +++ b/core/vm/sp_vm_basecontext.cpp @@ -767,7 +767,8 @@ int BaseContext::LookupLine(ucell_t addr, uint32_t *line) return SP_ERROR_NOT_FOUND; } - *line = ctx->lines[low].line; + /* Since the CIP occurs BEFORE the line, we have to add one */ + *line = ctx->lines[low].line + 1; return SP_ERROR_NONE; } diff --git a/core/vm/sp_vm_engine.cpp b/core/vm/sp_vm_engine.cpp index ae5e0c1ab..cb6cf7002 100644 --- a/core/vm/sp_vm_engine.cpp +++ b/core/vm/sp_vm_engine.cpp @@ -63,7 +63,7 @@ SourcePawnEngine::~SourcePawnEngine() while (m_FreedCalls) { pTemp = m_FreedCalls->next; - delete pTemp; + delete m_FreedCalls; m_FreedCalls = pTemp; } } @@ -386,15 +386,10 @@ TracedCall *SourcePawnEngine::MakeTracedCall(bool new_chain) /* Unlink the head node from the free list */ pCall = m_FreedCalls; m_FreedCalls = m_FreedCalls->next; - if (m_FreedCalls) - { - m_FreedCalls->prev = NULL; - } } /* Link as the head node into the call stack */ pCall->next = m_CallStack; - pCall->prev = NULL; if (new_chain) { @@ -403,10 +398,6 @@ TracedCall *SourcePawnEngine::MakeTracedCall(bool new_chain) pCall->chain = m_CurChain; } - if (m_CallStack) - { - m_CallStack->prev = pCall; - } m_CallStack = pCall; return pCall; @@ -418,10 +409,6 @@ void SourcePawnEngine::FreeTracedCall(TracedCall *pCall) if (pCall == m_CallStack) { m_CallStack = m_CallStack->next; - if (m_CallStack) - { - m_CallStack->prev = NULL; - } } /* Add this to our linked list of freed calls */ @@ -429,11 +416,8 @@ void SourcePawnEngine::FreeTracedCall(TracedCall *pCall) { m_FreedCalls = pCall; m_FreedCalls->next = NULL; - m_FreedCalls->prev = NULL; } else { pCall->next = m_FreedCalls; - pCall->prev = NULL; - m_FreedCalls->prev = pCall; m_FreedCalls = pCall; } } @@ -465,6 +449,7 @@ void SourcePawnEngine::RunTracer(sp_context_t *ctx, uint32_t frame, uint32_t cod * so we have to push a new call onto our list. */ TracedCall *pCall = MakeTracedCall(false); + pCall->ctx = ctx; pCall->frm = frame; } else if (m_CallStack->frm < frame) { /* The last frame has moved up the stack, @@ -483,7 +468,14 @@ void SourcePawnEngine::PopTracer(int error, const char *msg) if (error != SP_ERROR_NONE && m_pDebugHook) { - CContextTrace trace(m_CallStack, error, msg); + uint32_t native = INVALID_CIP; + + if (m_CallStack->ctx->n_err) + { + native = m_CallStack->ctx->n_idx; + } + + CContextTrace trace(m_CallStack, error, msg, native); m_pDebugHook->OnContextExecuteError(m_CallStack->ctx->context, &trace); } @@ -496,8 +488,8 @@ void SourcePawnEngine::PopTracer(int error, const char *msg) m_CurChain--; } -CContextTrace::CContextTrace(TracedCall *pStart, int error, const char *msg) : - m_Error(error), m_pMsg(msg), m_pStart(pStart), m_pIterator(pStart) +CContextTrace::CContextTrace(TracedCall *pStart, int error, const char *msg, uint32_t native) : + m_Error(error), m_pMsg(msg), m_pStart(pStart), m_pIterator(pStart), m_Native(native) { } @@ -534,7 +526,7 @@ void CContextTrace::ResetTrace() bool CContextTrace::GetTraceInfo(CallStackInfo *trace) { - if (m_pIterator->chain != m_pStart->chain) + if (!m_pIterator || (m_pIterator->chain != m_pStart->chain)) { return false; } @@ -547,8 +539,6 @@ bool CContextTrace::GetTraceInfo(CallStackInfo *trace) IPluginContext *pContext = m_pIterator->ctx->context; IPluginDebugInfo *pInfo = pContext->GetDebugInfo(); - m_pIterator = m_pIterator->next; - if (!pInfo) { return false; @@ -556,6 +546,7 @@ bool CContextTrace::GetTraceInfo(CallStackInfo *trace) if (!trace) { + m_pIterator = m_pIterator->next; return true; } @@ -574,5 +565,28 @@ bool CContextTrace::GetTraceInfo(CallStackInfo *trace) trace->line = 0; } + m_pIterator = m_pIterator->next; + return true; } + +const char *CContextTrace::GetLastNative(uint32_t *index) +{ + if (m_Native == INVALID_CIP) + { + return NULL; + } + + sp_native_t *native; + if (m_pIterator->ctx->context->GetNativeByIndex(m_Native, &native) != SP_ERROR_NONE) + { + return NULL; + } + + if (index) + { + *index = m_Native; + } + + return native->name; +} diff --git a/core/vm/sp_vm_engine.h b/core/vm/sp_vm_engine.h index b25ec729e..ca0ac7f3f 100644 --- a/core/vm/sp_vm_engine.h +++ b/core/vm/sp_vm_engine.h @@ -11,7 +11,6 @@ namespace SourcePawn uint32_t frm; sp_context_t *ctx; TracedCall *next; - TracedCall *prev; unsigned int chain; }; @@ -19,7 +18,7 @@ namespace SourcePawn class CContextTrace : public IContextTrace { public: - CContextTrace(TracedCall *pStart, int error, const char *msg); + CContextTrace(TracedCall *pStart, int error, const char *msg, uint32_t native); public: virtual int GetErrorCode(); virtual const char *GetErrorString(); @@ -27,11 +26,13 @@ namespace SourcePawn virtual const char *GetCustomErrorString(); virtual bool GetTraceInfo(CallStackInfo *trace); virtual void ResetTrace(); + virtual const char *GetLastNative(uint32_t *index); private: TracedCall *m_pStart; TracedCall *m_pIterator; const char *m_pMsg; int m_Error; + uint32_t m_Native; };