1179187Sjb/*-
2179187Sjb * Copyright (c) 2007 John Birrell (jb@freebsd.org)
3179187Sjb * All rights reserved.
4179187Sjb *
5179187Sjb * Redistribution and use in source and binary forms, with or without
6179187Sjb * modification, are permitted provided that the following conditions
7179187Sjb * are met:
8179187Sjb * 1. Redistributions of source code must retain the above copyright
9179187Sjb *    notice, this list of conditions and the following disclaimer.
10179187Sjb * 2. Redistributions in binary form must reproduce the above copyright
11179187Sjb *    notice, this list of conditions and the following disclaimer in the
12179187Sjb *    documentation and/or other materials provided with the distribution.
13179187Sjb *
14179187Sjb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15179187Sjb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16179187Sjb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17179187Sjb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18179187Sjb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19179187Sjb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20179187Sjb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21179187Sjb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22179187Sjb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23179187Sjb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24179187Sjb * SUCH DAMAGE.
25179187Sjb *
26179187Sjb * $FreeBSD$
27179187Sjb */
28179187Sjb
29179187Sjb#include <stdlib.h>
30179187Sjb#include "_libdwarf.h"
31179187Sjb
32179187Sjbint
33179187Sjbdwarf_finish(Dwarf_Debug *dbgp, Dwarf_Error *error)
34179187Sjb{
35179187Sjb	Dwarf_Abbrev ab;
36179187Sjb	Dwarf_Abbrev tab;
37179187Sjb	Dwarf_Attribute at;
38179187Sjb	Dwarf_Attribute tat;
39179187Sjb	Dwarf_AttrValue av;
40179187Sjb	Dwarf_AttrValue tav;
41179187Sjb	Dwarf_CU cu;
42179187Sjb	Dwarf_CU tcu;
43179187Sjb	Dwarf_Debug dbg;
44179187Sjb	Dwarf_Die die;
45179187Sjb	Dwarf_Die tdie;
46179187Sjb
47179187Sjb	if (error == NULL)
48179187Sjb		/* Can only return a generic error. */
49179187Sjb		return DWARF_E_ERROR;
50179187Sjb
51179187Sjb	if (dbgp == NULL) {
52179187Sjb		DWARF_SET_ERROR(error, DWARF_E_ARGUMENT);
53179187Sjb		return DWARF_E_ERROR;
54179187Sjb	}
55179187Sjb
56179187Sjb	if ((dbg = *dbgp) == NULL)
57179187Sjb		return DWARF_E_NONE;
58179187Sjb
59179187Sjb	/* Free entries in the compilation unit list. */
60179187Sjb	STAILQ_FOREACH_SAFE(cu, &dbg->dbg_cu, cu_next, tcu) {
61179187Sjb		/* Free entries in the die list */
62179187Sjb		STAILQ_FOREACH_SAFE(die, &cu->cu_die, die_next, tdie) {
63179187Sjb			/* Free entries in the attribute value list */
64179187Sjb			STAILQ_FOREACH_SAFE(av, &die->die_attrval, av_next, tav) {
65179187Sjb				STAILQ_REMOVE(&die->die_attrval, av, _Dwarf_AttrValue, av_next);
66179187Sjb				free(av);
67179187Sjb			}
68179187Sjb
69179187Sjb			STAILQ_REMOVE(&cu->cu_die, die, _Dwarf_Die, die_next);
70179187Sjb			free(die);
71179187Sjb		}
72179187Sjb
73179187Sjb		/* Free entries in the abbrev list */
74179187Sjb		STAILQ_FOREACH_SAFE(ab, &cu->cu_abbrev, a_next, tab) {
75179187Sjb			/* Free entries in the attribute list */
76179187Sjb			STAILQ_FOREACH_SAFE(at, &ab->a_attrib, at_next, tat) {
77179187Sjb				STAILQ_REMOVE(&ab->a_attrib, at, _Dwarf_Attribute, at_next);
78179187Sjb				free(at);
79179187Sjb			}
80179187Sjb
81179187Sjb			STAILQ_REMOVE(&cu->cu_abbrev, ab, _Dwarf_Abbrev, a_next);
82179187Sjb			free(ab);
83179187Sjb		}
84179187Sjb
85179187Sjb		STAILQ_REMOVE(&dbg->dbg_cu, cu, _Dwarf_CU, cu_next);
86179187Sjb		free(cu);
87179187Sjb	}
88179187Sjb
89179187Sjb	if (dbg->dbg_elf_close)
90179187Sjb		/* Free resources associated with the ELF file. */
91179187Sjb		elf_end(dbg->dbg_elf);
92179187Sjb
93179187Sjb	free(dbg);
94179187Sjb
95179187Sjb	*dbgp = NULL;
96179187Sjb
97179187Sjb	return DWARF_E_NONE;
98179187Sjb}
99