Attribute variables and custom debug info

Hello,

I have an identifier that is passed around in various dynamic structures. To help debug, I’d like to have visible various related information, but without filling my program with various debug related predicates – could i use attribute variables, somehow, to centralize the printing of debug information across the many predicates that process the identifier?

any thoughts are much appreciated,

Dan

What’s the nature of the identifier? An atomic term? A compound term?

its an atom

Attributed variables associate attributes with variables.

Maybe use the recorded database to associate terms with the information that you to record with the identifier? See https://www.swi-prolog.org/pldoc/man?section=recdb For example:

?- recorda(foo, a(1), _).
true.

?- recorda(foo, b(2), _).
true.

?- recorded(foo, T, _).
T = b(2) ;
T = a(1).

Still, is easy to have debug printing messages that are only active when debugging.

Thank you.

What i have in mind is something like an aspectual capability, where i can define a “point-cut” – a location in the code where, when reached, some predicate is called – without including an explicit call at that location.

Key is that all this is defined outside of the program in a separate aspectual-module.

I thought that attribute variables could be a mechanism for this.

Dan

A number of solutions are possible but the most appropriated one depends on the details of the code that you want to instrument. I know a few using Logtalk’s categories for hot patching and using the event-driven programming support. The low level SWI-Prolog machinery that is used for tracing also may provide a solution.

1 Like

Followup.

I added a simple example of Aspect-Oriented Programming in Logtalk reusing the familiar bank transfer example:

https://github.com/LogtalkDotOrg/logtalk3/tree/master/examples/aspects

Hot patching via a category is used to limit transfer amounts and events are used to generate logging messages. Note that there are other AOP examples in the distribution (e.g. blocks and searching).

I also failed to mention in my previous message SWI-Prolog support for wrapping predicates:

https://www.swi-prolog.org/pldoc/doc/\_SWI_/library/prolog_wrap.pl

This is really nice, how easily event broadcast can be used for modularizing aspectual functionality.

Any idea what the overhead is for such a feature – although, as long as its for debugging it would matter – i presume that it can be swiched off (selectively?).

Dan

You can turn on/off events dynamically at runtime. See:

https://logtalk.org/manuals/userman/events.html

There’s a sub-section on performance. See also:

https://logtalk.org/manuals/userman/performance.html#messages

Usually you only generate events for selected objects (senders) and messages so it can be as selective as you want. But, as your main use case is debugging, I would not worry much about performance. After debugging, you can simply comment out the calls to define_events/5 and turn off events globally if not used for anything else (actually, that’s the default). Similar for using categories to implement aspects via hot patching, which is also disallowed by default.