Tuesday, July 3, 2012

perl - Identifying who is loading a module

So far I was under the impression that it is difficult to identify who is loading a module since all the use statements are executed even before the debugger prompt appears on the terminal.
Ref: http://perldoc.perl.org/perlmod.html#BEGIN%2c-UNITCHECK%2c-CHECK%2c-INIT-and-END

One can setup a break point when a module is loaded by another module.
b load GT/A.pm # this will set a breakpoint on module load of A.pm

But since use statements are evaluated much before the debug prompt appears on the terminal, we will not be able to identify who has actually loaded the module.

However, we can identify the same via a workaround as follows:

1. Start the debugger
2. set a breakpoint on load of a module (eg: b load GT/A.pm)
3. R # This will restart the debugger session - however, the debug point will be retained
4. T # see the stack trace to identify who is trying to load the module


I will upload the script and module used to experiment this, shortly. Meanwhile, one can take a look at the demonstration of this hack below.


ganesathandavamponnuraj@GT-MBP.local:DebugModuleLoad:>>perl -d useAB.pl

Loading DB routines from perl5db.pl version 1.3
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

A BEGIN
A static
A method print
B BEGIN
B static
B method print
Inside BEGIN block
main::(useAB.pl:8): print "Before function call\n";
  DB<1> b load GT/A.pm
Will stop on load of `GT/A.pm'.
  DB<2> R
Warning: some settings and command-line options may be lost!

Loading DB routines from perl5db.pl version 1.3
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

A BEGIN
'GT/A.pm' loaded...
GT::A::CODE(0x825600)(GT/A.pm:8): print "A static\n";
  DB<2> T
$ = require 'GT/A.pm' called from file `useAB.pl' line 1
$ = main::BEGIN() called from file `GT/A.pm' line 0
$ = eval {...} called from file `GT/A.pm' line 0
  DB<2>

No comments:

Post a Comment