3 # Copyright The SCons Foundation
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 """Code for debugging SCons internal things.
26 Shouldn't be needed by most users. Quick shortcuts::
28 from SCons.Debug import caller_trace
39 # Global variable that gets set to 'True' by the Main script,
40 # when the creation of class instances should get tracked.
41 track_instances
= False
42 # List of currently tracked classes
44 # Global variable that gets set to 'True' by the Main script
45 # when SConscript call tracing should be enabled.
46 sconscript_trace
= False
48 def logInstanceCreation(instance
, name
=None) -> None:
50 name
= instance
.__class
__.__name
__
51 if name
not in tracked_classes
:
52 tracked_classes
[name
] = []
53 if hasattr(instance
, '__dict__'):
54 tracked_classes
[name
].append(weakref
.ref(instance
))
56 # weakref doesn't seem to work when the instance
57 # contains only slots...
58 tracked_classes
[name
].append(instance
)
60 def string_to_classes(s
):
62 return sorted(tracked_classes
.keys())
66 def fetchLoggedInstances(classes
: str="*"):
67 classnames
= string_to_classes(classes
)
68 return [(cn
, len(tracked_classes
[cn
])) for cn
in classnames
]
70 def countLoggedInstances(classes
, file=sys
.stdout
) -> None:
71 for classname
in string_to_classes(classes
):
72 file.write("%s: %d\n" % (classname
, len(tracked_classes
[classname
])))
74 def listLoggedInstances(classes
, file=sys
.stdout
) -> None:
75 for classname
in string_to_classes(classes
):
76 file.write('\n%s:\n' % classname
)
77 for ref
in tracked_classes
[classname
]:
78 if inspect
.isclass(ref
):
83 file.write(' %s\n' % repr(obj
))
85 def dumpLoggedInstances(classes
, file=sys
.stdout
) -> None:
86 for classname
in string_to_classes(classes
):
87 file.write('\n%s:\n' % classname
)
88 for ref
in tracked_classes
[classname
]:
91 file.write(' %s:\n' % obj
)
92 for key
, value
in obj
.__dict
__.items():
93 file.write(' %20s : %s\n' % (key
, value
))
96 if sys
.platform
[:5] == "linux":
97 # Linux doesn't actually support memory usage stats from getrusage().
99 with
open('/proc/self/stat') as f
:
101 mstr
= mstr
.split()[22]
103 elif sys
.platform
[:6] == 'darwin':
104 #TODO really get memory stats for OS X
107 elif sys
.platform
== 'win32':
108 from SCons
.compat
.win32
import get_peak_memory_usage
109 memory
= get_peak_memory_usage
118 res
= resource
.getrusage(resource
.RUSAGE_SELF
)
123 """return caller's stack"""
125 tb
= traceback
.extract_stack()
126 # strip itself and the caller from the output
130 # (filename, line number, function name, text)
132 result
.append('%s:%d(%s)' % func_shorten(key
))
138 def caller_trace(back
: int=0) -> None:
140 Trace caller stack and save info into global dicts, which
141 are printed automatically at the end of SCons execution.
143 global caller_bases
, caller_dicts
145 tb
= traceback
.extract_stack(limit
=3+back
)
148 caller_bases
[callee
] = caller_bases
.get(callee
, 0) + 1
149 for caller
in tb
[2:]:
150 caller
= callee
+ caller
[:3]
152 entry
= caller_dicts
[callee
]
154 caller_dicts
[callee
] = entry
= {}
155 entry
[caller
] = entry
.get(caller
, 0) + 1
158 # print a single caller and its callers, if any
159 def _dump_one_caller(key
, file, level
: int=0) -> None:
161 for v
,c
in sorted([(-v
,c
) for c
,v
in caller_dicts
[key
].items()]):
162 file.write("%s %6d %s:%d(%s)\n" % ((leader
,-v
) + func_shorten(c
[-3:])))
163 if c
in caller_dicts
:
164 _dump_one_caller(c
, file, level
+1)
166 # print each call tree
167 def dump_caller_counts(file=sys
.stdout
) -> None:
168 for k
in sorted(caller_bases
.keys()):
169 file.write("Callers of %s:%d(%s), %d calls:\n"
170 % (func_shorten(k
) + (caller_bases
[k
],)))
171 _dump_one_caller(k
, file)
174 ( '/scons/SCons/', 1),
175 ( '/src/engine/SCons/', 1),
176 ( '/usr/lib/python', 0),
180 shorten_list
= [(t
[0].replace('/', os
.sep
), t
[1]) for t
in shorten_list
]
182 def func_shorten(func_tuple
):
184 for t
in shorten_list
:
189 return (f
[i
:],)+func_tuple
[1:]
194 if sys
.platform
== 'win32':
197 TraceDefault
= '/dev/tty'
198 TimeStampDefault
= False
199 StartTime
= time
.perf_counter()
200 PreviousTime
= StartTime
202 def Trace(msg
, tracefile
=None, mode
: str='w', tstamp
: bool=False) -> None:
203 """Write a trace message.
205 Write messages when debugging which do not interfere with stdout.
206 Useful in tests, which monitor stdout and would break with
207 unexpected output. Trace messages can go to the console (which is
208 opened as a file), or to a disk file; the tracefile argument persists
209 across calls unless overridden.
212 tracefile: file to write trace message to. If omitted,
213 write to the previous trace file (default: console).
214 mode: file open mode (default: 'w')
215 tstamp: write relative timestamps with trace. Outputs time since
216 scons was started, and time since last trace (default: False)
220 global TimeStampDefault
223 def trace_cleanup(traceFP
) -> None:
226 if tracefile
is None:
227 tracefile
= TraceDefault
229 TraceDefault
= tracefile
231 tstamp
= TimeStampDefault
233 TimeStampDefault
= tstamp
235 fp
= TraceFP
[tracefile
]
238 fp
= TraceFP
[tracefile
] = open(tracefile
, mode
)
239 atexit
.register(trace_cleanup
, fp
)
241 # Assume we were passed an open file pointer.
244 now
= time
.perf_counter()
245 fp
.write('%8.4f %8.4f: ' % (now
- StartTime
, now
- PreviousTime
))
252 # indent-tabs-mode:nil
254 # vim: set expandtab tabstop=4 shiftwidth=4: