in reply to Re: Analyze a C Call-Stack sequence trace and get call sequence from given Function name onwards.
in thread Analyze a C Call-Stack sequence trace and get call sequence from given Function name onwards.

Thanks, yes, i am currently prototyping. For simplicity i process all pushes to a stack. Once a pop comes in i will store the stack data. Thus i get the stack usage over execution time. By simply appending the functions to each other i can use regex for matching without big effort. In the end it is simpler than expected :-)

Here my prototype in case of interrest

package CallStackAnalyzer; use strict; use warnings; use diagnostics; sub new { my $self = { name => 'CallStackAnalyzer', tracefilecontent => undef, stack => [], stacktraces => {}, direction => '', }; bless $self; return $self; } sub TraceFile{ my $self = shift; $self->{tracefilecontent} = shift; _AnalyzeTraceFile($self); } sub _AnalyzeTraceFile{ my $self = shift; my @trace = @{$self->{tracefilecontent}}; foreach my $line (@trace){ if($line=~m/^(ENTRY):\s+\S+\s+\S+\s+(\S+)\(\)/){ push(@{$self->{stack}}, $2) && ($self->{direction} = 'up'); } elsif ($line=~m/^(EXIT):\s+\S+\s+\S+\s+(\S+)\(\)/){ my @stack = $self->{stack}; ($self->{stacktraces}{join('->',@{$self->{stack}})} = \@stack ) && ($self->{direction} = 'down') if( not $self->{direction} eq 'down'); pop(@{$self->{stack}}); } } return ; } 1; package TraceCallStack; use strict; use warnings; use diagnostics; use CallStackAnalyzer; sub Main{ my $trace = open(FH, "CallStack.txt"); my @tracecontent = <FH>; close FH; my $CSA = CallStackAnalyzer->new(); $CSA->TraceFile(\@tracecontent); return; } Main(); 1;
It works out great. I will add one more interface to do the matching against a collection of functions..
Many thanks!!!

Tobias
  • Comment on Re^2: Analyze a C Call-Stack sequence trace and get call sequence from given Function name onwards.
  • Download Code

Replies are listed 'Best First'.
Re^3: Analyze a C Call-Stack sequence trace and get call sequence from given Function name onwards.
by Athanasius (Archbishop) on Aug 26, 2014 at 15:38 UTC

    Hello tobias_hofer,

    I notice that in sub new, $self->{stack} is assigned [], a reference to an anonymous array; but in sub _AnalyzeTraceFile you have:

    ... my @stack = $self->{stack}; ($self->{stacktraces}{join('->',@{$self->{stack}})} = \@stack ) && ...

    which treats $self->{stack} as an array, not a reference. I think you meant to write this:

    ... my @stack = @{ $self->{stack} }; ($self->{stacktraces}{ join('->', @{$self->{stack}}) } = \@stack) && ...

    Incidentally, while looking at the code I found it useful to add another method to the CallStackAnalyzer package:

    sub print { use Data::Dump; my $self = shift; dd $self; }

    and to call it in sub Main:

    ... $CSA->TraceFile(\@tracecontent); $CSA->print; ...

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      oh.. yes :-D Thanks a lot!