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 <getopt.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2007-01-11 19:07:05 +01:00
|
|
|
#include "dwarves.h"
|
2006-11-10 22:19:58 +01:00
|
|
|
|
|
|
|
static struct option long_options[] = {
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ NULL, 0, NULL, 0, }
|
|
|
|
};
|
|
|
|
|
|
|
|
static void usage(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"usage: prefcnt [options] <file_name>\n"
|
|
|
|
" where: \n"
|
|
|
|
" -h, --help usage options\n");
|
|
|
|
}
|
|
|
|
|
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 */
|
2007-01-04 04:41:11 +01:00
|
|
|
struct tag *type = cu__find_tag_by_id(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 */
|
2006-12-30 19:34:20 +01:00
|
|
|
struct tag *type = cu__find_tag_by_id(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
|
|
|
{
|
2006-11-18 14:54:02 +01:00
|
|
|
if (variable->tag.type != 0) { /* if not void */
|
2007-01-03 01:26:01 +01:00
|
|
|
struct tag *type = cu__find_tag_by_id(cu, variable->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
|
|
|
{
|
2006-11-18 14:54:02 +01:00
|
|
|
if (exp->tag.type != 0) { /* if not void */
|
2007-01-03 01:26:01 +01:00
|
|
|
struct tag *type = cu__find_tag_by_id(cu, exp->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
|
|
|
|
2006-12-29 18:28:58 +01:00
|
|
|
tag->refcnt++;
|
2006-11-18 17:33:48 +01:00
|
|
|
|
2006-12-29 18:28:58 +01:00
|
|
|
if (tag->tag == DW_TAG_structure_type ||
|
|
|
|
tag->tag == DW_TAG_union_type)
|
2007-01-09 13:00:47 +01:00
|
|
|
list_for_each_entry(member, &tag__type(tag)->members, tag.node)
|
2007-01-04 04:41:11 +01:00
|
|
|
refcnt_member(member, cu);
|
2006-11-18 17:33:48 +01:00
|
|
|
}
|
|
|
|
|
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;
|
2007-01-03 01:26:01 +01:00
|
|
|
struct tag *pos;
|
2006-11-10 22:19:58 +01:00
|
|
|
|
2006-12-30 19:34:20 +01:00
|
|
|
function->proto.tag.refcnt++;
|
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 */ {
|
2007-01-04 00:57:35 +01:00
|
|
|
struct tag *type =
|
|
|
|
cu__find_tag_by_id(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-01-03 01:26:01 +01:00
|
|
|
list_for_each_entry(pos, &function->lexblock.tags, node)
|
|
|
|
switch (pos->tag) {
|
|
|
|
case DW_TAG_variable:
|
2007-01-04 00:57:35 +01:00
|
|
|
refcnt_variable(tag__variable(pos), cu);
|
2007-01-03 01:26:01 +01:00
|
|
|
break;
|
|
|
|
case DW_TAG_inlined_subroutine:
|
2007-01-04 00:57:35 +01:00
|
|
|
refcnt_inline_expansion(tag__inline_expansion(pos), cu);
|
2007-01-03 01:26:01 +01:00
|
|
|
break;
|
|
|
|
}
|
2006-11-10 22:19:58 +01:00
|
|
|
}
|
|
|
|
|
2007-01-04 00:57:35 +01:00
|
|
|
static int refcnt_function_iterator(struct function *function,
|
2007-01-29 13:42:03 +01:00
|
|
|
const struct cu *cu,
|
|
|
|
void *cookie __unused)
|
2006-11-10 22:19:58 +01:00
|
|
|
{
|
2007-01-04 00:57:35 +01:00
|
|
|
refcnt_function(function, cu);
|
2006-11-18 17:33:48 +01:00
|
|
|
return 0;
|
2006-11-10 22:19:58 +01:00
|
|
|
}
|
|
|
|
|
2006-12-29 18:28:58 +01:00
|
|
|
static int refcnt_tag_iterator(struct tag *tag, struct cu *cu, void *cookie)
|
2006-11-10 22:19:58 +01:00
|
|
|
{
|
2006-12-29 18:28:58 +01:00
|
|
|
if (tag->tag == DW_TAG_structure_type)
|
2007-01-04 04:41:11 +01:00
|
|
|
class__find_holes(tag__class(tag), cu);
|
2007-01-04 00:29:24 +01:00
|
|
|
else if (tag->tag == DW_TAG_structure_type)
|
2007-01-04 00:57:35 +01:00
|
|
|
refcnt_function_iterator(tag__function(tag), cu, cookie);
|
2006-11-10 22:19:58 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cu_refcnt_iterator(struct cu *cu, void *cookie)
|
|
|
|
{
|
2006-12-29 18:28:58 +01:00
|
|
|
cu__for_each_tag(cu, refcnt_tag_iterator, cookie, NULL);
|
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
|
|
|
{
|
2006-12-29 18:28:58 +01:00
|
|
|
if (tag->refcnt == 0 && tag->decl_file != NULL)
|
2007-02-01 16:50:28 +01:00
|
|
|
tag__print(tag, cu, NULL, NULL, 0, stdout);
|
2006-11-10 22:19:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cu_lost_iterator(struct cu *cu, void *cookie)
|
|
|
|
{
|
2006-12-29 18:28:58 +01:00
|
|
|
return cu__for_each_tag(cu, lost_iterator, cookie, NULL);
|
2006-11-10 22:19:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int option, option_index;
|
2006-11-11 19:31:04 +01:00
|
|
|
struct cus *cus;
|
2006-11-11 17:15:50 +01:00
|
|
|
const char *file_name;
|
2006-11-10 22:19:58 +01:00
|
|
|
|
|
|
|
while ((option = getopt_long(argc, argv, "h",
|
|
|
|
long_options, &option_index)) >= 0)
|
|
|
|
switch (option) {
|
|
|
|
case 'h': usage(); return EXIT_SUCCESS;
|
|
|
|
default: usage(); return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind < argc) {
|
|
|
|
switch (argc - optind) {
|
|
|
|
case 1: file_name = argv[optind++]; break;
|
|
|
|
default: usage(); return EXIT_FAILURE;
|
|
|
|
}
|
2006-11-11 17:15:50 +01:00
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
return EXIT_FAILURE;
|
2006-11-10 22:19:58 +01:00
|
|
|
}
|
|
|
|
|
2007-01-19 00:41:25 +01:00
|
|
|
dwarves__init(0);
|
|
|
|
|
2007-01-07 15:30:58 +01:00
|
|
|
cus = cus__new(NULL, NULL);
|
2006-11-11 19:31:04 +01:00
|
|
|
if (cus == NULL) {
|
|
|
|
fputs("prefcnt: insufficient memory\n", stderr);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2006-12-20 15:35:45 +01:00
|
|
|
if (cus__load(cus, file_name) != 0) {
|
2006-11-10 22:19:58 +01:00
|
|
|
fprintf(stderr, "prefcnt: couldn't load DWARF info from %s\n",
|
|
|
|
file_name);
|
|
|
|
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;
|
|
|
|
}
|