xref: /titanic_51/usr/src/tools/ctf/cvt/util.c (revision fd845fc0cb4fbc8e85f974e2e4eaacca1cc26e81)
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, 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 		terminate("gelf_getehdr: %s\n", elf_errmsg(elf_errno()));
72 	}
73 
74 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
75 		char *name;
76 
77 		if (gelf_getshdr(scn, &shdr) == NULL ||
78 		    (name = elf_strptr(elf, ehdr.e_shstrndx,
79 		    (size_t)shdr.sh_name)) == NULL) {
80 			terminate("gelf_getehdr: %s\n",
81 			    elf_errmsg(elf_errno()));
82 		}
83 
84 		if (strcmp(name, tofind) == 0)
85 			return (elf_ndxscn(scn));
86 	}
87 
88 	return (-1);
89 }
90 
91 /*PRINTFLIKE2*/
92 static void
93 whine(char *type, char *format, va_list ap)
94 {
95 	int error = errno;
96 
97 	fprintf(stderr, "%s: %s: ", type, progname);
98 	vfprintf(stderr, format, ap);
99 
100 	if (format[strlen(format) - 1] != '\n')
101 		fprintf(stderr, ": %s\n", strerror(error));
102 }
103 
104 void
105 vaterminate(char *format, va_list ap)
106 {
107 	whine("ERROR", format, ap);
108 }
109 
110 void
111 set_terminate_cleanup(void (*cleanup)())
112 {
113 	terminate_cleanup = cleanup;
114 }
115 
116 /*PRINTFLIKE1*/
117 void
118 terminate(char *format, ...)
119 {
120 	if (format) {
121 		va_list ap;
122 
123 		va_start(ap, format);
124 		whine("ERROR", format, ap);
125 		va_end(ap);
126 	}
127 
128 	if (terminate_cleanup)
129 		terminate_cleanup();
130 
131 	exit(1);
132 }
133 
134 /*PRINTFLIKE1*/
135 void
136 warning(char *format, ...)
137 {
138 	va_list ap;
139 
140 	va_start(ap, format);
141 	whine("WARNING", format, ap);
142 	va_end(ap);
143 
144 	if (debug_level >= 3)
145 		terminate("Termination due to warning\n");
146 }
147 
148 /*PRINTFLIKE2*/
149 void
150 vadebug(int level, char *format, va_list ap)
151 {
152 	if (level > debug_level)
153 		return;
154 
155 	(void) fprintf(DEBUG_STREAM, "DEBUG: ");
156 	(void) vfprintf(DEBUG_STREAM, format, ap);
157 	fflush(DEBUG_STREAM);
158 }
159 
160 /*PRINTFLIKE2*/
161 void
162 debug(int level, char *format, ...)
163 {
164 	va_list ap;
165 
166 	if (level > debug_level)
167 		return;
168 
169 	va_start(ap, format);
170 	(void) vadebug(level, format, ap);
171 	va_end(ap);
172 }
173 
174 char *
175 mktmpname(const char *origname, const char *suffix)
176 {
177 	char *newname;
178 
179 	newname = xmalloc(strlen(origname) + strlen(suffix) + 1);
180 	(void) strcpy(newname, origname);
181 	(void) strcat(newname, suffix);
182 	return (newname);
183 }
184 
185 /*PRINTFLIKE2*/
186 void
187 elfterminate(const char *file, const char *fmt, ...)
188 {
189 	static char msgbuf[BUFSIZ];
190 	va_list ap;
191 
192 	va_start(ap, fmt);
193 	vsnprintf(msgbuf, sizeof (msgbuf), fmt, ap);
194 	va_end(ap);
195 
196 	terminate("%s: %s: %s\n", file, msgbuf, elf_errmsg(elf_errno()));
197 }
198 
199 const char *
200 tdesc_name(tdesc_t *tdp)
201 {
202 	return (tdp->t_name == NULL ? "(anon)" : tdp->t_name);
203 }
204