gdb: Don't reorder line table entries too much when sorting.
[deliverable/binutils-gdb.git] / gdb / ser-mingw.c
CommitLineData
0ea3f30e
DJ
1/* Serial interface for local (hardwired) serial ports on Windows systems
2
b811d2c2 3 Copyright (C) 2006-2020 Free Software Foundation, Inc.
0ea3f30e
DJ
4
5 This file is part of GDB.
6
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
0ea3f30e
DJ
10 (at your option) any later version.
11
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.
16
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/>. */
0ea3f30e
DJ
19
20#include "defs.h"
21#include "serial.h"
22#include "ser-base.h"
23#include "ser-tcp.h"
24
25#include <windows.h>
c3e2b812 26#include <conio.h>
0ea3f30e
DJ
27
28#include <fcntl.h>
29#include <unistd.h>
30#include <sys/types.h>
31
efdb2a86
PA
32#include "command.h"
33
0ea3f30e
DJ
34struct ser_windows_state
35{
36 int in_progress;
37 OVERLAPPED ov;
38 DWORD lastCommMask;
39 HANDLE except_event;
40};
41
4af53f97
PM
42/* CancelIo is not available for Windows 95 OS, so we need to use
43 LoadLibrary/GetProcAddress to avoid a startup failure. */
44#define CancelIo dyn_CancelIo
cd78b7a1
PA
45typedef BOOL WINAPI (CancelIo_ftype) (HANDLE);
46static CancelIo_ftype *CancelIo;
4af53f97 47
0ea3f30e
DJ
48/* Open up a real live device for serial I/O. */
49
50static int
51ser_windows_open (struct serial *scb, const char *name)
52{
53 HANDLE h;
54 struct ser_windows_state *state;
55 COMMTIMEOUTS timeouts;
56
0ea3f30e
DJ
57 h = CreateFile (name, GENERIC_READ | GENERIC_WRITE, 0, NULL,
58 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
59 if (h == INVALID_HANDLE_VALUE)
60 {
61 errno = ENOENT;
62 return -1;
63 }
64
1be7fe8d 65 scb->fd = _open_osfhandle ((intptr_t) h, O_RDWR);
0ea3f30e
DJ
66 if (scb->fd < 0)
67 {
68 errno = ENOENT;
69 return -1;
70 }
71
72 if (!SetCommMask (h, EV_RXCHAR))
73 {
74 errno = EINVAL;
75 return -1;
76 }
77
78 timeouts.ReadIntervalTimeout = MAXDWORD;
79 timeouts.ReadTotalTimeoutConstant = 0;
80 timeouts.ReadTotalTimeoutMultiplier = 0;
81 timeouts.WriteTotalTimeoutConstant = 0;
82 timeouts.WriteTotalTimeoutMultiplier = 0;
83 if (!SetCommTimeouts (h, &timeouts))
84 {
85 errno = EINVAL;
86 return -1;
87 }
88
8d749320 89 state = XCNEW (struct ser_windows_state);
0ea3f30e
DJ
90 scb->state = state;
91
92 /* Create a manual reset event to watch the input buffer. */
93 state->ov.hEvent = CreateEvent (0, TRUE, FALSE, 0);
94
95 /* Create a (currently unused) handle to record exceptions. */
96 state->except_event = CreateEvent (0, TRUE, FALSE, 0);
97
98 return 0;
99}
100
101/* Wait for the output to drain away, as opposed to flushing (discarding)
102 it. */
103
104static int
105ser_windows_drain_output (struct serial *scb)
106{
107 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
108
109 return (FlushFileBuffers (h) != 0) ? 0 : -1;
110}
111
112static int
113ser_windows_flush_output (struct serial *scb)
114{
115 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
116
117 return (PurgeComm (h, PURGE_TXCLEAR) != 0) ? 0 : -1;
118}
119
120static int
121ser_windows_flush_input (struct serial *scb)
122{
123 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
124
125 return (PurgeComm (h, PURGE_RXCLEAR) != 0) ? 0 : -1;
126}
127
128static int
129ser_windows_send_break (struct serial *scb)
130{
131 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
132
133 if (SetCommBreak (h) == 0)
134 return -1;
135
136 /* Delay for 250 milliseconds. */
137 Sleep (250);
138
139 if (ClearCommBreak (h))
140 return -1;
141
142 return 0;
143}
144
145static void
146ser_windows_raw (struct serial *scb)
147{
148 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
149 DCB state;
150
151 if (GetCommState (h, &state) == 0)
152 return;
153
0ea3f30e
DJ
154 state.fOutxCtsFlow = FALSE;
155 state.fOutxDsrFlow = FALSE;
156 state.fDtrControl = DTR_CONTROL_ENABLE;
157 state.fDsrSensitivity = FALSE;
158 state.fOutX = FALSE;
159 state.fInX = FALSE;
160 state.fNull = FALSE;
161 state.fAbortOnError = FALSE;
162 state.ByteSize = 8;
0ea3f30e 163
0ea3f30e 164 if (SetCommState (h, &state) == 0)
b37520b6 165 warning (_("SetCommState failed"));
0ea3f30e
DJ
166}
167
168static int
169ser_windows_setstopbits (struct serial *scb, int num)
170{
171 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
172 DCB state;
173
174 if (GetCommState (h, &state) == 0)
175 return -1;
176
177 switch (num)
178 {
179 case SERIAL_1_STOPBITS:
180 state.StopBits = ONESTOPBIT;
181 break;
182 case SERIAL_1_AND_A_HALF_STOPBITS:
183 state.StopBits = ONE5STOPBITS;
184 break;
185 case SERIAL_2_STOPBITS:
186 state.StopBits = TWOSTOPBITS;
187 break;
188 default:
189 return 1;
190 }
191
192 return (SetCommState (h, &state) != 0) ? 0 : -1;
193}
194
236af5e3
YG
195/* Implement the "setparity" serial_ops callback. */
196
197static int
198ser_windows_setparity (struct serial *scb, int parity)
199{
200 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
201 DCB state;
202
203 if (GetCommState (h, &state) == 0)
204 return -1;
205
206 switch (parity)
207 {
208 case GDBPARITY_NONE:
209 state.Parity = NOPARITY;
210 state.fParity = FALSE;
211 break;
212 case GDBPARITY_ODD:
213 state.Parity = ODDPARITY;
214 state.fParity = TRUE;
215 break;
216 case GDBPARITY_EVEN:
217 state.Parity = EVENPARITY;
218 state.fParity = TRUE;
219 break;
220 default:
221 internal_warning (__FILE__, __LINE__,
8a4506c0 222 "Incorrect parity value: %d", parity);
236af5e3
YG
223 return -1;
224 }
225
226 return (SetCommState (h, &state) != 0) ? 0 : -1;
227}
228
0ea3f30e
DJ
229static int
230ser_windows_setbaudrate (struct serial *scb, int rate)
231{
232 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
233 DCB state;
234
235 if (GetCommState (h, &state) == 0)
236 return -1;
237
238 state.BaudRate = rate;
239
240 return (SetCommState (h, &state) != 0) ? 0 : -1;
241}
242
243static void
244ser_windows_close (struct serial *scb)
245{
246 struct ser_windows_state *state;
247
e25b2cfa
PM
248 /* Stop any pending selects. On Windows 95 OS, CancelIo function does
249 not exist. In that case, it can be replaced by a call to CloseHandle,
250 but this is not necessary here as we do close the Windows handle
251 by calling close (scb->fd) below. */
4af53f97
PM
252 if (CancelIo)
253 CancelIo ((HANDLE) _get_osfhandle (scb->fd));
cd78b7a1 254 state = (struct ser_windows_state *) scb->state;
0ea3f30e
DJ
255 CloseHandle (state->ov.hEvent);
256 CloseHandle (state->except_event);
257
258 if (scb->fd < 0)
259 return;
260
261 close (scb->fd);
262 scb->fd = -1;
263
264 xfree (scb->state);
265}
266
267static void
268ser_windows_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
269{
270 struct ser_windows_state *state;
271 COMSTAT status;
272 DWORD errors;
273 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
274
cd78b7a1 275 state = (struct ser_windows_state *) scb->state;
0ea3f30e
DJ
276
277 *except = state->except_event;
278 *read = state->ov.hEvent;
279
280 if (state->in_progress)
281 return;
282
283 /* Reset the mask - we are only interested in any characters which
284 arrive after this point, not characters which might have arrived
285 and already been read. */
286
287 /* This really, really shouldn't be necessary - just the second one.
288 But otherwise an internal flag for EV_RXCHAR does not get
289 cleared, and we get a duplicated event, if the last batch
290 of characters included at least two arriving close together. */
291 if (!SetCommMask (h, 0))
292 warning (_("ser_windows_wait_handle: reseting mask failed"));
293
294 if (!SetCommMask (h, EV_RXCHAR))
295 warning (_("ser_windows_wait_handle: reseting mask failed (2)"));
296
297 /* There's a potential race condition here; we must check cbInQue
298 and not wait if that's nonzero. */
299
300 ClearCommError (h, &errors, &status);
301 if (status.cbInQue > 0)
302 {
303 SetEvent (state->ov.hEvent);
304 return;
305 }
306
307 state->in_progress = 1;
308 ResetEvent (state->ov.hEvent);
309 state->lastCommMask = -2;
310 if (WaitCommEvent (h, &state->lastCommMask, &state->ov))
311 {
312 gdb_assert (state->lastCommMask & EV_RXCHAR);
313 SetEvent (state->ov.hEvent);
314 }
315 else
316 gdb_assert (GetLastError () == ERROR_IO_PENDING);
317}
318
319static int
320ser_windows_read_prim (struct serial *scb, size_t count)
321{
322 struct ser_windows_state *state;
323 OVERLAPPED ov;
8d49165d 324 DWORD bytes_read;
0ea3f30e 325 HANDLE h;
0ea3f30e 326
cd78b7a1 327 state = (struct ser_windows_state *) scb->state;
0ea3f30e
DJ
328 if (state->in_progress)
329 {
330 WaitForSingleObject (state->ov.hEvent, INFINITE);
331 state->in_progress = 0;
332 ResetEvent (state->ov.hEvent);
333 }
334
335 memset (&ov, 0, sizeof (OVERLAPPED));
336 ov.hEvent = CreateEvent (0, FALSE, FALSE, 0);
337 h = (HANDLE) _get_osfhandle (scb->fd);
338
339 if (!ReadFile (h, scb->buf, /* count */ 1, &bytes_read, &ov))
340 {
341 if (GetLastError () != ERROR_IO_PENDING
342 || !GetOverlappedResult (h, &ov, &bytes_read, TRUE))
343 bytes_read = -1;
344 }
345
346 CloseHandle (ov.hEvent);
347 return bytes_read;
348}
349
350static int
351ser_windows_write_prim (struct serial *scb, const void *buf, size_t len)
352{
0ea3f30e
DJ
353 OVERLAPPED ov;
354 DWORD bytes_written;
355 HANDLE h;
356
357 memset (&ov, 0, sizeof (OVERLAPPED));
358 ov.hEvent = CreateEvent (0, FALSE, FALSE, 0);
359 h = (HANDLE) _get_osfhandle (scb->fd);
360 if (!WriteFile (h, buf, len, &bytes_written, &ov))
361 {
362 if (GetLastError () != ERROR_IO_PENDING
363 || !GetOverlappedResult (h, &ov, &bytes_written, TRUE))
364 bytes_written = -1;
365 }
366
367 CloseHandle (ov.hEvent);
368 return bytes_written;
369}
370
4577549b
DJ
371/* On Windows, gdb_select is implemented using WaitForMulpleObjects.
372 A "select thread" is created for each file descriptor. These
373 threads looks for activity on the corresponding descriptor, using
374 whatever techniques are appropriate for the descriptor type. When
375 that activity occurs, the thread signals an appropriate event,
376 which wakes up WaitForMultipleObjects.
377
378 Each select thread is in one of two states: stopped or started.
379 Select threads begin in the stopped state. When gdb_select is
380 called, threads corresponding to the descriptors of interest are
381 started by calling a wait_handle function. Each thread that
382 notices activity signals the appropriate event and then reenters
383 the stopped state. Before gdb_select returns it calls the
384 wait_handle_done functions, which return the threads to the stopped
385 state. */
386
387enum select_thread_state {
388 STS_STARTED,
389 STS_STOPPED
390};
391
0ea3f30e
DJ
392struct ser_console_state
393{
4577549b
DJ
394 /* Signaled by the select thread to indicate that data is available
395 on the file descriptor. */
0ea3f30e 396 HANDLE read_event;
4577549b
DJ
397 /* Signaled by the select thread to indicate that an exception has
398 occurred on the file descriptor. */
0ea3f30e 399 HANDLE except_event;
4577549b
DJ
400 /* Signaled by the select thread to indicate that it has entered the
401 started state. HAVE_STARTED and HAVE_STOPPED are never signaled
402 simultaneously. */
403 HANDLE have_started;
404 /* Signaled by the select thread to indicate that it has stopped,
405 either because data is available (and READ_EVENT is signaled),
406 because an exception has occurred (and EXCEPT_EVENT is signaled),
407 or because STOP_SELECT was signaled. */
408 HANDLE have_stopped;
0ea3f30e 409
4577549b
DJ
410 /* Signaled by the main program to tell the select thread to enter
411 the started state. */
0ea3f30e 412 HANDLE start_select;
4577549b 413 /* Signaled by the main program to tell the select thread to enter
c378eb4e 414 the stopped state. */
0ea3f30e 415 HANDLE stop_select;
4577549b
DJ
416 /* Signaled by the main program to tell the select thread to
417 exit. */
c3e2b812 418 HANDLE exit_select;
c3e2b812 419
4577549b 420 /* The handle for the select thread. */
c3e2b812 421 HANDLE thread;
4577549b
DJ
422 /* The state of the select thread. This field is only accessed in
423 the main program, never by the select thread itself. */
424 enum select_thread_state thread_state;
0ea3f30e
DJ
425};
426
4577549b
DJ
427/* Called by a select thread to enter the stopped state. This
428 function does not return until the thread has re-entered the
429 started state. */
430static void
431select_thread_wait (struct ser_console_state *state)
432{
433 HANDLE wait_events[2];
434
435 /* There are two things that can wake us up: a request that we enter
436 the started state, or that we exit this thread. */
437 wait_events[0] = state->start_select;
438 wait_events[1] = state->exit_select;
439 if (WaitForMultipleObjects (2, wait_events, FALSE, INFINITE)
440 != WAIT_OBJECT_0)
441 /* Either the EXIT_SELECT event was signaled (requesting that the
442 thread exit) or an error has occurred. In either case, we exit
443 the thread. */
444 ExitThread (0);
445
446 /* We are now in the started state. */
447 SetEvent (state->have_started);
448}
449
450typedef DWORD WINAPI (*thread_fn_type)(void *);
451
452/* Create a new select thread for SCB executing THREAD_FN. The STATE
453 will be filled in by this function before return. */
a95babbf 454static void
4577549b
DJ
455create_select_thread (thread_fn_type thread_fn,
456 struct serial *scb,
457 struct ser_console_state *state)
458{
459 DWORD threadId;
460
461 /* Create all of the events. These are all auto-reset events. */
462 state->read_event = CreateEvent (NULL, FALSE, FALSE, NULL);
463 state->except_event = CreateEvent (NULL, FALSE, FALSE, NULL);
464 state->have_started = CreateEvent (NULL, FALSE, FALSE, NULL);
465 state->have_stopped = CreateEvent (NULL, FALSE, FALSE, NULL);
466 state->start_select = CreateEvent (NULL, FALSE, FALSE, NULL);
467 state->stop_select = CreateEvent (NULL, FALSE, FALSE, NULL);
468 state->exit_select = CreateEvent (NULL, FALSE, FALSE, NULL);
469
470 state->thread = CreateThread (NULL, 0, thread_fn, scb, 0, &threadId);
471 /* The thread begins in the stopped state. */
472 state->thread_state = STS_STOPPED;
473}
474
475/* Destroy the select thread indicated by STATE. */
476static void
477destroy_select_thread (struct ser_console_state *state)
478{
479 /* Ask the thread to exit. */
480 SetEvent (state->exit_select);
481 /* Wait until it does. */
482 WaitForSingleObject (state->thread, INFINITE);
483
484 /* Destroy the events. */
485 CloseHandle (state->read_event);
486 CloseHandle (state->except_event);
487 CloseHandle (state->have_started);
488 CloseHandle (state->have_stopped);
489 CloseHandle (state->start_select);
490 CloseHandle (state->stop_select);
491 CloseHandle (state->exit_select);
492}
493
494/* Called by gdb_select to start the select thread indicated by STATE.
495 This function does not return until the thread has started. */
496static void
497start_select_thread (struct ser_console_state *state)
498{
499 /* Ask the thread to start. */
500 SetEvent (state->start_select);
501 /* Wait until it does. */
502 WaitForSingleObject (state->have_started, INFINITE);
503 /* The thread is now started. */
504 state->thread_state = STS_STARTED;
505}
506
507/* Called by gdb_select to stop the select thread indicated by STATE.
508 This function does not return until the thread has stopped. */
509static void
510stop_select_thread (struct ser_console_state *state)
511{
512 /* If the thread is already in the stopped state, we have nothing to
513 do. Some of the wait_handle functions avoid calling
514 start_select_thread if they notice activity on the relevant file
515 descriptors. The wait_handle_done functions still call
516 stop_select_thread -- but it is already stopped. */
517 if (state->thread_state != STS_STARTED)
518 return;
519 /* Ask the thread to stop. */
520 SetEvent (state->stop_select);
521 /* Wait until it does. */
522 WaitForSingleObject (state->have_stopped, INFINITE);
523 /* The thread is now stopped. */
524 state->thread_state = STS_STOPPED;
525}
526
0ea3f30e
DJ
527static DWORD WINAPI
528console_select_thread (void *arg)
529{
cd78b7a1 530 struct serial *scb = (struct serial *) arg;
c3e2b812
DJ
531 struct ser_console_state *state;
532 int event_index;
0ea3f30e
DJ
533 HANDLE h;
534
cd78b7a1 535 state = (struct ser_console_state *) scb->state;
c3e2b812 536 h = (HANDLE) _get_osfhandle (scb->fd);
0ea3f30e
DJ
537
538 while (1)
539 {
540 HANDLE wait_events[2];
541 INPUT_RECORD record;
542 DWORD n_records;
543
4577549b 544 select_thread_wait (state);
c3e2b812 545
4577549b
DJ
546 while (1)
547 {
548 wait_events[0] = state->stop_select;
549 wait_events[1] = h;
0ea3f30e 550
3e43a32a
MS
551 event_index = WaitForMultipleObjects (2, wait_events,
552 FALSE, INFINITE);
0ea3f30e 553
4577549b
DJ
554 if (event_index == WAIT_OBJECT_0
555 || WaitForSingleObject (state->stop_select, 0) == WAIT_OBJECT_0)
556 break;
0ea3f30e 557
4577549b
DJ
558 if (event_index != WAIT_OBJECT_0 + 1)
559 {
560 /* Wait must have failed; assume an error has occured, e.g.
561 the handle has been closed. */
562 SetEvent (state->except_event);
563 break;
564 }
0ea3f30e 565
4577549b
DJ
566 /* We've got a pending event on the console. See if it's
567 of interest. */
568 if (!PeekConsoleInput (h, &record, 1, &n_records) || n_records != 1)
569 {
570 /* Something went wrong. Maybe the console is gone. */
571 SetEvent (state->except_event);
572 break;
573 }
0ea3f30e 574
4577549b 575 if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown)
c3e2b812 576 {
4577549b
DJ
577 WORD keycode = record.Event.KeyEvent.wVirtualKeyCode;
578
579 /* Ignore events containing only control keys. We must
580 recognize "enhanced" keys which we are interested in
581 reading via getch, if they do not map to ASCII. But we
582 do not want to report input available for e.g. the
583 control key alone. */
584
585 if (record.Event.KeyEvent.uChar.AsciiChar != 0
586 || keycode == VK_PRIOR
587 || keycode == VK_NEXT
588 || keycode == VK_END
589 || keycode == VK_HOME
590 || keycode == VK_LEFT
591 || keycode == VK_UP
592 || keycode == VK_RIGHT
593 || keycode == VK_DOWN
594 || keycode == VK_INSERT
595 || keycode == VK_DELETE)
596 {
597 /* This is really a keypress. */
598 SetEvent (state->read_event);
599 break;
600 }
c3e2b812 601 }
4577549b
DJ
602
603 /* Otherwise discard it and wait again. */
604 ReadConsoleInput (h, &record, 1, &n_records);
0ea3f30e
DJ
605 }
606
4577549b 607 SetEvent(state->have_stopped);
0ea3f30e 608 }
a32d7317 609 return 0;
0ea3f30e
DJ
610}
611
612static int
613fd_is_pipe (int fd)
614{
615 if (PeekNamedPipe ((HANDLE) _get_osfhandle (fd), NULL, 0, NULL, NULL, NULL))
616 return 1;
617 else
618 return 0;
619}
620
7e71daaa
JG
621static int
622fd_is_file (int fd)
623{
624 if (GetFileType ((HANDLE) _get_osfhandle (fd)) == FILE_TYPE_DISK)
625 return 1;
626 else
627 return 0;
628}
629
0ea3f30e
DJ
630static DWORD WINAPI
631pipe_select_thread (void *arg)
632{
cd78b7a1 633 struct serial *scb = (struct serial *) arg;
c3e2b812 634 struct ser_console_state *state;
0ea3f30e
DJ
635 HANDLE h;
636
cd78b7a1 637 state = (struct ser_console_state *) scb->state;
c3e2b812 638 h = (HANDLE) _get_osfhandle (scb->fd);
0ea3f30e
DJ
639
640 while (1)
641 {
0ea3f30e
DJ
642 DWORD n_avail;
643
4577549b 644 select_thread_wait (state);
c3e2b812 645
4577549b
DJ
646 /* Wait for something to happen on the pipe. */
647 while (1)
0ea3f30e 648 {
4577549b
DJ
649 if (!PeekNamedPipe (h, NULL, 0, NULL, &n_avail, NULL))
650 {
651 SetEvent (state->except_event);
652 break;
653 }
0ea3f30e 654
4577549b
DJ
655 if (n_avail > 0)
656 {
657 SetEvent (state->read_event);
658 break;
659 }
0ea3f30e 660
4577549b
DJ
661 /* Delay 10ms before checking again, but allow the stop
662 event to wake us. */
663 if (WaitForSingleObject (state->stop_select, 10) == WAIT_OBJECT_0)
664 break;
665 }
0ea3f30e 666
4577549b 667 SetEvent (state->have_stopped);
0ea3f30e 668 }
a32d7317 669 return 0;
0ea3f30e
DJ
670}
671
7e71daaa
JG
672static DWORD WINAPI
673file_select_thread (void *arg)
674{
cd78b7a1 675 struct serial *scb = (struct serial *) arg;
7e71daaa 676 struct ser_console_state *state;
7e71daaa
JG
677 HANDLE h;
678
cd78b7a1 679 state = (struct ser_console_state *) scb->state;
7e71daaa
JG
680 h = (HANDLE) _get_osfhandle (scb->fd);
681
682 while (1)
683 {
4577549b 684 select_thread_wait (state);
7e71daaa 685
3e43a32a
MS
686 if (SetFilePointer (h, 0, NULL, FILE_CURRENT)
687 == INVALID_SET_FILE_POINTER)
4577549b
DJ
688 SetEvent (state->except_event);
689 else
690 SetEvent (state->read_event);
7e71daaa 691
4577549b 692 SetEvent (state->have_stopped);
7e71daaa 693 }
a32d7317 694 return 0;
7e71daaa
JG
695}
696
0ea3f30e
DJ
697static void
698ser_console_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
699{
cd78b7a1 700 struct ser_console_state *state = (struct ser_console_state *) scb->state;
0ea3f30e
DJ
701
702 if (state == NULL)
703 {
4577549b 704 thread_fn_type thread_fn;
0ea3f30e
DJ
705 int is_tty;
706
707 is_tty = isatty (scb->fd);
7e71daaa 708 if (!is_tty && !fd_is_file (scb->fd) && !fd_is_pipe (scb->fd))
0ea3f30e
DJ
709 {
710 *read = NULL;
711 *except = NULL;
712 return;
713 }
714
8d749320 715 state = XCNEW (struct ser_console_state);
0ea3f30e
DJ
716 scb->state = state;
717
0ea3f30e 718 if (is_tty)
4577549b 719 thread_fn = console_select_thread;
7e71daaa 720 else if (fd_is_pipe (scb->fd))
4577549b 721 thread_fn = pipe_select_thread;
7e71daaa 722 else
4577549b
DJ
723 thread_fn = file_select_thread;
724
725 create_select_thread (thread_fn, scb, state);
0ea3f30e
DJ
726 }
727
c3e2b812
DJ
728 *read = state->read_event;
729 *except = state->except_event;
730
731 /* Start from a blank state. */
0ea3f30e
DJ
732 ResetEvent (state->read_event);
733 ResetEvent (state->except_event);
c3e2b812
DJ
734 ResetEvent (state->stop_select);
735
736 /* First check for a key already in the buffer. If there is one,
737 we don't need a thread. This also catches the second key of
738 multi-character returns from getch, for instance for arrow
739 keys. The second half is in a C library internal buffer,
740 and PeekConsoleInput will not find it. */
741 if (_kbhit ())
742 {
743 SetEvent (state->read_event);
744 return;
745 }
0ea3f30e 746
c3e2b812 747 /* Otherwise, start the select thread. */
4577549b 748 start_select_thread (state);
c3e2b812 749}
0ea3f30e 750
c3e2b812
DJ
751static void
752ser_console_done_wait_handle (struct serial *scb)
753{
cd78b7a1 754 struct ser_console_state *state = (struct ser_console_state *) scb->state;
c3e2b812
DJ
755
756 if (state == NULL)
757 return;
758
4577549b 759 stop_select_thread (state);
0ea3f30e
DJ
760}
761
762static void
763ser_console_close (struct serial *scb)
764{
cd78b7a1 765 struct ser_console_state *state = (struct ser_console_state *) scb->state;
0ea3f30e
DJ
766
767 if (scb->state)
768 {
4577549b 769 destroy_select_thread (state);
0ea3f30e
DJ
770 xfree (scb->state);
771 }
772}
773
774struct ser_console_ttystate
775{
776 int is_a_tty;
777};
778
779static serial_ttystate
780ser_console_get_tty_state (struct serial *scb)
781{
782 if (isatty (scb->fd))
783 {
784 struct ser_console_ttystate *state;
433759f7 785
8d749320 786 state = XNEW (struct ser_console_ttystate);
0ea3f30e
DJ
787 state->is_a_tty = 1;
788 return state;
789 }
790 else
791 return NULL;
792}
793
5f1fb6dc
JB
794struct pipe_state
795{
796 /* Since we use the pipe_select_thread for our select emulation,
797 we need to place the state structure it requires at the front
798 of our state. */
799 struct ser_console_state wait;
800
801 /* The pex obj for our (one-stage) pipeline. */
802 struct pex_obj *pex;
803
804 /* Streams for the pipeline's input and output. */
805 FILE *input, *output;
806};
807
808static struct pipe_state *
809make_pipe_state (void)
810{
8d749320 811 struct pipe_state *ps = XCNEW (struct pipe_state);
5f1fb6dc 812
5f1fb6dc
JB
813 ps->wait.read_event = INVALID_HANDLE_VALUE;
814 ps->wait.except_event = INVALID_HANDLE_VALUE;
815 ps->wait.start_select = INVALID_HANDLE_VALUE;
816 ps->wait.stop_select = INVALID_HANDLE_VALUE;
817
818 return ps;
819}
820
821static void
822free_pipe_state (struct pipe_state *ps)
823{
824 int saved_errno = errno;
825
826 if (ps->wait.read_event != INVALID_HANDLE_VALUE)
4577549b 827 destroy_select_thread (&ps->wait);
5f1fb6dc
JB
828
829 /* Close the pipe to the child. We must close the pipe before
830 calling pex_free because pex_free will wait for the child to exit
831 and the child will not exit until the pipe is closed. */
832 if (ps->input)
833 fclose (ps->input);
834 if (ps->pex)
58f07bae
PA
835 {
836 pex_free (ps->pex);
837 /* pex_free closes ps->output. */
838 }
839 else if (ps->output)
840 fclose (ps->output);
5f1fb6dc
JB
841
842 xfree (ps);
843
844 errno = saved_errno;
845}
846
3f0dbd67 847struct pipe_state_destroyer
5f1fb6dc 848{
3f0dbd67
TT
849 void operator() (pipe_state *ps) const
850 {
851 free_pipe_state (ps);
852 }
853};
5f1fb6dc 854
3f0dbd67 855typedef std::unique_ptr<pipe_state, pipe_state_destroyer> pipe_state_up;
5f1fb6dc
JB
856
857static int
858pipe_windows_open (struct serial *scb, const char *name)
859{
65cc4390 860 FILE *pex_stderr;
ef7723eb 861
d1a41061
PP
862 if (name == NULL)
863 error_no_arg (_("child command"));
864
773a1edc 865 gdb_argv argv (name);
d1a41061 866
5f1fb6dc 867 if (! argv[0] || argv[0][0] == '\0')
a73c6dcd 868 error (_("missing child command"));
5f1fb6dc 869
3f0dbd67 870 pipe_state_up ps (make_pipe_state ());
5f1fb6dc
JB
871
872 ps->pex = pex_init (PEX_USE_PIPES, "target remote pipe", NULL);
873 if (! ps->pex)
3f0dbd67 874 return -1;
5f1fb6dc
JB
875 ps->input = pex_input_pipe (ps->pex, 1);
876 if (! ps->input)
3f0dbd67 877 return -1;
5f1fb6dc
JB
878
879 {
880 int err;
881 const char *err_msg
65cc4390
VP
882 = pex_run (ps->pex, PEX_SEARCH | PEX_BINARY_INPUT | PEX_BINARY_OUTPUT
883 | PEX_STDERR_TO_PIPE,
773a1edc 884 argv[0], argv.get (), NULL, NULL,
5f1fb6dc
JB
885 &err);
886
887 if (err_msg)
888 {
889 /* Our caller expects us to return -1, but all they'll do with
890 it generally is print the message based on errno. We have
891 all the same information here, plus err_msg provided by
892 pex_run, so we just raise the error here. */
893 if (err)
a73c6dcd 894 error (_("error starting child process '%s': %s: %s"),
5f1fb6dc
JB
895 name, err_msg, safe_strerror (err));
896 else
a73c6dcd 897 error (_("error starting child process '%s': %s"),
5f1fb6dc
JB
898 name, err_msg);
899 }
900 }
901
902 ps->output = pex_read_output (ps->pex, 1);
903 if (! ps->output)
3f0dbd67 904 return -1;
5f1fb6dc 905 scb->fd = fileno (ps->output);
65cc4390
VP
906
907 pex_stderr = pex_read_err (ps->pex, 1);
908 if (! pex_stderr)
3f0dbd67 909 return -1;
65cc4390
VP
910 scb->error_fd = fileno (pex_stderr);
911
3f0dbd67 912 scb->state = ps.release ();
5f1fb6dc 913
5f1fb6dc 914 return 0;
5f1fb6dc
JB
915}
916
58f07bae
PA
917static int
918pipe_windows_fdopen (struct serial *scb, int fd)
919{
920 struct pipe_state *ps;
921
922 ps = make_pipe_state ();
923
924 ps->input = fdopen (fd, "r+");
925 if (! ps->input)
926 goto fail;
927
928 ps->output = fdopen (fd, "r+");
929 if (! ps->output)
930 goto fail;
931
932 scb->fd = fd;
933 scb->state = (void *) ps;
934
935 return 0;
936
937 fail:
938 free_pipe_state (ps);
939 return -1;
940}
5f1fb6dc
JB
941
942static void
943pipe_windows_close (struct serial *scb)
944{
cd78b7a1 945 struct pipe_state *ps = (struct pipe_state *) scb->state;
5f1fb6dc
JB
946
947 /* In theory, we should try to kill the subprocess here, but the pex
948 interface doesn't give us enough information to do that. Usually
949 closing the input pipe will get the message across. */
950
951 free_pipe_state (ps);
952}
953
954
955static int
956pipe_windows_read (struct serial *scb, size_t count)
957{
4998c1df 958 HANDLE pipeline_out = (HANDLE) _get_osfhandle (scb->fd);
ef7723eb
VP
959 DWORD available;
960 DWORD bytes_read;
961
5f1fb6dc
JB
962 if (pipeline_out == INVALID_HANDLE_VALUE)
963 return -1;
964
5f1fb6dc
JB
965 if (! PeekNamedPipe (pipeline_out, NULL, 0, NULL, &available, NULL))
966 return -1;
967
968 if (count > available)
969 count = available;
970
5f1fb6dc
JB
971 if (! ReadFile (pipeline_out, scb->buf, count, &bytes_read, NULL))
972 return -1;
973
974 return bytes_read;
975}
976
977
978static int
979pipe_windows_write (struct serial *scb, const void *buf, size_t count)
980{
cd78b7a1 981 struct pipe_state *ps = (struct pipe_state *) scb->state;
ef7723eb
VP
982 HANDLE pipeline_in;
983 DWORD written;
984
5f1fb6dc
JB
985 int pipeline_in_fd = fileno (ps->input);
986 if (pipeline_in_fd < 0)
987 return -1;
988
ef7723eb 989 pipeline_in = (HANDLE) _get_osfhandle (pipeline_in_fd);
5f1fb6dc
JB
990 if (pipeline_in == INVALID_HANDLE_VALUE)
991 return -1;
992
5f1fb6dc
JB
993 if (! WriteFile (pipeline_in, buf, count, &written, NULL))
994 return -1;
995
996 return written;
997}
998
999
1000static void
1001pipe_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
1002{
cd78b7a1 1003 struct pipe_state *ps = (struct pipe_state *) scb->state;
5f1fb6dc
JB
1004
1005 /* Have we allocated our events yet? */
1006 if (ps->wait.read_event == INVALID_HANDLE_VALUE)
4577549b
DJ
1007 /* Start the thread. */
1008 create_select_thread (pipe_select_thread, scb, &ps->wait);
5f1fb6dc 1009
774a49c0
DJ
1010 *read = ps->wait.read_event;
1011 *except = ps->wait.except_event;
1012
1013 /* Start from a blank state. */
5f1fb6dc
JB
1014 ResetEvent (ps->wait.read_event);
1015 ResetEvent (ps->wait.except_event);
774a49c0 1016 ResetEvent (ps->wait.stop_select);
5f1fb6dc 1017
4577549b 1018 start_select_thread (&ps->wait);
5f1fb6dc
JB
1019}
1020
774a49c0
DJ
1021static void
1022pipe_done_wait_handle (struct serial *scb)
1023{
cd78b7a1 1024 struct pipe_state *ps = (struct pipe_state *) scb->state;
774a49c0
DJ
1025
1026 /* Have we allocated our events yet? */
1027 if (ps->wait.read_event == INVALID_HANDLE_VALUE)
1028 return;
1029
4577549b 1030 stop_select_thread (&ps->wait);
774a49c0 1031}
5f1fb6dc 1032
65cc4390
VP
1033static int
1034pipe_avail (struct serial *scb, int fd)
1035{
1036 HANDLE h = (HANDLE) _get_osfhandle (fd);
1037 DWORD numBytes;
1038 BOOL r = PeekNamedPipe (h, NULL, 0, NULL, &numBytes, NULL);
433759f7 1039
65cc4390
VP
1040 if (r == FALSE)
1041 numBytes = 0;
1042 return numBytes;
1043}
1044
58f07bae
PA
1045int
1046gdb_pipe (int pdes[2])
1047{
1048 if (_pipe (pdes, 512, _O_BINARY | _O_NOINHERIT) == -1)
1049 return -1;
1050 return 0;
1051}
1052
0ea3f30e
DJ
1053struct net_windows_state
1054{
4577549b
DJ
1055 struct ser_console_state base;
1056
0ea3f30e
DJ
1057 HANDLE sock_event;
1058};
1059
364fe1f7
PA
1060/* Check whether the socket has any pending data to be read. If so,
1061 set the select thread's read event. On error, set the select
1062 thread's except event. If any event was set, return true,
1063 otherwise return false. */
1064
1065static int
1066net_windows_socket_check_pending (struct serial *scb)
1067{
cd78b7a1 1068 struct net_windows_state *state = (struct net_windows_state *) scb->state;
364fe1f7
PA
1069 unsigned long available;
1070
1071 if (ioctlsocket (scb->fd, FIONREAD, &available) != 0)
1072 {
1073 /* The socket closed, or some other error. */
1074 SetEvent (state->base.except_event);
1075 return 1;
1076 }
1077 else if (available > 0)
1078 {
1079 SetEvent (state->base.read_event);
1080 return 1;
1081 }
1082
1083 return 0;
1084}
1085
0ea3f30e
DJ
1086static DWORD WINAPI
1087net_windows_select_thread (void *arg)
1088{
cd78b7a1 1089 struct serial *scb = (struct serial *) arg;
4577549b 1090 struct net_windows_state *state;
c3e2b812 1091 int event_index;
0ea3f30e 1092
cd78b7a1 1093 state = (struct net_windows_state *) scb->state;
0ea3f30e
DJ
1094
1095 while (1)
1096 {
1097 HANDLE wait_events[2];
1098 WSANETWORKEVENTS events;
1099
4577549b 1100 select_thread_wait (&state->base);
c3e2b812 1101
4577549b 1102 wait_events[0] = state->base.stop_select;
0ea3f30e
DJ
1103 wait_events[1] = state->sock_event;
1104
364fe1f7
PA
1105 /* Wait for something to happen on the socket. */
1106 while (1)
0ea3f30e 1107 {
364fe1f7
PA
1108 event_index = WaitForMultipleObjects (2, wait_events, FALSE, INFINITE);
1109
1110 if (event_index == WAIT_OBJECT_0
1111 || WaitForSingleObject (state->base.stop_select, 0) == WAIT_OBJECT_0)
1112 {
1113 /* We have been requested to stop. */
1114 break;
1115 }
1116
1117 if (event_index != WAIT_OBJECT_0 + 1)
1118 {
1119 /* Some error has occured. Assume that this is an error
1120 condition. */
1121 SetEvent (state->base.except_event);
1122 break;
1123 }
1124
4577549b
DJ
1125 /* Enumerate the internal network events, and reset the
1126 object that signalled us to catch the next event. */
364fe1f7
PA
1127 if (WSAEnumNetworkEvents (scb->fd, state->sock_event, &events) != 0)
1128 {
1129 /* Something went wrong. Maybe the socket is gone. */
1130 SetEvent (state->base.except_event);
1131 break;
1132 }
1133
4577549b 1134 if (events.lNetworkEvents & FD_READ)
364fe1f7
PA
1135 {
1136 if (net_windows_socket_check_pending (scb))
1137 break;
1138
1139 /* Spurious wakeup. That is, the socket's event was
1140 signalled before we last called recv. */
1141 }
1142
4577549b 1143 if (events.lNetworkEvents & FD_CLOSE)
364fe1f7
PA
1144 {
1145 SetEvent (state->base.except_event);
1146 break;
1147 }
0ea3f30e
DJ
1148 }
1149
4577549b 1150 SetEvent (state->base.have_stopped);
0ea3f30e 1151 }
364fe1f7 1152 return 0;
0ea3f30e
DJ
1153}
1154
1155static void
1156net_windows_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
1157{
cd78b7a1 1158 struct net_windows_state *state = (struct net_windows_state *) scb->state;
0ea3f30e 1159
c3e2b812 1160 /* Start from a clean slate. */
4577549b
DJ
1161 ResetEvent (state->base.read_event);
1162 ResetEvent (state->base.except_event);
1163 ResetEvent (state->base.stop_select);
0ea3f30e 1164
4577549b
DJ
1165 *read = state->base.read_event;
1166 *except = state->base.except_event;
c3e2b812 1167
364fe1f7
PA
1168 /* Check any pending events. Otherwise, start the select
1169 thread. */
1170 if (!net_windows_socket_check_pending (scb))
1171 start_select_thread (&state->base);
c3e2b812
DJ
1172}
1173
1174static void
1175net_windows_done_wait_handle (struct serial *scb)
1176{
cd78b7a1 1177 struct net_windows_state *state = (struct net_windows_state *) scb->state;
c3e2b812 1178
4577549b 1179 stop_select_thread (&state->base);
0ea3f30e
DJ
1180}
1181
1182static int
1183net_windows_open (struct serial *scb, const char *name)
1184{
1185 struct net_windows_state *state;
1186 int ret;
0ea3f30e
DJ
1187
1188 ret = net_open (scb, name);
1189 if (ret != 0)
1190 return ret;
1191
8d749320 1192 state = XCNEW (struct net_windows_state);
0ea3f30e
DJ
1193 scb->state = state;
1194
0ea3f30e
DJ
1195 /* Associate an event with the socket. */
1196 state->sock_event = CreateEvent (0, TRUE, FALSE, 0);
1197 WSAEventSelect (scb->fd, state->sock_event, FD_READ | FD_CLOSE);
1198
4577549b
DJ
1199 /* Start the thread. */
1200 create_select_thread (net_windows_select_thread, scb, &state->base);
0ea3f30e
DJ
1201
1202 return 0;
1203}
1204
1205
1206static void
1207net_windows_close (struct serial *scb)
1208{
cd78b7a1 1209 struct net_windows_state *state = (struct net_windows_state *) scb->state;
0ea3f30e 1210
4577549b 1211 destroy_select_thread (&state->base);
0ea3f30e
DJ
1212 CloseHandle (state->sock_event);
1213
1214 xfree (scb->state);
1215
1216 net_close (scb);
1217}
1218
12e8c7d7
TT
1219/* The serial port driver. */
1220
1221static const struct serial_ops hardwire_ops =
1222{
1223 "hardwire",
1224 ser_windows_open,
1225 ser_windows_close,
1226 NULL,
1227 ser_base_readchar,
1228 ser_base_write,
1229 ser_windows_flush_output,
1230 ser_windows_flush_input,
1231 ser_windows_send_break,
1232 ser_windows_raw,
1233 /* These are only used for stdin; we do not need them for serial
1234 ports, so supply the standard dummies. */
1235 ser_base_get_tty_state,
1236 ser_base_copy_tty_state,
1237 ser_base_set_tty_state,
1238 ser_base_print_tty_state,
12e8c7d7
TT
1239 ser_windows_setbaudrate,
1240 ser_windows_setstopbits,
236af5e3 1241 ser_windows_setparity,
12e8c7d7
TT
1242 ser_windows_drain_output,
1243 ser_base_async,
1244 ser_windows_read_prim,
1245 ser_windows_write_prim,
1246 NULL,
1247 ser_windows_wait_handle
1248};
1249
1250/* The dummy serial driver used for terminals. We only provide the
1251 TTY-related methods. */
1252
1253static const struct serial_ops tty_ops =
1254{
1255 "terminal",
1256 NULL,
1257 ser_console_close,
1258 NULL,
1259 NULL,
1260 NULL,
1261 NULL,
1262 NULL,
1263 NULL,
1264 NULL,
1265 ser_console_get_tty_state,
1266 ser_base_copy_tty_state,
1267 ser_base_set_tty_state,
1268 ser_base_print_tty_state,
12e8c7d7
TT
1269 NULL,
1270 NULL,
236af5e3 1271 NULL,
12e8c7d7
TT
1272 ser_base_drain_output,
1273 NULL,
1274 NULL,
1275 NULL,
1276 NULL,
1277 ser_console_wait_handle,
1278 ser_console_done_wait_handle
1279};
1280
1281/* The pipe interface. */
1282
1283static const struct serial_ops pipe_ops =
1284{
1285 "pipe",
1286 pipe_windows_open,
1287 pipe_windows_close,
1288 pipe_windows_fdopen,
1289 ser_base_readchar,
1290 ser_base_write,
1291 ser_base_flush_output,
1292 ser_base_flush_input,
1293 ser_base_send_break,
1294 ser_base_raw,
1295 ser_base_get_tty_state,
1296 ser_base_copy_tty_state,
1297 ser_base_set_tty_state,
1298 ser_base_print_tty_state,
12e8c7d7
TT
1299 ser_base_setbaudrate,
1300 ser_base_setstopbits,
236af5e3 1301 ser_base_setparity,
12e8c7d7
TT
1302 ser_base_drain_output,
1303 ser_base_async,
1304 pipe_windows_read,
1305 pipe_windows_write,
1306 pipe_avail,
1307 pipe_wait_handle,
1308 pipe_done_wait_handle
1309};
1310
1311/* The TCP/UDP socket driver. */
1312
1313static const struct serial_ops tcp_ops =
1314{
1315 "tcp",
1316 net_windows_open,
1317 net_windows_close,
1318 NULL,
1319 ser_base_readchar,
1320 ser_base_write,
1321 ser_base_flush_output,
1322 ser_base_flush_input,
1323 ser_tcp_send_break,
1324 ser_base_raw,
1325 ser_base_get_tty_state,
1326 ser_base_copy_tty_state,
1327 ser_base_set_tty_state,
1328 ser_base_print_tty_state,
12e8c7d7
TT
1329 ser_base_setbaudrate,
1330 ser_base_setstopbits,
236af5e3 1331 ser_base_setparity,
12e8c7d7
TT
1332 ser_base_drain_output,
1333 ser_base_async,
1334 net_read_prim,
1335 net_write_prim,
1336 NULL,
1337 net_windows_wait_handle,
1338 net_windows_done_wait_handle
1339};
1340
6c265988 1341void _initialize_ser_windows ();
0ea3f30e 1342void
6c265988 1343_initialize_ser_windows ()
0ea3f30e
DJ
1344{
1345 WSADATA wsa_data;
0ea3f30e 1346
4af53f97
PM
1347 HMODULE hm = NULL;
1348
1349 /* First find out if kernel32 exports CancelIo function. */
1350 hm = LoadLibrary ("kernel32.dll");
1351 if (hm)
1352 {
cd78b7a1 1353 CancelIo = (CancelIo_ftype *) GetProcAddress (hm, "CancelIo");
4af53f97
PM
1354 FreeLibrary (hm);
1355 }
1356 else
1357 CancelIo = NULL;
0ea3f30e 1358
12e8c7d7
TT
1359 serial_add_interface (&hardwire_ops);
1360 serial_add_interface (&tty_ops);
1361 serial_add_interface (&pipe_ops);
5f1fb6dc 1362
0ea3f30e
DJ
1363 /* If WinSock works, register the TCP/UDP socket driver. */
1364
1365 if (WSAStartup (MAKEWORD (1, 0), &wsa_data) != 0)
1366 /* WinSock is unavailable. */
1367 return;
1368
12e8c7d7 1369 serial_add_interface (&tcp_ops);
0ea3f30e 1370}
This page took 1.499569 seconds and 4 git commands to generate.