1 | /* |
---|
2 | * Copyright (c) 2016 Chris Johns <chrisj@rtems.org>. |
---|
3 | * All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions |
---|
7 | * are met: |
---|
8 | * 1. Redistributions of source code must retain the above copyright |
---|
9 | * notice, this list of conditions and the following disclaimer. |
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer in the |
---|
12 | * documentation and/or other materials provided with the distribution. |
---|
13 | * |
---|
14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
24 | * SUCH DAMAGE. |
---|
25 | */ |
---|
26 | |
---|
27 | #ifdef HAVE_CONFIG_H |
---|
28 | #include "config.h" |
---|
29 | #endif |
---|
30 | |
---|
31 | #include <errno.h> |
---|
32 | #include <inttypes.h> |
---|
33 | #include <stdlib.h> |
---|
34 | #include <unistd.h> |
---|
35 | |
---|
36 | #define __need_getopt_newlib |
---|
37 | #include <getopt.h> |
---|
38 | |
---|
39 | #include <rtems.h> |
---|
40 | #include <rtems/printer.h> |
---|
41 | #include <rtems/shell.h> |
---|
42 | |
---|
43 | #include <rtems/rtems-debugger.h> |
---|
44 | |
---|
45 | /* |
---|
46 | * Debugger command for the RTEMS shell. |
---|
47 | */ |
---|
48 | |
---|
49 | static int rtems_shell_main_debugger(int argc, char *argv[]) |
---|
50 | { |
---|
51 | if (argc == 1) { |
---|
52 | printf("RTEMS debugger is %srunning\n", rtems_debugger_running() ? "" : "not "); |
---|
53 | return 0; |
---|
54 | } |
---|
55 | |
---|
56 | if (strcasecmp(argv[1], "start") == 0) { |
---|
57 | rtems_printer printer; |
---|
58 | const char* remote = "tcp"; |
---|
59 | const char* device = "1122"; |
---|
60 | int timeout = RTEMS_DEBUGGER_TIMEOUT; |
---|
61 | rtems_task_priority priority = 1; |
---|
62 | bool verbose = false; |
---|
63 | struct getopt_data data; |
---|
64 | char* end; |
---|
65 | int r; |
---|
66 | |
---|
67 | if (rtems_debugger_running()) { |
---|
68 | printf("error: debugger already running.\n"); |
---|
69 | return 1; |
---|
70 | } |
---|
71 | |
---|
72 | memset(&data, 0, sizeof(data)); |
---|
73 | |
---|
74 | rtems_print_printer_fprintf(&printer, stdout); |
---|
75 | |
---|
76 | argv += 1; |
---|
77 | argc -= 1; |
---|
78 | |
---|
79 | while (true) { |
---|
80 | int c; |
---|
81 | |
---|
82 | c = getopt_r(argc, argv, "vR:d:t:P:l:", &data); |
---|
83 | if (c == -1) |
---|
84 | break; |
---|
85 | |
---|
86 | switch (c) { |
---|
87 | case 'v': |
---|
88 | verbose = true; |
---|
89 | break; |
---|
90 | case 'R': |
---|
91 | remote = data.optarg; |
---|
92 | break; |
---|
93 | case 'd': |
---|
94 | device = data.optarg; |
---|
95 | break; |
---|
96 | case 't': |
---|
97 | timeout = strtoul(data.optarg, &end, 10); |
---|
98 | if (timeout == 0 || *end != '\0') { |
---|
99 | printf("error: invalid timeout: %s\n", data.optarg); |
---|
100 | return 1; |
---|
101 | } |
---|
102 | break; |
---|
103 | case 'P': |
---|
104 | priority = strtoul(data.optarg, &end, 10); |
---|
105 | if (priority == 0 || *end != '\0') { |
---|
106 | printf("error: invalid priority: %s\n", data.optarg); |
---|
107 | return 1; |
---|
108 | } |
---|
109 | break; |
---|
110 | case 'l': |
---|
111 | if (strcasecmp(data.optarg, "stdout") == 0) |
---|
112 | rtems_print_printer_fprintf(&printer, stdout); |
---|
113 | else if (strcasecmp(data.optarg, "stderr") == 0) |
---|
114 | rtems_print_printer_fprintf(&printer, stderr); |
---|
115 | else if (strcasecmp(data.optarg, "kernel") == 0) |
---|
116 | rtems_print_printer_printk(&printer); |
---|
117 | else { |
---|
118 | printf("error: unknown printer (stdout, stderr, kernel): %s\n", data.optarg); |
---|
119 | return 1; |
---|
120 | } |
---|
121 | break; |
---|
122 | default: |
---|
123 | case '?': |
---|
124 | if (data.optarg != NULL) |
---|
125 | printf("error: unknown option: %s\n", data.optarg); |
---|
126 | else |
---|
127 | printf("error: invalid start command\n"); |
---|
128 | return 1; |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | printf("RTEMS Debugger start: remote=%s device=%s priority=%" PRIu32 "\n", |
---|
133 | remote, device, priority); |
---|
134 | |
---|
135 | r = rtems_debugger_start(remote, device, timeout, priority, &printer); |
---|
136 | if (r < 0) { |
---|
137 | printf("debugger start failed\n"); |
---|
138 | return 1; |
---|
139 | } |
---|
140 | |
---|
141 | rtems_debugger_set_verbose(verbose); |
---|
142 | } |
---|
143 | else if (strcasecmp(argv[1], "stop") == 0) { |
---|
144 | int r; |
---|
145 | |
---|
146 | if (!rtems_debugger_running()) { |
---|
147 | printf("error: debugger not running.\n"); |
---|
148 | return 1; |
---|
149 | } |
---|
150 | |
---|
151 | r = rtems_debugger_stop(); |
---|
152 | if (r < 0) { |
---|
153 | printf("debugger stop failed\n"); |
---|
154 | return 1; |
---|
155 | } |
---|
156 | } |
---|
157 | else if (strcasecmp(argv[1], "remote-debug") == 0) { |
---|
158 | int r; |
---|
159 | |
---|
160 | if (!rtems_debugger_running()) { |
---|
161 | printf("error: debugger not running.\n"); |
---|
162 | return 1; |
---|
163 | } |
---|
164 | |
---|
165 | if (argc == 3 && strcasecmp(argv[2], "on") == 0) { |
---|
166 | r = rtems_debugger_remote_debug(true); |
---|
167 | if (r < 0) { |
---|
168 | printf("debugger remote-debug on failed\n"); |
---|
169 | return 1; |
---|
170 | } |
---|
171 | } |
---|
172 | else if (argc == 3 && strcasecmp(argv[2], "off") == 0) { |
---|
173 | r = rtems_debugger_remote_debug(false); |
---|
174 | if (r < 0) { |
---|
175 | printf("debugger remote-debug off failed\n"); |
---|
176 | return 1; |
---|
177 | } |
---|
178 | } |
---|
179 | else { |
---|
180 | printf("debugger remote-debug: not on or off\n"); |
---|
181 | return 1; |
---|
182 | } |
---|
183 | } |
---|
184 | else if (strcasecmp(argv[1], "verbose") == 0) { |
---|
185 | if (!rtems_debugger_running()) { |
---|
186 | printf("error: debugger not running.\n"); |
---|
187 | return 1; |
---|
188 | } |
---|
189 | |
---|
190 | if (argc == 3 && strcasecmp(argv[2], "on") == 0) { |
---|
191 | rtems_debugger_set_verbose(true); |
---|
192 | } |
---|
193 | else if (argc == 3 && strcasecmp(argv[2], "off") == 0) { |
---|
194 | rtems_debugger_set_verbose(false); |
---|
195 | } |
---|
196 | else { |
---|
197 | printf("debugger verbose: not on or off\n"); |
---|
198 | return 1; |
---|
199 | } |
---|
200 | } |
---|
201 | else if (strcasecmp(argv[1], "help") == 0) { |
---|
202 | printf("debugger [start/stop/help] ...\n" \ |
---|
203 | " start -v -R remote -d device -t secs -P priority -l [stdout,stderr,kernel]\n" \ |
---|
204 | " stop\n" \ |
---|
205 | " remote-debug <on/off>\n" \ |
---|
206 | " verbose <on/off>\n" \ |
---|
207 | " help\n"); |
---|
208 | } |
---|
209 | else { |
---|
210 | printf("error: unknown command: %s\n", argv[1]); |
---|
211 | return 1; |
---|
212 | } |
---|
213 | |
---|
214 | return 0; |
---|
215 | } |
---|
216 | |
---|
217 | rtems_shell_cmd_t rtems_shell_DEBUGGER_Command = { |
---|
218 | "debugger", /* name */ |
---|
219 | "debugger [start/stop] [options ...]", /* usage */ |
---|
220 | "misc", /* topic */ |
---|
221 | rtems_shell_main_debugger, /* command */ |
---|
222 | NULL, /* alias */ |
---|
223 | NULL, /* next */ |
---|
224 | 0755, |
---|
225 | 0, |
---|
226 | 0 |
---|
227 | }; |
---|