# Using prolog\_trace\_interception/4

**URL:** <https://swi-prolog.discourse.group/t/using-prolog-trace-interception-4/4376>\
**Category:** Predicate\
**Created:** [September 3, 2021, 3:59pm UTC](https://swi-prolog.discourse.group/t/using-prolog-trace-interception-4/4376 "2021-09-03T15:59:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ridgeworks](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ridgeworks/32/886_2.png) [@ridgeworks](https://swi-prolog.discourse.group/u/ridgeworks)\
**Post date:** [September 3, 2021, 3:59pm UTC](https://swi-prolog.discourse.group/t/using-prolog-trace-interception-4/4376/1 "2021-09-03T15:59:58Z")

</div>

I’d like to use `prolog_trace_interception/4` to construct a custom tracing facility within a module. The documentation suggests this was a dynamic predicate so I was expecting it to work much like the `exception/3` hook (multifile, dynamic). When defined, it appears to be static (using `predicate_property`) although it works as intended. However, this prevents another module from doing the same (redefined static procedure warning).

Bug or misunderstanding?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [September 3, 2021, 4:17pm UTC](https://swi-prolog.discourse.group/t/using-prolog-trace-interception-4/4376/2 "2021-09-03T16:17:39Z")

</div>

It is actually initially not defined at all. That could be a good idea, so you can define it as static if you don’t want anyone to mess with your definition or dynamic if you do not mind. Different definitions typically cooperate poorly.

Note that if you want basically a normal trace but you to hide some details and present things a little different, there are other ways to achieve that.

---

<div class="post-metadata">

**Author:** ![ridgeworks](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ridgeworks/32/886_2.png) [@ridgeworks](https://swi-prolog.discourse.group/u/ridgeworks)\
**Post date:** [September 3, 2021, 4:50pm UTC](https://swi-prolog.discourse.group/t/using-prolog-trace-interception-4/4376/3 "2021-09-03T16:50:46Z")

</div>

I thought I was trying to define it dynamic, multifile, but was doing it wrong. Now works fine, just need to declare it as such in each module.

What I’m trying to do is trace instructions in a VM implemented in Prolog (like local propagation in `clpBNR`) without incurring overhead when not tracing. So effectively I’m trying to trace a single (local) predicate within the VM implementation. And I’d like to do so while minimally impacting normal debug functionality or other VM’s that might be running at the same time. So the instances of `prolog_trace_interception` just need to fail if the “instruction” isn’t one of interest, analogous to using `exception` for initializing global vars. `prolog_trace_interception/4` actually seems to work pretty well for this, now that I’ve declared it properly.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [September 3, 2021, 5:08pm UTC](https://swi-prolog.discourse.group/t/using-prolog-trace-interception-4/4376/4 "2021-09-03T17:08:56Z")

</div>

> [@ridgeworks](#):
>
> So the instances of `prolog_trace_interception` just need to fail if the “instruction” isn’t one of interest

Not `fail`, return `continue`.

> Action must be unified with a term that specifies how execution must continue. The following actions are defined: …

To explain in more deatil.

You don’t want prolog\_trace\_interception/4 to fail as it is a hook that runs inline with the code. So if it fails the goal that initiated it will fail. You want to set the `Action` value to `continue` if you want the code to continue as normal after doing whatever is desired within prolog\_trace\_interception/4.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [September 3, 2021, 5:29pm UTC](https://swi-prolog.discourse.group/t/using-prolog-trace-interception-4/4376/5 "2021-09-03T17:29:16Z")

</div>

> [@EricGT](#):
>
> You don’t want [prolog\_trace\_interception/4](https://www.swi-prolog.org/pldoc/doc_for?object=prolog_trace_interception/4) to fail as it is a hook that runs inline with the code. So if it fails the goal that initiated it will fail. You want to set the `Action` value to `continue` if you want the code to continue as normal after doing whatever is desired within [prolog\_trace\_interception/4](https://www.swi-prolog.org/pldoc/doc_for?object=prolog_trace_interception/4).

Not really. Failing does the default thing the internal debugger does (display ports, ask for action, etc.). Succeeding with `continue` means you have taken care of the (user) interaction and the system should continue without invoking the default debugger. The tracer never alters the logic of the program unless your return one of the actions that does (i.e., `retry`).

---

<div class="post-metadata">

**Author:** ![ridgeworks](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ridgeworks/32/886_2.png) [@ridgeworks](https://swi-prolog.discourse.group/u/ridgeworks)\
**Post date:** [September 3, 2021, 6:14pm UTC](https://swi-prolog.discourse.group/t/using-prolog-trace-interception-4/4376/6 "2021-09-03T18:14:45Z")

</div>

> [@jan](#):
>
> Not really. Failing does the default thing the internal debugger does (display ports, ask for action, etc.).

Yes, this. `Action=continue` corresponds to the success case when the predicate is one of interest, i.e. print trace message and continue. Otherwise fail to default debugger semantics (or some other clause of `prolog_trace_interception`).
