【问题标题】:PHP debugging in visual studio code only works sometimesVisual Studio 代码中的 PHP 调试有时只能工作
【发布时间】:2017-08-31 19:17:26
【问题描述】:

我在 Visual Studio 中使用 xdebug 进行 php 调试有时只能工作。有时我只是获得正常的浏览器页面加载(下面的错误日志)。

编辑: 起初我认为这是由于文件的位置,但我注意到它可能发生在任何位置;要么 xdebug 在代码中发现错误而没有在我自己的断点处停止;或者它可以找到所有类型的断点或什么都找不到......现在我认为它与刷新、修改和重新保存文件以及文件中的错误类型有关。如果我能找出原因,我会回到这个问题。

在我当前的设置中,我的本地主机 webroot 位于 /home/user/Git/www/projects,这是 127.0.0.1 指向的位置。但是,我还设置了多个虚拟主机,它们驻留在各自的子文件夹中:/home/user/Git/www/projects/project_name


一些我已经看过的东西

在这个帖子中:Visual Studio Code - Xdebug won't work - 他们建议设置xdebug.remote_connect_back = 1,但这并不能解决问题。他们还建议在 launch.json 中设置 localSourceRoot 以指向服务器根目录。但是,简单地添加"localSourceRoot": "http://127.0.0.1" 或类似"localSourceRoot": "http://127.0.0.1/project_name" 的东西是行不通的。


日志

/etc/hosts

127.0.0.1   project_names
127.0.0.1   php
127.0.0.1   localhost 
::1         localhost

php.ini:

[Xdebug] 
zend_extension = xdebug.so
xdebug.remote_enable = on
xdebug.remote_autostart = on
xdebug.remote_handler = dbgp
xdebug.remote_host = 127.0.0.1
xdebug.remote_port = 9000
xdebug.remote_mode = req
xdebug.idekey=php-vs
;xdebug.remote_connect_back = 1

launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "log": true,
            "request": "launch",
            "port": 9000
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000
        }
    ]
}

VS 调试控制台:

<- launchResponse
Response {
  seq: 0,
  type: 'response',
  request_seq: 2,
  command: 'launch',
  success: true }

new connection 1
<- threadEvent
ThreadEvent {
  seq: 0,
  type: 'event',
  event: 'thread',
  body: { reason: 'started', threadId: 1 } }

<- initializedEvent
InitializedEvent { seq: 0, type: 'event', event: 'initialized' }

-> threadsRequest
{ command: 'threads', type: 'request', seq: 3 }

<- threadsResponse
Response {
  seq: 0,
  type: 'response',
  request_seq: 3,
  command: 'threads',
  success: true,
  body: { threads: [ Thread { id: 1, name: 'Request 1 (9:06:23 PM)' } ] } }

-> setBreakpointsRequest
{ command: 'setBreakpoints',
  arguments: 
   { source: 
      { path: '/home/micke/Git/www/projects/php/trial.php',
        name: 'trial.php' },
     lines: [ 14 ],
     breakpoints: [ { line: 14 } ],
     sourceModified: false },
  type: 'request',
  seq: 4 }

<- setBreakpointsResponse
Response {
  seq: 0,
  type: 'response',
  request_seq: 4,
  command: 'setBreakpoints',
  success: true,
  body: { breakpoints: [ { verified: true, line: 14 } ] } }

-> setFunctionBreakpointsRequest
{ command: 'setFunctionBreakpoints',
  arguments: { breakpoints: [] },
  type: 'request',
  seq: 5 }

<- setFunctionBreakpointsResponse
Response {
  seq: 0,
  type: 'response',
  request_seq: 5,
  command: 'setFunctionBreakpoints',
  success: true,
  body: { breakpoints: [] } }

-> setExceptionBreakpointsRequest
{ command: 'setExceptionBreakpoints',
  arguments: { filters: [ '*' ] },
  type: 'request',
  seq: 6 }

<- setExceptionBreakpointsResponse
Response {
  seq: 0,
  type: 'response',
  request_seq: 6,
  command: 'setExceptionBreakpoints',
  success: true }

-> configurationDoneRequest
{ command: 'configurationDone', type: 'request', seq: 7 }

-> threadsRequest
{ command: 'threads', type: 'request', seq: 8 }

<- threadsResponse
Response {
  seq: 0,
  type: 'response',
  request_seq: 8,
  command: 'threads',
  success: true,
  body: { threads: [ Thread { id: 1, name: 'Request 1 (9:06:23 PM)' } ] } }

<- configurationDoneResponse
Response {
  seq: 0,
  type: 'response',
  request_seq: 7,
  command: 'configurationDone',
  success: true }

<- threadEvent
ThreadEvent {
  seq: 0,
  type: 'event',
  event: 'thread',
  body: { reason: 'exited', threadId: 1 } }

【问题讨论】:

标签: php visual-studio-code xdebug lamp


【解决方案1】:

我终于意识到问题与挂载点有关。 VScode中的项目是在webserver的安装路径上打开的;不是它最初在apache中指向的地方。所以断点被认为是添加到另一个文件中,因为路径不一样。

【讨论】:

  • 您能分享一下您是如何解决这个问题的吗?我这里有 2 个本地项目,其中一个 xdebug 工作得很好,另一个没有
  • 现在是前一段时间了,但我想我只是在 apache 中指定的原始 web 路径中打开了 VScode,而不是在某个挂载或 mb 符号链接路径中间接打开它。
【解决方案2】:

你的debug有时候会成功,看来你的配置是对的,可能是php xdebug的端口造成的。当我这样做时,它对我很有效:

1.使用php --ini显示php配置文件并将xdebug端口更改为9900(或其他数字): xdebug.remote_port=9900 然后重启你的php

2.将 vscode xdebug 端口更改为 launch.json 中相同的数字: "port": 9900

3.享受你的调试

【讨论】:

    【解决方案3】:

    更新到最新版本的 xdebug 并将 xdebug.remote_timeout 设置为更高的值,例如 3000

    【讨论】:

      猜你喜欢
      • 2017-10-02
      • 1970-01-01
      • 1970-01-01
      • 2015-08-04
      • 1970-01-01
      • 2018-10-27
      • 2020-07-16
      • 1970-01-01
      • 2019-08-20
      相关资源
      最近更新 更多