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>
30 #include "common-remote-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
)
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
;
247 fileio_open_flags_to_host (int fileio_open_flags
, int *open_flags_p
)
251 if (fileio_open_flags
& ~FILEIO_O_SUPPORTED
)
254 if (fileio_open_flags
& FILEIO_O_CREAT
)
255 open_flags
|= O_CREAT
;
256 if (fileio_open_flags
& FILEIO_O_EXCL
)
257 open_flags
|= O_EXCL
;
258 if (fileio_open_flags
& FILEIO_O_TRUNC
)
259 open_flags
|= O_TRUNC
;
260 if (fileio_open_flags
& FILEIO_O_APPEND
)
261 open_flags
|= O_APPEND
;
262 if (fileio_open_flags
& FILEIO_O_RDONLY
)
263 open_flags
|= O_RDONLY
;
264 if (fileio_open_flags
& FILEIO_O_WRONLY
)
265 open_flags
|= O_WRONLY
;
266 if (fileio_open_flags
& FILEIO_O_RDWR
)
267 open_flags
|= O_RDWR
;
268 /* On systems supporting binary and text mode, always open files in
271 open_flags
|= O_BINARY
;
274 *open_flags_p
= open_flags
;
279 handle_open (char *own_buf
)
281 char filename
[HOSTIO_PATH_MAX
];
283 int fileio_flags
, mode
, flags
, fd
;
284 struct fd_list
*new_fd
;
286 p
= own_buf
+ strlen ("vFile:open:");
288 if (require_filename (&p
, filename
)
289 || require_comma (&p
)
290 || require_int (&p
, &fileio_flags
)
291 || require_comma (&p
)
292 || require_int (&p
, &mode
)
294 || fileio_open_flags_to_host (fileio_flags
, &flags
))
296 hostio_packet_error (own_buf
);
300 /* We do not need to convert MODE, since the fileio protocol
301 uses the standard values. */
302 fd
= open (filename
, flags
, mode
);
306 hostio_error (own_buf
);
310 /* Record the new file descriptor. */
311 new_fd
= xmalloc (sizeof (struct fd_list
));
313 new_fd
->next
= open_fds
;
316 hostio_reply (own_buf
, fd
);
320 handle_pread (char *own_buf
, int *new_packet_len
)
322 int fd
, ret
, len
, offset
, bytes_sent
;
325 p
= own_buf
+ strlen ("vFile:pread:");
327 if (require_int (&p
, &fd
)
328 || require_comma (&p
)
329 || require_valid_fd (fd
)
330 || require_int (&p
, &len
)
331 || require_comma (&p
)
332 || require_int (&p
, &offset
)
335 hostio_packet_error (own_buf
);
339 data
= xmalloc (len
);
341 ret
= pread (fd
, data
, len
, offset
);
345 /* If we have no pread or it failed for this file, use lseek/read. */
348 ret
= lseek (fd
, offset
, SEEK_SET
);
350 ret
= read (fd
, data
, len
);
355 hostio_error (own_buf
);
360 bytes_sent
= hostio_reply_with_data (own_buf
, data
, ret
, new_packet_len
);
362 /* If we were using read, and the data did not all fit in the reply,
363 we would have to back up using lseek here. With pread it does
364 not matter. But we still have a problem; the return value in the
365 packet might be wrong, so we must fix it. This time it will
367 if (bytes_sent
< ret
)
368 bytes_sent
= hostio_reply_with_data (own_buf
, data
, bytes_sent
,
375 handle_pwrite (char *own_buf
, int packet_len
)
377 int fd
, ret
, len
, offset
;
380 p
= own_buf
+ strlen ("vFile:pwrite:");
382 if (require_int (&p
, &fd
)
383 || require_comma (&p
)
384 || require_valid_fd (fd
)
385 || require_int (&p
, &offset
)
386 || require_comma (&p
)
387 || require_data (p
, packet_len
- (p
- own_buf
), &data
, &len
))
389 hostio_packet_error (own_buf
);
394 ret
= pwrite (fd
, data
, len
, offset
);
398 /* If we have no pwrite or it failed for this file, use lseek/write. */
401 ret
= lseek (fd
, offset
, SEEK_SET
);
403 ret
= write (fd
, data
, len
);
408 hostio_error (own_buf
);
413 hostio_reply (own_buf
, ret
);
418 handle_fstat (char *own_buf
, int *new_packet_len
)
425 p
= own_buf
+ strlen ("vFile:fstat:");
427 if (require_int (&p
, &fd
)
428 || require_valid_fd (fd
)
431 hostio_packet_error (own_buf
);
435 if (fstat (fd
, &st
) == -1)
437 hostio_error (own_buf
);
441 remote_fileio_to_fio_stat (&st
, &fst
);
443 bytes_sent
= hostio_reply_with_data (own_buf
,
444 (char *) &fst
, sizeof (fst
),
447 /* If the response does not fit into a single packet, do not attempt
448 to return a partial response, but simply fail. */
449 if (bytes_sent
< sizeof (fst
))
454 handle_close (char *own_buf
)
458 struct fd_list
**open_fd_p
, *old_fd
;
460 p
= own_buf
+ strlen ("vFile:close:");
462 if (require_int (&p
, &fd
)
463 || require_valid_fd (fd
)
466 hostio_packet_error (own_buf
);
474 hostio_error (own_buf
);
478 open_fd_p
= &open_fds
;
479 /* We know that fd is in the list, thanks to require_valid_fd. */
480 while ((*open_fd_p
)->fd
!= fd
)
481 open_fd_p
= &(*open_fd_p
)->next
;
484 *open_fd_p
= (*open_fd_p
)->next
;
487 hostio_reply (own_buf
, ret
);
491 handle_unlink (char *own_buf
)
493 char filename
[HOSTIO_PATH_MAX
];
497 p
= own_buf
+ strlen ("vFile:unlink:");
499 if (require_filename (&p
, filename
)
502 hostio_packet_error (own_buf
);
506 ret
= unlink (filename
);
510 hostio_error (own_buf
);
514 hostio_reply (own_buf
, ret
);
518 handle_readlink (char *own_buf
, int *new_packet_len
)
520 char filename
[HOSTIO_PATH_MAX
], linkname
[HOSTIO_PATH_MAX
];
524 p
= own_buf
+ strlen ("vFile:readlink:");
526 if (require_filename (&p
, filename
)
529 hostio_packet_error (own_buf
);
533 ret
= readlink (filename
, linkname
, sizeof (linkname
) - 1);
536 hostio_error (own_buf
);
540 bytes_sent
= hostio_reply_with_data (own_buf
, linkname
, ret
, new_packet_len
);
542 /* If the response does not fit into a single packet, do not attempt
543 to return a partial response, but simply fail. */
544 if (bytes_sent
< ret
)
545 sprintf (own_buf
, "F-1,%x", FILEIO_ENAMETOOLONG
);
548 /* Handle all the 'F' file transfer packets. */
551 handle_vFile (char *own_buf
, int packet_len
, int *new_packet_len
)
553 if (startswith (own_buf
, "vFile:open:"))
554 handle_open (own_buf
);
555 else if (startswith (own_buf
, "vFile:pread:"))
556 handle_pread (own_buf
, new_packet_len
);
557 else if (startswith (own_buf
, "vFile:pwrite:"))
558 handle_pwrite (own_buf
, packet_len
);
559 else if (startswith (own_buf
, "vFile:fstat:"))
560 handle_fstat (own_buf
, new_packet_len
);
561 else if (startswith (own_buf
, "vFile:close:"))
562 handle_close (own_buf
);
563 else if (startswith (own_buf
, "vFile:unlink:"))
564 handle_unlink (own_buf
);
565 else if (startswith (own_buf
, "vFile:readlink:"))
566 handle_readlink (own_buf
, new_packet_len
);
This page took 0.041873 seconds and 4 git commands to generate.