Update NEWS post GDB 9 branch creation.
[binutils-gdb.git] / libctf / ctf-subr.c
blob6bd7f10aeeac0dde395f57936742b637fcd2b3a5
1 /* Simple subrs.
2 Copyright (C) 2019 Free Software Foundation, Inc.
4 This file is part of libctf.
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.
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.
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/>. */
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>
29 int _libctf_version = CTF_VERSION; /* Library client version. */
30 int _libctf_debug = 0; /* Debugging messages enabled. */
32 /* Private, read-only mmap from a file, with fallback to copying.
34 No handling of page-offset issues at all: the caller must allow for that. */
36 _libctf_malloc_ void *
37 ctf_mmap (size_t length, size_t offset, int fd)
39 void *data;
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)
48 if (ctf_pread (fd, data, length, offset) <= 0)
50 free (data);
51 data = NULL;
54 #endif
55 return data;
58 void
59 ctf_munmap (void *buf, size_t length _libctf_unused_)
61 #ifdef HAVE_MMAP
62 (void) munmap (buf, length);
63 #else
64 free (buf);
65 #endif
68 ssize_t
69 ctf_pread (int fd, void *buf, ssize_t count, off_t offset)
71 ssize_t len;
72 size_t acc = 0;
73 char *data = (char *) buf;
75 #ifdef HAVE_PREAD
76 while (count > 0)
78 errno = 0;
79 if (((len = pread (fd, data, count, offset)) < 0) &&
80 errno != EINTR)
81 return len;
82 if (errno == EINTR)
83 continue;
85 acc += len;
86 if (len == 0) /* EOF. */
87 return acc;
89 count -= len;
90 offset += len;
91 data += len;
93 return acc;
94 #else
95 off_t orig_off;
97 if ((orig_off = lseek (fd, 0, SEEK_CUR)) < 0)
98 return -1;
99 if ((lseek (fd, offset, SEEK_SET)) < 0)
100 return -1;
102 while (count > 0)
104 errno = 0;
105 if (((len = read (fd, data, count)) < 0) &&
106 errno != EINTR)
107 return len;
108 if (errno == EINTR)
109 continue;
111 acc += len;
112 if (len == 0) /* EOF. */
113 break;
115 count -= len;
116 data += len;
118 if ((lseek (fd, orig_off, SEEK_SET)) < 0)
119 return -1; /* offset is smashed. */
120 #endif
122 return acc;
125 const char *
126 ctf_strerror (int err)
128 return (const char *) (strerror (err));
131 /* Set the CTF library client version to the specified version. If version is
132 zero, we just return the default library version number. */
134 ctf_version (int version)
136 if (version < 0)
138 errno = EINVAL;
139 return -1;
142 if (version > 0)
144 /* Dynamic version switching is not presently supported. */
145 if (version != CTF_VERSION)
147 errno = ENOTSUP;
148 return -1;
150 ctf_dprintf ("ctf_version: client using version %d\n", version);
151 _libctf_version = version;
154 return _libctf_version;
157 void
158 libctf_init_debug (void)
160 static int inited;
161 if (!inited)
163 _libctf_debug = getenv ("LIBCTF_DEBUG") != NULL;
164 inited = 1;
168 void ctf_setdebug (int debug)
170 /* Ensure that libctf_init_debug() has been called, so that we don't get our
171 debugging-on-or-off smashed by the next call. */
173 libctf_init_debug();
174 _libctf_debug = debug;
175 ctf_dprintf ("CTF debugging set to %i\n", debug);
178 int ctf_getdebug (void)
180 return _libctf_debug;
183 _libctf_printflike_ (1, 2)
184 void ctf_dprintf (const char *format, ...)
186 if (_libctf_debug)
188 va_list alist;
190 va_start (alist, format);
191 fflush (stdout);
192 (void) fputs ("libctf DEBUG: ", stderr);
193 (void) vfprintf (stderr, format, alist);
194 va_end (alist);