Debian Codegraph @

src/bin/dwarfdump/dd_makename.c

raw
1 /*
2 Copyright (C) 2000,2004 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright(C) David Anderson 2016-2019. All Rights reserved.
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of version 2 of the GNU General
7 Public License as published by the Free Software Foundation.
8
9 This program is distributed in the hope that it would be
10 useful, but WITHOUT ANY WARRANTY; without even the implied
11 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE.
13
14 Further, this software is distributed without any warranty
15 that it is free of the rightful claim of any third person
16 regarding infringement or the like. Any license provided
17 herein, whether implied or otherwise, applies only to this
18 software file. Patent licenses, if any, provided herein
19 do not apply to combinations of this program with other
20 software, or any other product whatsoever.
21
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write the Free
24 Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 Boston MA 02110-1301, USA.
26
27 makename.c
28 $Revision: 1.4 $
29 $Date: 2005/11/08 21:48:42 $
30
31 This used to be elaborate stuff.
32 Now it is trivial, as duplicating names is
33 unimportant in dwarfdump (in general).
34
35 And in fact, this is only called for attributes and
36 tags etc whose true name is unknown. Not for
37 any normal case.
38
39 */
40
41 #include <config.h>
42
43 #include <stdio.h> /* free() */
44 #include <stdlib.h> /* free() */
45 #include <string.h> /* strcmp() strdup() */
46
47 #include "dwarf.h"
48 #include "libdwarf.h"
49 #include "libdwarf_private.h"
50 #include "dd_defined_types.h"
51 #include "dd_checkutil.h"
52 #include "dd_glflags.h"
53 #include "dd_globals.h"
54 #include "dd_tsearchbal.h"
55 #include "dd_makename.h"
56 #include "dd_minimal.h"
57
58 static void * makename_data;
59 #define VALTYPE char *
60
61 static int
62 value_compare_func(const void *l, const void *r)
63 {
64 VALTYPE ml = (VALTYPE)l;
65 VALTYPE mr = (VALTYPE)r;
66 return strcmp(ml,mr);
67 }
68 /* Nothing to free for the 'value' example but
69 the key itself. */
70 static void
71 value_node_free(void *valp)
72 {
73 VALTYPE v = (VALTYPE)valp;
74 free(v);
75 }
76
77 void
78 makename_destructor(void)
79 {
80 /* Pass in root, not pointer to root */
81 dwarf_tdestroy(makename_data,value_node_free);
82 makename_data = 0;
83 }
84
85 /* WARNING: the tree walk functions will, if presented **tree
86 when *tree is wanted, simply find nothing. No error,
87 just bad results. So when a walk produces nothing
88 suspect a code mistake here.
89 The basic problem is void* is a terrible way to
90 pass in a pointer. But it's how tsearch was defined
91 long ago.
92
93 This function can return NULL if memory is exhausted.
94 */
95
96 char *
97 makename(const char *s)
98 {
99 char *newstr = 0;
100 VALTYPE re = 0;
101 void *retval = 0;
102 static int mnfailed = FALSE;
103
104 if (!s) {
105 return "";
106 }
107 retval = dwarf_tfind(s,&makename_data, value_compare_func);
108 if (retval) {
109 /* We found our string, it existed already. */
110 re = *(VALTYPE *)retval;
111 return re;
112 }
113 newstr = (char *)strdup(s);
114 if (!newstr) {
115 if (!mnfailed) {
116 printf("ERROR: Out of memory to record a string"
117 "dwarfdump will not work correctly."
118 " Future errors of this type are not shown.\n");
119 dd_minimal_count_global_error();
120 mnfailed = TRUE;
121 }
122 return "";
123 }
124 retval = dwarf_tsearch(newstr,&makename_data, value_compare_func);
125 if (!retval) {
126 if (!mnfailed) {
127 printf("ERROR: Out of memory to record a string"
128 " in a search table. "
129 "dwarfdump will not work correctly."
130 " Future errors of this type are not shown.\n");
131 dd_minimal_count_global_error();
132 mnfailed = TRUE;
133 }
134 /* Out of memory, */
135 free(newstr);
136 return "";
137 }
138 re = *(VALTYPE *)retval;
139 return re;
140 }