4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
41 #include <sys/param.h>
46 static void (*terminate_cleanup)(void) = NULL;
48 /* returns 1 if s1 == s2, 0 otherwise */
50 streq(const char *s1, const char *s2)
55 } else if (s2 == NULL)
57 else if (strcmp(s1, s2) != 0)
64 findelfsecidx(Elf *elf, const char *file, const char *tofind)
70 if (gelf_getehdr(elf, &ehdr) == NULL)
71 elfterminate(file, "Couldn't read ehdr");
73 while ((scn = elf_nextscn(elf, scn)) != NULL) {
76 if (gelf_getshdr(scn, &shdr) == NULL) {
78 "Couldn't read header for section %d",
82 if ((name = elf_strptr(elf, ehdr.e_shstrndx,
83 (size_t)shdr.sh_name)) == NULL) {
85 "Couldn't get name for section %d",
89 if (strcmp(name, tofind) == 0)
90 return (elf_ndxscn(scn));
101 if (gelf_getehdr(elf, &ehdr) == NULL) {
102 terminate("failed to read ELF header: %s\n",
106 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
108 else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64)
111 terminate("unknown ELF class %d\n", ehdr.e_ident[EI_CLASS]);
119 whine(const char *type, const char *format, va_list ap)
123 fprintf(stderr, "%s: %s: ", type, progname);
124 vfprintf(stderr, format, ap);
126 if (format[strlen(format) - 1] != '\n')
127 fprintf(stderr, ": %s\n", strerror(error));
131 set_terminate_cleanup(void (*cleanup)(void))
133 terminate_cleanup = cleanup;
138 terminate(const char *format, ...)
142 va_start(ap, format);
143 whine("ERROR", format, ap);
146 if (terminate_cleanup)
149 if (getenv("CTF_ABORT_ON_TERMINATE") != NULL)
151 #if defined(__FreeBSD__)
153 * For the time being just output the termination message, but don't
154 * return an exit status that would cause the build to fail. We need
155 * to get as much stuff built as possible before going back and
156 * figuring out what is wrong with certain files.
166 aborterr(const char *format, ...)
170 va_start(ap, format);
171 whine("ERROR", format, ap);
183 warning(const char *format, ...)
187 va_start(ap, format);
188 whine("WARNING", format, ap);
191 if (debug_level >= 3)
192 terminate("Termination due to warning\n");
197 vadebug(int level, const char *format, va_list ap)
199 if (level > debug_level)
202 (void) fprintf(DEBUG_STREAM, "DEBUG: ");
203 (void) vfprintf(DEBUG_STREAM, format, ap);
204 fflush(DEBUG_STREAM);
209 debug(int level, const char *format, ...)
213 if (level > debug_level)
216 va_start(ap, format);
217 (void) vadebug(level, format, ap);
222 mktmpname(const char *origname, const char *suffix)
226 newname = xmalloc(strlen(origname) + strlen(suffix) + 1);
227 (void) strcpy(newname, origname);
228 (void) strcat(newname, suffix);
234 elfterminate(const char *file, const char *fmt, ...)
236 static char msgbuf[BUFSIZ];
240 vsnprintf(msgbuf, sizeof (msgbuf), fmt, ap);
243 terminate("%s: %s: %s\n", file, msgbuf, elf_errmsg(-1));
247 tdesc_name(tdesc_t *tdp)
249 return (tdp->t_name == NULL ? "(anon)" : tdp->t_name);
252 static char *watch_address = NULL;
253 static int watch_length = 0;
256 watch_set(void *addr, int len)
258 watch_address = addr;
265 char *p = watch_address;
268 if (watch_address == NULL || watch_length == 0)
271 printf("%d: watch %p len %d\n",v,watch_address,watch_length);
272 for (i = 0; i < watch_length; i++) {
273 if (*p >= 0x20 && *p < 0x7f) {
274 printf(" %c",*p++ & 0xff);
276 printf(" %02x",*p++ & 0xff);