Support ymm rounding control for Intel AVX10.2
[binutils-gdb.git] / gdbserver / hostio.cc
blobcc47d6869d3b0a0ee86908e71bae121abf4395b1
1 /* Host file transfer support for gdbserver.
2 Copyright (C) 2007-2024 Free Software Foundation, Inc.
4 Contributed by CodeSourcery.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "gdbsupport/fileio.h"
22 #include "hostio.h"
24 #include <fcntl.h>
25 #include <limits.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include "gdbsupport/fileio.h"
31 struct fd_list
33 int fd;
34 struct fd_list *next;
37 static struct fd_list *open_fds;
39 static int
40 safe_fromhex (char a, int *nibble)
42 if (a >= '0' && a <= '9')
43 *nibble = a - '0';
44 else if (a >= 'a' && a <= 'f')
45 *nibble = a - 'a' + 10;
46 else if (a >= 'A' && a <= 'F')
47 *nibble = a - 'A' + 10;
48 else
49 return -1;
51 return 0;
54 /* Filenames are hex encoded, so the maximum we can handle is half the
55 packet buffer size. Cap to PATH_MAX, if it is shorter. */
56 #if !defined (PATH_MAX) || (PATH_MAX > (PBUFSIZ / 2 + 1))
57 # define HOSTIO_PATH_MAX (PBUFSIZ / 2 + 1)
58 #else
59 # define HOSTIO_PATH_MAX PATH_MAX
60 #endif
62 static int
63 require_filename (char **pp, char *filename)
65 int count;
66 char *p;
68 p = *pp;
69 count = 0;
71 while (*p && *p != ',')
73 int nib1, nib2;
75 /* Don't allow overflow. */
76 if (count >= HOSTIO_PATH_MAX - 1)
77 return -1;
79 if (safe_fromhex (p[0], &nib1)
80 || safe_fromhex (p[1], &nib2))
81 return -1;
83 filename[count++] = nib1 * 16 + nib2;
84 p += 2;
87 filename[count] = '\0';
88 *pp = p;
89 return 0;
92 static int
93 require_int (char **pp, int *value)
95 char *p;
96 int count, firstdigit;
98 p = *pp;
99 *value = 0;
100 count = 0;
101 firstdigit = -1;
103 while (*p && *p != ',')
105 int nib;
107 if (safe_fromhex (p[0], &nib))
108 return -1;
110 if (firstdigit == -1)
111 firstdigit = nib;
113 /* Don't allow overflow. */
114 if (count >= 8 || (count == 7 && firstdigit >= 0x8))
115 return -1;
117 *value = *value * 16 + nib;
118 p++;
119 count++;
122 *pp = p;
123 return 0;
126 static int
127 require_data (char *p, int p_len, char **data, int *data_len)
129 int input_index, output_index, escaped;
131 *data = (char *) xmalloc (p_len);
133 output_index = 0;
134 escaped = 0;
135 for (input_index = 0; input_index < p_len; input_index++)
137 char b = p[input_index];
139 if (escaped)
141 (*data)[output_index++] = b ^ 0x20;
142 escaped = 0;
144 else if (b == '}')
145 escaped = 1;
146 else
147 (*data)[output_index++] = b;
150 if (escaped)
152 free (*data);
153 return -1;
156 *data_len = output_index;
157 return 0;
160 static int
161 require_comma (char **pp)
163 if (**pp == ',')
165 (*pp)++;
166 return 0;
168 else
169 return -1;
172 static int
173 require_end (char *p)
175 if (*p == '\0')
176 return 0;
177 else
178 return -1;
181 static int
182 require_valid_fd (int fd)
184 struct fd_list *fd_ptr;
186 for (fd_ptr = open_fds; fd_ptr != NULL; fd_ptr = fd_ptr->next)
187 if (fd_ptr->fd == fd)
188 return 0;
190 return -1;
193 /* Fill BUF with an hostio error packet representing the last hostio
194 error, from errno. */
196 static void
197 hostio_error (char *own_buf)
199 int fileio_error = host_to_fileio_error (errno);
200 sprintf (own_buf, "F-1,%x", fileio_error);
203 static void
204 hostio_packet_error (char *own_buf)
206 sprintf (own_buf, "F-1,%x", FILEIO_EINVAL);
209 static void
210 hostio_reply (char *own_buf, int result)
212 sprintf (own_buf, "F%x", result);
215 static int
216 hostio_reply_with_data (char *own_buf, char *buffer, int len,
217 int *new_packet_len)
219 int input_index, output_index, out_maxlen;
221 sprintf (own_buf, "F%x;", len);
222 output_index = strlen (own_buf);
224 out_maxlen = PBUFSIZ;
226 for (input_index = 0; input_index < len; input_index++)
228 char b = buffer[input_index];
230 if (b == '$' || b == '#' || b == '}' || b == '*')
232 /* These must be escaped. */
233 if (output_index + 2 > out_maxlen)
234 break;
235 own_buf[output_index++] = '}';
236 own_buf[output_index++] = b ^ 0x20;
238 else
240 if (output_index + 1 > out_maxlen)
241 break;
242 own_buf[output_index++] = b;
246 *new_packet_len = output_index;
247 return input_index;
250 /* Process ID of inferior whose filesystem hostio functions
251 that take FILENAME arguments will use. Zero means to use
252 our own filesystem. */
254 static int hostio_fs_pid;
256 /* See hostio.h. */
258 void
259 hostio_handle_new_gdb_connection (void)
261 hostio_fs_pid = 0;
264 /* Handle a "vFile:setfs:" packet. */
266 static void
267 handle_setfs (char *own_buf)
269 char *p;
270 int pid;
272 /* If the target doesn't have any of the in-filesystem-of methods
273 then there's no point in GDB sending "vFile:setfs:" packets. We
274 reply with an empty packet (i.e. we pretend we don't understand
275 "vFile:setfs:") and that should stop GDB sending any more. */
276 if (!the_target->supports_multifs ())
278 own_buf[0] = '\0';
279 return;
282 p = own_buf + strlen ("vFile:setfs:");
284 if (require_int (&p, &pid)
285 || pid < 0
286 || require_end (p))
288 hostio_packet_error (own_buf);
289 return;
292 hostio_fs_pid = pid;
294 hostio_reply (own_buf, 0);
297 static void
298 handle_open (char *own_buf)
300 char filename[HOSTIO_PATH_MAX];
301 char *p;
302 int fileio_flags, fileio_mode, flags, fd;
303 mode_t mode;
304 struct fd_list *new_fd;
306 p = own_buf + strlen ("vFile:open:");
308 if (require_filename (&p, filename)
309 || require_comma (&p)
310 || require_int (&p, &fileio_flags)
311 || require_comma (&p)
312 || require_int (&p, &fileio_mode)
313 || require_end (p)
314 || fileio_to_host_openflags (fileio_flags, &flags)
315 || fileio_to_host_mode (fileio_mode, &mode))
317 hostio_packet_error (own_buf);
318 return;
321 /* We do not need to convert MODE, since the fileio protocol
322 uses the standard values. */
323 if (hostio_fs_pid != 0)
324 fd = the_target->multifs_open (hostio_fs_pid, filename, flags, mode);
325 else
326 fd = open (filename, flags, mode);
328 if (fd == -1)
330 hostio_error (own_buf);
331 return;
334 /* Record the new file descriptor. */
335 new_fd = XNEW (struct fd_list);
336 new_fd->fd = fd;
337 new_fd->next = open_fds;
338 open_fds = new_fd;
340 hostio_reply (own_buf, fd);
343 static void
344 handle_pread (char *own_buf, int *new_packet_len)
346 int fd, ret, len, offset, bytes_sent;
347 char *p, *data;
348 static int max_reply_size = -1;
350 p = own_buf + strlen ("vFile:pread:");
352 if (require_int (&p, &fd)
353 || require_comma (&p)
354 || require_valid_fd (fd)
355 || require_int (&p, &len)
356 || require_comma (&p)
357 || require_int (&p, &offset)
358 || require_end (p))
360 hostio_packet_error (own_buf);
361 return;
364 /* Do not attempt to read more than the maximum number of bytes
365 hostio_reply_with_data can fit in a packet. We may still read
366 too much because of escaping, but this is handled below. */
367 if (max_reply_size == -1)
369 sprintf (own_buf, "F%x;", PBUFSIZ);
370 max_reply_size = PBUFSIZ - strlen (own_buf);
372 if (len > max_reply_size)
373 len = max_reply_size;
375 data = (char *) xmalloc (len);
376 #ifdef HAVE_PREAD
377 ret = pread (fd, data, len, offset);
378 #else
379 ret = -1;
380 #endif
381 /* If we have no pread or it failed for this file, use lseek/read. */
382 if (ret == -1)
384 ret = lseek (fd, offset, SEEK_SET);
385 if (ret != -1)
386 ret = read (fd, data, len);
389 if (ret == -1)
391 hostio_error (own_buf);
392 free (data);
393 return;
396 bytes_sent = hostio_reply_with_data (own_buf, data, ret, new_packet_len);
398 /* If we were using read, and the data did not all fit in the reply,
399 we would have to back up using lseek here. With pread it does
400 not matter. But we still have a problem; the return value in the
401 packet might be wrong, so we must fix it. This time it will
402 definitely fit. */
403 if (bytes_sent < ret)
404 bytes_sent = hostio_reply_with_data (own_buf, data, bytes_sent,
405 new_packet_len);
407 free (data);
410 static void
411 handle_pwrite (char *own_buf, int packet_len)
413 int fd, ret, len, offset;
414 char *p, *data;
416 p = own_buf + strlen ("vFile:pwrite:");
418 if (require_int (&p, &fd)
419 || require_comma (&p)
420 || require_valid_fd (fd)
421 || require_int (&p, &offset)
422 || require_comma (&p)
423 || require_data (p, packet_len - (p - own_buf), &data, &len))
425 hostio_packet_error (own_buf);
426 return;
429 #ifdef HAVE_PWRITE
430 ret = pwrite (fd, data, len, offset);
431 #else
432 ret = -1;
433 #endif
434 /* If we have no pwrite or it failed for this file, use lseek/write. */
435 if (ret == -1)
437 ret = lseek (fd, offset, SEEK_SET);
438 if (ret != -1)
439 ret = write (fd, data, len);
442 if (ret == -1)
444 hostio_error (own_buf);
445 free (data);
446 return;
449 hostio_reply (own_buf, ret);
450 free (data);
453 static void
454 handle_fstat (char *own_buf, int *new_packet_len)
456 int fd, bytes_sent;
457 char *p;
458 struct stat st;
459 struct fio_stat fst;
461 p = own_buf + strlen ("vFile:fstat:");
463 if (require_int (&p, &fd)
464 || require_valid_fd (fd)
465 || require_end (p))
467 hostio_packet_error (own_buf);
468 return;
471 if (fstat (fd, &st) == -1)
473 hostio_error (own_buf);
474 return;
477 host_to_fileio_stat (&st, &fst);
479 bytes_sent = hostio_reply_with_data (own_buf,
480 (char *) &fst, sizeof (fst),
481 new_packet_len);
483 /* If the response does not fit into a single packet, do not attempt
484 to return a partial response, but simply fail. */
485 if (bytes_sent < sizeof (fst))
486 write_enn (own_buf);
489 static void
490 handle_stat (char *own_buf, int *new_packet_len)
492 int bytes_sent;
493 char *p;
494 struct stat st;
495 struct fio_stat fst;
496 char filename[HOSTIO_PATH_MAX];
498 p = own_buf + strlen ("vFile:stat:");
500 if (require_filename (&p, filename)
501 || require_end (p))
503 hostio_packet_error (own_buf);
504 return;
507 if (lstat (filename, &st) == -1)
509 hostio_error (own_buf);
510 return;
513 host_to_fileio_stat (&st, &fst);
515 bytes_sent = hostio_reply_with_data (own_buf,
516 (char *) &fst, sizeof (fst),
517 new_packet_len);
519 /* If the response does not fit into a single packet, do not attempt
520 to return a partial response, but simply fail. */
521 if (bytes_sent < sizeof (fst))
522 write_enn (own_buf);
525 static void
526 handle_close (char *own_buf)
528 int fd, ret;
529 char *p;
530 struct fd_list **open_fd_p, *old_fd;
532 p = own_buf + strlen ("vFile:close:");
534 if (require_int (&p, &fd)
535 || require_valid_fd (fd)
536 || require_end (p))
538 hostio_packet_error (own_buf);
539 return;
542 ret = close (fd);
544 if (ret == -1)
546 hostio_error (own_buf);
547 return;
550 open_fd_p = &open_fds;
551 /* We know that fd is in the list, thanks to require_valid_fd. */
552 while ((*open_fd_p)->fd != fd)
553 open_fd_p = &(*open_fd_p)->next;
555 old_fd = *open_fd_p;
556 *open_fd_p = (*open_fd_p)->next;
557 free (old_fd);
559 hostio_reply (own_buf, ret);
562 static void
563 handle_unlink (char *own_buf)
565 char filename[HOSTIO_PATH_MAX];
566 char *p;
567 int ret;
569 p = own_buf + strlen ("vFile:unlink:");
571 if (require_filename (&p, filename)
572 || require_end (p))
574 hostio_packet_error (own_buf);
575 return;
578 if (hostio_fs_pid != 0)
579 ret = the_target->multifs_unlink (hostio_fs_pid, filename);
580 else
581 ret = unlink (filename);
583 if (ret == -1)
585 hostio_error (own_buf);
586 return;
589 hostio_reply (own_buf, ret);
592 static void
593 handle_readlink (char *own_buf, int *new_packet_len)
595 char filename[HOSTIO_PATH_MAX], linkname[HOSTIO_PATH_MAX];
596 char *p;
597 int ret, bytes_sent;
599 p = own_buf + strlen ("vFile:readlink:");
601 if (require_filename (&p, filename)
602 || require_end (p))
604 hostio_packet_error (own_buf);
605 return;
608 if (hostio_fs_pid != 0)
609 ret = the_target->multifs_readlink (hostio_fs_pid, filename,
610 linkname,
611 sizeof (linkname) - 1);
612 else
613 ret = readlink (filename, linkname, sizeof (linkname) - 1);
615 if (ret == -1)
617 hostio_error (own_buf);
618 return;
621 bytes_sent = hostio_reply_with_data (own_buf, linkname, ret, new_packet_len);
623 /* If the response does not fit into a single packet, do not attempt
624 to return a partial response, but simply fail. */
625 if (bytes_sent < ret)
626 sprintf (own_buf, "F-1,%x", FILEIO_ENAMETOOLONG);
629 /* Handle all the 'F' file transfer packets. */
632 handle_vFile (char *own_buf, int packet_len, int *new_packet_len)
634 if (startswith (own_buf, "vFile:open:"))
635 handle_open (own_buf);
636 else if (startswith (own_buf, "vFile:pread:"))
637 handle_pread (own_buf, new_packet_len);
638 else if (startswith (own_buf, "vFile:pwrite:"))
639 handle_pwrite (own_buf, packet_len);
640 else if (startswith (own_buf, "vFile:fstat:"))
641 handle_fstat (own_buf, new_packet_len);
642 else if (startswith (own_buf, "vFile:stat:"))
643 handle_stat (own_buf, new_packet_len);
644 else if (startswith (own_buf, "vFile:close:"))
645 handle_close (own_buf);
646 else if (startswith (own_buf, "vFile:unlink:"))
647 handle_unlink (own_buf);
648 else if (startswith (own_buf, "vFile:readlink:"))
649 handle_readlink (own_buf, new_packet_len);
650 else if (startswith (own_buf, "vFile:setfs:"))
651 handle_setfs (own_buf);
652 else
653 return 0;
655 return 1;