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