1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2018 Jason King
* Copyright 2019, Joyent, Inc.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <sys/ctype.h>
#include <sys/debug.h>
#include <stdarg.h>
#include "demangle-sys.h"
#include "demangle_int.h"
#define DEMANGLE_DEBUG "DEMANGLE_DEBUG"
static pthread_once_t debug_once = PTHREAD_ONCE_INIT;
volatile boolean_t demangle_debug;
FILE *debugf = stderr;
static const char *
langstr(sysdem_lang_t lang)
{
switch (lang) {
case SYSDEM_LANG_AUTO:
return ("auto");
case SYSDEM_LANG_CPP:
return ("c++");
case SYSDEM_LANG_RUST:
return ("rust");
default:
return ("invalid");
}
}
static sysdem_lang_t
detect_lang(const char *str, size_t n)
{
const char *p = str;
size_t len;
if (n < 3 || str[0] != '_')
return (SYSDEM_LANG_AUTO);
/*
* Check for ^_Z or ^__Z
*/
p = str + 1;
if (*p == '_') {
p++;
}
if (*p != 'Z')
return (SYSDEM_LANG_AUTO);
/*
* Sadly, rust currently uses the same prefix as C++, however
* demangling rust as a C++ mangled name yields less than desirable
* results. However rust names end with a hash. We use that to
* attempt to disambiguate
*/
/* Find 'h'<hexdigit>+E$ */
if ((p = strrchr(p, 'h')) == NULL)
return (SYSDEM_LANG_CPP);
if ((len = strspn(p + 1, "0123456789abcdef")) == 0)
return (SYSDEM_LANG_CPP);
p += len + 1;
if (p[0] != 'E' || p[1] != '\0')
return (SYSDEM_LANG_CPP);
return (SYSDEM_LANG_RUST);
}
static void
check_debug(void)
{
if (getenv(DEMANGLE_DEBUG))
demangle_debug = B_TRUE;
}
char *
sysdemangle(const char *str, sysdem_lang_t lang, sysdem_ops_t *ops)
{
/*
* While the language specific demangler code can handle non-NUL
* terminated strings, we currently don't expose this to consumers.
* Consumers should still pass in a NUL-terminated string.
*/
size_t slen;
VERIFY0(pthread_once(&debug_once, check_debug));
DEMDEBUG("name = '%s'", (str == NULL) ? "(NULL)" : str);
DEMDEBUG("lang = %s (%d)", langstr(lang), lang);
if (str == NULL) {
errno = EINVAL;
return (NULL);
}
slen = strlen(str);
switch (lang) {
case SYSDEM_LANG_AUTO:
case SYSDEM_LANG_CPP:
case SYSDEM_LANG_RUST:
break;
default:
errno = EINVAL;
return (NULL);
}
if (ops == NULL)
ops = sysdem_ops_default;
if (lang == SYSDEM_LANG_AUTO) {
lang = detect_lang(str, slen);
if (lang != SYSDEM_LANG_AUTO)
DEMDEBUG("detected language is %s", langstr(lang));
}
switch (lang) {
case SYSDEM_LANG_CPP:
return (cpp_demangle(str, slen, ops));
case SYSDEM_LANG_RUST:
return (rust_demangle(str, slen, ops));
case SYSDEM_LANG_AUTO:
DEMDEBUG("could not detect language");
errno = ENOTSUP;
return (NULL);
default:
/*
* This can't happen unless there's a bug with detect_lang,
* but gcc doesn't know that.
*/
errno = EINVAL;
return (NULL);
}
}
int
demdebug(const char *fmt, ...)
{
va_list ap;
flockfile(debugf);
(void) fprintf(debugf, "LIBDEMANGLE: ");
va_start(ap, fmt);
(void) vfprintf(debugf, fmt, ap);
(void) fputc('\n', debugf);
(void) fflush(debugf);
va_end(ap);
funlockfile(debugf);
return (0);
}
|