3 # Generate Valgrind's module dependence graph in 'dot' format.
5 # You can run it from anywhere(?) in a Valgrind source tree, but the two
6 # most interesting places are:
7 # - the root, to get the entire module dependence graph.
8 # - coregrind/, to get the core's module dependence graph.
10 # It sends a dot-format graph to stdout. You can use dot (part of the
11 # "GraphViz" package) to generated a PostScript graphs like this:
13 # dot -Tps foo.dot -o foo.ps
16 # - It's not a proper parser. If you have a #include that is commented out,
17 # it will think it's there. We see that particularly in m_demangle.
18 # - It only looks in C files, not in header files, so it will miss any
19 # extra dependencies this causes. Fortunately we don't have many like
25 #----------------------------------------------------------------------------
27 #----------------------------------------------------------------------------
29 # The dependence graph is a set of src-->dst pairs, stored in a double-hash:
31 # hash(src, hash(dst, dst_realname))
33 # Each of 'src' and 'dst' are node names. 'src' is always a module name,
34 # eg. 'm_main' or 'memcheck'. The destination is sometimes a module name,
35 # and sometimes a header file. Because Dot can't handle certain characters
36 # in its node names, when 'dst' represents a header file it's a transformed
37 # version of the header file name, eg. 'INT_memcheck_h' or 'EXT_sys_mman_h'.
38 # The 'dst_realname' holds the original name, eg. '"memcheck.h"' or
39 # "<sys/mman.h>". We use 'dst' for the node name in the graph, but label it
40 # with 'dst_realname' and that's what gets seen. (Although we use "" for
41 # 'dst_realname' when it would be the same as 'dst'.)
44 # Directories to skip. These are the defaults, they can be augmented
45 # using command-line options.
46 my %dirs_to_skip = ( auxprogs
=> 1, hp2ps
=> 1, tests
=> 1 );
48 # Command-line variables -- things we should show. Default is yes.
57 my @tools = ( "cachegrind", "helgrind",
58 "lackey", "massif", "memcheck", "none" );
61 usage: gen-mdg [options]
64 --headers=no|yes show headers, ie. show module-to-module deps only
65 --hide=<a>,<b>,... hide module(s) named <a>, <b>, ...
69 #----------------------------------------------------------------------------
71 #----------------------------------------------------------------------------
73 sub process_cmd_line
()
78 if ($arg =~ /^--headers=(yes|no)$/) {
79 $show_headers = 1 if ($1 eq "yes");
80 $show_headers = 0 if ($1 eq "no");
83 } elsif ($arg =~ /^--hide=(.*)$/) {
84 my @hiders = split(/,/, $1);
85 foreach my $h (@hiders) {
95 foreach my $tool (@tools) {
96 $dirs_to_skip{$tool} = 1;
101 # Convert a header filename into a node name acceptable by dot.
102 sub clean_nodename
($)
105 $s =~ s/"([^"]+)"/INT_$1/; # "foo.h" --> foo.h
106 $s =~ s/<([^>]+)>/EXT_$1/; # <foo.h> --> foo.h
107 $s =~ s/\./_/g; # foo.h --> foo_h
108 $s =~ s/-/_/g; # foo-bar.h --> foo_bar_h
109 $s =~ s/\//_
/g; # bar/foo_h
--> bar_foo_h
113 # Convert a header filename into a node label acceptable by dot.
114 sub clean_nodelabel
($)
117 $s =~ s/"/\\"/g; # "foo.h" --> \"foo.h\"
121 # $module is the module to which the C/asm file $f belongs.
122 sub scan_C_or_asm_file
($$)
124 my ($module, $f) = @_;
126 # Skip if this is a module we want to hide
127 if ($hide{$module}) {
131 # Get any existing dependencies for this module, initialise if none
132 my $module_deps = $deps->{$module};
133 if (not defined $module_deps) {
137 # Scan the C/asm file
138 open(CFILE
, "< $f") || die "File $f not openable\n";
139 while (my $line = <CFILE
>) {
140 if ($line =~ /#include\s+(("|<)[^">]+("|>))/) {
141 # Right! We've found a #include line.
142 my $include_string = $1;
145 if ($include_string =~ /"pub_(core|tool)_([A-Za-z]+).h"/) {
146 # If #include string is "pub_core_foo.h" or "pub_tool_foo.h",
147 # the target module is "m_foo".
149 # Nb: assuming the "foo" part does not contains underscores!
153 # But don't show hidden modules
154 if ($hide{$target}) {
158 } elsif ($show_headers) {
159 # Otherwise use the #include string as-is for the target.
160 # Note that "#include pub_core_foo_asm.h" falls into this
161 # category. We don't consider that part of the m_foo module
162 # because the *_asm.h only define some constants.
163 $target = clean_nodename
($include_string);
164 $realname = clean_nodelabel
($include_string);
167 # Don't record anything
172 # Maybe record dependency (unless it's circular)
173 if ($target ne "" and $target ne $module) {
174 $module_deps->{$target} = $realname;
180 # Store the updated dependencies.
181 $deps->{$module} = $module_deps;
184 sub process_dir
($); # forward declarations required because of recursion
189 # Go through each file/dir in the directory.
191 foreach my $f (@fs) {
193 # Directory -- recursively process unless we want to skip it.
194 if (not exists $dirs_to_skip{$f}) {
201 if ($f =~ /\w+\.[cS]$/) {
202 # If this is a .c/.S file in coregrind/, it's a module in its
203 # own right, eg. coregrind/m_redir.c --> module name of
206 # Otherwise, it belongs to the module whose name is that of
207 # the parent directory, eg. coregrind/m_debuginfo/symtab.c
208 # --> module name of "m_debuginfo".
210 if ($parentd eq "coregrind") {
212 $module =~ s/(\w+).[cS]$/$1/; # foo.c --> foo
216 # Now the module/f pair is either:
217 # - like this: (m_redir, m_redir.c)
218 # - or like this: (m_debuginfo, symtab.c)
219 scan_C_or_asm_file
($module, $f);
223 die "$f is not a dir nor a file\n";
230 my %printed_realnames;
232 print("digraph G {\n");
233 while (my ($src, $dst_hash) = each %$deps) {
234 while (my ($dst, $dst_realname) = each %$dst_hash) {
236 # If the dstnode has a realname, print just the dstnode with that
237 # realname, and record it in %printed_realnames so we don't print
239 if ($dst_realname ne "") {
240 if (not defined $printed_realnames{$dst}) {
241 print(" $dst [label=\"$dst_realname\"]\n");
242 $printed_realnames{$dst} = 1;
246 # Print the src-->dst edge.
247 print(" $src -> $dst\n");
253 #----------------------------------------------------------------------------
255 #----------------------------------------------------------------------------
258 my $start_dir = `basename \`pwd\
``;
259 chop($start_dir); # trim newline
260 process_dir
($start_dir);