author | Claus Gittinger <cg@exept.de> |
Sun, 17 Dec 1995 16:20:58 +0100 | |
changeset 161 | de0f00bd60ca |
parent 156 | bb53b47392fa |
child 162 | ed6f37d2cc33 |
permissions | -rw-r--r-- |
8 | 1 |
" |
2 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
11 | 3 |
All Rights Reserved |
8 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
120 | 13 |
Object subclass:#MessageTracer |
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
14 |
instanceVariableNames:'traceDetail' |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
15 |
classVariableNames:'BreakpointSignal CallingLevel BreakBlock TraceSenderBlock |
156 | 16 |
LeaveBlock MethodCounts TraceFullBlock' |
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
17 |
poolDictionaries:'' |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
18 |
category:'System-Debugging-Support' |
8 | 19 |
! |
20 |
||
21 |
!MessageTracer class methodsFor:'documentation'! |
|
22 |
||
9 | 23 |
copyright |
24 |
" |
|
25 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
11 | 26 |
All Rights Reserved |
9 | 27 |
|
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
8 | 37 |
documentation |
38 |
" |
|
11 | 39 |
This class provides a common home for the tracing |
40 |
facilities (originally, they where in Object, but have been moved to |
|
8 | 41 |
allow easier separation of development vs. runtime configurations. |
42 |
||
11 | 43 |
tracing execution of a block: |
44 |
||
45 |
MessageTracer trace:[ ... ] |
|
46 |
||
47 |
MessageTracer traceFull:[ ... ] |
|
48 |
||
49 |
(for system developper only:) |
|
50 |
||
51 |
MessageTracer debugTrace:[ ... ] |
|
52 |
||
53 |
||
8 | 54 |
trapping sends to a specific object: |
55 |
||
11 | 56 |
MessageTracer trap:anObject selector:aSelector |
57 |
... |
|
58 |
MessageTracer untrap:anObject selector:aSelector |
|
59 |
or: |
|
60 |
MessageTracer untrap:anObject |
|
61 |
||
8 | 62 |
|
63 |
||
27 | 64 |
trapping some messages sent to a specific object: |
65 |
||
66 |
MessageTracer trap:anObject selectors:aCollectionOfSelectors |
|
67 |
... |
|
68 |
MessageTracer untrap:anObject |
|
69 |
||
70 |
||
71 |
||
72 |
trapping any message sent to a specific object: |
|
73 |
||
74 |
MessageTracer trapAll:anObject |
|
75 |
... |
|
76 |
MessageTracer untrap:anObject |
|
77 |
||
78 |
||
79 |
||
8 | 80 |
trapping evaluation of a specific method: |
81 |
||
11 | 82 |
MessageTracer trapMethod:aMethod |
83 |
... |
|
84 |
MessageTracer unwrapMethod:aMethod |
|
8 | 85 |
|
86 |
||
87 |
||
12 | 88 |
trapping evaluation of a specific method with |
89 |
receiver being an instance of some class: |
|
90 |
||
91 |
MessageTracer trapMethod:aMethod forInstancesOf:aClass |
|
92 |
... |
|
93 |
MessageTracer unwrapMethod:aMethod |
|
94 |
||
95 |
||
96 |
||
8 | 97 |
tracing sends to a specific object: |
98 |
||
11 | 99 |
MessageTracer trace:anObject selector:aSelector |
100 |
... |
|
101 |
MessageTracer untrace:anObject selector:aSelector |
|
102 |
or: |
|
103 |
MessageTracer untrace:anObject |
|
104 |
||
8 | 105 |
|
106 |
||
107 |
tracing sender only: |
|
108 |
||
11 | 109 |
MessageTracer traceSender:anObject selector:aSelector |
110 |
... |
|
111 |
MessageTracer untrace:anObject selector:aSelector |
|
112 |
or: |
|
113 |
MessageTracer untrace:anObject |
|
114 |
||
8 | 115 |
|
116 |
||
117 |
tracing evaluation of a specific method: |
|
118 |
||
11 | 119 |
MessageTracer traceMethod:aMethod |
120 |
... |
|
121 |
MessageTracer unwrapmethod:aMethod |
|
18 | 122 |
|
123 |
see more in examples and in method comments. |
|
8 | 124 |
" |
12 | 125 |
! |
126 |
||
127 |
examples |
|
128 |
" |
|
129 |
For the common cases, you will find a menu entry in the SystemBrowser. |
|
130 |
Howeever, more special cases (especially with condition checks) can be |
|
131 |
set up by evaluating the lower level entries. |
|
132 |
||
133 |
||
134 |
trapping specific methods: |
|
135 |
(by class/selector): |
|
136 |
||
137 |
MessageTracer trapClass:Collection selector:#select:. |
|
138 |
Dictionary new select:[:e | ]. 'not cought - Dictionary has its own select'. |
|
139 |
(Array new:10) select:[:e | ]. 'not cought - SeqColl has its own select'. |
|
140 |
Set new select:[:e | ]. 'cought - Set inherits this from Collection'. |
|
141 |
MessageTracer untrapClass:Collection |
|
142 |
||
143 |
(by method): |
|
144 |
MessageTracer trapMethod:(Collection compiledMethodAt:#select:). |
|
145 |
Dictionary new select:[:e | ]. 'not cought - Dictionary has its own select'. |
|
146 |
(Array new:10) select:[:e | ]. 'not cought - SeqColl has its own select'. |
|
147 |
Set new select:[:e | ]. 'cought - Set inherits this from Collection'. |
|
148 |
MessageTracer unwrapMethod:(Collection compiledMethodAt:#select:). |
|
149 |
||
27 | 150 |
(by method & instance class): |
151 |
MessageTracer trapMethod:(SequenceableCollection compiledMethodAt:#select:) |
|
152 |
forInstancesOf:SortedCollection. |
|
153 |
Dictionary new select:[:e | ]. 'not cought - Dictionary has its own select'. |
|
154 |
(Array new:10) select:[:e | ]. 'not cought - not a SortedCollection'. |
|
155 |
OrderedCollection new select:[:e | ]. 'not cought - not a SortedCollection'. |
|
156 |
SortedCollection new select:[:e | ]. 'cought - Set inherits this from Collection'. |
|
157 |
MessageTracer unwrapMethod:(SequenceableCollection compiledMethodAt:#select:). |
|
158 |
||
12 | 159 |
tracing specific methods: |
160 |
(by class/selector): |
|
161 |
||
162 |
MessageTracer traceClass:SequenceableCollection selector:#quickSortFrom:to:. |
|
163 |
#(6 1 9 66 2 17) copy sort. |
|
164 |
MessageTracer untraceClass:SequenceableCollection |
|
165 |
||
166 |
(by method): |
|
167 |
MessageTracer traceMethod:(SequenceableCollection compiledMethodAt:#quickSortFrom:to:). |
|
168 |
#(6 1 9 66 2 17) copy sort. |
|
169 |
MessageTracer unwrapMethod:(SequenceableCollection compiledMethodAt:#quickSortFrom:to:). |
|
27 | 170 |
|
171 |
object trapping: |
|
172 |
|o| |
|
173 |
||
174 |
o := OrderedCollection new. |
|
175 |
MessageTracer trapAll:o. |
|
176 |
o collect:[:el | el]. |
|
12 | 177 |
" |
8 | 178 |
! ! |
179 |
||
11 | 180 |
!MessageTracer class methodsFor:'initialization'! |
181 |
||
182 |
initialize |
|
183 |
BreakpointSignal isNil ifTrue:[ |
|
156 | 184 |
BreakpointSignal := HaltSignal newSignalMayProceed:true. |
185 |
BreakpointSignal nameClass:self message:#breakpointSignal. |
|
186 |
BreakpointSignal notifierString:'breakpoint encountered'. |
|
27 | 187 |
|
156 | 188 |
BreakBlock := [:con | BreakpointSignal raiseIn:con]. |
189 |
TraceSenderBlock := [:con | MessageTracer printEntrySender:con]. |
|
190 |
TraceFullBlock := [:con | con fullPrintAll]. |
|
191 |
LeaveBlock := [:con :retVal | ]. |
|
11 | 192 |
] |
156 | 193 |
|
194 |
" |
|
195 |
BreakpointSignal := nil. |
|
196 |
MessageTracer initialize |
|
197 |
" |
|
198 |
||
199 |
"Modified: 15.12.1995 / 18:19:13 / cg" |
|
11 | 200 |
! ! |
201 |
||
88 | 202 |
!MessageTracer class methodsFor:'Signal constants'! |
203 |
||
204 |
breakpointSignal |
|
205 |
^ BreakpointSignal |
|
206 |
! ! |
|
207 |
||
208 |
!MessageTracer class methodsFor:'class tracing'! |
|
209 |
||
210 |
traceClass:aClass selector:aSelector |
|
211 |
"arrange for a trace message to be output on Stderr, when a message with aSelector is |
|
212 |
sent to instances of aClass (or subclass instances). Use untraceClass to remove this." |
|
213 |
||
214 |
self traceMethod:(aClass compiledMethodAt:aSelector) |
|
215 |
||
216 |
" |
|
217 |
MessageTracer traceClass:Integer selector:#factorial. |
|
218 |
5 factorial. |
|
219 |
MessageTracer untraceClass:Integer |
|
220 |
" |
|
221 |
" |
|
222 |
MessageTracer traceClass:SequenceableCollection selector:#quickSortFrom:to:. |
|
223 |
#(6 1 9 66 2 17) copy sort. |
|
224 |
MessageTracer untraceClass:SequenceableCollection |
|
225 |
" |
|
226 |
" |
|
227 |
MessageTracer traceClass:Array selector:#at:. |
|
228 |
MessageTracer traceClass:Array selector:#at:put:. |
|
229 |
#(6 1 9 66 2 17) copy sort. |
|
230 |
MessageTracer untraceClass:Array |
|
231 |
" |
|
232 |
! |
|
233 |
||
234 |
untraceAllClasses |
|
235 |
"remove all traces of messages sent to any class" |
|
236 |
||
237 |
"just a rename for your convenience - the same basic mechanism is used for all of these |
|
238 |
trace facilities ..." |
|
239 |
||
240 |
^ self untrapAllClasses |
|
241 |
! |
|
242 |
||
243 |
untraceClass:aClass |
|
244 |
"remove all traces of messages sent to instances of aClass" |
|
245 |
||
246 |
"just a rename for your convenience - the same basic mechanism is used for all of these |
|
247 |
trace facilities ..." |
|
248 |
||
249 |
^ self untrapClass:aClass |
|
250 |
! ! |
|
251 |
||
252 |
!MessageTracer class methodsFor:'class wrapping'! |
|
253 |
||
254 |
wrapClass:aClass selector:aSelector onEntry:entryBlock onExit:exitBlock |
|
255 |
"arrange for the two blocks entryBlock and exitBlock to be evaluated whenever |
|
256 |
aSelector is sent to instances of aClass or subclasses. |
|
257 |
EntryBlock will be called on entry, and get the current context passed as argument. |
|
258 |
ExitBlock will be called, when the method is left, and get context and the methods return value as arguments. |
|
259 |
" |
|
260 |
||
261 |
|myMetaclass trapMethod s spec lits idx newClass save| |
|
262 |
||
263 |
" |
|
264 |
create a new method, which calls the original one, |
|
265 |
but only if not already being trapped. |
|
266 |
" |
|
267 |
spec := Parser methodSpecificationForSelector:aSelector. |
|
268 |
||
269 |
s := WriteStream on:String new. |
|
270 |
s nextPutAll:spec. |
|
271 |
s cr. |
|
272 |
s nextPutAll:'|retVal stubClass|'; cr. |
|
273 |
entryBlock notNil ifTrue:[ |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
274 |
s nextPutAll:'#literal1 yourself value:thisContext.'; cr. |
88 | 275 |
]. |
276 |
s nextPutAll:('retVal := super ' , spec , '.'); cr. |
|
277 |
exitBlock notNil ifTrue:[ |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
278 |
s nextPutAll:'#literal2 yourself value:thisContext value:retVal.'; cr. |
88 | 279 |
]. |
280 |
s nextPutAll:'^ retVal'; cr. |
|
281 |
||
282 |
save := Compiler stcCompilation. |
|
283 |
Compiler stcCompilation:#never. |
|
284 |
[ |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
285 |
Class withoutUpdatingChangesDo:[ |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
286 |
trapMethod := Compiler compile:s contents |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
287 |
forClass:aClass |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
288 |
inCategory:'trapping' |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
289 |
notifying:nil |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
290 |
install:false |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
291 |
skipIfSame:false |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
292 |
silent:true. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
293 |
] |
88 | 294 |
] valueNowOrOnUnwindDo:[ |
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
295 |
Compiler stcCompilation:save |
88 | 296 |
]. |
297 |
||
298 |
lits := trapMethod literals. |
|
299 |
entryBlock notNil ifTrue:[ |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
300 |
lits at:(lits indexOf:#literal1) put:entryBlock. |
88 | 301 |
]. |
302 |
exitBlock notNil ifTrue:[ |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
303 |
lits at:(lits indexOf:#literal2) put:exitBlock. |
88 | 304 |
]. |
305 |
" |
|
306 |
change the source of this new method |
|
307 |
(to avoid confusion in the debugger ...) |
|
308 |
" |
|
309 |
trapMethod source:'this is a wrapper method - not the real one'. |
|
310 |
||
311 |
" |
|
312 |
if not already trapping, create a new class |
|
313 |
" |
|
314 |
aClass category == #trapping ifTrue:[ |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
315 |
idx := aClass selectorArray indexOf:aSelector. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
316 |
idx ~~ 0 ifTrue:[ |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
317 |
aClass methodArray at:idx put:trapMethod |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
318 |
] ifFalse:[ |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
319 |
aClass |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
320 |
setSelectors:(aClass selectorArray copyWith:aSelector) |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
321 |
methods:(aClass methodArray copyWith:trapMethod) |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
322 |
]. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
323 |
lits at:(lits indexOf:#literal3) put:aClass superclass. |
88 | 324 |
] ifFalse:[ |
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
325 |
myMetaclass := aClass class. |
88 | 326 |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
327 |
newClass := myMetaclass copy new. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
328 |
newClass setSuperclass:aClass superclass. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
329 |
newClass instSize:aClass instSize. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
330 |
newClass flags:aClass flags. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
331 |
newClass setClassVariableString:aClass classVariableString. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
332 |
newClass setInstanceVariableString:aClass instanceVariableString. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
333 |
newClass setName:aClass name. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
334 |
newClass category:aClass category. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
335 |
newClass |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
336 |
setSelectors:aClass selectorArray |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
337 |
methods:aClass methodArray. |
88 | 338 |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
339 |
aClass setSuperclass:newClass. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
340 |
aClass setClassVariableString:''. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
341 |
aClass setInstanceVariableString:''. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
342 |
aClass category:#trapping. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
343 |
aClass |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
344 |
setSelectors:(Array with:aSelector) |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
345 |
methods:(Array with:trapMethod). |
88 | 346 |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
347 |
lits at:(lits indexOf:#literal3) put:newClass. |
88 | 348 |
]. |
349 |
||
350 |
ObjectMemory flushCaches. |
|
351 |
||
352 |
" |
|
353 |
MessageTracer |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
354 |
wrapMethod:(Point compiledMethodAt:#scaleBy:) |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
355 |
onEntry:nil |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
356 |
onExit:[:con :retVal | |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
357 |
Transcript show:'leave Point>>scaleBy:; returning:'. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
358 |
Transcript showCr:retVal printString. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
359 |
Transcript endEntry |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
360 |
]. |
88 | 361 |
(1@2) scaleBy:5. |
362 |
MessageTracer untrapClass:Point. |
|
363 |
(1@2) scaleBy:5. |
|
364 |
" |
|
365 |
" |
|
366 |
MessageTracer |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
367 |
wrapMethod:(Integer compiledMethodAt:#factorial) |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
368 |
onEntry:[:con | |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
369 |
Transcript showCr:('entering ' , con receiver printString , '>>factorial'). |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
370 |
] |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
371 |
onExit:[:con :retVal | |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
372 |
Transcript show:'leave Integer>>factorial; returning:'. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
373 |
Transcript showCr:retVal printString. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
374 |
Transcript endEntry |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
375 |
]. |
88 | 376 |
Transcript showCr:'5 factorial traced'. |
377 |
5 factorial. |
|
378 |
MessageTracer untrapClass:Integer. |
|
379 |
Transcript showCr:'5 factorial normal'. |
|
380 |
5 factorial. |
|
381 |
" |
|
382 |
" |
|
383 |
|lvl| |
|
384 |
||
385 |
lvl := 0. |
|
386 |
MessageTracer |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
387 |
wrapMethod:(Integer compiledMethodAt:#factorial) |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
388 |
onEntry:[:con | |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
389 |
Transcript spaces:lvl. lvl := lvl + 2. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
390 |
Transcript showCr:('entering ' , con receiver printString , '>>factorial'). |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
391 |
] |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
392 |
onExit:[:con :retVal | |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
393 |
lvl := lvl - 2. Transcript spaces:lvl. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
394 |
Transcript show:('leave ' , con receiver printString , '>>factorial; returning:'). |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
395 |
Transcript showCr:retVal printString. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
396 |
Transcript endEntry |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
397 |
]. |
88 | 398 |
Transcript showCr:'5 factorial traced'. |
399 |
5 factorial. |
|
400 |
MessageTracer untrapClass:Integer. |
|
401 |
Transcript showCr:'5 factorial normal'. |
|
402 |
5 factorial. |
|
403 |
" |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
404 |
|
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
405 |
"Modified: 13.12.1995 / 16:05:26 / cg" |
88 | 406 |
! ! |
407 |
||
27 | 408 |
!MessageTracer class methodsFor:'cleanup'! |
409 |
||
410 |
cleanup |
|
411 |
"if you forgot which classes/methods where wrapped and/or trapped, |
|
412 |
this cleans up everything ..." |
|
413 |
||
414 |
self untrapAllClasses. |
|
415 |
self unwrapAllMethods |
|
416 |
||
417 |
" |
|
418 |
MessageTracer cleanup |
|
419 |
" |
|
420 |
! ! |
|
421 |
||
88 | 422 |
!MessageTracer class methodsFor:'execution trace '! |
423 |
||
424 |
debugTrace:aBlock |
|
425 |
"trace execution of aBlock. This is for system debugging only" |
|
11 | 426 |
|
88 | 427 |
Smalltalk debugOn. |
428 |
^ aBlock valueNowOrOnUnwindDo:[ |
|
429 |
Smalltalk debugOff. |
|
430 |
] |
|
11 | 431 |
|
88 | 432 |
" |
433 |
MessageTracer debugTrace:[#(6 5 4 3 2 1) sort] |
|
434 |
" |
|
435 |
! |
|
11 | 436 |
|
437 |
trace:aBlock |
|
438 |
"evaluate aBlock sending trace information to stdout. |
|
27 | 439 |
Return the value of the block." |
11 | 440 |
|
17 | 441 |
^ self new trace:aBlock detail:false. |
11 | 442 |
|
443 |
" |
|
444 |
MessageTracer trace:[#(6 5 4 3 2 1) sort] |
|
445 |
" |
|
446 |
! |
|
447 |
||
448 |
traceFull:aBlock |
|
449 |
"evaluate aBlock sending trace information to stdout. |
|
450 |
Return the value of the block. |
|
27 | 451 |
The trace information is more detailed." |
11 | 452 |
|
17 | 453 |
^ self new trace:aBlock detail:true. |
11 | 454 |
|
455 |
" |
|
456 |
MessageTracer traceFull:[#(6 5 4 3 2 1) sort] |
|
457 |
" |
|
88 | 458 |
! ! |
459 |
||
460 |
!MessageTracer class methodsFor:'method breakpointing'! |
|
461 |
||
462 |
trapClass:aClass selector:aSelector |
|
463 |
"arrange for the debugger to be entered when a message with aSelector is |
|
464 |
sent to instances of aClass (or subclass instances). Use untrapClass to remove this trap. |
|
465 |
Be careful, to not place a trap on code needed in the debugger (i.e. on scrollBars etc.); |
|
466 |
if there is a need to trap those, use the low-level wrap-methods, and put a check into the |
|
467 |
entry/leave blocks." |
|
468 |
||
469 |
self trapMethod:(aClass compiledMethodAt:aSelector) |
|
470 |
||
471 |
" |
|
472 |
MessageTracer trapClass:Collection selector:#select:. |
|
473 |
Dictionary new select:[:e | ]. 'not cought - Dictionary has its own select'. |
|
474 |
(Array new:10) select:[:e | ]. 'not cought - SeqColl has its own select'. |
|
475 |
Set new select:[:e | ]. 'cought - Set inherits this from Collection'. |
|
476 |
MessageTracer untrapClass:Collection |
|
477 |
" |
|
11 | 478 |
! |
479 |
||
88 | 480 |
trapMethod:aMethod |
481 |
"arrange for the debugger to be entered when aMethod is about to be executed. |
|
482 |
Use unwrapMethod or untrapClass to remove this trap. |
|
483 |
Be careful, to not place a trap on code needed in the debugger (i.e. on scrollBars etc.); |
|
484 |
if there is a need to trap those, use the low-level wrap-methods, and put a check into the |
|
485 |
entry/leave blocks." |
|
486 |
||
487 |
^ self wrapMethod:aMethod |
|
488 |
onEntry:BreakBlock |
|
489 |
onExit:LeaveBlock. |
|
490 |
||
491 |
" |
|
492 |
MessageTracer trapMethod:(Collection compiledMethodAt:#select:). |
|
493 |
Dictionary new select:[:e | ]. 'not cought - Dictionary has its own select'. |
|
494 |
(Array new:10) select:[:e | ]. 'not cought - SeqColl has its own select'. |
|
495 |
Set new select:[:e | ]. 'cought - Set inherits this from Collection'. |
|
496 |
MessageTracer unwrapMethod:(Collection compiledMethodAt:#select:). |
|
497 |
" |
|
498 |
! |
|
11 | 499 |
|
88 | 500 |
trapMethod:aMethod forInstancesOf:aClass |
501 |
"arrange for the debugger to be entered when aMethod is about to be executed |
|
502 |
for an instance of aClass. |
|
503 |
Use unwrapMethod or untrapClass to remove this trap. |
|
504 |
Be careful, to not place a trap on code needed in the debugger (i.e. on scrollBars etc.); |
|
505 |
if there is a need to trap those, use the low-level wrap-methods, and put a check into the |
|
506 |
entry/leave blocks." |
|
507 |
||
508 |
^ self wrapMethod:aMethod |
|
509 |
onEntry:[:context | |
|
510 |
(context receiver isMemberOf:aClass) ifTrue:[ |
|
511 |
BreakpointSignal raiseIn:context |
|
512 |
] |
|
513 |
] |
|
514 |
onExit:LeaveBlock. |
|
515 |
||
516 |
" |
|
517 |
MessageTracer trapMethod:(View compiledMethodAt:#redraw) forInstancesOf:myView. |
|
518 |
" |
|
519 |
! |
|
520 |
||
521 |
untrapAllClasses |
|
522 |
"remove any traps on any class" |
|
523 |
||
524 |
Smalltalk allBehaviorsDo:[:aClass | |
|
525 |
self untrapClass:aClass |
|
11 | 526 |
] |
527 |
||
528 |
" |
|
88 | 529 |
MessageTracer untrapAllClasses |
530 |
" |
|
531 |
! |
|
532 |
||
533 |
untrapClass:aClass |
|
534 |
"remove any traps on aClass" |
|
535 |
||
536 |
"this is done by just patching the class back to the original" |
|
537 |
||
538 |
|orgClass| |
|
539 |
||
540 |
aClass category == #trapping ifFalse:[ |
|
541 |
^ self |
|
542 |
]. |
|
543 |
orgClass := aClass superclass. |
|
544 |
||
545 |
aClass setSuperclass:orgClass superclass. |
|
546 |
aClass setClassVariableString:orgClass classVariableString. |
|
547 |
aClass setInstanceVariableString:orgClass instanceVariableString. |
|
548 |
aClass category:orgClass category. |
|
549 |
aClass |
|
550 |
setSelectors:orgClass selectorArray |
|
551 |
methods:orgClass methodArray. |
|
552 |
||
553 |
ObjectMemory flushCaches. |
|
554 |
||
555 |
" |
|
556 |
MessageTracer untrapClass:Point |
|
11 | 557 |
" |
88 | 558 |
! |
559 |
||
560 |
untrapClass:aClass selector:aSelector |
|
561 |
"remove trap of aSelector sent to aClass" |
|
562 |
||
563 |
|idx sels newSels newMethods| |
|
564 |
||
565 |
aClass category == #trapping ifFalse:[ |
|
566 |
^ self |
|
567 |
]. |
|
568 |
||
569 |
sels := aClass selectorArray. |
|
570 |
idx := sels indexOf:aSelector. |
|
571 |
idx == 0 ifTrue:[ |
|
572 |
^ self |
|
573 |
]. |
|
574 |
sels size == 1 ifTrue:[ |
|
575 |
"the last trapped method" |
|
576 |
^ self untrapClass:aClass |
|
577 |
]. |
|
578 |
newSels := sels copyWithoutIndex:idx. |
|
579 |
newMethods := aClass methodArray copyWithoutIndex:idx. |
|
580 |
aClass selectors:newSels methods:newMethods. |
|
581 |
||
582 |
" |
|
583 |
MessageTracer trapClass:Point selector:#copy. |
|
584 |
(1@2) copy. |
|
585 |
(1@2) deepCopy. |
|
586 |
MessageTracer trapClass:Point selector:#deepCopy. |
|
587 |
(1@2) copy. |
|
588 |
(1@2) deepCopy. |
|
589 |
MessageTracer untrapClass:Point selector:#copy. |
|
590 |
(1@2) copy. |
|
591 |
(1@2) deepCopy. |
|
592 |
MessageTracer untrapClass:Point selector:#deepCopy. |
|
593 |
(1@2) copy. |
|
594 |
(1@2) deepCopy. |
|
595 |
" |
|
596 |
! |
|
597 |
||
598 |
untrapMethod:aMethod |
|
599 |
"remove break on aMethod" |
|
600 |
||
601 |
"just a rename for your convenience - the same basic mechanism is used for all of these |
|
602 |
trace facilities ..." |
|
603 |
||
604 |
^ self unwrapMethod:aMethod |
|
11 | 605 |
! ! |
606 |
||
155 | 607 |
!MessageTracer class methodsFor:'method counting'! |
608 |
||
609 |
countMethod:aMethod |
|
610 |
"arrange for a aMethods execution to be counted. |
|
611 |
Use unwrapMethod to remove this." |
|
612 |
||
613 |
|lvl inside| |
|
614 |
||
615 |
MethodCounts isNil ifTrue:[ |
|
616 |
MethodCounts := IdentityDictionary new. |
|
617 |
]. |
|
618 |
MethodCounts at:aMethod put:0. |
|
619 |
||
620 |
^ self wrapMethod:aMethod |
|
621 |
onEntry:[:con | |
|
622 |
|cnt| |
|
623 |
||
624 |
cnt := MethodCounts at:aMethod ifAbsent:0. |
|
625 |
MethodCounts at:aMethod put:(cnt + 1). |
|
626 |
] |
|
627 |
onExit:[:con :retVal | |
|
628 |
] |
|
629 |
||
630 |
" |
|
631 |
MessageTracer countMethod:(Integer compiledMethodAt:#factorial). |
|
632 |
5 factorial. |
|
633 |
MessageTracer executionCountOf:(Integer compiledMethodAt:#factorial) printNL. |
|
634 |
MessageTracer stopCountingMethod:(Integer compiledMethodAt:#factorial) |
|
635 |
" |
|
636 |
||
637 |
"Created: 15.12.1995 / 10:57:49 / cg" |
|
638 |
"Modified: 15.12.1995 / 15:46:41 / cg" |
|
639 |
! |
|
640 |
||
641 |
executionCountOfMethod:aMethod |
|
642 |
"return the current count" |
|
643 |
||
644 |
|count| |
|
645 |
||
646 |
MethodCounts isNil ifTrue:[^ 0]. |
|
647 |
aMethod isWrapped ifTrue:[ |
|
648 |
count := MethodCounts at:aMethod originalMethod ifAbsent:nil. |
|
649 |
count notNil ifTrue:[^ count]. |
|
650 |
]. |
|
651 |
^ MethodCounts at:aMethod ifAbsent:0 |
|
652 |
||
653 |
"Created: 15.12.1995 / 11:01:56 / cg" |
|
654 |
"Modified: 15.12.1995 / 15:45:15 / cg" |
|
655 |
! |
|
656 |
||
657 |
isCounting:aMethod |
|
658 |
"return true if aMethod is counted" |
|
659 |
||
660 |
MethodCounts isNil ifTrue:[^ false]. |
|
661 |
(MethodCounts includesKey:aMethod) ifTrue:[^ true]. |
|
662 |
aMethod isWrapped ifTrue:[ |
|
663 |
^ MethodCounts includesKey:aMethod originalMethod |
|
664 |
]. |
|
665 |
^ false |
|
666 |
||
667 |
"Created: 15.12.1995 / 11:07:58 / cg" |
|
668 |
"Modified: 15.12.1995 / 15:42:10 / cg" |
|
669 |
! |
|
670 |
||
671 |
stopCountingMethod:aMethod |
|
672 |
"remove counting of aMethod" |
|
673 |
||
674 |
^ self unwrapMethod:aMethod |
|
675 |
||
676 |
"Modified: 15.12.1995 / 15:43:53 / cg" |
|
677 |
! ! |
|
678 |
||
88 | 679 |
!MessageTracer class methodsFor:'method tracing'! |
11 | 680 |
|
88 | 681 |
traceMethod:aMethod |
682 |
"arrange for a trace message to be output on Stderr, when aMethod is executed. |
|
683 |
Use unwrapMethod to remove this." |
|
684 |
||
685 |
|lvl inside| |
|
11 | 686 |
|
88 | 687 |
^ self wrapMethod:aMethod |
688 |
onEntry:[:con | |
|
689 |
inside isNil ifTrue:[ |
|
690 |
inside := true. |
|
691 |
CallingLevel isNil ifTrue:[ |
|
692 |
CallingLevel := 0. |
|
693 |
]. |
|
694 |
lvl notNil ifTrue:[ |
|
695 |
lvl := lvl + 1 |
|
696 |
] ifFalse:[ |
|
697 |
CallingLevel := lvl := CallingLevel + 1. |
|
698 |
]. |
|
699 |
MessageTracer printEntryFull:con level:lvl. |
|
700 |
inside := nil |
|
701 |
] |
|
702 |
] |
|
703 |
onExit:[:con :retVal | |
|
704 |
inside isNil ifTrue:[ |
|
705 |
inside := true. |
|
706 |
MessageTracer printExit:con with:retVal level:lvl. |
|
707 |
CallingLevel := lvl := lvl - 1. |
|
708 |
inside := nil |
|
709 |
] |
|
710 |
] |
|
11 | 711 |
|
712 |
" |
|
88 | 713 |
MessageTracer traceMethod:(Integer compiledMethodAt:#factorial). |
714 |
5 factorial. |
|
715 |
MessageTracer untraceMethod:(Integer compiledMethodAt:#factorial) |
|
716 |
" |
|
717 |
" |
|
718 |
MessageTracer traceMethod:(SequenceableCollection compiledMethodAt:#quickSortFrom:to:). |
|
719 |
#(6 1 9 66 2 17) copy sort. |
|
720 |
MessageTracer untraceMethod:(SequenceableCollection compiledMethodAt:#quickSortFrom:to:). |
|
721 |
" |
|
722 |
" |
|
723 |
dont do this: |
|
724 |
" |
|
725 |
" |
|
726 |
MessageTracer traceMethod:(Object compiledMethodAt:#at:). |
|
727 |
MessageTracer traceMethod:(Object compiledMethodAt:#at:put:). |
|
728 |
#(6 1 9 66 2 17) copy sort. |
|
729 |
MessageTracer untraceMethod:(Object compiledMethodAt:#at:). |
|
730 |
MessageTracer untraceMethod:(Object compiledMethodAt:#at:put:). |
|
11 | 731 |
" |
732 |
! |
|
733 |
||
156 | 734 |
traceMethodFull:aMethod |
735 |
"arrange for a full trace message to be output on Stderr, when amethod is executed. |
|
736 |
Only the sender is traced on entry. |
|
737 |
Use untraceMethod to remove this trace." |
|
738 |
||
739 |
^ self wrapMethod:aMethod |
|
740 |
onEntry:TraceFullBlock |
|
741 |
onExit:LeaveBlock. |
|
742 |
||
743 |
"Created: 15.12.1995 / 18:19:31 / cg" |
|
744 |
! |
|
745 |
||
88 | 746 |
traceMethodSender:aMethod |
747 |
"arrange for a trace message to be output on Stderr, when amethod is executed. |
|
748 |
Only the sender is traced on entry. |
|
749 |
Use untraceMethod to remove this trace." |
|
35 | 750 |
|
88 | 751 |
^ self wrapMethod:aMethod |
752 |
onEntry:TraceSenderBlock |
|
753 |
onExit:LeaveBlock. |
|
754 |
! |
|
755 |
||
756 |
untraceMethod:aMethod |
|
757 |
"remove tracing of aMethod" |
|
758 |
||
759 |
"just a rename for your convenience - the same basic mechanism is used for all of these |
|
760 |
trace facilities ..." |
|
761 |
||
762 |
^ self unwrapMethod:aMethod |
|
11 | 763 |
! ! |
764 |
||
88 | 765 |
!MessageTracer class methodsFor:'method wrapping'! |
11 | 766 |
|
88 | 767 |
unwrapAllMethods |
768 |
"just in case you dont know what methods have break/trace-points |
|
769 |
on them; this removes them all" |
|
770 |
||
771 |
WrappedMethod allInstancesDo:[:aMethod | |
|
772 |
self unwrapMethod:aMethod |
|
773 |
] |
|
11 | 774 |
! |
775 |
||
88 | 776 |
unwrapMethod:aMethod |
777 |
"remove any wrapper on aMethod" |
|
778 |
||
779 |
|selector class originalMethod idx| |
|
780 |
||
155 | 781 |
MethodCounts notNil ifTrue:[ |
782 |
aMethod isWrapped ifTrue:[ |
|
783 |
MethodCounts removeKey:aMethod originalMethod ifAbsent:nil. |
|
784 |
]. |
|
785 |
MethodCounts removeKey:aMethod ifAbsent:nil. |
|
786 |
MethodCounts isEmpty ifTrue:[MethodCounts := nil]. |
|
787 |
]. |
|
788 |
||
88 | 789 |
CallingLevel := 0. |
790 |
||
791 |
(aMethod isNil or:[aMethod isWrapped not]) ifTrue:[ |
|
155 | 792 |
^ aMethod |
88 | 793 |
]. |
794 |
||
795 |
" |
|
796 |
get class/selector |
|
797 |
" |
|
798 |
class := aMethod containingClass. |
|
799 |
class isNil ifTrue:[ |
|
161 | 800 |
'MSGTRACER: no containing class for method found' infoPrintNL. |
155 | 801 |
^ aMethod |
88 | 802 |
]. |
803 |
selector := class selectorAtMethod:aMethod. |
|
804 |
||
805 |
originalMethod := aMethod originalMethod. |
|
806 |
originalMethod isNil ifTrue:[ |
|
155 | 807 |
self error:'oops, could not find original method'. |
808 |
^ aMethod |
|
88 | 809 |
]. |
810 |
||
811 |
idx := class selectorArray indexOf:selector. |
|
812 |
idx ~~ 0 ifTrue:[ |
|
155 | 813 |
class methodArray at:idx put:originalMethod |
88 | 814 |
] ifFalse:[ |
155 | 815 |
self halt:'oops, unexpected error'. |
816 |
^ aMethod |
|
88 | 817 |
]. |
818 |
||
819 |
ObjectMemory flushCaches. |
|
820 |
^ originalMethod |
|
155 | 821 |
|
161 | 822 |
"Modified: 17.12.1995 / 16:00:55 / cg" |
13 | 823 |
! |
824 |
||
88 | 825 |
wrapMethod:aMethod onEntry:entryBlock onExit:exitBlock |
826 |
"arrange for the two blocks entryBlock and exitBlock to be evaluated whenever |
|
827 |
aMethod is evaluated. |
|
828 |
EntryBlock will be called on entry, and gets the current context passed as argument. |
|
829 |
ExitBlock will be called, when the method is left, and gets the context and |
|
830 |
the methods return value as arguments." |
|
831 |
||
832 |
|selector class trapMethod s spec lits src idx save| |
|
833 |
||
834 |
CallingLevel := 0. |
|
835 |
||
836 |
" |
|
837 |
create a new method, which calls the original one, |
|
838 |
but only if not already being trapped. |
|
839 |
" |
|
840 |
(aMethod isNil or:[aMethod isWrapped]) ifTrue:[ |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
841 |
^ aMethod |
88 | 842 |
]. |
843 |
aMethod isLazyMethod ifTrue:[ |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
844 |
aMethod makeRealMethod |
88 | 845 |
]. |
846 |
||
847 |
" |
|
848 |
get class/selector |
|
849 |
" |
|
850 |
class := aMethod containingClass. |
|
851 |
class isNil ifTrue:[ |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
852 |
self error:'cannot place trap (no containing class found)'. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
853 |
^ aMethod |
88 | 854 |
]. |
855 |
selector := class selectorAtMethod:aMethod. |
|
856 |
||
857 |
||
858 |
WrappedMethod autoload. "/ for small systems |
|
859 |
||
860 |
" |
|
861 |
get a new method-spec |
|
862 |
" |
|
863 |
spec := Parser methodSpecificationForSelector:selector. |
|
864 |
||
865 |
" |
|
866 |
create a method, executing the trap-blocks and the original method via a direct call |
|
867 |
" |
|
868 |
s := WriteStream on:String new. |
|
869 |
s nextPutAll:spec. |
|
870 |
s nextPutAll:' |retVal| '. |
|
871 |
entryBlock notNil ifTrue:[ |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
872 |
s nextPutAll:'#entryBlock yourself value:thisContext. '. |
88 | 873 |
]. |
874 |
s nextPutAll:'retVal := #originalMethod yourself'; |
|
875 |
nextPutAll: ' valueWithReceiver:(thisContext receiver)'; |
|
876 |
nextPutAll: ' arguments:(thisContext args)'; |
|
877 |
nextPutAll: ' selector:(thisContext selector)'; |
|
878 |
nextPutAll: ' search:(thisContext searchClass)'; |
|
879 |
nextPutAll: ' sender:nil. '. |
|
880 |
||
881 |
exitBlock notNil ifTrue:[ |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
882 |
s nextPutAll:'#exitBlock yourself value:thisContext value:retVal.'. |
88 | 883 |
]. |
884 |
s nextPutAll:'^ retVal'; cr. |
|
885 |
||
886 |
src := s contents. |
|
887 |
save := Compiler stcCompilation. |
|
888 |
Compiler stcCompilation:#never. |
|
889 |
[ |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
890 |
Class withoutUpdatingChangesDo:[ |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
891 |
trapMethod := Compiler compile:src |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
892 |
forClass:UndefinedObject |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
893 |
inCategory:aMethod category |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
894 |
notifying:nil |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
895 |
install:false |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
896 |
skipIfSame:false |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
897 |
silent:true. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
898 |
] |
88 | 899 |
] valueNowOrOnUnwindDo:[ |
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
900 |
Compiler stcCompilation:save |
88 | 901 |
]. |
902 |
||
903 |
trapMethod changeClassTo:WrappedMethod. |
|
904 |
||
905 |
" |
|
906 |
raising our eyebrows here ... |
|
907 |
" |
|
908 |
lits := trapMethod basicLiterals. |
|
909 |
entryBlock notNil ifTrue:[ |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
910 |
lits at:(lits indexOf:#entryBlock) put:entryBlock. |
88 | 911 |
]. |
912 |
lits at:(lits indexOf:#originalMethod) put:aMethod. |
|
913 |
exitBlock notNil ifTrue:[ |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
914 |
lits at:(lits indexOf:#exitBlock) put:exitBlock. |
88 | 915 |
]. |
916 |
" |
|
917 |
change the source of this new method |
|
918 |
(to avoid confusion in the debugger ...) |
|
919 |
" |
|
920 |
trapMethod source:'this is a wrapper method - not the real one'. |
|
921 |
||
922 |
idx := class selectorArray indexOf:selector. |
|
923 |
idx ~~ 0 ifTrue:[ |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
924 |
class methodArray at:idx put:trapMethod |
88 | 925 |
] ifFalse:[ |
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
926 |
self halt:'oops, unexpected error'. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
927 |
^ aMethod |
88 | 928 |
]. |
929 |
||
930 |
ObjectMemory flushCaches. |
|
931 |
^ trapMethod |
|
932 |
||
933 |
" |
|
934 |
MessageTracer |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
935 |
wrapMethod:(Point compiledMethodAt:#scaleBy:) |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
936 |
onEntry:nil |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
937 |
onExit:[:con :retVal | |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
938 |
Transcript show:'leave Point>>scaleBy:; returning:'. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
939 |
Transcript showCr:retVal printString. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
940 |
Transcript endEntry |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
941 |
]. |
88 | 942 |
(1@2) scaleBy:5. |
943 |
MessageTracer unwrapMethod:(Point compiledMethodAt:#scaleBy:). |
|
944 |
(1@2) scaleBy:5. |
|
945 |
" |
|
946 |
" |
|
947 |
MessageTracer |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
948 |
wrapMethod:(Integer compiledMethodAt:#factorial) |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
949 |
onEntry:[:con | |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
950 |
Transcript showCr:('entering ' , con receiver printString , '>>factorial'). |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
951 |
] |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
952 |
onExit:[:con :retVal | |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
953 |
Transcript show:'leave Integer>>factorial; returning:'. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
954 |
Transcript showCr:retVal printString. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
955 |
Transcript endEntry |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
956 |
]. |
88 | 957 |
Transcript showCr:'5 factorial traced'. |
958 |
5 factorial. |
|
959 |
MessageTracer unwrapMethod:(Integer compiledMethodAt:#factorial). |
|
960 |
Transcript showCr:'5 factorial normal'. |
|
961 |
5 factorial. |
|
962 |
" |
|
963 |
" |
|
964 |
|lvl| |
|
965 |
||
966 |
lvl := 0. |
|
967 |
MessageTracer |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
968 |
wrapMethod:(Integer compiledMethodAt:#factorial) |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
969 |
onEntry:[:con | |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
970 |
Transcript spaces:lvl. lvl := lvl + 2. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
971 |
Transcript showCr:('entering ' , con receiver printString , '>>factorial'). |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
972 |
] |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
973 |
onExit:[:con :retVal | |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
974 |
lvl := lvl - 2. Transcript spaces:lvl. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
975 |
Transcript show:('leave ' , con receiver printString , '>>factorial; returning:'). |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
976 |
Transcript showCr:retVal printString. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
977 |
Transcript endEntry |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
978 |
]. |
88 | 979 |
Transcript showCr:'5 factorial traced'. |
980 |
5 factorial. |
|
981 |
MessageTracer unwrapMethod:(Integer compiledMethodAt:#factorial). |
|
982 |
Transcript showCr:'5 factorial normal'. |
|
983 |
5 factorial. |
|
984 |
" |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
985 |
|
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
986 |
"Modified: 13.12.1995 / 16:06:22 / cg" |
88 | 987 |
! ! |
988 |
||
989 |
!MessageTracer class methodsFor:'object breakpointing'! |
|
990 |
||
991 |
trap:anObject selector:aSelector |
|
992 |
"arrange for the debugger to be entered when a message with aSelector is |
|
993 |
sent to anObject. Use untrap to remove this trap. |
|
994 |
The current implementation does not allow integers or nil to be trapped." |
|
995 |
||
996 |
self wrap:anObject |
|
997 |
selector:aSelector |
|
998 |
onEntry:BreakBlock |
|
999 |
onExit:LeaveBlock. |
|
1000 |
||
1001 |
" |
|
1002 |
|p| |
|
1003 |
||
1004 |
p := Point new. |
|
1005 |
MessageTracer trap:p selector:#x:. |
|
1006 |
p x:5 |
|
1007 |
" |
|
11 | 1008 |
! |
1009 |
||
88 | 1010 |
trap:anObject selectors:aCollection |
1011 |
self wrap:anObject |
|
1012 |
selectors:aCollection |
|
1013 |
onEntry:BreakBlock |
|
1014 |
onExit:LeaveBlock. |
|
1015 |
! |
|
1016 |
||
1017 |
trapAll:anObject |
|
1018 |
"trap on all messages which are understood by anObject" |
|
1019 |
||
1020 |
self wrapAll:anObject |
|
1021 |
onEntry:BreakBlock |
|
1022 |
onExit:LeaveBlock. |
|
1023 |
! |
|
1024 |
||
1025 |
trapAll:anObject from:aClass |
|
1026 |
"trap on all messages defined in aClass sent to anObject" |
|
1027 |
||
1028 |
self trap:anObject selectors:aClass selectorArray |
|
1029 |
! |
|
1030 |
||
1031 |
untrap:anObject |
|
1032 |
"remove any traps on anObject" |
|
1033 |
||
1034 |
"this is done by just patching the objects class back to the original" |
|
1035 |
||
1036 |
|orgClass| |
|
1037 |
||
1038 |
orgClass := anObject class. |
|
1039 |
orgClass category == #trapping ifFalse:[ |
|
1040 |
^ self |
|
1041 |
]. |
|
1042 |
||
1043 |
anObject changeClassTo:orgClass superclass |
|
1044 |
||
1045 |
" |
|
1046 |
|p| |
|
1047 |
||
1048 |
p := Point new copy. |
|
1049 |
MessageTracer trace:p selector:#x:. |
|
1050 |
MessageTracer trace:p selector:#y:. |
|
1051 |
p y:1. |
|
1052 |
p x:2. |
|
1053 |
MessageTracer untrap:p |
|
1054 |
p y:2. |
|
1055 |
p x:1. |
|
1056 |
" |
|
13 | 1057 |
! |
1058 |
||
88 | 1059 |
untrap:anObject selector:aSelector |
1060 |
"remove trap on aSelector from anObject" |
|
1061 |
||
1062 |
|orgClass idx sels| |
|
1063 |
||
1064 |
orgClass := anObject class. |
|
1065 |
orgClass category == #trapping ifFalse:[^ self]. |
|
1066 |
||
1067 |
sels := orgClass selectorArray. |
|
1068 |
idx := sels indexOf:aSelector. |
|
1069 |
idx == 0 ifTrue:[^ self]. |
|
1070 |
||
1071 |
sels size == 1 ifTrue:[ |
|
1072 |
"the last trap got removed" |
|
1073 |
anObject changeClassTo:orgClass superclass. |
|
1074 |
^ self |
|
1075 |
]. |
|
1076 |
orgClass |
|
1077 |
setSelectors:(sels copyWithoutIndex:idx) |
|
1078 |
methods:(orgClass methodArray copyWithoutIndex:idx). |
|
1079 |
ObjectMemory flushCaches. "avoid calling the old trap method" |
|
1080 |
||
1081 |
" |
|
1082 |
|p| |
|
1083 |
||
1084 |
p := Point new copy. |
|
1085 |
MessageTracer trace:p selector:#x:. |
|
1086 |
MessageTracer trace:p selector:#y:. |
|
1087 |
'trace both ...' errorPrintNL. |
|
1088 |
p x:2. |
|
1089 |
p y:1. |
|
1090 |
'trace only y ...' errorPrintNL. |
|
1091 |
MessageTracer untrap:p selector:#x:. |
|
1092 |
p x:2. |
|
1093 |
p y:1. |
|
1094 |
'trace none ...' errorPrintNL. |
|
1095 |
MessageTracer untrap:p selector:#y:. |
|
1096 |
p x:2. |
|
1097 |
p y:1. |
|
1098 |
" |
|
11 | 1099 |
! ! |
1100 |
||
88 | 1101 |
!MessageTracer class methodsFor:'object tracing'! |
1102 |
||
1103 |
trace:anObject selector:aSelector |
|
1104 |
"arrange for a trace message to be output on Stderr, when a message with |
|
1105 |
aSelector is sent to anObject. Both entry and exit are traced. |
|
1106 |
Use untrap to remove this trace. |
|
1107 |
The current implementation does not allow integers or nil to be traced." |
|
1108 |
||
1109 |
|methodName| |
|
1110 |
||
1111 |
methodName := anObject class name , '>>' , aSelector. |
|
1112 |
self wrap:anObject |
|
1113 |
selector:aSelector |
|
1114 |
onEntry:[:con | |
|
1115 |
'enter ' errorPrint. methodName errorPrint. |
|
1116 |
' receiver=' errorPrint. con receiver printString errorPrint. |
|
1117 |
' args=' errorPrint. (con args) printString errorPrint. |
|
1118 |
' from:' errorPrint. con sender errorPrintNL. |
|
1119 |
] |
|
1120 |
onExit:[:con :retVal | |
|
1121 |
'leave ' errorPrint. methodName errorPrint. |
|
1122 |
' receiver=' errorPrint. con receiver printString errorPrint. |
|
1123 |
' returning:' errorPrint. retVal printString errorPrintNL. |
|
1124 |
]. |
|
1125 |
||
1126 |
" |
|
1127 |
|p| |
|
8 | 1128 |
|
88 | 1129 |
p := Point new. |
1130 |
MessageTracer trace:p selector:#x:. |
|
1131 |
p x:5. |
|
1132 |
p y:1. |
|
1133 |
p x:10. |
|
1134 |
MessageTracer untrap:p. |
|
1135 |
p x:7 |
|
1136 |
" |
|
1137 |
" |
|
1138 |
|a| |
|
1139 |
||
1140 |
a := #(6 1 9 66 2 17) copy. |
|
1141 |
MessageTracer trace:a selector:#at:put:. |
|
1142 |
MessageTracer trace:a selector:#at:. |
|
1143 |
a sort. |
|
1144 |
" |
|
1145 |
! |
|
1146 |
||
1147 |
trace:anObject selectors:aCollection |
|
1148 |
aCollection do:[:aSelector | |
|
1149 |
self trace:anObject selector:aSelector |
|
1150 |
] |
|
1151 |
||
1152 |
" |
|
1153 |
trace all methods in Display, which are implemented |
|
1154 |
in the DisplayWorkstation class. |
|
1155 |
" |
|
1156 |
||
1157 |
" |
|
1158 |
MessageTracer trace:Display selectors:(XWorkstation selectorArray) |
|
1159 |
MessageTracer untrace:Display |
|
1160 |
" |
|
1161 |
! |
|
1162 |
||
1163 |
traceAll:anObject |
|
1164 |
"trace all messages which are understood by anObject" |
|
27 | 1165 |
|
1166 |
|allSelectors| |
|
1167 |
||
1168 |
allSelectors := IdentitySet new. |
|
1169 |
anObject class withAllSuperclasses do:[:aClass | |
|
1170 |
aClass selectorArray addAllTo:allSelectors |
|
1171 |
]. |
|
88 | 1172 |
self trace:anObject selectors:allSelectors |
1173 |
||
1174 |
" |
|
1175 |
trace all (implemented) messages sent to Display |
|
1176 |
(other messages lead to an error, anyway) |
|
1177 |
" |
|
1178 |
||
1179 |
" |
|
1180 |
MessageTracer traceAll:Display |
|
1181 |
MessageTracer untrace:Display |
|
1182 |
" |
|
1183 |
! |
|
1184 |
||
1185 |
traceAll:anObject from:aClass |
|
1186 |
"trace all messages defined in aClass sent to anObject" |
|
1187 |
||
1188 |
self trace:anObject selectors:aClass selectorArray |
|
1189 |
||
1190 |
" |
|
1191 |
trace all methods in Display, which are implemented |
|
1192 |
in the DisplayWorkstation class. |
|
1193 |
" |
|
1194 |
||
1195 |
" |
|
1196 |
MessageTracer traceAll:Display from:XWorkstation |
|
1197 |
MessageTracer untrace:Display |
|
1198 |
" |
|
27 | 1199 |
! |
1200 |
||
88 | 1201 |
traceSender:anObject selector:aSelector |
1202 |
"arrange for a trace message to be output on Stderr, when a message with |
|
1203 |
aSelector is sent to anObject. Only the sender is traced on entry. |
|
1204 |
Use untrap to remove this trace. |
|
1205 |
The current implementation does not allow integers or nil to be traced." |
|
1206 |
||
1207 |
|methodName| |
|
1208 |
||
1209 |
methodName := anObject class name , '>>' , aSelector. |
|
1210 |
self wrap:anObject |
|
1211 |
selector:aSelector |
|
1212 |
onEntry:[:con | |
|
1213 |
methodName errorPrint. |
|
1214 |
' from ' errorPrint. |
|
1215 |
con sender errorPrintNL. |
|
1216 |
] |
|
1217 |
onExit:LeaveBlock. |
|
1218 |
||
1219 |
" |
|
1220 |
|p| |
|
27 | 1221 |
|
88 | 1222 |
p := Point new. |
1223 |
MessageTracer traceSender:p selector:#x:. |
|
1224 |
p x:5. |
|
1225 |
p y:1. |
|
1226 |
p x:10. |
|
1227 |
MessageTracer untrap:p. |
|
1228 |
p x:7 |
|
1229 |
" |
|
1230 |
" |
|
1231 |
|a| |
|
1232 |
||
1233 |
a := #(6 1 9 66 2 17) copy. |
|
1234 |
MessageTracer traceSender:a selector:#at:put:. |
|
1235 |
MessageTracer traceSender:a selector:#at:. |
|
1236 |
a sort. |
|
1237 |
" |
|
11 | 1238 |
! |
1239 |
||
88 | 1240 |
untrace:anObject |
1241 |
"remove any traces on anObject" |
|
1242 |
||
1243 |
"just a rename for your convenience - the same basic mechanism is used for all of these |
|
1244 |
trace facilities ..." |
|
1245 |
||
1246 |
^ self untrap:anObject |
|
1247 |
! |
|
1248 |
||
1249 |
untrace:anObject selector:aSelector |
|
1250 |
"remove traces of aSelector sent to anObject" |
|
1251 |
||
1252 |
"just a rename for your convenience - the same basic mechanism is used for all of these |
|
1253 |
trace facilities ..." |
|
1254 |
||
1255 |
^ self untrap:anObject selector:aSelector |
|
1256 |
! ! |
|
1257 |
||
1258 |
!MessageTracer class methodsFor:'object wrapping'! |
|
1259 |
||
8 | 1260 |
wrap:anObject selector:aSelector onEntry:entryBlock onExit:exitBlock |
1261 |
"arrange for the two blocks entryBlock and exitBlock to be evaluated whenever |
|
1262 |
a message with aSelector is sent to anObject. EntryBlock will be called on |
|
1263 |
entry, and get the current context passed as argument. ExitBlock will be called, |
|
1264 |
when the method is left, and get the context and the methods return value as arguments. |
|
1265 |
The current implementation does not allow integers or nil to be wrapped." |
|
1266 |
||
1267 |
"I have not yet enough experience, if the wrapped original method should |
|
1268 |
run as an instance of the original, or of the catching class; |
|
1269 |
The latter has the advantage of catching recursive and other sends, while |
|
1270 |
it might lead into trouble when the message is sent from a debugger or a long |
|
1271 |
return is done out of the original method ... |
|
1272 |
Time will show, you can experiment by setting the withOriginalClass: flag to false |
|
1273 |
" |
|
1274 |
^ self wrap:anObject selector:aSelector onEntry:entryBlock onExit:exitBlock withOriginalClass:true |
|
1275 |
! |
|
1276 |
||
1277 |
wrap:anObject selector:aSelector onEntry:entryBlock onExit:exitBlock withOriginalClass:withOriginalClass |
|
1278 |
"arrange for the two blocks entryBlock and exitBlock to be evaluated whenever |
|
1279 |
a message with aSelector is sent to anObject. EntryBlock will be called on |
|
1280 |
entry, and get the current context passed as argument. ExitBlock will be called, |
|
1281 |
when the method is left, and get the methods return value as argument. |
|
1282 |
The argument withOriginalClass controls if the original method should be called for with |
|
1283 |
the receiver being trapped upon or not. |
|
1284 |
The current implementation does not allow integers or nil to be wrapped." |
|
1285 |
||
37 | 1286 |
|newClass orgClass myMetaclass trapMethod s spec lits implClass save| |
8 | 1287 |
|
1288 |
" |
|
27 | 1289 |
some are not allowed (otherwise we get into trouble ...) |
1290 |
" |
|
1291 |
(#(class changeClassTo:) includes:aSelector) ifTrue:[ |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1292 |
Transcript showCr:'sorry, cannot place trap on: ' , aSelector. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1293 |
^ self |
27 | 1294 |
]. |
1295 |
||
1296 |
WrappedMethod autoload. "/ just to make sure ... |
|
1297 |
||
1298 |
" |
|
8 | 1299 |
create a new (anonymous) subclass of the receivers class |
1300 |
but only if not already being trapped. |
|
1301 |
" |
|
1302 |
orgClass := anObject class. |
|
1303 |
orgClass category == #trapping ifTrue:[ |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1304 |
newClass := orgClass |
8 | 1305 |
] ifFalse:[ |
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1306 |
myMetaclass := orgClass class. |
8 | 1307 |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1308 |
newClass := myMetaclass copy new. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1309 |
newClass setSuperclass:orgClass. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1310 |
newClass instSize:orgClass instSize. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1311 |
newClass flags:orgClass flags. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1312 |
newClass setClassVariableString:''. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1313 |
newClass setInstanceVariableString:''. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1314 |
newClass setName:orgClass name. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1315 |
newClass category:#trapping. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1316 |
newClass setSelectors:(Array new) methods:(Array new). |
8 | 1317 |
]. |
1318 |
||
1319 |
" |
|
1320 |
create a method, executing the trap-blocks and the original method via a super-send |
|
1321 |
" |
|
1322 |
spec := Parser methodSpecificationForSelector:aSelector. |
|
1323 |
s := WriteStream on:String new. |
|
1324 |
s nextPutAll:spec. |
|
27 | 1325 |
s nextPutAll:' |retVal stubClass| '. |
8 | 1326 |
withOriginalClass ifTrue:[ |
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1327 |
s nextPutAll:'stubClass := self class. '. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1328 |
s nextPutAll:'self changeClassTo:(stubClass superclass). '. |
8 | 1329 |
]. |
1330 |
entryBlock notNil ifTrue:[ |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1331 |
s nextPutAll:'#literal1 yourself value:thisContext. '. |
8 | 1332 |
]. |
27 | 1333 |
s nextPutAll:('retVal := #originalMethod. '). "/ just to get a place for the origianlMethod |
1334 |
s nextPutAll:('retVal := super ' , spec , '. '). |
|
8 | 1335 |
exitBlock notNil ifTrue:[ |
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1336 |
s nextPutAll:'#literal2 yourself value:thisContext value:retVal. '. |
8 | 1337 |
]. |
1338 |
withOriginalClass ifTrue:[ |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1339 |
s nextPutAll:'self changeClassTo:stubClass. '. |
8 | 1340 |
]. |
1341 |
s nextPutAll:'^ retVal'; cr. |
|
1342 |
||
37 | 1343 |
save := Compiler stcCompilation. |
1344 |
Compiler stcCompilation:#never. |
|
1345 |
[ |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1346 |
Class withoutUpdatingChangesDo:[ |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1347 |
trapMethod := Compiler compile:s contents |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1348 |
forClass:newClass |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1349 |
inCategory:'breakpointed' |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1350 |
notifying:nil |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1351 |
install:false |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1352 |
skipIfSame:false |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1353 |
silent:true. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1354 |
] |
37 | 1355 |
] valueNowOrOnUnwindDo:[ |
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1356 |
Compiler stcCompilation:save |
37 | 1357 |
]. |
8 | 1358 |
|
1359 |
lits := trapMethod literals. |
|
29 | 1360 |
implClass := orgClass whichClassIncludesSelector:aSelector. |
1361 |
implClass isNil ifTrue:[ |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1362 |
Transcript showCr:aSelector , ' is not understood by ' , orgClass name. |
29 | 1363 |
] ifFalse:[ |
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1364 |
lits at:(lits indexOf:#originalMethod) put:(implClass compiledMethodAt:aSelector). |
29 | 1365 |
]. |
8 | 1366 |
entryBlock notNil ifTrue:[ |
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1367 |
lits at:(lits indexOf:#literal1) put:entryBlock. |
8 | 1368 |
]. |
1369 |
exitBlock notNil ifTrue:[ |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1370 |
lits at:(lits indexOf:#literal2) put:exitBlock. |
8 | 1371 |
]. |
1372 |
" |
|
1373 |
change the source of this new method |
|
1374 |
(to avoid confusion in the debugger ...) |
|
1375 |
" |
|
1376 |
trapMethod source:'this is a wrapper method - not the real one'. |
|
27 | 1377 |
trapMethod changeClassTo:WrappedMethod. |
8 | 1378 |
|
1379 |
" |
|
1380 |
install this new method |
|
1381 |
" |
|
11 | 1382 |
newClass |
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1383 |
setSelectors:(newClass selectorArray copyWith:aSelector) |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1384 |
methods:(newClass methodArray copyWith:trapMethod). |
8 | 1385 |
|
1386 |
" |
|
1387 |
and finally, the big trick: |
|
1388 |
" |
|
1389 |
anObject changeClassTo:newClass |
|
1390 |
||
1391 |
" |
|
1392 |
|p| |
|
1393 |
||
1394 |
p := Point new copy. |
|
1395 |
MessageTracer |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1396 |
wrap:p |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1397 |
Selector:#y: |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1398 |
onEntry:nil |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1399 |
onExit:[:retVal | |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1400 |
Transcript show:'leave Point>>x:, returning:'. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1401 |
Transcript showCr:retVal printString. |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1402 |
Transcript endEntry |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1403 |
]. |
8 | 1404 |
Transcript showCr:'sending x: ...'. |
1405 |
p x:1. |
|
1406 |
Transcript showCr:'sending y: ...'. |
|
1407 |
p y:2. |
|
1408 |
p untrap. |
|
1409 |
Transcript showCr:'sending x: ...'. |
|
1410 |
p x:2. |
|
1411 |
Transcript showCr:'sending y: ...'. |
|
1412 |
p y:1. |
|
1413 |
" |
|
1414 |
||
1415 |
" |
|
1416 |
|p| |
|
1417 |
||
1418 |
p := Point new copy. |
|
1419 |
MessageTracer wrap:p |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1420 |
Selector:#y: |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1421 |
onEntry:[:context | self halt:'you are trapped'] |
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1422 |
onExit:nil. |
8 | 1423 |
Transcript showCr:'sending x: ...'. |
1424 |
p x:1. |
|
1425 |
Transcript showCr:'sending y: ...'. |
|
1426 |
p y:2. |
|
1427 |
p untrap. |
|
1428 |
Transcript showCr:'sending x: ...'. |
|
1429 |
p x:2. |
|
1430 |
Transcript showCr:'sending y: ...'. |
|
1431 |
p y:1. |
|
1432 |
" |
|
150
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1433 |
|
54b6185e9aa7
compile without updating changes-file (also disables historyManager for the wrap)
Claus Gittinger <cg@exept.de>
parents:
120
diff
changeset
|
1434 |
"Modified: 13.12.1995 / 16:06:56 / cg" |
8 | 1435 |
! |
1436 |
||
88 | 1437 |
wrap:anObject selectors:aCollection onEntry:entryBlock onExit:exitBlock |
1438 |
"install wrappers for anObject on all selectors from aCollection" |
|
8 | 1439 |
|
88 | 1440 |
aCollection do:[:aSelector | |
1441 |
self wrap:anObject selector:aSelector onEntry:entryBlock onExit:exitBlock |
|
1442 |
] |
|
8 | 1443 |
! |
1444 |
||
88 | 1445 |
wrapAll:anObject onEntry:entryBlock onExit:exitBlock |
1446 |
"install wrappers for anObject on all implemented selectors" |
|
27 | 1447 |
|
1448 |
|allSelectors| |
|
1449 |
||
1450 |
allSelectors := IdentitySet new. |
|
1451 |
anObject class withAllSuperclasses do:[:aClass | |
|
1452 |
aClass selectorArray addAllTo:allSelectors |
|
1453 |
]. |
|
88 | 1454 |
self wrap:anObject selectors:allSelectors onEntry:entryBlock onExit:exitBlock |
1455 |
! ! |
|
1456 |
||
1457 |
!MessageTracer class methodsFor:'trace helpers'! |
|
1458 |
||
1459 |
printEntryFull:aContext |
|
1460 |
self printEntryFull:aContext level:0 |
|
1461 |
! |
|
27 | 1462 |
|
88 | 1463 |
printEntryFull:aContext level:lvl |
1464 |
(String new:lvl) errorPrint. |
|
1465 |
'enter ' errorPrint. |
|
1466 |
aContext methodClass name errorPrint. |
|
1467 |
' ' errorPrint. |
|
1468 |
aContext selector errorPrint. |
|
1469 |
' rcvr=' errorPrint. |
|
1470 |
aContext receiver "printString" errorPrint. |
|
1471 |
' args=' errorPrint. |
|
1472 |
(aContext args) "printString" errorPrint. |
|
1473 |
' from:' errorPrint. aContext sender errorPrintNL. |
|
1474 |
! |
|
27 | 1475 |
|
88 | 1476 |
printEntrySender:aContext |
1477 |
aContext methodClass name errorPrint. |
|
1478 |
' ' errorPrint. aContext selector errorPrint. |
|
1479 |
' from ' errorPrint. |
|
1480 |
aContext sender errorPrintNL. |
|
1481 |
! |
|
1482 |
||
1483 |
printExit:aContext with:retVal |
|
1484 |
self printExit:aContext with:retVal level:0 |
|
27 | 1485 |
! |
1486 |
||
88 | 1487 |
printExit:aContext with:retVal level:lvl |
1488 |
(String new:lvl) errorPrint. |
|
1489 |
'leave ' errorPrint. |
|
1490 |
aContext methodClass name errorPrint. |
|
1491 |
' ' errorPrint. |
|
1492 |
aContext selector errorPrint. |
|
1493 |
' rcvr=' errorPrint. |
|
1494 |
aContext receiver "printString" errorPrint. |
|
1495 |
' return:' errorPrint. retVal "printString" errorPrintNL. |
|
1496 |
! ! |
|
13 | 1497 |
|
88 | 1498 |
!MessageTracer methodsFor:'trace helpers '! |
1499 |
||
1500 |
stepInterrupt |
|
1501 |
"called for every send while tracing" |
|
13 | 1502 |
|
88 | 1503 |
|con| |
13 | 1504 |
|
88 | 1505 |
StepInterruptPending := nil. |
1506 |
con := thisContext sender. |
|
1507 |
con lineNumber == 1 ifTrue:[ |
|
1508 |
traceDetail == true ifTrue:[ |
|
1509 |
self class printEntryFull:con. |
|
1510 |
] ifFalse:[ |
|
1511 |
con printNL. |
|
1512 |
] |
|
1513 |
]. |
|
1514 |
ObjectMemory flushInlineCaches. |
|
1515 |
StepInterruptPending := 1. |
|
1516 |
InterruptPending := 1. |
|
1517 |
^ self |
|
13 | 1518 |
! |
1519 |
||
88 | 1520 |
trace:aBlock detail:fullDetail |
1521 |
"trace execution of aBlock." |
|
1522 |
||
1523 |
traceDetail := fullDetail. |
|
1524 |
ObjectMemory stepInterruptHandler:self. |
|
1525 |
^ [ |
|
1526 |
ObjectMemory flushInlineCaches. |
|
1527 |
StepInterruptPending := 1. |
|
1528 |
InterruptPending := 1. |
|
1529 |
aBlock value |
|
1530 |
] valueNowOrOnUnwindDo:[ |
|
1531 |
StepInterruptPending := nil. |
|
1532 |
ObjectMemory stepInterruptHandler:nil. |
|
11 | 1533 |
] |
1534 |
||
1535 |
" |
|
88 | 1536 |
MessageTracer trace:[#(6 5 4 3 2 1) sort] detail:false |
1537 |
MessageTracer trace:[#(6 5 4 3 2 1) sort] detail:true |
|
8 | 1538 |
" |
1539 |
! ! |
|
27 | 1540 |
|
98 | 1541 |
!MessageTracer class methodsFor:'documentation'! |
1542 |
||
1543 |
version |
|
161 | 1544 |
^ '$Header: /cvs/stx/stx/libbasic3/MessageTracer.st,v 1.29 1995-12-17 15:20:58 cg Exp $' |
98 | 1545 |
! ! |
27 | 1546 |
MessageTracer initialize! |