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

查看:186
本文介绍了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 很多东西在到达发生错误的地方之前。

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.

有什么想法,如何有这个堆栈跟踪?或动态跟踪 / 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的调试器解决这个问题。但是,如果您看不到所有的打印,或者太慢,您可能会考虑以下调试器。

@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".

我个人不会使用由Prolog系统提供的调试器,原因很简单,因为它们大部分打印得太多,通常都是自带的bug,并且有自己特定的不断变化的约定,我无法学习。

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

* removes the goal. This is for generalizing a program doing program modification/slicing in a pure monotonic program. For examples how to use it, see the following answers/debugging sessions 1, 2, 3, 4, 5, 6, 7, 8.

_ / * term * / 用匿名变量替换术语。这个概括了一个甚至比 * 更远的程序。示例会话:
1
2
3

_/*term*/ replaces a term by an anonymous variable. This generalizes a program even further than * alone. Example sessions: 1, 2, 3.

以这种方式,您可以减少您所观看的信息。

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天全站免登陆