• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <getopt.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 
23 #include <cctype>
24 #include <string>
25 
26 #include <demangle.h>
27 
28 extern "C" char* __cxa_demangle(const char*, char*, size_t*, int*);
29 
Usage(const char * prog_name)30 static void Usage(const char* prog_name) {
31   printf("usage: %s [-c] [NAME_TO_DEMANGLE...]\n", prog_name);
32   printf("\n");
33   printf("Demangles C++ mangled names if supplied on the command-line, or found\n");
34   printf("reading from stdin otherwise.\n");
35   printf("\n");
36   printf("-c\tCompare against __cxa_demangle\n");
37   printf("\n");
38 }
39 
DemangleWithCxa(const char * name)40 static std::string DemangleWithCxa(const char* name) {
41   const char* cxa_demangle = __cxa_demangle(name, nullptr, nullptr, nullptr);
42   if (cxa_demangle == nullptr) {
43     return name;
44   }
45 
46   // The format of our demangler is slightly different from the cxa demangler
47   // so modify the cxa demangler output. Specifically, for templates, remove
48   // the spaces between '>' and '>'.
49   std::string demangled_str;
50   for (size_t i = 0; i < strlen(cxa_demangle); i++) {
51     if (i > 2 && cxa_demangle[i] == '>' && std::isspace(cxa_demangle[i - 1]) &&
52         cxa_demangle[i - 2] == '>') {
53       demangled_str.resize(demangled_str.size() - 1);
54     }
55     demangled_str += cxa_demangle[i];
56   }
57   return demangled_str;
58 }
59 
Compare(const char * name,const std::string & demangled_name)60 static void Compare(const char* name, const std::string& demangled_name) {
61   std::string cxa_demangled_name(DemangleWithCxa(name));
62   if (cxa_demangled_name != demangled_name) {
63     printf("\nMismatch!\n");
64     printf("\tmangled name: %s\n", name);
65     printf("\tour demangle: %s\n", demangled_name.c_str());
66     printf("\tcxa demangle: %s\n", cxa_demangled_name.c_str());
67     exit(1);
68   }
69 }
70 
Filter(bool compare)71 static int Filter(bool compare) {
72   char* line = nullptr;
73   size_t line_length = 0;
74 
75   while ((getline(&line, &line_length, stdin)) != -1) {
76     char* p = line;
77     char* name;
78     while ((name = strstr(p, "_Z")) != nullptr) {
79       // Output anything before the identifier.
80       *name = 0;
81       printf("%s", p);
82       *name = '_';
83 
84       // Extract the identifier.
85       p = name;
86       while (*p && (std::isalnum(*p) || *p == '_' || *p == '.' || *p == '$')) ++p;
87 
88       // Demangle and output.
89       std::string identifier(name, p);
90       std::string demangled_name = demangle(identifier.c_str());
91       printf("%s", demangled_name.c_str());
92 
93       if (compare) Compare(identifier.c_str(), demangled_name);
94     }
95     // Output anything after the last identifier.
96     printf("%s", p);
97   }
98 
99   free(line);
100   return 0;
101 }
102 
main(int argc,char ** argv)103 int main(int argc, char** argv) {
104 #ifdef __BIONIC__
105   const char* prog_name = getprogname();
106 #else
107   const char* prog_name = argv[0];
108 #endif
109 
110   bool compare = false;
111   int opt_char;
112   while ((opt_char = getopt(argc, argv, "c")) != -1) {
113     if (opt_char == 'c') {
114       compare = true;
115     } else {
116       Usage(prog_name);
117       return 1;
118     }
119   }
120 
121   // With no arguments, act as a filter.
122   if (optind == argc) return Filter(compare);
123 
124   // Otherwise demangle each argument.
125   while (optind < argc) {
126     const char* name = argv[optind++];
127     std::string demangled_name = demangle(name);
128     printf("%s\n", demangled_name.c_str());
129 
130     if (compare) Compare(name, demangled_name);
131   }
132   return 0;
133 }
134