【问题标题】:Local variables not shown when OPcache is enabled while debugging with Xdebug使用 Xdebug 进行调试时启用 OPcache 时未显示局部变量
【发布时间】:2019-03-21 09:05:13
【问题描述】:

我已启用 Xdebug 并与 OPcache 一起工作。 OPcache 模块首先由 phpfpm 加载(重要)。如果您尝试(从浏览器)调试下面的代码并在局部变量中放置一个断点,您将看到它们没有显示。如果我停用 OPCache,一切都会按预期工作。我正在使用 PhpStorm 2018.2.5 进行调试。

class Foo {
    public $member = 'Some value';

    public function methodFoo() {
        $localFoo = "local2";
    }
}

class Bar {
    public $firstMember = "foo";
    public $secondMember = "bar";

    public function create() {
        $localBar = 10;
        $newFoo = new Foo();
        $newFoo->methodFoo();
        $localBar += 1;
        $this->firstMember = "Other value";
    }

}

$foo = "testing";
$bar = "testing2";

$obj = new Bar();
$obj->create();

我不知道是我遗漏了什么还是一个错误。几年前在 xdebug 中也有一个类似的错误,但它与 OPcache 无关: bug

我留下与我的系统相关的信息和Xdebug的日志

phpinfo()
PHP Version => 7.2.10-0ubuntu0.18.04.1

System => Linux pablo-dv6 4.15.0-36-generic #39-Ubuntu SMP Mon Sep 24 16:19:09 UTC 2018 x86_64
Build Date => Sep 13 2018 13:45:02
Server API => Command Line Interface
Virtual Directory Support => disabled
Configuration File (php.ini) Path => /etc/php/7.2/cli
Loaded Configuration File => /etc/php/7.2/cli/php.ini
Scan this dir for additional .ini files => /etc/php/7.2/cli/conf.d
Additional .ini files parsed => /etc/php/7.2/cli/conf.d/10-opcache.ini,
/etc/php/7.2/cli/conf.d/10-pdo.ini,
/etc/php/7.2/cli/conf.d/15-xml.ini,
/etc/php/7.2/cli/conf.d/20-calendar.ini,
/etc/php/7.2/cli/conf.d/20-ctype.ini,
/etc/php/7.2/cli/conf.d/20-curl.ini,
/etc/php/7.2/cli/conf.d/20-dom.ini,
/etc/php/7.2/cli/conf.d/20-exif.ini,
/etc/php/7.2/cli/conf.d/20-fileinfo.ini,
/etc/php/7.2/cli/conf.d/20-ftp.ini,
/etc/php/7.2/cli/conf.d/20-gettext.ini,
/etc/php/7.2/cli/conf.d/20-iconv.ini,
/etc/php/7.2/cli/conf.d/20-intl.ini,
/etc/php/7.2/cli/conf.d/20-json.ini,
/etc/php/7.2/cli/conf.d/20-mbstring.ini,
/etc/php/7.2/cli/conf.d/20-pdo_sqlite.ini,
/etc/php/7.2/cli/conf.d/20-phar.ini,
/etc/php/7.2/cli/conf.d/20-posix.ini,
/etc/php/7.2/cli/conf.d/20-readline.ini,
/etc/php/7.2/cli/conf.d/20-shmop.ini,
/etc/php/7.2/cli/conf.d/20-simplexml.ini,
/etc/php/7.2/cli/conf.d/20-sockets.ini,
/etc/php/7.2/cli/conf.d/20-sqlite3.ini,
/etc/php/7.2/cli/conf.d/20-sysvmsg.ini,
/etc/php/7.2/cli/conf.d/20-sysvsem.ini,
/etc/php/7.2/cli/conf.d/20-sysvshm.ini,
/etc/php/7.2/cli/conf.d/20-tokenizer.ini,
/etc/php/7.2/cli/conf.d/20-wddx.ini,
/etc/php/7.2/cli/conf.d/20-xmlreader.ini,
/etc/php/7.2/cli/conf.d/20-xmlwriter.ini,
/etc/php/7.2/cli/conf.d/20-xsl.ini,
/etc/php/7.2/cli/conf.d/20-zip.ini,
/etc/php/7.2/cli/conf.d/30-xdebug.ini

PHP API => 20170718
PHP Extension => 20170718
Zend Extension => 320170718
Zend Extension Build => API320170718,NTS
PHP Extension Build => API20170718,NTS
Debug Build => no
Thread Safety => disabled
Zend Signal Handling => enabled
Zend Memory Manager => enabled
Zend Multibyte Support => provided by mbstring
IPv6 Support => enabled
DTrace Support => available, disabled

Registered PHP Streams => https, ftps, compress.zlib, php, file, glob, data, http, ftp, phar, zip
Registered Stream Socket Transports => tcp, udp, unix, udg, ssl, tls, tlsv1.0, tlsv1.1, tlsv1.2
Registered Stream Filters => zlib.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk, convert.iconv.*

This program makes use of the Zend Scripting Language Engine:
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.10-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies
    with Xdebug v2.6.1, Copyright (c) 2002-2018, by Derick Rethans


Zend OPcache

Opcode Caching => Disabled
Optimization => Disabled
SHM Cache => Enabled
File Cache => Disabled
Startup Failed => Opcode Caching is disabled for CLI

Directive => Local Value => Master Value
opcache.blacklist_filename => no value => no value
opcache.consistency_checks => 0 => 0
opcache.dups_fix => Off => Off
opcache.enable => On => On
opcache.enable_cli => Off => Off
opcache.enable_file_override => Off => Off
opcache.error_log => no value => no value
opcache.file_cache => no value => no value
opcache.file_cache_consistency_checks => 1 => 1
opcache.file_cache_only => 0 => 0
opcache.file_update_protection => 2 => 2
opcache.force_restart_timeout => 180 => 180
opcache.huge_code_pages => Off => Off
opcache.inherited_hack => On => On
opcache.interned_strings_buffer => 8 => 8
opcache.lockfile_path => /tmp => /tmp
opcache.log_verbosity_level => 1 => 1
opcache.max_accelerated_files => 10000 => 10000
opcache.max_file_size => 0 => 0
opcache.max_wasted_percentage => 5 => 5
opcache.memory_consumption => 128 => 128
opcache.opt_debug_level => 0 => 0
opcache.optimization_level => 0x7FFFBFFF => 0x7FFFBFFF
opcache.preferred_memory_model => no value => no value
opcache.protect_memory => 0 => 0
opcache.restrict_api => no value => no value
opcache.revalidate_freq => 2 => 2
opcache.revalidate_path => Off => Off
opcache.save_comments => 1 => 1
opcache.use_cwd => On => On
opcache.validate_permission => Off => Off
opcache.validate_root => Off => Off
opcache.validate_timestamps => On => On

这部分相关的Xdebug日志(可以看到$localBar没有出现):

<- context_get -i 46 -d 0 -c 0
-> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" command="context_get" transaction_id="46" context="0"><property name="$newFoo" fullname="$newFoo" type="uninitialized"></property><property name="$this" fullname="$this" type="object" classname="Bar" children="1" numchildren="2" page="0" pagesize="100"><property name="firstMember" fullname="$this-&gt;firstMember" facet="public" type="string" size="3" encoding="base64"><![CDATA[Zm9v]]></property><property name="secondMember" fullname="$this-&gt;secondMember" facet="public" type="string" size="3" encoding="base64"><![CDATA[YmFy]]></property></property></response>

欢迎任何帮助,谢谢:)

【问题讨论】:

  • 为什么你认为这是一个错误?这只是一个简单的优化......因为你的变量没有在任何地方使用(就像它不存在一样)。
  • 这不是真的。您可以使用变量并且行为是相同的。这根本不是问题。如果需要,请自行检查。 @LazyOne
  • 如何从浏览器调试?据我所知,调试标志被传递给 PHPStorm 以启动调试会话,所有调试都在其中完成。
  • OK -- 它在哪里(在什么阶段)不起作用?请截图。我已经使用 PHP 7.2.9 x32、Xdebug 2.6.0、OPCache 7.2.9 (Windows 10) 在 PhpStorm 2018.2.5 中测试了您的代码,它似乎工作正常(在 CLI 中)。但是,是的,我看到其他人陈述过类似的问题......
  • 通过浏览器检查 .... 我确实看到缺少一些变量。我添加了一些回显语句......并且没有看到任何输出。但是在这里和那里修改了一些东西(添加了更多代码等)之后——它对我有用。调试时它也可以在真实代码中工作......所以不知道该怎么想。

标签: php debugging phpstorm xdebug opcache


【解决方案1】:

我自己回答,以防它对某人有用。 这是 OPCache 的一个特性。它通过有时删除不需要的变量来优化 PHP 代码。 解决方法:调试时禁用OPCache。

sudo phpdismod opcache

并根据你的环境重启phpfpm或者apache

sudo service php7.2-fpm restart

sudo service apache2 restart

更多信息here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-13
    • 1970-01-01
    • 2020-05-07
    • 1970-01-01
    • 2015-11-11
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多