Petr Machata | e99af27 | 2012-10-26 00:29:52 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of ltrace. |
Petr Machata | 98ff309 | 2013-03-08 22:11:36 +0100 | [diff] [blame] | 3 | * Copyright (C) 2012,2013 Petr Machata, Red Hat Inc. |
Petr Machata | e99af27 | 2012-10-26 00:29:52 +0200 | [diff] [blame] | 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 | |
Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 23 | #include "config.h" |
Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 24 | |
Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 25 | #include <string.h> |
Juan Cespedes | 1b9cfd6 | 1999-08-30 19:34:50 +0200 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <stdio.h> |
Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 28 | |
Petr Machata | 3ac8db6 | 2012-11-23 18:43:10 +0100 | [diff] [blame] | 29 | #include "demangle.h" |
| 30 | #include "dict.h" |
| 31 | #include "debug.h" |
Juan Cespedes | 1b9cfd6 | 1999-08-30 19:34:50 +0200 | [diff] [blame] | 32 | |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 33 | #ifdef USE_DEMANGLE |
| 34 | |
Juan Cespedes | 1b9cfd6 | 1999-08-30 19:34:50 +0200 | [diff] [blame] | 35 | /*****************************************************************************/ |
| 36 | |
Petr Machata | d7e4ca8 | 2012-11-28 03:38:47 +0100 | [diff] [blame] | 37 | static struct dict *name_cache = NULL; |
Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 38 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 39 | const char * |
| 40 | my_demangle(const char *function_name) { |
Petr Machata | cdd17b8 | 2012-06-01 19:35:24 +0200 | [diff] [blame] | 41 | #ifdef USE_CXA_DEMANGLE |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 42 | extern char *__cxa_demangle(const char *, char *, size_t *, int *); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 43 | #endif |
Juan Cespedes | 1b9cfd6 | 1999-08-30 19:34:50 +0200 | [diff] [blame] | 44 | |
Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 45 | debug(DEBUG_FUNCTION, "my_demangle(name=%s)", function_name); |
| 46 | |
Petr Machata | d7e4ca8 | 2012-11-28 03:38:47 +0100 | [diff] [blame] | 47 | if (name_cache == NULL) { |
| 48 | name_cache = malloc(sizeof(*name_cache)); |
| 49 | if (name_cache != NULL) |
Peter Wu | 9748550 | 2013-09-26 00:55:56 +0200 | [diff] [blame] | 50 | DICT_INIT(name_cache, char *, const char *, |
Petr Machata | d7e4ca8 | 2012-11-28 03:38:47 +0100 | [diff] [blame] | 51 | dict_hash_string, dict_eq_string, NULL); |
Juan Cespedes | 1b9cfd6 | 1999-08-30 19:34:50 +0200 | [diff] [blame] | 52 | } |
Petr Machata | d7e4ca8 | 2012-11-28 03:38:47 +0100 | [diff] [blame] | 53 | |
Petr Machata | 98ff309 | 2013-03-08 22:11:36 +0100 | [diff] [blame] | 54 | const char *tmp = NULL; |
| 55 | if (name_cache != NULL |
| 56 | && DICT_FIND_VAL(name_cache, &function_name, &tmp) == 0) |
| 57 | return tmp; |
Petr Machata | d7e4ca8 | 2012-11-28 03:38:47 +0100 | [diff] [blame] | 58 | |
| 59 | #ifdef HAVE_LIBIBERTY |
Petr Machata | 98ff309 | 2013-03-08 22:11:36 +0100 | [diff] [blame] | 60 | tmp = cplus_demangle(function_name, |
Petr Machata | d7e4ca8 | 2012-11-28 03:38:47 +0100 | [diff] [blame] | 61 | DMGL_ANSI | DMGL_PARAMS); |
| 62 | #elif defined USE_CXA_DEMANGLE |
| 63 | int status = 0; |
Petr Machata | 98ff309 | 2013-03-08 22:11:36 +0100 | [diff] [blame] | 64 | tmp = __cxa_demangle(function_name, NULL, NULL, &status); |
Petr Machata | d7e4ca8 | 2012-11-28 03:38:47 +0100 | [diff] [blame] | 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 | |
Juan Cespedes | 1b9cfd6 | 1999-08-30 19:34:50 +0200 | [diff] [blame] | 82 | return tmp; |
Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | #endif |