Devel::Cover 和 ModPerl::Registry 没有覆盖运行时

发布于 2024-08-28 19:35:49 字数 1346 浏览 10 评论 0原文

当我使用 ModPerl::Registry 运行 Devel::Cover 时,除了 BEGIN 块之外,我没有得到任何覆盖信息。当我从命令行或作为 CGI 使用 Devel::Cover 运行相同的脚本时,一切正常(显然)。

如何让 Devel::Cover “看到”我的代码在运行时执行?

这是我的 httpd.conf 中与 Devel::Cover 相关的内容:

MaxClients 1
PerlSetEnv DEVEL_COVER_OPTIONS -db,/tmp/cover_db,-silent,1
PerlRequire /var/www/project/startup.pl

这是 startup.pl:(

#!/usr/bin/perl
use strict;
use warnings;

use Apache2::Directive ();

BEGIN {
    # Devel::Cover database must be writable by worker processes
    my $conftree = Apache2::Directive::conftree->as_hash;
    my $name = $conftree->{User}
        or die "couldn't find user in Apache config";
    print "user=$name\n";

    my $uid = getpwnam($name);
    defined $uid
        or die "couldn't determine uid by name";

    no warnings 'redefine';
    local $> = $uid;

    require Devel::Cover;

    my $old_report = \&Devel::Cover::report;
    *Devel::Cover::report = sub { local $> = $uid; $old_report->(@_) };

    Devel::Cover->import;
}

1;

如您所见,我制作了一个猴子补丁对于 Devel::Cover 因为 startup.pl 是由 root 运行的,但是工作进程在不同的用户下运行,否则他们不能'不要阅读由 startup.pl 创建的目录。如果您知道更好的解决方案,请记下。)

When I'm running Devel::Cover with ModPerl::Registry, I get no coverage info except for BEGIN blocks. When I'm running the same script with Devel::Cover from command line or as a CGI, everything works alright (obviously).

How can I make Devel::Cover "see" my code being executed in the runtime?

Here's Devel::Cover related stuff in my httpd.conf:

MaxClients 1
PerlSetEnv DEVEL_COVER_OPTIONS -db,/tmp/cover_db,-silent,1
PerlRequire /var/www/project/startup.pl

Here's startup.pl:

#!/usr/bin/perl
use strict;
use warnings;

use Apache2::Directive ();

BEGIN {
    # Devel::Cover database must be writable by worker processes
    my $conftree = Apache2::Directive::conftree->as_hash;
    my $name = $conftree->{User}
        or die "couldn't find user in Apache config";
    print "user=$name\n";

    my $uid = getpwnam($name);
    defined $uid
        or die "couldn't determine uid by name";

    no warnings 'redefine';
    local 
gt; = $uid;

    require Devel::Cover;

    my $old_report = \&Devel::Cover::report;
    *Devel::Cover::report = sub { local 
gt; = $uid; $old_report->(@_) };

    Devel::Cover->import;
}

1;

(As you may see, I made a monkey patch for Devel::Cover since startup.pl is being run by root, but worker processes run under a different user, and otherwise they couldn't read directories created by startup.pl. If you know a better solution, make a note, please.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画中仙 2024-09-04 19:35:49

尝试使用 -X 开关运行 apache,使其作为单个进程运行。您可能还需要将 MaxRequestsPerChild 设置为较低的值(甚至可能为 1),以便在少量请求后退出。

Try running apache with the -X switch, to make it run as a single process. You may also want to set MaxRequestsPerChild to a low value (maybe even 1) so that it will exit after a small number of requests.

太傻旳人生 2024-09-04 19:35:49

我认为这是由于 Devel::Cover 来得太晚而无法添加钩子,即在所有代码都已编译之后。我会尝试在startup.pl的开头添加use Devel::Cover,或者在httpd.conf中的其他mod_perl内容之前添加PerlModule Devel::Cover

I think is due to Devel::Cover coming in too late to add hooks, i.e. after all your code has been compiled. I'd try adding use Devel::Cover at the beginning of your startup.pl, or PerlModule Devel::Cover before the other mod_perl stuff in httpd.conf.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文