Merge commit 'fdfb62c8fbf6e03ca943243b626360bede206f18'
[unleashed.git] / bin / dis / dis_util.cc
blob44d68eeceae6683f12e51558deafa82e5fe690bf
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <dlfcn.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <cxxabi.h>
35 #include "dis_util.h"
37 extern 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
41 * program.
43 void
44 die(const char *fmt, ...)
46 va_list ap;
48 (void) fprintf(stderr, "dis: fatal: ");
50 va_start(ap, fmt);
51 (void) vfprintf(stderr, fmt, ap);
52 va_end(ap);
54 (void) fprintf(stderr, "\n");
56 exit(1);
60 * Non-fatal error. Print out the error with a leading "dis: ", set the global
61 * error flag, and return.
63 void
64 warn(const char *fmt, ...)
66 va_list ap;
68 (void) fprintf(stderr, "dis: warning: ");
70 va_start(ap, fmt);
71 (void) vfprintf(stderr, fmt, ap);
72 va_end(ap);
74 (void) fprintf(stderr, "\n");
76 g_error = 1;
80 * Convenience wrapper around malloc() to cleanly exit if any allocation fails.
82 void *
83 safe_malloc(size_t size)
85 void *ret;
87 if ((ret = calloc(1, size)) == NULL)
88 die("Out of memory");
90 return (ret);
95 * Generic interface to demangle C++ names. Calls __cxa_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
98 * demangle().
100 * We dlopen() libstdc++.so rather than linking directly against it in case it
101 * is not installed on the system.
103 const char *
104 dis_demangle(const char *name)
106 static char *demangled_name;
107 static char *(*demangle_func)(
108 const char *, char *, size_t *, int *
109 ) = NULL;
110 static size_t size = BUFSIZE;
111 static int first_flag = 0;
114 * If this is the first call, allocate storage
115 * for the buffer.
117 if (first_flag == 0) {
118 void *demangle_hand;
120 demangle_hand = dlopen("libstdc++.so.6", RTLD_LAZY);
121 if (demangle_hand != NULL)
122 demangle_func = (char *(*)(const char *, char *,
123 size_t *, int *))dlsym(demangle_hand,
124 "__cxa_demangle");
126 demangled_name = (char*) safe_malloc(size);
127 first_flag = 1;
130 /* If we failed to find __cxa_demangle, return the original name. */
131 if (!demangle_func)
132 return (name);
135 * __cxa_demangle will realloc our buffer if necessary, but even if
136 * that fails we want to keep the buffer around for the next call.
138 if (!demangle_func(name, demangled_name, &size, NULL))
139 return (name);
141 return (demangled_name);