3 Code for debugging SCons internal things. Not everything here is
4 guaranteed to work all the way back to Python 1.5.2, and shouldn't be
12 # Permission is hereby granted, free of charge, to any person obtaining
13 # a copy of this software and associated documentation files (the
14 # "Software"), to deal in the Software without restriction, including
15 # without limitation the rights to use, copy, modify, merge, publish,
16 # distribute, sublicense, and/or sell copies of the Software, and to
17 # permit persons to whom the Software is furnished to do so, subject to
18 # the following conditions:
20 # The above copyright notice and this permission notice shall be included
21 # in all copies or substantial portions of the Software.
23 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
24 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
25 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
38 # Recipe 14.10 from the Python Cookbook.
42 def logInstanceCreation(instance, name=None):
45 def logInstanceCreation(instance, name=None):
47 name = instance.__class__.__name__
48 if name not in tracked_classes:
49 tracked_classes[name] = []
50 tracked_classes[name].append(weakref.ref(instance))
56 def string_to_classes(s):
58 return sorted(tracked_classes.keys())
62 def fetchLoggedInstances(classes="*"):
63 classnames = string_to_classes(classes)
64 return [(cn, len(tracked_classes[cn])) for cn in classnames]
66 def countLoggedInstances(classes, file=sys.stdout):
67 for classname in string_to_classes(classes):
68 file.write("%s: %d\n" % (classname, len(tracked_classes[classname])))
70 def listLoggedInstances(classes, file=sys.stdout):
71 for classname in string_to_classes(classes):
72 file.write('\n%s:\n' % classname)
73 for ref in tracked_classes[classname]:
76 file.write(' %s\n' % repr(obj))
78 def dumpLoggedInstances(classes, file=sys.stdout):
79 for classname in string_to_classes(classes):
80 file.write('\n%s:\n' % classname)
81 for ref in tracked_classes[classname]:
84 file.write(' %s:\n' % obj)
85 for key, value in obj.__dict__.items():
86 file.write(' %20s : %s\n' % (key, value))
90 if sys.platform[:5] == "linux":
91 # Linux doesn't actually support memory usage stats from getrusage().
93 mstr = open('/proc/self/stat').read()
94 mstr = mstr.split()[22]
108 process_handle = win32api.GetCurrentProcess()
109 memory_info = win32process.GetProcessMemoryInfo( process_handle )
110 return memory_info['PeakWorkingSetSize']
113 res = resource.getrusage(resource.RUSAGE_SELF)
116 # returns caller's stack
117 def caller_stack(*backlist):
122 for back in backlist:
123 tb = traceback.extract_stack(limit=3+back)
125 result.append('%s:%d(%s)' % func_shorten(key))
131 # trace a caller's stack
132 def caller_trace(back=0):
134 tb = traceback.extract_stack(limit=3+back)
137 caller_bases[callee] = caller_bases.get(callee, 0) + 1
138 for caller in tb[2:]:
139 caller = callee + caller[:3]
141 entry = caller_dicts[callee]
143 caller_dicts[callee] = entry = {}
144 entry[caller] = entry.get(caller, 0) + 1
147 # print a single caller and its callers, if any
148 def _dump_one_caller(key, file, level=0):
150 for v,c in sorted([(-v,c) for c,v in caller_dicts[key].items()]):
151 file.write("%s %6d %s:%d(%s)\n" % ((leader,-v) + func_shorten(c[-3:])))
152 if c in caller_dicts:
153 _dump_one_caller(c, file, level+1)
155 # print each call tree
156 def dump_caller_counts(file=sys.stdout):
157 for k in sorted(caller_bases.keys()):
158 file.write("Callers of %s:%d(%s), %d calls:\n"
159 % (func_shorten(k) + (caller_bases[k],)))
160 _dump_one_caller(k, file)
163 ( '/scons/SCons/', 1),
164 ( '/src/engine/SCons/', 1),
165 ( '/usr/lib/python', 0),
169 shorten_list = [(t[0].replace('/', os.sep), t[1]) for t in shorten_list]
171 def func_shorten(func_tuple):
173 for t in shorten_list:
178 return (f[i:],)+func_tuple[1:]
183 if sys.platform == 'win32':
186 TraceDefault = '/dev/tty'
188 TimeStampDefault = None
189 StartTime = time.time()
190 PreviousTime = StartTime
192 def Trace(msg, file=None, mode='w', tstamp=None):
193 """Write a trace message to a file. Whenever a file is specified,
194 it becomes the default for the next call to Trace()."""
196 global TimeStampDefault
203 tstamp = TimeStampDefault
205 TimeStampDefault = tstamp
210 fp = TraceFP[file] = open(file, mode)
212 # Assume we were passed an open file pointer.
216 fp.write('%8.4f %8.4f: ' % (now - StartTime, now - PreviousTime))
223 # indent-tabs-mode:nil
225 # vim: set expandtab tabstop=4 shiftwidth=4: