~1chb1n/charms/trusty/nova-compute/next.normalize-makefile-test-deps

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/python/debug.py

  • Committer: james.page at ubuntu
  • Date: 2015-03-31 15:01:48 UTC
  • mfrom: (116 nova-compute)
  • mto: This revision was merged to the branch mainline in revision 117.
  • Revision ID: james.page@ubuntu.com-20150331150148-fn3iawy9bqboe0d3
Rebase, resync

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# coding: utf-8
 
3
 
 
4
# Copyright 2014-2015 Canonical Limited.
 
5
#
 
6
# This file is part of charm-helpers.
 
7
#
 
8
# charm-helpers is free software: you can redistribute it and/or modify
 
9
# it under the terms of the GNU Lesser General Public License version 3 as
 
10
# published by the Free Software Foundation.
 
11
#
 
12
# charm-helpers is distributed in the hope that it will be useful,
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
# GNU Lesser General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU Lesser General Public License
 
18
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
 
19
 
 
20
from __future__ import print_function
 
21
 
 
22
__author__ = "Jorge Niedbalski <jorge.niedbalski@canonical.com>"
 
23
 
 
24
import atexit
 
25
import sys
 
26
 
 
27
from charmhelpers.contrib.python.rpdb import Rpdb
 
28
from charmhelpers.core.hookenv import (
 
29
    open_port,
 
30
    close_port,
 
31
    ERROR,
 
32
    log
 
33
)
 
34
 
 
35
DEFAULT_ADDR = "0.0.0.0"
 
36
DEFAULT_PORT = 4444
 
37
 
 
38
 
 
39
def _error(message):
 
40
    log(message, level=ERROR)
 
41
 
 
42
 
 
43
def set_trace(addr=DEFAULT_ADDR, port=DEFAULT_PORT):
 
44
    """
 
45
    Set a trace point using the remote debugger
 
46
    """
 
47
    atexit.register(close_port, port)
 
48
    try:
 
49
        log("Starting a remote python debugger session on %s:%s" % (addr,
 
50
                                                                    port))
 
51
        open_port(port)
 
52
        debugger = Rpdb(addr=addr, port=port)
 
53
        debugger.set_trace(sys._getframe().f_back)
 
54
    except:
 
55
        _error("Cannot start a remote debug session on %s:%s" % (addr,
 
56
                                                                 port))