gprolog:在异常后获取堆栈跟踪 [英] gprolog: Getting a stacktrace after an exception

查看:35
本文介绍了gprolog:在异常后获取堆栈跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 gprolog 时,我经常遇到没有任何类型的行号或上下文的异常,例如:

While using gprolog I often have exceptions without any kind of line numbers or context like this one:

uncaught exception: error(instantiation_error,(is)/2)

没有任何上下文.我知道我可以做一个 trace 但是用 trace 调试它需要很长时间,因为在到达错误发生的地方之前我需要执行很多事情.

Without any kind of context. I know I can do a trace but it would take very long to debug it with trace since I need to execute a lot of things before getting to the place where the error occur.

知道如何使用此堆栈跟踪吗?还是动态的trace/notrace ?

Any idea on how to have this stacktrace ? Or a dynamic trace / notrace ?

或者只是自动打印整个 trace 输出.

Or just automate the printing of the whole trace output.

推荐答案

@gusbro 的回答 (s(X)) 向您展示了如何使用 GNU 的调试器在一定程度上解决这个问题.但是,如果您无法看到所有的打印,或者打印速度太慢,您可以考虑使用以下调试器":可作为 library(debug) 在 Scryer 中使用.

@gusbro's answer (s(X)) shows you how you somewhat solve this with GNU's debugger. However, if you cannot afford to see all the printing going on, or it is much too slow, you might consider the following "debugger" available as library(debug) in Scryer.

我个人不使用 Prolog 系统提供的调试器,原因很简单,因为它们中的大多数打印太多,本身经常有问题,并且有自己特定的不断变化的约定,我负担不起学习.

I personally do not use debuggers offered by Prolog systems for the simple reason that most of them print much too much, are often buggy themselves, and have their own specific ever changing conventions, I can't afford to learn.

:- op(900, fx, [@,$,$-]).

$-(G_0) :-
   catch(G_0, Ex, ( portray_clause(exception:Ex:G_0), throw(Ex) ) ).

$(G_0) :-
   portray_clause(call:G_0),
   $-G_0,
   portray_clause(exit:G_0).

@(G_0) :-
   (   $-G_0
   *-> true
   ;   portray_clause(badfail:G_0),
       throw(goal_failed(G_0))
   ).

:- op(950, fy, *).
*(_).

要使用它,只需在特定目标前添加 $-$@.

To use it, simply add $-, $, or @ in front of a specific goal.

$- 表示:仅通过此目标发出异常信号

$- means: only signal exceptions going through this goal

$另外显示调用和退出

@ 保证至少有一个答案,如果没有则上报并抛出异常.

@ assures that there is at least one answer, and if not, it is reported and an exception is thrown.

谨慎使用以上注释!

* 删除目标.这是为了概括一个程序进行程序修改/ 在纯单调程序中.在意外失败的情况下,您需要这样做.有关如何使用它的示例,请参阅以下答案/调试会话1,2,3,4,5,6,7,8,9,10.

* removes the goal. This is for generalizing a program doing program modification/program-slicing in a pure monotonic program. You need this in case of unexpected failures. For examples how to use it, see the following answers/debugging sessions 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.

_/*term*/ 用匿名变量替换术语.这比单独的 * 更能概括一个程序.示例会话:1,2,3,4,5,6,7,8,9,10.

_/*term*/ replaces a term by an anonymous variable. This generalizes a program even further than * alone. Example sessions: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.

通过这种方式,您可以显着减少观看的信息.

In this manner you can reduce the information you watch significantly.

在其他支持 meta_predicate 指令的系统中,如 SICStus、YAP 和 SWI,在前面添加以下指令:

In other systems supporting meta_predicate directives like SICStus, YAP and SWI, add in front the following directive:

:- meta_predicate(( $-(0), $(0), @(0) )).

这篇关于gprolog:在异常后获取堆栈跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆