• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2#  Copyright © 2019 Collabora Ltd.
3#
4#  Permission is hereby granted, free of charge, to any person obtaining
5#  a copy of this software and associated documentation files (the
6#  "Software"), to deal in the Software without restriction, including
7#  without limitation the rights to use, copy, modify, merge, publish,
8#  distribute, sublicense, and/or sell copies of the Software, and to
9#  permit persons to whom the Software is furnished to do so, subject to
10#  the following conditions:
11#
12#  The above copyright notice and this permission notice (including the
13#  next paragraph) shall be included in all copies or substantial
14#  portions of the Software.
15#
16#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17#  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18#  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19#  NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20#  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21#  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22#  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23#  SOFTWARE.
24#
25# Usage: source this script then 'display_flight_rec'
26#
27
28import gdb
29
30class DisplayFlightRecorder(gdb.Command):
31    def __init__(self):
32
33        self.rb = ''
34        symbol_found = False
35
36        ring_buff =  gdb.lookup_global_symbol("weston_primary_flight_recorder_ring_buffer")
37        if ring_buff == None:
38            print("'weston_ring_buffer' symbol not found!")
39            print("Either weston is too old or weston hasn't been loaded in memory")
40        else:
41            self.rb = ring_buff
42            self.display_rb_data(self.rb)
43            symbol_found = True
44
45        if symbol_found:
46            super(DisplayFlightRecorder, self).__init__("display_flight_rec",
47                                                        gdb.COMMAND_DATA)
48
49    def display_rb_data(self, rb):
50        print("Flight recorder data found. Use 'display_flight_rec' "
51                "to display its contents")
52        # display this data (only) if symbol is not empty (happens if the program is not ran at all)
53        if rb.value():
54            print("Data at byte {append}, Size: {size}B, "
55                  "Overlaped: {overlap}".format(append=rb.value()['append_pos'],
56                                                size=rb.value()['size'],
57                                                overlap=rb.value()['overlap']))
58
59    # poor's man fwrite()
60    def gen_contents(self, start, stop):
61        _str = ''
62        for j in range(start, stop):
63            _str += chr(self.rb.value()['buf'][j])
64        return _str
65
66    # mirrors C version, as to make sure we're not reading other parts...
67    def display_flight_rec_contents(self):
68
69        # symbol is there but not loaded, we're not far enough
70        if self.rb.value() == 0x0:
71            print("Flight recorder found, but not loaded yet!")
72            return
73        else:
74            print("Displaying flight recorder contents:")
75
76        append_pos = self.rb.value()['append_pos']
77        size = self.rb.value()['size']
78        overlap = self.rb.value()['overlap']
79
80        # if we haven't overflown and we're still at 0 means
81        # we still aren't far enough to be populated
82        if append_pos == 0 and not overlap:
83            print("Flight recorder doesn't have anything to display right now")
84            return
85
86        # now we can print stuff
87        rb_data = ''
88        if not overlap:
89            if append_pos:
90                rb_data = self.gen_contents(0, append_pos)
91            else:
92                rb_data = self.gen_contents(0, size)
93        else:
94            rb_data = self.gen_contents(append_pos, size)
95            rb_data += self.gen_contents(0, append_pos)
96
97        print("{data}".format(data=rb_data))
98
99    # called when invoking 'display_flight_rec'
100    def invoke(self, arg, from_tty):
101        self.display_flight_rec_contents()
102
103DisplayFlightRecorder()
104