2006-10-25 01:42:39 +02:00
|
|
|
/*
|
2006-10-25 17:49:47 +02:00
|
|
|
Copyright (C) 2006 Mandriva Conectiva S.A.
|
|
|
|
Copyright (C) 2006 Arnaldo Carvalho de Melo <acme@mandriva.com>
|
|
|
|
|
2006-10-25 01:42:39 +02:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2006-11-02 17:47:37 +01:00
|
|
|
#include <getopt.h>
|
2006-10-25 01:42:39 +02:00
|
|
|
#include <stdio.h>
|
2006-10-28 23:22:42 +02:00
|
|
|
#include <dwarf.h>
|
2006-10-25 01:42:39 +02:00
|
|
|
#include <stdlib.h>
|
2006-11-11 17:15:50 +01:00
|
|
|
#include <string.h>
|
2006-10-25 01:42:39 +02:00
|
|
|
|
2006-10-28 18:45:59 +02:00
|
|
|
#include "classes.h"
|
2006-10-25 01:42:39 +02:00
|
|
|
|
2006-12-01 02:23:41 +01:00
|
|
|
static char *class__exclude_prefix;
|
|
|
|
static size_t class__exclude_prefix_len;
|
2006-12-01 02:12:21 +01:00
|
|
|
|
2006-12-01 02:36:55 +01:00
|
|
|
static char *cu__exclude_prefix;
|
|
|
|
static size_t cu__exclude_prefix_len;
|
|
|
|
|
2006-12-01 03:08:10 +01:00
|
|
|
static char *decl_exclude_prefix;
|
|
|
|
static size_t decl_exclude_prefix_len;
|
|
|
|
|
2006-11-04 00:24:37 +01:00
|
|
|
struct structure {
|
|
|
|
struct list_head node;
|
|
|
|
const struct class *class;
|
|
|
|
unsigned int nr_files;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct structure *structure__new(const struct class *class)
|
|
|
|
{
|
|
|
|
struct structure *self = malloc(sizeof(*self));
|
|
|
|
|
|
|
|
if (self != NULL) {
|
|
|
|
self->class = class;
|
|
|
|
self->nr_files = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static LIST_HEAD(structures__list);
|
|
|
|
|
|
|
|
static struct structure *structures__find(const char *name)
|
|
|
|
{
|
|
|
|
struct structure *pos;
|
|
|
|
|
|
|
|
list_for_each_entry(pos, &structures__list, node)
|
|
|
|
if (strcmp(pos->class->name, name) == 0)
|
|
|
|
return pos;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void structures__add(const struct class *class)
|
|
|
|
{
|
|
|
|
struct structure *str = structures__find(class->name);
|
|
|
|
|
|
|
|
if (str == NULL) {
|
|
|
|
str = structure__new(class);
|
|
|
|
if (str != NULL)
|
|
|
|
list_add(&str->node, &structures__list);
|
|
|
|
} else {
|
|
|
|
if (str->class->size != class->size)
|
|
|
|
printf("%s size changed! was %llu, now its %llu\n",
|
|
|
|
class->name, str->class->size, class->size);
|
|
|
|
str->nr_files++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void structure__print(struct structure *self)
|
|
|
|
{
|
2006-11-11 17:15:50 +01:00
|
|
|
printf("%-32.32s %5u\n", self->class->name, self->nr_files);
|
2006-11-04 00:24:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void print_total_structure_stats(void)
|
|
|
|
{
|
|
|
|
struct structure *pos;
|
|
|
|
|
|
|
|
printf("%-32.32s %5.5s\n", "name", "src#");
|
|
|
|
list_for_each_entry(pos, &structures__list, node)
|
|
|
|
structure__print(pos);
|
|
|
|
}
|
|
|
|
|
2006-12-01 02:36:55 +01:00
|
|
|
struct cu *cu__filter(struct cu *cu)
|
|
|
|
{
|
|
|
|
if (cu__exclude_prefix != NULL &&
|
|
|
|
(cu->name == NULL ||
|
|
|
|
strncmp(cu__exclude_prefix, cu->name,
|
|
|
|
cu__exclude_prefix_len) == 0))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return cu;
|
|
|
|
}
|
|
|
|
|
2006-12-01 02:12:21 +01:00
|
|
|
struct class *class__to_struct(struct class *class)
|
|
|
|
{
|
|
|
|
struct class *typedef_alias;
|
|
|
|
|
|
|
|
if (!class__is_struct(class, &typedef_alias))
|
|
|
|
return NULL;
|
|
|
|
return typedef_alias ?: class;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct class *class__filter(struct class *class)
|
|
|
|
{
|
|
|
|
class = class__to_struct(class);
|
|
|
|
if (class == NULL) /* Not a structure */
|
|
|
|
return NULL;
|
|
|
|
|
2006-12-01 03:00:24 +01:00
|
|
|
if (class->name == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2006-12-01 02:23:41 +01:00
|
|
|
if (class__exclude_prefix != NULL &&
|
2006-12-01 03:00:24 +01:00
|
|
|
strncmp(class__exclude_prefix, class->name,
|
|
|
|
class__exclude_prefix_len) == 0)
|
2006-12-01 02:12:21 +01:00
|
|
|
return NULL;
|
|
|
|
|
2006-12-01 03:08:10 +01:00
|
|
|
if (decl_exclude_prefix != NULL &&
|
|
|
|
(class->tag.decl_file == NULL ||
|
|
|
|
strncmp(decl_exclude_prefix, class->tag.decl_file,
|
|
|
|
decl_exclude_prefix_len) == 0))
|
|
|
|
return NULL;
|
|
|
|
|
2006-12-01 02:12:21 +01:00
|
|
|
return class;
|
|
|
|
}
|
|
|
|
|
2006-11-12 20:33:37 +01:00
|
|
|
static int total_structure_iterator(struct class *class, void *cookie)
|
2006-11-04 00:24:37 +01:00
|
|
|
{
|
2006-12-01 03:00:24 +01:00
|
|
|
structures__add(class);
|
2006-11-04 00:24:37 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cu_total_structure_iterator(struct cu *cu, void *cookie)
|
|
|
|
{
|
2006-12-01 03:00:24 +01:00
|
|
|
return cu__for_each_class(cu, total_structure_iterator, cookie,
|
|
|
|
class__filter);
|
2006-11-04 00:24:37 +01:00
|
|
|
}
|
|
|
|
|
2006-11-02 17:47:37 +01:00
|
|
|
static struct option long_options[] = {
|
2006-11-05 18:34:54 +01:00
|
|
|
{ "cacheline_size", required_argument, NULL, 'c' },
|
2006-11-02 17:47:37 +01:00
|
|
|
{ "class_name_len", no_argument, NULL, 'N' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
2006-12-02 15:51:59 +01:00
|
|
|
{ "holes", required_argument, NULL, 'H' },
|
2006-11-02 17:47:37 +01:00
|
|
|
{ "nr_members", no_argument, NULL, 'n' },
|
|
|
|
{ "sizes", no_argument, NULL, 's' },
|
2006-11-04 00:24:37 +01:00
|
|
|
{ "total_struct_stats", no_argument, NULL, 't' },
|
2006-12-01 02:12:21 +01:00
|
|
|
{ "exclude", required_argument, NULL, 'x' },
|
2006-12-01 02:36:55 +01:00
|
|
|
{ "cu_exclude", required_argument, NULL, 'X' },
|
2006-12-01 03:08:10 +01:00
|
|
|
{ "decl_exclude", required_argument, NULL, 'D' },
|
2006-11-02 17:47:37 +01:00
|
|
|
{ NULL, 0, NULL, 0, }
|
|
|
|
};
|
|
|
|
|
|
|
|
static void usage(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"usage: pfunct [options] <file_name> {<function_name>}\n"
|
|
|
|
" where: \n"
|
2006-11-05 18:34:54 +01:00
|
|
|
" -h, --help show usage info\n"
|
2006-12-02 15:51:59 +01:00
|
|
|
" -H, --holes <nr_holes> show only structs at least <nr_holes> holes\n"
|
|
|
|
" -c, --cacheline_size <size> set cacheline size (default=%d)\n"
|
2006-11-30 22:03:59 +01:00
|
|
|
" -n, --nr_members show number of members\n"
|
2006-11-05 18:34:54 +01:00
|
|
|
" -N, --class_name_len show size of classes\n"
|
|
|
|
" -s, --sizes show size of classes\n"
|
2006-12-01 02:12:21 +01:00
|
|
|
" -t, --total_struct_stats show Multi-CU structure stats\n"
|
2006-12-01 03:08:10 +01:00
|
|
|
" -D, --decl_exclude <prefix> exclude classes declared in files with prefix\n"
|
2006-12-01 02:36:55 +01:00
|
|
|
" -x, --exclude <prefix> exclude prefixed classes from reports\n"
|
|
|
|
" -X, --cu_exclude <prefix> exclude prefixed compilation units from reports\n",
|
2006-11-05 18:34:54 +01:00
|
|
|
DEFAULT_CACHELINE_SIZE);
|
2006-11-02 17:47:37 +01:00
|
|
|
}
|
|
|
|
|
2006-11-12 20:33:37 +01:00
|
|
|
static int nr_members_iterator(struct class *class, void *cookie)
|
2006-11-02 17:47:37 +01:00
|
|
|
{
|
2006-12-01 03:00:24 +01:00
|
|
|
if (class->nr_members > 0)
|
2006-11-02 17:47:37 +01:00
|
|
|
printf("%s: %u\n", class->name, class->nr_members);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cu_nr_members_iterator(struct cu *cu, void *cookie)
|
|
|
|
{
|
2006-12-01 03:00:24 +01:00
|
|
|
return cu__for_each_class(cu, nr_members_iterator, cookie,
|
|
|
|
class__filter);
|
2006-11-02 17:47:37 +01:00
|
|
|
}
|
|
|
|
|
2006-11-12 20:33:37 +01:00
|
|
|
static int sizes_iterator(struct class *class, void *cookie)
|
2006-11-02 17:47:37 +01:00
|
|
|
{
|
2006-12-01 03:00:24 +01:00
|
|
|
if (class->size > 0) {
|
2006-12-01 02:12:21 +01:00
|
|
|
class__find_holes(class);
|
|
|
|
printf("%s: %llu %u\n",
|
|
|
|
class->name, class->size, class->nr_holes);
|
|
|
|
}
|
2006-11-02 17:47:37 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cu_sizes_iterator(struct cu *cu, void *cookie)
|
|
|
|
{
|
2006-12-01 03:00:24 +01:00
|
|
|
return cu__for_each_class(cu, sizes_iterator, cookie, class__filter);
|
2006-11-02 17:47:37 +01:00
|
|
|
}
|
|
|
|
|
2006-11-27 18:54:02 +01:00
|
|
|
static int holes_iterator(struct class *class, void *cookie)
|
|
|
|
{
|
2006-12-02 15:51:59 +01:00
|
|
|
const unsigned int nr_holes = *(unsigned int *)cookie;
|
2006-12-01 03:00:24 +01:00
|
|
|
class__find_holes(class);
|
2006-12-02 15:51:59 +01:00
|
|
|
if (class->nr_holes >= nr_holes)
|
2006-12-01 03:00:24 +01:00
|
|
|
class__print(class);
|
2006-11-27 18:54:02 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cu_holes_iterator(struct cu *cu, void *cookie)
|
|
|
|
{
|
2006-12-02 15:51:59 +01:00
|
|
|
return cu__for_each_class(cu, holes_iterator, cookie, class__filter);
|
2006-11-27 18:54:02 +01:00
|
|
|
}
|
|
|
|
|
2006-11-30 22:35:29 +01:00
|
|
|
static int class_name_len_iterator(struct class *class, void *cookie)
|
|
|
|
{
|
2006-12-01 03:00:24 +01:00
|
|
|
printf("%s: %u\n", class->name, strlen(class->name));
|
2006-11-30 22:35:29 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cu_class_name_len_iterator(struct cu *cu, void *cookie)
|
|
|
|
{
|
2006-12-01 03:00:24 +01:00
|
|
|
return cu__for_each_class(cu, class_name_len_iterator, NULL,
|
|
|
|
class__filter);
|
2006-11-30 22:35:29 +01:00
|
|
|
}
|
|
|
|
|
2006-12-01 02:12:21 +01:00
|
|
|
static int class__iterator(struct class *class, void *cookie)
|
|
|
|
{
|
2006-12-01 03:00:24 +01:00
|
|
|
class__find_holes(class);
|
|
|
|
class__print(class);
|
2006-12-01 02:12:21 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cu__iterator(struct cu *cu, void *cookie)
|
|
|
|
{
|
2006-12-01 03:00:24 +01:00
|
|
|
return cu__for_each_class(cu, class__iterator, NULL,
|
|
|
|
class__filter);
|
2006-12-01 02:12:21 +01:00
|
|
|
}
|
|
|
|
|
2006-10-28 19:08:46 +02:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2006-11-02 17:47:37 +01:00
|
|
|
int option, option_index;
|
2006-11-11 19:31:04 +01:00
|
|
|
struct cus *cus;
|
2006-11-02 17:47:37 +01:00
|
|
|
char *file_name;
|
|
|
|
char *class_name = NULL;
|
2006-12-05 17:55:31 +01:00
|
|
|
unsigned opts = 0;
|
|
|
|
enum {
|
|
|
|
FLAG_show_sizes = 1,
|
|
|
|
FLAG_show_nr_members,
|
|
|
|
FLAG_show_class_name_len,
|
|
|
|
FLAG_show_total_structure_stats
|
|
|
|
};
|
2006-12-02 15:51:59 +01:00
|
|
|
int nr_holes = 0;
|
2006-11-02 17:47:37 +01:00
|
|
|
|
2006-12-02 15:51:59 +01:00
|
|
|
while ((option = getopt_long(argc, argv, "c:D:hH:nNstx:X:",
|
2006-11-02 17:47:37 +01:00
|
|
|
long_options, &option_index)) >= 0)
|
|
|
|
switch (option) {
|
2006-11-05 18:34:54 +01:00
|
|
|
case 'c': cacheline_size = atoi(optarg); break;
|
2006-12-02 15:51:59 +01:00
|
|
|
case 'H': nr_holes = atoi(optarg); break;
|
2006-12-05 17:55:31 +01:00
|
|
|
case 's': opts |= FLAG_show_sizes; break;
|
|
|
|
case 'n': opts |= FLAG_show_nr_members; break;
|
|
|
|
case 'N': opts |= FLAG_show_class_name_len; break;
|
|
|
|
case 't': opts |= FLAG_show_total_structure_stats; break;
|
2006-12-01 03:08:10 +01:00
|
|
|
case 'D': decl_exclude_prefix = optarg;
|
|
|
|
decl_exclude_prefix_len = strlen(decl_exclude_prefix);
|
2006-12-05 17:56:47 +01:00
|
|
|
break;
|
2006-12-01 02:23:41 +01:00
|
|
|
case 'x': class__exclude_prefix = optarg;
|
|
|
|
class__exclude_prefix_len = strlen(class__exclude_prefix);
|
2006-12-05 17:56:47 +01:00
|
|
|
break;
|
2006-12-01 02:36:55 +01:00
|
|
|
case 'X': cu__exclude_prefix = optarg;
|
|
|
|
cu__exclude_prefix_len = strlen(cu__exclude_prefix);
|
2006-12-05 17:56:47 +01:00
|
|
|
break;
|
2006-11-04 00:24:37 +01:00
|
|
|
case 'h': usage(); return EXIT_SUCCESS;
|
|
|
|
default: usage(); return EXIT_FAILURE;
|
2006-11-02 17:47:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (optind < argc) {
|
|
|
|
switch (argc - optind) {
|
|
|
|
case 1: file_name = argv[optind++]; break;
|
|
|
|
case 2: file_name = argv[optind++];
|
|
|
|
class_name = argv[optind++]; break;
|
|
|
|
default: usage(); return EXIT_FAILURE;
|
|
|
|
}
|
2006-11-11 17:15:50 +01:00
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
return EXIT_FAILURE;
|
2006-10-28 19:08:46 +02:00
|
|
|
}
|
|
|
|
|
2006-11-11 19:31:04 +01:00
|
|
|
cus = cus__new(file_name);
|
|
|
|
if (cus == NULL) {
|
|
|
|
fputs("pahole: insufficient memory\n", stderr);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cus__load(cus) != 0) {
|
2006-10-28 19:08:46 +02:00
|
|
|
fprintf(stderr, "pahole: couldn't load DWARF info from %s\n",
|
2006-11-02 17:47:37 +01:00
|
|
|
file_name);
|
2006-10-28 19:08:46 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2006-10-26 08:06:32 +02:00
|
|
|
|
2006-12-05 17:55:31 +01:00
|
|
|
if (opts & FLAG_show_total_structure_stats) {
|
2006-12-01 02:48:34 +01:00
|
|
|
cus__for_each_cu(cus, cu_total_structure_iterator, NULL,
|
|
|
|
cu__filter);
|
2006-11-04 00:24:37 +01:00
|
|
|
print_total_structure_stats();
|
2006-12-05 17:55:31 +01:00
|
|
|
} else if (opts & FLAG_show_nr_members)
|
2006-12-01 02:48:34 +01:00
|
|
|
cus__for_each_cu(cus, cu_nr_members_iterator, NULL,
|
|
|
|
cu__filter);
|
2006-12-05 17:55:31 +01:00
|
|
|
else if (opts & FLAG_show_sizes)
|
2006-12-01 02:48:34 +01:00
|
|
|
cus__for_each_cu(cus, cu_sizes_iterator, NULL, cu__filter);
|
2006-12-05 17:55:31 +01:00
|
|
|
else if (opts & FLAG_show_class_name_len)
|
2006-12-01 02:48:34 +01:00
|
|
|
cus__for_each_cu(cus, cu_class_name_len_iterator, NULL,
|
|
|
|
cu__filter);
|
2006-12-02 15:51:59 +01:00
|
|
|
else if (nr_holes > 0)
|
|
|
|
cus__for_each_cu(cus, cu_holes_iterator, &nr_holes, cu__filter);
|
2006-11-02 17:47:37 +01:00
|
|
|
else if (class_name != NULL) {
|
2006-11-12 20:33:37 +01:00
|
|
|
struct class *class = cus__find_class_by_name(cus, class_name);
|
2006-11-05 05:17:19 +01:00
|
|
|
struct class *alias;
|
|
|
|
|
2006-11-14 16:30:15 +01:00
|
|
|
if (class != NULL && class__is_struct(class, &alias)) {
|
2006-11-12 20:33:37 +01:00
|
|
|
class__find_holes(alias ?: class);
|
|
|
|
class__print(alias ?: class);
|
2006-10-28 23:10:47 +02:00
|
|
|
} else
|
2006-11-02 17:47:37 +01:00
|
|
|
printf("struct %s not found!\n", class_name);
|
|
|
|
} else
|
2006-12-01 02:48:34 +01:00
|
|
|
cus__for_each_cu(cus, cu__iterator, NULL, cu__filter);
|
2006-10-25 18:32:07 +02:00
|
|
|
|
2006-10-28 19:08:46 +02:00
|
|
|
return EXIT_SUCCESS;
|
2006-10-25 01:42:39 +02:00
|
|
|
}
|