455f18dcffbbd20e9f1b8f1da89177494997b40b
[deliverable/binutils-gdb.git] / libctf / ctf-subr.c
1 /* Simple subrs.
2 Copyright (C) 2019-2020 Free Software Foundation, Inc.
3
4 This file is part of libctf.
5
6 libctf is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include <ctf-impl.h>
21 #ifdef HAVE_MMAP
22 #include <sys/mman.h>
23 #endif
24 #include <sys/types.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 int _libctf_version = CTF_VERSION; /* Library client version. */
30 int _libctf_debug = 0; /* Debugging messages enabled. */
31
32 /* Private, read-only mmap from a file, with fallback to copying.
33
34 No handling of page-offset issues at all: the caller must allow for that. */
35
36 _libctf_malloc_ void *
37 ctf_mmap (size_t length, size_t offset, int fd)
38 {
39 void *data;
40
41 #ifdef HAVE_MMAP
42 data = mmap (NULL, length, PROT_READ, MAP_PRIVATE, fd, offset);
43 if (data == MAP_FAILED)
44 data = NULL;
45 #else
46 if ((data = malloc (length)) != NULL)
47 {
48 if (ctf_pread (fd, data, length, offset) <= 0)
49 {
50 free (data);
51 data = NULL;
52 }
53 }
54 #endif
55 return data;
56 }
57
58 void
59 ctf_munmap (void *buf, size_t length _libctf_unused_)
60 {
61 #ifdef HAVE_MMAP
62 (void) munmap (buf, length);
63 #else
64 free (buf);
65 #endif
66 }
67
68 ssize_t
69 ctf_pread (int fd, void *buf, ssize_t count, off_t offset)
70 {
71 ssize_t len;
72 size_t acc = 0;
73 char *data = (char *) buf;
74
75 #ifdef HAVE_PREAD
76 while (count > 0)
77 {
78 errno = 0;
79 if (((len = pread (fd, data, count, offset)) < 0) &&
80 errno != EINTR)
81 return len;
82 if (errno == EINTR)
83 continue;
84
85 acc += len;
86 if (len == 0) /* EOF. */
87 return acc;
88
89 count -= len;
90 offset += len;
91 data += len;
92 }
93 return acc;
94 #else
95 off_t orig_off;
96
97 if ((orig_off = lseek (fd, 0, SEEK_CUR)) < 0)
98 return -1;
99 if ((lseek (fd, offset, SEEK_SET)) < 0)
100 return -1;
101
102 while (count > 0)
103 {
104 errno = 0;
105 if (((len = read (fd, data, count)) < 0) &&
106 errno != EINTR)
107 return len;
108 if (errno == EINTR)
109 continue;
110
111 acc += len;
112 if (len == 0) /* EOF. */
113 break;
114
115 count -= len;
116 data += len;
117 }
118 if ((lseek (fd, orig_off, SEEK_SET)) < 0)
119 return -1; /* offset is smashed. */
120 #endif
121
122 return acc;
123 }
124
125 /* Set the CTF library client version to the specified version. If version is
126 zero, we just return the default library version number. */
127 int
128 ctf_version (int version)
129 {
130 if (version < 0)
131 {
132 errno = EINVAL;
133 return -1;
134 }
135
136 if (version > 0)
137 {
138 /* Dynamic version switching is not presently supported. */
139 if (version != CTF_VERSION)
140 {
141 errno = ENOTSUP;
142 return -1;
143 }
144 ctf_dprintf ("ctf_version: client using version %d\n", version);
145 _libctf_version = version;
146 }
147
148 return _libctf_version;
149 }
150
151 void
152 libctf_init_debug (void)
153 {
154 static int inited;
155 if (!inited)
156 {
157 _libctf_debug = getenv ("LIBCTF_DEBUG") != NULL;
158 inited = 1;
159 }
160 }
161
162 void ctf_setdebug (int debug)
163 {
164 /* Ensure that libctf_init_debug() has been called, so that we don't get our
165 debugging-on-or-off smashed by the next call. */
166
167 libctf_init_debug();
168 _libctf_debug = debug;
169 ctf_dprintf ("CTF debugging set to %i\n", debug);
170 }
171
172 int ctf_getdebug (void)
173 {
174 return _libctf_debug;
175 }
176
177 _libctf_printflike_ (1, 2)
178 void ctf_dprintf (const char *format, ...)
179 {
180 if (_libctf_unlikely_ (_libctf_debug))
181 {
182 va_list alist;
183
184 va_start (alist, format);
185 fflush (stdout);
186 (void) fputs ("libctf DEBUG: ", stderr);
187 (void) vfprintf (stderr, format, alist);
188 va_end (alist);
189 }
190 }
191
192 /* Errors and warnings. */
193 _libctf_printflike_ (3, 4)
194 extern void
195 ctf_err_warn (ctf_file_t *fp, int is_warning, const char *format, ...)
196 {
197 va_list alist;
198 ctf_err_warning_t *cew;
199
200 /* Don't bother reporting errors here: we can't do much about them if they
201 happen. If we're so short of memory that a tiny malloc doesn't work, a
202 vfprintf isn't going to work either and the caller will have to rely on the
203 ENOMEM return they'll be getting in short order anyway. */
204
205 if ((cew = malloc (sizeof (ctf_err_warning_t))) == NULL)
206 return;
207
208 cew->cew_is_warning = is_warning;
209 va_start (alist, format);
210 if (vasprintf (&cew->cew_text, format, alist) < 0)
211 {
212 free (cew);
213 va_end (alist);
214 return;
215 }
216 va_end (alist);
217
218 ctf_dprintf ("%s: %s\n", is_warning ? "error" : "warning", cew->cew_text);
219
220 ctf_list_append (&fp->ctf_errs_warnings, cew);
221 }
222
223 /* Error-warning reporting: an 'iterator' that returns errors and warnings from
224 the error/warning list, in order of emission. Errors and warnings are popped
225 after return: the caller must free the returned error-text pointer. */
226 char *
227 ctf_errwarning_next (ctf_file_t *fp, ctf_next_t **it, int *is_warning)
228 {
229 ctf_next_t *i = *it;
230 char *ret;
231 ctf_err_warning_t *cew;
232
233 if (!i)
234 {
235 if ((i = ctf_next_create ()) == NULL)
236 {
237 ctf_set_errno (fp, ENOMEM);
238 return NULL;
239 }
240
241 i->cu.ctn_fp = fp;
242 i->ctn_iter_fun = (void (*) (void)) ctf_errwarning_next;
243 *it = i;
244 }
245
246 if ((void (*) (void)) ctf_errwarning_next != i->ctn_iter_fun)
247 {
248 ctf_set_errno (fp, ECTF_NEXT_WRONGFUN);
249 return NULL;
250 }
251
252 if (fp != i->cu.ctn_fp)
253 {
254 ctf_set_errno (fp, ECTF_NEXT_WRONGFP);
255 return NULL;
256 }
257
258 cew = ctf_list_next (&fp->ctf_errs_warnings);
259
260 if (!cew)
261 {
262 ctf_next_destroy (i);
263 *it = NULL;
264 ctf_set_errno (fp, ECTF_NEXT_END);
265 return NULL;
266 }
267
268 if (is_warning)
269 *is_warning = cew->cew_is_warning;
270 ret = cew->cew_text;
271 ctf_list_delete (&fp->ctf_errs_warnings, cew);
272 free (cew);
273 return ret;
274 }
275
276 void
277 ctf_assert_fail_internal (ctf_file_t *fp, const char *file, size_t line,
278 const char *exprstr)
279 {
280 ctf_err_warn (fp, 0, "%s: %lu: libctf assertion failed: %s", file,
281 (long unsigned int) line, exprstr);
282 ctf_set_errno (fp, ECTF_INTERNAL);
283 }
This page took 0.033917 seconds and 3 git commands to generate.