8
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
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 |
|
|
13 |
Object subclass:#MessageTracer
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'System-Support'
|
|
18 |
!
|
|
19 |
|
|
20 |
MessageTracer comment:'
|
|
21 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
|
22 |
All Rights Reserved
|
|
23 |
'!
|
|
24 |
|
|
25 |
!MessageTracer class methodsFor:'documentation'!
|
|
26 |
|
9
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
|
30 |
All Rights Reserved
|
|
31 |
|
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
"
|
|
39 |
!
|
|
40 |
|
|
41 |
version
|
|
42 |
"
|
|
43 |
$Header: /cvs/stx/stx/libbasic3/MessageTracer.st,v 1.2 1994-06-02 17:19:55 claus Exp $
|
|
44 |
"
|
|
45 |
!
|
|
46 |
|
8
|
47 |
documentation
|
|
48 |
"
|
|
49 |
This is not a real class, in that there are no instances of
|
|
50 |
MessageTracer.
|
|
51 |
It has been created, to provide a common home for the tracing
|
|
52 |
facilities (originally, they where in Object, but have moved to
|
|
53 |
allow easier separation of development vs. runtime configurations.
|
|
54 |
|
|
55 |
trapping sends to a specific object:
|
|
56 |
|
|
57 |
MessageTracer trap:anObject selector:aSelector
|
|
58 |
...
|
|
59 |
MessageTracer untrap:anObject selector:aSelector
|
|
60 |
or:
|
|
61 |
MessageTracer untrap:anObject
|
|
62 |
|
|
63 |
|
|
64 |
trapping evaluation of a specific method:
|
|
65 |
|
|
66 |
MessageTracer trapMethod:aMethod
|
|
67 |
...
|
|
68 |
MessageTracer unwrapMethod:aMethod
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
tracing sends to a specific object:
|
|
73 |
|
|
74 |
MessageTracer trace:anObject selector:aSelector
|
|
75 |
...
|
|
76 |
MessageTracer untrace:anObject selector:aSelector
|
|
77 |
or:
|
|
78 |
MessageTracer untrace:anObject
|
|
79 |
|
|
80 |
|
|
81 |
tracing sender only:
|
|
82 |
|
|
83 |
MessageTracer traceSender:anObject selector:aSelector
|
|
84 |
...
|
|
85 |
MessageTracer untrace:anObject selector:aSelector
|
|
86 |
or:
|
|
87 |
MessageTracer untrace:anObject
|
|
88 |
|
|
89 |
|
|
90 |
tracing evaluation of a specific method:
|
|
91 |
|
|
92 |
MessageTracer traceMethod:aMethod
|
|
93 |
...
|
|
94 |
MessageTracer unwrapmethod:aMethod
|
|
95 |
"
|
|
96 |
! !
|
|
97 |
|
|
98 |
!MessageTracer class methodsFor:'object wrapping'!
|
|
99 |
|
|
100 |
wrap:anObject selector:aSelector onEntry:entryBlock onExit:exitBlock
|
|
101 |
"arrange for the two blocks entryBlock and exitBlock to be evaluated whenever
|
|
102 |
a message with aSelector is sent to anObject. EntryBlock will be called on
|
|
103 |
entry, and get the current context passed as argument. ExitBlock will be called,
|
|
104 |
when the method is left, and get the context and the methods return value as arguments.
|
|
105 |
The current implementation does not allow integers or nil to be wrapped."
|
|
106 |
|
|
107 |
"I have not yet enough experience, if the wrapped original method should
|
|
108 |
run as an instance of the original, or of the catching class;
|
|
109 |
The latter has the advantage of catching recursive and other sends, while
|
|
110 |
it might lead into trouble when the message is sent from a debugger or a long
|
|
111 |
return is done out of the original method ...
|
|
112 |
Time will show, you can experiment by setting the withOriginalClass: flag to false
|
|
113 |
"
|
|
114 |
^ self wrap:anObject selector:aSelector onEntry:entryBlock onExit:exitBlock withOriginalClass:true
|
|
115 |
!
|
|
116 |
|
|
117 |
wrap:anObject selector:aSelector onEntry:entryBlock onExit:exitBlock withOriginalClass:withOriginalClass
|
|
118 |
"arrange for the two blocks entryBlock and exitBlock to be evaluated whenever
|
|
119 |
a message with aSelector is sent to anObject. EntryBlock will be called on
|
|
120 |
entry, and get the current context passed as argument. ExitBlock will be called,
|
|
121 |
when the method is left, and get the methods return value as argument.
|
|
122 |
The argument withOriginalClass controls if the original method should be called for with
|
|
123 |
the receiver being trapped upon or not.
|
|
124 |
The current implementation does not allow integers or nil to be wrapped."
|
|
125 |
|
|
126 |
|newClass orgClass myMetaclass trapMethod s spec lits|
|
|
127 |
|
|
128 |
"
|
|
129 |
create a new (anonymous) subclass of the receivers class
|
|
130 |
but only if not already being trapped.
|
|
131 |
"
|
|
132 |
orgClass := anObject class.
|
|
133 |
orgClass category == #trapping ifTrue:[
|
|
134 |
newClass := orgClass
|
|
135 |
] ifFalse:[
|
|
136 |
myMetaclass := orgClass class.
|
|
137 |
|
|
138 |
newClass := myMetaclass new.
|
|
139 |
newClass setSuperclass:orgClass.
|
|
140 |
newClass instSize:orgClass instSize.
|
|
141 |
newClass flags:orgClass flags.
|
|
142 |
newClass setClassVariableString:''.
|
|
143 |
newClass setInstanceVariableString:''.
|
|
144 |
newClass setName:orgClass name.
|
|
145 |
newClass category:#trapping.
|
|
146 |
newClass setSelectorArray:(Array new).
|
|
147 |
newClass setMethodArray:(Array new).
|
|
148 |
].
|
|
149 |
|
|
150 |
"
|
|
151 |
create a method, executing the trap-blocks and the original method via a super-send
|
|
152 |
"
|
|
153 |
spec := Parser methodSpecificationForSelector:aSelector.
|
|
154 |
s := WriteStream on:String new.
|
|
155 |
s nextPutAll:spec.
|
|
156 |
s cr.
|
|
157 |
s nextPutAll:'|retVal stubClass|'; cr.
|
|
158 |
withOriginalClass ifTrue:[
|
|
159 |
s nextPutAll:'stubClass := self class.'; cr.
|
|
160 |
s nextPutAll:'self changeClassTo:(stubClass superclass).'; cr.
|
|
161 |
].
|
|
162 |
entryBlock notNil ifTrue:[
|
|
163 |
s nextPutAll:'#literal1 yourself value:thisContext.'; cr.
|
|
164 |
].
|
|
165 |
s nextPutAll:('retVal := super ' , spec , '.'); cr.
|
|
166 |
exitBlock notNil ifTrue:[
|
|
167 |
s nextPutAll:'#literal2 yourself value:thisContext value:retVal.'; cr.
|
|
168 |
].
|
|
169 |
withOriginalClass ifTrue:[
|
|
170 |
s nextPutAll:'self changeClassTo:stubClass.'; cr.
|
|
171 |
].
|
|
172 |
s nextPutAll:'^ retVal'; cr.
|
|
173 |
|
|
174 |
trapMethod := Compiler compile:s contents
|
|
175 |
forClass:newClass
|
|
176 |
inCategory:'breakpointed'
|
|
177 |
notifying:nil
|
|
178 |
install:false
|
|
179 |
skipIfSame:false
|
|
180 |
silent:true.
|
|
181 |
|
|
182 |
lits := trapMethod literals.
|
|
183 |
entryBlock notNil ifTrue:[
|
|
184 |
lits at:(lits indexOf:#literal1) put:entryBlock.
|
|
185 |
].
|
|
186 |
exitBlock notNil ifTrue:[
|
|
187 |
lits at:(lits indexOf:#literal2) put:exitBlock.
|
|
188 |
].
|
|
189 |
"
|
|
190 |
change the source of this new method
|
|
191 |
(to avoid confusion in the debugger ...)
|
|
192 |
"
|
|
193 |
trapMethod source:'this is a wrapper method - not the real one'.
|
|
194 |
|
|
195 |
"
|
|
196 |
install this new method
|
|
197 |
"
|
|
198 |
newClass setSelectorArray:(newClass selectorArray copyWith:aSelector).
|
|
199 |
newClass setMethodArray:(newClass methodArray copyWith:trapMethod).
|
|
200 |
|
|
201 |
"
|
|
202 |
and finally, the big trick:
|
|
203 |
"
|
|
204 |
anObject changeClassTo:newClass
|
|
205 |
|
|
206 |
"
|
|
207 |
|p|
|
|
208 |
|
|
209 |
p := Point new copy.
|
|
210 |
MessageTracer
|
|
211 |
wrap:p
|
|
212 |
Selector:#y:
|
|
213 |
onEntry:nil
|
|
214 |
onExit:[:retVal |
|
|
215 |
Transcript show:'leave Point>>x:, returning:'.
|
|
216 |
Transcript showCr:retVal printString.
|
|
217 |
Transcript endEntry
|
|
218 |
].
|
|
219 |
Transcript showCr:'sending x: ...'.
|
|
220 |
p x:1.
|
|
221 |
Transcript showCr:'sending y: ...'.
|
|
222 |
p y:2.
|
|
223 |
p untrap.
|
|
224 |
Transcript showCr:'sending x: ...'.
|
|
225 |
p x:2.
|
|
226 |
Transcript showCr:'sending y: ...'.
|
|
227 |
p y:1.
|
|
228 |
"
|
|
229 |
|
|
230 |
"
|
|
231 |
|p|
|
|
232 |
|
|
233 |
p := Point new copy.
|
|
234 |
MessageTracer wrap:p
|
|
235 |
Selector:#y:
|
|
236 |
onEntry:[:context | self halt:'you are trapped']
|
|
237 |
onExit:nil.
|
|
238 |
Transcript showCr:'sending x: ...'.
|
|
239 |
p x:1.
|
|
240 |
Transcript showCr:'sending y: ...'.
|
|
241 |
p y:2.
|
|
242 |
p untrap.
|
|
243 |
Transcript showCr:'sending x: ...'.
|
|
244 |
p x:2.
|
|
245 |
Transcript showCr:'sending y: ...'.
|
|
246 |
p y:1.
|
|
247 |
"
|
|
248 |
! !
|
|
249 |
|
|
250 |
!MessageTracer class methodsFor:'method wrapping'!
|
|
251 |
|
|
252 |
wrapMethod:aMethod onEntry:entryBlock onExit:exitBlock
|
|
253 |
"arrange for the two blocks entryBlock and exitBlock to be evaluated whenever
|
|
254 |
aMethod is evaluated.
|
|
255 |
EntryBlock will be called on entry, and get the current context passed as argument.
|
|
256 |
ExitBlock will be called, when the method is left, and get context and
|
|
257 |
the methods return value as arguments."
|
|
258 |
|
|
259 |
|parser selector args nArgs class trapMethod s spec lits src idx|
|
|
260 |
|
|
261 |
"
|
|
262 |
create a new method, which calls the original one,
|
|
263 |
but only if not already being trapped.
|
|
264 |
"
|
|
265 |
aMethod isWrapped ifTrue:[
|
|
266 |
^ aMethod
|
|
267 |
].
|
|
268 |
|
|
269 |
"
|
|
270 |
get class/selector
|
|
271 |
"
|
|
272 |
class := aMethod containingClass.
|
|
273 |
class isNil ifTrue:[
|
|
274 |
self error:'cannot place trap (no containing class found)'.
|
|
275 |
^ aMethod
|
|
276 |
].
|
|
277 |
selector := class selectorForMethod:aMethod.
|
|
278 |
|
|
279 |
"
|
|
280 |
get a new method-spec
|
|
281 |
"
|
|
282 |
spec := Parser methodSpecificationForSelector:selector.
|
|
283 |
|
|
284 |
"
|
|
285 |
create a method, executing the trap-blocks and the original method via a direct call
|
|
286 |
"
|
|
287 |
s := WriteStream on:String new.
|
|
288 |
s nextPutAll:spec.
|
|
289 |
s nextPutAll:' |retVal| '.
|
|
290 |
entryBlock notNil ifTrue:[
|
|
291 |
s nextPutAll:'#entryBlock yourself value:thisContext. '.
|
|
292 |
].
|
|
293 |
s nextPutAll:'retVal := #originalMethod yourself';
|
|
294 |
nextPutAll: ' valueWithReceiver:(thisContext receiver)';
|
|
295 |
nextPutAll: ' arguments:(thisContext args)';
|
|
296 |
nextPutAll: ' selector:(thisContext selector)';
|
|
297 |
nextPutAll: ' search:(thisContext searchClass) yourself. '.
|
|
298 |
|
|
299 |
exitBlock notNil ifTrue:[
|
|
300 |
s nextPutAll:'#exitBlock yourself value:thisContext value:retVal.'.
|
|
301 |
].
|
|
302 |
s nextPutAll:'^ retVal'; cr.
|
|
303 |
|
|
304 |
src := s contents.
|
|
305 |
trapMethod := Compiler compile:src
|
|
306 |
forClass:UndefinedObject
|
|
307 |
inCategory:aMethod category
|
|
308 |
notifying:nil
|
|
309 |
install:false
|
|
310 |
skipIfSame:false
|
|
311 |
silent:true.
|
|
312 |
trapMethod changeClassTo:WrappedMethod.
|
|
313 |
|
|
314 |
lits := trapMethod basicLiterals.
|
|
315 |
entryBlock notNil ifTrue:[
|
|
316 |
lits at:(lits indexOf:#entryBlock) put:entryBlock.
|
|
317 |
].
|
|
318 |
lits at:(lits indexOf:#originalMethod) put:aMethod.
|
|
319 |
exitBlock notNil ifTrue:[
|
|
320 |
lits at:(lits indexOf:#exitBlock) put:exitBlock.
|
|
321 |
].
|
|
322 |
"
|
|
323 |
change the source of this new method
|
|
324 |
(to avoid confusion in the debugger ...)
|
|
325 |
"
|
|
326 |
trapMethod source:'this is a wrapper method - not the real one'.
|
|
327 |
|
|
328 |
idx := class selectorArray indexOf:selector.
|
|
329 |
idx ~~ 0 ifTrue:[
|
|
330 |
class methodArray at:idx put:trapMethod
|
|
331 |
] ifFalse:[
|
|
332 |
self halt:'oops, unexpected error'.
|
|
333 |
^ aMethod
|
|
334 |
].
|
|
335 |
|
|
336 |
ObjectMemory flushCaches.
|
|
337 |
^ trapMethod
|
|
338 |
|
|
339 |
"
|
|
340 |
MessageTracer
|
|
341 |
wrapMethod:(Point compiledMethodAt:#scaleBy:)
|
|
342 |
onEntry:nil
|
|
343 |
onExit:[:con :retVal |
|
|
344 |
Transcript show:'leave Point>>scaleBy:; returning:'.
|
|
345 |
Transcript showCr:retVal printString.
|
|
346 |
Transcript endEntry
|
|
347 |
].
|
|
348 |
(1@2) scaleBy:5.
|
|
349 |
MessageTracer unwrapMethod:(Point compiledMethodAt:#scaleBy:).
|
|
350 |
(1@2) scaleBy:5.
|
|
351 |
"
|
|
352 |
"
|
|
353 |
MessageTracer
|
|
354 |
wrapMethod:(Integer compiledMethodAt:#factorial)
|
|
355 |
onEntry:[:con |
|
|
356 |
Transcript showCr:('entering ' , con receiver printString , '>>factorial').
|
|
357 |
]
|
|
358 |
onExit:[:con :retVal |
|
|
359 |
Transcript show:'leave Integer>>factorial; returning:'.
|
|
360 |
Transcript showCr:retVal printString.
|
|
361 |
Transcript endEntry
|
|
362 |
].
|
|
363 |
Transcript showCr:'5 factorial traced'.
|
|
364 |
5 factorial.
|
|
365 |
MessageTracer unwrapMethod:(Integer compiledMethodAt:#factorial).
|
|
366 |
Transcript showCr:'5 factorial normal'.
|
|
367 |
5 factorial.
|
|
368 |
"
|
|
369 |
"
|
|
370 |
|lvl|
|
|
371 |
|
|
372 |
lvl := 0.
|
|
373 |
MessageTracer
|
|
374 |
wrapMethod:(Integer compiledMethodAt:#factorial)
|
|
375 |
onEntry:[:con |
|
|
376 |
Transcript spaces:lvl. lvl := lvl + 2.
|
|
377 |
Transcript showCr:('entering ' , con receiver printString , '>>factorial').
|
|
378 |
]
|
|
379 |
onExit:[:con :retVal |
|
|
380 |
lvl := lvl - 2. Transcript spaces:lvl.
|
|
381 |
Transcript show:('leave ' , con receiver printString , '>>factorial; returning:').
|
|
382 |
Transcript showCr:retVal printString.
|
|
383 |
Transcript endEntry
|
|
384 |
].
|
|
385 |
Transcript showCr:'5 factorial traced'.
|
|
386 |
5 factorial.
|
|
387 |
MessageTracer unwrapMethod:(Integer compiledMethodAt:#factorial).
|
|
388 |
Transcript showCr:'5 factorial normal'.
|
|
389 |
5 factorial.
|
|
390 |
"
|
|
391 |
!
|
|
392 |
|
|
393 |
unwrapMethod:aMethod
|
|
394 |
"remove any wrapper on aMethod"
|
|
395 |
|
|
396 |
|parser selector args nArgs class originalMethod s spec lits src idx|
|
|
397 |
|
|
398 |
aMethod isWrapped ifFalse:[
|
|
399 |
^ aMethod
|
|
400 |
].
|
|
401 |
|
|
402 |
"
|
|
403 |
get class/selector
|
|
404 |
"
|
|
405 |
class := aMethod containingClass.
|
|
406 |
class isNil ifTrue:[
|
|
407 |
self error:'cannot place trap (no containing class found)'.
|
|
408 |
^ aMethod
|
|
409 |
].
|
|
410 |
selector := class selectorForMethod:aMethod.
|
|
411 |
|
|
412 |
originalMethod := aMethod originalMethod.
|
|
413 |
originalMethod isNil ifTrue:[
|
|
414 |
self error:'oops, could not find original method'.
|
|
415 |
^ aMethod
|
|
416 |
].
|
|
417 |
|
|
418 |
idx := class selectorArray indexOf:selector.
|
|
419 |
idx ~~ 0 ifTrue:[
|
|
420 |
class methodArray at:idx put:originalMethod
|
|
421 |
] ifFalse:[
|
|
422 |
self halt:'oops, unexpected error'.
|
|
423 |
^ aMethod
|
|
424 |
].
|
|
425 |
|
|
426 |
ObjectMemory flushCaches.
|
|
427 |
^ originalMethod
|
|
428 |
!
|
|
429 |
|
|
430 |
unwrapAllMethods
|
|
431 |
"just in case you dont know what methods have break/trace-points
|
|
432 |
on them; this removes them all"
|
|
433 |
|
|
434 |
WrappedMethod allInstancesDo:[:aMethod |
|
|
435 |
self unwrapMethod:aMethod
|
|
436 |
]
|
|
437 |
! !
|
|
438 |
|
|
439 |
!MessageTracer class methodsFor:'class wrapping'!
|
|
440 |
|
|
441 |
wrapClass:aClass selector:aSelector onEntry:entryBlock onExit:exitBlock
|
|
442 |
"arrange for the two blocks entryBlock and exitBlock to be evaluated whenever
|
|
443 |
aSelector is sent to instances of aClass or subclasses.
|
|
444 |
EntryBlock will be called on entry, and get the current context passed as argument.
|
|
445 |
ExitBlock will be called, when the method is left, and get context and the methods return value as arguments.
|
|
446 |
"
|
|
447 |
|
|
448 |
|parser sourceString selector args nArgs newClass orgClass myMetaclass trapMethod s spec lits src idx|
|
|
449 |
|
|
450 |
"
|
|
451 |
create a new method, which calls the original one,
|
|
452 |
but only if not already being trapped.
|
|
453 |
"
|
|
454 |
spec := Parser methodSpecificationForSelector:aSelector.
|
|
455 |
|
|
456 |
s := WriteStream on:String new.
|
|
457 |
s nextPutAll:spec.
|
|
458 |
s cr.
|
|
459 |
s nextPutAll:'|retVal stubClass|'; cr.
|
|
460 |
entryBlock notNil ifTrue:[
|
|
461 |
s nextPutAll:'#literal1 yourself value:thisContext.'; cr.
|
|
462 |
].
|
|
463 |
s nextPutAll:('retVal := super ' , spec , '.'); cr.
|
|
464 |
exitBlock notNil ifTrue:[
|
|
465 |
s nextPutAll:'#literal2 yourself value:thisContext value:retVal.'; cr.
|
|
466 |
].
|
|
467 |
s nextPutAll:'^ retVal'; cr.
|
|
468 |
|
|
469 |
trapMethod := Compiler compile:s contents
|
|
470 |
forClass:newClass
|
|
471 |
inCategory:'trapping'
|
|
472 |
notifying:nil
|
|
473 |
install:false
|
|
474 |
skipIfSame:false
|
|
475 |
silent:true.
|
|
476 |
|
|
477 |
lits := trapMethod literals.
|
|
478 |
entryBlock notNil ifTrue:[
|
|
479 |
lits at:(lits indexOf:#literal1) put:entryBlock.
|
|
480 |
].
|
|
481 |
exitBlock notNil ifTrue:[
|
|
482 |
lits at:(lits indexOf:#literal2) put:exitBlock.
|
|
483 |
].
|
|
484 |
"
|
|
485 |
change the source of this new method
|
|
486 |
(to avoid confusion in the debugger ...)
|
|
487 |
"
|
|
488 |
trapMethod source:'this is a wrapper method - not the real one'.
|
|
489 |
|
|
490 |
"
|
|
491 |
if not already trapping, create a new class
|
|
492 |
"
|
|
493 |
aClass category == #trapping ifTrue:[
|
|
494 |
idx := aClass selectorArray indexOf:aSelector.
|
|
495 |
idx ~~ 0 ifTrue:[
|
|
496 |
aClass methodArray at:idx put:trapMethod
|
|
497 |
] ifFalse:[
|
|
498 |
aClass setSelectorArray:(aClass selectorArray copyWith:aSelector).
|
|
499 |
aClass setMethodArray:(aClass methodArray copyWith:trapMethod)
|
|
500 |
].
|
|
501 |
lits at:(lits indexOf:#literal3) put:aClass superclass.
|
|
502 |
] ifFalse:[
|
|
503 |
myMetaclass := aClass class.
|
|
504 |
|
|
505 |
newClass := myMetaclass new.
|
|
506 |
newClass setSuperclass:aClass superclass.
|
|
507 |
newClass instSize:aClass instSize.
|
|
508 |
newClass flags:aClass flags.
|
|
509 |
newClass setClassVariableString:aClass classVariableString.
|
|
510 |
newClass setInstanceVariableString:aClass instanceVariableString.
|
|
511 |
newClass setName:aClass name.
|
|
512 |
newClass category:aClass category.
|
|
513 |
newClass setSelectorArray:aClass selectorArray.
|
|
514 |
newClass setMethodArray:aClass methodArray.
|
|
515 |
|
|
516 |
aClass setSuperclass:newClass.
|
|
517 |
aClass setClassVariableString:''.
|
|
518 |
aClass setInstanceVariableString:''.
|
|
519 |
aClass category:#trapping.
|
|
520 |
aClass setSelectorArray:(Array with:aSelector).
|
|
521 |
aClass setMethodArray:(Array with:trapMethod).
|
|
522 |
|
|
523 |
lits at:(lits indexOf:#literal3) put:newClass.
|
|
524 |
].
|
|
525 |
|
|
526 |
ObjectMemory flushCaches.
|
|
527 |
|
|
528 |
"
|
|
529 |
MessageTracer
|
|
530 |
wrapMethod:(Point compiledMethodAt:#scaleBy:)
|
|
531 |
onEntry:nil
|
|
532 |
onExit:[:con :retVal |
|
|
533 |
Transcript show:'leave Point>>scaleBy:; returning:'.
|
|
534 |
Transcript showCr:retVal printString.
|
|
535 |
Transcript endEntry
|
|
536 |
].
|
|
537 |
(1@2) scaleBy:5.
|
|
538 |
MessageTracer untrapClass:Point.
|
|
539 |
(1@2) scaleBy:5.
|
|
540 |
"
|
|
541 |
"
|
|
542 |
MessageTracer
|
|
543 |
wrapMethod:(Integer compiledMethodAt:#factorial)
|
|
544 |
onEntry:[:con |
|
|
545 |
Transcript showCr:('entering ' , con receiver printString , '>>factorial').
|
|
546 |
]
|
|
547 |
onExit:[:con :retVal |
|
|
548 |
Transcript show:'leave Integer>>factorial; returning:'.
|
|
549 |
Transcript showCr:retVal printString.
|
|
550 |
Transcript endEntry
|
|
551 |
].
|
|
552 |
Transcript showCr:'5 factorial traced'.
|
|
553 |
5 factorial.
|
|
554 |
MessageTracer untrapClass:Integer.
|
|
555 |
Transcript showCr:'5 factorial normal'.
|
|
556 |
5 factorial.
|
|
557 |
"
|
|
558 |
"
|
|
559 |
|lvl|
|
|
560 |
|
|
561 |
lvl := 0.
|
|
562 |
MessageTracer
|
|
563 |
wrapMethod:(Integer compiledMethodAt:#factorial)
|
|
564 |
onEntry:[:con |
|
|
565 |
Transcript spaces:lvl. lvl := lvl + 2.
|
|
566 |
Transcript showCr:('entering ' , con receiver printString , '>>factorial').
|
|
567 |
]
|
|
568 |
onExit:[:con :retVal |
|
|
569 |
lvl := lvl - 2. Transcript spaces:lvl.
|
|
570 |
Transcript show:('leave ' , con receiver printString , '>>factorial; returning:').
|
|
571 |
Transcript showCr:retVal printString.
|
|
572 |
Transcript endEntry
|
|
573 |
].
|
|
574 |
Transcript showCr:'5 factorial traced'.
|
|
575 |
5 factorial.
|
|
576 |
MessageTracer untrapClass:Integer.
|
|
577 |
Transcript showCr:'5 factorial normal'.
|
|
578 |
5 factorial.
|
|
579 |
"
|
|
580 |
! !
|
|
581 |
|
|
582 |
!MessageTracer class methodsFor:'object breakpointing'!
|
|
583 |
|
|
584 |
trap:anObject selector:aSelector
|
|
585 |
"arrange for the debugger to be entered when a message with aSelector is
|
|
586 |
sent to anObject. Use untrap to remove this trap.
|
|
587 |
The current implementation does not allow integers or nil to be trapped."
|
|
588 |
|
|
589 |
self wrap:anObject
|
|
590 |
selector:aSelector
|
|
591 |
onEntry:[:context |
|
|
592 |
Debugger enter:context withMessage:'breakPoint hit'
|
|
593 |
]
|
|
594 |
onExit:[:context :retVal | ].
|
|
595 |
|
|
596 |
"
|
|
597 |
|p|
|
|
598 |
|
|
599 |
p := Point new.
|
|
600 |
MessageTracer trap:p selector:#x:.
|
|
601 |
p x:5
|
|
602 |
"
|
|
603 |
!
|
|
604 |
|
|
605 |
untrap:anObject selector:aSelector
|
|
606 |
"remove trap on aSelector from anObject"
|
|
607 |
|
|
608 |
|orgClass idx sels|
|
|
609 |
|
|
610 |
orgClass := anObject class.
|
|
611 |
orgClass category == #trapping ifFalse:[^ self].
|
|
612 |
|
|
613 |
sels := orgClass selectorArray.
|
|
614 |
idx := sels indexOf:aSelector.
|
|
615 |
idx == 0 ifTrue:[^ self].
|
|
616 |
|
|
617 |
sels size == 1 ifTrue:[
|
|
618 |
"the last trap got removed"
|
|
619 |
anObject changeClassTo:orgClass superclass.
|
|
620 |
^ self
|
|
621 |
].
|
|
622 |
orgClass setSelectorArray:(sels copyWithoutIndex:idx).
|
|
623 |
orgClass setMethodArray:(orgClass methodArray copyWithoutIndex:idx).
|
|
624 |
ObjectMemory flushCaches. "avoid calling the old trap method"
|
|
625 |
|
|
626 |
"
|
|
627 |
|p|
|
|
628 |
|
|
629 |
p := Point new copy.
|
|
630 |
MessageTracer trace:p selector:#x:.
|
|
631 |
MessageTracer trace:p selector:#y:.
|
|
632 |
'trace both ...' errorPrintNL.
|
|
633 |
p x:2.
|
|
634 |
p y:1.
|
|
635 |
'trace only y ...' errorPrintNL.
|
|
636 |
MessageTracer untrap:p selector:#x:.
|
|
637 |
p x:2.
|
|
638 |
p y:1.
|
|
639 |
'trace none ...' errorPrintNL.
|
|
640 |
MessageTracer untrap:p selector:#y:.
|
|
641 |
p x:2.
|
|
642 |
p y:1.
|
|
643 |
"
|
|
644 |
!
|
|
645 |
|
|
646 |
untrap:anObject
|
|
647 |
"remove any traps on anObject"
|
|
648 |
|
|
649 |
"this is done by just patching the objects class back to the original"
|
|
650 |
|
|
651 |
|orgClass|
|
|
652 |
|
|
653 |
orgClass := anObject class.
|
|
654 |
orgClass category == #trapping ifFalse:[
|
|
655 |
^ self
|
|
656 |
].
|
|
657 |
|
|
658 |
anObject changeClassTo:orgClass superclass
|
|
659 |
|
|
660 |
"
|
|
661 |
|p|
|
|
662 |
|
|
663 |
p := Point new copy.
|
|
664 |
MessageTracer trace:p selector:#x:.
|
|
665 |
MessageTracer trace:p selector:#y:.
|
|
666 |
p y:1.
|
|
667 |
p x:2.
|
|
668 |
MessageTracer untrap:p
|
|
669 |
p y:2.
|
|
670 |
p x:1.
|
|
671 |
"
|
|
672 |
! !
|
|
673 |
|
|
674 |
!MessageTracer class methodsFor:'method breakpointing'!
|
|
675 |
|
|
676 |
trapMethod:aMethod
|
|
677 |
"arrange for the debugger to be entered when aMethod is about to be executed.
|
|
678 |
Use unwrapMethod or untrapClass to remove this trap.
|
|
679 |
Be careful, to not place a trap on code needed in the debugger (i.e. on scrollBars etc.);
|
|
680 |
if there is a need to trap those, use the low-level wrap-methods, and put a check into the
|
|
681 |
entry/leave blocks."
|
|
682 |
|
|
683 |
^ self wrapMethod:aMethod
|
|
684 |
onEntry:[:context |
|
|
685 |
Debugger enter:context withMessage:'breakPoint hit'
|
|
686 |
]
|
|
687 |
onExit:[:context :retVal | ].
|
|
688 |
|
|
689 |
"
|
|
690 |
MessageTracer trapMethod:(Collection compiledMethodAt:#select:).
|
|
691 |
Dictionary new select:[:e | ]. 'not cought - Dictionary has its own select'.
|
|
692 |
(Array new:10) select:[:e | ]. 'not cought - SeqColl has its own select'.
|
|
693 |
Set new select:[:e | ]. 'cought - Set inherits this from Collection'.
|
|
694 |
MessageTracer unwrapMethod:(Collection compiledMethodAt:#select:).
|
|
695 |
"
|
|
696 |
!
|
|
697 |
|
|
698 |
untrapMethod:aMethod
|
|
699 |
"remove break on aMethod"
|
|
700 |
|
|
701 |
"just a rename for your convenience - the same basic mechanism is used for all of these
|
|
702 |
trace facilities ..."
|
|
703 |
|
|
704 |
^ self unwrapMethod:aMethod
|
|
705 |
! !
|
|
706 |
|
|
707 |
!MessageTracer class methodsFor:'class breakpointing'!
|
|
708 |
|
|
709 |
trapClass:aClass selector:aSelector
|
|
710 |
"arrange for the debugger to be entered when a message with aSelector is
|
|
711 |
sent to instances of aClass (or subclass instances). Use untrapClass to remove this trap.
|
|
712 |
Be careful, to not place a trap on code needed in the debugger (i.e. on scrollBars etc.);
|
|
713 |
if there is a need to trap those, use the low-level wrap-methods, and put a check into the
|
|
714 |
entry/leave blocks."
|
|
715 |
|
|
716 |
self wrapMethod:(aClass compiledMethodAt:aSelector)
|
|
717 |
onEntry:[:context |
|
|
718 |
Debugger enter:context withMessage:'breakPoint hit'
|
|
719 |
]
|
|
720 |
onExit:[:context :retVal | ].
|
|
721 |
|
|
722 |
"
|
|
723 |
MessageTracer trapClass:Collection selector:#select:.
|
|
724 |
Dictionary new select:[:e | ]. 'not cought - Dictionary has its own select'.
|
|
725 |
(Array new:10) select:[:e | ]. 'not cought - SeqColl has its own select'.
|
|
726 |
Set new select:[:e | ]. 'cought - Set inherits this from Collection'.
|
|
727 |
MessageTracer untrapClass:Collection
|
|
728 |
"
|
|
729 |
!
|
|
730 |
|
|
731 |
untrapClass:aClass selector:aSelector
|
|
732 |
"remove trap of aSelector sent to aClass"
|
|
733 |
|
|
734 |
|idx sels newSels newMethods|
|
|
735 |
|
|
736 |
aClass category == #trapping ifFalse:[
|
|
737 |
^ self
|
|
738 |
].
|
|
739 |
|
|
740 |
sels := aClass selectorArray.
|
|
741 |
idx := sels indexOf:aSelector.
|
|
742 |
idx == 0 ifTrue:[
|
|
743 |
^ self
|
|
744 |
].
|
|
745 |
sels size == 1 ifTrue:[
|
|
746 |
"the last trapped method"
|
|
747 |
^ self untrapClass:aClass
|
|
748 |
].
|
|
749 |
newSels := sels copyWithoutIndex:idx.
|
|
750 |
newMethods := aClass methodArray copyWithoutIndex:idx.
|
|
751 |
aClass selectors:newSels methods:newMethods.
|
|
752 |
|
|
753 |
"
|
|
754 |
MessageTracer trapClass:Point selector:#copy.
|
|
755 |
(1@2) copy.
|
|
756 |
(1@2) deepCopy.
|
|
757 |
MessageTracer trapClass:Point selector:#deepCopy.
|
|
758 |
(1@2) copy.
|
|
759 |
(1@2) deepCopy.
|
|
760 |
MessageTracer untrapClass:Point selector:#copy.
|
|
761 |
(1@2) copy.
|
|
762 |
(1@2) deepCopy.
|
|
763 |
MessageTracer untrapClass:Point selector:#deepCopy.
|
|
764 |
(1@2) copy.
|
|
765 |
(1@2) deepCopy.
|
|
766 |
"
|
|
767 |
!
|
|
768 |
|
|
769 |
untrapClass:aClass
|
|
770 |
"remove any traps on aClass"
|
|
771 |
|
|
772 |
"this is done by just patching the class back to the original"
|
|
773 |
|
|
774 |
|orgClass|
|
|
775 |
|
|
776 |
aClass category == #trapping ifFalse:[
|
|
777 |
^ self
|
|
778 |
].
|
|
779 |
orgClass := aClass superclass.
|
|
780 |
|
|
781 |
aClass setSuperclass:orgClass superclass.
|
|
782 |
aClass setClassVariableString:orgClass classVariableString.
|
|
783 |
aClass setInstanceVariableString:orgClass instanceVariableString.
|
|
784 |
aClass category:orgClass category.
|
|
785 |
aClass setSelectorArray:orgClass selectorArray.
|
|
786 |
aClass setMethodArray:orgClass methodArray.
|
|
787 |
|
|
788 |
ObjectMemory flushCaches.
|
|
789 |
|
|
790 |
"
|
|
791 |
MessageTracer untrapClass:Point
|
|
792 |
"
|
|
793 |
!
|
|
794 |
|
|
795 |
untrapAllClasses
|
|
796 |
"remove any traps on any class"
|
|
797 |
|
|
798 |
Smalltalk allBehaviorsDo:[:aClass |
|
|
799 |
self untrapClass:aClass
|
|
800 |
]
|
|
801 |
|
|
802 |
"
|
|
803 |
MessageTracer untrapAllClasses
|
|
804 |
"
|
|
805 |
! !
|
|
806 |
|
|
807 |
!MessageTracer class methodsFor:'object tracing'!
|
|
808 |
|
|
809 |
trace:anObject selector:aSelector
|
|
810 |
"arrange for a trace message to be output on Stderr, when a message with
|
|
811 |
aSelector is sent to anObject. Both entry and exit are traced.
|
|
812 |
Use untrap to remove this trace.
|
|
813 |
The current implementation does not allow integers or nil to be traced."
|
|
814 |
|
|
815 |
|methodName|
|
|
816 |
|
|
817 |
methodName := anObject class name , '>>' , aSelector.
|
|
818 |
self wrap:anObject
|
|
819 |
selector:aSelector
|
|
820 |
onEntry:[:con |
|
|
821 |
'enter ' errorPrint. methodName errorPrint.
|
|
822 |
' receiver=' errorPrint. con receiver printString errorPrint.
|
|
823 |
' args=' errorPrint. (con args) printString errorPrintNL.
|
|
824 |
]
|
|
825 |
onExit:[:con :retVal |
|
|
826 |
'leave ' errorPrint. methodName errorPrint.
|
|
827 |
' receiver=' errorPrint. con receiver printString errorPrint.
|
|
828 |
' returning:' errorPrint. retVal printString errorPrintNL.
|
|
829 |
].
|
|
830 |
|
|
831 |
"
|
|
832 |
|p|
|
|
833 |
|
|
834 |
p := Point new.
|
|
835 |
MessageTracer trace:p selector:#x:.
|
|
836 |
p x:5.
|
|
837 |
p y:1.
|
|
838 |
p x:10.
|
|
839 |
MessageTracer untrap:p.
|
|
840 |
p x:7
|
|
841 |
"
|
|
842 |
"
|
|
843 |
|a|
|
|
844 |
|
|
845 |
a := #(6 1 9 66 2 17) copy.
|
|
846 |
MessageTracer trace:a selector:#at:put:.
|
|
847 |
MessageTracer trace:a selector:#at:.
|
|
848 |
a sort.
|
|
849 |
"
|
|
850 |
!
|
|
851 |
|
|
852 |
traceSender:anObject selector:aSelector
|
|
853 |
"arrange for a trace message to be output on Stderr, when a message with
|
|
854 |
aSelector is sent to anObject. Only the sender is traced on entry.
|
|
855 |
Use untrap to remove this trace.
|
|
856 |
The current implementation does not allow integers or nil to be traced."
|
|
857 |
|
|
858 |
|methodName|
|
|
859 |
|
|
860 |
methodName := anObject class name , '>>' , aSelector.
|
|
861 |
self wrap:anObject
|
|
862 |
selector:aSelector
|
|
863 |
onEntry:[:con |
|
|
864 |
methodName errorPrint.
|
|
865 |
' from ' errorPrint.
|
|
866 |
con sender errorPrintNL.
|
|
867 |
]
|
|
868 |
onExit:[:con :retVal |
|
|
869 |
].
|
|
870 |
|
|
871 |
"
|
|
872 |
|p|
|
|
873 |
|
|
874 |
p := Point new.
|
|
875 |
MessageTracer traceSender:p selector:#x:.
|
|
876 |
p x:5.
|
|
877 |
p y:1.
|
|
878 |
p x:10.
|
|
879 |
MessageTracer untrap:p.
|
|
880 |
p x:7
|
|
881 |
"
|
|
882 |
"
|
|
883 |
|a|
|
|
884 |
|
|
885 |
a := #(6 1 9 66 2 17) copy.
|
|
886 |
MessageTracer traceSender:a selector:#at:put:.
|
|
887 |
MessageTracer traceSender:a selector:#at:.
|
|
888 |
a sort.
|
|
889 |
"
|
|
890 |
!
|
|
891 |
|
|
892 |
untrace:anObject selector:aSelector
|
|
893 |
"remove traces of aSelector sent to anObject"
|
|
894 |
|
|
895 |
"just a rename for your convenience - the same basic mechanism is used for all of these
|
|
896 |
trace facilities ..."
|
|
897 |
|
|
898 |
^ self untrap:anObject selector:aSelector
|
|
899 |
!
|
|
900 |
|
|
901 |
untrace:anObject
|
|
902 |
"remove any traces on anObject"
|
|
903 |
|
|
904 |
"just a rename for your convenience - the same basic mechanism is used for all of these
|
|
905 |
trace facilities ..."
|
|
906 |
|
|
907 |
^ self untrap:anObject
|
|
908 |
! !
|
|
909 |
|
|
910 |
!MessageTracer class methodsFor:'method tracing'!
|
|
911 |
|
|
912 |
traceMethod:aMethod
|
|
913 |
"arrange for a trace message to be output on Stderr, when aMethod is executed.
|
|
914 |
Use unwrapMethod to remove this."
|
|
915 |
|
|
916 |
^ self wrapMethod:aMethod
|
|
917 |
onEntry:[:con |
|
|
918 |
'enter ' errorPrint. con receiver class name errorPrint.
|
|
919 |
'>>' errorPrint.
|
|
920 |
con selector errorPrint.
|
|
921 |
' receiver=' errorPrint. con receiver printString errorPrint.
|
|
922 |
' args=' errorPrint. (con args) printString errorPrintNL.
|
|
923 |
]
|
|
924 |
onExit:[:con :retVal |
|
|
925 |
'leave ' errorPrint. con receiver class name errorPrint.
|
|
926 |
'>>' errorPrint.
|
|
927 |
con selector errorPrint.
|
|
928 |
' receiver=' errorPrint. con receiver printString errorPrint.
|
|
929 |
' returning:' errorPrint. retVal printString errorPrintNL.
|
|
930 |
].
|
|
931 |
|
|
932 |
"
|
|
933 |
MessageTracer traceMethod:(Integer compiledMethodAt:#factorial).
|
|
934 |
5 factorial.
|
|
935 |
MessageTracer unwrapMethod:(Integer compiledMethodAt:#factorial)
|
|
936 |
"
|
|
937 |
"
|
|
938 |
MessageTracer traceMethod:(SequenceableCollection compiledMethodAt:#quickSortFrom:to:).
|
|
939 |
#(6 1 9 66 2 17) copy sort.
|
|
940 |
MessageTracer unwrapMethod:(SequenceableCollection compiledMethodAt:#quickSortFrom:to:).
|
|
941 |
"
|
|
942 |
"
|
|
943 |
MessageTracer traceMethod:(Array compiledMethodAt:#at:).
|
|
944 |
MessageTracer traceMethod:(Array compiledMethodAt:#at:put:).
|
|
945 |
#(6 1 9 66 2 17) copy sort.
|
|
946 |
MessageTracer unwrapMethod:(Array compiledMethodAt:#at:).
|
|
947 |
MessageTracer unwrapMethod:(Array compiledMethodAt:#at:put:).
|
|
948 |
"
|
|
949 |
!
|
|
950 |
|
|
951 |
traceMethodSender:aMethod
|
|
952 |
"arrange for a trace message to be output on Stderr, when amethod is executed.
|
|
953 |
Only the sender is traced on entry.
|
|
954 |
Use untraceMethod to remove this trace."
|
|
955 |
|
|
956 |
^ self wrapMethod:aMethod
|
|
957 |
onEntry:[:con |
|
|
958 |
con receiver class name errorPrint.
|
|
959 |
'>>' errorPrint. con selector errorPrint.
|
|
960 |
' from ' errorPrint.
|
|
961 |
con sender errorPrintNL.
|
|
962 |
]
|
|
963 |
onExit:[:con :retVal | ].
|
|
964 |
!
|
|
965 |
|
|
966 |
untraceMethod:aMethod
|
|
967 |
"remove tracing of aMethod"
|
|
968 |
|
|
969 |
"just a rename for your convenience - the same basic mechanism is used for all of these
|
|
970 |
trace facilities ..."
|
|
971 |
|
|
972 |
^ self unwrapMethod:aMethod
|
|
973 |
! !
|
|
974 |
|
|
975 |
!MessageTracer class methodsFor:'class tracing'!
|
|
976 |
|
|
977 |
traceClass:aClass selector:aSelector
|
|
978 |
"arrange for a trace message to be output on Stderr, when a message with aSelector is
|
|
979 |
sent to instances of aClass (or subclass instances). Use untraceClass to remove this."
|
|
980 |
|
|
981 |
self wrapMethod:(aClass compiledMethodAt:aSelector)
|
|
982 |
onEntry:[:con |
|
|
983 |
'enter ' errorPrint. con receiver class name errorPrint.
|
|
984 |
'>>' errorPrint.
|
|
985 |
con selector errorPrint.
|
|
986 |
' receiver=' errorPrint. con receiver printString errorPrint.
|
|
987 |
' args=' errorPrint. (con args) printString errorPrintNL.
|
|
988 |
]
|
|
989 |
onExit:[:con :retVal |
|
|
990 |
'leave ' errorPrint. con receiver class name errorPrint.
|
|
991 |
'>>' errorPrint.
|
|
992 |
con selector errorPrint.
|
|
993 |
' receiver=' errorPrint. con receiver printString errorPrint.
|
|
994 |
' returning:' errorPrint. retVal printString errorPrintNL.
|
|
995 |
].
|
|
996 |
|
|
997 |
"
|
|
998 |
MessageTracer traceClass:Integer selector:#factorial.
|
|
999 |
5 factorial.
|
|
1000 |
MessageTracer untraceClass:Integer
|
|
1001 |
"
|
|
1002 |
"
|
|
1003 |
MessageTracer traceClass:SequenceableCollection selector:#quickSortFrom:to:.
|
|
1004 |
#(6 1 9 66 2 17) copy sort.
|
|
1005 |
MessageTracer untraceClass:SequenceableCollection
|
|
1006 |
"
|
|
1007 |
"
|
|
1008 |
MessageTracer traceClass:Array selector:#at:.
|
|
1009 |
MessageTracer traceClass:Array selector:#at:put:.
|
|
1010 |
#(6 1 9 66 2 17) copy sort.
|
|
1011 |
"
|
|
1012 |
!
|
|
1013 |
|
|
1014 |
untraceClass:aClass
|
|
1015 |
"remove all traces of messages sent to instances of aClass"
|
|
1016 |
|
|
1017 |
"just a rename for your convenience - the same basic mechanism is used for all of these
|
|
1018 |
trace facilities ..."
|
|
1019 |
|
|
1020 |
^ self untrapClass:aClass
|
|
1021 |
!
|
|
1022 |
|
|
1023 |
untraceAllClasses
|
|
1024 |
"remove all traces of messages sent to any class"
|
|
1025 |
|
|
1026 |
"just a rename for your convenience - the same basic mechanism is used for all of these
|
|
1027 |
trace facilities ..."
|
|
1028 |
|
|
1029 |
^ self untrapAllClasses
|
|
1030 |
! !
|