For now its just the direct ancestor of struct type. But it will exists by
itself, to represent the DW_TAG_namespace DWARF tag, that is how the C++
'namespace' (and other languages too, heck, I'd love to get my hands on a
binary with DWARF info built from, say, ADA source code, objectiveC... COBOL!
:-P).
Signed-off-by: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
This is in preparation for the introduction of struct namespace, that will be
struct type ancestor.
Signed-off-by: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
For now only affects the --contains output.
Example showing the structs that include struct list_head in a linux kernel module:
[acme@filo pahole]$ pahole --recursive --contains list_head examples/ipv6.ko.debug.x86-64
inet_protosw
proto
sock_iocb
key_type
msg_queue
msg_msg
nf_hook_ops
softnet_data
net_device
softnet_data
dma_device
dma_client
dma_chan
class_device
net_device
softnet_data
dma_chan
class
klist_node
device_driver
device
klist
device_driver
bus_type
device
file_system_type
nfs_lock_info
file_lock
block_device
address_space
inode
dquot
mem_dqinfo
super_block
inode
signal_struct
page
kioctx
file
kiocb
work_struct
delayed_work
kioctx
timer_list
ifmcaddr6
inet6_dev
inet6_ifaddr
neigh_table
neighbour
net_device
softnet_data
sock
inet_sock
delayed_work
kioctx
plist_head
task_struct
sigpending
signal_struct
task_struct
user_struct
device
dev_pm_info
device
mutex_waiter
mutex
seq_file
block_device
quota_info
super_block
dquot
super_block
inode
zone
per_cpu_pages
free_area
kset
bus_type
subsystem
class
bus_type
__wait_queue_head
__wait_queue
rw_semaphore
quota_info
super_block
super_block
inode
key
blocking_notifier_head
bus_type
subsystem
class
bus_type
mm_struct
dentry
vm_area_struct
kobject
class_device
net_device
softnet_data
dma_chan
device_driver
module_kobject
module
device
kset
bus_type
subsystem
class
bus_type
lock_class
module
mm_struct
task_struct
Handling in multi-cu objects is not very precise, as the same struct has
different dwarf offsets (id) in each CU. A mitigation for this problem will be
provided with the --cu_list and --cu_name upcoming options, where one will be
able to get a list of the object files in a, for instance, linux kernel .ko
module and also to specify a cu name to be the only to be considered when
processing multi-cu files (again, such as .ko linux kernel modules).
This ends up being also useful to generate a reverse class hierarchy :-)
Signed-off-by: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
First user is pahole, that now has a --contains CLASS_NAME option, that will
show which classes contains CLASS_NAME, i.e.:
struct foo {
struct bar baz;
int i;
};
on an object file called with '--contains bar' will produce:
foo
if --verbose is used it will tell the number of CLASS_NAME members, so, in the
above example:
foo:1
Next thing will be a --recursive flag, that will show all the structs that
contains CLASS_NAME and the ones that contains the ones which contains and...
:-)
Useful to evaluate the impact that increasing or decreasing the size of some
important struct will have on the whole project that uses the struct.
Signed-off-by: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
By default, pahole will display the offsets of the inner struct members from
the top level struct. If the user wants to focus on some inner structs, just
call the tool with the -r option to use relative offset instead of the base
offset.
Signed-off-by: Eugene Teo <eteo@redhat.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
So that we can go on adding more config knobs without requiring adding new
parameters to lots of functions.
Signed-off-by: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
Just look for a typedef that points to the anonymous struct, if it matches
the -x prefix, exclude it.
[acme@filo pahole]$ pahole -PAae examples/anonymous_struct_typedef
/home/acme/git/pahole/examples/anonymous_struct_typedef.c:12 12 8 4
teste_t 12 8 4
[acme@filo pahole]$ pahole -x teste -PAae examples/anonymous_struct_typedef
/home/acme/git/pahole/examples/anonymous_struct_typedef.c:12 12 8 4
[acme@filo pahole]$
Signed-off-by: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
For example, the inner, anonymous struct here:
struct {
int d;
int z;
struct {
short a;
int b;
char c;
};
} biba = { .d = 2, };
[acme@filo pahole]$ pahole --packable --anon_include --nested_anon_include -e examples/anonymous_struct_typedef
/home/acme/git/pahole/examples/anonymous_struct_typedef.c:12 12 8 4
teste_t 12 8 4
[acme@filo pahole]$
The long options can be shortened to -PAae :-)
Signed-off-by: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
Suggested by: Jeff Muizelaar.
And it was wrong in the sense that the help was like:
--executable|-e FILE <SNIP lots of other options> FILE
So now its a bit redundant, like:
--executable|-e FILE <SNIP lots of other options> -e FILE
But as this is the most common usage pattern, give it more visibility.
Signed-off-by: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
A more brute force approach: create a clone, reorganize it, if the resulting
size is less than the cloned class, it is packable.
Signed-off-by: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
Now we have:
[acme@filo pahole]$ pahole --help
Usage: pahole [OPTION...] [FILE] {[CLASS]}
-a, --anon_include include anonymous classes
-A, --nested_anon_include include nested (inside other structs) anonymous
classes
-B, --bit_holes=NR_HOLES Show only structs at least NR_HOLES bit holes
-c, --cacheline_size=SIZE set cacheline size to SIZE
-D, --decl_exclude=PREFIX exclude classes declared in files with PREFIX
-E, --expand_types expand class members
-H, --holes=NR_HOLES show only structs at least NR_HOLES holes
-m, --nr_methods show number of methods
-n, --nr_members show number of members
-N, --class_name_len show size of classes
-P, --packable show only structs that has holes that can be
packed
-R, --reorganize reorg struct trying to kill holes
-s, --sizes show size of classes
-S, --show_reorg_steps show the struct layout at each reorganization step
-t, --nr_definitions show how many times struct was defined
-V, --verbose be verbose
-x, --exclude=PREFIX exclude PREFIXed classes
-X, --cu_exclude=PREFIX exclude PREFIXed compilation units
Input Selection:
--debuginfo-path=PATH Search path for separate debuginfo files
-e, --executable=FILE Find addresses in FILE
-k, --kernel Find addresses in the running kernel
-K, --offline-kernel[=RELEASE] Kernel with all modules
-M, --linux-process-map=FILE Find addresses in files mapped as read from
FILE in Linux /proc/PID/maps format
-p, --pid=PID Find addresses in files mapped into process PID
-?, --help Give this help list
--usage Give a short usage message
Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.
Signed-off-by: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
[acme@mica pahole]$ pahole lala
pahole: Permission denied
[acme@mica pahole]$ pahole foo
pahole: No such file or directory
[acme@mica pahole]$ pahole ctracer.c
pahole: couldn't load DWARF info from ctracer.c
[acme@mica pahole]$
Thanks to Matthew Wilcox for noticing how lame it was :-)
Signed-off-by: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
So that in tools like ctracer we can print to a file, most of the tools just
pass stdout, keeping the previous behaviour.
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Will be used in ctracer to create a struct subset with just the types for which
we have "collectors", i.e. functions that reduce complex types to base types
that will be put in the mini-struct, that will be as tightly packed as it can
be.
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This cset also does a fixup for cases where the compiler keeps the type
specified by the programmer for a bitfield but uses less space to combine with
the next, non-bitfield member, these cases can be caught using plain pahole and
will appear with this comment:
/* --- cacheline 1 boundary (64 bytes) --- */
int bitfield1:1; /* 64 4 */
int bitfield2:1; /* 64 4 */
/* XXX 14 bits hole, try to pack */
/* Bitfield WARNING: DWARF size=4, real size=2 */
short int d; /* 66 2 */
The fixup is done prior to reorganizing the fields.
Now an example of this code in action:
[acme@filo examples]$ cat swiss_cheese.c
<SNIP>
struct cheese {
char id;
short number;
char name[52];
int a:1;
int b;
int bitfield1:1;
int bitfield2:1;
short d;
short e;
short last:5;
};
<SNIP>
[acme@filo examples]$
Lets look at the layout:
[acme@filo examples]$ pahole swiss_cheese cheese
/* <11b> /home/acme/git/pahole/examples/swiss_cheese.c:3 */
struct cheese {
char id; /* 0 1 */
/* XXX 1 byte hole, try to pack */
short int number; /* 2 2 */
char name[52]; /* 4 52 */
int a:1; /* 56 4 */
/* XXX 31 bits hole, try to pack */
int b; /* 60 4 */
/* --- cacheline 1 boundary (64 bytes) --- */
int bitfield1:1; /* 64 4 */
int bitfield2:1; /* 64 4 */
/* XXX 14 bits hole, try to pack */
/* Bitfield WARNING: DWARF size=4, real size=2 */
short int d; /* 66 2 */
short int e; /* 68 2 */
short int last:5; /* 70 2 */
}; /* size: 72, cachelines: 2 */
/* sum members: 71, holes: 1, sum holes: 1 */
/* bit holes: 2, sum bit holes: 45 bits */
/* bit_padding: 11 bits */
/* last cacheline: 8 bytes */
[acme@filo examples]$
Full of holes, has bit padding and uses more than one 64 bytes cacheline.
Now lets ask pahole to reorganize it:
[acme@filo examples]$ pahole --reorganize --verbose swiss_cheese cheese
/* Demoting bitfield ('a' ... 'a') from 'int' to 'unsigned char' */
/* Demoting bitfield ('bitfield1' ... 'bitfield2') from 'short unsigned int' to 'unsigned char' */
/* Demoting bitfield ('last') from 'short int' to 'unsigned char' */
/* Moving 'bitfield2:1' from after 'bitfield1' to after 'a:1' */
/* Moving 'bitfield1:1' from after 'b' to after 'bitfield2:1' */
/* Moving 'last:5' from after 'e' to after 'bitfield1:1' */
/* Moving bitfield('a' ... 'last') from after 'name' to after 'id' */
/* Moving 'e' from after 'd' to after 'b' */
/* <11b> /home/acme/git/pahole/examples/swiss_cheese.c:3 */
struct cheese {
char id; /* 0 1 */
unsigned char a:1; /* 1 1 */
unsigned char bitfield2:1; /* 1 1 */
unsigned char bitfield1:1; /* 1 1 */
unsigned char last:5; /* 1 1 */
short int number; /* 2 2 */
char name[52]; /* 4 52 */
int b; /* 56 4 */
short int e; /* 60 2 */
short int d; /* 62 2 */
/* --- cacheline 1 boundary (64 bytes) --- */
}; /* size: 64, cachelines: 1 */
/* saved 8 bytes and 1 cacheline! */
[acme@filo examples]$
Instant karma, it gets completely packed, and look ma, no
__attribute__((packed)) :-)
With this struct task_struct in the linux kernel is shrunk by 12 bytes, there
is more 4 bytes to save with another technique that involves not combining
holes, but using the last single hole to fill it with members at the tail of
the struct.
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This allows us to save 4 more bytes in struct task_struct, for instance, now we
need to combine whole bitfields with other fields if some bitfield has a size
less than sizeof(void *) and there is a suitable hole.
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Using export CFLAGS="-Wall -Wfatal-errors -Wformat=2 -Wsequence-point -Wextra
-Wno-parentheses -g", suggested by Davi Arnault, amazing how cruft piles up
when one is not looking ;)
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Some are just typedefs, others are inside structs and in some cases its
useful to see the statistics for them, so add two new cmd line options:
-a, --anon_include include anonymous classes\
-A, --nested_anon_include include nested (inside other structs) anonymous classes
Commiter note: I've reworked several aspects of the patch, but mostly to
give better names for the new find_first_typedef_of_type function, adding
a clarifying comment and introducing --nested_anon_include so that we
can select just the typedef'ed anonymous structs.
Damn, I had commited just dwarves.c, here is the dwarves.h and pahole.c bits.
Signed-off-by: Davi Arnaut <davi@haxent.com.br>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
To show how many non inline functions receive as a parameter each of the structs
in a project, example:
[acme@newtoy ctracer_example]$ pahole --nr_methods vmlinux | sort -k2 -nr | head -5
file: 526
inode: 479
sk_buff: 386
sock: 383
dentry: 295
[acme@newtoy ctracer_example]$
Signed-off-by: Arnaldo Carvalho de Melo <acme@mandriva.com>
For now it just affects showing differences in definitions of structs with the
same name found in different object files, that could be a real problem but
could as well be just a namespace colision not affecting the project's build
process as they were be local to specific objects.
Signed-off-by: Arnaldo Carvalho de Melo <acme@mandriva.com>
Out of struct typedef_tag, that now becomes the superclass of struct class, and
that also will be for struct enumeration, struct union_type and then finally
for struct struct_type, when struct class finally dies.
Signed-off-by: Arnaldo Carvalho de Melo <acme@mandriva.com>
By having its own class, struct typedef_tag.
As it, as structs, unions and enums have a common part, the node and visited
fields, required when emitting its definitions there is an opportunity for
consolidation, that will be explored when adding the specific classes for
DW_TAG_enumeration & DW_TAG_union.
Signed-off-by: Arnaldo Carvalho de Melo <acme@mandriva.com>
Almost mirroring the DWARF on-disk linkage on memory, more to come before
getting over these simplification refactorings.
Signed-off-by: Arnaldo Carvalho de Melo <acme@mandriva.com>