demangle.c revision d7e4ca82e1cf20bb2605befb1da74dd1688c706e
1/* 2 * This file is part of ltrace. 3 * Copyright (C) 2012 Petr Machata, Red Hat Inc. 4 * Copyright (C) 1998,1999,2003,2004,2008,2009 Juan Cespedes 5 * Copyright (C) 2006 Ian Wienand 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License as 9 * published by the Free Software Foundation; either version 2 of the 10 * License, or (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, but 13 * WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 20 * 02110-1301 USA 21 */ 22 23#include "config.h" 24 25#include <string.h> 26#include <stdlib.h> 27#include <stdio.h> 28 29#include "common.h" 30 31#ifdef USE_DEMANGLE 32 33/*****************************************************************************/ 34 35static struct dict *name_cache = NULL; 36 37const char * 38my_demangle(const char *function_name) { 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 (name_cache == NULL) { 46 name_cache = malloc(sizeof(*name_cache)); 47 if (name_cache != NULL) 48 DICT_INIT(name_cache, const char *, const char *, 49 dict_hash_string, dict_eq_string, NULL); 50 } 51 52 const char **found = NULL; 53 if (name_cache != NULL) 54 found = DICT_FIND(name_cache, &function_name, const char *); 55 56 if (found != NULL) 57 return *found; 58 59#ifdef HAVE_LIBIBERTY 60 const char *tmp = cplus_demangle(function_name, 61 DMGL_ANSI | DMGL_PARAMS); 62#elif defined USE_CXA_DEMANGLE 63 int status = 0; 64 const char *tmp = __cxa_demangle(function_name, NULL, NULL, &status); 65#endif 66 if (name_cache == NULL || tmp == NULL) { 67 fail: 68 if (tmp == NULL) 69 return function_name; 70 return tmp; 71 } 72 73 const char *fn_copy = strdup(function_name); 74 if (fn_copy == NULL) 75 goto fail; 76 77 if (DICT_INSERT(name_cache, &fn_copy, &tmp) < 0) { 78 free((char *)fn_copy); 79 goto fail; 80 } 81 82 return tmp; 83} 84 85#endif 86