2009-03-14 17:50:36 +01:00
|
|
|
/*
|
2006-11-10 22:19:58 +01:00
|
|
|
Copyright (C) 2006 Mandriva Conectiva S.A.
|
|
|
|
Copyright (C) 2006 Arnaldo Carvalho de Melo <acme@mandriva.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it
|
|
|
|
under the terms of version 2 of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <dwarf.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2007-01-11 19:07:05 +01:00
|
|
|
#include "dwarves.h"
|
2007-12-16 17:47:59 +01:00
|
|
|
#include "dutil.h"
|
2006-11-10 22:19:58 +01:00
|
|
|
|
2007-01-04 04:41:11 +01:00
|
|
|
static void refcnt_tag(struct tag *tag, const struct cu *cu);
|
2006-11-10 22:19:58 +01:00
|
|
|
|
2007-01-04 04:41:11 +01:00
|
|
|
static void refcnt_member(struct class_member *member, const struct cu *cu)
|
2006-11-10 22:19:58 +01:00
|
|
|
{
|
|
|
|
if (member->visited)
|
|
|
|
return;
|
|
|
|
member->visited = 1;
|
2006-11-18 14:54:02 +01:00
|
|
|
if (member->tag.type != 0) { /* if not void */
|
2009-03-18 16:17:07 +01:00
|
|
|
struct tag *type = cu__type(cu, member->tag.type);
|
2006-11-10 22:19:58 +01:00
|
|
|
if (type != NULL)
|
2007-01-04 04:41:11 +01:00
|
|
|
refcnt_tag(type, cu);
|
2006-11-10 22:19:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-30 19:34:20 +01:00
|
|
|
static void refcnt_parameter(const struct parameter *parameter,
|
|
|
|
const struct cu *cu)
|
2006-11-10 22:19:58 +01:00
|
|
|
{
|
2006-11-18 17:33:48 +01:00
|
|
|
if (parameter->tag.type != 0) { /* if not void */
|
2009-03-18 16:17:07 +01:00
|
|
|
struct tag *type = cu__type(cu, parameter->tag.type);
|
2006-11-10 22:19:58 +01:00
|
|
|
if (type != NULL)
|
2007-01-04 04:41:11 +01:00
|
|
|
refcnt_tag(type, cu);
|
2006-11-10 22:19:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-03 01:26:01 +01:00
|
|
|
static void refcnt_variable(const struct variable *variable,
|
|
|
|
const struct cu *cu)
|
2006-11-10 22:19:58 +01:00
|
|
|
{
|
2009-06-04 22:30:06 +02:00
|
|
|
if (variable->ip.tag.type != 0) { /* if not void */
|
|
|
|
struct tag *type = cu__type(cu, variable->ip.tag.type);
|
2006-11-10 22:19:58 +01:00
|
|
|
if (type != NULL)
|
2007-01-04 04:41:11 +01:00
|
|
|
refcnt_tag(type, cu);
|
2006-11-10 22:19:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-03 01:26:01 +01:00
|
|
|
static void refcnt_inline_expansion(const struct inline_expansion *exp,
|
|
|
|
const struct cu *cu)
|
2006-11-10 22:19:58 +01:00
|
|
|
{
|
2009-06-04 22:30:06 +02:00
|
|
|
if (exp->ip.tag.type != 0) { /* if not void */
|
|
|
|
struct tag *type = cu__function(cu, exp->ip.tag.type);
|
2006-11-18 17:33:48 +01:00
|
|
|
if (type != NULL)
|
2007-01-04 04:41:11 +01:00
|
|
|
refcnt_tag(type, cu);
|
2006-11-10 22:19:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-04 04:41:11 +01:00
|
|
|
static void refcnt_tag(struct tag *tag, const struct cu *cu)
|
2006-11-10 22:19:58 +01:00
|
|
|
{
|
|
|
|
struct class_member *member;
|
2006-11-18 17:33:48 +01:00
|
|
|
|
2009-03-03 14:56:37 +01:00
|
|
|
tag->visited = 1;
|
2006-11-18 17:33:48 +01:00
|
|
|
|
2007-05-31 07:21:36 +02:00
|
|
|
if (tag__is_struct(tag) || tag__is_union(tag))
|
2007-05-24 02:43:01 +02:00
|
|
|
type__for_each_member(tag__type(tag), member)
|
2007-01-04 04:41:11 +01:00
|
|
|
refcnt_member(member, cu);
|
2006-11-18 17:33:48 +01:00
|
|
|
}
|
|
|
|
|
2007-04-28 22:19:50 +02:00
|
|
|
static void refcnt_lexblock(const struct lexblock *lexblock, const struct cu *cu)
|
|
|
|
{
|
|
|
|
struct tag *pos;
|
|
|
|
|
|
|
|
list_for_each_entry(pos, &lexblock->tags, node)
|
|
|
|
switch (pos->tag) {
|
|
|
|
case DW_TAG_variable:
|
|
|
|
refcnt_variable(tag__variable(pos), cu);
|
|
|
|
break;
|
|
|
|
case DW_TAG_inlined_subroutine:
|
|
|
|
refcnt_inline_expansion(tag__inline_expansion(pos), cu);
|
|
|
|
break;
|
|
|
|
case DW_TAG_lexical_block:
|
|
|
|
refcnt_lexblock(tag__lexblock(pos), cu);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-04 00:57:35 +01:00
|
|
|
static void refcnt_function(struct function *function, const struct cu *cu)
|
2006-11-18 17:33:48 +01:00
|
|
|
{
|
|
|
|
struct parameter *parameter;
|
2006-11-10 22:19:58 +01:00
|
|
|
|
2009-03-03 14:56:37 +01:00
|
|
|
function->proto.tag.visited = 1;
|
2006-11-10 22:19:58 +01:00
|
|
|
|
2006-12-30 19:34:20 +01:00
|
|
|
if (function->proto.tag.type != 0) /* if not void */ {
|
2009-03-18 16:17:07 +01:00
|
|
|
struct tag *type = cu__type(cu, function->proto.tag.type);
|
2006-11-10 22:19:58 +01:00
|
|
|
if (type != NULL)
|
2007-01-04 04:41:11 +01:00
|
|
|
refcnt_tag(type, cu);
|
2006-11-10 22:19:58 +01:00
|
|
|
}
|
|
|
|
|
2006-12-30 19:34:20 +01:00
|
|
|
list_for_each_entry(parameter, &function->proto.parms, tag.node)
|
2007-01-04 00:57:35 +01:00
|
|
|
refcnt_parameter(parameter, cu);
|
2006-11-10 22:19:58 +01:00
|
|
|
|
2007-04-28 22:19:50 +02:00
|
|
|
refcnt_lexblock(&function->lexblock, cu);
|
2006-11-10 22:19:58 +01:00
|
|
|
}
|
|
|
|
|
2009-04-01 16:43:58 +02:00
|
|
|
static int cu_refcnt_iterator(struct cu *cu, void *cookie __unused)
|
2006-11-10 22:19:58 +01:00
|
|
|
{
|
2009-04-01 16:43:58 +02:00
|
|
|
struct function *pos;
|
|
|
|
uint32_t id;
|
2006-11-10 22:19:58 +01:00
|
|
|
|
2009-04-01 16:43:58 +02:00
|
|
|
cu__for_each_function(cu, id, pos)
|
|
|
|
refcnt_function(pos, cu);
|
2006-11-18 17:33:48 +01:00
|
|
|
return 0;
|
2006-11-10 22:19:58 +01:00
|
|
|
}
|
|
|
|
|
2007-01-29 13:42:03 +01:00
|
|
|
static int lost_iterator(struct tag *tag, struct cu *cu,
|
|
|
|
void *cookie __unused)
|
2006-11-10 22:19:58 +01:00
|
|
|
{
|
dwarves: Remove some more DWARF details from the core
Had to be a big sweeping change, but the regression tests shows just
improvements :-)
Now we stop using an id in struct tag, only storing the type, that now
uses 16 bits only, as CTF does.
Each format loader has to go on adding the types to the core, that
figures out if it is a tag that can be on the tag->type field
(tag__is_tag_type).
Formats that already have the types separated and in sequence, such as
CTF, just ask the core to insert in the types_table directly with its
original ID.
For DWARF, we ask the core to put it on the table, in sequence, and return the
index, that is then stashed with the DWARF specific info (original id, type,
decl_line, etc) and hashed by the original id. Later we recode everything,
looking up via the original type, getting the small_id to put on the tag->type.
The underlying debugging info not needed by the core is stashed in tag->priv,
and the DWARF loader now just allocates sizeof(struct dwarf_tag) at the end of
the core tag and points it there, and makes that info available thru
cu->orig_info. In the future we can ask, when loading a cu, that this info be
trown away, so that we reduce the memory footprint for big multi-cu files such
as the Linux kernel.
There is also a routine to ask for inserting a NULL, as we still have
bugs in the CTF decoding and thus some entries are being lost, to avoid
using an undefined pointer when traversing the types_table the ctf
loader puts a NULL there via cu__table_nullify_type_entry() and then
cu__for_each_type skips those.
There is some more cleanups for leftovers that I avoided cleaning to
reduce this changeset.
And also while doing this I saw that enums can appear without any
enumerators and that an array with DW_TAG_GNU_vector is actually a
different tag, encoded this way till we get to DWARF4 ;-)
So now we don't have to lookup on a hash table looking for DWARF
offsets, we can do the more sensible thing of just indexing the
types_tags array.
Now to do some cleanups and try to get the per cu encoder done. Then
order all the cus per number of type entries, pick the one with more,
then go on merging/recoding the types of the others and putting the
parent linkage in place.
Just to show the extent of the changes:
$ codiff /tmp/libdwarves.so.1.0.0 build/libdwarves.so.1.0.0
/home/acme/git/pahole/dwarves.c:
struct cu | -4048
struct tag | -32
struct ptr_to_member_type | -32
struct namespace | -32
struct type | -32
struct class | -32
struct base_type | -32
struct array_type | -32
struct class_member | -32
struct lexblock | -32
struct ftype | -32
struct function | -64
struct parameter | -32
struct variable | -32
struct inline_expansion | -32
struct label | -32
struct enumerator | -32
17 structs changed
tag__follow_typedef | +3
tag__fprintf_decl_info | +25
array_type__fprintf | +6
type__name | -126
type__find_first_biggest_size_base_type_member | -3
typedef__fprintf | +16
imported_declaration__fprintf | +6
imported_module__fprintf | +3
cu__new | +26
cu__delete | +26
hashtags__hash | -65
hash_64 | -124
hlist_add_head | -78
hashtags__find | -157
cu__hash | -80
cu__add_tag | +20
tag__prefix | -3
cu__find_tag_by_id | -2
cu__find_type_by_id | -3
cu__find_first_typedef_of_type | +38
cu__find_base_type_by_name | +68
cu__find_base_type_by_name_and_size | +72
cu__find_struct_by_name | +59
cus__find_struct_by_name | +8
cus__find_tag_by_id | +5
cus__find_cu_by_name | -6
lexblock__find_tag_by_id | -173
cu__find_variable_by_id | -197
list__find_tag_by_id | -308
cu__find_parameter_by_id | -60
tag__ptr_name | +6
tag__name | +15
variable__type | +13
variable__name | +7
class_member__size | +6
parameter__name | -119
tag__parameter | -14
parameter__type | -143
type__fprintf | -29
union__fprintf | +6
class__add_vtable_entry | -9
type__add_member | -6
type__clone_members | -3
enumeration__add | -6
function__name | -156
ftype__has_parm_of_type | -39
class__find_holes | -27
class__has_hole_ge | -3
type__nr_members_of_type | +3
lexblock__account_inline_expansions | +3
cu__account_inline_expansions | -18
ftype__fprintf_parms | +46
function__tag_fprintf | +24
lexblock__fprintf | -6
ftype__fprintf | +3
function__fprintf_stats | -18
function__size | -6
class__vtable_fprintf | -11
class__fprintf | -21
tag__fprintf | -35
60 functions changed, 513 bytes added, 2054 bytes removed, diff: -1541
/home/acme/git/pahole/ctf_loader.c:
struct ctf_short_type | +0
14 structs changed
type__init | -14
type__new | -9
class__new | -12
create_new_base_type | -7
create_new_base_type_float | -7
create_new_array | -8
create_new_subroutine_type | -9
create_full_members | -18
create_short_members | -18
create_new_class | +1
create_new_union | +1
create_new_enumeration | -19
create_new_forward_decl | -2
create_new_typedef | +3
create_new_tag | -5
load_types | +16
class__fixup_ctf_bitfields | -3
17 functions changed, 21 bytes added, 131 bytes removed, diff: -110
/home/acme/git/pahole/dwarf_loader.c:
17 structs changed
zalloc | -56
tag__init | +3
array_type__new | +20
type__init | -24
class_member__new | +46
inline_expansion__new | +12
class__new | +81
lexblock__init | +19
function__new | +43
die__create_new_array | +20
die__create_new_parameter | +4
die__create_new_label | +4
die__create_new_subroutine_type | +113
die__create_new_enumeration | -21
die__process_class | +79
die__process_namespace | +76
die__create_new_inline_expansion | +4
die__process_function | +147
__die__process_tag | +34
die__process_unit | +56
die__process | +90
21 functions changed, 851 bytes added, 101 bytes removed, diff: +750
/home/acme/git/pahole/dwarves.c:
struct ptr_table | +16
struct cu_orig_info | +32
2 structs changed
tag__decl_line | +68
tag__decl_file | +70
tag__orig_id | +71
ptr_table__init | +46
ptr_table__exit | +37
ptr_table__add | +183
ptr_table__add_with_id | +165
ptr_table__entry | +64
cu__table_add_tag | +171
cu__table_nullify_type_entry | +38
10 functions changed, 913 bytes added, diff: +913
/home/acme/git/pahole/ctf_loader.c:
2 structs changed
tag__alloc | +52
1 function changed, 52 bytes added, diff: +52
/home/acme/git/pahole/dwarf_loader.c:
struct dwarf_tag | +48
struct dwarf_cu | +4104
4 structs changed
dwarf_cu__init | +83
hashtags__hash | +61
hash_64 | +124
hlist_add_head | +78
hashtags__find | +161
cu__hash | +95
tag__is_tag_type | +171
tag__is_type | +85
tag__is_union | +28
tag__is_struct | +57
tag__is_typedef | +28
tag__is_enumeration | +28
dwarf_cu__find_tag_by_id | +56
dwarf_cu__find_type_by_id | +63
tag__alloc | +114
__tag__print_type_not_found | +108
namespace__recode_dwarf_types | +346
tag__namespace | +14
tag__has_namespace | +86
tag__is_namespace | +28
type__recode_dwarf_specification | +182
tag__type | +14
__tag__print_abstract_origin_not_found | +105
ftype__recode_dwarf_types | +322
tag__ftype | +14
tag__parameter | +14
lexblock__recode_dwarf_types | +736
tag__lexblock | +14
tag__label | +14
tag__recode_dwarf_type | +766
tag__ptr_to_member_type | +14
cu__recode_dwarf_types_table | +88
cu__recode_dwarf_types | +48
dwarf_tag__decl_file | +77
strings__ptr | +33
dwarf_tag__decl_line | +59
dwarf_tag__orig_id | +59
dwarf_tag__orig_type | +59
38 functions changed, 4432 bytes added, diff: +4432
build/libdwarves.so.1.0.0:
147 functions changed, 6782 bytes added, 2286 bytes removed, diff: +4496
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2009-03-06 00:29:35 +01:00
|
|
|
if (!tag->visited && tag__decl_file(tag, cu)) {
|
2007-04-28 21:13:35 +02:00
|
|
|
tag__fprintf(tag, cu, NULL, stdout);
|
2007-03-30 16:25:25 +02:00
|
|
|
puts(";\n");
|
|
|
|
}
|
2006-11-10 22:19:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cu_lost_iterator(struct cu *cu, void *cookie)
|
|
|
|
{
|
2009-04-01 16:43:58 +02:00
|
|
|
return cu__for_all_tags(cu, lost_iterator, cookie);
|
2006-11-10 22:19:58 +01:00
|
|
|
}
|
|
|
|
|
2009-02-13 13:57:23 +01:00
|
|
|
int main(int argc __unused, char *argv[])
|
2006-11-10 22:19:58 +01:00
|
|
|
{
|
2007-04-24 21:09:34 +02:00
|
|
|
int err;
|
2008-10-01 17:47:42 +02:00
|
|
|
struct cus *cus = cus__new();
|
2006-11-10 22:19:58 +01:00
|
|
|
|
2008-10-02 19:34:42 +02:00
|
|
|
if (dwarves__init(0) || cus == NULL) {
|
2006-11-11 19:31:04 +01:00
|
|
|
fputs("prefcnt: insufficient memory\n", stderr);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2009-03-13 14:49:01 +01:00
|
|
|
err = cus__load_files(cus, NULL, argv + 1);
|
2007-03-30 19:07:09 +02:00
|
|
|
if (err != 0)
|
2006-11-10 22:19:58 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2006-12-01 02:48:34 +01:00
|
|
|
cus__for_each_cu(cus, cu_refcnt_iterator, NULL, NULL);
|
|
|
|
cus__for_each_cu(cus, cu_lost_iterator, NULL, NULL);
|
2006-11-10 22:19:58 +01:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|