root/tags/lgt2212/library/event_dbg.lgt

Revision 790, 3.7 KB (checked in by pmoura, 5 years ago)

Renamed entities "debuggerp" and "debugger" to, respectivley, "event_dbgp" and "event_dbg" to avoid conflict with the new built-in pseudo-object debugger.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1
2:- object(event_dbg,
3    implements(event_dbgp, event_handlersp),
4    imports(monitor)).
5
6
7    :- info([
8        version is 1.0,
9        date is 2000/7/24,
10        author is 'Paulo Moura',
11        comment is 'Debugging facilities similar to those found in most Prolog compilers.']).
12
13
14    :- initialization(::init).
15
16
17    :- protected(port_output/4).
18
19    :- mode(port_output(+atom, +object, @callable, +object), one).
20
21    :- info(port_output/4, [
22        comment is 'Outputs current port information.',
23        argnames is ['Port', 'Object', 'Message', 'Sender']]).
24
25
26    :- protected(execute_option/1).
27
28    :- mode(execute_option(+atom), one).
29
30    :- info(execute_option/1, [
31        comment is 'Executes a user option at a debugger port.',
32        argnames is ['Option']]).
33
34
35    :- protected(query_user/1).
36
37    :- mode(query_user(-atom), one).
38
39    :- info(query_user/1, [
40        comment is 'Query a user about an option at a debugger port.',
41        argnames is ['Option']]).
42
43
44    :- private(stream_/2).
45    :- dynamic(stream_/2).
46
47    :- mode(stream_(?atom, ?stream), zero_or_more).
48
49    :- info(stream/2, [
50        comment is 'Stores the current debugger input and ouput streams.',
51        argnames is ['Kind', 'Stream']]).
52
53
54    stream(Name, Stream) :-
55        ::stream_(Name, Stream).
56
57
58    set_stream(Name, Stream) :-
59        ::retractall(stream_(Name, _)),
60        ::assertz(stream_(Name, Stream)).
61
62
63    trace :-
64        self(Self),
65        abolish_events(before, _, _, _, Self),
66        abolish_events(after, _, _, _, Self),
67        define_events(before, _, _, _, Self),
68        define_events(after, _, _, _, Self).
69
70
71    notrace :-
72        self(Self),
73        abolish_events(before, _, _, _, Self),
74        abolish_events(after, _, _, _, Self).
75
76
77    debugging :-
78        ::monitor_activated.
79
80
81    debug :-
82        ::activate_monitor.
83
84
85    nodebug :-
86        ::suspend_monitor.
87
88
89    port_output(Port, Object, Message, Sender) :-
90        ::stream(output, Output),
91        write(Output, Port),
92        write(Output, ':     '),
93        writeq(Output, Object),
94        write(Output, ' <- '),
95        writeq(Output, Message),
96        write(Output, ' from '),
97        writeq(Output, Sender),
98        nl(Output).
99
100
101    query_user(Option) :-
102        ::stream(output, Output),
103        ::stream(input, Input),
104        repeat,
105            write(Output, '    >> '),
106            read(Input, Option),
107            nl(Output),
108            (valid_option(Option) ->
109                true
110                ;
111                ::execute_option(h), fail),
112        !.
113
114
115    execute_option(c).
116
117    execute_option(f) :-
118        !, fail.
119
120    execute_option(n) :-
121        ::nodebug.
122
123    execute_option(b) :-
124        ::stream(output,Output),
125        ::stream(input, Input),
126        repeat,
127            write(Output, '     :- '),
128            read(Input, Goal),
129            writeq(Output, Goal),
130            nl(Output),
131            (once(Goal) ->
132                write(Output, '     answer: '),
133                writeq(Output, Goal), nl(Output)
134                ;
135                write(Output, '     no'), nl(Output)),
136        Goal = true,
137        !.
138
139    execute_option(a) :-
140        throw(error(logtalk_execution_aborted)).
141
142    execute_option(h) :-
143        ::stream(output, Output),
144        write(Output, '     Available options are:'), nl(Output),
145        write(Output, '       c - creep (go on)'), nl(Output),
146        write(Output, '       f - fail (force failure or backtracking)'), nl(Output),
147        write(Output, '       n - nodebug (turn off debug)'), nl(Output),
148        write(Output, '       b - break (submit queries to the interpreter, type true to terminate)'), nl(Output),
149        write(Output, '       a - abort (return to top level interpreter)'), nl(Output),
150        write(Output, '       h - help (prints this list of options)'), nl(Output),
151        nl(Output).
152
153
154    valid_option(c).
155    valid_option(f).
156    valid_option(n).
157    valid_option(b).
158    valid_option(a).
159
160
161    before(Object, Message, Sender) :-
162        ::port_output(call, Object, Message, Sender),
163        ::query_user(Option),
164        ::execute_option(Option).
165
166
167    after(Object, Message, Sender) :-
168        ::port_output(exit, Object, Message, Sender),
169        ::query_user(Option),
170        ::execute_option(Option).
171
172
173    init :-
174        ::reset_monitor,
175        current_input(Input),
176        ::set_stream(input, Input),
177        current_output(Output),
178        ::set_stream(output, Output).
179
180
181:- end_object.
Note: See TracBrowser for help on using the browser.