Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by GrandFather (Saint)
on Aug 26, 2014 at 11:51 UTC ( [id://1098599]=note: print w/replies, xml ) Need Help??


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

How about you try an implementation and see how it pans out? That way at least your teacher can see some progress, and we can see that you've done some work and are more likely to help with actual implementation problems you may have.

A good implementation would abstract the storage back end so you can get parsing and other front end stuff sorted out without bothering too much about an efficient back end, then if the simple first cut back end is too slow, replace it with something new when you've figured out where the problems are.

BTW, in Perl linked lists are just arrays. Using the various array functions like push, pop, shift and unshift along with splice you can efficiently manipulate arrays in very much the way you would a linked list.

Perl is the programming world's equivalent of English

Replies are listed 'Best First'.
Re^2: Analyze a C Call-Stack sequence trace and get call sequence from given Function name onwards.
by tobias_hofer (Friar) on Aug 26, 2014 at 13:42 UTC
    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

      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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1098599]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-03-29 10:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found