1 /* Maintenance of module list in libdwfl.
2 Copyright (C) 2005 Red Hat, Inc.
4 This program is Open Source software; you can redistribute it and/or
5 modify it under the terms of the Open Software License version 1.0 as
6 published by the Open Source Initiative.
8 You should have received a copy of the Open Software License along
9 with this program; if not, you may obtain a copy of the Open Software
10 License version 1.0 from http://www.opensource.org/licenses/osl.php or
11 by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
12 3001 King Ranch Road, Ukiah, CA 95482. */
18 free_cu (struct dwfl_cu *cu)
20 if (cu->lines != NULL)
26 nofree (void *arg __attribute__ ((unused)))
32 __libdwfl_module_free (Dwfl_Module *mod)
34 if (mod->lazy_cu_root != NULL)
35 tdestroy (mod->lazy_cu_root, nofree);
37 if (mod->aranges != NULL)
42 for (size_t i = 0; i < mod->ncu; ++i)
51 ebl_closebackend (mod->ebl);
53 if (mod->debug.elf != mod->main.elf && mod->debug.elf != NULL)
54 elf_end (mod->debug.elf);
55 if (mod->main.elf != NULL)
56 elf_end (mod->main.elf);
62 dwfl_report_begin (Dwfl *dwfl)
64 for (Dwfl_Module *m = dwfl->modulelist; m != NULL; m = m->next)
67 if (dwfl->modules != NULL)
72 INTDEF (dwfl_report_begin)
74 /* Report that a module called NAME pans addresses [START, END).
75 Returns the module handle, either existing or newly allocated,
76 or returns a null pointer for an allocation error. */
78 dwfl_report_module (Dwfl *dwfl, const char *name,
79 GElf_Addr start, GElf_Addr end)
81 Dwfl_Module **tailp = &dwfl->modulelist, **prevp = tailp;
82 for (Dwfl_Module *m = *prevp; m != NULL; m = *(prevp = &m->next))
84 if (m->low_addr == start && m->high_addr == end
85 && !strcmp (m->name, name))
87 /* This module is still here. Move it to the place in the list
88 after the last module already reported. */
101 Dwfl_Module *mod = calloc (1, sizeof *mod);
105 mod->name = strdup (name);
106 if (mod->name == NULL)
110 __libdwfl_seterrno (DWFL_E_NOMEM);
114 mod->low_addr = start;
115 mod->high_addr = end;
124 INTDEF (dwfl_report_module)
127 compare_modules (const void *a, const void *b)
129 Dwfl_Module *const *p1 = a, *const *p2 = b;
130 const Dwfl_Module *m1 = *p1, *m2 = *p2;
135 return (GElf_Sxword) (m1->low_addr - m2->low_addr);
139 /* Finish reporting the current set of modules to the library.
140 If REMOVED is not null, it's called for each module that
141 existed before but was not included in the current report.
142 Returns a nonzero return value from the callback.
143 DWFL cannot be used until this function has returned zero. */
144 int dwfl_report_end (Dwfl *dwfl,
145 int (*removed) (Dwfl_Module *, void *,
146 const char *, Dwarf_Addr,
150 assert (dwfl->modules == NULL);
152 Dwfl_Module **tailp = &dwfl->modulelist;
153 while (*tailp != NULL)
155 Dwfl_Module *m = *tailp;
156 if (m->gc && removed != NULL)
158 int result = (*removed) (MODCB_ARGS (m), arg);
165 __libdwfl_module_free (m);
171 dwfl->modules = malloc (dwfl->nmodules * sizeof dwfl->modules[0]);
172 if (dwfl->modules == NULL && dwfl->nmodules != 0)
176 for (Dwfl_Module *m = dwfl->modulelist; m != NULL; m = m->next)
179 dwfl->modules[i++] = m;
181 assert (i == dwfl->nmodules);
183 qsort (dwfl->modules, dwfl->nmodules, sizeof dwfl->modules[0],
188 INTDEF (dwfl_report_end)