Imported Upstream version 0.7.2
[platform/upstream/ltrace.git] / demangle.c
1 /*
2  * This file is part of ltrace.
3  * Copyright (C) 1998,1999,2003,2004,2008,2009 Juan Cespedes
4  * Copyright (C) 2006 Ian Wienand
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19  * 02110-1301 USA
20  */
21
22 #include "config.h"
23
24 #include <string.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27
28 #include "common.h"
29
30 #ifdef USE_DEMANGLE
31
32 /*****************************************************************************/
33
34 static Dict *d = NULL;
35
36 const char *
37 my_demangle(const char *function_name) {
38         const char *tmp, *fn_copy;
39 #ifdef USE_CXA_DEMANGLE
40         extern char *__cxa_demangle(const char *, char *, size_t *, int *);
41 #endif
42
43         debug(DEBUG_FUNCTION, "my_demangle(name=%s)", function_name);
44
45         if (!d)
46                 d = dict_init(dict_key2hash_string, dict_key_cmp_string);
47
48         tmp = dict_find_entry(d, (void *)function_name);
49         if (!tmp) {
50                 fn_copy = strdup(function_name);
51 #ifdef HAVE_LIBIBERTY
52                 tmp = cplus_demangle(function_name, DMGL_ANSI | DMGL_PARAMS);
53 #elif defined USE_CXA_DEMANGLE
54                 int status = 0;
55                 tmp = __cxa_demangle(function_name, NULL, NULL, &status);
56 #endif
57                 if (!tmp)
58                         tmp = fn_copy;
59                 if (tmp)
60                         dict_enter(d, (void *)fn_copy, (void *)tmp);
61         }
62         return tmp;
63 }
64
65 #endif