blob: 030d03f813cecbe5a25b70f392be535adfc8e289 [file] [log] [blame]
njna6d93912005-06-20 01:02:53 +00001#! /usr/bin/perl
2#
3# Generate Valgrind's module dependence graph in 'dot' format.
4#
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.
9#
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:
12#
13# dot -Tps foo.dot -o foo.ps
14#
15# Caveats:
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
20# that.
21
22use warnings;
23use strict;
24
25#----------------------------------------------------------------------------
26# Global variables
27#----------------------------------------------------------------------------
28
29# The dependence graph is a set of src-->dst pairs, stored in a double-hash:
30#
31# hash(src, hash(dst, dst_realname))
32#
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'.)
42my $deps = {};
43
44# Directories to skip. These are the defaults, they can be augmented
45# using command-line options.
46my %dirs_to_skip = ( auxprogs => 1, hp2ps => 1, tests => 1 );
47
48# Command-line variables -- things we should show. Default is yes.
49my $show_tools = 1;
50my $show_headers = 1;
51my $show_libc = 1;
52
53# Modules to hide.
54my %hide;
55
56# List of all tools.
57my @tools = ( "addrcheck", "cachegrind", "corecheck", "helgrind",
58 "lackey", "massif", "memcheck", "none" );
59
60my $usage = <<END
61usage: gen-mdg [options]
62
63 options:
64 --headers=no|yes show headers, ie. show module-to-module deps only
65 --libc=no|yes show m_libc* modules
66 --hide=<module> hide module named <module>
67END
68;
69
70#----------------------------------------------------------------------------
71# Subroutines
72#----------------------------------------------------------------------------
73
74sub process_cmd_line()
75{
76 for my $arg (@ARGV) {
77
78 # --headers=yes|no
79 if ($arg =~ /^--headers=(yes|no)$/) {
80 $show_headers = 1 if ($1 eq "yes");
81 $show_headers = 0 if ($1 eq "no");
82
83 # --libc=yes|no
84 } elsif ($arg =~ /^--libc=(yes|no)$/) {
85 $show_libc = 1 if ($1 eq "yes");
86 $show_libc = 0 if ($1 eq "no");
87
88 # --hide=<module>
89 } elsif ($arg =~ /^--hide=(.*)$/) {
90 $hide{$1} = 1;
91
92 } else {
93 die $usage;
94 }
95 }
96
97 if (!$show_tools) {
98 foreach my $tool (@tools) {
99 $dirs_to_skip{$tool} = 1;
100 }
101 }
102}
103
104# Convert a header filename into a node name acceptable by dot.
105sub clean_nodename($)
106{
107 my ($s) = @_;
108 $s =~ s/"([^"]+)"/INT_$1/; # "foo.h" --> foo.h
109 $s =~ s/<([^>]+)>/EXT_$1/; # <foo.h> --> foo.h
110 $s =~ s/\./_/g; # foo.h --> foo_h
111 $s =~ s/-/_/g; # foo-bar.h --> foo_bar_h
112 $s =~ s/\//_/g; # bar/foo_h --> bar_foo_h
113 return $s;
114}
115
116# Convert a header filename into a node label acceptable by dot.
117sub clean_nodelabel($)
118{
119 my ($s) = @_;
120 $s =~ s/"/\\"/g; # "foo.h" --> \"foo.h\"
121 return $s;
122}
123
124# $module is the module to which the C file $f belongs.
125sub scan_C_file($$)
126{
127 my ($module, $f) = @_;
128
129 # Skip if this is a module we want to hide
130 if ($hide{$module}) {
131 return;
132 }
133
134 # Skip if this is a m_libc*.c file and we aren't showing them.
135 if (not $show_libc and $f =~ /^m_libc\w+.c/) {
136 return;
137 }
138
139 # Get any existing dependencies for this module, initialise if none
140 my $module_deps = $deps->{$module};
141 if (not defined $module_deps) {
142 $module_deps = {};
143 }
144
145 # Scan the C file
146 open(CFILE, "< $f") || die "File $f not openable\n";
147 while (my $line = <CFILE>) {
148 if ($line =~ /#include\s+(("|<)[^">]+("|>))/) {
149 # Right! We've found a #include line.
150 my $include_string = $1;
151 my $target;
152 my $realname;
153 if ($include_string =~ /"pub_(core|tool)_([\w]+).h"/) {
154 # If #include string is "pub_core_foo.h" or "pub_tool_foo.h",
155 # the target module is m_foo.
156 $target = "m_$2";
157 $realname = "";
158
159 # But don't show m_libc* dst modules if asked not to.
160 if (not $show_libc and $target =~ /m_libc/) {
161 $target = "";
162 }
163
164 # And don't show hidden modules
165 if ($hide{$target}) {
166 $target = "";
167 }
168
169 } elsif ($show_headers) {
170 # Otherwise use the #include string as-is for the target.
171 $target = clean_nodename($include_string);
172 $realname = clean_nodelabel($include_string);
173
174 } else {
175 # Don't record anything
176 $target = "";
177 $realname = "";
178 }
179
180 # Maybe record dependency (unless it's circular)
181 if ($target ne "" and $target ne $module) {
182 $module_deps->{$target} = $realname;
183 }
184 }
185 }
186 close(CFILE);
187
188 # Store the updated dependencies.
189 $deps->{$module} = $module_deps;
190}
191
192sub process_dir($); # forward declarations required because of recursion
193sub process_dir($)
194{
195 my ($parentd) = @_;
196
197 # Go through each file/dir in the directory.
198 my @fs = <*>;
199 foreach my $f (@fs) {
200 if (-d $f) {
201 # Directory -- recursively process unless we want to skip it.
202 if (not exists $dirs_to_skip{$f}) {
203 chdir $f or die;
204 process_dir($f);
205 chdir ".." or die;
206 }
207
208 } elsif (-f $f) {
209 if ($f =~ /\w+\.c$/) {
210 # If this is a .c file in coregrind/, it's a module in its
211 # own right, eg. coregrind/m_redir.c --> module name of
212 # "m_redir".
213 #
214 # Otherwise, it belongs to the module whose name is that of
215 # the parent directory, eg. coregrind/m_debuginfo/symtab.c
216 # --> module name of "m_debuginfo".
217 my $module;
218 if ($parentd eq "coregrind") {
219 $module = $f;
220 $module =~ s/(\w+).c/$1/; # foo.c --> foo
221 } else {
222 $module = $parentd;
223 }
224 # Now the module/f pair is either:
225 # - like this: (m_redir, m_redir.c)
226 # - or like this: (m_debuginfo, symtab.c)
227 scan_C_file($module, $f);
228 }
229
230 } else {
231 die "$f is not a dir nor a file\n";
232 }
233 }
234}
235
236sub print_graph()
237{
238 my %printed_realnames;
239
240 print("digraph G {\n");
241 while (my ($src, $dst_hash) = each %$deps) {
242 while (my ($dst, $dst_realname) = each %$dst_hash) {
243
244 # If the dstnode has a realname, print just the dstnode with that
245 # realname, and record it in %printed_realnames so we don't print
246 # it again.
247 if ($dst_realname ne "") {
248 if (not defined $printed_realnames{$dst}) {
249 print(" $dst [label=\"$dst_realname\"]\n");
250 $printed_realnames{$dst} = 1;
251 }
252 }
253
254 # Print the src-->dst edge.
255 print(" $src -> $dst\n");
256 }
257 }
258 print("}\n");
259}
260
261#----------------------------------------------------------------------------
262# main
263#----------------------------------------------------------------------------
264process_cmd_line();
265
266my $start_dir = `basename \`pwd\``;
267chop($start_dir); # trim newline
268process_dir($start_dir);
269
270print_graph();
271
272