xref: /titanic_51/usr/src/tools/ctf/cvt/util.c (revision 4088bb40326b75ef60834a6c2a92e29e25474b68)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Utility functions
30  */
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <libelf.h>
36 #include <gelf.h>
37 #include <errno.h>
38 #include <stdarg.h>
39 #include <pthread.h>
40 #include <unistd.h>
41 #include <sys/param.h>
42 
43 #include "ctftools.h"
44 #include "memory.h"
45 
46 static void (*terminate_cleanup)() = NULL;
47 
48 /* returns 1 if s1 == s2, 0 otherwise */
49 int
50 streq(char *s1, char *s2)
51 {
52 	if (s1 == NULL) {
53 		if (s2 != NULL)
54 			return (0);
55 	} else if (s2 == NULL)
56 		return (0);
57 	else if (strcmp(s1, s2) != 0)
58 		return (0);
59 
60 	return (1);
61 }
62 
63 int
64 findelfsecidx(Elf *elf, const char *file, const char *tofind)
65 {
66 	Elf_Scn *scn = NULL;
67 	GElf_Ehdr ehdr;
68 	GElf_Shdr shdr;
69 
70 	if (gelf_getehdr(elf, &ehdr) == NULL)
71 		elfterminate(file, "Couldn't read ehdr");
72 
73 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
74 		char *name;
75 
76 		if (gelf_getshdr(scn, &shdr) == NULL) {
77 			elfterminate(file,
78 			    "Couldn't read header for section %d",
79 			    elf_ndxscn(scn));
80 		}
81 
82 		if ((name = elf_strptr(elf, ehdr.e_shstrndx,
83 		    (size_t)shdr.sh_name)) == NULL) {
84 			elfterminate(file,
85 			    "Couldn't get name for section %d",
86 			    elf_ndxscn(scn));
87 		}
88 
89 		if (strcmp(name, tofind) == 0)
90 			return (elf_ndxscn(scn));
91 	}
92 
93 	return (-1);
94 }
95 
96 /*PRINTFLIKE2*/
97 static void
98 whine(char *type, char *format, va_list ap)
99 {
100 	int error = errno;
101 
102 	fprintf(stderr, "%s: %s: ", type, progname);
103 	vfprintf(stderr, format, ap);
104 
105 	if (format[strlen(format) - 1] != '\n')
106 		fprintf(stderr, ": %s\n", strerror(error));
107 }
108 
109 void
110 set_terminate_cleanup(void (*cleanup)())
111 {
112 	terminate_cleanup = cleanup;
113 }
114 
115 /*PRINTFLIKE1*/
116 void
117 terminate(char *format, ...)
118 {
119 	va_list ap;
120 
121 	va_start(ap, format);
122 	whine("ERROR", format, ap);
123 	va_end(ap);
124 
125 	if (terminate_cleanup)
126 		terminate_cleanup();
127 
128 	if (getenv("CTF_ABORT_ON_TERMINATE") != NULL)
129 		abort();
130 	exit(1);
131 }
132 
133 /*PRINTFLIKE1*/
134 void
135 aborterr(char *format, ...)
136 {
137 	va_list ap;
138 
139 	va_start(ap, format);
140 	whine("ERROR", format, ap);
141 	va_end(ap);
142 
143 	abort();
144 }
145 
146 /*PRINTFLIKE1*/
147 void
148 warning(char *format, ...)
149 {
150 	va_list ap;
151 
152 	va_start(ap, format);
153 	whine("WARNING", format, ap);
154 	va_end(ap);
155 
156 	if (debug_level >= 3)
157 		terminate("Termination due to warning\n");
158 }
159 
160 /*PRINTFLIKE2*/
161 void
162 vadebug(int level, char *format, va_list ap)
163 {
164 	if (level > debug_level)
165 		return;
166 
167 	(void) fprintf(DEBUG_STREAM, "DEBUG: ");
168 	(void) vfprintf(DEBUG_STREAM, format, ap);
169 	fflush(DEBUG_STREAM);
170 }
171 
172 /*PRINTFLIKE2*/
173 void
174 debug(int level, char *format, ...)
175 {
176 	va_list ap;
177 
178 	if (level > debug_level)
179 		return;
180 
181 	va_start(ap, format);
182 	(void) vadebug(level, format, ap);
183 	va_end(ap);
184 }
185 
186 char *
187 mktmpname(const char *origname, const char *suffix)
188 {
189 	char *newname;
190 
191 	newname = xmalloc(strlen(origname) + strlen(suffix) + 1);
192 	(void) strcpy(newname, origname);
193 	(void) strcat(newname, suffix);
194 	return (newname);
195 }
196 
197 /*PRINTFLIKE2*/
198 void
199 elfterminate(const char *file, const char *fmt, ...)
200 {
201 	static char msgbuf[BUFSIZ];
202 	va_list ap;
203 
204 	va_start(ap, fmt);
205 	vsnprintf(msgbuf, sizeof (msgbuf), fmt, ap);
206 	va_end(ap);
207 
208 	terminate("%s: %s: %s\n", file, msgbuf, elf_errmsg(-1));
209 }
210 
211 const char *
212 tdesc_name(tdesc_t *tdp)
213 {
214 	return (tdp->t_name == NULL ? "(anon)" : tdp->t_name);
215 }
216