Skip to content

Conversation

@kamahen
Copy link
Member

@kamahen kamahen commented Feb 8, 2024

No description provided.

op(100, fx, #)
]).
:- use_module(library(debug),[debug/3]).
:- use_module(library(prolog_stack)). % For catch_with_backtrace
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ordering of this use_module seems to be important.

main(Argv) :-
catch_with_backtrace(main0(Argv),
Error,
( set_prolog_flag(backtrace_goal_depth, 20),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 2 set_prolog_flag calls didn't have the desired effect ... but I couldn't find another flag for setting the option for the write.

This is what I got when running from cmake:

ERROR: [2,630] tex:item_commands([_484|_486],[],[_490|_492]) at /home/peter/src/swipl-devel/build.debug/home/library/ext/ltx2htm/latex2html.pl:983
ERROR: [1,904] tex:items([\(classitem,...),'\n'|...],[[...|...]|_554]) at /home/peter/src/swipl-devel/build.debug/home/library/ext/ltx2htm/latex2html.pl:961
ERROR: [1,890] tex:env(description([],['\n'|...]),[html('<dl class="latex">'),_624|...]) at /home/peter/src/swipl-devel/build.debug/home/library/ext/ltx2htm/latex2html.pl:935

but when I ran it by hand (calling main0/1), and did the debugger u command, I got more useful information:

   Exception: (1,887) tex:translate_env(description([], ['\n', \(classitem, [{'PlTerm'}]), '\n', 'Generic', 'Prolog', term|...]), file, file, _353630) ? 

which helped me figure out where the bad markup was.

@JanWielemaker
Copy link
Member

The catch_with_backtrace/3 is in any case not needed as that is already in main/0. I don't know whether it works properly. Adjusting the amount of information captured from the trace depends on the application and could be a good idea for this case. Although markup errors are better reported in some other way ...

If you want to change these, you cannot do that in the exception handler. That is too late. The stack is assembled after detecting we want a backtrace (because of catch_with_backtrace/3 or the exception is unhandled) and before the stack is unwound and the handler is called.

I think the way to go is that if there is a normal Prolog exception in the code, this needs to be debugged. In some cases temporary changing the stack printing is a way to find the issue.

@kamahen
Copy link
Member Author

kamahen commented Feb 9, 2024

It appears that the use_module(library(prolog_stack)) needs to come before use_module(library(main)) to get a backtrace.
I agree that a backtrace isn't a very good tool; but without that, all I had was one of the less useful error messages in Prolog: "Arguments are not sufficiently instantiated" error from functor/2, which needs a backtrace to figure out the cause.

Because I've fixed the underlying problem, I can't reproduce this error now. Anyway, this particular problem is at

item_commands(List, [], List) :-
    List = [Cmd|_],
    functor(Cmd, \, _),
    arg(1, Cmd, TexCmd),
    tex_command_function(TexCmd, item),
    !.

so the call to functor/3 could have a catch/3 around it and could print out the first few items after the Cmd, I suppose. Without the backtrace, there's no context at all for figuring things out.

So, does it make sense to at least add use_module(library(prolog_stack))?

@JanWielemaker
Copy link
Member

I see. It reproduces using this. Gets a nice backtrace with prolog_stack loaded and not without. I'm not sure where to change that though. I kind of dislike the Python approach to always print a backtrace. As a normal user of some app, it tends to be annoying.

:- use_module(library(prolog_stack)).
:- initialization(main, main).

main(_Argv) :-
    a(10).

a(N) :- N > 0, N1 is N-1, a(N1).
a(_) :-
    atom_concat(_,_,_).

You can also get the desired effect after deleting the use_module(library(prolog_stack)) and running

 swipl -g "use_module(library(prolog_stack))" t.pl

Possibly it does make sense for ltx2html to include it by default as it is not an end-user application anyway.

@kamahen
Copy link
Member Author

kamahen commented Feb 10, 2024

https://www.swi-prolog.org/pldoc/man?predicate=catch_with_backtrace/3 says:

This is good practice for a catch-all wrapper around an application.

This use of catch_with_backtrace/3 is for situations where you think you've handled all exceptions, to allow debugging the code (the particular bug that I triggered, for example, which gave an uninstantiated variable to functor/3). Anything else can be handled by a regular catch/3.

Presumably, we should also set debugger_write_options and/or answer_write_options so that the backtrace has sufficient context for figuring out what's wrong in the .tex file? (I apologize for not preserving the original file so that we could figure out the real problem - a bug in latex2html.pl)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants