Y-KANOH
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
1
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
PHPerのための「Xdebugの活用方法」を語るTechCafe ===================================== # PHPer's NEWS!! ::: info **[PHP8のモダンな書き方チートシートがgithabにあるの見つけたで、 というITコンサルしている海外の人のツイート](https://twitter.com/afilina/status/1462904647933382656?s=21)** ・モダンを知ることでレガシーを知れる ::: ::: info **[The New Life of PHP – The PHP Foundation](https://blog.jetbrains.com/phpstorm/2021/11/the-php-foundation)** ・もっとPHPの言語内でパラダイム・シフトが活発になりそう ・レガシーシステムを作っている会社はDOKIDOKI★HETOHETOが待っていそう★★ ::: ::: info **[2021 アドベントカレンダー Laravel](https://qiita.com/advent-calendar/2021/laravel)** 著名なエンジニアによる記事が満載。Laravel の最新情報などもそろっている雰囲気です ::: ::: info **[JetBrainsがコードエディタの「Fleet」を発表](https://blog.jetbrains.com/ja/blog/2021/11/29/welcome-to-fleet/)** PHPは実装予定。単純に使ってみたいです。 ::: ::: info **[Appleシリコン対応Docker Desktop、一般提供を開始](https://codezine.jp/article/detail/15238)** M1 Mac不具合が解決していけば最新Mac買いたい。 まだIntel Macの信用度は高い。 ::: ::: info **[PHP: PHP 8.1.0 Release Announcement](https://www.php.net/releases/8.1/en.php)** 新機能については、うちのTechBlogでもまとめられています ::: ::: info **[PHPerKaigi petit - PHP8.1リリース祝賀会](https://phperkaigi.connpass.com/event/233022/)** 参加者のアイコンが見たことあるアイコンばかり… ::: ::: info **[「動的プロパティの禁止」が僅差で可決](https://externals.io/message/116519)** ライブラリやら諸々取り込むのは大変そう ちなみに投票数の2/3が賛成で可決です ::: ::: info **[PHPerKaigi petit - PHP8.1リリース祝賀会](https://phperkaigi.connpass.com/event/233022//)** ::: ::: info **[「Log4j」に新たな脆弱性、深刻度は「High」 バージョン2.17.0へのアップデートを呼び掛け](https://www.itmedia.co.jp/news/articles/2112/20/news135.html)** ::: ::: info **[【PHP8.1】リリースページが日本語訳されてなかったので深い悲しみに包まれた](https://qiita.com/rana_kualu/items/e245ef26da2c75d02ba1)** ::: ::: info **[「Laravel 8.77」がリリース](https://laravel-news.com/laravel-8-77-0)** ::: ::: info **[【PHP8.1】PHP8.1はPHP7.4より20%高速化している](https://qiita.com/rana_kualu/items/a402756fdd888a5f7c3a)** ::: ::: info [オープンソース開発者が広く使われているライブラリを改ざん、大量のプロジェクトに影響](https://hisubway.online/news/developer-corrupts-open-source-libraries/) ::: ::: info [「GitLab 14.7」がリリース](https://about.gitlab.com/releases/2022/01/22/gitlab-14-7-released/#group-access-tokens) ::: ::: info [PHP 8.1.2がリリースされました!](https://www.php.net/archive/2022.php#2022-01-21-1) ::: ::: info [The PHP Foundation Update, January 2022](https://opencollective.com/phpfoundation/updates/the-php-foundation-update) ::: ::: info [Laravel 9 がリリース](https://readouble.com/laravel/9.x/ja/installation.html) ::: ::: info [GitHubでMermaidが使えるようになった](https://zenn.dev/yasuhiroki/articles/dd0feae790ba41) ::: # 特集「Xdebugの活用方法」を語る [Xdebug](https://xdebug.org/) [Laravel Sail の Xdebug 設定方法](https://readouble.com/laravel/8.x/ja/sail.html#debugging-with-xdebug) ## そもそもデバッグツールって何よ * de(除去) - bug(不具合) のためのツール * 多くのデバッグツールは基本機能として以下を備えている * ブレイクポイント * 実行中のプログラムを任意の箇所で一時停止する * ステップ実行 * ソースコードを1ステップずつ実行する * プログラムの流れを追う際に使用 * 変数の可視化・変更 * 実行中のプログラムの変数の値を確認したり変更する * 関数・式の実行 * デバッグ中に任意の関数や式を実行する * デバッグツールがないと調査がとても大変。すごく大変。 * 大量のデバッグログ… * 処理を無理やり分岐させるための調査用の実装… * 同時実行など再現が難しい場合… * デバッグ用に入れた処理が本番に残ってしまって大災害… ## Xdebug の概要 * Xdebugは、PHPでデバッグを行う上で便利な機能を提供する**拡張機能** * 多機能で、基本的なデバッグツールとしての機能だけでなく、様々な機能を持つ ### 導入方法 * ポイント * C言語で実装されており、コンパイルされたファイル``.soファイル``を組み込む(php.iniに読み込み設定を追記)ことで有効になる * [公式のインストール手順](https://xdebug.org/docs/install) * [Linux:公式リポジトリを使う](https://xdebug.org/docs/install#linux) * 手軽。 * 一方、リポジトリに反映されているものを使うため、最新バージョンが使えない場合も。 * 例:Ubuntu * ``sudo apt-get install php-xdebug`` * Ubuntu20.04だとv2系が入る * [PECLで入れる](https://xdebug.org/docs/install#pecl) * こちらも手軽。Macの場合はこちら。 * ``pecl install xdebug`` * [ソースをコンパイル](https://xdebug.org/docs/install#source) * 特定のバージョンが必要な場合など、特別な制約がある場合はこちら。 * 特に難しいわけではないが、公式の手順が何を意味しているのか理解しておいたほうがよいと思います。 * Dockerで使う * [公式イメージの説明](https://hub.docker.com/_/php) * Dockerfileの例 ```dockerfile FROM php:8.1-cli RUN pecl install xdebug \ && docker-php-ext-enable xdebug ``` * ``docker-php-ext-enable``を記述すると、コンテナ上のPHPでxdebugが有効化される。 ## Step Debugging ### ステップデバッグとは 任意の行で処理を止めながら行うデバッグのこと。 ### デバッグ方法 (PhpStorm) #### 1. 受信デバッグ接続のリッスンを有効にする ![](https://i.imgur.com/PSb7bXO.png) #### 2. ブレークポイントをセット ![](https://i.imgur.com/IvAk3KP.png) #### 3. デバッグセッションの開始 対象のページをリロードすると、ブレークポイントで処理が止まる。 ![](https://i.imgur.com/7hn5Dbe.png) ### ステップデバッグでできること #### ![](https://i.imgur.com/UMABUJp.png)デバッグセッション再開 * 次のブレークポイントへ進む * 次に通ったブレークポイントにて再度処理が止まる #### ![](https://i.imgur.com/7L0Ge3k.png)ステップオーバ (F8) *  次の行へ処理を進める * 進める行のメソッドがある場合、メソッド内の処理はスキップされる * メソッド内にブレークポイントがある場合は、そこで止まる * "強制ステップオーバ" を使うことで、スキップもできる #### ![](https://i.imgur.com/F4s8XBA.png)ステップイン (F7) * メソッド内の処理を調べたい時に利用 * 止まっている行にあるメソッド内の処理に入って先頭処理で止まる #### ![](https://i.imgur.com/pDObu4c.png)ステップアウト (F8) * 調査中のメソッド内に問題がないことがわかった時に利用 * 現在のメソッドから抜けだして、呼び出し元の処理で止まる #### ![](https://i.imgur.com/JnfUXJN.png)強制ステップイン * ステップインではサードパーティのライブラリなどのメソッドへ入ることができない * 強制ステップインでは、これらのメソッド内の処理へ入ることができる #### 停止している処理での調査 ##### フレーム調査 * どこから呼び出された処理かを確認 ![](https://i.imgur.com/CEHHmg3.png) ##### 変数の調査 / 更新 * 現在の変数に格納された値の確認 * 値の編集も可能 ![](https://i.imgur.com/5SMWDaa.png) ##### 式の評価 * 選択した部分の式評価 ![](https://i.imgur.com/m07NTX0.png) * ![](https://i.imgur.com/RyzaFw5.png)アイコンから任意の式での評価結果確認も可能 ![](https://i.imgur.com/OpCsROV.png) #### その他テクニック ##### ![](https://i.imgur.com/vANWRCo.png)停止している行を表示 * デバック時に停止している行を見失った時に使用 ##### ![](https://i.imgur.com/FTtfAPO.png)ウォッチリストに変数を追加 * 変数を監視リストとして登録可能 ##### ![](https://i.imgur.com/229dyZz.png)ブレークポイントのミュートモード * ONにして以降のブレークポイントで処理を止めなくなる ## [Improvements to PHP's error reporting](https://xdebug.org/docs/develop) * [var_dump()の強化](https://xdebug.org/docs/develop#improved_var_dump) * [スタックトレースの追加](https://xdebug.org/docs/develop#stack_trace) ### 詳細 #### php.ini ``` xdebug.mode=develop ``` #### var_dump()の強化 * サンプルコード ```php $sampleArray = [ 1, // int 0.1, // float true, // bool "string", // string ["nest1", ["nest2", ["nest3"]]] // array ]; var_dump($sampleArray); ``` * developモードがOFFのとき |![](https://i.imgur.com/HiozrqP.png)| |:--| * developモードがONのとき |![](https://i.imgur.com/NQEbQXO.png)| |:--| * 補足情報 * `php.ini`の`html_errors`設定が有効のときしかキレイにならないので注意 * `xdebug.var_display_max_depth` * 配列やオブジェクトのネストについて、どの深さまで表示するか制御できる * `xdebug.cli_color` * コマンドラインで表示されるvar_dump()の結果に色を付けることができる #### スタックトレースの追加 * developモードがOFFのとき |![](https://i.imgur.com/pwFoLoj.png)| |:--| * developモードがONのとき ![](https://i.imgur.com/kcXoaK0.png) * 補足情報 * `xdebug.show_local_vars` * 有効にすると、エラー発生時のスコープ内で使用可能な変数の情報が出力される * その他にも設定が多数あり * https://xdebug.org/docs/develop#related_settings_and_functions ## [Tracing](https://xdebug.org/docs/trace) ### 概要 * 関数呼び出しの履歴を出力できる * 関数がどのような経路で呼ばれているか、全体像を把握できる * 引数、戻り値も確認できる ### 詳細 * php.ini(設定例) ``` xdebug.mode=trace xdebug.trace_format=0 xdebug.output_dir=/tmp/tracing xdebug.start_with_request=no ``` * サンプルコード ```php class SampleClass { public function a() { $this->b(); } private function b() { var_dump("tracing"); } } $sampleClass = new SampleClass(); xdebug_start_trace(); $sampleClass->a(); xdebug_stop_trace(); $sampleClass->a(); ``` * 実行結果 * trace.1373756769.xtというファイルが出力されました ![](https://i.imgur.com/JqUpB1U.png) * 補足情報 * `xdebug.collect_assignments` * 関数に渡された引数の値を出力できる * `xdebug.collect_return` * 関数の戻り値の値を出力できる * `xdebug.trace_format`で出力フォーマットを選択できる * 1(コンピューター可読形式) * 実行結果 ![](https://i.imgur.com/venUi8h.png) * 公式が分析用の[スクリプト](https://github.com/xdebug/xdebug/blob/master/contrib/tracefile-analyser.php)を準備してくれている * 2(HTML) * 実行結果 ![](https://i.imgur.com/ONXeRTj.png) * その他にも設定が多数あり * https://xdebug.org/docs/trace#related_settings_and_functions ## [Profiling](https://xdebug.org/docs/profiler) ### 機能の概要 * 処理の監視や記録を行う機能 * 処理に時間がかかっている箇所やリソース消費の激しい箇所を特定することができる ### 使い方 1. php.ini 設定 * ``` ; profile の設定を有効化 xdebug.mode=profile ; profile の出力先 xdebug.output_dir=/tmp/profiling ``` 2. php のプログラム実行 * `xdebug.output_dir` で指定したディレクトリにファイルが出力される 3. [Webgrind](https://github.com/jokkedk/webgrind) で確認 * profiling されたファイルはそのままでも確認できるが、ツールを使うことで視覚的にわかりやすく確認できる * ![](https://i.imgur.com/Su984iR.png) * 他にもおすすめのツールがあれば教えてください * PhpStorm など? ### 使うシーン * パフォーマンスが悪い処理のボトルネック特定 * 意図しない箇所で例外処理されてしまって原因特定が困難な時 * [debug_backtrace](https://www.php.net/manual/ja/function.debug-backtrace.php) や [error_log](https://www.php.net/manual/ja/errorfunc.configuration.php#ini.log-errors) でも良いと思うが ### 備考 * 現状の php.ini 設定の場合は、すべてのリクエストが profiling されてしまう * [string xdebug.start_with_request](https://xdebug.org/docs/all_settings#start_with_request#trigger) を `trigger` に設定することで profiling を開始したいタイミングを指定できる ### 備忘録 * 項目名は `Profiling` なのに公式のURLは`profiler` になっている ## [Code Coverage Analysis](https://xdebug.org/docs/code_coverage) ### 機能の概要 * 処理を通過した箇所を記録することができる * 単体テストなどでそのプログラムがどの程度テストできているかを把握できる ### 使い方 * 基本的には [PHPUnit](https://phpunit.readthedocs.io/ja/latest/) と組み合わせるのが一般的 * [PHPUnit のコードカバレッジ](https://phpunit.readthedocs.io/ja/latest/code-coverage-analysis.html) も内部でXdebug のコードカバレッジを利用している * php.ini の設定は不要 * ソースコード上でカバレッジを取得したい箇所を `xdebug_start_code_coverage()` と `xdebug_stop_code_coverage()` で挟むだけ * `xdebug_get_code_coverage()` を実行することで上記で挟んだ箇所のカバレッジを取得できる * サンプル * ```php <?php xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE); function a($a) { return; echo $a * 2.5; } function b($count) { if ($count > 25) { echo "too much\n"; } for ($i = 0; $i < $count; $i++) { a($i + 0.17); } } b(6); b(10); var_dump(xdebug_get_code_coverage()); ``` * 実行結果 * ``` array '/home/httpd/html/test/xdebug/docs/xdebug_get_code_coverage.php' => array (size=11) 5 => int 1 6 => int -2 7 => int -2 10 => int 1 11 => int -1 13 => int 1 14 => int 1 16 => int 1 18 => int 1 19 => int 1 21 => int 1 ``` * キー * 対象ファイルの行数 * 値 * カバレッジ取得結果 * 1: 実行した * -1: 実行していない * -2: デッドコードになっている * 使うシーン * PHPUnit による単体テストがどの程度カバーできているかを確認する * ![](https://i.imgur.com/rbIg9rx.png) * デッドコードの洗い出しにも使えそう * 明示的なデッドコードは「-2」になっている箇所を見ればよいだけなのですぐに見つけられる * なかなか分岐に入りにくいようなケースの洗い出し * 本番環境やステージング環境などにカバレッジ取得の処理を組み込む * 開発環境などのソースが変更されやすい環境だと結果がおかしくなりそう * カバレッジの結果をマージ * これを定期的に実施 * 長期間「-1」になっている箇所を確認 * 使われなくなった関数などではいないか? * バージョンアップによって論理的にデッドコードになったのではないか? ---- * 参加者の声 * dockerてxdebugするときのコツ、落とし穴 * デバックの実用的な実践例、使いどころやコツを広げて語ってほしい * Xdebugをprofilerモードで使う場合の使い方を教授いただけるとうれしいです * 優先度はかなり低めですが最近xdebugを使い始めてprofile, garbage 機能の使い方がまだdocを読んでいる途中でして分かってなく、もし議題が無ければ教授頂けるとすごく助かります! * gcstatsを活用してる方がいたらお聞きしたいです! ---- # 参考資料 * [ステップ実行だけじゃないXdebug(きんじょうひでき さん)](https://fortee.jp/phpcon-2021/proposal/3ed8a69b-8618-4644-9a8c-655505078743) * PHP Conference Japan 2021

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully