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