1<?php $this->extends('layout.php', ['title' => 'Generating a backtrace on Windows']) ?>
2
3<?php $this->start('content') ?>
4
5<p>
6<a href="/bugs-generating-backtrace.php">Unix</a> | <strong>Windows</strong>
7</p>
8
9<h1>Generating a backtrace, <u>with</u> a compiler, on Windows</h1>
10
11<p>You'll need to install MS Visual Studio 2008, 2012 or later. You'll also need to</p>
12<ul>
13    <li>either download the debug-pack for your PHP version from <a href="https://windows.php.net/download/">windows.php.net/download</a></li>
14    <li>or compile your own PHP with <code>--enable-dbg-pack</code> or <code>--enable-debug</code></li>
15</ul>
16
17<p>If you downloaded the debug-pack from the snaps site, extract it into your
18PHP directory and be sure to put the PDB files that belong to the extensions
19into your extension directory.</p>
20
21<p>If you compile PHP by your own, you can also use a newer version of MSVC.</p>
22
23<p>When PHP crashes, click <em>Cancel</em> to debug the process. Now MSVC starts
24up in <em>Debug View</em>. If you don't already see the call stack, go into the
25<em>View</em> menu and choose <em>Debug Windows</em> &rarr; <em>Call Stack</em>.</p>
26
27<p>You'll now see something similar to the following lines, this is the backtrace:</p>
28<pre><code>
29_efree(void * 0x00000000) line 286 + 3 bytes
30zif_http_test(int 0, _zval_struct * 0x007bc3b0, _zval_struct * * 0x00000000, _zval_struct * 0x00000000, int 0, void * * * 0x00792cd0) line 1685 + 8 bytes
31zend_do_fcall_common_helper_SPEC(_zend_execute_data * 0x0012fd6c, void * * * 0x00792cd0) line 188 + 95 bytes
32ZEND_DO_FCALL_SPEC_CONST_HANDLER(_zend_execute_data * 0x0012fd6c, void * * * 0x00792cd0) line 1578 + 13 bytes
33execute(_zend_op_array * 0x007bc880, void * * * 0x00792cd0) line 88 + 13 bytes
34zend_eval_string(char * 0x00793bce, _zval_struct * 0x00000000, char * 0x00404588 tring', void * * * 0x00792cd0) line 1056 + 14 bytes
35zend_eval_string_ex(char * 0x00793bce, _zval_struct * 0x00000000, char * 0x00404588 tring', int 1, void * * * 0x00792cd0) line 1090 + 21 bytes
36main(int 3, char * * 0x00793ba8) line 1078 + 23 bytes
37PHP! mainCRTStartup + 227 bytes
38KERNEL32! 77e81af6()
39</code></pre>
40
41<h1>Generating backtrace, <u>without</u> compiler, on Windows</h1>
42
43<p>Generating a backtrace without compiler is usually a two step process:</p>
44<ul>
45<li>Generate a crash dump file (.dmp)</li>
46<li>Analyze the crash dump file to get a stack backtrace</li>
47</ul>
48<p>There are several solutions for either step; in the following we describe one solution each.</p>
49
50<h2>Use ProcDump to generate crash dump files</h2>
51
52<p>Download <a href="https://docs.microsoft.com/en-us/sysinternals/downloads/procdump">ProcDump</a>,
53and register it as the Just-in-Time (AeDebug) debugger:</p>
54<pre><code>procdump -ma -i C:\dumps</code></pre>
55<p>Use any suitable folder to store the crash dump files instead of <code>C:\dumps</code>.
56Whenever a process crashes, a crash dump file will be written to the specified folder.</p>
57
58<h2>Use Debug Diagnostic Tool to analyze the crash dump</h2>
59
60<ul>
61<li>Download and install <a
62href="https://www.microsoft.com/en-us/download/details.aspx?id=58210">Debug Diagnostic Tool</a></li>
63<li><a href="https://windows.php.net/downloads/">Download</a> and unpack the PHP debug pack corresponding to the PHP version you are running</li>
64<li>Start the Debug Diagnostic Tool</li>
65<li>Add the path to the unpacked PHP debug pack as symbol search path in the settings</li>
66<li>Press "add data file" and select the crash dump file formerly generated</li>
67<li>Select "default analysis"</li>
68<li>Press "start analysis"</li>
69</ul>
70<p>After the analysis has finished, the DebugDiag Analysis Report opens in Internet
71Explorer; the relevant part of that report is the stack backtrace, which looks similar
72to the following:</p>
73<p><img src="/images/backtrace-images-win32/backtrace.jpg" alt="Debug report backtrace screenshot"></a></p>
74
75<?php $this->end('content') ?>
76