Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 79c2706a962d45587baa890248fe6ec9ed130ec9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# *******************************************************************************
# * Copyright (c) 2011 Wind River Systems, Inc. and others.
# * All rights reserved. This program and the accompanying materials
# * are made available under the terms of the Eclipse Public License v1.0
# * which accompanies this distribution, and is available at
# * http:#www.eclipse.org/legal/epl-v10.html
# *
# * Contributors:
# *     Wind River Systems - initial API and implementation
# *******************************************************************************

import cStringIO, time, types

# Error report attribute names
ERROR_CODE = "Code"           # integer
ERROR_TIME = "Time"           # integer
ERROR_SERVICE = "Service"     # string
ERROR_FORMAT = "Format"       # string
ERROR_PARAMS = "Params"       # array
ERROR_SEVERITY = "Severity"   # integer
ERROR_ALT_CODE = "AltCode"    # integer
ERROR_ALT_ORG = "AltOrg"      # string
ERROR_CAUSED_BY = "CausedBy"  # object

# Error severity codes
SEVERITY_ERROR = 0
SEVERITY_WARNING = 1
SEVERITY_FATAL = 2

# Error code ranges
# Standard TCF code range */
CODE_STD_MIN = 0
CODE_STD_MAX = 0xffff

# Service specific codes. Decoding requires service ID. */
CODE_SERVICE_SPECIFIC_MIN = 0x10000
CODE_SERVICE_SPECIFIC_MAX = 0x1ffff

# Reserved codes - will never be used by the TCF standard */
CODE_RESERVED_MIN = 0x20000
CODE_RESERVED_MAX = 0x2ffff

# Standard TCF error codes
TCF_ERROR_OTHER               = 1
TCF_ERROR_JSON_SYNTAX         = 2
TCF_ERROR_PROTOCOL            = 3
TCF_ERROR_BUFFER_OVERFLOW     = 4
TCF_ERROR_CHANNEL_CLOSED      = 5
TCF_ERROR_COMMAND_CANCELLED   = 6
TCF_ERROR_UNKNOWN_PEER        = 7
TCF_ERROR_BASE64              = 8
TCF_ERROR_EOF                 = 9
TCF_ERROR_ALREADY_STOPPED     = 10
TCF_ERROR_ALREADY_EXITED      = 11
TCF_ERROR_ALREADY_RUNNING     = 12
TCF_ERROR_ALREADY_ATTACHED    = 13
TCF_ERROR_IS_RUNNING          = 14
TCF_ERROR_INV_DATA_SIZE       = 15
TCF_ERROR_INV_CONTEXT         = 16
TCF_ERROR_INV_ADDRESS         = 17
TCF_ERROR_INV_EXPRESSION      = 18
TCF_ERROR_INV_FORMAT          = 19
TCF_ERROR_INV_NUMBER          = 20
TCF_ERROR_INV_DWARF           = 21
TCF_ERROR_SYM_NOT_FOUND       = 22
TCF_ERROR_UNSUPPORTED         = 23
TCF_ERROR_INV_DATA_TYPE       = 24
TCF_ERROR_INV_COMMAND         = 25
TCF_ERROR_INV_TRANSPORT       = 26
TCF_ERROR_CACHE_MISS          = 27
TCF_ERROR_NOT_ACTIVE          = 28

_timestamp_format = "%Y-%m-%d %H:%M:%S"

class ErrorReport(Exception):
    def __init__(self, msg, attrs):
        super(ErrorReport, self).__init__(msg)
        if type(attrs) is types.IntType:
            attrs = {
                ERROR_CODE : attrs,
                ERROR_TIME : int(time.time() * 1000),
                ERROR_FORMAT : msg,
                ERROR_SEVERITY : SEVERITY_ERROR
            }
        self.attrs = attrs
        caused_by = attrs.get(ERROR_CAUSED_BY)
        if caused_by:
            map = caused_by
            bf = cStringIO.StringIO()
            bf.write("TCF error report:")
            bf.write('\n')
            appendErrorProps(bf, map)
            self.caused_by = ErrorReport(bf.getvalue(), map)

    def getErrorCode(self):
        return self.attrs.get(ERROR_CODE) or 0

    def getAltCode(self):
        return self.attrs.get(ERROR_ALT_CODE) or 0

    def getAltOrg(self):
        return self.attrs.get(ERROR_ALT_ORG)

    def getAttributes(self):
        return self.attrs


def toErrorString(data):
    if not data: return None
    map = data
    fmt = map.get(ERROR_FORMAT)
    if fmt:
        c = map.get(ERROR_PARAMS)
        if c: return fmt.format(c)
        return fmt
    code = map.get(ERROR_CODE)
    if code is not None:
        if code == TCF_ERROR_OTHER:
            alt_org = map.get(ERROR_ALT_ORG)
            alt_code = map.get(ERROR_ALT_CODE)
            if alt_org and alt_code:
                return "%s Error %d" % (alt_org, alt_code)
        return "TCF Error %d" % code
    return "Invalid error report format"

def appendErrorProps(bf, map):
    timeVal = map.get(ERROR_TIME)
    code = map.get(ERROR_CODE)
    service = map.get(ERROR_SERVICE)
    severity = map.get(ERROR_SEVERITY)
    alt_code = map.get(ERROR_ALT_CODE)
    alt_org = map.get(ERROR_ALT_ORG)
    if timeVal:
        bf.write('\n')
        bf.write("Time: ")
        bf.write(time.strftime(_timestamp_format, time.localtime(timeVal/1000.)))
    if severity:
        bf.write('\n')
        bf.write("Severity: ")
        if severity == SEVERITY_ERROR: bf.write("Error")
        elif severity == SEVERITY_FATAL: bf.write("Fatal")
        elif severity == SEVERITY_WARNING: bf.write("Warning")
        else: bf.write("Unknown")
    bf.write('\n')
    bf.write("Error text: ")
    bf.write(toErrorString(map))
    bf.write('\n')
    bf.write("Error code: ")
    bf.write(str(code))
    if service:
        bf.write('\n')
        bf.write("Service: ")
        bf.write(service)
    if alt_code:
        bf.write('\n')
        bf.write("Alt code: ")
        bf.write(str(alt_code))
        if alt_org:
            bf.write('\n')
            bf.write("Alt org: ")
            bf.write(alt_org)

Back to the top