From 0094a5937fadec30e0ce71b21989108ad2680209 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 3 Oct 2021 12:00:17 +0200 Subject: [PATCH] Break on exception. --- src/AngelscriptDebugger.cpp | 38 ++++++++++++++++++++--------- src/DebugAdapterProtocol/Events.hpp | 8 +++--- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/AngelscriptDebugger.cpp b/src/AngelscriptDebugger.cpp index 0f584ad..0d3ac74 100644 --- a/src/AngelscriptDebugger.cpp +++ b/src/AngelscriptDebugger.cpp @@ -55,15 +55,22 @@ size_t GetBreakpointHash(const std::string& section, size_t line) { return ((hash()(section) ^ (hash()(line) << 1)) >> 1); } -void AngelscriptDebugger::on_exception_callback(asIScriptContext*, AngelscriptDebugger*) { - return; - // const char* scriptSection = nullptr; - // int column = 0; - // int line = ctx->GetExceptionLineNumber(&column, &scriptSection); - // if (line == 0) - // return; - // auto b = Breakpoint{.Section = scriptSection, .Line = line}; - // d->EnterBreakpoint(ctx, b); +void AngelscriptDebugger::on_exception_callback(asIScriptContext* ctx, AngelscriptDebugger* d) { + ctx->Suspend(); + d->_pausedContexts.push_back(ctx); + + const char* scriptSection = nullptr; + int column = 0; + int line = ctx->GetLineNumber(0, &column, &scriptSection); + if (line == 0) + return; + + auto exception = ctx->GetExceptionString(); + + auto* o = new DebugAdapterProtocol::StoppedEvent( + new DebugAdapterProtocol::StoppedEventBody("exception", "Paused on exception", exception)); + + d->Send(o); } void AngelscriptDebugger::EnterBreakpoint(asIScriptContext* ctx, const std::string& section, size_t line) { @@ -71,7 +78,7 @@ void AngelscriptDebugger::EnterBreakpoint(asIScriptContext* ctx, const std::stri _pausedContexts.push_back(ctx); auto* o = new DebugAdapterProtocol::StoppedEvent( - new DebugAdapterProtocol::StoppedEventBody(GetBreakpointHash(section, line))); + new DebugAdapterProtocol::StoppedEventBody("breakpoint", GetBreakpointHash(section, line))); Send(o); } @@ -244,7 +251,12 @@ void AngelscriptDebugger::OnRequest(asio::ip::tcp::socket* client, DebugAdapterP auto func = ctx->GetFunction(i); const char* scriptSection = nullptr; int column = 0; - int line = ctx->GetLineNumber(i, &column, &scriptSection); + int line; + if (i == 0 && ctx->GetExceptionString() != nullptr) { + line = ctx->GetExceptionLineNumber(&column, &scriptSection); + } else { + line = ctx->GetLineNumber(i, &column, &scriptSection); + } stackTrace.emplace_back(i, func->GetName(), DebugAdapterProtocol::Source(GetResolvedScriptPath(scriptSection)), line, column); @@ -285,6 +297,10 @@ void AngelscriptDebugger::OnRequest(asio::ip::tcp::socket* client, DebugAdapterP auto c = e->CreateContext(); if (holds_alternative>(variant)) { + if (ctx->GetExceptionString() != nullptr) { + variables.push_back( + DebugAdapterProtocol::Variable("exception", ctx->GetExceptionString(), "exception", {})); + } auto frameId = std::get>(variant)->StackLevel; // auto scopeType = get(variant).Type; auto varCount = ctx->GetVarCount(frameId); diff --git a/src/DebugAdapterProtocol/Events.hpp b/src/DebugAdapterProtocol/Events.hpp index eed740a..239387c 100644 --- a/src/DebugAdapterProtocol/Events.hpp +++ b/src/DebugAdapterProtocol/Events.hpp @@ -188,10 +188,10 @@ namespace DebugAdapterProtocol { std::optional allThreadsStopped = true; std::optional> hitBreakpointIds; - explicit StoppedEventBody(size_t breakpoint) { - reason = "breakpoint"; - hitBreakpointIds = {breakpoint}; - } + explicit StoppedEventBody(std::string reason, size_t breakpoint) + : reason(std::move(reason)), hitBreakpointIds({breakpoint}) {} + explicit StoppedEventBody(std::string reason, std::string description, std::string text) + : reason(std::move(reason)), description(std::move(description)), text(std::move(text)) {} nlohmann::json ToJson() const override { auto o = EventBody::ToJson();