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