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"
32 extern int remote_debug
;
40 static struct fd_list
*open_fds
;
43 safe_fromhex (char a
, int *nibble
)
45 if (a
>= '0' && a
<= '9')
47 else if (a
>= 'a' && a
<= 'f')
48 *nibble
= a
- 'a' + 10;
49 else if (a
>= 'A' && a
<= 'F')
50 *nibble
= a
- 'A' + 10;
57 /* Filenames are hex encoded, so the maximum we can handle is half the
58 packet buffer size. Cap to PATH_MAX, if it is shorter. */
59 #if !defined (PATH_MAX) || (PATH_MAX > (PBUFSIZ / 2 + 1))
60 # define HOSTIO_PATH_MAX (PBUFSIZ / 2 + 1)
62 # define HOSTIO_PATH_MAX PATH_MAX
66 require_filename (char **pp
, char *filename
)
74 while (*p
&& *p
!= ',')
78 /* Don't allow overflow. */
79 if (count
>= HOSTIO_PATH_MAX
- 1)
82 if (safe_fromhex (p
[0], &nib1
)
83 || safe_fromhex (p
[1], &nib2
))
86 filename
[count
++] = nib1
* 16 + nib2
;
90 filename
[count
] = '\0';
96 require_int (char **pp
, int *value
)
99 int count
, firstdigit
;
106 while (*p
&& *p
!= ',')
110 if (safe_fromhex (p
[0], &nib
))
113 if (firstdigit
== -1)
116 /* Don't allow overflow. */
117 if (count
>= 8 || (count
== 7 && firstdigit
>= 0x8))
120 *value
= *value
* 16 + nib
;
130 require_data (char *p
, int p_len
, char **data
, int *data_len
)
132 int input_index
, output_index
, escaped
;
134 *data
= (char *) xmalloc (p_len
);
138 for (input_index
= 0; input_index
< p_len
; input_index
++)
140 char b
= p
[input_index
];
144 (*data
)[output_index
++] = b
^ 0x20;
150 (*data
)[output_index
++] = b
;
159 *data_len
= output_index
;
164 require_comma (char **pp
)
176 require_end (char *p
)
185 require_valid_fd (int fd
)
187 struct fd_list
*fd_ptr
;
189 for (fd_ptr
= open_fds
; fd_ptr
!= NULL
; fd_ptr
= fd_ptr
->next
)
190 if (fd_ptr
->fd
== fd
)
196 /* Fill in own_buf with the last hostio error packet, however it
197 suitable for the target. */
199 hostio_error (char *own_buf
)
201 the_target
->hostio_last_error (own_buf
);
205 hostio_packet_error (char *own_buf
)
207 sprintf (own_buf
, "F-1,%x", FILEIO_EINVAL
);
211 hostio_reply (char *own_buf
, int result
)
213 sprintf (own_buf
, "F%x", result
);
217 hostio_reply_with_data (char *own_buf
, char *buffer
, int len
,
220 int input_index
, output_index
, out_maxlen
;
222 sprintf (own_buf
, "F%x;", len
);
223 output_index
= strlen (own_buf
);
225 out_maxlen
= PBUFSIZ
;
227 for (input_index
= 0; input_index
< len
; input_index
++)
229 char b
= buffer
[input_index
];
231 if (b
== '$' || b
== '#' || b
== '}' || b
== '*')
233 /* These must be escaped. */
234 if (output_index
+ 2 > out_maxlen
)
236 own_buf
[output_index
++] = '}';
237 own_buf
[output_index
++] = b
^ 0x20;
241 if (output_index
+ 1 > out_maxlen
)
243 own_buf
[output_index
++] = b
;
247 *new_packet_len
= output_index
;
251 /* Process ID of inferior whose filesystem hostio functions
252 that take FILENAME arguments will use. Zero means to use
253 our own filesystem. */
255 static int hostio_fs_pid
;
260 hostio_handle_new_gdb_connection (void)
265 /* Handle a "vFile:setfs:" packet. */
268 handle_setfs (char *own_buf
)
273 /* If the target doesn't have any of the in-filesystem-of methods
274 then there's no point in GDB sending "vFile:setfs:" packets. We
275 reply with an empty packet (i.e. we pretend we don't understand
276 "vFile:setfs:") and that should stop GDB sending any more. */
277 if (the_target
->multifs_open
== NULL
278 && the_target
->multifs_unlink
== NULL
279 && the_target
->multifs_readlink
== NULL
)
285 p
= own_buf
+ strlen ("vFile:setfs:");
287 if (require_int (&p
, &pid
)
291 hostio_packet_error (own_buf
);
297 hostio_reply (own_buf
, 0);
301 handle_open (char *own_buf
)
303 char filename
[HOSTIO_PATH_MAX
];
305 int fileio_flags
, fileio_mode
, flags
, fd
;
307 struct fd_list
*new_fd
;
309 p
= own_buf
+ strlen ("vFile:open:");
311 if (require_filename (&p
, filename
)
312 || require_comma (&p
)
313 || require_int (&p
, &fileio_flags
)
314 || require_comma (&p
)
315 || require_int (&p
, &fileio_mode
)
317 || fileio_to_host_openflags (fileio_flags
, &flags
)
318 || fileio_to_host_mode (fileio_mode
, &mode
))
320 hostio_packet_error (own_buf
);
324 /* We do not need to convert MODE, since the fileio protocol
325 uses the standard values. */
326 if (hostio_fs_pid
!= 0 && the_target
->multifs_open
!= NULL
)
327 fd
= the_target
->multifs_open (hostio_fs_pid
, filename
,
330 fd
= open (filename
, flags
, mode
);
334 hostio_error (own_buf
);
338 /* Record the new file descriptor. */
339 new_fd
= XNEW (struct fd_list
);
341 new_fd
->next
= open_fds
;
344 hostio_reply (own_buf
, fd
);
348 handle_pread (char *own_buf
, int *new_packet_len
)
350 int fd
, ret
, len
, offset
, bytes_sent
;
352 static int max_reply_size
= -1;
354 p
= own_buf
+ strlen ("vFile:pread:");
356 if (require_int (&p
, &fd
)
357 || require_comma (&p
)
358 || require_valid_fd (fd
)
359 || require_int (&p
, &len
)
360 || require_comma (&p
)
361 || require_int (&p
, &offset
)
364 hostio_packet_error (own_buf
);
368 /* Do not attempt to read more than the maximum number of bytes
369 hostio_reply_with_data can fit in a packet. We may still read
370 too much because of escaping, but this is handled below. */
371 if (max_reply_size
== -1)
373 sprintf (own_buf
, "F%x;", PBUFSIZ
);
374 max_reply_size
= PBUFSIZ
- strlen (own_buf
);
376 if (len
> max_reply_size
)
377 len
= max_reply_size
;
379 data
= (char *) xmalloc (len
);
381 ret
= pread (fd
, data
, len
, offset
);
385 /* If we have no pread or it failed for this file, use lseek/read. */
388 ret
= lseek (fd
, offset
, SEEK_SET
);
390 ret
= read (fd
, data
, len
);
395 hostio_error (own_buf
);
400 bytes_sent
= hostio_reply_with_data (own_buf
, data
, ret
, new_packet_len
);
402 /* If we were using read, and the data did not all fit in the reply,
403 we would have to back up using lseek here. With pread it does
404 not matter. But we still have a problem; the return value in the
405 packet might be wrong, so we must fix it. This time it will
407 if (bytes_sent
< ret
)
408 bytes_sent
= hostio_reply_with_data (own_buf
, data
, bytes_sent
,
415 handle_pwrite (char *own_buf
, int packet_len
)
417 int fd
, ret
, len
, offset
;
420 p
= own_buf
+ strlen ("vFile:pwrite:");
422 if (require_int (&p
, &fd
)
423 || require_comma (&p
)
424 || require_valid_fd (fd
)
425 || require_int (&p
, &offset
)
426 || require_comma (&p
)
427 || require_data (p
, packet_len
- (p
- own_buf
), &data
, &len
))
429 hostio_packet_error (own_buf
);
434 ret
= pwrite (fd
, data
, len
, offset
);
438 /* If we have no pwrite or it failed for this file, use lseek/write. */
441 ret
= lseek (fd
, offset
, SEEK_SET
);
443 ret
= write (fd
, data
, len
);
448 hostio_error (own_buf
);
453 hostio_reply (own_buf
, ret
);
458 handle_fstat (char *own_buf
, int *new_packet_len
)
465 p
= own_buf
+ strlen ("vFile:fstat:");
467 if (require_int (&p
, &fd
)
468 || require_valid_fd (fd
)
471 hostio_packet_error (own_buf
);
475 if (fstat (fd
, &st
) == -1)
477 hostio_error (own_buf
);
481 host_to_fileio_stat (&st
, &fst
);
483 bytes_sent
= hostio_reply_with_data (own_buf
,
484 (char *) &fst
, sizeof (fst
),
487 /* If the response does not fit into a single packet, do not attempt
488 to return a partial response, but simply fail. */
489 if (bytes_sent
< sizeof (fst
))
494 handle_close (char *own_buf
)
498 struct fd_list
**open_fd_p
, *old_fd
;
500 p
= own_buf
+ strlen ("vFile:close:");
502 if (require_int (&p
, &fd
)
503 || require_valid_fd (fd
)
506 hostio_packet_error (own_buf
);
514 hostio_error (own_buf
);
518 open_fd_p
= &open_fds
;
519 /* We know that fd is in the list, thanks to require_valid_fd. */
520 while ((*open_fd_p
)->fd
!= fd
)
521 open_fd_p
= &(*open_fd_p
)->next
;
524 *open_fd_p
= (*open_fd_p
)->next
;
527 hostio_reply (own_buf
, ret
);
531 handle_unlink (char *own_buf
)
533 char filename
[HOSTIO_PATH_MAX
];
537 p
= own_buf
+ strlen ("vFile:unlink:");
539 if (require_filename (&p
, filename
)
542 hostio_packet_error (own_buf
);
546 if (hostio_fs_pid
!= 0 && the_target
->multifs_unlink
!= NULL
)
547 ret
= the_target
->multifs_unlink (hostio_fs_pid
, filename
);
549 ret
= unlink (filename
);
553 hostio_error (own_buf
);
557 hostio_reply (own_buf
, ret
);
561 handle_readlink (char *own_buf
, int *new_packet_len
)
563 char filename
[HOSTIO_PATH_MAX
], linkname
[HOSTIO_PATH_MAX
];
567 p
= own_buf
+ strlen ("vFile:readlink:");
569 if (require_filename (&p
, filename
)
572 hostio_packet_error (own_buf
);
576 if (hostio_fs_pid
!= 0 && the_target
->multifs_readlink
!= NULL
)
577 ret
= the_target
->multifs_readlink (hostio_fs_pid
, filename
,
579 sizeof (linkname
) - 1);
581 ret
= readlink (filename
, linkname
, sizeof (linkname
) - 1);
585 hostio_error (own_buf
);
589 bytes_sent
= hostio_reply_with_data (own_buf
, linkname
, ret
, new_packet_len
);
591 /* If the response does not fit into a single packet, do not attempt
592 to return a partial response, but simply fail. */
593 if (bytes_sent
< ret
)
594 sprintf (own_buf
, "F-1,%x", FILEIO_ENAMETOOLONG
);
597 /* Handle all the 'F' file transfer packets. */
600 handle_vFile (char *own_buf
, int packet_len
, int *new_packet_len
)
602 if (startswith (own_buf
, "vFile:open:"))
603 handle_open (own_buf
);
604 else if (startswith (own_buf
, "vFile:pread:"))
605 handle_pread (own_buf
, new_packet_len
);
606 else if (startswith (own_buf
, "vFile:pwrite:"))
607 handle_pwrite (own_buf
, packet_len
);
608 else if (startswith (own_buf
, "vFile:fstat:"))
609 handle_fstat (own_buf
, new_packet_len
);
610 else if (startswith (own_buf
, "vFile:close:"))
611 handle_close (own_buf
);
612 else if (startswith (own_buf
, "vFile:unlink:"))
613 handle_unlink (own_buf
);
614 else if (startswith (own_buf
, "vFile:readlink:"))
615 handle_readlink (own_buf
, new_packet_len
);
616 else if (startswith (own_buf
, "vFile:setfs:"))
617 handle_setfs (own_buf
);