Branch data Line data Source code
1 : : /* Release debugging handling context.
2 : : Copyright (C) 2002-2011, 2014, 2018 Red Hat, Inc.
3 : : This file is part of elfutils.
4 : : Written by Ulrich Drepper <drepper@redhat.com>, 2002.
5 : :
6 : : This file is free software; you can redistribute it and/or modify
7 : : it under the terms of either
8 : :
9 : : * the GNU Lesser General Public License as published by the Free
10 : : Software Foundation; either version 3 of the License, or (at
11 : : your option) any later version
12 : :
13 : : or
14 : :
15 : : * the GNU General Public License as published by the Free
16 : : Software Foundation; either version 2 of the License, or (at
17 : : your option) any later version
18 : :
19 : : or both in parallel, as here.
20 : :
21 : : elfutils is distributed in the hope that it will be useful, but
22 : : WITHOUT ANY WARRANTY; without even the implied warranty of
23 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 : : General Public License for more details.
25 : :
26 : : You should have received copies of the GNU General Public License and
27 : : the GNU Lesser General Public License along with this program. If
28 : : not, see <http://www.gnu.org/licenses/>. */
29 : :
30 : : #ifdef HAVE_CONFIG_H
31 : : # include <config.h>
32 : : #endif
33 : :
34 : : #include <search.h>
35 : : #include <stdlib.h>
36 : : #include <assert.h>
37 : : #include <string.h>
38 : :
39 : : #include "libdwP.h"
40 : : #include "cfi.h"
41 : :
42 : :
43 : : static void
44 : 24372 : dwarf_package_index_free (Dwarf_Package_Index *index)
45 : : {
46 [ + + ]: 24372 : if (index != NULL)
47 : : {
48 : 68 : free (index->debug_info_offsets);
49 : 68 : free (index);
50 : : }
51 : 24372 : }
52 : :
53 : :
54 : : static void
55 : 1404588 : noop_free (void *arg __attribute__ ((unused)))
56 : : {
57 : 1404588 : }
58 : :
59 : :
60 : : static void
61 : 79184 : cu_free (void *arg)
62 : : {
63 : 79184 : struct Dwarf_CU *p = (struct Dwarf_CU *) arg;
64 : :
65 : 79184 : eu_search_tree_fini (&p->locs_tree, noop_free);
66 : 79184 : rwlock_fini (p->split_lock);
67 : 79184 : mutex_fini (p->abbrev_lock);
68 : 79184 : mutex_fini (p->src_lock);
69 : 79184 : mutex_fini (p->str_off_base_lock);
70 : 79184 : mutex_fini (p->intern_lock);
71 : :
72 : : /* Only free the CU internals if its not a fake CU. */
73 [ + + + + ]: 79184 : if (p != p->dbg->fake_loc_cu && p != p->dbg->fake_loclists_cu
74 [ + + ]: 68390 : && p != p->dbg->fake_addr_cu)
75 : : {
76 : 68248 : Dwarf_Abbrev_Hash_free (&p->abbrev_hash);
77 : :
78 : : /* Free split dwarf one way (from skeleton to split). */
79 [ + + ]: 68248 : if (p->unit_type == DW_UT_skeleton
80 [ + + + + ]: 270 : && p->split != NULL && p->split != (void *)-1)
81 : : {
82 : : /* The fake_addr_cu might be shared, only release one. */
83 [ + + ]: 196 : if (p->dbg->fake_addr_cu == p->split->dbg->fake_addr_cu)
84 : 136 : p->split->dbg->fake_addr_cu = NULL;
85 : : /* There is only one DWP file. We free it later. */
86 [ + + ]: 196 : if (p->split->dbg != p->dbg->dwp_dwarf)
87 : 108 : INTUSE(dwarf_end) (p->split->dbg);
88 : : }
89 : : }
90 : 79184 : }
91 : :
92 : :
93 : : int
94 : 12212 : dwarf_end (Dwarf *dwarf)
95 : : {
96 [ + + ]: 12212 : if (dwarf != NULL)
97 : : {
98 : 12186 : dwarf_package_index_free (dwarf->tu_index);
99 : 12186 : dwarf_package_index_free (dwarf->cu_index);
100 : :
101 [ + + ]: 12186 : if (dwarf->cfi != NULL)
102 : : /* Clean up the CFI cache. */
103 : 36 : __libdw_destroy_frame_cache (dwarf->cfi);
104 : :
105 : 12186 : Dwarf_Sig8_Hash_free (&dwarf->sig8_hash);
106 : :
107 : : /* The search tree for the CUs. NB: the CU data itself is
108 : : allocated separately, but the abbreviation hash tables need
109 : : to be handled. */
110 : 12186 : eu_search_tree_fini (&dwarf->cu_tree, cu_free);
111 : 12186 : eu_search_tree_fini (&dwarf->tu_tree, cu_free);
112 : :
113 : : /* Search tree for macro opcode tables. */
114 : 12186 : eu_search_tree_fini (&dwarf->macro_ops_tree, noop_free);
115 : :
116 : : /* Search tree for decoded .debug_lines units. */
117 : 12186 : eu_search_tree_fini (&dwarf->files_lines_tree, noop_free);
118 : :
119 : : /* And the split Dwarf. */
120 : 12186 : eu_search_tree_fini (&dwarf->split_tree, noop_free);
121 : :
122 : : /* Free the internally allocated memory. */
123 [ + + ]: 27301 : for (size_t i = 0; i < dwarf->mem_stacks; i++)
124 : : {
125 : 2929 : struct libdw_memblock *memp = dwarf->mem_tails[i];
126 [ + + ]: 99323 : while (memp != NULL)
127 : : {
128 : 96394 : struct libdw_memblock *prevp = memp->prev;
129 : 96394 : free (memp);
130 : 96394 : memp = prevp;
131 : : }
132 : : }
133 [ + + ]: 12186 : if (dwarf->mem_tails != NULL)
134 : 1760 : free (dwarf->mem_tails);
135 : 12186 : pthread_rwlock_destroy (&dwarf->mem_rwl);
136 : 12186 : mutex_fini (dwarf->dwarf_lock);
137 : 12186 : mutex_fini (dwarf->macro_lock);
138 : 12186 : mutex_fini (dwarf->dwp_lock);
139 : 12186 : mutex_fini (dwarf->lines_files_lock);
140 : :
141 : : /* Free the pubnames helper structure. */
142 : 12186 : free (dwarf->pubnames_sets);
143 : :
144 : : /* Free the ELF descriptor if necessary. */
145 [ + + ]: 12186 : if (dwarf->free_elf)
146 : 762 : elf_end (dwarf->elf);
147 : :
148 : : /* Free the fake location list CU. */
149 [ + + ]: 12186 : if (dwarf->fake_loc_cu != NULL)
150 : : {
151 : 332 : cu_free (dwarf->fake_loc_cu);
152 : 332 : free (dwarf->fake_loc_cu);
153 : : }
154 [ + + ]: 12186 : if (dwarf->fake_loclists_cu != NULL)
155 : : {
156 : 10462 : cu_free (dwarf->fake_loclists_cu);
157 : 10462 : free (dwarf->fake_loclists_cu);
158 : : }
159 : :
160 : : /* Free dwp_dwarf before freeing fake_addr_cu. The fake_addr_cu pointer
161 : : needs to be valid during the freeing of dwp_dwarf so that it can tell
162 : : whether or not it owns fake_addr_cu. */
163 [ + + ]: 12186 : if (dwarf->dwp_fd != -1)
164 : : {
165 : 28 : INTUSE(dwarf_end) (dwarf->dwp_dwarf);
166 : 28 : close (dwarf->dwp_fd);
167 : : }
168 : :
169 : : /* Free fake_addr_cu only if this dwarf owns it. */
170 [ + + ]: 12186 : if (dwarf->fake_addr_cu != NULL
171 [ + - ]: 142 : && dwarf->fake_addr_cu->dbg == dwarf)
172 : : {
173 : 142 : cu_free (dwarf->fake_addr_cu);
174 : 142 : free (dwarf->fake_addr_cu);
175 : : }
176 : :
177 : : /* Did we find and allocate the alt Dwarf ourselves? */
178 [ + + ]: 12186 : if (dwarf->alt_fd != -1)
179 : : {
180 : 8 : INTUSE(dwarf_end) (dwarf->alt_dwarf);
181 : 8 : close (dwarf->alt_fd);
182 : : }
183 : :
184 : : /* The cached path and dir we found the Dwarf ELF file in. */
185 : 12186 : free (dwarf->elfpath);
186 : 12186 : free (dwarf->debugdir);
187 : :
188 : : /* Free the context descriptor. */
189 : 12186 : free (dwarf);
190 : : }
191 : :
192 : 12212 : return 0;
193 : : }
194 : : INTDEF(dwarf_end)
|