簡要說明
簡單的記錄一下php-xdebug的配置,主要實現通過瀏覽器的插件配合phpstorm來進行斷點調試,單步,以及通過xdebug來生成性能分析報告。
1.php-xdebug擴展的安裝相對來說很簡單,主要是通過apt-get、yum、pecl,或者直接通過編譯的方式來進行安裝都可以,很容易。
2.瀏覽器插件的安裝,這次主要是使用chrome的xdebug-helper插件
3.phpstorm的配置
php.ini文件的修改
[xdebug]
;斷點調試
xdebug.remote_enable=On ;遠程調試
xdebug.remote_autostart=On ;開啟遠程調試自動啟動
xdebug.remote_host=192.168.33.1
xdebug.remote_port=9001
xdebug.auto_trace=on ;啟用代碼自動跟蹤
xdebug.collect_vars=On ;收集變量
xdebug.collect_return=On ;收集返回值
xdebug.collect_params=On ;收集參數
xdebug.idekey=hanpy
xdebug.remote_log="/tmp/xdebug.log"
;性能分析
xdebug.profiler_enable=On ;啟用性能檢測分析
;xdebug.trace_output_dir="/tmp/xdebug_profiler" ;指定堆棧跟蹤文件的存放目錄
xdebug.profiler_output_dir="/tmp/xdebug_profiler" ;指定性能分析文件的存放目錄
xdebug.profiler_output_name="cachegrind.out.%p"
xdebug.profiler_enable_trigger=1
------------------------------------------------------------------------------------------
補充發現
xdebug.remote_autostart : 當此設置設置為1時,即使GET / POST / COOKIE變量不存在,Xdebug也將始終嘗試啟動遠程調試會話並嘗試連接到客戶端。 (這個意思也就是不用安裝瀏覽器插件了吧~~~)
xdebug官網有一個原理圖,放上來記錄一下
1️⃣ The IP of the server is 10.0.1.2 with HTTP on port 80 2️⃣ The IDE is on IP 10.0.1.42, so xdebug.remote_host is set to 10.0.1.42 3️⃣ The IDE listens on port 9000, so xdebug.remote_port is set to 9000 4️⃣ The HTTP request is started on the machine running the IDE 5️⃣ Xdebug connects to 10.0.1.42:9000 6️⃣ Debugging runs, HTTP Response provided

1️⃣ The IP of the server is 10.0.1.2 with HTTP on port 80 2️⃣ The IDE is on an unknown IP, so xdebug.remote_connect_back is set to 1 3️⃣ The IDE listens on port 9000, so xdebug.remote_port is set to 9000 4️⃣ The HTTP request is made, Xdebug detects the IP addres from the HTTP headers 5️⃣ Xdebug connects to the detected IP (10.0.1.42) on port 9000 6️⃣ Debugging runs, HTTP Response provided

phpstorm的配置
1.首先配置phpstorm的Debug,只有一個地方需要配置就是 Debug port 端口(和php.ini中的端口一樣)。在Pre-configuration中已經清晰的寫明白了步驟
1️⃣ 安裝Xdebug
2️⃣ 驗證配置
3️⃣ 安裝瀏覽器插件
4️⃣ 開啟phpstorm的Debug監聽

2.配置phpstorm的Debug Servers
1️⃣ 配置host,項目的host
2️⃣ 配置文件目錄的映射,就是本地文件和服務器文件的映射
3.開啟監聽就可以了

4.最后就是要配置瀏覽器插件了。在前面的php.ini中有一個配置xdebug.idekey,需要把這個key配置到瀏覽器插件中,然后就可以開始單步調試了。
在瀏覽器中訪問a.com,然后在項目中打斷點,phpstorm就可以監聽到。
性能報告文件的生成
xdebg官網:
https://xdebug.org
生成性能報告需要
https://github.com/jokkedk/webgrind/
https://github.com/jrfonseca/gprof2dot
gprof2dot安裝
yum install python3 graphviz
yum 源沒有3,特么的……,手動來吧……
wget http://www.python.org/ftp/python/3.3.0/Python-3.3.0.tgz
tar -xzvf Python-3.3.0.tgz
cd Python-3.3.0
./configure --prefix=/usr/local/python3
make
make install
安裝好之后給webgrind配置一個虛擬主機,直接訪問就可以了,這個就比較簡單了,忘記了可以看一下github里面的文檔。
安裝好之后要修改一下webgrind的配置文件,把python的路徑修改一下,這樣才能生成性能報告的那個圖。
性能報告文件是這樣的

