View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker and Anjo Anjewierden
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org/
    6    Copyright (c)  2011-2018, University of Amsterdam
    7                              VU University Amsterdam
    8                              CWI, Amsterdam
    9    All rights reserved.
   10
   11    Redistribution and use in source and binary forms, with or without
   12    modification, are permitted provided that the following conditions
   13    are met:
   14
   15    1. Redistributions of source code must retain the above copyright
   16       notice, this list of conditions and the following disclaimer.
   17
   18    2. Redistributions in binary form must reproduce the above copyright
   19       notice, this list of conditions and the following disclaimer in
   20       the documentation and/or other materials provided with the
   21       distribution.
   22
   23    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   24    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   25    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   26    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   27    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   28    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   29    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   30    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   31    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   32    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   33    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   34    POSSIBILITY OF SUCH DAMAGE.
   35*/
   36
   37:- module(prolog_breakpoints,
   38          [ set_breakpoint/4,           % +File, +Line, +CharPos, -Id
   39            set_breakpoint/5,           % +Owner, +File, +Line, +CharPos, -Id
   40            delete_breakpoint/1,        % +Id
   41            breakpoint_property/2       % ?Id, ?Property
   42          ]).   43:- use_module(library(prolog_clause)).   44:- use_module(library(debug)).   45:- use_module(library(error)).

Manage Prolog break-points

This module provides an interface for development tools to set and delete break-points, giving a location in the source. Development tools that want to track changes to breakpoints must use user:message_hook/3 to intercept these message terms:

Note that the hook must fail after creating its side-effects to give other hooks the opportunity to react. */

 set_breakpoint(+File, +Line, +Char, -Id) is det
 set_breakpoint(+Owner, +File, +Line, +Char, -Id) is det
Put a breakpoint at the indicated source-location. File is a current sourcefile (as reported by source_file/1). Line is the 1-based line in which Char is. Char is the position of the break.

First, '$clause_from_source'/4 uses the SWI-Prolog clause-source information to find the last clause starting before Line. '$break_pc' generated (on backtracking), a list of possible break-points.

Note that in addition to setting the break-point, the system must be in debug mode for the breakpoint to take effect. With threading enabled, there are various different ways this may be done. See debug/0, tdebug/0 and tdebug/1. Therefore, this predicate does not enable debug mode.

Arguments:
Owner- denotes the file that owns the clause. set_breakpoint/5 is used to set breakpoints in an included file in the context of the Owner main file. See source_file_property/2.
   84set_breakpoint(File, Line, Char, Id) :-
   85    set_breakpoint(File, File, Line, Char, Id).
   86set_breakpoint(Owner, File, Line, Char, Id) :-
   87    debug(break, 'break_at(~q, ~d, ~d).', [File, Line, Char]),
   88    '$clause_from_source'(Owner, File, Line, ClauseRef),
   89    clause_info(ClauseRef, InfoFile, TermPos, _NameOffset),
   90    (   InfoFile == File
   91    ->  '$break_pc'(ClauseRef, PC, NextPC),
   92        debug(break, 'Clause ~p, PC=~p NextPC=~p', [ClauseRef, PC, NextPC]),
   93        '$clause_term_position'(ClauseRef, NextPC, List),
   94        debug(break, 'Location = ~w', [List]),
   95        range(List, TermPos, A, Z),
   96        debug(break, 'Term from ~w-~w', [A, Z]),
   97        Z >= Char, !,
   98        Len is Z - A,
   99        b_setval('$breakpoint', file_location(File, Line, A, Len))
  100    ;   print_message(warning, breakpoint(no_source(ClauseRef, File, Line))),
  101        '$break_pc'(ClauseRef, PC, _), !,
  102        nb_delete('$breakpoint')
  103    ),
  104    debug(break, 'Break at clause ~w, PC=~w', [ClauseRef, PC]),
  105    '$break_at'(ClauseRef, PC, true),
  106    nb_delete('$breakpoint'),
  107    known_breakpoint(ClauseRef, PC, _Location, Id).
  108
  109range(_,  Pos, _, _) :-
  110    var(Pos), !, fail.
  111range([], Pos, A, Z) :-
  112    arg(1, Pos, A),
  113    arg(2, Pos, Z).
  114range([H|T], term_position(_, _, _, _, PosL), A, Z) :-
  115    nth1(H, PosL, Pos),
  116    range(T, Pos, A, Z).
  117
  118:- dynamic
  119    known_breakpoint/4,             % ClauseRef, PC, Location, Id
  120    break_id/1.  121
  122next_break_id(Id) :-
  123    retract(break_id(Id0)),
  124    !,
  125    Id is Id0+1,
  126    asserta(break_id(Id)).
  127next_break_id(1) :-
  128    asserta(break_id(1)).
 delete_breakpoint(+Id) is det
Delete breakpoint with given Id. If successful, print_message(breakpoint(delete, Id)) is called. Message hooks working on this message may still call breakpoint_property/2.
Errors
- existence_error(breakpoint, Id).
  138delete_breakpoint(Id) :-
  139    integer(Id),
  140    known_breakpoint(ClauseRef, PC, _Location, Id),
  141    !,
  142    '$break_at'(ClauseRef, PC, false).
  143delete_breakpoint(Id) :-
  144    existence_error(breakpoint, Id).
 breakpoint_property(?Id, ?Property) is nondet
True when Property is a property of the breakpoint Id. Defined properties are:
file(File)
Provided if the breakpoint is in a clause associated to a file. May not be known.
line_count(Line)
Line of the breakpoint. May not be known.
character_range(Start, Len)
One-based character offset of the break-point. May not be known.
clause(Reference)
Reference of the clause in which the breakpoint resides.
  162breakpoint_property(Id, file(File)) :-
  163    known_breakpoint(ClauseRef,_,_,Id),
  164    clause_property(ClauseRef, file(File)).
  165breakpoint_property(Id, line_count(Line)) :-
  166    known_breakpoint(_,_,Location,Id),
  167    location_line(Location, Line).
  168breakpoint_property(Id, character_range(Start, Len)) :-
  169    known_breakpoint(ClauseRef,PC,Location,Id),
  170    (   Location = file_location(_File, _Line, Start, Len)
  171    ->  true
  172    ;   break_location(ClauseRef, PC, _File, Start-End),
  173        Len is End+1-Start
  174    ).
  175breakpoint_property(Id, clause(Reference)) :-
  176    known_breakpoint(Reference,_,_,Id).
  177
  178location_line(file_location(_File, Line, _Start, _Len), Line).
  179location_line(file_character_range(File, Start, _Len), Line) :-
  180    file_line(File, Start, Line).
  181location_line(file_line(_File, Line), Line).
 file_line(+File, +StartIndex, -Line) is det
True when Line is the 1-based line offset in which we find character StartIndex.
  189file_line(File, Start, Line) :-
  190    setup_call_cleanup(
  191        prolog_clause:try_open_source(File, In),
  192        stream_line(In, Start, 1, Line),
  193        close(In)).
  194
  195stream_line(In, _, Line0, Line) :-
  196    at_end_of_stream(In),
  197    !,
  198    Line = Line0.
  199stream_line(In, Index, Line0, Line) :-
  200    skip(In, 0'\n),
  201    character_count(In, At),
  202    (   At > Index
  203    ->  Line = Line0
  204    ;   Line1 is Line0+1,
  205        stream_line(In, Index, Line1, Line)
  206    ).
  207
  208
  209                 /*******************************
  210                 *            FEEDBACK          *
  211                 *******************************/
  212
  213:- initialization
  214    prolog_unlisten(break, onbreak),
  215    prolog_listen(break, onbreak).  216
  217onbreak(exist, ClauseRef, PC) :-
  218    known_breakpoint(ClauseRef, PC, _Location, Id),
  219    !,
  220    break_message(breakpoint(exist, Id)).
  221onbreak(true, ClauseRef, PC) :-
  222    !,
  223    debug(break, 'Trap in Clause ~p, PC ~d', [ClauseRef, PC]),
  224    with_mutex('$break', next_break_id(Id)),
  225    (   nb_current('$breakpoint', Location)
  226    ->  true
  227    ;   break_location(ClauseRef, PC, File, A-Z)
  228    ->  Len is Z+1-A,
  229        Location = file_character_range(File, A, Len)
  230    ;   clause_property(ClauseRef, file(File)),
  231        clause_property(ClauseRef, line_count(Line))
  232    ->  Location = file_line(File, Line)
  233    ;   Location = unknown
  234    ),
  235    asserta(known_breakpoint(ClauseRef, PC, Location, Id)),
  236    break_message(breakpoint(set, Id)).
  237onbreak(false, ClauseRef, PC) :-
  238    debug(break, 'Remove breakpoint from ~p, PC ~d', [ClauseRef, PC]),
  239    clause(known_breakpoint(ClauseRef, PC, _Location, Id), true, Ref),
  240    call_cleanup(break_message(breakpoint(delete, Id)), erase(Ref)).
  241onbreak(gc, ClauseRef, PC) :-
  242    debug(break, 'Remove breakpoint from ~p, PC ~d (due to CGC)',
  243          [ClauseRef, PC]),
  244    retractall(known_breakpoint(ClauseRef, PC, _Location, _Id)).
  245
  246break_message(Message) :-
  247    print_message(informational, Message).
 break_location(+ClauseRef, +PC, -File, -AZ) is det
True when File and AZ represent the location of the goal called at PC in ClauseRef.
Arguments:
AZ- is a term A-Z, where A and Z are character positions in File.
  257break_location(ClauseRef, PC, File, A-Z) :-
  258    clause_info(ClauseRef, File, TermPos, _NameOffset),
  259    '$fetch_vm'(ClauseRef, PC, NPC, _VMI),
  260    '$clause_term_position'(ClauseRef, NPC, List),
  261    debug(break, 'ClausePos = ~w', [List]),
  262    range(List, TermPos, A, Z),
  263    debug(break, 'Range: ~d .. ~d', [A, Z]).
  264
  265
  266                 /*******************************
  267                 *            MESSAGES          *
  268                 *******************************/
  269
  270:- multifile
  271    prolog:message/3.  272
  273prolog:message(breakpoint(no_source(ClauseRef, _File, Line))) -->
  274    [ 'Failed to find line ~d in body of clause ~p.  Breaking at start of body.'-
  275      [Line, ClauseRef]
  276    ].
  277prolog:message(breakpoint(SetClear, Id)) -->
  278    setclear(SetClear),
  279    breakpoint(Id).
  280
  281setclear(set) -->
  282    ['Breakpoint '].
  283setclear(exist) -->
  284    ['Existing breakpoint '].
  285setclear(delete) -->
  286    ['Deleted breakpoint '].
  287
  288breakpoint(Id) -->
  289    breakpoint_name(Id),
  290    (   { breakpoint_property(Id, file(File)),
  291          file_base_name(File, Base),
  292          breakpoint_property(Id, line_count(Line))
  293        }
  294    ->  [ ' at ~w:~d'-[Base, Line] ]
  295    ;   []
  296    ).
  297
  298breakpoint_name(Id) -->
  299    { breakpoint_property(Id, clause(ClauseRef)) },
  300    (   { clause_property(ClauseRef, erased) }
  301    ->  ['~w'-[Id]]
  302    ;   { clause_name(ClauseRef, Name) },
  303        ['~w in ~w'-[Id, Name]]
  304    )