9 __doc__ =
"""py-module to hold a few tools and utilities to help debugging
10 configurables and/or Athena application.
16 """Class to hold the stage at which the user asked to hook the debugger
19 allowed_values = (
"conf",
"init",
"exec",
"fini" )
23 """debugging helper, hooks debugger to running interpreter process
24 gdb can be overridden via $ATLAS_DEBUGGER
27 debugger = os.environ.get(
'ATLAS_DEBUGGER',
'gdb')
29 pid = os.spawnvp(os.P_NOWAIT,
30 debugger, [debugger,
'-q',
'python',
str(os.getpid())])
37 os.waitpid( pid, os.WNOHANG )
42 """Same than for hookDebugger: spawn strace and attach to the running
46 if out
is None: out =
'athena.strace.log.%i' % os.getpid()
47 if isinstance(out, io.IOBase):
49 elif not isinstance(out,str):
50 raise TypeError(
'argument 0 needs to be either a file or a filename')
52 pid = os.spawnvp(os.P_NOWAIT,
55 '-p',
str(os.getpid()),
63 os.waitpid( pid, os.WNOHANG )
69 """On kernels with Yama enabled, ptrace may not work by default on processes
70 which are not decendants of the tracing process. Among other things, that
71 causes the way we attach the debugger to fail. However, we can disable this
72 on a per-process basis. Do that here.
74 See https://www.kernel.org/doc/Documentation/security/Yama.txt and prctl(2).
80 if not os.path.exists (
'/proc/sys/kernel/yama/ptrace_scope'):
return
83 with open(
'/proc/sys/kernel/yama/ptrace_scope')
as f:
84 if f.readline().strip() ==
'0':
88 from ctypes
import CDLL
89 libc = CDLL(
"libc.so.6")
92 libc.prctl (0x59616d61, 0xffffffffffffffff)
98 """Save python profile data of the default athena profiler instance
99 into filename (.txt or .pkl format).
101 from AthenaCommon.Logging
import log
105 profiler = cProfile._athena_python_profiler
107 if filename.endswith(
".txt"):
108 stats = pstats.Stats(profiler, stream=
open(filename,
'w'))
109 stats.strip_dirs().sort_stats(
"time").print_stats()
110 log.info(
"Python profile summary stored in %s", filename)
112 profiler.dump_stats(filename)
113 log.info(
"Python profile stored in %s", filename)
117 """Run script with the given trace level"""
125 ignore_dirs = [x
for x
in sys.path
if x.find(
"ROOT")!=-1]
126 ignore_dirs.append(sys.prefix)
128 ignore_mods += [
'_db',
'__init__',
'_configurables',
'GaudiHandles',
'six']
132 'Configurable',
'Configurables',
'PropertyProxy',
'DataHandle',
'ConfigurableDb',
133 'ConfigurableMeta',
'CfgMgr',
135 'ComponentAccumulator',
'Logging',
'AthConfigFlags',
'AllConfigFlags',
'Deduplication',
136 'semantics',
'AccumulatorCache',
'DebuggingContext',
'AtlasSemantics',
'ComponentFactory',
137 'LegacySupport',
'ItemListSemantics'
141 tracer = trace.Trace(ignoredirs=ignore_dirs, ignoremods=ignore_mods)
143 with io.open_code(script)
as f:
144 code = compile(f.read(), script,
'exec')