【问题标题】:Invoking Perl debugger so that it runs until first breakpoint调用 Perl 调试器,使其运行到第一个断点
【发布时间】:2013-05-06 21:11:50
【问题描述】:

当我使用 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

【问题讨论】:

    标签: perl debugging


    【解决方案1】:

    $DB::single = 1;
    

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


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

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

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

    还有一个"NonStop" 选项,您可以在.perldbPERLDB_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");
    

    【讨论】:

    • 我的.perldb 中有以下内容:push @DB::typeahead, "c"; parse_options("NonStop=1");,然后在打印语句之前的代码中有DB::single = 1;。然而,调试器似乎忽略了这一点,并在我的脚本结束时停止。对可能导致这种情况的任何想法?
    • 这可能是因为您的程序从未达到该语句。
    • 另外,同时使用NonStop=1@typeahead=("c") 就像按两次“c”,这可能不是您的意思。
    • 谢谢@mob。我更新了原帖。我还是没弄好。
    • 我刚刚在您的回答中添加了一个新内容,以解决我提出的最后一个问题。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2014-09-24
      • 2017-10-11
      • 2016-07-19
      • 2021-08-09
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      相关资源
      最近更新 更多