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