1 | # |
---|
2 | # RTEMS Objects Support |
---|
3 | # Copyright 2010 Chris Johns (chrisj@rtems.org) |
---|
4 | # |
---|
5 | # $Id$ |
---|
6 | # |
---|
7 | |
---|
8 | import gdb |
---|
9 | import itertools |
---|
10 | import re |
---|
11 | |
---|
12 | class infotables: |
---|
13 | """Manage the object information tables.""" |
---|
14 | |
---|
15 | tables_types = { |
---|
16 | 'internal/time' : ('TOD_Control', '_TOD'), |
---|
17 | 'internal/wdticks' : ('Chain_Control', '_Watchdog_Ticks_chain'), |
---|
18 | |
---|
19 | 'classic/tasks' : ('Thread_Control', '_RTEMS_tasks_Information'), |
---|
20 | 'classic/timers' : ('Timer_Control', '_Timer_Information'), |
---|
21 | 'classic/semaphores' : ('Semaphore_Control', '_Semaphore_Information'), |
---|
22 | 'classic/message_queues' : ('Message_queue_Control', '_Message_queue_Information'), |
---|
23 | 'classic/partitions' : ('Partition_Control', '_Partition_Information'), |
---|
24 | 'classic/regions' : ('Region_Control', '_Region_Information'), |
---|
25 | 'classic/ports' : ('Port_Control', '_Port_Information'), |
---|
26 | 'classic/periods' : ('Period_Control', '_Period_Information'), |
---|
27 | 'classic/extensions' : ('Extension_Control', '_Extension_Information'), |
---|
28 | 'classic/barriers' : ('Barrier_Control', '_Barrier_Information') |
---|
29 | } |
---|
30 | |
---|
31 | def __init__(self): |
---|
32 | self.invalidate() |
---|
33 | |
---|
34 | def invalidate(self): |
---|
35 | self.tables = {} |
---|
36 | |
---|
37 | def name(self, api, _class): |
---|
38 | return api + '/' + _class |
---|
39 | |
---|
40 | def load(self, n): |
---|
41 | if n in self.tables_types: |
---|
42 | if n not in self.tables: |
---|
43 | self.tables[n] = gdb.parse_and_eval(self.tables_types[n][1]) |
---|
44 | |
---|
45 | def get(self, api, _class): |
---|
46 | n = self.name(api, _class) |
---|
47 | self.load(n) |
---|
48 | if n in self.tables: |
---|
49 | return self.tables[n] |
---|
50 | return None |
---|
51 | |
---|
52 | def maximum(self, api, _class): |
---|
53 | n = self.name(api, _class) |
---|
54 | self.load(n) |
---|
55 | return int(self.tables[n]['maximum']) |
---|
56 | |
---|
57 | def object(self, id): |
---|
58 | if type(id) == gdb.Value: |
---|
59 | id = ident(id) |
---|
60 | if type(id) == tuple: |
---|
61 | api = id[0] |
---|
62 | _class = id[1] |
---|
63 | index = id[2] |
---|
64 | else: |
---|
65 | api = id.api() |
---|
66 | _class = id._class() |
---|
67 | index = id.index() |
---|
68 | return self.object_return(api, _class, index) |
---|
69 | |
---|
70 | def object_return(self, api, _class, index=-1): |
---|
71 | n = self.name(api, _class) |
---|
72 | self.load(n) |
---|
73 | |
---|
74 | table_type = self.tables_types[n] |
---|
75 | |
---|
76 | if api == 'internal': |
---|
77 | expr = '(' + table_type[0] + ')' + table_type[1] |
---|
78 | |
---|
79 | else: |
---|
80 | max = self.maximum(api, _class) |
---|
81 | if index > max: |
---|
82 | raise IndexError('object index out of range (%d)' % (max)) |
---|
83 | expr = '(' + table_type[0] + '*)' + \ |
---|
84 | table_type[1] + '.local_table[' + str(index) + ']' |
---|
85 | return gdb.parse_and_eval(expr) |
---|
86 | |
---|
87 | def is_string(self, api, _class): |
---|
88 | n = self.name(api, _class) |
---|
89 | self.load(n) |
---|
90 | if n in self.tables: |
---|
91 | if self.tables[n]['is_string']: |
---|
92 | return True |
---|
93 | return False |
---|
94 | |
---|
95 | # |
---|
96 | # Global info tables. These are global in the target. |
---|
97 | # |
---|
98 | information = infotables() |
---|
99 | |
---|
100 | class ident: |
---|
101 | "An RTEMS object id with support for its bit fields." |
---|
102 | |
---|
103 | bits = [ |
---|
104 | { 'index': (0, 15), |
---|
105 | 'node': (0, 0), |
---|
106 | 'api': (8, 10), |
---|
107 | 'class': (11, 15) }, |
---|
108 | { 'index': (0, 15), |
---|
109 | 'node': (16, 23), |
---|
110 | 'api': (24, 26), |
---|
111 | 'class': (27, 31) } |
---|
112 | ] |
---|
113 | |
---|
114 | OBJECT_16_BITS = 0 |
---|
115 | OBJECT_31_BITS = 1 |
---|
116 | |
---|
117 | api_labels = [ |
---|
118 | 'none', |
---|
119 | 'internal', |
---|
120 | 'classic', |
---|
121 | 'posix', |
---|
122 | 'itron' |
---|
123 | ] |
---|
124 | |
---|
125 | class_labels = { |
---|
126 | 'internal' : ('threads', |
---|
127 | 'mutexes'), |
---|
128 | 'classic' : ('none', |
---|
129 | 'tasks', |
---|
130 | 'timers', |
---|
131 | 'semaphores', |
---|
132 | 'message_queues', |
---|
133 | 'partitions', |
---|
134 | 'regions', |
---|
135 | 'ports', |
---|
136 | 'periods', |
---|
137 | 'extensions', |
---|
138 | 'barriers'), |
---|
139 | 'posix' : ('none', |
---|
140 | 'threads', |
---|
141 | 'keys', |
---|
142 | 'interrupts', |
---|
143 | 'message_queue_fds', |
---|
144 | 'message_queues', |
---|
145 | 'mutexes', |
---|
146 | 'semaphores', |
---|
147 | 'condition_variables', |
---|
148 | 'timers', |
---|
149 | 'barriers', |
---|
150 | 'spinlocks', |
---|
151 | 'rwlocks'), |
---|
152 | 'itron' : ('none', |
---|
153 | 'tasks', |
---|
154 | 'eventflags', |
---|
155 | 'mailboxes', |
---|
156 | 'message_buffers', |
---|
157 | 'ports', |
---|
158 | 'semaphores', |
---|
159 | 'variable_memory_pools', |
---|
160 | 'fixed_memory_pools') |
---|
161 | } |
---|
162 | |
---|
163 | def __init__(self, id): |
---|
164 | if type(id) != gdb.Value and type(id) != int and type(id) != unicode: |
---|
165 | raise TypeError('%s: must be gdb.Value, int, unicoded int' % (type(id))) |
---|
166 | if type(id) == int: |
---|
167 | id = gdb.Value(id) |
---|
168 | self.id = id |
---|
169 | if self.id.type.sizeof == 2: |
---|
170 | self.idSize = self.OBJECT_16_BITS |
---|
171 | else: |
---|
172 | self.idSize = self.OBJECT_31_BITS |
---|
173 | |
---|
174 | def get(self, field): |
---|
175 | if field in self.bits[self.idSize]: |
---|
176 | bits = self.bits[self.idSize][field] |
---|
177 | if bits[1] > 0: |
---|
178 | return (int(self.id) >> bits[0]) & ((1 << (bits[1] - bits[0] + 1)) - 1) |
---|
179 | return 0 |
---|
180 | |
---|
181 | def value(self): |
---|
182 | return int(self.id) |
---|
183 | |
---|
184 | def index(self): |
---|
185 | return self.get('index') |
---|
186 | |
---|
187 | def node(self): |
---|
188 | return self.get('node') |
---|
189 | |
---|
190 | def api_val(self): |
---|
191 | return self.get('api') |
---|
192 | |
---|
193 | def class_val(self): |
---|
194 | return self.get('class') |
---|
195 | |
---|
196 | def api(self): |
---|
197 | api = self.api_val() |
---|
198 | if api < len(self.api_labels): |
---|
199 | return self.api_labels[api] |
---|
200 | return 'none' |
---|
201 | |
---|
202 | def _class(self): |
---|
203 | api = self.api() |
---|
204 | if api == 'none': |
---|
205 | return 'invalid' |
---|
206 | _class = self.class_val() |
---|
207 | if _class < len(self.class_labels[api]): |
---|
208 | return self.class_labels[api][_class] |
---|
209 | return 'invalid' |
---|
210 | |
---|
211 | def valid(self): |
---|
212 | return self.api() != 'none' and self._class() != 'invalid' |
---|
213 | |
---|
214 | class name: |
---|
215 | """The Objects_Name can either be told what the name is or can take a |
---|
216 | guess.""" |
---|
217 | |
---|
218 | def __init__(self, name, is_string = None): |
---|
219 | self.name = name |
---|
220 | if is_string == None: |
---|
221 | self.is_string = 'auto' |
---|
222 | else: |
---|
223 | if is_string: |
---|
224 | self.is_string = 'yes' |
---|
225 | else: |
---|
226 | self.is_string = 'no' |
---|
227 | |
---|
228 | def __str__(self): |
---|
229 | if self.is_string != 'yes': |
---|
230 | u32 = int(self.name['name_u32']) |
---|
231 | s = chr((u32 >> 24) & 0xff) + \ |
---|
232 | chr((u32 >> 16) & 0xff) + chr((u32 >> 8) & 0xff) + \ |
---|
233 | chr(u32 & 0xff) |
---|
234 | for c in range(0,4): |
---|
235 | if s[c] < ' ' or s[c] > '~': |
---|
236 | s = None |
---|
237 | break |
---|
238 | if s: |
---|
239 | return s |
---|
240 | return str(self.name['name_p'].dereference()) |
---|
241 | |
---|
242 | class control: |
---|
243 | """The Objects_Control structure.""" |
---|
244 | |
---|
245 | def __init__(self, object): |
---|
246 | self.object = object |
---|
247 | self._id = ident(self.object['id']) |
---|
248 | |
---|
249 | def node(self): |
---|
250 | return self.object['Node'] |
---|
251 | |
---|
252 | def id(self): |
---|
253 | return self.object['id'] |
---|
254 | |
---|
255 | def name(self): |
---|
256 | is_string = information.is_string(self._id.api(), self._id._class()) |
---|
257 | val = str(name(self.object['name'],is_string)) |
---|
258 | |
---|
259 | # Normal comaprision is a bit tricky with quotes |
---|
260 | # 0 '\000' in hex == '3020275c30303027' |
---|
261 | if val.encode('hex') == '3020275c30303027': |
---|
262 | val = "" |
---|
263 | |
---|
264 | return val |
---|