forked from koknat/callGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallGraph
executable file
·1519 lines (1434 loc) · 66.2 KB
/
callGraph
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
# #!/home/utils/perl-5.8.8/bin/perl
use utf8;
use open qw(:std :encoding(utf8));
use warnings;
use strict;
use Data::Dumper;
use File::Basename qw(basename dirname);
use File::Find;
use File::Temp qw(tempdir);
use Getopt::Long;
use POSIX qw( strftime );
sub say { print @_, "\n" }
$Data::Dumper::Indent = 1;
$Data::Dumper::Sortkeys = 1;
my $scriptName = 'callGraph';
sub usage {
my $usage = "\n
'$scriptName' by Chris Koknat https://github.com/koknat/callGraph
Purpose:
$scriptName is a multi-language tool which statically parses source code for function definitions and calls.
It generates a call graph image and displays it on screen.
The parser was designed for Perl/Python/TCL, and has been extended for other languages, such as
awk, bash, basic, dart, fortran, go, javascript, julia, lua, kotlin, matlab, pascal, php, R, raku,
ruby, rust, scala, swift, and typescript.
C/C++/Java are not supported, since their complicated syntax requires heavy machinery.
Usage:
$scriptName <files> <options>
If a script calls helper modules, and you want the call graph to display the modules' functions,
list the modules explicitly on the command line:
$scriptName script.pl path/moduleA.pm path/moduleB.pm
Options:
-language <lang> By default, filename extensions are parsed for .pl .pm .tcl .py, etc.
If those are not found, the first line of the script (#! shebang) is inspected.
If neither of those give clues, use this option to specify 'pl', 'tcl', 'py', etc
This option is required if a directory is scanned
-start <function> Specify function(s) as starting point instead of the main code.
These are displayed in green.
This is useful when parsing a large script, as the generated graph can be huge.
In addition, the calls leading to this function are charted.
Functions which are not reachable from one of the starting points
are not charted.
-start __MAIN__ can be very useful when multiple source files
are specified on the command line
The filename can be included as well:
-start <file>:<function>
-ignore <regex> Specify function(s) to ignore.
This is useful when pruning the output of a large graph.
In particular, use it to remove logging or other helper functions which are
called by many functions, and only clutter up the graph.
To ignore multiple functions, use this regex format:
-ignore '(abc|xyz)'
-output <filename> Specify an output filename
By default, the .png file is named according to the first filename.
If a filename ending in .dot is given,
only the intermediate .dot file is created.
If a filename ending in .svg is given, svg format is used
If a filename ending in .pdf is given, pdf format is used
-noShow By default, the .png file is displayed. This option prevents that behavior.
-fullPath By default, the script strips off the path name of the input file(s).
This option prevents that behavior.
-writeSubsetCode <file> Create an output source code file which includes only the functions
included in the graph.
This can be useful when trying to comprehend a large legacy code.
-jsnOut <file>
-ymlOut <file> Create an output JSON/YAML file which describes the following for each function:
* which functions call it
* which functions it calls
This can be useful to create your own automation or custom formatting
-jsnIn <file>
-ymlIn <file> Parse a JSON/YAML file instead of parsing source files
The format is the same as the one created by -jsnOut or -ymlOut
-verbose Provides 2 additional functionalities:
1) Displays the external scripts referenced within each function
2) For Perl/TCL, attempts to list the global variables
used in each function call in the graph.
Global variables are arguably not the best design paradigm,
but they are found extensively in real-world legacy scripts.
Perl:
'my' variables will affect this determination (use strict).
Does not distinguish between \$var, \@var and \%var.
TCL:
Variables declared as 'global' but not used, are marked with a '*'
Usage examples:
$scriptName example.py
$scriptName example.pl example_helper_lib.pm
$scriptName <directory> -language 'go'
Algorithm:
$scriptName uses a simple line-by-line algorithm, using regexes to find function definitions and calls.
Function definitions can be detected easily, since they start with identifiers such as:
'sub', 'def', 'proc', 'function', 'func', 'fun', or 'fn'
Function definitions end with '}' or 'end' at the same nesting level as the definition.
Function calls are a bit more tricky, since built-in function calls look exactly like user function calls.
To solve this, the algorithm first assumes that anything matching 'word(...)' is a function call,
and then discards any calls which do not have corresponding definitions.
For example, Perl:
sub funcA {
...
if (\$x) {
print(\$y);
funcB(\$y);
}
...
}
sub funcB {
...
}
Since this is not a true parser, the formatting must be consistent so that nesting can be determined.
If your script does not follow this rule, consider running it through a linter first.
Also, don't expect miracles such as parsing dynamic function calls.
Caveats aside, it seems to work well on garden-variety scripts spanning tens of thousands of lines,
and has helped me unravel large pieces of legacy code to implement urgent bug fixes.
";
$usage .= "
Acknowledgements:
This code borrows core functionality from https://github.com/cobber/perl_call_graph
Requirements:
GraphViz and the Perl GraphViz library must be installed:
sudo apt install graphviz
sudo apt install make
sudo apt install cpanminus
sudo cpanm install GraphViz
On macOS:
brew install graphviz
brew install cpanminus
sudo cpanm GraphViz
\n" if !-d '/home/ckoknat';
say "\n$usage\n";
exit 0;
}
# Changes from original code:
# automatically convert dot to png using system command 'dot -Tpng', and displays png to screen using 'eog' or similar
# do not print the filename in the graph bubbles unless multiple files are given
# support languages besides Perl
# handles '\' line continuation characters
# handle Perl '__END__' syntax
# verbose mode creates report with all globals used in each function in the graph
# option -writeSubsetCode <file> to create an output source file containing only the procs used in the graph
# Additional caveats:
# it is assumed that function names are unique across files
# Notes:
# For C/C++/Java, consider using Doxygen or kcachegrind or Intel Single Event API or Eclipse instead.
# For Perl, if you need something more sophisticated, you can run the dynamic profiler NYTProf:
# <path_to_perl>/bin/perl -d:NYTProf <script>
# <path_to_perl>/bin/nytprofcg
# kcachegrind nytprof.callgrind
# Python and PHP can also use a similar toolchain
# Dynamic generation will only include paths which are actually run, and may take much longer to run than static generation
# If the resulting png size is too big, use ImageMagick:
# convert foo.png -resize 1000 foo-small.png
# Goals:
# Ease of use: user runs one command to parse file, create png, display png
# Works on any Linux system (including Apple), as long as GraphViz is installed
# Monolithic code for ease of sharing
my %opt;
my $d;
Getopt::Long::GetOptions(
\%opt,
#'cluster!',
'help|?',
'output=s',
'start=s',
'ignore=s',
'fullPath',
'verbose',
'language=s',
'regression',
'noShow',
'obfuscate',
'writeSubsetCode=s',
'jsnIn=s',
'jsnOut=s',
'ymlIn=s',
'ymlOut=s',
'writeFunctions',
'd' => sub { $d = 1 },
) or usage();
usage() if $opt{help} or ( !@ARGV and !defined $opt{jsnIn} and !defined $opt{ymlIn} );
# Debugger from CPAN: "sudo cpan" "install Debug::Statements"
sub D { say "Debug::Statements has been disabled" }
sub d { say "Debug::Statements has been disabled" if $d }
sub ls { }
#use lib "/home/ate/scripts/regression";
#use Debug::Statements ":all"; # d ''
d '%opt';
eval 'use GraphViz ()';
if ($@) {
say "\n$@";
say "ERROR: Install GraphViz and the CPAN GraphViz module to create the call graph";
say " sudo apt install graphviz";
say " and";
say " sudo apt install make";
say " and";
say " sudo cpan install GraphViz";
exit 1;
}
my ( @files, $tmpdir, $output );
if ( defined $opt{jsnIn} ) {
if ( defined $opt{output} ) {
$output = $opt{output};
} else {
( $output = $opt{jsnIn} ) =~ s/\.jso?n$//;
}
} elsif ( defined $opt{ymlIn} ) {
if ( defined $opt{output} ) {
$output = $opt{output};
} else {
( $output = $opt{ymlIn} ) =~ s/\.ya?ml$//;
}
} else {
if ( -d $ARGV[0] ) {
die "\nERROR: Must specify -language <language>\n" if !defined $opt{language};
my $language = $opt{language};
$language = 'pl|.pm' if $language eq 'pl';
for my $dir (@ARGV) {
if ( -d $dir ) {
find(
sub {
return if -d $File::Find::name;
return unless $File::Find::name =~ /(\.$language)$/;
push @files, $File::Find::name;
},
$dir
);
if ( !@files ) {
die "\nERROR: Did not find any $language files in $dir/\n";
}
} else {
push @files, $dir;
}
}
} else {
@files = @ARGV;
}
d '@files';
for my $file (@ARGV) {
die "\nERROR: $file not found!\n" if !-r $file;
}
if ( defined $opt{output} ) {
$output = $opt{output};
if ( -f $output ) {
die "\nERROR: file $output is not writeable\n" if !-w $output;
} else {
die "\nERROR: '$output' is not writeable\n" if !-w dirname($output);
}
} else {
if ( -w '.' ) {
#$output = './' . basename( $ARGV[0] );
$tmpdir = tempdir( '/tmp/call_graph_XXXX', CLEANUP => 0 );
$output = "$tmpdir/" . basename( $ARGV[0] );
} else {
$tmpdir = tempdir( '/tmp/call_graph_XXXX', CLEANUP => 0 );
$output = "$tmpdir/" . basename( $ARGV[0] );
}
}
if ( -d $output ) {
# TODO test -output <directory>
# If a directory name is given in -output (or no -output), create a name based on timestamp
my $base_name = 'call_graph_';
if ( $opt{start} ) {
$base_name .= $opt{start};
$base_name =~ s/\\.//g;
$base_name =~ s/[^-\w]//g;
} else {
$base_name .= strftime( "%Y%m%d-%H%M%S", localtime() );
}
$output =~ s{/*$}{/$base_name};
}
}
( my $dot = $output ) =~ s/\.(dot|png|svg|pdf)$//;
$dot .= '.dot';
my $call_graph;
if ( $opt{jsnIn} ) {
eval 'use JSON::XS ()';
if ($@) {
say "\n$@\n";
say "ERROR: Install JSON::XS to use option -jsnIn";
say " sudo cpan install JSON::XS";
exit 1;
} else {
say "Reading file $opt{jsnIn}";
my $json;
{
local $/;
open my $fh, "<", $opt{jsnIn};
$json = <$fh>;
close $fh;
}
$call_graph = JSON::XS::decode_json($json);
my ( $calls, $called_by );
for my $sub ( keys %{$call_graph} ) {
# $call_graph->{"$caller_file:$caller_sub"}{calls}{"$caller_file:$referenced_sub"}++;
# $call_graph->{"$caller_file:$referenced_sub"}{called_by}{"$caller_file:$caller_sub"}++;
for my $item ( keys %{ $call_graph->{$sub} } ) {
$calls++ if $item eq 'calls';
$called_by++ if $item eq 'called_by';
}
}
d '$call_graph $calls $called_by';
if ( !$calls ) {
die "\nERROR: input json does not contain any 'calls' items!\n The format should be similar to the one created by -jsnOut <file>\n";
}
if ( !$called_by ) {
die "\nERROR: input json does not contain any 'called_by' items!\n The format should be similar to the one created by -jsnOut <file>\n";
}
}
goto INTERMEDIATE_FORMAT;
} elsif ( $opt{ymlIn} ) {
eval 'use YAML::XS ()';
if ($@) {
say "\n$@\n";
say "ERROR: Install YAML::XS to use option -ymlIn";
say " sudo cpan install YAML::XS";
exit 1;
} else {
say "Reading file $opt{ymlIn}";
$call_graph = YAML::XS::LoadFile( $opt{ymlIn} );
# Sanity check
my ( $calls, $called_by );
for my $sub ( keys %{$call_graph} ) {
# $call_graph->{"$caller_file:$caller_sub"}{calls}{"$caller_file:$referenced_sub"}++;
# $call_graph->{"$caller_file:$referenced_sub"}{called_by}{"$caller_file:$caller_sub"}++;
for my $item ( keys %{ $call_graph->{$sub} } ) {
$calls++ if $item eq 'calls';
$called_by++ if $item eq 'called_by';
}
}
d '$call_graph $calls $called_by';
if ( !$calls ) {
die "\nERROR: input yml does not contain any 'calls' items!\n The format should be similar to the one created by -ymlOut <file>\n";
}
if ( !$called_by ) {
die "\nERROR: input yml does not contain any 'called_by' items!\n The format should be similar to the one created by -ymlOut <file>\n";
}
}
goto INTERMEDIATE_FORMAT;
} else {
# do nothing
}
# Determine language
die "\nERROR: No input files specified!\n" unless @ARGV;
my $language = defined $opt{language} ? $opt{language} : getScriptType( file => $files[0], scriptsOnly => 1 );
d '$language';
die "\nERROR: language could not be determined. Use option -language <language>\n" if !defined $language;
my $main = '__MAIN__';
sub defineSyntax {
my $language = shift;
# Define the regular expressions which detect function definitions & calls
my $languageSyntax = {
# http://rigaux.org/language-study/syntax-across-languages.html
# To add support for another language, add to the functionDefinition dictionary, and add the extension to the regex in getScriptType
# Note that sometimes functions span lines, such as:
# function foo (a,
# b, c,
# d)
# Since the parser uses a simple line-by-line algorithm, it's helpful to only require the first parenthesis
functionDefinition => {
awk => '(\s*)(function)\s+(\w+)\s*\(',
bas => '(\s*)(?:public\s+)?(function|sub)\s+(\w+)\s*\(',
dart => '(\s*)((?<=main)|void|bool|int|double|String|List|Map|Runes|Symbol)\s+(\w+)\s*\(', # Because no type is required, encountering false positive for built-in functions like 'if(...){}', so requiring that 'void' is used for any function besides 'main'
for => '(\s*)(?:character\s+|complex\s+|elemental\s+|integer\s+|logical\s+|module\s+|pure\s+|real\s+|recursive\s+)*(function|program|subroutine)\s+(\w+)\s*\(', # fortran .fypp is problematic because of preprocessor
go => '(\s*)(func)\s+(?:\(.*?\))?\s*(\w+)', # Non-greedy because of func (f *Field) Alive(x, y int) bool {}
jl => '(\s*)(?:@inline\s+)?(function)\s+([\w\.\!\$]+)\s*[^\(]*\(',
js => '(\s*)(?:async\s+)?(function)\s+(\w+)\s*\(',
kt => '(\s*)(?:internal\s+|private\s+|protected\s+|public\s+)?(fun)\s+(\w+)\s*\(',
lua => '(\s*)(?:local\s+)?(function)\s+([\w\.]+)\s*\(',
m => '(\s*)(function)[^=]*=\s*(\w+)\s*\(', # matlab
pas => '(\s*)(function|procedure)\s+(\w+)\s*\(',
php => '(\s*)(?:abstract\s+|final\s+|private\s+|protected\s+|public\s+)?(?:static\s+)?(function)\s+(\w+)\s*\(',
pl => '(\s*)(sub)\s+(\w+)',
py => '(\s*)(def)\s+(\w+)\(',
r => '(\s*)()(\w+)\s+(?:<-|=)\s+function\s*\(',
rb => '(\s*)(def)\s+(\w+)',
rs => '(\s*)(?:pub\s+|async\s+)?(fn)\s+(\w+)',
sc => '(\s*)(?:private\s+|protected\s+)?(def)\s+(\w+)',
sh => '(\s*)(function\b|)\s*(\w+)\(?\)?\s*{',
swift => '(\s*)(?:fileprivate\s+|internal\s+|open\s+|public\s+)?(?:static\s+)?(func)\s+(\w+)',
tcl => '(\s*)(proc)\s+([\w:]+)',
ts => '(\s*)(function)\s+(\w+)\s*\(',
# function bit [7:0] sum;
# function byte mul (...);
# task automatic display()
# task sum
v => '(\s*)(?:virtual\s+)?(function|task)\s+(?:[^(]+)?\s+(\w+)\s*(?:$|;|\()',
# C, C++, and Java syntaxes are not well suited for regexes
#
# Since java function does not begin with 'function' or similar, require that the opening curly brace is on the same line
# spaces public static void foo
#java => '^()((?:(?:public|private|protected|static|final|native|synchronized|abstract|transient)+\s+)*(?:[$_\w<>\[\]\s]+))*?\s+(\S+)\s*\(.*\)\s*\{\s*$',
java => '(\s*)(?=public|private|protected|static|final|native|synchronized|abstract|transient)([^(]+)\s+(\S+)\s*\(',
# cpp regex will be somewhat similar to java, but a lot more complicated. I'm not pursuing this
# To create a cpp call graph:
# clang++ -S -emit-llvm main.cpp -o - | opt -analyze -dot-callgraph
# dot -Tpng -ocallgraph.png callgraph.dot
#cpp => '^(\s*)((?:(?:public|private|protected|static|final|native|synchronized|abstract|transient)+\s+)*(?:[$_\w<>\[\]\s]+))*?\s+(\S+)\s*\(.*\)\s*(\{)\s*$',
cpp => '(\s*)(?=.*(?:void|bool|int|short|long|double|char|size_t|string|vector|unsigned|istream|ostream))([^(]+)\s+(\S+)\s*\(',
# Attempting to support simple C syntax
# int main(int argc, char *argv[])
# int CTclParser::SplitList( int *line, char *list, int *argcPtr, char ***argvPtr )
#c => '(\s*)([^(]+)\s+(\S+)\s*\(.*\)',
c => '(\s*)(?=.*(?:void|bool|int|short|long|double|char|size_t))([^(]+)\s+(\S+)\s*\(',
},
functionEnd => {
awk => '\s*}',
bas => '\s*end (function|sub)',
c => '\s*}',
cpp => '\s*}',
dart => '\s*}',
for => '\s*(function|program|subroutine) end',
go => '\s*}',
java => '\s*}',
jl => '\s*end',
js => '\s*}',
kt => '\s*}',
lua => '\s*end',
m => '\s*end',
pas => '\s*end',
php => '\s*}',
pl => '\s*}',
py => '\s*\S',
r => '\s*}',
rb => '\s*end',
rs => '\s*}',
sc => '\s*}',
sh => '\s*}',
swift => '\s*}',
tcl => '\s*}',
ts => '\s*}',
v => '\s*(endfunction|endtask)',
},
functionCall => {
awk => '(\w+)\s*\(',
bas => '(?:call\s+)?(\w+)\s*\(',
c => '(\w+)\s*\(',
cpp => '(\w+)\s*\(',
dart => '(\w+)\s*\(',
for => '(\w+)\s*\(',
go => '(\w+)\s*\(',
java => '(\w+)\s*\(',
jl => '([\w\.\!\$]+)\s*\(',
js => '(\w+)\s*\(',
kt => '(\w+)\s*\(',
lua => '([\w\.]+)\s*\(',
m => '(\w+)\s*\(',
pas => '(\w+)\s*\(',
php => '(?<![\$])(\w+)\s*\(', # (?< ...) is a negative lookahead assertion
pl => '(?<![\$\%\@])(\w+)\s*\(', # while it's possible to call functions without the '()', this is not commonly done
py => '(\w+)\s*\(',
r => '(\w+)\s*\(',
rb => '(\w+)\s*',
rs => '(\w+)\s*\(',
sc => '(\w+)',
sh => '(\w+)',
swift => '(\w+)\s*\(',
tcl => '(?<![\$])(\w+)\s*',
ts => '(\w+)\s*\(',
v => '(\w+)',
},
comment => {
# Comments are removed
awk => '#',
bas => '^REM\s+',
c => '//', # doesn't handle multiline comments /* */
cpp => '//', # doesn't handle multiline comments /* */
dart => '//', # doesn't handle multiline comments /* */
for => '!',
go => '//',
java => '//', # doesn't handle multiline comments /* */
js => '//',
kt => '//',
lua => '--',
jl => '#', # doesn't handle multiline comments #= =#
m => '%',
pas => '(//|\{.*\}|\(\*.*\*\))',
php => '(//|#)', # doesn't handle multiline comments /* */
pl => '#', # unless $line =~ /s#[^#]+#[^#]+#/;
py => '#',
r => '#',
rb => '#',
rs => '//',
sc => '//', # doesn't handle multiline comments /* */
sh => '#',
swift => '//', # doesn't handle multiline comments /* */
tcl => ';?\s*#', # remove comments and superfluous ';'
ts => '//',
v => '//', # doesn't handle multiline comments /* */
},
variable => {
# This only affects the global variable searching with -verbose mode
awk => '(\w+)',
bas => '(\w+)',
c => '(\w+)',
cpp => '(\w+)',
dart => '(\w+)',
for => '(\w+)',
go => '(\w+)',
java => '(\w+)',
jl => '(\w+)',
js => '(\w+)',
kt => '(\w+)',
lua => '(\w+)',
m => '(\w+)',
pas => '(\w+)',
php => '[\$]\{?(\w+)',
pl => '[\$\@\%]\{?(\w+)',
py => '(\w+)',
r => '(\w+)',
rb => '(\w+)',
rs => '(\w+)',
sc => '(\w+)',
sh => '(\w+)',
swift => '(\w+)',
tcl => '(\w+)', # set foo $bar <- one of the variables does not have a '$'
ts => '(\w+)',
v => '(\w+)',
},
};
if ( !defined $languageSyntax->{functionDefinition}{$language} ) {
say "ERROR: Language '$language' not recognized. Specify it with -language <language>";
say " For example: -language pl";
say " For example: -language py";
say " For example: -language tcl";
say " Supported languages are: " . join ' ', sort keys %{ $languageSyntax->{functionDefinition} };
exit 1;
}
return $languageSyntax;
}
sub parseFiles {
# Create data structure of each function and the functions they call
my %options = @_;
my $language = $options{language};
my $languageSyntax = $options{languageSyntax};
my ( $file, $shebang, $in_pod, @funcName, @funcSpaces, $funcDefinition, $funcContents, $funcCall );
for my $file ( @{ $options{files} } ) {
open my $FH, $file or die "\nERROR: Cannot find file $file\n";
@funcName = ($main);
@funcSpaces = ('');
my $fileContent = do { local $/; <$FH> };
# Handle '\' line continuation characters
$fileContent =~ s/\\\n//g;
if ( $language =~ /^(c|cpp|java)$/ ) {
# Convert ')\n{' to ') {', which is only needed for C/C++/Java
$fileContent =~ s/\)\s*\n\s*{/) {/g;
}
my $lineNum = 0;
LINE: for my $line ( split /\n/, $fileContent ) {
$lineNum++;
d '.';
d '$lineNum';
chomp($line);
my $originalLine = $line;
$shebang = $line if $lineNum == 1 and $line =~ /^#!/;
next if $line =~ /^\s*(#.*)?$/; # skip empty lines and whole-line comments
d '$line';
if ( $language eq 'pl' ) {
if ( $line =~ /^__END__/ ) {
last LINE;
}
# Skip Perl POD documentation
if ( $line =~ /^=(\w+)/ ) {
$in_pod = ( $1 eq 'cut' ) ? 0 : 1;
next;
}
next if $in_pod;
}
# Remove comments. This is not 100% optimal, as '#' may exist in the middle of a "string"
if ( $language eq 'pl' ) {
$line =~ s/\s*$languageSyntax->{comment}{$language}.*// unless $line =~ /s#[^#]+#[^#]+#/;
} else {
$line =~ s/\s*$languageSyntax->{comment}{$language}.*//;
}
# Perl sub, TCL proc, Python/Ruby def
if ( $line =~ /^$languageSyntax->{functionDefinition}{$language}/i ) {
my $leadingSpaces = $1;
my $funcKeyword = $2;
my $funcName = $3;
#my $junk4 = $4;
#my $junk5 = $5;
#my $junk6 = $6;
#my $junk7 = $7;
#d '$leadingSpaces $funcKeyword $funcName $junk4 $junk5 $junk6 $junk7';
# TCL proc may start with or contain '::'
$funcName =~ s/^.*://; # Remove package::
d "Found start of function '$funcName'";
if ( $funcName eq '' ) {
# TCL proc ::$name dynamic definitions
say "Found dynamically generated proc at line $lineNum of $file. The call graph will show an empty bubble.";
}
$funcDefinition->{$funcName}{$file}{line} = $lineNum;
$funcContents->{$funcName}{$file} = "$line\n";
if ( $language eq 'py' ) {
# Python does not mark the end of functions. Can't support the parsing of nested functions
@funcName = ( '__MAIN__', $funcName );
@funcSpaces = ( '', $leadingSpaces );
} elsif ( $line =~ /}\s*(;\s*)?(#.*)?$/ and $language !~ /(jl)/ ) {
d 'function has started and ended on the same line, do not push';
# proc d {args} {}
# sub say { print @_, "\n" }; # comment
} else {
push @funcName, $funcName;
push @funcSpaces, $leadingSpaces;
}
#d '@funcName @funcSpaces';
next;
}
# End of sub or proc
d( '@funcName $funcName[-1] $main @funcSpaces', 'z' );
if ( $funcName[-1] eq $main ) {
$funcContents->{ $funcName[-1] }{$file} .= "$line\n";
} else {
# Current sub name is not __MAIN__
# Note that this implementation could be fooled by multiline strings
if ( $line =~ /^$languageSyntax->{functionEnd}{$language}/i and $line =~ /^$funcSpaces[-1]\S/ ) {
d "Found end of function $funcName[-1]";
if ( $funcSpaces[0] eq '' ) {
if ( $language eq 'py' ) {
# In Python, there is no '}' to buffer the end of one function from the beginning of the next
$funcContents->{$main}{$file} .= "$line\n";
} else {
# This is a tweak designed to potentially recover from incorrect parsing
# If the function starts at the left border, then assume it was not nested in the middle of some other function
$funcContents->{ $funcName[-1] }{$file} .= "$line\n";
}
@funcName = ($main);
@funcSpaces = ('');
} else {
$funcContents->{ $funcName[-1] }{$file} .= "$line\n";
pop @funcName;
pop @funcSpaces;
}
} else {
$funcContents->{ $funcName[-1] }{$file} .= "$line\n";
}
}
# Find anything that looks like it might be a function, casting a very wide net
while ( $line =~ s/^.*?$languageSyntax->{functionCall}{$language}//i ) {
my $call = $1;
if ( $language =~ /^(tcl|pl)$/ ) {
$call =~ s/^.*://; # Remove package::
}
$funcCall->{ $funcName[-1] }{$file}{$call}++;
d "Found potential function call: $call";
}
$line =~ s/[\$\%\@]//g; # To enhance the following debug statement
d "remain: $line" if $line =~ /\S/; # inspect remainder not caught by the above regex
}
}
return ( $shebang, $funcContents, $funcDefinition, $funcCall );
}
my $languageSyntax = defineSyntax($language);
my ( $shebang, $funcContents, $funcDefinition, $funcCall ) = parseFiles( language => $language, languageSyntax => $languageSyntax, files => \@files );
d '$funcDefinition'; # $funcDefinition->{$funcName}{$file}{line} = line_number
d '$funcCall'; # $funcCall->{$funcName}{$file}{$word} = occurrences
#d '$funcContents'; # $funcContents->{$funcName}{$file} = contents (long)
# Experimental feature -writeFunctions
# Optionally create an output source file for each function in the source file(s)
if ( 1 and $opt{writeFunctions} ) {
writeFunctions();
}
sub writeFunctions {
my %options;
#$options{destDir} = "/tmp/foo";
#$options{removeComments} = 1;
#$options{grep} = 'get_name';
#$options{grep} = 'global';
$options{sort} = 1;
say "\n### Grepping for $options{grep} ###"; # TODO remove this
#$options{reverse} = 1;
#$options{numOccurrences} = 1;
#my $d = 1;
#d '$funcContents';
my %matched;
for my $func ( keys %{$funcContents} ) {
d '$func';
for my $file ( keys %{ $funcContents->{$func} } ) {
d '$file';
if ( defined $options{grep} ) {
for my $line ( split /\n/, $funcContents->{$func}{$file} ) {
d '$line';
if ( $line =~ /$options{grep}/ ) {
$line =~ s/^\s*//;
$matched{$file}{$func}{occurrences}++;
push @{ $matched{$file}{$func}{contents} }, ' ' . $line;
}
}
d '%matched';
} else {
my $bfile = basename($file);
my $ext = '';
if ( $bfile =~ /(.*)(\..*)$/ ) {
( $bfile, $ext ) = ( $1, $2 );
}
my $functionFile = "$tmpdir/${bfile}__${func}${ext}";
say "Creating function source file $functionFile";
open( my $OUT, ">", $functionFile ) or die "\nERROR: Cannot open file for writing: $functionFile\n";
print $OUT $funcContents->{$func}{$file};
close $OUT;
`chmod +x $functionFile`;
#say `ls -l $subsetFile`; # commented because of regressions
}
}
}
#exit 0;
if ( defined $options{grep} ) {
my $format;
if ( scalar keys %matched == 1 ) {
$format = "%s %s\n";
} else {
$format = "%s %s %s\n";
}
my $printFilename = scalar keys %matched > 1 ? 1 : 0;
d '%matched $printFilename';
for my $file ( sort keys %matched ) {
my @sorted = sort { $matched{$file}{$a}{occurrences} <=> $matched{$file}{$b}{occurrences} } keys %{ $matched{$file} };
@sorted = reverse @sorted if $options{reverse};
for my $func (@sorted) {
if ($printFilename) {
printf( $format, $file, $func, $matched{$file}{$func}{occurrences} );
} else {
printf( $format, $func, $matched{$file}{$func}{occurrences} );
}
if ( !$options{numOccurrences} ) {
if ( $options{sort} ) {
say( join "\n", sort @{ $matched{$file}{$func}{contents} } );
} else {
say( join "\n", @{ $matched{$file}{$func}{contents} } );
}
}
}
}
}
exit 0;
}
# Match callers with callees
# first: try to find a match within the same file.
# second: see if the function is defined in ONE other file
## third: complain about an ambiguous call if the callee has multiple definitions
for my $caller_sub ( keys %{$funcCall} ) {
next if ( defined $opt{ignore} and $caller_sub =~ /$opt{ignore}/ );
for my $caller_file ( keys %{ $funcCall->{$caller_sub} } ) {
for my $referenced_sub ( keys %{ $funcCall->{$caller_sub}{$caller_file} } ) {
next unless ( exists $funcDefinition->{$referenced_sub} );
next if ( defined $opt{ignore} and $referenced_sub =~ /$opt{ignore}/ );
my $caller_file_caller_sub = "$caller_file:$caller_sub";
my $caller_file_referenced_sub = "$caller_file:$referenced_sub";
if ( exists $funcDefinition->{$referenced_sub}{$caller_file} ) {
#$call_graph->{$caller_file_caller_sub}{calls}{$caller_file_referenced_sub}++; # OLD, value is always 1
$call_graph->{$caller_file_caller_sub}{calls}{$caller_file_referenced_sub} += $funcCall->{$caller_sub}{$caller_file}{$referenced_sub};
$call_graph->{$caller_file_referenced_sub}{called_by}{$caller_file_caller_sub}++;
} else {
my @referenced_files = sort keys %{ $funcDefinition->{$referenced_sub} };
if ( @referenced_files == 1 ) {
my $referenced_file_referenced_sub = "$referenced_files[0]:$referenced_sub";
#$call_graph->{$caller_file_caller_sub}{calls}{$referenced_file_referenced_sub}++; # OLD, value is always 1
$call_graph->{$caller_file_caller_sub}{calls}{$referenced_file_referenced_sub} += $funcCall->{$caller_sub}{$caller_file}{$referenced_sub};
$call_graph->{$referenced_file_referenced_sub}{called_by}{$caller_file_caller_sub}++;
} else {
# say( "AMBIGUOUS: $caller_file:$caller_sub() -> $referenced_sub() defined in @referenced_files" );
}
}
}
}
}
d '$call_graph';
INTERMEDIATE_FORMAT:
if ( $opt{jsnOut} ) {
eval 'use JSON::XS ()';
if ($@) {
say "\n$@\n";
say "ERROR: Install JSON::XS to use option -jsnOut";
say " sudo cpan install JSON::XS";
exit 1;
} else {
say "Creating file $opt{jsnOut}";
my $coder = JSON::XS->new->ascii->pretty;
my $json_text = $coder->encode($call_graph);
open(my $fh, '>', $opt{jsnOut}) or die "Could not open file '$opt{jsnOut}' $!";
print $fh $json_text;
close $fh;
}
} elsif ( $opt{ymlOut} ) {
eval 'use YAML::XS ()';
if ($@) {
say "\n$@\n";
say "ERROR: Install YAML::XS to use option -ymlOut";
say " sudo cpan install YAML::XS";
exit 1;
} else {
say "Creating file $opt{ymlOut}";
YAML::XS::DumpFile( $opt{ymlOut}, $call_graph );
}
} else {
# do nothing
}
if ( $opt{obfuscate} ) {
my %cache;
#my @gibberish = qw(aerate amplify approximate assemble benchmark calculate compile compress decode defrag delete download emulate encapsulate encode enhance exaggerate grep hypothesize infer inspect interpolate multiplex mutate obfuscate predict process procrastinate profile prune refactor rewind smite spawn swizzle transform translate);
my @gibberish = qw( abscond acknowledge aerate amplify approximate assemble benchmark bite blast boast boil boost broadcast build burn burrow calculate captivate capture charge chortle climb clone communicate compile compress crash create crush curse decode decorate defrag delete demoralize demystify desalinate detonate devour differentiate disinfect disintegrate divulge download edit elevate embezzle emulate emulsify encapsulate enchant encode energize enhance enjoy enlarge erase exaggerate exfoliate experiment explain explode explore extract exude falsify fight flutter forecast fumble fundraise generate gerrymander grep growl hatch hover howl hurry hyperventilate hypothesize illuminate improvise infer inherit inquire insert inspect instantiate interpolate ionize irradiate jostle jump lather leap liquefy locate magnify message mock-up modify multiplex mutate obfuscate obtain overdeliver oxidize pander panic parse pasteurize peek plunder poke pontificate postulate predict process procrastinate profile propose prune publish quench refactor refine refuel relax repair repeat replace reschedule revolve rewind rinse roar ruin sanitize sautee scan schedule scorch scrape search seize self-medicate serve shatter shred shrink sizzle slash sleep slurp smack smite snarl snoop socialize soothe spawn speculate squeal squeeze steer sterilize stretch struggle strut stumble stutter submit subvert superimpose supersize surprise swindle swipe swizzle taunt teleport tenderize test tilt titrate toast transform translate trim untangle upload vaccinate validate vaporize wait warp whimper whisper yell );
if ( defined $ENV{SEED} ) {
# Should be an integer
srand( $ENV{SEED} );
}
$call_graph = renameFunctions( $call_graph, { cache => \%cache, ignore => [qw( __MAIN__ )], jargon => \@gibberish } );
sub renameFunctions {
my ( $in, $options ) = @_;
my $new;
my $numFunctions = scalar keys %{$in};
my $numMapped = scalar @{ $options->{jargon} };
my $numSkipped = @{ $options->{ignore} };
d '$in $options $numFunctions $numMapped';
say "$numFunctions function names (including __MAIN__)";
say "$numSkipped function names will not be mapped";
say "$numMapped obfuscated names in the jargon list";
say "WARNING: Obfuscation needs a bigger word list, will use random words" if $numFunctions > ( $numMapped + $numSkipped );
for my $from_file_sub ( keys %{$in} ) {
my ( $from_file, $from_sub ) = split( /:/, $from_file_sub );
my $from_sub_new = transformVerbs( $from_sub, $options );
for my $calls_or_called_by ( keys %{ $in->{$from_file_sub} } ) {
for my $to_file_sub ( keys %{ $in->{$from_file_sub}{$calls_or_called_by} } ) {
my ( $to_file, $to_sub ) = split( /:/, $to_file_sub );
my $to_sub_new = transformVerbs( $to_sub, $options );
$new->{"$from_file:$from_sub_new"}{$calls_or_called_by}{"$to_file:$to_sub_new"} = $in->{"$from_file:$from_sub"}{$calls_or_called_by}{"$to_file:$to_sub"};
}
}
}
d '$new';
return $new;
}
sub transformVerbs {
my ( $name, $options ) = @_;
my $cache = $options->{cache};
my $new;
if ( defined $cache->{$name} ) {
# We've seen this one before
$new = $cache->{$name};
} elsif ( grep( /^$name$/, @{ $options->{ignore} } ) ) {
# Keep the same name
$new = $name;
$cache->{$name} = $new;
} elsif ( !scalar @{ $options->{jargon} } ) {
# The word list has been used up, think of a random word
SCRABBLE:
my @letters = split //, 'aaaaaaaaabbccddddeeeeeeeeeeeeffgggghhiiiiiiiiijkllllmmnnnnnnooooooooppqrrrrrrssssttttttuuuuvvwwxyyz';
my $name = join '', @letters[ map rand @letters, 1 .. 9 ];
goto SCRABBLE if defined $cache->{$name}; # Don't reuse any random words
$new = $name;
$cache->{$name} = $new;
} else {
# Grab a name out of the bag
my $i = rand( @{ $options->{jargon} } );
$new = $options->{jargon}[$i];
$options->{jargon}[$i] = undef; # Ensure that same name is not mapped twice
@{ $options->{jargon} } = grep { defined($_) } @{ $options->{jargon} };
$cache->{$name} = $new;
}
return $new;
}
}
# Determine which nodes to start graphing from
my @initial_nodes = ();
if ( defined $opt{start} ) {
# The keys of $call_graph are "$filename_including_path:$function"
@initial_nodes = sort grep( /$opt{start}/i, keys %{$call_graph} );
} else {
for my $file_sub ( sort keys %{$call_graph} ) {
my ( $file, $sub ) = split( /:/, $file_sub );
unless ( $call_graph->{$file_sub}{called_by} ) {
push( @initial_nodes, $file_sub );
}
}
}
d '@initial_nodes'; # If there are unused subroutines, they will show up here
if ( $opt{regression} ) {
print Data::Dumper->Dump( [ $funcDefinition, $funcCall, $call_graph ], [qw(funcDefinition funcCall call_graph)] );
}
if ( $opt{jsnIn} or $opt{ymlIn} ) {
# do nothing
} else {
my $numFuncDefinitions = scalar keys %$funcDefinition;
if ( !$numFuncDefinitions or !@initial_nodes ) {
if ( defined $opt{start} ) {
say "\nERROR: Could not find any functions which match '$opt{start}'\n";
} else {
#say "Function definitions: " . join "\n", sort keys %$funcDefinition if $verbose;
if ($numFuncDefinitions) {
say "\nERROR: Found $numFuncDefinitions function definitions, but could not find any matching function calls in your file";
} else {
say "\nERROR: Could not find any function definitions in your file";
}
if ( $language !~ /^(tcl|pl|py)$/ ) {
say " It is possible that $scriptName is not searching for them correctly in your .$language file";
say " It parses one line at a time, using this regular expression for function definitions:";
say " ^$languageSyntax->{functionDefinition}{$language}";
say " and using this regular expression for function calls:";
say " $languageSyntax->{functionCall}{$language}";
say " To enhance the regular expression, edit the code which defines the 'languageSyntax' for 'functionDefinition' and/or 'functionCall',";
say " and ensure that the function name is captured in the 3rd group of the definition'\n";
}
}
exit 1;
}
}
# Produce the graph
my $graph = graph->new(
'call_graph' => $call_graph,
'dot_name' => $dot,
'cluster_files' => $opt{cluster},
'generate_dot' => 1,
);
for my $file_sub (@initial_nodes) {
$graph->plot($file_sub);
}
d '$graph';
d '$graph->{node}';
# Verbose mode to inspect global variables:
# Only analyze the subroutines included in the graph
# For each function:
# Perl:
# determine which variables are local to each sub (my)
# compare these with the variables used
# then determine which global variables are used
# If script does not use 'my' variables, then Python rules are used
# Python:
# Any variables defined in __MAIN__ are globals
# TCL:
# parses 'global' statements and global $::foo variables
if ( $opt{jsnIn} or $opt{ymlIn} ) {
# do nothing
} elsif ( $opt{verbose} ) {
d 'verbose';
my %sub_info;
my $perl_my_var_found;
my $externalScriptCallsFound;
# Add main function to list
my @file_subs = ( sort keys %{ $graph->{node} } );
for my $file_sub ( sort @file_subs ) {
d '$file_sub';
my ( $file, $sub ) = split( /:/, $file_sub );
d '$file $sub';
if ( !grep( /$file:$main/, @file_subs ) ) {
push @file_subs, "$file:$main";
}
}
d '@file_subs';
for my $file_sub ( sort @file_subs ) {
my ( $file, $sub ) = split( /:/, $file_sub );
d '.';
d '.';