Statistics
| Branch: | Tag: | Revision:

root / snf-common / synnefo / lib / trace.py @ d509e6da

History | View | Annotate | Download (2.6 kB)

1
# Copyright 2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34

    
35
def set_signal_trap():
36
    from os import getpid
37
    from traceback import format_stack, print_exc
38
    from signal import signal, SIGTRAP
39
    from sys import stderr
40
    import gc
41

    
42
    def greenlet_trace(arg):
43
        i = 0
44
        stderr.write("--- Greenlet trace: %s\n" % arg)
45
        for ob in gc.get_objects():
46
            if not isinstance(ob, greenlet):
47
                continue
48
            if not ob:
49
                continue
50
            i = i + 1
51
            stderr.write(("--- > Greenlet %d:\n" % i +
52
                          "".join(format_stack(ob.gr_frame)) + "\n\n"))
53
        stderr.write("--- End of trace: %s\n" % arg)
54

    
55
    try:
56
        from greenlet import greenlet
57
    except ImportError:
58

    
59
        def greenlet_trace(arg):
60
            return
61

    
62
    def handle_trap(*args):
63
        try:
64
            import trap_inject
65
            reload(trap_inject)
66
            trap_inject.inject()
67
        except ImportError:
68
            pass
69
        except:
70
            print_exc()
71

    
72
        msg = ('=== pid: %s' % getpid()) + '\n'.join(format_stack()) + '\n'
73
        stderr.write(msg)
74
        greenlet_trace('TRAP')
75

    
76
    signal(SIGTRAP, handle_trap)