author | Paul Beltyukov <beltyukov.p.a@gmail.com> |
Fri, 09 Sep 2016 11:47:00 +0500 | |
changeset 1527 | 642bae8e8607 |
parent 1433 | 4a45f6642523 |
child 1571 | 486f94a8032c |
permissions | -rw-r--r-- |
592 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
# |
|
4 |
#Copyright (C) 2011: Edouard TISSERANT and Laurent BESSARD |
|
5 |
# |
|
6 |
#See COPYING file for copyrights details. |
|
7 |
# |
|
8 |
#This library is free software; you can redistribute it and/or |
|
9 |
#modify it under the terms of the GNU General Public |
|
10 |
#License as published by the Free Software Foundation; either |
|
11 |
#version 2.1 of the License, or (at your option) any later version. |
|
12 |
# |
|
13 |
#This library is distributed in the hope that it will be useful, |
|
14 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
16 |
#General Public License for more details. |
|
17 |
# |
|
18 |
#You should have received a copy of the GNU General Public |
|
19 |
#License along with this library; if not, write to the Free Software |
|
20 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
21 |
||
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1075
diff
changeset
|
22 |
import ctypes |
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1075
diff
changeset
|
23 |
ctypes.pythonapi.PyString_AsString.argtypes = (ctypes.c_void_p,) |
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1075
diff
changeset
|
24 |
ctypes.pythonapi.PyString_AsString.restype = ctypes.POINTER(ctypes.c_char) |
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1075
diff
changeset
|
25 |
|
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1075
diff
changeset
|
26 |
|
592 | 27 |
from ctypes import * |
593 | 28 |
from datetime import timedelta as td |
592 | 29 |
|
30 |
class IEC_STRING(Structure): |
|
31 |
""" |
|
32 |
Must be changed according to changes in iec_types.h |
|
33 |
""" |
|
34 |
_fields_ = [("len", c_uint8), |
|
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1075
diff
changeset
|
35 |
("body", c_char * 126)] |
592 | 36 |
|
37 |
class IEC_TIME(Structure): |
|
38 |
""" |
|
39 |
Must be changed according to changes in iec_types.h |
|
40 |
""" |
|
41 |
_fields_ = [("s", c_long), #tv_sec |
|
42 |
("ns", c_long)] #tv_nsec |
|
43 |
||
44 |
def _t(t, u=lambda x:x.value, p=lambda t,x:t(x)): return (t, u, p) |
|
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1075
diff
changeset
|
45 |
def _ttime(): return (IEC_TIME, |
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1075
diff
changeset
|
46 |
lambda x:td(0, x.s, x.ns/1000), |
595
6348c0110e0f
Added support for handling ANY_DATE types in debugger type translation
Edouard Tisserant
parents:
594
diff
changeset
|
47 |
lambda t,x:t(x.days * 24 * 3600 + x.seconds, x.microseconds*1000)) |
592 | 48 |
|
49 |
SameEndianessTypeTranslator = { |
|
50 |
"BOOL" : _t(c_uint8, lambda x:x.value!=0), |
|
51 |
"STEP" : _t(c_uint8), |
|
52 |
"TRANSITION" : _t(c_uint8), |
|
53 |
"ACTION" : _t(c_uint8), |
|
54 |
"SINT" : _t(c_int8), |
|
55 |
"USINT" : _t(c_uint8), |
|
56 |
"BYTE" : _t(c_uint8), |
|
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1075
diff
changeset
|
57 |
"STRING" : (IEC_STRING, |
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1075
diff
changeset
|
58 |
lambda x:x.body[:x.len], |
592 | 59 |
lambda t,x:t(len(x),x)), |
60 |
"INT" : _t(c_int16), |
|
61 |
"UINT" : _t(c_uint16), |
|
62 |
"WORD" : _t(c_uint16), |
|
63 |
"DINT" : _t(c_int32), |
|
64 |
"UDINT" : _t(c_uint32), |
|
65 |
"DWORD" : _t(c_uint32), |
|
66 |
"LINT" : _t(c_int64), |
|
67 |
"ULINT" : _t(c_uint64), |
|
68 |
"LWORD" : _t(c_uint64), |
|
69 |
"REAL" : _t(c_float), |
|
70 |
"LREAL" : _t(c_double), |
|
595
6348c0110e0f
Added support for handling ANY_DATE types in debugger type translation
Edouard Tisserant
parents:
594
diff
changeset
|
71 |
"TIME" : _ttime(), |
6348c0110e0f
Added support for handling ANY_DATE types in debugger type translation
Edouard Tisserant
parents:
594
diff
changeset
|
72 |
"TOD" : _ttime(), |
6348c0110e0f
Added support for handling ANY_DATE types in debugger type translation
Edouard Tisserant
parents:
594
diff
changeset
|
73 |
"DATE" : _ttime(), |
6348c0110e0f
Added support for handling ANY_DATE types in debugger type translation
Edouard Tisserant
parents:
594
diff
changeset
|
74 |
"DT" : _ttime(), |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1075
diff
changeset
|
75 |
} |
592 | 76 |
|
77 |
SwapedEndianessTypeTranslator = { |
|
78 |
#TODO |
|
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1075
diff
changeset
|
79 |
} |
592 | 80 |
|
1075
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
81 |
TypeTranslator=SameEndianessTypeTranslator |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
82 |
|
592 | 83 |
# Construct debugger natively supported types |
84 |
DebugTypesSize = dict([(key,sizeof(t)) for key,(t,p,u) in SameEndianessTypeTranslator.iteritems() if t is not None]) |
|
85 |
||
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1075
diff
changeset
|
86 |
def UnpackDebugBuffer(buff, indexes): |
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1075
diff
changeset
|
87 |
res = [] |
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1075
diff
changeset
|
88 |
buffoffset = 0 |
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1075
diff
changeset
|
89 |
buffsize = len(buff) |
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1075
diff
changeset
|
90 |
buffptr = cast(ctypes.pythonapi.PyString_AsString(id(buff)),c_void_p).value |
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1075
diff
changeset
|
91 |
for iectype in indexes: |
1075
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
92 |
c_type,unpack_func, pack_func = \ |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
93 |
TypeTranslator.get(iectype, |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
94 |
(None,None,None)) |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1075
diff
changeset
|
95 |
if c_type is not None and buffoffset < buffsize: |
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1075
diff
changeset
|
96 |
cursor = c_void_p( buffptr + buffoffset) |
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1075
diff
changeset
|
97 |
value = unpack_func( cast(cursor, |
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1075
diff
changeset
|
98 |
POINTER(c_type)).contents) |
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1075
diff
changeset
|
99 |
buffoffset += sizeof(c_type) if iectype != "STRING" else len(value)+1 |
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1075
diff
changeset
|
100 |
res.append(value) |
1075
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
101 |
else: |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
102 |
break |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1075
diff
changeset
|
103 |
if buffoffset and buffoffset == buffsize: |
1075
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
104 |
return res |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
105 |
return None |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
106 |
|
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
107 |
|
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
108 |
|
917 | 109 |
LogLevels = ["CRITICAL","WARNING","INFO","DEBUG"] |
110 |
LogLevelsCount = len(LogLevels) |
|
111 |
LogLevelsDict = dict(zip(LogLevels,range(LogLevelsCount))) |
|
112 |
LogLevelsDefault = LogLevelsDict["DEBUG"] |
|
972 | 113 |