SlideShare a Scribd company logo
1 of 8
Download to read offline
Signal::StackTrace
When you're there and you know it.




        Steven Lembark
    <lembark@wrkhors.com>
Signals In General
●
    Signals are used on *NIX for asynchronous 
    communication.
●
    You send them when you want to, the recipiant gets 
    them [pretty much] right away.
●
    This gives a nice mechanism for notifying a process 
    that it needs to do something – say print a stack 
    trace.
Perly Signal Handling
●
    Pretty simple: 
     $SIG{ $signame } = sub { ... }

●
    You can turn them off just as easily:
     $SIG{ $signame } = 'IGNORE'
     or
     delete $SIG{ $signame }

●
    You can localize them like any other value:
    Local $SIG{ $signame } = sub { ... }


●
    In this case I used 'USR1' by default:
     $SIG{ USR1 } = sub { ... }
Finding Where You Are From
●
    The 'caller' function tells where the  currently 
    executing sub was called from.
●
    In an array context this includes the subroutine and 
    line number.
●
    Caller can look 'up' the stack by passing a value to 
    caller.
●
    Incrementing the value until there are no more 
    callers gives a stack trace.
Signal Handling With Caller
●
    Fortunately, signal handlers run in the context of the 
    current call: caller reports the stack for the currently 
    running subroutine.
●
    Stack tracing from a signal handler will tell where 
    the code was running when the signal hit.
Stack Trace Code
my $stack_trace
= sub
{
   my %data = ();

     # walk up the stack until caller returns nada.

     for( my $i = 0 ; my @caller = caller $i ; ++$i )
     {
        # using a hash slice names the values.

         @data{ @headerz } = @caller;

         $print_list->( "Caller level $i:", %data );
     }

     $print_list->( "End of trace" );

     return
};
Installing the Signal Handler
sub import
{
  shift;

    # remainder of the stack are signal names, default to SIGUSR1.
    # %SIG is global, no need to worry about the caller's package.

    if( @_ )
    {
        if( my @junk = grep { ! exists $known_sigz{ $_ } } @_ )
        {
            croak "Unknown signals: unknown signals @junk";
        }

      # all the signals are known, install them all
      # with the stack_trace handler.

      @SIG{ @_ } = ( $stack_trace ) x @_;
    }
    else
    {
       $SIG{ USR1 } = $stack_trace;
    }

    return
}
Oddz & Endz
●
    Legit signal names are installed with perl:
    my %known_sigz = ();

    @known_sigz{ split ' ', $Config{ sig_name } } = ();

●
    Pretty printing a list: Dumper refs.
    my $print_list
    = sub
    {
      local $Data::Dumper::Purity        = 0;
      local $Data::Dumper::Terse         = 1;
      local $Data::Dumper::Indent        = 1;
      local $Data::Dumper::Deparse       = 1;
      local $Data::Dumper::Sortkeys      = 1;
      local $Data::Dumper::Deepcopy      = 0;
      local $Data::Dumper::Quotekeys     = 0;

         print STDERR join "n", map { ref $_ ? Dumper $_ : $_ } @_
    };

More Related Content

What's hot

Symfony 2 (PHP Quebec 2009)
Symfony 2 (PHP Quebec 2009)Symfony 2 (PHP Quebec 2009)
Symfony 2 (PHP Quebec 2009)
Fabien Potencier
 

What's hot (20)

Let's begin resource monitoring with munin 2011 1209 zem_distribution
Let's begin resource monitoring with munin 2011 1209 zem_distributionLet's begin resource monitoring with munin 2011 1209 zem_distribution
Let's begin resource monitoring with munin 2011 1209 zem_distribution
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
画像Hacks
画像Hacks画像Hacks
画像Hacks
 
Reactive computing
Reactive computingReactive computing
Reactive computing
 
Clean code in unit testing
Clean code in unit testingClean code in unit testing
Clean code in unit testing
 
Method::Signatures
Method::SignaturesMethod::Signatures
Method::Signatures
 
Score (smart contract for icon)
Score (smart contract for icon) Score (smart contract for icon)
Score (smart contract for icon)
 
The Truth About Lambdas in PHP
The Truth About Lambdas in PHPThe Truth About Lambdas in PHP
The Truth About Lambdas in PHP
 
Backdoor coding
Backdoor codingBackdoor coding
Backdoor coding
 
Debug like a doctor
Debug like a doctorDebug like a doctor
Debug like a doctor
 
COMP2021 Final Project - LightHTML
COMP2021 Final Project - LightHTMLCOMP2021 Final Project - LightHTML
COMP2021 Final Project - LightHTML
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
A Series of Fortunate Events - Drupalcon Europe, Amsterdam 2014
A Series of Fortunate Events - Drupalcon Europe, Amsterdam 2014A Series of Fortunate Events - Drupalcon Europe, Amsterdam 2014
A Series of Fortunate Events - Drupalcon Europe, Amsterdam 2014
 
Symfony 2 (PHP Quebec 2009)
Symfony 2 (PHP Quebec 2009)Symfony 2 (PHP Quebec 2009)
Symfony 2 (PHP Quebec 2009)
 
OMG JavaScript
OMG JavaScriptOMG JavaScript
OMG JavaScript
 
ECMAScript 6
ECMAScript 6ECMAScript 6
ECMAScript 6
 
C++ programs
C++ programsC++ programs
C++ programs
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 

Viewers also liked

Viewers also liked (7)

Lies, Damn Lies, and Benchmarks
Lies, Damn Lies, and BenchmarksLies, Damn Lies, and Benchmarks
Lies, Damn Lies, and Benchmarks
 
Memory unmanglement
Memory unmanglementMemory unmanglement
Memory unmanglement
 
Getting testy with Perl
Getting testy with PerlGetting testy with Perl
Getting testy with Perl
 
Selenium sandwich-3: Being where you aren't.
Selenium sandwich-3: Being where you aren't.Selenium sandwich-3: Being where you aren't.
Selenium sandwich-3: Being where you aren't.
 
Get your teeth into Plack
Get your teeth into PlackGet your teeth into Plack
Get your teeth into Plack
 
Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.
 
Digital Age 2.0 - Andrea Harrison
Digital Age 2.0 - Andrea HarrisonDigital Age 2.0 - Andrea Harrison
Digital Age 2.0 - Andrea Harrison
 

Similar to Signal Stacktrace

Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
Kang-min Liu
 
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Masahiro Nagano
 
Introduction to CloudForecast / YAPC::Asia 2010 Tokyo
Introduction to CloudForecast / YAPC::Asia 2010 TokyoIntroduction to CloudForecast / YAPC::Asia 2010 Tokyo
Introduction to CloudForecast / YAPC::Asia 2010 Tokyo
Masahiro Nagano
 
Crazy things done on PHP
Crazy things done on PHPCrazy things done on PHP
Crazy things done on PHP
Taras Kalapun
 
C programs
C programsC programs
C programs
Minu S
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdf
anujmkt
 

Similar to Signal Stacktrace (20)

Bag of tricks
Bag of tricksBag of tricks
Bag of tricks
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
 
There's more than one way to empty it
There's more than one way to empty itThere's more than one way to empty it
There's more than one way to empty it
 
Introduction to CloudForecast / YAPC::Asia 2010 Tokyo
Introduction to CloudForecast / YAPC::Asia 2010 TokyoIntroduction to CloudForecast / YAPC::Asia 2010 Tokyo
Introduction to CloudForecast / YAPC::Asia 2010 Tokyo
 
Crazy things done on PHP
Crazy things done on PHPCrazy things done on PHP
Crazy things done on PHP
 
Perl Bag of Tricks - Baltimore Perl mongers
Perl Bag of Tricks  -  Baltimore Perl mongersPerl Bag of Tricks  -  Baltimore Perl mongers
Perl Bag of Tricks - Baltimore Perl mongers
 
Bouncingballs sh
Bouncingballs shBouncingballs sh
Bouncingballs sh
 
Perl on Amazon Elastic MapReduce
Perl on Amazon Elastic MapReducePerl on Amazon Elastic MapReduce
Perl on Amazon Elastic MapReduce
 
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
 
Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01
 
Ns2programs
Ns2programsNs2programs
Ns2programs
 
Drush. Secrets come out.
Drush. Secrets come out.Drush. Secrets come out.
Drush. Secrets come out.
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrow
 
Data Types Master
Data Types MasterData Types Master
Data Types Master
 
C programs
C programsC programs
C programs
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdf
 
Pop3ck sh
Pop3ck shPop3ck sh
Pop3ck sh
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
distill
distilldistill
distill
 

More from Workhorse Computing

More from Workhorse Computing (20)

Wheels we didn't re-invent: Perl's Utility Modules
Wheels we didn't re-invent: Perl's Utility ModulesWheels we didn't re-invent: Perl's Utility Modules
Wheels we didn't re-invent: Perl's Utility Modules
 
mro-every.pdf
mro-every.pdfmro-every.pdf
mro-every.pdf
 
Paranormal statistics: Counting What Doesn't Add Up
Paranormal statistics: Counting What Doesn't Add UpParanormal statistics: Counting What Doesn't Add Up
Paranormal statistics: Counting What Doesn't Add Up
 
The $path to knowledge: What little it take to unit-test Perl.
The $path to knowledge: What little it take to unit-test Perl.The $path to knowledge: What little it take to unit-test Perl.
The $path to knowledge: What little it take to unit-test Perl.
 
Unit Testing Lots of Perl
Unit Testing Lots of PerlUnit Testing Lots of Perl
Unit Testing Lots of Perl
 
Generating & Querying Calendar Tables in Posgresql
Generating & Querying Calendar Tables in PosgresqlGenerating & Querying Calendar Tables in Posgresql
Generating & Querying Calendar Tables in Posgresql
 
Hypers and Gathers and Takes! Oh my!
Hypers and Gathers and Takes! Oh my!Hypers and Gathers and Takes! Oh my!
Hypers and Gathers and Takes! Oh my!
 
BSDM with BASH: Command Interpolation
BSDM with BASH: Command InterpolationBSDM with BASH: Command Interpolation
BSDM with BASH: Command Interpolation
 
Findbin libs
Findbin libsFindbin libs
Findbin libs
 
Memory Manglement in Raku
Memory Manglement in RakuMemory Manglement in Raku
Memory Manglement in Raku
 
BASH Variables Part 1: Basic Interpolation
BASH Variables Part 1: Basic InterpolationBASH Variables Part 1: Basic Interpolation
BASH Variables Part 1: Basic Interpolation
 
Effective Benchmarks
Effective BenchmarksEffective Benchmarks
Effective Benchmarks
 
Metadata-driven Testing
Metadata-driven TestingMetadata-driven Testing
Metadata-driven Testing
 
The W-curve and its application.
The W-curve and its application.The W-curve and its application.
The W-curve and its application.
 
Keeping objects healthy with Object::Exercise.
Keeping objects healthy with Object::Exercise.Keeping objects healthy with Object::Exercise.
Keeping objects healthy with Object::Exercise.
 
Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.
 
Smoking docker
Smoking dockerSmoking docker
Smoking docker
 
Getting Testy With Perl6
Getting Testy With Perl6Getting Testy With Perl6
Getting Testy With Perl6
 
Neatly folding-a-tree
Neatly folding-a-treeNeatly folding-a-tree
Neatly folding-a-tree
 
Light my-fuse
Light my-fuseLight my-fuse
Light my-fuse
 

Recently uploaded

Recently uploaded (20)

Strategic AI Integration in Engineering Teams
Strategic AI Integration in Engineering TeamsStrategic AI Integration in Engineering Teams
Strategic AI Integration in Engineering Teams
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
 
Syngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdfSyngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and Planning
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
 
Designing for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at ComcastDesigning for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at Comcast
 
Enterprise Knowledge Graphs - Data Summit 2024
Enterprise Knowledge Graphs - Data Summit 2024Enterprise Knowledge Graphs - Data Summit 2024
Enterprise Knowledge Graphs - Data Summit 2024
 
Connecting the Dots in Product Design at KAYAK
Connecting the Dots in Product Design at KAYAKConnecting the Dots in Product Design at KAYAK
Connecting the Dots in Product Design at KAYAK
 
Buy Epson EcoTank L3210 Colour Printer Online.pptx
Buy Epson EcoTank L3210 Colour Printer Online.pptxBuy Epson EcoTank L3210 Colour Printer Online.pptx
Buy Epson EcoTank L3210 Colour Printer Online.pptx
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
Buy Epson EcoTank L3210 Colour Printer Online.pdf
Buy Epson EcoTank L3210 Colour Printer Online.pdfBuy Epson EcoTank L3210 Colour Printer Online.pdf
Buy Epson EcoTank L3210 Colour Printer Online.pdf
 
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfIntroduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
 
Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024
 

Signal Stacktrace

  • 1. Signal::StackTrace When you're there and you know it. Steven Lembark <lembark@wrkhors.com>
  • 2. Signals In General ● Signals are used on *NIX for asynchronous  communication. ● You send them when you want to, the recipiant gets  them [pretty much] right away. ● This gives a nice mechanism for notifying a process  that it needs to do something – say print a stack  trace.
  • 3. Perly Signal Handling ● Pretty simple:  $SIG{ $signame } = sub { ... } ● You can turn them off just as easily: $SIG{ $signame } = 'IGNORE' or delete $SIG{ $signame } ● You can localize them like any other value: Local $SIG{ $signame } = sub { ... } ● In this case I used 'USR1' by default: $SIG{ USR1 } = sub { ... }
  • 4. Finding Where You Are From ● The 'caller' function tells where the  currently  executing sub was called from. ● In an array context this includes the subroutine and  line number. ● Caller can look 'up' the stack by passing a value to  caller. ● Incrementing the value until there are no more  callers gives a stack trace.
  • 5. Signal Handling With Caller ● Fortunately, signal handlers run in the context of the  current call: caller reports the stack for the currently  running subroutine. ● Stack tracing from a signal handler will tell where  the code was running when the signal hit.
  • 6. Stack Trace Code my $stack_trace = sub { my %data = (); # walk up the stack until caller returns nada. for( my $i = 0 ; my @caller = caller $i ; ++$i ) { # using a hash slice names the values. @data{ @headerz } = @caller; $print_list->( "Caller level $i:", %data ); } $print_list->( "End of trace" ); return };
  • 7. Installing the Signal Handler sub import { shift; # remainder of the stack are signal names, default to SIGUSR1. # %SIG is global, no need to worry about the caller's package. if( @_ ) { if( my @junk = grep { ! exists $known_sigz{ $_ } } @_ ) { croak "Unknown signals: unknown signals @junk"; } # all the signals are known, install them all # with the stack_trace handler. @SIG{ @_ } = ( $stack_trace ) x @_; } else { $SIG{ USR1 } = $stack_trace; } return }
  • 8. Oddz & Endz ● Legit signal names are installed with perl: my %known_sigz = (); @known_sigz{ split ' ', $Config{ sig_name } } = (); ● Pretty printing a list: Dumper refs. my $print_list = sub { local $Data::Dumper::Purity = 0; local $Data::Dumper::Terse = 1; local $Data::Dumper::Indent = 1; local $Data::Dumper::Deparse = 1; local $Data::Dumper::Sortkeys = 1; local $Data::Dumper::Deepcopy = 0; local $Data::Dumper::Quotekeys = 0; print STDERR join "n", map { ref $_ ? Dumper $_ : $_ } @_ };