source: rtems-tools/tools/gdb/python/rtems.py @ b743d63

4.104.115
Last change on this file since b743d63 was b743d63, checked in by Dhananjay Balan <mb.dhananjay@…>, on 08/09/13 at 12:16:37

Catch nonvalid indexes.

Catch IndexErrors? generated while referancing non existant indexes.

  • Property mode set to 100644
File size: 5.7 KB
Line 
1#
2# RTEMS Pretty Printers
3# Copyright 2010 Chris Johns (chrisj@rtems.org)
4#
5# $Id$
6#
7
8import gdb
9import re
10
11import objects
12import threads
13import classic
14
15# ToDo: Move every printing out
16import supercore_printer
17import classic_printer
18
19nesting = 0
20
21def type_from_value(val):
22    type = val.type;
23    # If it points to a reference, get the reference.
24    if type.code == gdb.TYPE_CODE_REF:
25        type = type.target ()
26    # Get the unqualified type
27    return type.unqualified ()
28
29def register_rtems_printers (obj):
30    "Register RTEMS pretty-printers with objfile Obj."
31
32    if obj == None:
33        obj = gdb
34
35    obj.pretty_printers.append (lookup_function)
36
37def lookup_function (val):
38    "Look-up and return a pretty-printer that can print val."
39
40    global nesting
41
42    typename = str(type_from_value(val))
43
44    for function in pp_dict:
45        if function.search (typename):
46            nesting += 1
47            result = pp_dict[function] (val)
48            nesting -= 1
49            if nesting == 0:
50                objects.information.invalidate()
51            return result
52
53    # Cannot find a pretty printer.  Return None.
54    return None
55
56def build_rtems_dict():
57    pp_dict[re.compile('^rtems_id$')]   = lambda val: supercore_printer.id(val)
58    pp_dict[re.compile('^Objects_Id$')] = lambda val: supercore_printer.id(val)
59    pp_dict[re.compile('^Objects_Name$')] = lambda val: supercore_printer.name(val)
60    pp_dict[re.compile('^Objects_Control$')] = lambda val: supercore_printer.control(val)
61    pp_dict[re.compile('^States_Control$')] = lambda val: supercore_printer.state(val)
62    pp_dict[re.compile('^rtems_attribute$')] = lambda val: classic_printer.attribute(val)
63    pp_dict[re.compile('^Semaphore_Control$')] = lambda val: classic_printer.semaphore(val)
64
65class rtems(gdb.Command):
66    """Prefix command for RTEMS."""
67
68    def __init__(self):
69        super(rtems, self).__init__('rtems',
70                                    gdb.COMMAND_STATUS,
71                                    gdb.COMPLETE_NONE,
72                                    True)
73
74class rtems_object(gdb.Command):
75    """Object sub-command for RTEMS"""
76
77    objects = {
78        'classic/semaphores': lambda obj: classic.semaphore(obj),
79        'classic/tasks': lambda obj: classic.task(obj),
80        'classic/message_queues': lambda obj: classic.message_queue(obj),
81        'classic/timers' : lambda obj: classic.timer(obj),
82        'classic/partitions' : lambda obj: classic.partition(obj),
83        'classic/regions' : lambda obj: classic.region(obj),
84        'classic/barriers' : lambda obj: classic.barrier(obj)
85        }
86
87    def __init__(self):
88        self.__doc__ = 'Display the RTEMS object given a numeric ID (Or a reference to rtems_object).'
89        super(rtems_object, self).__init__('rtems object',
90                                           gdb.COMMAND_STATUS)
91
92    def invoke(self, arg, from_tty):
93        for num in arg.split():
94            try:
95                val = gdb.parse_and_eval(num)
96                num = int(val)
97            except:
98                print 'error: "%s" is not a number' % (num)
99                return
100            id = objects.ident(num)
101            if not id.valid():
102                print 'Invalid object id'
103                return
104
105            print 'API:%s Class:%s Node:%d Index:%d Id:%08X' % \
106                (id.api(), id._class(), id.node(), id.index(), id.value())
107            objectname = id.api() + '/' + id._class()
108
109            obj = objects.information.object(id).dereference()
110            if objectname in self.objects:
111                object = self.objects[objectname](obj)
112                object.show(from_tty)
113        objects.information.invalidate()
114
115class rtems_semaphore(gdb.Command):
116    '''Semaphore subcommand for rtems'''
117
118    api = 'classic'
119    _class = 'semaphores'
120
121    def __init__(self):
122        self.__doc__ = 'Display the RTEMS semaphores by index'
123        super(rtems_semaphore, self).__init__('rtems semaphore',
124                                           gdb.COMMAND_STATUS)
125
126    def invoke(self, arg, from_tty):
127        for val in arg.split():
128            try:
129                index = int(val)
130            except ValueError:
131                print "error: %s is not an index" % (val)
132                return
133            try:
134                obj = objects.information.object_return( self.api,
135                                                         self._class,
136                                                         index ).dereference()
137            except IndexError:
138                print "error: index %s is invalid" % (index)
139                return
140
141            instance = classic.semaphore(obj)
142            instance.show(from_tty)
143        objects.information.invalidate()
144
145class rtems_task(gdb.Command):
146    '''tasks subcommand for rtems'''
147
148    api = 'classic'
149    _class = 'tasks'
150
151    def __init__(self):
152        self.__doc__ = 'Display the RTEMS tasks by index(s)'
153        super(rtems_task,self).__init__('rtems task', gdb.COMMAND_STATUS)
154
155    def invoke(self, arg, from_tty):
156        for val in arg.split():
157            try:
158                index = int(val)
159            except ValueError:
160                print "error: %s is not an index" % (val)
161                return
162
163            try:
164                obj = objects.information.object_return(self.api,
165                                                        self._class,
166                                                        index).dereference()
167            except IndexError:
168                print "error: index %s is invalid" % (index)
169                return
170
171            instance = classic.task(obj)
172            instance.show(from_tty)
173        objects.information.invalidate()
174
175#
176# Main
177#
178pp_dict = {}
179build_rtems_dict()
180gdb.pretty_printers = []
181gdb.pretty_printers.append (lookup_function)
182rtems()
183rtems_object()
184rtems_semaphore()
185rtems_task()
Note: See TracBrowser for help on using the repository browser.