1 /* Host file transfer support for gdbserver.
2 Copyright (C) 2007-2013 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 extern int remote_debug
;
36 static struct fd_list
*open_fds
;
39 safe_fromhex (char a
, int *nibble
)
41 if (a
>= '0' && a
<= '9')
43 else if (a
>= 'a' && a
<= 'f')
44 *nibble
= a
- 'a' + 10;
45 else if (a
>= 'A' && a
<= 'F')
46 *nibble
= a
- 'A' + 10;
53 /* Filenames are hex encoded, so the maximum we can handle is half the
54 packet buffer size. Cap to PATH_MAX, if it is shorter. */
55 #if !defined (PATH_MAX) || (PATH_MAX > (PBUFSIZ / 2 + 1))
56 # define HOSTIO_PATH_MAX (PBUFSIZ / 2 + 1)
58 # define HOSTIO_PATH_MAX PATH_MAX
62 require_filename (char **pp
, char *filename
)
70 while (*p
&& *p
!= ',')
74 /* Don't allow overflow. */
75 if (count
>= HOSTIO_PATH_MAX
- 1)
78 if (safe_fromhex (p
[0], &nib1
)
79 || safe_fromhex (p
[1], &nib2
))
82 filename
[count
++] = nib1
* 16 + nib2
;
86 filename
[count
] = '\0';
92 require_int (char **pp
, int *value
)
101 while (*p
&& *p
!= ',')
105 /* Don't allow overflow. */
109 if (safe_fromhex (p
[0], &nib
))
111 *value
= *value
* 16 + nib
;
121 require_data (char *p
, int p_len
, char **data
, int *data_len
)
123 int input_index
, output_index
, escaped
;
125 *data
= xmalloc (p_len
);
129 for (input_index
= 0; input_index
< p_len
; input_index
++)
131 char b
= p
[input_index
];
135 (*data
)[output_index
++] = b
^ 0x20;
141 (*data
)[output_index
++] = b
;
150 *data_len
= output_index
;
155 require_comma (char **pp
)
167 require_end (char *p
)
176 require_valid_fd (int fd
)
178 struct fd_list
*fd_ptr
;
180 for (fd_ptr
= open_fds
; fd_ptr
!= NULL
; fd_ptr
= fd_ptr
->next
)
181 if (fd_ptr
->fd
== fd
)
187 /* Fill in own_buf with the last hostio error packet, however it
188 suitable for the target. */
190 hostio_error (char *own_buf
)
192 the_target
->hostio_last_error (own_buf
);
196 hostio_packet_error (char *own_buf
)
198 sprintf (own_buf
, "F-1,%x", FILEIO_EINVAL
);
202 hostio_reply (char *own_buf
, int result
)
204 sprintf (own_buf
, "F%x", result
);
208 hostio_reply_with_data (char *own_buf
, char *buffer
, int len
,
211 int input_index
, output_index
, out_maxlen
;
213 sprintf (own_buf
, "F%x;", len
);
214 output_index
= strlen (own_buf
);
216 out_maxlen
= PBUFSIZ
;
218 for (input_index
= 0; input_index
< len
; input_index
++)
220 char b
= buffer
[input_index
];
222 if (b
== '$' || b
== '#' || b
== '}' || b
== '*')
224 /* These must be escaped. */
225 if (output_index
+ 2 > out_maxlen
)
227 own_buf
[output_index
++] = '}';
228 own_buf
[output_index
++] = b
^ 0x20;
232 if (output_index
+ 1 > out_maxlen
)
234 own_buf
[output_index
++] = b
;
238 *new_packet_len
= output_index
;
243 fileio_open_flags_to_host (int fileio_open_flags
, int *open_flags_p
)
247 if (fileio_open_flags
& ~FILEIO_O_SUPPORTED
)
250 if (fileio_open_flags
& FILEIO_O_CREAT
)
251 open_flags
|= O_CREAT
;
252 if (fileio_open_flags
& FILEIO_O_EXCL
)
253 open_flags
|= O_EXCL
;
254 if (fileio_open_flags
& FILEIO_O_TRUNC
)
255 open_flags
|= O_TRUNC
;
256 if (fileio_open_flags
& FILEIO_O_APPEND
)
257 open_flags
|= O_APPEND
;
258 if (fileio_open_flags
& FILEIO_O_RDONLY
)
259 open_flags
|= O_RDONLY
;
260 if (fileio_open_flags
& FILEIO_O_WRONLY
)
261 open_flags
|= O_WRONLY
;
262 if (fileio_open_flags
& FILEIO_O_RDWR
)
263 open_flags
|= O_RDWR
;
264 /* On systems supporting binary and text mode, always open files in
267 open_flags
|= O_BINARY
;
270 *open_flags_p
= open_flags
;
275 handle_open (char *own_buf
)
277 char filename
[HOSTIO_PATH_MAX
];
279 int fileio_flags
, mode
, flags
, fd
;
280 struct fd_list
*new_fd
;
282 p
= own_buf
+ strlen ("vFile:open:");
284 if (require_filename (&p
, filename
)
285 || require_comma (&p
)
286 || require_int (&p
, &fileio_flags
)
287 || require_comma (&p
)
288 || require_int (&p
, &mode
)
290 || fileio_open_flags_to_host (fileio_flags
, &flags
))
292 hostio_packet_error (own_buf
);
296 /* We do not need to convert MODE, since the fileio protocol
297 uses the standard values. */
298 fd
= open (filename
, flags
, mode
);
302 hostio_error (own_buf
);
306 /* Record the new file descriptor. */
307 new_fd
= xmalloc (sizeof (struct fd_list
));
309 new_fd
->next
= open_fds
;
312 hostio_reply (own_buf
, fd
);
316 handle_pread (char *own_buf
, int *new_packet_len
)
318 int fd
, ret
, len
, offset
, bytes_sent
;
321 p
= own_buf
+ strlen ("vFile:pread:");
323 if (require_int (&p
, &fd
)
324 || require_comma (&p
)
325 || require_valid_fd (fd
)
326 || require_int (&p
, &len
)
327 || require_comma (&p
)
328 || require_int (&p
, &offset
)
331 hostio_packet_error (own_buf
);
335 data
= xmalloc (len
);
337 ret
= pread (fd
, data
, len
, offset
);
341 /* If we have no pread or it failed for this file, use lseek/read. */
344 ret
= lseek (fd
, offset
, SEEK_SET
);
346 ret
= read (fd
, data
, len
);
351 hostio_error (own_buf
);
356 bytes_sent
= hostio_reply_with_data (own_buf
, data
, ret
, new_packet_len
);
358 /* If we were using read, and the data did not all fit in the reply,
359 we would have to back up using lseek here. With pread it does
360 not matter. But we still have a problem; the return value in the
361 packet might be wrong, so we must fix it. This time it will
363 if (bytes_sent
< ret
)
364 bytes_sent
= hostio_reply_with_data (own_buf
, data
, bytes_sent
,
371 handle_pwrite (char *own_buf
, int packet_len
)
373 int fd
, ret
, len
, offset
;
376 p
= own_buf
+ strlen ("vFile:pwrite:");
378 if (require_int (&p
, &fd
)
379 || require_comma (&p
)
380 || require_valid_fd (fd
)
381 || require_int (&p
, &offset
)
382 || require_comma (&p
)
383 || require_data (p
, packet_len
- (p
- own_buf
), &data
, &len
))
385 hostio_packet_error (own_buf
);
390 ret
= pwrite (fd
, data
, len
, offset
);
394 /* If we have no pwrite or it failed for this file, use lseek/write. */
397 ret
= lseek (fd
, offset
, SEEK_SET
);
399 ret
= write (fd
, data
, len
);
404 hostio_error (own_buf
);
409 hostio_reply (own_buf
, ret
);
414 handle_close (char *own_buf
)
418 struct fd_list
**open_fd_p
, *old_fd
;
420 p
= own_buf
+ strlen ("vFile:close:");
422 if (require_int (&p
, &fd
)
423 || require_valid_fd (fd
)
426 hostio_packet_error (own_buf
);
434 hostio_error (own_buf
);
438 open_fd_p
= &open_fds
;
439 /* We know that fd is in the list, thanks to require_valid_fd. */
440 while ((*open_fd_p
)->fd
!= fd
)
441 open_fd_p
= &(*open_fd_p
)->next
;
444 *open_fd_p
= (*open_fd_p
)->next
;
447 hostio_reply (own_buf
, ret
);
451 handle_unlink (char *own_buf
)
453 char filename
[HOSTIO_PATH_MAX
];
457 p
= own_buf
+ strlen ("vFile:unlink:");
459 if (require_filename (&p
, filename
)
462 hostio_packet_error (own_buf
);
466 ret
= unlink (filename
);
470 hostio_error (own_buf
);
474 hostio_reply (own_buf
, ret
);
478 handle_readlink (char *own_buf
, int *new_packet_len
)
480 #if defined (HAVE_READLINK)
481 char filename
[HOSTIO_PATH_MAX
], linkname
[HOSTIO_PATH_MAX
];
485 p
= own_buf
+ strlen ("vFile:readlink:");
487 if (require_filename (&p
, filename
)
490 hostio_packet_error (own_buf
);
494 ret
= readlink (filename
, linkname
, sizeof (linkname
) - 1);
497 hostio_error (own_buf
);
501 bytes_sent
= hostio_reply_with_data (own_buf
, linkname
, ret
, new_packet_len
);
503 /* If the response does not fit into a single packet, do not attempt
504 to return a partial response, but simply fail. */
505 if (bytes_sent
< ret
)
506 sprintf (own_buf
, "F-1,%x", FILEIO_ENAMETOOLONG
);
507 #else /* ! HAVE_READLINK */
508 sprintf (own_buf
, "F-1,%x", FILEIO_ENOSYS
);
512 /* Handle all the 'F' file transfer packets. */
515 handle_vFile (char *own_buf
, int packet_len
, int *new_packet_len
)
517 if (strncmp (own_buf
, "vFile:open:", 11) == 0)
518 handle_open (own_buf
);
519 else if (strncmp (own_buf
, "vFile:pread:", 11) == 0)
520 handle_pread (own_buf
, new_packet_len
);
521 else if (strncmp (own_buf
, "vFile:pwrite:", 12) == 0)
522 handle_pwrite (own_buf
, packet_len
);
523 else if (strncmp (own_buf
, "vFile:close:", 12) == 0)
524 handle_close (own_buf
);
525 else if (strncmp (own_buf
, "vFile:unlink:", 13) == 0)
526 handle_unlink (own_buf
);
527 else if (strncmp (own_buf
, "vFile:readlink:", 15) == 0)
528 handle_readlink (own_buf
, new_packet_len
);
This page took 0.046066 seconds and 4 git commands to generate.