]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ser-event.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / ser-event.c
CommitLineData
00340e1b 1/* Serial interface for a selectable event.
1d506c26 2 Copyright (C) 2016-2024 Free Software Foundation, Inc.
00340e1b
PA
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19#include "defs.h"
20#include "ser-event.h"
21#include "serial.h"
268a13a5 22#include "gdbsupport/filestuff.h"
00340e1b
PA
23
24/* On POSIX hosts, a serial_event is basically an abstraction for the
25 classical self-pipe trick.
26
27 On Windows, a serial_event is a wrapper around a native Windows
28 event object. Because we want to interface with gdb_select, which
29 takes file descriptors, we need to wrap that Windows event object
30 in a file descriptor. As _open_osfhandle can not be used with
31 event objects, we instead create a dummy file wrap that in a file
32 descriptor with _open_osfhandle, and pass that as selectable
33 descriptor to callers. As Windows' gdb_select converts file
34 descriptors back to Windows handles by calling serial->wait_handle,
35 nothing ever actually waits on that file descriptor. */
36
37struct serial_event_state
38 {
39#ifdef USE_WIN32API
40 /* The Windows event object, created with CreateEvent. */
41 HANDLE event;
42#else
43 /* The write side of the pipe. The read side is in
44 serial->fd. */
45 int write_fd;
46#endif
47 };
48
49/* Open a new serial event. */
50
a2e0acea 51static void
00340e1b
PA
52serial_event_open (struct serial *scb, const char *name)
53{
54 struct serial_event_state *state;
55
56 state = XNEW (struct serial_event_state);
57 scb->state = state;
58
59#ifndef USE_WIN32API
60 {
61 int fds[2];
62
63 if (gdb_pipe_cloexec (fds) == -1)
f34652de 64 internal_error ("creating serial event pipe failed.");
00340e1b
PA
65
66 fcntl (fds[0], F_SETFL, O_NONBLOCK);
67 fcntl (fds[1], F_SETFL, O_NONBLOCK);
68
69 scb->fd = fds[0];
70 state->write_fd = fds[1];
71 }
72#else
73 {
74 /* A dummy file object that can be wrapped in a file descriptor.
75 We don't need to store this handle because closing the file
76 descriptor automatically closes this. */
77 HANDLE dummy_file;
78
79 /* A manual-reset event. */
80 state->event = CreateEvent (0, TRUE, FALSE, 0);
81
82 /* The dummy file handle. Created just so we have something
83 wrappable in a file descriptor. */
84 dummy_file = CreateFile ("nul", 0, 0, NULL, OPEN_EXISTING, 0, NULL);
85 scb->fd = _open_osfhandle ((intptr_t) dummy_file, 0);
86 }
87#endif
00340e1b
PA
88}
89
90static void
91serial_event_close (struct serial *scb)
92{
93 struct serial_event_state *state = (struct serial_event_state *) scb->state;
94
95 close (scb->fd);
96#ifndef USE_WIN32API
97 close (state->write_fd);
98#else
99 CloseHandle (state->event);
100#endif
101
102 scb->fd = -1;
103
104 xfree (state);
105 scb->state = NULL;
106}
107
108#ifdef USE_WIN32API
109
110/* Implementation of the wait_handle method. Returns the native
111 Windows event object handle. */
112
113static void
114serial_event_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
115{
116 struct serial_event_state *state = (struct serial_event_state *) scb->state;
117
118 *read = state->event;
119}
120
121#endif
122
123/* The serial_ops for struct serial_event objects. Note we never
124 register this serial type with serial_add_interface, because this
125 is internal implementation detail never to be used by remote
126 targets for protocol transport. */
127
128static const struct serial_ops serial_event_ops =
129{
130 "event",
131 serial_event_open,
132 serial_event_close,
133 NULL, /* fdopen */
134 NULL, /* readchar */
135 NULL, /* write */
136 NULL, /* flush_output */
137 NULL, /* flush_input */
138 NULL, /* send_break */
139 NULL, /* go_raw */
140 NULL, /* get_tty_state */
141 NULL, /* copy_tty_state */
142 NULL, /* set_tty_state */
143 NULL, /* print_tty_state */
00340e1b
PA
144 NULL, /* setbaudrate */
145 NULL, /* setstopbits */
146 NULL, /* setparity */
147 NULL, /* drain_output */
148 NULL, /* async */
149 NULL, /* read_prim */
150 NULL, /* write_prim */
151 NULL, /* avail */
152#ifdef USE_WIN32API
153 serial_event_wait_handle,
154#endif
155};
156
157/* See ser-event.h. */
158
159struct serial_event *
160make_serial_event (void)
161{
162 return (struct serial_event *) serial_open_ops (&serial_event_ops);
163}
164
165/* See ser-event.h. */
166
167int
168serial_event_fd (struct serial_event *event)
169{
170 struct serial *ser = (struct serial *) event;
171
172 return ser->fd;
173}
174
175/* See ser-event.h. */
176
177void
178serial_event_set (struct serial_event *event)
179{
180 struct serial *ser = (struct serial *) event;
181 struct serial_event_state *state = (struct serial_event_state *) ser->state;
182#ifndef USE_WIN32API
183 int r;
184 char c = '+'; /* Anything. */
185
186 do
187 {
188 r = write (state->write_fd, &c, 1);
189 }
190 while (r < 0 && errno == EINTR);
191#else
192 SetEvent (state->event);
193#endif
194}
195
196/* See ser-event.h. */
197
198void
199serial_event_clear (struct serial_event *event)
200{
201 struct serial *ser = (struct serial *) event;
00340e1b
PA
202#ifndef USE_WIN32API
203 int r;
204
205 do
206 {
207 char c;
208
209 r = read (ser->fd, &c, 1);
210 }
211 while (r > 0 || (r < 0 && errno == EINTR));
212#else
798a7429 213 struct serial_event_state *state = (struct serial_event_state *) ser->state;
00340e1b
PA
214 ResetEvent (state->event);
215#endif
216}