1 /* Host file transfer support for gdbserver.
2 Copyright (C) 2007-2019 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/>. */
22 #include "gdb/fileio.h"
28 #include <sys/types.h>
30 #include "common/fileio.h"
38 static struct fd_list
*open_fds
;
41 safe_fromhex (char a
, int *nibble
)
43 if (a
>= '0' && a
<= '9')
45 else if (a
>= 'a' && a
<= 'f')
46 *nibble
= a
- 'a' + 10;
47 else if (a
>= 'A' && a
<= 'F')
48 *nibble
= a
- 'A' + 10;
55 /* Filenames are hex encoded, so the maximum we can handle is half the
56 packet buffer size. Cap to PATH_MAX, if it is shorter. */
57 #if !defined (PATH_MAX) || (PATH_MAX > (PBUFSIZ / 2 + 1))
58 # define HOSTIO_PATH_MAX (PBUFSIZ / 2 + 1)
60 # define HOSTIO_PATH_MAX PATH_MAX
64 require_filename (char **pp
, char *filename
)
72 while (*p
&& *p
!= ',')
76 /* Don't allow overflow. */
77 if (count
>= HOSTIO_PATH_MAX
- 1)
80 if (safe_fromhex (p
[0], &nib1
)
81 || safe_fromhex (p
[1], &nib2
))
84 filename
[count
++] = nib1
* 16 + nib2
;
88 filename
[count
] = '\0';
94 require_int (char **pp
, int *value
)
97 int count
, firstdigit
;
104 while (*p
&& *p
!= ',')
108 if (safe_fromhex (p
[0], &nib
))
111 if (firstdigit
== -1)
114 /* Don't allow overflow. */
115 if (count
>= 8 || (count
== 7 && firstdigit
>= 0x8))
118 *value
= *value
* 16 + nib
;
128 require_data (char *p
, int p_len
, char **data
, int *data_len
)
130 int input_index
, output_index
, escaped
;
132 *data
= (char *) xmalloc (p_len
);
136 for (input_index
= 0; input_index
< p_len
; input_index
++)
138 char b
= p
[input_index
];
142 (*data
)[output_index
++] = b
^ 0x20;
148 (*data
)[output_index
++] = b
;
157 *data_len
= output_index
;
162 require_comma (char **pp
)
174 require_end (char *p
)
183 require_valid_fd (int fd
)
185 struct fd_list
*fd_ptr
;
187 for (fd_ptr
= open_fds
; fd_ptr
!= NULL
; fd_ptr
= fd_ptr
->next
)
188 if (fd_ptr
->fd
== fd
)
194 /* Fill in own_buf with the last hostio error packet, however it
195 suitable for the target. */
197 hostio_error (char *own_buf
)
199 the_target
->hostio_last_error (own_buf
);
203 hostio_packet_error (char *own_buf
)
205 sprintf (own_buf
, "F-1,%x", FILEIO_EINVAL
);
209 hostio_reply (char *own_buf
, int result
)
211 sprintf (own_buf
, "F%x", result
);
215 hostio_reply_with_data (char *own_buf
, char *buffer
, int len
,
218 int input_index
, output_index
, out_maxlen
;
220 sprintf (own_buf
, "F%x;", len
);
221 output_index
= strlen (own_buf
);
223 out_maxlen
= PBUFSIZ
;
225 for (input_index
= 0; input_index
< len
; input_index
++)
227 char b
= buffer
[input_index
];
229 if (b
== '$' || b
== '#' || b
== '}' || b
== '*')
231 /* These must be escaped. */
232 if (output_index
+ 2 > out_maxlen
)
234 own_buf
[output_index
++] = '}';
235 own_buf
[output_index
++] = b
^ 0x20;
239 if (output_index
+ 1 > out_maxlen
)
241 own_buf
[output_index
++] = b
;
245 *new_packet_len
= output_index
;
249 /* Process ID of inferior whose filesystem hostio functions
250 that take FILENAME arguments will use. Zero means to use
251 our own filesystem. */
253 static int hostio_fs_pid
;
258 hostio_handle_new_gdb_connection (void)
263 /* Handle a "vFile:setfs:" packet. */
266 handle_setfs (char *own_buf
)
271 /* If the target doesn't have any of the in-filesystem-of methods
272 then there's no point in GDB sending "vFile:setfs:" packets. We
273 reply with an empty packet (i.e. we pretend we don't understand
274 "vFile:setfs:") and that should stop GDB sending any more. */
275 if (the_target
->multifs_open
== NULL
276 && the_target
->multifs_unlink
== NULL
277 && the_target
->multifs_readlink
== NULL
)
283 p
= own_buf
+ strlen ("vFile:setfs:");
285 if (require_int (&p
, &pid
)
289 hostio_packet_error (own_buf
);
295 hostio_reply (own_buf
, 0);
299 handle_open (char *own_buf
)
301 char filename
[HOSTIO_PATH_MAX
];
303 int fileio_flags
, fileio_mode
, flags
, fd
;
305 struct fd_list
*new_fd
;
307 p
= own_buf
+ strlen ("vFile:open:");
309 if (require_filename (&p
, filename
)
310 || require_comma (&p
)
311 || require_int (&p
, &fileio_flags
)
312 || require_comma (&p
)
313 || require_int (&p
, &fileio_mode
)
315 || fileio_to_host_openflags (fileio_flags
, &flags
)
316 || fileio_to_host_mode (fileio_mode
, &mode
))
318 hostio_packet_error (own_buf
);
322 /* We do not need to convert MODE, since the fileio protocol
323 uses the standard values. */
324 if (hostio_fs_pid
!= 0 && the_target
->multifs_open
!= NULL
)
325 fd
= the_target
->multifs_open (hostio_fs_pid
, filename
,
328 fd
= open (filename
, flags
, mode
);
332 hostio_error (own_buf
);
336 /* Record the new file descriptor. */
337 new_fd
= XNEW (struct fd_list
);
339 new_fd
->next
= open_fds
;
342 hostio_reply (own_buf
, fd
);
346 handle_pread (char *own_buf
, int *new_packet_len
)
348 int fd
, ret
, len
, offset
, bytes_sent
;
350 static int max_reply_size
= -1;
352 p
= own_buf
+ strlen ("vFile:pread:");
354 if (require_int (&p
, &fd
)
355 || require_comma (&p
)
356 || require_valid_fd (fd
)
357 || require_int (&p
, &len
)
358 || require_comma (&p
)
359 || require_int (&p
, &offset
)
362 hostio_packet_error (own_buf
);
366 /* Do not attempt to read more than the maximum number of bytes
367 hostio_reply_with_data can fit in a packet. We may still read
368 too much because of escaping, but this is handled below. */
369 if (max_reply_size
== -1)
371 sprintf (own_buf
, "F%x;", PBUFSIZ
);
372 max_reply_size
= PBUFSIZ
- strlen (own_buf
);
374 if (len
> max_reply_size
)
375 len
= max_reply_size
;
377 data
= (char *) xmalloc (len
);
379 ret
= pread (fd
, data
, len
, offset
);
383 /* If we have no pread or it failed for this file, use lseek/read. */
386 ret
= lseek (fd
, offset
, SEEK_SET
);
388 ret
= read (fd
, data
, len
);
393 hostio_error (own_buf
);
398 bytes_sent
= hostio_reply_with_data (own_buf
, data
, ret
, new_packet_len
);
400 /* If we were using read, and the data did not all fit in the reply,
401 we would have to back up using lseek here. With pread it does
402 not matter. But we still have a problem; the return value in the
403 packet might be wrong, so we must fix it. This time it will
405 if (bytes_sent
< ret
)
406 bytes_sent
= hostio_reply_with_data (own_buf
, data
, bytes_sent
,
413 handle_pwrite (char *own_buf
, int packet_len
)
415 int fd
, ret
, len
, offset
;
418 p
= own_buf
+ strlen ("vFile:pwrite:");
420 if (require_int (&p
, &fd
)
421 || require_comma (&p
)
422 || require_valid_fd (fd
)
423 || require_int (&p
, &offset
)
424 || require_comma (&p
)
425 || require_data (p
, packet_len
- (p
- own_buf
), &data
, &len
))
427 hostio_packet_error (own_buf
);
432 ret
= pwrite (fd
, data
, len
, offset
);
436 /* If we have no pwrite or it failed for this file, use lseek/write. */
439 ret
= lseek (fd
, offset
, SEEK_SET
);
441 ret
= write (fd
, data
, len
);
446 hostio_error (own_buf
);
451 hostio_reply (own_buf
, ret
);
456 handle_fstat (char *own_buf
, int *new_packet_len
)
463 p
= own_buf
+ strlen ("vFile:fstat:");
465 if (require_int (&p
, &fd
)
466 || require_valid_fd (fd
)
469 hostio_packet_error (own_buf
);
473 if (fstat (fd
, &st
) == -1)
475 hostio_error (own_buf
);
479 host_to_fileio_stat (&st
, &fst
);
481 bytes_sent
= hostio_reply_with_data (own_buf
,
482 (char *) &fst
, sizeof (fst
),
485 /* If the response does not fit into a single packet, do not attempt
486 to return a partial response, but simply fail. */
487 if (bytes_sent
< sizeof (fst
))
492 handle_close (char *own_buf
)
496 struct fd_list
**open_fd_p
, *old_fd
;
498 p
= own_buf
+ strlen ("vFile:close:");
500 if (require_int (&p
, &fd
)
501 || require_valid_fd (fd
)
504 hostio_packet_error (own_buf
);
512 hostio_error (own_buf
);
516 open_fd_p
= &open_fds
;
517 /* We know that fd is in the list, thanks to require_valid_fd. */
518 while ((*open_fd_p
)->fd
!= fd
)
519 open_fd_p
= &(*open_fd_p
)->next
;
522 *open_fd_p
= (*open_fd_p
)->next
;
525 hostio_reply (own_buf
, ret
);
529 handle_unlink (char *own_buf
)
531 char filename
[HOSTIO_PATH_MAX
];
535 p
= own_buf
+ strlen ("vFile:unlink:");
537 if (require_filename (&p
, filename
)
540 hostio_packet_error (own_buf
);
544 if (hostio_fs_pid
!= 0 && the_target
->multifs_unlink
!= NULL
)
545 ret
= the_target
->multifs_unlink (hostio_fs_pid
, filename
);
547 ret
= unlink (filename
);
551 hostio_error (own_buf
);
555 hostio_reply (own_buf
, ret
);
559 handle_readlink (char *own_buf
, int *new_packet_len
)
561 char filename
[HOSTIO_PATH_MAX
], linkname
[HOSTIO_PATH_MAX
];
565 p
= own_buf
+ strlen ("vFile:readlink:");
567 if (require_filename (&p
, filename
)
570 hostio_packet_error (own_buf
);
574 if (hostio_fs_pid
!= 0 && the_target
->multifs_readlink
!= NULL
)
575 ret
= the_target
->multifs_readlink (hostio_fs_pid
, filename
,
577 sizeof (linkname
) - 1);
579 ret
= readlink (filename
, linkname
, sizeof (linkname
) - 1);
583 hostio_error (own_buf
);
587 bytes_sent
= hostio_reply_with_data (own_buf
, linkname
, ret
, new_packet_len
);
589 /* If the response does not fit into a single packet, do not attempt
590 to return a partial response, but simply fail. */
591 if (bytes_sent
< ret
)
592 sprintf (own_buf
, "F-1,%x", FILEIO_ENAMETOOLONG
);
595 /* Handle all the 'F' file transfer packets. */
598 handle_vFile (char *own_buf
, int packet_len
, int *new_packet_len
)
600 if (startswith (own_buf
, "vFile:open:"))
601 handle_open (own_buf
);
602 else if (startswith (own_buf
, "vFile:pread:"))
603 handle_pread (own_buf
, new_packet_len
);
604 else if (startswith (own_buf
, "vFile:pwrite:"))
605 handle_pwrite (own_buf
, packet_len
);
606 else if (startswith (own_buf
, "vFile:fstat:"))
607 handle_fstat (own_buf
, new_packet_len
);
608 else if (startswith (own_buf
, "vFile:close:"))
609 handle_close (own_buf
);
610 else if (startswith (own_buf
, "vFile:unlink:"))
611 handle_unlink (own_buf
);
612 else if (startswith (own_buf
, "vFile:readlink:"))
613 handle_readlink (own_buf
, new_packet_len
);
614 else if (startswith (own_buf
, "vFile:setfs:"))
615 handle_setfs (own_buf
);