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]
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
37 int g_error
; /* global process exit status, set when warn() is called */
40 * Fatal error. Print out the error with a leading "dis: ", and then exit the
44 die(const char *fmt
, ...)
48 (void) fprintf(stderr
, "dis: fatal: ");
51 (void) vfprintf(stderr
, fmt
, ap
);
54 (void) fprintf(stderr
, "\n");
60 * Non-fatal error. Print out the error with a leading "dis: ", set the global
61 * error flag, and return.
64 warn(const char *fmt
, ...)
68 (void) fprintf(stderr
, "dis: warning: ");
71 (void) vfprintf(stderr
, fmt
, ap
);
74 (void) fprintf(stderr
, "\n");
80 * Convenience wrapper around malloc() to cleanly exit if any allocation fails.
83 safe_malloc(size_t size
)
87 if ((ret
= calloc(1, size
)) == NULL
)
95 * Generic interface to demangle C++ names. Calls cplus_demangle to do the
96 * necessary translation. If the translation fails, the argument is returned
97 * unchanged. The memory returned is only valid until the next call to
100 * We dlopen() libdemangle.so rather than linking directly against it in case it
101 * is not installed on the system.
104 dis_demangle(const char *name
)
106 static char *demangled_name
;
107 static int (*demangle_func
)() = NULL
;
108 static int size
= BUFSIZE
;
109 static int first_flag
= 0;
113 * If this is the first call, allocate storage
116 if (first_flag
== 0) {
119 demangle_hand
= dlopen("libdemangle.so.1", RTLD_LAZY
);
120 if (demangle_hand
!= NULL
)
121 demangle_func
= (int (*)(int))dlsym(
122 demangle_hand
, "cplus_demangle");
124 demangled_name
= safe_malloc(size
);
129 * If libdemangle is not present, pass through unchanged.
131 if (demangle_func
== NULL
)
135 * The function returns -1 when the buffer size is not sufficient.
137 while ((ret
= (*demangle_func
)(name
, demangled_name
, size
)) == -1) {
138 free(demangled_name
);
139 size
= size
+ BUFSIZE
;
140 demangled_name
= safe_malloc(size
);
146 return (demangled_name
);