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 /*
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 *
26 * Copyright 2017 Jason King.
27 */
28
29 #include <dlfcn.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <demangle.h>
34 #include <sysdemangle.h>
35
36 #include "dis_util.h"
37
38 int g_error; /* global process exit status, set when warn() is called */
39
40 /*
41 * Fatal error. Print out the error with a leading "dis: ", and then exit the
42 * program.
43 */
44 void
45 die(const char *fmt, ...)
46 {
47 va_list ap;
48
49 (void) fprintf(stderr, "dis: fatal: ");
50
51 va_start(ap, fmt);
52 (void) vfprintf(stderr, fmt, ap);
53 va_end(ap);
54
76
77 g_error = 1;
78 }
79
80 /*
81 * Convenience wrapper around malloc() to cleanly exit if any allocation fails.
82 */
83 void *
84 safe_malloc(size_t size)
85 {
86 void *ret;
87
88 if ((ret = calloc(1, size)) == NULL)
89 die("Out of memory");
90
91 return (ret);
92 }
93
94
95 /*
96 * Since -C flag explicitly says C++, for now at least, force language to
97 * C++
98 */
99 const char *
100 dis_demangle(const char *name)
101 {
102 static char *demangled_name = NULL;
103
104 /*
105 * Since demangled_name is static, it may be preserved across
106 * invocations. As such, make sure any memory that might be present
107 * from previous invocations is freed.
108 */
109 free(demangled_name);
110 demangled_name = sysdemangle(name, SYSDEM_LANG_CPP, NULL);
111 return ((demangled_name != NULL) ? demangled_name : name);
112 }
|
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 /*
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 *
26 * Copyright 2018 Jason King.
27 */
28
29 #include <dlfcn.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <demangle-sys.h>
34
35 #include "dis_util.h"
36
37 int g_error; /* global process exit status, set when warn() is called */
38
39 /*
40 * Fatal error. Print out the error with a leading "dis: ", and then exit the
41 * program.
42 */
43 void
44 die(const char *fmt, ...)
45 {
46 va_list ap;
47
48 (void) fprintf(stderr, "dis: fatal: ");
49
50 va_start(ap, fmt);
51 (void) vfprintf(stderr, fmt, ap);
52 va_end(ap);
53
75
76 g_error = 1;
77 }
78
79 /*
80 * Convenience wrapper around malloc() to cleanly exit if any allocation fails.
81 */
82 void *
83 safe_malloc(size_t size)
84 {
85 void *ret;
86
87 if ((ret = calloc(1, size)) == NULL)
88 die("Out of memory");
89
90 return (ret);
91 }
92
93
94 /*
95 * Since the -C flag explicitly says C++, for now at least, force language to
96 * C++
97 */
98 const char *
99 dis_demangle(const char *name)
100 {
101 static char *demangled_name = NULL;
102
103 /*
104 * Since demangled_name is static, it may be preserved across
105 * invocations. As such, make sure any memory that might be present
106 * from previous invocations is freed.
107 */
108 free(demangled_name);
109 demangled_name = sysdemangle(name, SYSDEM_LANG_CPP, NULL);
110 return ((demangled_name != NULL) ? demangled_name : name);
111 }
|