summaryrefslogtreecommitdiffstats
path: root/tester/rt/gdb.py
blob: 4abbca537c84afbcb3224fc3435a7cfb9a943ad2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#
# RTEMS Tools Project (http://www.rtems.org/)
# Copyright 2013, 2020 Chris Johns (chrisj@rtems.org)
# All rights reserved.
#
# This file is part of the RTEMS Tools package in 'rtems-tools'.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

#
# RTEMS Testing GDB Interface
#

from __future__ import print_function

import datetime
import os
try:
    import Queue
    queue = Queue
except ImportError:
    import queue
import sys
import threading
import time

from rtemstoolkit import error
from rtemstoolkit import execute
from rtemstoolkit import options
from rtemstoolkit import path

import tester.rt.pygdb

class gdb(object):
    '''RTEMS Testing GDB base.'''

    def __init__(self, bsp_arch, bsp, trace = False, mi_trace = False):
        self.session = tester.rt.pygdb.mi_parser.session()
        self.trace = trace
        self.mi_trace = mi_trace
        self.lock_trace = False
        self.lock_locked = None
        self.lock = threading.RLock()
        self.script = None
        self.script_line = 0
        self.bsp = bsp
        self.bsp_arch = bsp_arch
        self.output = None
        self.output_length = 0
        self.gdb_console = None
        self.input = queue.Queue()
        self.commands = queue.Queue()
        self.process = None
        self.ecode = None
        self.state = {}
        self.running = False
        self.breakpoints = {}
        self.output = None
        self.output_buffer = ''
        self.timeout = None
        self.test_too_long = None
        self.lc = 0

    def _lock(self, msg):
        if self.lock_trace:
            print('|[   LOCK:%s ]|' % (msg))
            self.lock_locked = datetime.datetime.now()
        self.lock.acquire()

    def _unlock(self, msg):
        if self.lock_trace:
            period = datetime.datetime.now() - self.lock_locked
            print('|] UNLOCK:%s [| : %s' % (msg, period))
        self.lock.release()

    def _put(self, text):
        if self.trace:
            print(')))', text)
        self.commands.put(text)

    def _input_commands(self):
        if self.commands.empty():
            return False
        try:
            if self.trace:
                print('... input empty ', self.input.empty())
            if self.input.empty():
                line = self.commands.get(block = False)
                if self.trace:
                    print('+++', line)
                self.input.put(line)
        except:
            pass
        return True

    def _reader(self, line):
        self._lock('_reader')
        if self.trace:
            print('<<<', line)
        try:
            self.lc += 1
            if line.startswith('(gdb)'):
                if self.trace:
                    print('^^^ (gdb)')
                if not self._input_commands():
                    self.gdb_expect()
                    self._input_commands()
            else:
                self.gdb_parse(line)
        finally:
            self._unlock('_reader')

    def _writer(self):
        try:
            try:
                self._lock('_writer')
                try:
                    if self.process is None:
                        return None
                finally:
                    self._unlock('_writer')
                line = self.input.get(timeout = 0.5)
                if self.trace:
                    print('>>> input: queue=%d' % (self.input.qsize()), line)
            except queue.Empty:
                return True
            if line is None:
                return None
            return line + os.linesep
        except:
            if self.trace:
                print('writer exception')
            pass
        if self.trace:
            print('writer closing')
        return False

    def _timeout(self):
        self._stop()
        if self.timeout is not None:
            self.timeout()

    def _test_too_long(self):
        self._stop()
        if self.test_too_long is not None:
            self.test_too_long()

    def _cleanup(self, proc):
        self._lock('_cleanup')
        try:
            self._put(None)
        finally:
            self._unlock('_cleanup')

    def _gdb_quit(self, backtrace = False):
        self._lock('_gdb_quit')
        try:
            self._put('-exec-interrupt')
            if backtrace:
                self._put('bt')
            self._put('quit')
            self._put('None')
            if self.script:
                self.script_line = len(self.script)
        finally:
            self._unlock('_gdb_quit')

    def _stop(self):
        self._gdb_quit(backtrace=True)
        seconds = 5
        step = 0.1
        while self.process and seconds > 0:
            if seconds > step:
                seconds -= step
            else:
                seconds = 0
            self._unlock('_stop')
            time.sleep(step)
            self._lock('_stop')
        if self.process and seconds == 0:
            self._kill()

    def _kill(self):
        if self.process:
            self.process.kill()
        self.process = None

    def _execute_gdb(self, args):
        '''Thread to execute GDB and to wait for it to finish.'''
        cmds = args
        self.gdb_console('gdb: %s' % (' '.join(cmds)))
        ecode, proc = self.process.open(cmds)
        if self.trace:
            print('gdb done', ecode)
        self._lock('_execute_gdb')
        self.ecode = ecode
        self.process = None
        self.running = False
        self._unlock('_execute_gdb')

    def _monitor(self, timeout):
        output_length = self.output_length
        step = 0.25
        period = timeout[0]
        seconds = timeout[1]
        while self.process and period > 0 and seconds > 0:
            current_length = self.output_length
            if output_length != current_length:
                period = timeout[0]
            output_length = current_length
            if seconds < step:
                seconds = 0
            else:
                seconds -= step
            if period < step:
                step = period
                period = 0
            else:
                period -= step
            self._unlock('_monitor')
            time.sleep(step)
            self._lock('_monitor')
        if self.process is not None:
            if period == 0:
                self._timeout()
            elif seconds == 0:
                self._test_too_long()

    def open(self, command, executable,
             output, gdb_console, timeout,
             script = None, tty = None):
        self._lock('_open')
        self.timeout = timeout[2]
        self.test_too_long = timeout[3]
        try:
            cmds = execute.arg_list(command) + ['-i=mi',
                                                '--nx',
                                                '--quiet']
            if tty:
                cmds += ['--tty=%s' % tty]
            if executable:
                cmds += [executable]
            self.output = output
            self.gdb_console = gdb_console
            self.script = script
            self.process = execute.execute(output = self._reader,
                                           input = self._writer,
                                           cleanup = self._cleanup)
            exec_thread = threading.Thread(target=self._execute_gdb,
                                           args=[cmds])
            exec_thread.start()
            self._monitor(timeout)
            if self.ecode is not None and self.ecode > 0:
                raise error.general('gdb exec: %s: %s' % (cmds[0],
                                                          os.strerror(self.ecode)))
        finally:
            self._unlock('_open')

    def kill(self):
        self._lock('_kill')
        try:
            self._kill()
        finally:
            self._unlock('_kill')

    def gdb_expect(self):
        if self.trace:
            print('}}} gdb-expect')
        if self.process and not self.running and self.script is not None:
            if self.script_line == len(self.script):
                self._put(None)
            else:
                if self.script_line == 0:
                    self._put('-gdb-set confirm no')
                line = self.script[self.script_line]
                self.script_line += 1
                self._put(line)

    def gdb_parse(self, lines):
        try:
            if self.mi_trace:
                print('mi-data:', lines)
            rec = self.session.process(lines)
            if self.mi_trace:
                print('mi-rec:', rec)
            if rec.record_type == 'result':
                if rec.type == 'result':
                    if rec.class_ == 'error':
                        self._gdb_quit()
                    elif 'register_names' in dir(rec.result):
                        self.register_names = rec.result.register_names
                    elif 'register_values' in dir(rec.result):
                        self.register_values = rec.result.register_values
                elif rec.type == 'exec':
                    if rec.class_ == 'running':
                        if self.trace:
                            print('*** running')
                        self._put('')
                        self.running = True
                    elif rec.class_ == 'stopped':
                        if self.trace:
                            print('*** stopped')
                        self.running = False
                        #self._put('-data-list-register-values')
                elif rec.type == 'breakpoint':
                    if rec.class_ == 'breakpoint-created':
                        self.breakpoints[rec.result.bkpt.number] = rec.result.bkpt
                    elif rec.class_ == 'breakpoint-modified':
                        self.breakpoints[rec.result.bkpt.number] = rec.result.bkpt
                    elif rec.class_ == 'breakpoint-deleted':
                        if rec.result.id in self.breakpoints:
                            del self.breakpoints[rec.result.id]
            elif rec.record_type == 'error':
                self._gdb_quit()
            elif rec.record_type == 'stream':
                if rec.type == 'console' or rec.type == 'log':
                    for line in rec.value.splitlines():
                        self.gdb_console(line)
                if rec.type == 'target':
                    self.output_buffer += rec.value
                    last_lf = self.output_buffer.rfind('\n')
                    if last_lf >= 0:
                        lines = self.output_buffer[:last_lf]
                        if self.trace:
                            print('/// console output: ', len(lines))
                        for line in lines.splitlines():
                            self.output_length += len(line)
                            self.output(line)
                        self.output_buffer = self.output_buffer[last_lf + 1:]
        except:
            if self.trace:
                print('/// exception: console output')
            for line in lines.splitlines():
                self.output(line)

if __name__ == "__main__":
    import tester.rt.console
    stdtty = tester.rt.console.save()
    try:
        def output(text):
            print(']', text)
        def gdb_console(text):
            print('>', text)
        script = ['target sim']
        if len(sys.argv) > 1:
            executable = sys.argv[1]
            script += ['load',
                       'run',
                       'info reg',
                       '-stack-list-frames',
                       '-stack-list-arguments --all-values']
        else:
            executable = None
        script += ['quit']
        g = gdb('sparc', 'sis', mi_trace = True)
        g.open('sparc-rtems4.11-gdb', executable, output, gdb_console, script)
    except:
        tester.rt.console.restore(stdtty)
        raise
    finally:
        tester.rt.console.restore(stdtty)