ubsan: moxie: left shift of negative value
[deliverable/binutils-gdb.git] / gdb / gdbserver / hostio.c
CommitLineData
a6b151f1 1/* Host file transfer support for gdbserver.
42a4f53d 2 Copyright (C) 2007-2019 Free Software Foundation, Inc.
a6b151f1
DJ
3
4 Contributed by CodeSourcery.
5
6 This file is part of GDB.
7
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
32de4b9d 10 the Free Software Foundation; either version 3 of the License, or
a6b151f1
DJ
11 (at your option) any later version.
12
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.
17
18 You should have received a copy of the GNU General Public License
32de4b9d 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
a6b151f1
DJ
20
21#include "server.h"
22#include "gdb/fileio.h"
533b0600 23#include "hostio.h"
a6b151f1 24
a6b151f1
DJ
25#include <fcntl.h>
26#include <limits.h>
27#include <unistd.h>
aa9e327f
GB
28#include <sys/types.h>
29#include <sys/stat.h>
268a13a5 30#include "gdbsupport/fileio.h"
a6b151f1 31
a6b151f1
DJ
32struct fd_list
33{
34 int fd;
35 struct fd_list *next;
36};
37
38static struct fd_list *open_fds;
39
40static int
41safe_fromhex (char a, int *nibble)
42{
43 if (a >= '0' && a <= '9')
44 *nibble = a - '0';
45 else if (a >= 'a' && a <= 'f')
46 *nibble = a - 'a' + 10;
47 else if (a >= 'A' && a <= 'F')
48 *nibble = a - 'A' + 10;
49 else
50 return -1;
51
52 return 0;
53}
54
d5749ee7
PA
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)
59#else
60# define HOSTIO_PATH_MAX PATH_MAX
61#endif
62
a6b151f1
DJ
63static int
64require_filename (char **pp, char *filename)
65{
66 int count;
67 char *p;
68
69 p = *pp;
70 count = 0;
71
72 while (*p && *p != ',')
73 {
74 int nib1, nib2;
75
76 /* Don't allow overflow. */
d5749ee7 77 if (count >= HOSTIO_PATH_MAX - 1)
a6b151f1
DJ
78 return -1;
79
80 if (safe_fromhex (p[0], &nib1)
81 || safe_fromhex (p[1], &nib2))
82 return -1;
83
84 filename[count++] = nib1 * 16 + nib2;
85 p += 2;
86 }
87
88 filename[count] = '\0';
89 *pp = p;
90 return 0;
91}
92
93static int
94require_int (char **pp, int *value)
95{
96 char *p;
81e25b7c 97 int count, firstdigit;
a6b151f1
DJ
98
99 p = *pp;
100 *value = 0;
101 count = 0;
81e25b7c 102 firstdigit = -1;
a6b151f1
DJ
103
104 while (*p && *p != ',')
105 {
106 int nib;
107
81e25b7c 108 if (safe_fromhex (p[0], &nib))
a6b151f1
DJ
109 return -1;
110
81e25b7c
EK
111 if (firstdigit == -1)
112 firstdigit = nib;
113
114 /* Don't allow overflow. */
115 if (count >= 8 || (count == 7 && firstdigit >= 0x8))
a6b151f1 116 return -1;
81e25b7c 117
a6b151f1
DJ
118 *value = *value * 16 + nib;
119 p++;
120 count++;
121 }
122
123 *pp = p;
124 return 0;
125}
126
127static int
128require_data (char *p, int p_len, char **data, int *data_len)
129{
130 int input_index, output_index, escaped;
131
224c3ddb 132 *data = (char *) xmalloc (p_len);
a6b151f1
DJ
133
134 output_index = 0;
135 escaped = 0;
136 for (input_index = 0; input_index < p_len; input_index++)
137 {
138 char b = p[input_index];
139
140 if (escaped)
141 {
142 (*data)[output_index++] = b ^ 0x20;
143 escaped = 0;
144 }
145 else if (b == '}')
146 escaped = 1;
147 else
148 (*data)[output_index++] = b;
149 }
150
151 if (escaped)
9130f83e 152 {
8040bd49 153 free (*data);
9130f83e
MS
154 return -1;
155 }
a6b151f1
DJ
156
157 *data_len = output_index;
158 return 0;
159}
160
161static int
162require_comma (char **pp)
163{
164 if (**pp == ',')
165 {
166 (*pp)++;
167 return 0;
168 }
169 else
170 return -1;
171}
172
173static int
174require_end (char *p)
175{
176 if (*p == '\0')
177 return 0;
178 else
179 return -1;
180}
181
182static int
183require_valid_fd (int fd)
184{
185 struct fd_list *fd_ptr;
186
187 for (fd_ptr = open_fds; fd_ptr != NULL; fd_ptr = fd_ptr->next)
188 if (fd_ptr->fd == fd)
189 return 0;
190
191 return -1;
192}
193
59a016f0
PA
194/* Fill in own_buf with the last hostio error packet, however it
195 suitable for the target. */
a6b151f1 196static void
59a016f0 197hostio_error (char *own_buf)
a6b151f1 198{
59a016f0 199 the_target->hostio_last_error (own_buf);
a6b151f1
DJ
200}
201
202static void
203hostio_packet_error (char *own_buf)
204{
59a016f0 205 sprintf (own_buf, "F-1,%x", FILEIO_EINVAL);
a6b151f1
DJ
206}
207
208static void
209hostio_reply (char *own_buf, int result)
210{
211 sprintf (own_buf, "F%x", result);
212}
213
214static int
215hostio_reply_with_data (char *own_buf, char *buffer, int len,
216 int *new_packet_len)
217{
218 int input_index, output_index, out_maxlen;
219
220 sprintf (own_buf, "F%x;", len);
221 output_index = strlen (own_buf);
222
223 out_maxlen = PBUFSIZ;
224
225 for (input_index = 0; input_index < len; input_index++)
226 {
227 char b = buffer[input_index];
228
229 if (b == '$' || b == '#' || b == '}' || b == '*')
230 {
231 /* These must be escaped. */
232 if (output_index + 2 > out_maxlen)
233 break;
234 own_buf[output_index++] = '}';
235 own_buf[output_index++] = b ^ 0x20;
236 }
237 else
238 {
239 if (output_index + 1 > out_maxlen)
240 break;
241 own_buf[output_index++] = b;
242 }
243 }
244
245 *new_packet_len = output_index;
246 return input_index;
247}
248
14d2069a
GB
249/* Process ID of inferior whose filesystem hostio functions
250 that take FILENAME arguments will use. Zero means to use
251 our own filesystem. */
252
253static int hostio_fs_pid;
254
255/* See hostio.h. */
256
257void
258hostio_handle_new_gdb_connection (void)
259{
260 hostio_fs_pid = 0;
261}
262
263/* Handle a "vFile:setfs:" packet. */
264
265static void
266handle_setfs (char *own_buf)
267{
268 char *p;
269 int pid;
270
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)
278 {
279 own_buf[0] = '\0';
280 return;
281 }
282
283 p = own_buf + strlen ("vFile:setfs:");
284
285 if (require_int (&p, &pid)
286 || pid < 0
287 || require_end (p))
288 {
289 hostio_packet_error (own_buf);
290 return;
291 }
292
293 hostio_fs_pid = pid;
294
295 hostio_reply (own_buf, 0);
296}
297
a6b151f1
DJ
298static void
299handle_open (char *own_buf)
300{
d5749ee7 301 char filename[HOSTIO_PATH_MAX];
a6b151f1 302 char *p;
3ac2e371
GB
303 int fileio_flags, fileio_mode, flags, fd;
304 mode_t mode;
a6b151f1
DJ
305 struct fd_list *new_fd;
306
307 p = own_buf + strlen ("vFile:open:");
308
309 if (require_filename (&p, filename)
310 || require_comma (&p)
311 || require_int (&p, &fileio_flags)
312 || require_comma (&p)
3ac2e371 313 || require_int (&p, &fileio_mode)
a6b151f1 314 || require_end (p)
3ac2e371
GB
315 || fileio_to_host_openflags (fileio_flags, &flags)
316 || fileio_to_host_mode (fileio_mode, &mode))
a6b151f1
DJ
317 {
318 hostio_packet_error (own_buf);
319 return;
320 }
321
322 /* We do not need to convert MODE, since the fileio protocol
323 uses the standard values. */
14d2069a
GB
324 if (hostio_fs_pid != 0 && the_target->multifs_open != NULL)
325 fd = the_target->multifs_open (hostio_fs_pid, filename,
326 flags, mode);
327 else
328 fd = open (filename, flags, mode);
a6b151f1
DJ
329
330 if (fd == -1)
331 {
59a016f0 332 hostio_error (own_buf);
a6b151f1
DJ
333 return;
334 }
335
336 /* Record the new file descriptor. */
8d749320 337 new_fd = XNEW (struct fd_list);
a6b151f1
DJ
338 new_fd->fd = fd;
339 new_fd->next = open_fds;
340 open_fds = new_fd;
341
342 hostio_reply (own_buf, fd);
343}
344
345static void
346handle_pread (char *own_buf, int *new_packet_len)
347{
348 int fd, ret, len, offset, bytes_sent;
349 char *p, *data;
45face3b 350 static int max_reply_size = -1;
a6b151f1
DJ
351
352 p = own_buf + strlen ("vFile:pread:");
353
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)
360 || require_end (p))
361 {
362 hostio_packet_error (own_buf);
363 return;
364 }
365
45face3b
GB
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)
370 {
371 sprintf (own_buf, "F%x;", PBUFSIZ);
372 max_reply_size = PBUFSIZ - strlen (own_buf);
373 }
374 if (len > max_reply_size)
375 len = max_reply_size;
376
224c3ddb 377 data = (char *) xmalloc (len);
4e799345 378#ifdef HAVE_PREAD
a6b151f1 379 ret = pread (fd, data, len, offset);
4e799345 380#else
7c3270ae 381 ret = -1;
4e799345 382#endif
7c3270ae
UW
383 /* If we have no pread or it failed for this file, use lseek/read. */
384 if (ret == -1)
385 {
386 ret = lseek (fd, offset, SEEK_SET);
387 if (ret != -1)
388 ret = read (fd, data, len);
389 }
a6b151f1
DJ
390
391 if (ret == -1)
392 {
59a016f0 393 hostio_error (own_buf);
a6b151f1
DJ
394 free (data);
395 return;
396 }
397
398 bytes_sent = hostio_reply_with_data (own_buf, data, ret, new_packet_len);
399
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
404 definitely fit. */
405 if (bytes_sent < ret)
406 bytes_sent = hostio_reply_with_data (own_buf, data, bytes_sent,
407 new_packet_len);
408
409 free (data);
410}
411
412static void
413handle_pwrite (char *own_buf, int packet_len)
414{
415 int fd, ret, len, offset;
416 char *p, *data;
417
418 p = own_buf + strlen ("vFile:pwrite:");
419
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))
426 {
427 hostio_packet_error (own_buf);
428 return;
429 }
430
4e799345 431#ifdef HAVE_PWRITE
a6b151f1 432 ret = pwrite (fd, data, len, offset);
4e799345 433#else
7c3270ae 434 ret = -1;
4e799345 435#endif
7c3270ae
UW
436 /* If we have no pwrite or it failed for this file, use lseek/write. */
437 if (ret == -1)
438 {
439 ret = lseek (fd, offset, SEEK_SET);
440 if (ret != -1)
441 ret = write (fd, data, len);
442 }
a6b151f1
DJ
443
444 if (ret == -1)
445 {
59a016f0 446 hostio_error (own_buf);
a6b151f1
DJ
447 free (data);
448 return;
449 }
450
451 hostio_reply (own_buf, ret);
452 free (data);
453}
454
aa9e327f
GB
455static void
456handle_fstat (char *own_buf, int *new_packet_len)
457{
458 int fd, bytes_sent;
459 char *p;
460 struct stat st;
461 struct fio_stat fst;
462
463 p = own_buf + strlen ("vFile:fstat:");
464
465 if (require_int (&p, &fd)
466 || require_valid_fd (fd)
467 || require_end (p))
468 {
469 hostio_packet_error (own_buf);
470 return;
471 }
472
473 if (fstat (fd, &st) == -1)
474 {
475 hostio_error (own_buf);
476 return;
477 }
478
7823a941 479 host_to_fileio_stat (&st, &fst);
aa9e327f
GB
480
481 bytes_sent = hostio_reply_with_data (own_buf,
482 (char *) &fst, sizeof (fst),
483 new_packet_len);
484
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))
488 write_enn (own_buf);
489}
490
a6b151f1
DJ
491static void
492handle_close (char *own_buf)
493{
494 int fd, ret;
495 char *p;
496 struct fd_list **open_fd_p, *old_fd;
497
498 p = own_buf + strlen ("vFile:close:");
499
500 if (require_int (&p, &fd)
501 || require_valid_fd (fd)
502 || require_end (p))
503 {
504 hostio_packet_error (own_buf);
505 return;
506 }
507
508 ret = close (fd);
509
510 if (ret == -1)
511 {
59a016f0 512 hostio_error (own_buf);
a6b151f1
DJ
513 return;
514 }
515
516 open_fd_p = &open_fds;
588eebee
MS
517 /* We know that fd is in the list, thanks to require_valid_fd. */
518 while ((*open_fd_p)->fd != fd)
a6b151f1
DJ
519 open_fd_p = &(*open_fd_p)->next;
520
521 old_fd = *open_fd_p;
522 *open_fd_p = (*open_fd_p)->next;
523 free (old_fd);
524
525 hostio_reply (own_buf, ret);
526}
527
528static void
529handle_unlink (char *own_buf)
530{
d5749ee7 531 char filename[HOSTIO_PATH_MAX];
a6b151f1
DJ
532 char *p;
533 int ret;
534
535 p = own_buf + strlen ("vFile:unlink:");
536
537 if (require_filename (&p, filename)
538 || require_end (p))
539 {
540 hostio_packet_error (own_buf);
541 return;
542 }
543
14d2069a
GB
544 if (hostio_fs_pid != 0 && the_target->multifs_unlink != NULL)
545 ret = the_target->multifs_unlink (hostio_fs_pid, filename);
546 else
547 ret = unlink (filename);
a6b151f1
DJ
548
549 if (ret == -1)
550 {
59a016f0 551 hostio_error (own_buf);
a6b151f1
DJ
552 return;
553 }
554
555 hostio_reply (own_buf, ret);
556}
557
b9e7b9c3
UW
558static void
559handle_readlink (char *own_buf, int *new_packet_len)
560{
d5749ee7 561 char filename[HOSTIO_PATH_MAX], linkname[HOSTIO_PATH_MAX];
b9e7b9c3
UW
562 char *p;
563 int ret, bytes_sent;
564
565 p = own_buf + strlen ("vFile:readlink:");
566
567 if (require_filename (&p, filename)
568 || require_end (p))
569 {
570 hostio_packet_error (own_buf);
571 return;
572 }
573
14d2069a
GB
574 if (hostio_fs_pid != 0 && the_target->multifs_readlink != NULL)
575 ret = the_target->multifs_readlink (hostio_fs_pid, filename,
576 linkname,
577 sizeof (linkname) - 1);
578 else
579 ret = readlink (filename, linkname, sizeof (linkname) - 1);
580
b9e7b9c3
UW
581 if (ret == -1)
582 {
583 hostio_error (own_buf);
584 return;
585 }
586
587 bytes_sent = hostio_reply_with_data (own_buf, linkname, ret, new_packet_len);
588
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);
593}
594
a6b151f1
DJ
595/* Handle all the 'F' file transfer packets. */
596
597int
598handle_vFile (char *own_buf, int packet_len, int *new_packet_len)
599{
61012eef 600 if (startswith (own_buf, "vFile:open:"))
a6b151f1 601 handle_open (own_buf);
61012eef 602 else if (startswith (own_buf, "vFile:pread:"))
a6b151f1 603 handle_pread (own_buf, new_packet_len);
61012eef 604 else if (startswith (own_buf, "vFile:pwrite:"))
a6b151f1 605 handle_pwrite (own_buf, packet_len);
aa9e327f
GB
606 else if (startswith (own_buf, "vFile:fstat:"))
607 handle_fstat (own_buf, new_packet_len);
61012eef 608 else if (startswith (own_buf, "vFile:close:"))
a6b151f1 609 handle_close (own_buf);
61012eef 610 else if (startswith (own_buf, "vFile:unlink:"))
a6b151f1 611 handle_unlink (own_buf);
61012eef 612 else if (startswith (own_buf, "vFile:readlink:"))
b9e7b9c3 613 handle_readlink (own_buf, new_packet_len);
14d2069a
GB
614 else if (startswith (own_buf, "vFile:setfs:"))
615 handle_setfs (own_buf);
a6b151f1
DJ
616 else
617 return 0;
618
619 return 1;
620}
This page took 1.542911 seconds and 4 git commands to generate.