1 /* Host file transfer support for gdbserver.
2 Copyright (C) 2007-2015 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>
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
)
105 while (*p
&& *p
!= ',')
109 /* Don't allow overflow. */
113 if (safe_fromhex (p
[0], &nib
))
115 *value
= *value
* 16 + nib
;
125 require_data (char *p
, int p_len
, char **data
, int *data_len
)
127 int input_index
, output_index
, escaped
;
129 *data
= xmalloc (p_len
);
133 for (input_index
= 0; input_index
< p_len
; input_index
++)
135 char b
= p
[input_index
];
139 (*data
)[output_index
++] = b
^ 0x20;
145 (*data
)[output_index
++] = b
;
154 *data_len
= output_index
;
159 require_comma (char **pp
)
171 require_end (char *p
)
180 require_valid_fd (int fd
)
182 struct fd_list
*fd_ptr
;
184 for (fd_ptr
= open_fds
; fd_ptr
!= NULL
; fd_ptr
= fd_ptr
->next
)
185 if (fd_ptr
->fd
== fd
)
191 /* Fill in own_buf with the last hostio error packet, however it
192 suitable for the target. */
194 hostio_error (char *own_buf
)
196 the_target
->hostio_last_error (own_buf
);
200 hostio_packet_error (char *own_buf
)
202 sprintf (own_buf
, "F-1,%x", FILEIO_EINVAL
);
206 hostio_reply (char *own_buf
, int result
)
208 sprintf (own_buf
, "F%x", result
);
212 hostio_reply_with_data (char *own_buf
, char *buffer
, int len
,
215 int input_index
, output_index
, out_maxlen
;
217 sprintf (own_buf
, "F%x;", len
);
218 output_index
= strlen (own_buf
);
220 out_maxlen
= PBUFSIZ
;
222 for (input_index
= 0; input_index
< len
; input_index
++)
224 char b
= buffer
[input_index
];
226 if (b
== '$' || b
== '#' || b
== '}' || b
== '*')
228 /* These must be escaped. */
229 if (output_index
+ 2 > out_maxlen
)
231 own_buf
[output_index
++] = '}';
232 own_buf
[output_index
++] = b
^ 0x20;
236 if (output_index
+ 1 > out_maxlen
)
238 own_buf
[output_index
++] = b
;
242 *new_packet_len
= output_index
;
246 /* Process ID of inferior whose filesystem hostio functions
247 that take FILENAME arguments will use. Zero means to use
248 our own filesystem. */
250 static int hostio_fs_pid
;
255 hostio_handle_new_gdb_connection (void)
260 /* Handle a "vFile:setfs:" packet. */
263 handle_setfs (char *own_buf
)
268 /* If the target doesn't have any of the in-filesystem-of methods
269 then there's no point in GDB sending "vFile:setfs:" packets. We
270 reply with an empty packet (i.e. we pretend we don't understand
271 "vFile:setfs:") and that should stop GDB sending any more. */
272 if (the_target
->multifs_open
== NULL
273 && the_target
->multifs_unlink
== NULL
274 && the_target
->multifs_readlink
== NULL
)
280 p
= own_buf
+ strlen ("vFile:setfs:");
282 if (require_int (&p
, &pid
)
286 hostio_packet_error (own_buf
);
292 hostio_reply (own_buf
, 0);
296 handle_open (char *own_buf
)
298 char filename
[HOSTIO_PATH_MAX
];
300 int fileio_flags
, fileio_mode
, flags
, fd
;
302 struct fd_list
*new_fd
;
304 p
= own_buf
+ strlen ("vFile:open:");
306 if (require_filename (&p
, filename
)
307 || require_comma (&p
)
308 || require_int (&p
, &fileio_flags
)
309 || require_comma (&p
)
310 || require_int (&p
, &fileio_mode
)
312 || fileio_to_host_openflags (fileio_flags
, &flags
)
313 || fileio_to_host_mode (fileio_mode
, &mode
))
315 hostio_packet_error (own_buf
);
319 /* We do not need to convert MODE, since the fileio protocol
320 uses the standard values. */
321 if (hostio_fs_pid
!= 0 && the_target
->multifs_open
!= NULL
)
322 fd
= the_target
->multifs_open (hostio_fs_pid
, filename
,
325 fd
= open (filename
, flags
, mode
);
329 hostio_error (own_buf
);
333 /* Record the new file descriptor. */
334 new_fd
= xmalloc (sizeof (struct fd_list
));
336 new_fd
->next
= open_fds
;
339 hostio_reply (own_buf
, fd
);
343 handle_pread (char *own_buf
, int *new_packet_len
)
345 int fd
, ret
, len
, offset
, bytes_sent
;
348 p
= own_buf
+ strlen ("vFile:pread:");
350 if (require_int (&p
, &fd
)
351 || require_comma (&p
)
352 || require_valid_fd (fd
)
353 || require_int (&p
, &len
)
354 || require_comma (&p
)
355 || require_int (&p
, &offset
)
358 hostio_packet_error (own_buf
);
362 data
= xmalloc (len
);
364 ret
= pread (fd
, data
, len
, offset
);
368 /* If we have no pread or it failed for this file, use lseek/read. */
371 ret
= lseek (fd
, offset
, SEEK_SET
);
373 ret
= read (fd
, data
, len
);
378 hostio_error (own_buf
);
383 bytes_sent
= hostio_reply_with_data (own_buf
, data
, ret
, new_packet_len
);
385 /* If we were using read, and the data did not all fit in the reply,
386 we would have to back up using lseek here. With pread it does
387 not matter. But we still have a problem; the return value in the
388 packet might be wrong, so we must fix it. This time it will
390 if (bytes_sent
< ret
)
391 bytes_sent
= hostio_reply_with_data (own_buf
, data
, bytes_sent
,
398 handle_pwrite (char *own_buf
, int packet_len
)
400 int fd
, ret
, len
, offset
;
403 p
= own_buf
+ strlen ("vFile:pwrite:");
405 if (require_int (&p
, &fd
)
406 || require_comma (&p
)
407 || require_valid_fd (fd
)
408 || require_int (&p
, &offset
)
409 || require_comma (&p
)
410 || require_data (p
, packet_len
- (p
- own_buf
), &data
, &len
))
412 hostio_packet_error (own_buf
);
417 ret
= pwrite (fd
, data
, len
, offset
);
421 /* If we have no pwrite or it failed for this file, use lseek/write. */
424 ret
= lseek (fd
, offset
, SEEK_SET
);
426 ret
= write (fd
, data
, len
);
431 hostio_error (own_buf
);
436 hostio_reply (own_buf
, ret
);
441 handle_fstat (char *own_buf
, int *new_packet_len
)
448 p
= own_buf
+ strlen ("vFile:fstat:");
450 if (require_int (&p
, &fd
)
451 || require_valid_fd (fd
)
454 hostio_packet_error (own_buf
);
458 if (fstat (fd
, &st
) == -1)
460 hostio_error (own_buf
);
464 host_to_fileio_stat (&st
, &fst
);
466 bytes_sent
= hostio_reply_with_data (own_buf
,
467 (char *) &fst
, sizeof (fst
),
470 /* If the response does not fit into a single packet, do not attempt
471 to return a partial response, but simply fail. */
472 if (bytes_sent
< sizeof (fst
))
477 handle_close (char *own_buf
)
481 struct fd_list
**open_fd_p
, *old_fd
;
483 p
= own_buf
+ strlen ("vFile:close:");
485 if (require_int (&p
, &fd
)
486 || require_valid_fd (fd
)
489 hostio_packet_error (own_buf
);
497 hostio_error (own_buf
);
501 open_fd_p
= &open_fds
;
502 /* We know that fd is in the list, thanks to require_valid_fd. */
503 while ((*open_fd_p
)->fd
!= fd
)
504 open_fd_p
= &(*open_fd_p
)->next
;
507 *open_fd_p
= (*open_fd_p
)->next
;
510 hostio_reply (own_buf
, ret
);
514 handle_unlink (char *own_buf
)
516 char filename
[HOSTIO_PATH_MAX
];
520 p
= own_buf
+ strlen ("vFile:unlink:");
522 if (require_filename (&p
, filename
)
525 hostio_packet_error (own_buf
);
529 if (hostio_fs_pid
!= 0 && the_target
->multifs_unlink
!= NULL
)
530 ret
= the_target
->multifs_unlink (hostio_fs_pid
, filename
);
532 ret
= unlink (filename
);
536 hostio_error (own_buf
);
540 hostio_reply (own_buf
, ret
);
544 handle_readlink (char *own_buf
, int *new_packet_len
)
546 char filename
[HOSTIO_PATH_MAX
], linkname
[HOSTIO_PATH_MAX
];
550 p
= own_buf
+ strlen ("vFile:readlink:");
552 if (require_filename (&p
, filename
)
555 hostio_packet_error (own_buf
);
559 if (hostio_fs_pid
!= 0 && the_target
->multifs_readlink
!= NULL
)
560 ret
= the_target
->multifs_readlink (hostio_fs_pid
, filename
,
562 sizeof (linkname
) - 1);
564 ret
= readlink (filename
, linkname
, sizeof (linkname
) - 1);
568 hostio_error (own_buf
);
572 bytes_sent
= hostio_reply_with_data (own_buf
, linkname
, ret
, new_packet_len
);
574 /* If the response does not fit into a single packet, do not attempt
575 to return a partial response, but simply fail. */
576 if (bytes_sent
< ret
)
577 sprintf (own_buf
, "F-1,%x", FILEIO_ENAMETOOLONG
);
580 /* Handle all the 'F' file transfer packets. */
583 handle_vFile (char *own_buf
, int packet_len
, int *new_packet_len
)
585 if (startswith (own_buf
, "vFile:open:"))
586 handle_open (own_buf
);
587 else if (startswith (own_buf
, "vFile:pread:"))
588 handle_pread (own_buf
, new_packet_len
);
589 else if (startswith (own_buf
, "vFile:pwrite:"))
590 handle_pwrite (own_buf
, packet_len
);
591 else if (startswith (own_buf
, "vFile:fstat:"))
592 handle_fstat (own_buf
, new_packet_len
);
593 else if (startswith (own_buf
, "vFile:close:"))
594 handle_close (own_buf
);
595 else if (startswith (own_buf
, "vFile:unlink:"))
596 handle_unlink (own_buf
);
597 else if (startswith (own_buf
, "vFile:readlink:"))
598 handle_readlink (own_buf
, new_packet_len
);
599 else if (startswith (own_buf
, "vFile:setfs:"))
600 handle_setfs (own_buf
);
This page took 0.041665 seconds and 4 git commands to generate.