1 | # |
---|
2 | # RTEMS Pretty Printers |
---|
3 | # Copyright 2010 Chris Johns (chrisj@rtems.org) |
---|
4 | # |
---|
5 | # $Id$ |
---|
6 | # |
---|
7 | |
---|
8 | import gdb |
---|
9 | import re |
---|
10 | |
---|
11 | import objects |
---|
12 | import threads |
---|
13 | import classic |
---|
14 | |
---|
15 | nesting = 0 |
---|
16 | |
---|
17 | def type_from_value(val): |
---|
18 | type = val.type; |
---|
19 | # If it points to a reference, get the reference. |
---|
20 | if type.code == gdb.TYPE_CODE_REF: |
---|
21 | type = type.target () |
---|
22 | # Get the unqualified type |
---|
23 | return type.unqualified () |
---|
24 | |
---|
25 | def register_rtems_printers (obj): |
---|
26 | "Register RTEMS pretty-printers with objfile Obj." |
---|
27 | |
---|
28 | if obj == None: |
---|
29 | obj = gdb |
---|
30 | |
---|
31 | obj.pretty_printers.append (lookup_function) |
---|
32 | |
---|
33 | def lookup_function (val): |
---|
34 | "Look-up and return a pretty-printer that can print val." |
---|
35 | |
---|
36 | global nesting |
---|
37 | |
---|
38 | typename = str(type_from_value(val)) |
---|
39 | |
---|
40 | for function in pp_dict: |
---|
41 | if function.search (typename): |
---|
42 | nesting += 1 |
---|
43 | result = pp_dict[function] (val) |
---|
44 | nesting -= 1 |
---|
45 | if nesting == 0: |
---|
46 | objects.information.invalidate() |
---|
47 | return result |
---|
48 | |
---|
49 | # Cannot find a pretty printer. Return None. |
---|
50 | return None |
---|
51 | |
---|
52 | def build_rtems_dict(): |
---|
53 | pp_dict[re.compile('^rtems_id$')] = lambda val: objects.id_printer(val) |
---|
54 | pp_dict[re.compile('^Objects_Id$')] = lambda val: objects.id_printer(val) |
---|
55 | pp_dict[re.compile('^Objects_Name$')] = lambda val: objects.name_printer(val) |
---|
56 | pp_dict[re.compile('^Objects_Control$')] = lambda val: objects.control_printer(val) |
---|
57 | pp_dict[re.compile('^States_Control$')] = lambda val: threads.state_printer(val) |
---|
58 | pp_dict[re.compile('^rtems_attribute$')] = lambda val: classic.attribute_printer(val) |
---|
59 | pp_dict[re.compile('^Semaphore_Control$')] = lambda val: classic.semaphore_printer(val) |
---|
60 | |
---|
61 | class rtems(gdb.Command): |
---|
62 | """Prefix command for RTEMS.""" |
---|
63 | |
---|
64 | def __init__(self): |
---|
65 | super(rtems, self).__init__('rtems', |
---|
66 | gdb.COMMAND_STATUS, |
---|
67 | gdb.COMPLETE_NONE, |
---|
68 | True) |
---|
69 | |
---|
70 | class rtems_object(gdb.Command): |
---|
71 | """Object sub-command for RTEMS""" |
---|
72 | |
---|
73 | objects = { |
---|
74 | 'classic/semaphores': lambda id: classic.semaphore(id), |
---|
75 | 'classic/tasks': lambda id: classic.task(id), |
---|
76 | 'classic/message_queues': lambda id: classic.message_queue(id) |
---|
77 | } |
---|
78 | |
---|
79 | def __init__(self): |
---|
80 | self.__doc__ = 'Display the RTEMS object given a numeric ID.' |
---|
81 | super(rtems_object, self).__init__('rtems object', |
---|
82 | gdb.COMMAND_STATUS) |
---|
83 | |
---|
84 | def invoke(self, arg, from_tty): |
---|
85 | for num in arg.split(): |
---|
86 | try: |
---|
87 | val = gdb.parse_and_eval(num) |
---|
88 | num = int(val) |
---|
89 | except: |
---|
90 | print 'error: "%s" is not a number' % (num) |
---|
91 | return |
---|
92 | id = objects.ident(num) |
---|
93 | if not id.valid(): |
---|
94 | print 'Invalid object id' |
---|
95 | print 'API:%s Class:%s Node:%d Index:%d Id:%08X' % \ |
---|
96 | (id.api(), id._class(), id.node(), id.index(), id.value()) |
---|
97 | objectname = id.api() + '/' + id._class() |
---|
98 | if objectname in self.objects: |
---|
99 | object = self.objects[objectname](id) |
---|
100 | object.show(from_tty) |
---|
101 | objects.information.invalidate() |
---|
102 | |
---|
103 | # |
---|
104 | # Main |
---|
105 | # |
---|
106 | pp_dict = {} |
---|
107 | build_rtems_dict() |
---|
108 | gdb.pretty_printers = [] |
---|
109 | gdb.pretty_printers.append (lookup_function) |
---|
110 | rtems() |
---|
111 | rtems_object() |
---|