root / snf-common / synnefo / lib / trace.py @ d509e6da
History | View | Annotate | Download (2.6 kB)
1 | d509e6da | Stratos Psomadakis | # Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | d509e6da | Stratos Psomadakis | #
|
3 | d509e6da | Stratos Psomadakis | # Redistribution and use in source and binary forms, with or
|
4 | d509e6da | Stratos Psomadakis | # without modification, are permitted provided that the following
|
5 | d509e6da | Stratos Psomadakis | # conditions are met:
|
6 | d509e6da | Stratos Psomadakis | #
|
7 | d509e6da | Stratos Psomadakis | # 1. Redistributions of source code must retain the above
|
8 | d509e6da | Stratos Psomadakis | # copyright notice, this list of conditions and the following
|
9 | d509e6da | Stratos Psomadakis | # disclaimer.
|
10 | d509e6da | Stratos Psomadakis | #
|
11 | d509e6da | Stratos Psomadakis | # 2. Redistributions in binary form must reproduce the above
|
12 | d509e6da | Stratos Psomadakis | # copyright notice, this list of conditions and the following
|
13 | d509e6da | Stratos Psomadakis | # disclaimer in the documentation and/or other materials
|
14 | d509e6da | Stratos Psomadakis | # provided with the distribution.
|
15 | d509e6da | Stratos Psomadakis | #
|
16 | d509e6da | Stratos Psomadakis | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | d509e6da | Stratos Psomadakis | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | d509e6da | Stratos Psomadakis | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | d509e6da | Stratos Psomadakis | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | d509e6da | Stratos Psomadakis | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | d509e6da | Stratos Psomadakis | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | d509e6da | Stratos Psomadakis | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | d509e6da | Stratos Psomadakis | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | d509e6da | Stratos Psomadakis | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | d509e6da | Stratos Psomadakis | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | d509e6da | Stratos Psomadakis | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | d509e6da | Stratos Psomadakis | # POSSIBILITY OF SUCH DAMAGE.
|
28 | d509e6da | Stratos Psomadakis | #
|
29 | d509e6da | Stratos Psomadakis | # The views and conclusions contained in the software and
|
30 | d509e6da | Stratos Psomadakis | # documentation are those of the authors and should not be
|
31 | d509e6da | Stratos Psomadakis | # interpreted as representing official policies, either expressed
|
32 | d509e6da | Stratos Psomadakis | # or implied, of GRNET S.A.
|
33 | d509e6da | Stratos Psomadakis | |
34 | d509e6da | Stratos Psomadakis | |
35 | d509e6da | Stratos Psomadakis | def set_signal_trap(): |
36 | d509e6da | Stratos Psomadakis | from os import getpid |
37 | d509e6da | Stratos Psomadakis | from traceback import format_stack, print_exc |
38 | d509e6da | Stratos Psomadakis | from signal import signal, SIGTRAP |
39 | d509e6da | Stratos Psomadakis | from sys import stderr |
40 | d509e6da | Stratos Psomadakis | import gc |
41 | d509e6da | Stratos Psomadakis | |
42 | d509e6da | Stratos Psomadakis | def greenlet_trace(arg): |
43 | d509e6da | Stratos Psomadakis | i = 0
|
44 | d509e6da | Stratos Psomadakis | stderr.write("--- Greenlet trace: %s\n" % arg)
|
45 | d509e6da | Stratos Psomadakis | for ob in gc.get_objects(): |
46 | d509e6da | Stratos Psomadakis | if not isinstance(ob, greenlet): |
47 | d509e6da | Stratos Psomadakis | continue
|
48 | d509e6da | Stratos Psomadakis | if not ob: |
49 | d509e6da | Stratos Psomadakis | continue
|
50 | d509e6da | Stratos Psomadakis | i = i + 1
|
51 | d509e6da | Stratos Psomadakis | stderr.write(("--- > Greenlet %d:\n" % i +
|
52 | d509e6da | Stratos Psomadakis | "".join(format_stack(ob.gr_frame)) + "\n\n")) |
53 | d509e6da | Stratos Psomadakis | stderr.write("--- End of trace: %s\n" % arg)
|
54 | d509e6da | Stratos Psomadakis | |
55 | d509e6da | Stratos Psomadakis | try:
|
56 | d509e6da | Stratos Psomadakis | from greenlet import greenlet |
57 | d509e6da | Stratos Psomadakis | except ImportError: |
58 | d509e6da | Stratos Psomadakis | |
59 | d509e6da | Stratos Psomadakis | def greenlet_trace(arg): |
60 | d509e6da | Stratos Psomadakis | return
|
61 | d509e6da | Stratos Psomadakis | |
62 | d509e6da | Stratos Psomadakis | def handle_trap(*args): |
63 | d509e6da | Stratos Psomadakis | try:
|
64 | d509e6da | Stratos Psomadakis | import trap_inject |
65 | d509e6da | Stratos Psomadakis | reload(trap_inject)
|
66 | d509e6da | Stratos Psomadakis | trap_inject.inject() |
67 | d509e6da | Stratos Psomadakis | except ImportError: |
68 | d509e6da | Stratos Psomadakis | pass
|
69 | d509e6da | Stratos Psomadakis | except:
|
70 | d509e6da | Stratos Psomadakis | print_exc() |
71 | d509e6da | Stratos Psomadakis | |
72 | d509e6da | Stratos Psomadakis | msg = ('=== pid: %s' % getpid()) + '\n'.join(format_stack()) + '\n' |
73 | d509e6da | Stratos Psomadakis | stderr.write(msg) |
74 | d509e6da | Stratos Psomadakis | greenlet_trace('TRAP')
|
75 | d509e6da | Stratos Psomadakis | |
76 | d509e6da | Stratos Psomadakis | signal(SIGTRAP, handle_trap) |