SlideShare a Scribd company logo
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

PDF
Let it Flow - Introduction to Functional Reactive Programming
PDF
Phing - PHP Conference 2015
PDF
The Magic Of Tie
DOC
5 Rmi Print
PPTX
Rx – reactive extensions
DOCX
Stack array
PPTX
Phpbase
Let it Flow - Introduction to Functional Reactive Programming
Phing - PHP Conference 2015
The Magic Of Tie
5 Rmi Print
Rx – reactive extensions
Stack array
Phpbase

What's hot (20)

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

Viewers also liked (7)

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

More from Workhorse Computing (20)

PDF
Object::Trampoline: Follow the bouncing object.
PDF
Wheels we didn't re-invent: Perl's Utility Modules
PDF
mro-every.pdf
PDF
Paranormal statistics: Counting What Doesn't Add Up
PDF
The $path to knowledge: What little it take to unit-test Perl.
PDF
Unit Testing Lots of Perl
PDF
Generating & Querying Calendar Tables in Posgresql
PDF
Hypers and Gathers and Takes! Oh my!
PDF
BSDM with BASH: Command Interpolation
PDF
Findbin libs
PDF
Memory Manglement in Raku
PDF
BASH Variables Part 1: Basic Interpolation
PDF
Effective Benchmarks
PDF
Metadata-driven Testing
PDF
The W-curve and its application.
PDF
Keeping objects healthy with Object::Exercise.
PDF
Perl6 Regexen: Reduce the line noise in your code.
PDF
Smoking docker
PDF
Getting Testy With Perl6
PDF
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Object::Trampoline: Follow the bouncing object.
Wheels we didn't re-invent: Perl's Utility Modules
mro-every.pdf
Paranormal statistics: Counting What Doesn't Add Up
The $path to knowledge: What little it take to unit-test Perl.
Unit Testing Lots of Perl
Generating & Querying Calendar Tables in Posgresql
Hypers and Gathers and Takes! Oh my!
BSDM with BASH: Command Interpolation
Findbin libs
Memory Manglement in Raku
BASH Variables Part 1: Basic Interpolation
Effective Benchmarks
Metadata-driven Testing
The W-curve and its application.
Keeping objects healthy with Object::Exercise.
Perl6 Regexen: Reduce the line noise in your code.
Smoking docker
Getting Testy With Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6

Recently uploaded (20)

PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Big Data Technologies - Introduction.pptx
PPTX
A Presentation on Artificial Intelligence
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Machine learning based COVID-19 study performance prediction
PDF
Empathic Computing: Creating Shared Understanding
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Digital-Transformation-Roadmap-for-Companies.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Unlocking AI with Model Context Protocol (MCP)
MIND Revenue Release Quarter 2 2025 Press Release
Advanced methodologies resolving dimensionality complications for autism neur...
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Assigned Numbers - 2025 - Bluetooth® Document
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Big Data Technologies - Introduction.pptx
A Presentation on Artificial Intelligence
Programs and apps: productivity, graphics, security and other tools
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Encapsulation_ Review paper, used for researhc scholars
Machine learning based COVID-19 study performance prediction
Empathic Computing: Creating Shared Understanding

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 $_ : $_ } @_ };