Hi,there!
I want to extract all the function call chains with specific
length in source C codes. For example:
f1 calls (f2)
f2 calls (f3,f4)
f3 calls nothing
f4 calls f5
f5 calls nothing
So f1->f2->f4->f5 is one of the function call chains and the
length is 4. If I want to extract all the function call chains with length 3, then the results should be:
f1->f2->f3
f1->f2->f4
f2->f4->f5
The source codes are big and I have gotten all the function call information like above, i.e., f1 calls f2 and saved it
in a hash of array. The keys are the caller function names and the value arrays are the callee function names.
For the example above, the data structure is:
key value
f1 f2
f2 f3,f4
f3
f4 f5
f5
I tended to use recursion, but was stuck, pls help!
The code snippet is:
my %calls; #has all the function call information
my $length = 3; #the length of function chains
my @chain_store; #array of array, to save the chains
while (($key, $value) = each %calls) {
my @chain = ();
push @chain, $key;
get_chains($value, \@chain);
}
sub get_chains {
my $value = shift;
my $chain_ref = shift;
my @chain = @{$chain_ref};
foreach my $val @{$value} {
push @chain, $val;
my $is_present = 1;
for (my $count = 0; $count < $length; $count++) {
if (exist($calls{$val})) {
get_chains($calls{$val})
} else {
$is_present = 0;
last;
}
}
if ($is_present) {
push @chain_store, @chain;
}
}
}