]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Generic serial interface routines |
4fcf66da | 2 | |
d01e8234 | 3 | Copyright (C) 1992-2025 Free Software Foundation, Inc. |
c906108c | 4 | |
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 10 | (at your option) any later version. |
c906108c | 11 | |
c5aa993b JM |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
c906108c | 16 | |
c5aa993b | 17 | You should have received a copy of the GNU General Public License |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c | 19 | |
c906108c SS |
20 | #include <ctype.h> |
21 | #include "serial.h" | |
5b9707eb | 22 | #include "cli/cli-cmds.h" |
529480d0 | 23 | #include "cli/cli-utils.h" |
c906108c | 24 | |
c378eb4e | 25 | /* Is serial being debugged? */ |
2acceee2 | 26 | |
ccce17b0 | 27 | static unsigned int global_serial_debug_p; |
2acceee2 | 28 | |
fcd488ca TT |
29 | /* Serial I/O handlers. */ |
30 | ||
191cca63 | 31 | static std::vector<const struct serial_ops *> serial_ops_list; |
c906108c | 32 | |
5eb3b062 PA |
33 | /* Pointer to list of scb's. */ |
34 | ||
35 | static struct serial *scb_base; | |
36 | ||
c906108c | 37 | /* Non-NULL gives filename which contains a recording of the remote session, |
c378eb4e | 38 | suitable for playback by gdbserver. */ |
c906108c | 39 | |
e0700ba4 | 40 | static std::string serial_logfile; |
d9fcf2fb | 41 | static struct ui_file *serial_logfp = NULL; |
c906108c | 42 | |
fcd488ca | 43 | static const struct serial_ops *serial_interface_lookup (const char *); |
3e43a32a MS |
44 | static void serial_logchar (struct ui_file *stream, |
45 | int ch_type, int ch, int timeout); | |
53904c9e AC |
46 | static const char logbase_hex[] = "hex"; |
47 | static const char logbase_octal[] = "octal"; | |
48 | static const char logbase_ascii[] = "ascii"; | |
40478521 | 49 | static const char *const logbase_enums[] = |
c5aa993b | 50 | {logbase_hex, logbase_octal, logbase_ascii, NULL}; |
53904c9e | 51 | static const char *serial_logbase = logbase_ascii; |
c906108c | 52 | \f |
c5aa993b | 53 | |
c906108c SS |
54 | static int serial_current_type = 0; |
55 | ||
c378eb4e | 56 | /* Log char CH of type CHTYPE, with TIMEOUT. */ |
c906108c SS |
57 | |
58 | /* Define bogus char to represent a BREAK. Should be careful to choose a value | |
59 | that can't be confused with a normal char, or an error code. */ | |
60 | #define SERIAL_BREAK 1235 | |
61 | ||
62 | static void | |
d9fcf2fb | 63 | serial_logchar (struct ui_file *stream, int ch_type, int ch, int timeout) |
c906108c SS |
64 | { |
65 | if (ch_type != serial_current_type) | |
66 | { | |
6cb06a8c | 67 | gdb_printf (stream, "\n%c ", ch_type); |
c906108c SS |
68 | serial_current_type = ch_type; |
69 | } | |
70 | ||
71 | if (serial_logbase != logbase_ascii) | |
a11ac3b3 | 72 | gdb_putc (' ', stream); |
c906108c SS |
73 | |
74 | switch (ch) | |
75 | { | |
76 | case SERIAL_TIMEOUT: | |
6cb06a8c | 77 | gdb_printf (stream, "<Timeout: %d seconds>", timeout); |
c906108c SS |
78 | return; |
79 | case SERIAL_ERROR: | |
6cb06a8c | 80 | gdb_printf (stream, "<Error: %s>", safe_strerror (errno)); |
c906108c SS |
81 | return; |
82 | case SERIAL_EOF: | |
0426ad51 | 83 | gdb_puts ("<Eof>", stream); |
c906108c SS |
84 | return; |
85 | case SERIAL_BREAK: | |
0426ad51 | 86 | gdb_puts ("<Break>", stream); |
c906108c SS |
87 | return; |
88 | default: | |
89 | if (serial_logbase == logbase_hex) | |
6cb06a8c | 90 | gdb_printf (stream, "%02x", ch & 0xff); |
c906108c | 91 | else if (serial_logbase == logbase_octal) |
6cb06a8c | 92 | gdb_printf (stream, "%03o", ch & 0xff); |
c906108c SS |
93 | else |
94 | switch (ch) | |
95 | { | |
c5aa993b | 96 | case '\\': |
0426ad51 | 97 | gdb_puts ("\\\\", stream); |
c5aa993b JM |
98 | break; |
99 | case '\b': | |
0426ad51 | 100 | gdb_puts ("\\b", stream); |
c5aa993b JM |
101 | break; |
102 | case '\f': | |
0426ad51 | 103 | gdb_puts ("\\f", stream); |
c5aa993b JM |
104 | break; |
105 | case '\n': | |
0426ad51 | 106 | gdb_puts ("\\n", stream); |
c5aa993b JM |
107 | break; |
108 | case '\r': | |
0426ad51 | 109 | gdb_puts ("\\r", stream); |
c5aa993b JM |
110 | break; |
111 | case '\t': | |
0426ad51 | 112 | gdb_puts ("\\t", stream); |
c5aa993b JM |
113 | break; |
114 | case '\v': | |
0426ad51 | 115 | gdb_puts ("\\v", stream); |
c5aa993b JM |
116 | break; |
117 | default: | |
6cb06a8c TT |
118 | gdb_printf (stream, |
119 | isprint (ch) ? "%c" : "\\x%02x", ch & 0xFF); | |
c5aa993b | 120 | break; |
c906108c SS |
121 | } |
122 | } | |
123 | } | |
124 | ||
125 | void | |
4ab76ea3 | 126 | serial_log_command (struct target_ops *self, const char *cmd) |
c906108c SS |
127 | { |
128 | if (!serial_logfp) | |
129 | return; | |
130 | ||
131 | serial_current_type = 'c'; | |
132 | ||
0426ad51 TT |
133 | gdb_puts ("\nc ", serial_logfp); |
134 | gdb_puts (cmd, serial_logfp); | |
c906108c SS |
135 | |
136 | /* Make sure that the log file is as up-to-date as possible, | |
c378eb4e | 137 | in case we are getting ready to dump core or something. */ |
c906108c SS |
138 | gdb_flush (serial_logfp); |
139 | } | |
140 | ||
c2c6d25f | 141 | \f |
fcd488ca | 142 | static const struct serial_ops * |
58f07bae | 143 | serial_interface_lookup (const char *name) |
c906108c | 144 | { |
71b73764 | 145 | for (const serial_ops *ops : serial_ops_list) |
c906108c SS |
146 | if (strcmp (name, ops->name) == 0) |
147 | return ops; | |
148 | ||
149 | return NULL; | |
150 | } | |
151 | ||
152 | void | |
fcd488ca | 153 | serial_add_interface (const struct serial_ops *optable) |
c906108c | 154 | { |
191cca63 | 155 | serial_ops_list.push_back (optable); |
c906108c SS |
156 | } |
157 | ||
5eb3b062 PA |
158 | /* Return the open serial device for FD, if found, or NULL if FD is |
159 | not already opened. */ | |
160 | ||
161 | struct serial * | |
162 | serial_for_fd (int fd) | |
163 | { | |
164 | struct serial *scb; | |
165 | ||
166 | for (scb = scb_base; scb; scb = scb->next) | |
167 | if (scb->fd == fd) | |
168 | return scb; | |
169 | ||
170 | return NULL; | |
171 | } | |
172 | ||
00340e1b PA |
173 | /* Create a new serial for OPS. */ |
174 | ||
a2e0acea | 175 | static gdb::unique_xmalloc_ptr<struct serial> |
00340e1b PA |
176 | new_serial (const struct serial_ops *ops) |
177 | { | |
a2e0acea | 178 | gdb::unique_xmalloc_ptr<struct serial> scb (XCNEW (struct serial)); |
00340e1b PA |
179 | |
180 | scb->ops = ops; | |
181 | ||
182 | scb->bufp = scb->buf; | |
183 | scb->error_fd = -1; | |
184 | scb->refcnt = 1; | |
185 | ||
186 | return scb; | |
187 | } | |
188 | ||
189 | static struct serial *serial_open_ops_1 (const struct serial_ops *ops, | |
190 | const char *open_name); | |
191 | ||
c378eb4e | 192 | /* Open up a device or a network socket, depending upon the syntax of NAME. */ |
c906108c | 193 | |
819cc324 | 194 | struct serial * |
c2c6d25f | 195 | serial_open (const char *name) |
c906108c | 196 | { |
fcd488ca | 197 | const struct serial_ops *ops; |
c2c6d25f | 198 | const char *open_name = name; |
c906108c | 199 | |
ca1ca08b | 200 | if (startswith (name, "|")) |
cced7cac | 201 | ops = serial_interface_lookup ("pipe"); |
2821caf1 JB |
202 | /* Check for a colon, suggesting an IP address/port pair. |
203 | Do this *after* checking for all the interesting prefixes. We | |
204 | don't want to constrain the syntax of what can follow them. */ | |
431f22cc | 205 | else if (strchr (name, ':')) |
2821caf1 | 206 | ops = serial_interface_lookup ("tcp"); |
c906108c | 207 | else |
c1168a2f JD |
208 | { |
209 | #ifndef USE_WIN32API | |
210 | /* Check to see if name is a socket. If it is, then treat it | |
dda83cd7 | 211 | as such. Otherwise assume that it's a character device. */ |
c1168a2f | 212 | struct stat sb; |
431f22cc | 213 | if (stat (name, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFSOCK) |
c1168a2f JD |
214 | ops = serial_interface_lookup ("local"); |
215 | else | |
216 | #endif | |
217 | ops = serial_interface_lookup ("hardwire"); | |
218 | } | |
c906108c SS |
219 | |
220 | if (!ops) | |
a2e0acea | 221 | error (_("could not find serial handler for '%s'"), name); |
c906108c | 222 | |
00340e1b PA |
223 | return serial_open_ops_1 (ops, open_name); |
224 | } | |
c906108c | 225 | |
00340e1b | 226 | /* Open up a serial for OPS, passing OPEN_NAME to the open method. */ |
c906108c | 227 | |
00340e1b PA |
228 | static struct serial * |
229 | serial_open_ops_1 (const struct serial_ops *ops, const char *open_name) | |
230 | { | |
a2e0acea | 231 | gdb::unique_xmalloc_ptr<struct serial> scb = new_serial (ops); |
c906108c | 232 | |
766062f6 | 233 | /* `...->open (...)' would get expanded by the open(2) syscall macro. */ |
a2e0acea | 234 | (*scb->ops->open) (scb.get (), open_name); |
c906108c | 235 | |
4f837581 | 236 | scb->name = open_name != NULL ? xstrdup (open_name) : NULL; |
5eb3b062 | 237 | scb->next = scb_base; |
a2e0acea | 238 | scb_base = scb.get (); |
c906108c | 239 | |
e0700ba4 | 240 | if (!serial_logfile.empty ()) |
c906108c | 241 | { |
d7e74731 PA |
242 | stdio_file_up file (new stdio_file ()); |
243 | ||
e0700ba4 SM |
244 | if (!file->open (serial_logfile.c_str (), "w")) |
245 | perror_with_name (serial_logfile.c_str ()); | |
d7e74731 PA |
246 | |
247 | serial_logfp = file.release (); | |
c906108c SS |
248 | } |
249 | ||
a2e0acea | 250 | return scb.release (); |
c906108c SS |
251 | } |
252 | ||
00340e1b PA |
253 | /* See serial.h. */ |
254 | ||
255 | struct serial * | |
256 | serial_open_ops (const struct serial_ops *ops) | |
257 | { | |
258 | return serial_open_ops_1 (ops, NULL); | |
259 | } | |
260 | ||
58f07bae PA |
261 | /* Open a new serial stream using a file handle, using serial |
262 | interface ops OPS. */ | |
263 | ||
264 | static struct serial * | |
fcd488ca | 265 | serial_fdopen_ops (const int fd, const struct serial_ops *ops) |
c906108c | 266 | { |
0ea3f30e | 267 | if (!ops) |
58f07bae PA |
268 | { |
269 | ops = serial_interface_lookup ("terminal"); | |
270 | if (!ops) | |
24b21115 | 271 | ops = serial_interface_lookup ("hardwire"); |
58f07bae | 272 | } |
c906108c SS |
273 | |
274 | if (!ops) | |
275 | return NULL; | |
276 | ||
a2e0acea | 277 | gdb::unique_xmalloc_ptr<struct serial> scb = new_serial (ops); |
c906108c | 278 | |
4f837581 | 279 | scb->name = NULL; |
5eb3b062 | 280 | scb->next = scb_base; |
a2e0acea | 281 | scb_base = scb.get (); |
c906108c | 282 | |
58f07bae | 283 | if ((ops->fdopen) != NULL) |
a2e0acea | 284 | (*ops->fdopen) (scb.get (), fd); |
58f07bae PA |
285 | else |
286 | scb->fd = fd; | |
287 | ||
a2e0acea | 288 | return scb.release (); |
c906108c SS |
289 | } |
290 | ||
58f07bae PA |
291 | struct serial * |
292 | serial_fdopen (const int fd) | |
293 | { | |
294 | return serial_fdopen_ops (fd, NULL); | |
295 | } | |
296 | ||
c2c6d25f | 297 | static void |
819cc324 | 298 | do_serial_close (struct serial *scb, int really_close) |
c906108c | 299 | { |
819cc324 | 300 | struct serial *tmp_scb; |
c906108c | 301 | |
c906108c SS |
302 | if (serial_logfp) |
303 | { | |
0426ad51 | 304 | gdb_puts ("\nEnd of log\n", serial_logfp); |
c906108c SS |
305 | serial_current_type = 0; |
306 | ||
c378eb4e | 307 | /* XXX - What if serial_logfp == gdb_stdout or gdb_stderr? */ |
d7e74731 | 308 | delete serial_logfp; |
c906108c SS |
309 | serial_logfp = NULL; |
310 | } | |
311 | ||
c378eb4e | 312 | /* ensure that the FD has been taken out of async mode. */ |
c2c6d25f JM |
313 | if (scb->async_handler != NULL) |
314 | serial_async (scb, NULL, NULL); | |
315 | ||
c906108c SS |
316 | if (really_close) |
317 | scb->ops->close (scb); | |
318 | ||
4f837581 PA |
319 | xfree (scb->name); |
320 | ||
ddefb60f PA |
321 | /* For serial_is_open. */ |
322 | scb->bufp = NULL; | |
323 | ||
5eb3b062 PA |
324 | if (scb_base == scb) |
325 | scb_base = scb_base->next; | |
326 | else | |
327 | for (tmp_scb = scb_base; tmp_scb; tmp_scb = tmp_scb->next) | |
328 | { | |
329 | if (tmp_scb->next != scb) | |
330 | continue; | |
331 | ||
332 | tmp_scb->next = tmp_scb->next->next; | |
333 | break; | |
334 | } | |
335 | ||
ddefb60f | 336 | serial_unref (scb); |
c906108c SS |
337 | } |
338 | ||
c2c6d25f | 339 | void |
819cc324 | 340 | serial_close (struct serial *scb) |
c2c6d25f JM |
341 | { |
342 | do_serial_close (scb, 1); | |
343 | } | |
344 | ||
345 | void | |
819cc324 | 346 | serial_un_fdopen (struct serial *scb) |
c2c6d25f JM |
347 | { |
348 | do_serial_close (scb, 0); | |
349 | } | |
350 | ||
ddefb60f PA |
351 | int |
352 | serial_is_open (struct serial *scb) | |
353 | { | |
354 | return scb->bufp != NULL; | |
355 | } | |
356 | ||
357 | void | |
358 | serial_ref (struct serial *scb) | |
359 | { | |
360 | scb->refcnt++; | |
361 | } | |
362 | ||
363 | void | |
364 | serial_unref (struct serial *scb) | |
365 | { | |
366 | --scb->refcnt; | |
367 | if (scb->refcnt == 0) | |
368 | xfree (scb); | |
369 | } | |
370 | ||
c2c6d25f | 371 | int |
819cc324 | 372 | serial_readchar (struct serial *scb, int timeout) |
c2c6d25f JM |
373 | { |
374 | int ch; | |
375 | ||
2df3850c | 376 | /* FIXME: cagney/1999-10-11: Don't enable this check until the ASYNC |
c378eb4e | 377 | code is finished. */ |
2cd58942 | 378 | if (0 && serial_is_async_p (scb) && timeout < 0) |
f34652de | 379 | internal_error (_("serial_readchar: blocking read in async mode")); |
2df3850c | 380 | |
c2c6d25f JM |
381 | ch = scb->ops->readchar (scb, timeout); |
382 | if (serial_logfp != NULL) | |
383 | { | |
2acceee2 | 384 | serial_logchar (serial_logfp, 'r', ch, timeout); |
c2c6d25f JM |
385 | |
386 | /* Make sure that the log file is as up-to-date as possible, | |
dda83cd7 | 387 | in case we are getting ready to dump core or something. */ |
c2c6d25f JM |
388 | gdb_flush (serial_logfp); |
389 | } | |
2cd58942 | 390 | if (serial_debug_p (scb)) |
2acceee2 | 391 | { |
6cb06a8c | 392 | gdb_printf (gdb_stdlog, "["); |
2acceee2 | 393 | serial_logchar (gdb_stdlog, 'r', ch, timeout); |
6cb06a8c | 394 | gdb_printf (gdb_stdlog, "]"); |
2acceee2 JM |
395 | gdb_flush (gdb_stdlog); |
396 | } | |
c2c6d25f JM |
397 | |
398 | return (ch); | |
399 | } | |
400 | ||
d69939bd | 401 | void |
c628b528 | 402 | serial_write (struct serial *scb, const void *buf, size_t count) |
c2c6d25f JM |
403 | { |
404 | if (serial_logfp != NULL) | |
405 | { | |
19ba03f4 | 406 | const char *str = (const char *) buf; |
c628b528 | 407 | size_t c; |
c2c6d25f | 408 | |
c628b528 PA |
409 | for (c = 0; c < count; c++) |
410 | serial_logchar (serial_logfp, 'w', str[c] & 0xff, 0); | |
c2c6d25f JM |
411 | |
412 | /* Make sure that the log file is as up-to-date as possible, | |
dda83cd7 | 413 | in case we are getting ready to dump core or something. */ |
c2c6d25f JM |
414 | gdb_flush (serial_logfp); |
415 | } | |
9214d371 DE |
416 | if (serial_debug_p (scb)) |
417 | { | |
19ba03f4 | 418 | const char *str = (const char *) buf; |
c628b528 | 419 | size_t c; |
9214d371 | 420 | |
c628b528 | 421 | for (c = 0; c < count; c++) |
9214d371 | 422 | { |
6cb06a8c | 423 | gdb_printf (gdb_stdlog, "["); |
0e58ee40 | 424 | serial_logchar (gdb_stdlog, 'w', str[c] & 0xff, 0); |
6cb06a8c | 425 | gdb_printf (gdb_stdlog, "]"); |
9214d371 DE |
426 | } |
427 | gdb_flush (gdb_stdlog); | |
428 | } | |
c2c6d25f | 429 | |
d69939bd | 430 | scb->ops->write (scb, buf, count); |
c2c6d25f JM |
431 | } |
432 | ||
433 | void | |
467dc1e2 | 434 | serial_printf (struct serial *desc, const char *format, ...) |
c2c6d25f JM |
435 | { |
436 | va_list args; | |
c2c6d25f JM |
437 | va_start (args, format); |
438 | ||
467dc1e2 SM |
439 | std::string buf = string_vprintf (format, args); |
440 | serial_write (desc, buf.c_str (), buf.length ()); | |
c2c6d25f | 441 | |
c2c6d25f JM |
442 | va_end (args); |
443 | } | |
444 | ||
445 | int | |
819cc324 | 446 | serial_drain_output (struct serial *scb) |
c2c6d25f JM |
447 | { |
448 | return scb->ops->drain_output (scb); | |
449 | } | |
450 | ||
451 | int | |
819cc324 | 452 | serial_flush_output (struct serial *scb) |
c2c6d25f JM |
453 | { |
454 | return scb->ops->flush_output (scb); | |
455 | } | |
456 | ||
457 | int | |
819cc324 | 458 | serial_flush_input (struct serial *scb) |
c2c6d25f JM |
459 | { |
460 | return scb->ops->flush_input (scb); | |
461 | } | |
462 | ||
d69939bd | 463 | void |
819cc324 | 464 | serial_send_break (struct serial *scb) |
c2c6d25f JM |
465 | { |
466 | if (serial_logfp != NULL) | |
2acceee2 | 467 | serial_logchar (serial_logfp, 'w', SERIAL_BREAK, 0); |
c2c6d25f | 468 | |
d69939bd | 469 | scb->ops->send_break (scb); |
c2c6d25f JM |
470 | } |
471 | ||
472 | void | |
819cc324 | 473 | serial_raw (struct serial *scb) |
c2c6d25f JM |
474 | { |
475 | scb->ops->go_raw (scb); | |
476 | } | |
477 | ||
478 | serial_ttystate | |
819cc324 | 479 | serial_get_tty_state (struct serial *scb) |
c2c6d25f JM |
480 | { |
481 | return scb->ops->get_tty_state (scb); | |
482 | } | |
483 | ||
1e182ce8 UW |
484 | serial_ttystate |
485 | serial_copy_tty_state (struct serial *scb, serial_ttystate ttystate) | |
486 | { | |
487 | return scb->ops->copy_tty_state (scb, ttystate); | |
488 | } | |
489 | ||
c2c6d25f | 490 | int |
819cc324 | 491 | serial_set_tty_state (struct serial *scb, serial_ttystate ttystate) |
c2c6d25f JM |
492 | { |
493 | return scb->ops->set_tty_state (scb, ttystate); | |
494 | } | |
495 | ||
496 | void | |
819cc324 | 497 | serial_print_tty_state (struct serial *scb, |
c2c6d25f | 498 | serial_ttystate ttystate, |
d9fcf2fb | 499 | struct ui_file *stream) |
c2c6d25f JM |
500 | { |
501 | scb->ops->print_tty_state (scb, ttystate, stream); | |
502 | } | |
503 | ||
ad3cf8c6 | 504 | void |
819cc324 | 505 | serial_setbaudrate (struct serial *scb, int rate) |
c2c6d25f | 506 | { |
ad3cf8c6 | 507 | scb->ops->setbaudrate (scb, rate); |
c2c6d25f JM |
508 | } |
509 | ||
510 | int | |
819cc324 | 511 | serial_setstopbits (struct serial *scb, int num) |
c2c6d25f JM |
512 | { |
513 | return scb->ops->setstopbits (scb, num); | |
514 | } | |
515 | ||
236af5e3 YG |
516 | /* See serial.h. */ |
517 | ||
518 | int | |
519 | serial_setparity (struct serial *scb, int parity) | |
520 | { | |
521 | return scb->ops->setparity (scb, parity); | |
522 | } | |
523 | ||
c2c6d25f | 524 | int |
819cc324 | 525 | serial_can_async_p (struct serial *scb) |
c2c6d25f JM |
526 | { |
527 | return (scb->ops->async != NULL); | |
528 | } | |
529 | ||
530 | int | |
819cc324 | 531 | serial_is_async_p (struct serial *scb) |
c2c6d25f JM |
532 | { |
533 | return (scb->ops->async != NULL) && (scb->async_handler != NULL); | |
534 | } | |
535 | ||
536 | void | |
819cc324 | 537 | serial_async (struct serial *scb, |
c2c6d25f JM |
538 | serial_event_ftype *handler, |
539 | void *context) | |
540 | { | |
05ce04a4 | 541 | int changed = ((scb->async_handler == NULL) != (handler == NULL)); |
433759f7 | 542 | |
c2c6d25f JM |
543 | scb->async_handler = handler; |
544 | scb->async_context = context; | |
05ce04a4 VP |
545 | /* Only change mode if there is a need. */ |
546 | if (changed) | |
547 | scb->ops->async (scb, handler != NULL); | |
c2c6d25f JM |
548 | } |
549 | ||
2acceee2 | 550 | void |
819cc324 | 551 | serial_debug (struct serial *scb, int debug_p) |
2acceee2 JM |
552 | { |
553 | scb->debug_p = debug_p; | |
554 | } | |
555 | ||
556 | int | |
819cc324 | 557 | serial_debug_p (struct serial *scb) |
2acceee2 JM |
558 | { |
559 | return scb->debug_p || global_serial_debug_p; | |
560 | } | |
561 | ||
0ea3f30e DJ |
562 | #ifdef USE_WIN32API |
563 | void | |
564 | serial_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except) | |
565 | { | |
566 | if (scb->ops->wait_handle) | |
567 | scb->ops->wait_handle (scb, read, except); | |
568 | else | |
569 | { | |
570 | *read = (HANDLE) _get_osfhandle (scb->fd); | |
571 | *except = NULL; | |
572 | } | |
573 | } | |
c3e2b812 DJ |
574 | |
575 | void | |
576 | serial_done_wait_handle (struct serial *scb) | |
577 | { | |
578 | if (scb->ops->done_wait_handle) | |
579 | scb->ops->done_wait_handle (scb); | |
580 | } | |
0ea3f30e | 581 | #endif |
2acceee2 | 582 | |
58f07bae PA |
583 | int |
584 | serial_pipe (struct serial *scbs[2]) | |
585 | { | |
fcd488ca | 586 | const struct serial_ops *ops; |
58f07bae PA |
587 | int fildes[2]; |
588 | ||
589 | ops = serial_interface_lookup ("pipe"); | |
590 | if (!ops) | |
591 | { | |
592 | errno = ENOSYS; | |
593 | return -1; | |
594 | } | |
595 | ||
596 | if (gdb_pipe (fildes) == -1) | |
597 | return -1; | |
598 | ||
599 | scbs[0] = serial_fdopen_ops (fildes[0], ops); | |
600 | scbs[1] = serial_fdopen_ops (fildes[1], ops); | |
601 | return 0; | |
602 | } | |
603 | ||
e3abfe1d AC |
604 | /* Serial set/show framework. */ |
605 | ||
606 | static struct cmd_list_element *serial_set_cmdlist; | |
607 | static struct cmd_list_element *serial_show_cmdlist; | |
608 | ||
16e9019e | 609 | /* See serial.h. */ |
0d12017b JB |
610 | |
611 | int baud_rate = -1; | |
612 | ||
613 | static void | |
614 | serial_baud_show_cmd (struct ui_file *file, int from_tty, | |
615 | struct cmd_list_element *c, const char *value) | |
616 | { | |
6cb06a8c TT |
617 | gdb_printf (file, _("Baud rate for remote serial I/O is %s.\n"), |
618 | value); | |
0d12017b | 619 | } |
e3abfe1d | 620 | |
16e9019e | 621 | /* See serial.h. */ |
236af5e3 YG |
622 | |
623 | int serial_parity = GDBPARITY_NONE; | |
624 | ||
625 | static const char parity_none[] = "none"; | |
626 | static const char parity_odd[] = "odd"; | |
627 | static const char parity_even[] = "even"; | |
628 | static const char *const parity_enums[] = | |
629 | {parity_none, parity_odd, parity_even, NULL}; | |
630 | static const char *parity = parity_none; | |
631 | ||
632 | /* Set serial_parity value. */ | |
633 | ||
634 | static void | |
eb4c3f4a | 635 | set_parity (const char *ignore_args, int from_tty, struct cmd_list_element *c) |
236af5e3 YG |
636 | { |
637 | if (parity == parity_odd) | |
638 | serial_parity = GDBPARITY_ODD; | |
639 | else if (parity == parity_even) | |
640 | serial_parity = GDBPARITY_EVEN; | |
641 | else | |
642 | serial_parity = GDBPARITY_NONE; | |
643 | } | |
644 | ||
5fe70629 | 645 | INIT_GDB_FILE (serial) |
c906108c SS |
646 | { |
647 | #if 0 | |
1bedd215 AC |
648 | add_com ("connect", class_obscure, connect_command, _("\ |
649 | Connect the terminal directly up to the command monitor.\n\ | |
650 | Use <CR>~. or <CR>~^D to break out.")); | |
c906108c SS |
651 | #endif /* 0 */ |
652 | ||
f54bdb6d SM |
653 | add_setshow_prefix_cmd ("serial", class_maintenance, |
654 | _("Set default serial/parallel port configuration."), | |
655 | _("Show default serial/parallel port configuration."), | |
656 | &serial_set_cmdlist, &serial_show_cmdlist, | |
657 | &setlist, &showlist); | |
e3abfe1d | 658 | |
0d12017b JB |
659 | /* If target is open when baud changes, it doesn't take effect until |
660 | the next open (I think, not sure). */ | |
661 | add_setshow_zinteger_cmd ("baud", no_class, &baud_rate, _("\ | |
662 | Set baud rate for remote serial I/O."), _("\ | |
663 | Show baud rate for remote serial I/O."), _("\ | |
664 | This value is used to set the speed of the serial port when debugging\n\ | |
665 | using remote targets."), | |
666 | NULL, | |
667 | serial_baud_show_cmd, | |
668 | &serial_set_cmdlist, &serial_show_cmdlist); | |
669 | ||
236af5e3 | 670 | add_setshow_enum_cmd ("parity", no_class, parity_enums, |
dda83cd7 | 671 | &parity, _("\ |
590042fc PW |
672 | Set parity for remote serial I/O."), _("\ |
673 | Show parity for remote serial I/O."), NULL, | |
dda83cd7 SM |
674 | set_parity, |
675 | NULL, /* FIXME: i18n: */ | |
676 | &serial_set_cmdlist, &serial_show_cmdlist); | |
236af5e3 | 677 | |
f397e303 AC |
678 | add_setshow_filename_cmd ("remotelogfile", no_class, &serial_logfile, _("\ |
679 | Set filename for remote session recording."), _("\ | |
680 | Show filename for remote session recording."), _("\ | |
c906108c | 681 | This file is used to record the remote session for future playback\n\ |
f397e303 AC |
682 | by gdbserver."), |
683 | NULL, | |
684 | NULL, /* FIXME: i18n: */ | |
685 | &setlist, &showlist); | |
c906108c | 686 | |
7ab04401 AC |
687 | add_setshow_enum_cmd ("remotelogbase", no_class, logbase_enums, |
688 | &serial_logbase, _("\ | |
590042fc PW |
689 | Set numerical base for remote session logging."), _("\ |
690 | Show numerical base for remote session logging."), NULL, | |
7ab04401 AC |
691 | NULL, |
692 | NULL, /* FIXME: i18n: */ | |
693 | &setlist, &showlist); | |
2acceee2 | 694 | |
ccce17b0 YQ |
695 | add_setshow_zuinteger_cmd ("serial", class_maintenance, |
696 | &global_serial_debug_p, _("\ | |
85c07804 AC |
697 | Set serial debugging."), _("\ |
698 | Show serial debugging."), _("\ | |
699 | When non-zero, serial port debugging is enabled."), | |
ccce17b0 YQ |
700 | NULL, |
701 | NULL, /* FIXME: i18n: */ | |
702 | &setdebuglist, &showdebuglist); | |
c906108c | 703 | } |