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