~canonical-ci-engineering/charms/trusty/core-image-publisher/trunk

« back to all changes in this revision

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

  • Committer: Celso Providelo
  • Date: 2015-03-25 04:13:43 UTC
  • Revision ID: celso.providelo@canonical.com-20150325041343-jw05jaz6jscs3c8f
fork of core-image-watcher

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
import atexit
 
23
import sys
 
24
 
 
25
from charmhelpers.contrib.python.rpdb import Rpdb
 
26
from charmhelpers.core.hookenv import (
 
27
    open_port,
 
28
    close_port,
 
29
    ERROR,
 
30
    log
 
31
)
 
32
 
 
33
__author__ = "Jorge Niedbalski <jorge.niedbalski@canonical.com>"
 
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))