调用Perl调试器,使其运行到第一个断点

时间:2013-05-02 18:57:50

标签: perl debugging

当我使用perl -d myscript.pl调用我的Perl调试器时,调试器启动,但在我按n(下一个)或c(继续)之前,它不会执行任何代码。

无论如何调用调试器并默认运行代码直到遇到断点?

如果是这样,是否有任何语句可以在我的代码中用作断点,让调试器在命中时停止?

更新

以下是我在.perldb文件中的内容:

print "Reading ~/.perldb options.\n";
push @DB::typeahead, "c";
parse_options("NonStop=1");

这是我的hello_world.pl文件:

use strict;
use warnings;

print "Hello world.\n"; 
$DB::single=1;
print "How are you?";

以下是运行的调试会话:perl -d hello_world.pl

Reading ~/.perldb options.
Hello world
main::(hello_world.pl:6):       print "How are you?";
auto(-1)  DB<1> c
Debugged program terminated.  Use q to quit or R to restart,
  use o inhibit_exit to avoid stopping after program termination,
  h q, h R or h o to get additional info.  
  DB<1> v
9563    9564    
9565    sub at_exit {
9566==>     "Debugged program terminated.  Use `q' to quit or `R' to restart.";
9567    }
9568    
9569    package DB;    # Do not trace this 1; below!
  DB<1> 

换句话说,我的调试器会跳过print "How are you?",而是在程序完成后停止,这不是我想要的。

我想要的是调试器运行我的代码而不停在任何地方(也不是在开头,也不是在我的脚本末尾),除非我明确地拥有$DB::single=1;语句,在这种情况下我希望它在运行下一行之前停止。有什么办法吗?

供参考,我正在使用:

$perl --version 

This is perl 5, version 14, subversion 1 (v5.14.1) built for x86_64-linux

2 个答案:

答案 0 :(得分:12)

$DB::single = 1;

在任何在代码中设置永久断点的语句之前。 这也适用于编译时代码,并且可能是在编译阶段设置断点的唯一好方法。


要让调试器自动启动代码,您可以在代码中的@DB::typeahead文件或编译时(.perldb)块中操作BEGIN数组。例如:

# .perldb file
push @DB::typeahead, "c";

BEGIN { push @DB::typeahead, "p 'Hello!'", "c" }
...
$DB::single = 1;
$x = want_to_stop_here();

您可以在"NonStop".perldb环境变量中设置PERLDB_OPTS选项:

PERLDB_OPTS=NonStop perl -d myprogram.pl

所有这些(以及更多内容)都会在perldebugperl5db.pl

的内容深入讨论

更新

解决最近更新中提出的问题。在./perldb中使用以下内容:

print "Reading ~/.perldb options.\n";
push @DB::typeahead, "c";
parse_options("inhibit_exit=0");

答案 1 :(得分:2)

同时查看Enbugger。关于调试器的主题,请参阅Devel::Trepan,它也适用于Enbugger。