Debian Codegraph @

gdbserver/hostio.cc

raw
1 /* Host file transfer support for gdbserver.
2 Copyright (C) 2007-2025 Free Software Foundation, Inc.
3
4 Contributed by CodeSourcery.
5
6 This file is part of GDB.
7
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.
12
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.
17
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/>. */
20
21 #include "gdbsupport/fileio.h"
22 #include "hostio.h"
23
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"
30
31 struct fd_list
32 {
33 int fd;
34 struct fd_list *next;
35 };
36
37 static struct fd_list *open_fds;
38
39 static int
40 safe_fromhex (char a, int *nibble)
41 {
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;
50
51 return 0;
52 }
53
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
61
62 static int
63 require_filename (char **pp, char *filename)
64 {
65 int count;
66 char *p;
67
68 p = *pp;
69 count = 0;
70
71 while (*p && *p != ',')
72 {
73 int nib1, nib2;
74
75 /* Don't allow overflow. */
76 if (count >= HOSTIO_PATH_MAX - 1)
77 return -1;
78
79 if (safe_fromhex (p[0], &nib1)
80 || safe_fromhex (p[1], &nib2))
81 return -1;
82
83 filename[count++] = nib1 * 16 + nib2;
84 p += 2;
85 }
86
87 filename[count] = '\0';
88 *pp = p;
89 return 0;
90 }
91
92 template <typename T>
93 static int
94 require_int (char **pp, T *value)
95 {
96 constexpr bool is_signed = std::is_signed<T>::value;
97
98 char *p;
99 int count, firstdigit;
100
101 /* Max count of hexadecimal digits in T (1 hex digit is 4 bits). */
102 int max_count = sizeof (T) * CHAR_BIT / 4;
103
104 p = *pp;
105 *value = 0;
106 count = 0;
107 firstdigit = -1;
108
109 while (*p && *p != ',')
110 {
111 int nib;
112
113 if (safe_fromhex (p[0], &nib))
114 return -1;
115
116 if (firstdigit == -1)
117 firstdigit = nib;
118
119 /* Don't allow overflow. */
120 if (count >= max_count
121 || (is_signed && count == (max_count - 1) && firstdigit >= 0x8))
122 return -1;
123
124 *value = *value * 16 + nib;
125 p++;
126 count++;
127 }
128
129 *pp = p;
130 return 0;
131 }
132
133 static int
134 require_data (char *p, int p_len, char **data, int *data_len)
135 {
136 int input_index, output_index, escaped;
137
138 *data = (char *) xmalloc (p_len);
139
140 output_index = 0;
141 escaped = 0;
142 for (input_index = 0; input_index < p_len; input_index++)
143 {
144 char b = p[input_index];
145
146 if (escaped)
147 {
148 (*data)[output_index++] = b ^ 0x20;
149 escaped = 0;
150 }
151 else if (b == '}')
152 escaped = 1;
153 else
154 (*data)[output_index++] = b;
155 }
156
157 if (escaped)
158 {
159 free (*data);
160 return -1;
161 }
162
163 *data_len = output_index;
164 return 0;
165 }
166
167 static int
168 require_comma (char **pp)
169 {
170 if (**pp == ',')
171 {
172 (*pp)++;
173 return 0;
174 }
175 else
176 return -1;
177 }
178
179 static int
180 require_end (char *p)
181 {
182 if (*p == '\0')
183 return 0;
184 else
185 return -1;
186 }
187
188 static int
189 require_valid_fd (int fd)
190 {
191 struct fd_list *fd_ptr;
192
193 for (fd_ptr = open_fds; fd_ptr != NULL; fd_ptr = fd_ptr->next)
194 if (fd_ptr->fd == fd)
195 return 0;
196
197 return -1;
198 }
199
200 /* Fill BUF with an hostio error packet representing the last hostio
201 error, from errno. */
202
203 static void
204 hostio_error (char *own_buf)
205 {
206 int fileio_error = host_to_fileio_error (errno);
207 sprintf (own_buf, "F-1,%x", fileio_error);
208 }
209
210 static void
211 hostio_packet_error (char *own_buf)
212 {
213 sprintf (own_buf, "F-1,%x", FILEIO_EINVAL);
214 }
215
216 static void
217 hostio_reply (char *own_buf, int result)
218 {
219 sprintf (own_buf, "F%x", result);
220 }
221
222 static int
223 hostio_reply_with_data (char *own_buf, char *buffer, int len,
224 int *new_packet_len)
225 {
226 int input_index, output_index, out_maxlen;
227
228 sprintf (own_buf, "F%x;", len);
229 output_index = strlen (own_buf);
230
231 out_maxlen = PBUFSIZ;
232
233 for (input_index = 0; input_index < len; input_index++)
234 {
235 char b = buffer[input_index];
236
237 if (b == '$' || b == '#' || b == '}' || b == '*')
238 {
239 /* These must be escaped. */
240 if (output_index + 2 > out_maxlen)
241 break;
242 own_buf[output_index++] = '}';
243 own_buf[output_index++] = b ^ 0x20;
244 }
245 else
246 {
247 if (output_index + 1 > out_maxlen)
248 break;
249 own_buf[output_index++] = b;
250 }
251 }
252
253 *new_packet_len = output_index;
254 return input_index;
255 }
256
257 /* Process ID of inferior whose filesystem hostio functions
258 that take FILENAME arguments will use. Zero means to use
259 our own filesystem. */
260
261 static int hostio_fs_pid;
262
263 /* See hostio.h. */
264
265 void
266 hostio_handle_new_gdb_connection (void)
267 {
268 hostio_fs_pid = 0;
269 }
270
271 /* Handle a "vFile:setfs:" packet. */
272
273 static void
274 handle_setfs (char *own_buf)
275 {
276 char *p;
277 int pid;
278
279 /* If the target doesn't have any of the in-filesystem-of methods
280 then there's no point in GDB sending "vFile:setfs:" packets. We
281 reply with an empty packet (i.e. we pretend we don't understand
282 "vFile:setfs:") and that should stop GDB sending any more. */
283 if (!the_target->supports_multifs ())
284 {
285 own_buf[0] = '\0';
286 return;
287 }
288
289 p = own_buf + strlen ("vFile:setfs:");
290
291 if (require_int (&p, &pid)
292 || pid < 0
293 || require_end (p))
294 {
295 hostio_packet_error (own_buf);
296 return;
297 }
298
299 hostio_fs_pid = pid;
300
301 hostio_reply (own_buf, 0);
302 }
303
304 static void
305 handle_open (char *own_buf)
306 {
307 char filename[HOSTIO_PATH_MAX];
308 char *p;
309 int fileio_flags, fileio_mode, flags, fd;
310 mode_t mode;
311 struct fd_list *new_fd;
312
313 p = own_buf + strlen ("vFile:open:");
314
315 if (require_filename (&p, filename)
316 || require_comma (&p)
317 || require_int (&p, &fileio_flags)
318 || require_comma (&p)
319 || require_int (&p, &fileio_mode)
320 || require_end (p)
321 || fileio_to_host_openflags (fileio_flags, &flags)
322 || fileio_to_host_mode (fileio_mode, &mode))
323 {
324 hostio_packet_error (own_buf);
325 return;
326 }
327
328 /* We do not need to convert MODE, since the fileio protocol
329 uses the standard values. */
330 if (hostio_fs_pid != 0)
331 fd = the_target->multifs_open (hostio_fs_pid, filename, flags, mode);
332 else
333 fd = open (filename, flags, mode);
334
335 if (fd == -1)
336 {
337 hostio_error (own_buf);
338 return;
339 }
340
341 /* Record the new file descriptor. */
342 new_fd = XNEW (struct fd_list);
343 new_fd->fd = fd;
344 new_fd->next = open_fds;
345 open_fds = new_fd;
346
347 hostio_reply (own_buf, fd);
348 }
349
350 static void
351 handle_pread (char *own_buf, int *new_packet_len)
352 {
353 int fd, ret, len, bytes_sent;
354 off_t offset;
355 char *p, *data;
356 static int max_reply_size = -1;
357
358 p = own_buf + strlen ("vFile:pread:");
359
360 if (require_int (&p, &fd)
361 || require_comma (&p)
362 || require_valid_fd (fd)
363 || require_int (&p, &len)
364 || require_comma (&p)
365 || require_int (&p, &offset)
366 || require_end (p))
367 {
368 hostio_packet_error (own_buf);
369 return;
370 }
371
372 /* Do not attempt to read more than the maximum number of bytes
373 hostio_reply_with_data can fit in a packet. We may still read
374 too much because of escaping, but this is handled below. */
375 if (max_reply_size == -1)
376 {
377 sprintf (own_buf, "F%x;", PBUFSIZ);
378 max_reply_size = PBUFSIZ - strlen (own_buf);
379 }
380 if (len > max_reply_size)
381 len = max_reply_size;
382
383 data = (char *) xmalloc (len);
384 #ifdef HAVE_PREAD
385 ret = pread (fd, data, len, offset);
386 #else
387 ret = -1;
388 #endif
389 /* If we have no pread or it failed for this file, use lseek/read. */
390 if (ret == -1)
391 {
392 ret = lseek (fd, offset, SEEK_SET);
393 if (ret != -1)
394 ret = read (fd, data, len);
395 }
396
397 if (ret == -1)
398 {
399 hostio_error (own_buf);
400 free (data);
401 return;
402 }
403
404 bytes_sent = hostio_reply_with_data (own_buf, data, ret, new_packet_len);
405
406 /* If we were using read, and the data did not all fit in the reply,
407 we would have to back up using lseek here. With pread it does
408 not matter. But we still have a problem; the return value in the
409 packet might be wrong, so we must fix it. This time it will
410 definitely fit. */
411 if (bytes_sent < ret)
412 bytes_sent = hostio_reply_with_data (own_buf, data, bytes_sent,
413 new_packet_len);
414
415 free (data);
416 }
417
418 static void
419 handle_pwrite (char *own_buf, int packet_len)
420 {
421 int fd, ret, len;
422 off_t offset;
423 char *p, *data;
424
425 p = own_buf + strlen ("vFile:pwrite:");
426
427 if (require_int (&p, &fd)
428 || require_comma (&p)
429 || require_valid_fd (fd)
430 || require_int (&p, &offset)
431 || require_comma (&p)
432 || require_data (p, packet_len - (p - own_buf), &data, &len))
433 {
434 hostio_packet_error (own_buf);
435 return;
436 }
437
438 #ifdef HAVE_PWRITE
439 ret = pwrite (fd, data, len, offset);
440 #else
441 ret = -1;
442 #endif
443 /* If we have no pwrite or it failed for this file, use lseek/write. */
444 if (ret == -1)
445 {
446 ret = lseek (fd, offset, SEEK_SET);
447 if (ret != -1)
448 ret = write (fd, data, len);
449 }
450
451 if (ret == -1)
452 {
453 hostio_error (own_buf);
454 free (data);
455 return;
456 }
457
458 hostio_reply (own_buf, ret);
459 free (data);
460 }
461
462 static void
463 handle_fstat (char *own_buf, int *new_packet_len)
464 {
465 int fd, bytes_sent;
466 char *p;
467 struct stat st;
468 struct fio_stat fst;
469
470 p = own_buf + strlen ("vFile:fstat:");
471
472 if (require_int (&p, &fd)
473 || require_valid_fd (fd)
474 || require_end (p))
475 {
476 hostio_packet_error (own_buf);
477 return;
478 }
479
480 if (fstat (fd, &st) == -1)
481 {
482 hostio_error (own_buf);
483 return;
484 }
485
486 host_to_fileio_stat (&st, &fst);
487
488 bytes_sent = hostio_reply_with_data (own_buf,
489 (char *) &fst, sizeof (fst),
490 new_packet_len);
491
492 /* If the response does not fit into a single packet, do not attempt
493 to return a partial response, but simply fail. */
494 if (bytes_sent < sizeof (fst))
495 write_enn (own_buf);
496 }
497
498 static void
499 handle_stat (char *own_buf, int *new_packet_len)
500 {
501 int bytes_sent;
502 char *p;
503 struct stat st;
504 struct fio_stat fst;
505 char filename[HOSTIO_PATH_MAX];
506
507 p = own_buf + strlen ("vFile:stat:");
508
509 if (require_filename (&p, filename)
510 || require_end (p))
511 {
512 hostio_packet_error (own_buf);
513 return;
514 }
515
516 if (stat (filename, &st) == -1)
517 {
518 hostio_error (own_buf);
519 return;
520 }
521
522 host_to_fileio_stat (&st, &fst);
523
524 bytes_sent = hostio_reply_with_data (own_buf,
525 (char *) &fst, sizeof (fst),
526 new_packet_len);
527
528 /* If the response does not fit into a single packet, do not attempt
529 to return a partial response, but simply fail. */
530 if (bytes_sent < sizeof (fst))
531 write_enn (own_buf);
532 }
533
534 static void
535 handle_lstat (char *own_buf, int *new_packet_len)
536 {
537 int ret, bytes_sent;
538 char *p;
539 struct stat st;
540 struct fio_stat fst;
541 char filename[HOSTIO_PATH_MAX];
542
543 p = own_buf + strlen ("vFile:lstat:");
544
545 if (require_filename (&p, filename)
546 || require_end (p))
547 {
548 hostio_packet_error (own_buf);
549 return;
550 }
551
552 if (hostio_fs_pid != 0)
553 ret = the_target->multifs_lstat (hostio_fs_pid, filename, &st);
554 else
555 ret = lstat (filename, &st);
556
557 if (ret == -1)
558 {
559 hostio_error (own_buf);
560 return;
561 }
562
563 host_to_fileio_stat (&st, &fst);
564
565 bytes_sent = hostio_reply_with_data (own_buf,
566 (char *) &fst, sizeof (fst),
567 new_packet_len);
568
569 /* If the response does not fit into a single packet, do not attempt
570 to return a partial response, but simply fail. */
571 if (bytes_sent < sizeof (fst))
572 write_enn (own_buf);
573 }
574
575 static void
576 handle_close (char *own_buf)
577 {
578 int fd, ret;
579 char *p;
580 struct fd_list **open_fd_p, *old_fd;
581
582 p = own_buf + strlen ("vFile:close:");
583
584 if (require_int (&p, &fd)
585 || require_valid_fd (fd)
586 || require_end (p))
587 {
588 hostio_packet_error (own_buf);
589 return;
590 }
591
592 ret = close (fd);
593
594 if (ret == -1)
595 {
596 hostio_error (own_buf);
597 return;
598 }
599
600 open_fd_p = &open_fds;
601 /* We know that fd is in the list, thanks to require_valid_fd. */
602 while ((*open_fd_p)->fd != fd)
603 open_fd_p = &(*open_fd_p)->next;
604
605 old_fd = *open_fd_p;
606 *open_fd_p = (*open_fd_p)->next;
607 free (old_fd);
608
609 hostio_reply (own_buf, ret);
610 }
611
612 static void
613 handle_unlink (char *own_buf)
614 {
615 char filename[HOSTIO_PATH_MAX];
616 char *p;
617 int ret;
618
619 p = own_buf + strlen ("vFile:unlink:");
620
621 if (require_filename (&p, filename)
622 || require_end (p))
623 {
624 hostio_packet_error (own_buf);
625 return;
626 }
627
628 if (hostio_fs_pid != 0)
629 ret = the_target->multifs_unlink (hostio_fs_pid, filename);
630 else
631 ret = unlink (filename);
632
633 if (ret == -1)
634 {
635 hostio_error (own_buf);
636 return;
637 }
638
639 hostio_reply (own_buf, ret);
640 }
641
642 static void
643 handle_readlink (char *own_buf, int *new_packet_len)
644 {
645 char filename[HOSTIO_PATH_MAX], linkname[HOSTIO_PATH_MAX];
646 char *p;
647 int ret, bytes_sent;
648
649 p = own_buf + strlen ("vFile:readlink:");
650
651 if (require_filename (&p, filename)
652 || require_end (p))
653 {
654 hostio_packet_error (own_buf);
655 return;
656 }
657
658 if (hostio_fs_pid != 0)
659 ret = the_target->multifs_readlink (hostio_fs_pid, filename,
660 linkname,
661 sizeof (linkname) - 1);
662 else
663 ret = readlink (filename, linkname, sizeof (linkname) - 1);
664
665 if (ret == -1)
666 {
667 hostio_error (own_buf);
668 return;
669 }
670
671 bytes_sent = hostio_reply_with_data (own_buf, linkname, ret, new_packet_len);
672
673 /* If the response does not fit into a single packet, do not attempt
674 to return a partial response, but simply fail. */
675 if (bytes_sent < ret)
676 sprintf (own_buf, "F-1,%x", FILEIO_ENAMETOOLONG);
677 }
678
679 /* Handle all the 'F' file transfer packets. */
680
681 int
682 handle_vFile (char *own_buf, int packet_len, int *new_packet_len)
683 {
684 if (startswith (own_buf, "vFile:open:"))
685 handle_open (own_buf);
686 else if (startswith (own_buf, "vFile:pread:"))
687 handle_pread (own_buf, new_packet_len);
688 else if (startswith (own_buf, "vFile:pwrite:"))
689 handle_pwrite (own_buf, packet_len);
690 else if (startswith (own_buf, "vFile:fstat:"))
691 handle_fstat (own_buf, new_packet_len);
692 else if (startswith (own_buf, "vFile:stat:"))
693 handle_stat (own_buf, new_packet_len);
694 else if (startswith (own_buf, "vFile:lstat:"))
695 handle_lstat (own_buf, new_packet_len);
696 else if (startswith (own_buf, "vFile:close:"))
697 handle_close (own_buf);
698 else if (startswith (own_buf, "vFile:unlink:"))
699 handle_unlink (own_buf);
700 else if (startswith (own_buf, "vFile:readlink:"))
701 handle_readlink (own_buf, new_packet_len);
702 else if (startswith (own_buf, "vFile:setfs:"))
703 handle_setfs (own_buf);
704 else
705 return 0;
706
707 return 1;
708 }