summaryrefslogtreecommitdiffhomepage
path: root/slides.typ
blob: 112580067525516208bf7d8075a6c3b0ca8e3049 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#import "@preview/touying:0.6.3": *
#import "@preview/codly:1.3.0": *
#import "@preview/cjk-unbreak:0.2.0": remove-cjk-break-space, transform-childs
#import "@preview/fletcher:0.5.8" as fletcher: diagram, node, edge
#import "setoka.typ": *

#show: codly-init.with()

#show: remove-cjk-break-space

#show: setoka-theme.with(
  aspect-ratio: "16-9",
  config-info(
    title: [
      Deep Dive into Xdebug
    ],
    subtitle: [PHP カンファレンス小田原 2026],
    author: [nsfisis (いまむら)],
    date: datetime(year: 2026, month: 4, day: 11),
  ),
  config-common(preamble: {
    codly(
      fill: rgb("#eee"),
      lang-format: none,
      number-format: none,
      zebra-fill: none,
    )
  })
)

#set text(font: "BIZ UDPGothic", lang: "ja")
#show raw: set text(font: "UDEV Gothic 35")

#let box-color-highlight = rgb("#ffa500").lighten(60%)
#let box-color-highlight2 = rgb("#ffa500")
#let box-color-normal = rgb("#eee")

#title-slide()

#about-slide()

#[
  #set align(center + horizon)
  #set text(size: 40pt)
  Xdebug のステップ実行は\
  どう実現されているのか
]

---

#[
  #set text(size: 30pt)

  Xdebug

  - PHP の拡張 (extension)
  - ステップ実行
  - トレース
  - プロファイリング
  - コードカバレッジ計測
]

---

#[
  #set align(center + horizon)

  今回はステップ実行の話
]

---

#[
  #set align(center + horizon)

  ステップ実行

  #figure(
    image("./ss1.png", width: 60%)
  )
]

---

#[
  #set align(center + horizon)

  ステップ実行

  #figure(
    image("./ss2.png", width: 60%)
  )
]

---

#[
  #set align(center + horizon)
  #set text(size: 32pt)

  ステップ実行は \
  どのように実装されているのか?
]

---

#[
  #set align(center + horizon)
  #set text(size: 28pt)

  #diagram(
    spacing: 8em,
    node-stroke: 0.5pt,
    node((0, 0), [IDE], shape: rect, fill: box-color-normal, inset: 1em, corner-radius: 4pt),
    node((1, 0), [PHP + Xdebug], shape: rect, fill: box-color-normal, inset: 1em, corner-radius: 4pt),
    edge((0, 0), (1, 0), "->", shift: 8pt),
    edge((1, 0), (0, 0), "->", shift: 8pt),
  )
]

---

#[
  #set align(center + horizon)
  #set text(size: 28pt)

  #diagram(
    spacing: 8em,
    node-stroke: 0.5pt,
    node((0, 0), [IDE], shape: rect, fill: box-color-highlight, inset: 1em, corner-radius: 4pt),
    node((1, 0), [PHP + Xdebug], shape: rect, fill: box-color-normal, inset: 1em, corner-radius: 4pt),
    edge((0, 0), (1, 0), "->", shift: 8pt),
    edge((1, 0), (0, 0), "->", shift: 8pt),
  )
]

---

#[
  #set align(center + horizon)
  #set text(size: 28pt)

  #diagram(
    spacing: 8em,
    node-stroke: 0.5pt,
    node((0, 0), [IDE], shape: rect, fill: box-color-normal, inset: 1em, corner-radius: 4pt),
    node((1, 0), [PHP + Xdebug], shape: rect, fill: box-color-highlight, inset: 1em, corner-radius: 4pt),
    edge((0, 0), (1, 0), "->", shift: 8pt),
    edge((1, 0), (0, 0), "->", shift: 8pt),
  )
]

---

#[
  #set align(center + horizon)

  #figure(
    image("./d78b28e5-0358-4e7e-a32b-fc20106f7547.png", width: 80%)
  )
]

---

#[
  #set align(center + horizon)
  #set text(size: 28pt)

  #diagram(
    spacing: (0em, 1.5em),
    node-stroke: 0.5pt,
    node((0, 0), [ソースコード], shape: rect, fill: box-color-normal, inset: 0.5em, corner-radius: 4pt),
    node((0, 1), [AST], shape: rect, fill: box-color-normal, inset: 0.5em, corner-radius: 4pt),
    node((0, 2), [opcode], shape: rect, fill: box-color-normal, inset: 0.5em, corner-radius: 4pt),
    node((0, 3), [実行結果], shape: rect, fill: box-color-normal, inset: 0.5em, corner-radius: 4pt),
    edge((0, 0), (0, 1), "->"),
    edge((0, 1), (0, 2), "->"),
    edge((0, 2), (0, 3), "->"),
  )
]

---

#[
  #set align(center + horizon)

  #set text(size: 24pt)

  #codly-range(2)
  ```php
  <?php
  echo "Hello, ";
  echo "Odawara!\n";
  ```
    ↓
  ```
  ECHO string("Hello, ")
  ECHO string("Odawara!\n")
  ```
]

---

#[
  #set align(center + horizon)

  #set text(size: 24pt)

  #codly-range(2)
  ```php
  <?php
  echo "Hello, ";
  echo "Odawara!\n";
  ```
    ↓
  #codly(highlights: (
    (line: 1, fill: box-color-highlight2),
    (line: 3, fill: box-color-highlight2),
  ))
  ```
  EXT_STMT
  ECHO string("Hello, ")
  EXT_STMT
  ECHO string("Odawara!\n")
  ```
]

---

#[
  #set text(size: 20pt)

  ```c
  // php-src: Zend/zend_vm_def.h
  ZEND_VM_COLD_HANDLER(ZEND_EXT_STMT) {
      // 予め登録されているハンドラをすべて呼び出す
      zend_llist_apply_with_argument(
          &zend_extensions,
          zend_extension_statement_handler,
          execute_data
      );
  }
  ```
]

---

#[
  #set text(size: 20pt)

  ```c
  // xdebug: xdebug.c
  void xdebug_statement_call(zend_execute_data *frame) {
      if (ステップ実行が有効になっている?) {
          xdebug_debugger_statement_call(...);
      }
  }
  ```
]

---

#[
  #set text(size: 20pt)

  ```c
  // xdebug: src/debugger/debugger.c
  void xdebug_debugger_statement_call(...) {
      if (step out している?) { ... }
      if (step over している?) { ... }
      if (step into している?) { ... }
      if (この行にブレークポイントが設定されている?) { ... }
  }
  ```
]

---

#[
  #set text(size: 20pt)

  ```c
  // xdebug: src/debugger/handler_dbgp.c
  int xdebug_dbgp_breakpoint(...) {
      // レスポンスを生成して IDE へ送信
      send_message(context, response);
      // IDE からの命令を待ち受けるループへ入る
      xdebug_dbgp_cmdloop(...);
  }
  ```
]

---

#[
  #set text(size: 20pt)

  ```c
  // xdebug: src/debugger/handler_dbgp.c
  static int xdebug_dbgp_cmdloop(...) {
      do {
          // IDE からの命令を待ち受け
          option = xdebug_fd_read_line_delim(...);
          // IDE からの命令を実行
          // ret: 1=プログラムの実行再開 / 0=その他の命令
          ret = xdebug_dbgp_parse_option(...);
          if (ret != 1) {
              send_message(context, response);
          }
      } while (ret != 1);
  }
  ```
]

---

#[
  #set align(center + horizon)

  #set text(size: 30pt)

  ここまでのまとめ

  #set text(size: 24pt)

  #align(left)[
    - コンパイル時、各ステートメントの前に `EXT_STMT` が\
      挿入される
    - `EXT_STMT` 実行時に Xdebug のハンドラが呼ばれる
    - IDE へレスポンスを送信し、命令待ちループへ
    - IDE から実行再開命令が来ると、ループを抜けて\
      実行を再開
  ]
]

---

#[
  #set align(center + horizon)

  Xdebug はあくまで\
  PHP の一拡張として\
  実現されている

  #pause→ 自分でも作れるはず
]

---

#[
  #set align(center + horizon)

  ミニ Xdebug を作ろう!
]

---

#[
  #set align(center + horizon)
  #set text(size: 20pt)

  https://github.com/nsfisis/mini-xdebug

  #figure(
    image("./mini-xdebug.png", width: 60%)
  )
]

---

#[
  #set align(center + horizon)

  ご清聴\
  ありがとうございました
]

---

#[
  #set text(size: 20pt)

  参考文献:

  - https://github.com/php/php-src
  - https://github.com/xdebug/xdebug
  - https://xdebug.org/docs/step_debug
  - https://xdebug.org/docs/dbgp
  - https://speakerdeck.com/shin1x1/exploring-how-debugging-works-with-xdebug-and-an-ide
]