1# Copyright (c) 2018 Open Source Foundries Limited.
2# Copyright 2019 Foundries.io
3# Copyright (c) 2020 Nordic Semiconductor ASA
4#
5# SPDX-License-Identifier: Apache-2.0
6
7'''west "debug", "debugserver", and "attach" commands.'''
8
9from textwrap import dedent
10
11from run_common import add_parser_common, do_run_common
12from west.commands import WestCommand
13
14
15class Debug(WestCommand):
16
17    def __init__(self):
18        super().__init__(
19            'debug',
20            # Keep this in sync with the string in west-commands.yml.
21            'flash and interactively debug a Zephyr application',
22            dedent('''
23            Connect to the board, flash the program, and start a
24            debugging session. Use "west attach" instead to attach
25            a debugger without reflashing.'''),
26            accepts_unknown_args=True)
27        self.runner_key = 'debug-runner'  # in runners.yaml
28
29    def do_add_parser(self, parser_adder):
30        return add_parser_common(self, parser_adder)
31
32    def do_run(self, my_args, runner_args):
33        do_run_common(self, my_args, runner_args)
34
35
36class DebugServer(WestCommand):
37
38    def __init__(self):
39        super().__init__(
40            'debugserver',
41            # Keep this in sync with the string in west-commands.yml.
42            'connect to board and launch a debug server',
43            dedent('''
44            Connect to the board and launch a debug server which accepts
45            incoming connections for debugging the connected board.
46
47            The debug server binds to a known port, and allows client software
48            started elsewhere to connect to it and debug the running
49            Zephyr image.'''),
50            accepts_unknown_args=True)
51        self.runner_key = 'debug-runner'  # in runners.yaml
52
53    def do_add_parser(self, parser_adder):
54        return add_parser_common(self, parser_adder)
55
56    def do_run(self, my_args, runner_args):
57        do_run_common(self, my_args, runner_args)
58
59
60class Attach(WestCommand):
61
62    def __init__(self):
63        super().__init__(
64            'attach',
65            # Keep this in sync with the string in west-commands.yml.
66            'interactively debug a board',
67            "Like \"west debug\", but doesn't reflash the program.",
68            accepts_unknown_args=True)
69        self.runner_key = 'debug-runner'  # in runners.yaml
70
71    def do_add_parser(self, parser_adder):
72        return add_parser_common(self, parser_adder)
73
74    def do_run(self, my_args, runner_args):
75        do_run_common(self, my_args, runner_args)
76
77
78class Rtt(WestCommand):
79
80    def __init__(self):
81        super().__init__(
82            'rtt',
83            # Keep this in sync with the string in west-commands.yml.
84            'open an rtt shell',
85            "",
86            accepts_unknown_args=True)
87        self.runner_key = 'debug-runner'  # in runners.yaml
88
89    def do_add_parser(self, parser_adder):
90        return add_parser_common(self, parser_adder)
91
92    def do_run(self, my_args, runner_args):
93        do_run_common(self, my_args, runner_args)
94