Move linux_find_memory_regions_full & co.
[deliverable/binutils-gdb.git] / gdb / target / target-utils.c
1 /* Utility target functions for GDB, and GDBserver.
2
3 Copyright (C) 2015 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "common-defs.h"
21 #include "target/target-utils.h"
22
23 LONGEST
24 read_alloc (gdb_byte **buf_p, int handle, read_alloc_pread_ftype *pread_func,
25 int padding)
26 {
27 size_t buf_alloc, buf_pos;
28 gdb_byte *buf;
29 LONGEST n;
30 int target_errno;
31
32 /* Start by reading up to 4K at a time. The target will throttle
33 this number down if necessary. */
34 buf_alloc = 4096;
35 buf = xmalloc (buf_alloc);
36 buf_pos = 0;
37 while (1)
38 {
39 n = pread_func (handle, &buf[buf_pos], buf_alloc - buf_pos - padding,
40 buf_pos, &target_errno);
41 if (n <= 0)
42 {
43 if (n < 0 || (n == 0 && buf_pos == 0))
44 xfree (buf);
45 else
46 *buf_p = buf;
47 if (n < 0)
48 {
49 /* An error occurred. */
50 return -1;
51 }
52 else
53 {
54 /* Read all there was. */
55 return buf_pos;
56 }
57 }
58
59 buf_pos += n;
60
61 /* If the buffer is filling up, expand it. */
62 if (buf_alloc < buf_pos * 2)
63 {
64 buf_alloc *= 2;
65 buf = xrealloc (buf, buf_alloc);
66 }
67 }
68 }
69
70 char *
71 read_stralloc (struct inferior *inf, const char *filename,
72 read_stralloc_func_ftype *func)
73 {
74 gdb_byte *buffer;
75 char *bufstr;
76 LONGEST i, transferred;
77
78 transferred = func (inf, filename, &buffer, 1);
79 bufstr = (char *) buffer;
80
81 if (transferred < 0)
82 return NULL;
83
84 if (transferred == 0)
85 return xstrdup ("");
86
87 bufstr[transferred] = 0;
88
89 /* Check for embedded NUL bytes; but allow trailing NULs. */
90 for (i = strlen (bufstr); i < transferred; i++)
91 if (bufstr[i] != 0)
92 {
93 warning (_("target file %s "
94 "contained unexpected null characters"),
95 filename);
96 break;
97 }
98
99 return bufstr;
100 }
This page took 0.033516 seconds and 4 git commands to generate.