Manual:How to debug

This page gives a basic introduction to debugging MediaWiki software.

One of the first things you will notice is that "echo" generally does not work; this is part of the general design.

PHP errors
To see PHP errors, add this to the very top of LocalSettings.php:

This will cause PHP errors to be shown on-page. This might make it easier for attackers to find a way into your server, so disable it again when you have found the problem.

Note that fatal PHP errors may happen before the lines above are ever executed, or may prevent them from being shown. Fatal PHP errors are usually logged to Apache's error log &mdash; check the error_log setting in php.ini (or use phpinfo).

You can enable more details (like a stack trace) to be shown for some types of errors:

this is especially useful when debugging errors in the PHP code (as opposed to configuration problems).

Note that E_ALL does not include E_STRICT until PHP6, so if you want to receive notices, you may need to use instead:

You may want to set, in :

SQL errors
To display SQL errors in error messages instead of "(SQL query hidden)", add the following to LocalSettings.php:

Logging
For much greater detail, you need to profile and log errors.

To save SQL errors to a log, add $wgDebugLogFile to the LocalSettings.php file after the invocation of DefaultSettings.php. Change the value to a text file where you want to save the debug trace output.

This file will contain all of the built-in MediaWiki debug information as well as anything you try to log. To create a custom log file that only holds your debug statements, add this to LocalSettings.php.

Then debug to this custom log using a statement like this:

Profiling
To get more detail, you need to enable profiling. Profiling tracks code execution during a page action and reports back the percentage of total code execution that was spent in any specific function. The generated profile only includes functions that have specifically been marked to be profiled.

To enable profiling, you need to modify the StartProfiler.php (See StartProfiler.sample in the MediaWiki root folder for an example). By default the file includes a ProfilerStub which just dumps profiling information. To instead direct this information to a file, edit StartProfiler.php so that it looks like this:

Then you can customize profiling options in LocalSettings.php (not StartProfiler.php):

(be sure to edit beneath the requirement of DefaultSettings.php!)

Common configuration: (both <1.7 and >1.8)

In MediaWiki 1.7 and earlier, instead of editing StartProfiler.php, you have to set $wgProfiling to. This will generate basic page timing information in the file defined by $wgDebugLogFile.

In addition to the settings list above, these additional settings are available:

Advanced profiling
Once you have enabled profiling, you can trace code execution through any function that you want to investigate as a bottleneck by wrapping the function with the following code:

After you've added this information, browse to a page in the wiki. This will generate profiling info in the log file you defined above. Change $wgProfileCallTree</tt> in LocalSettings.php</tt> to true or false for different display formats.

Logging to Database
To log profiling information to a database, first you'll have to create a profiling table in your MediaWiki database the table definition in the file maintenance/archives/patch-profiling.sql (or for SQLite users maintenance/sqlite/archives/patch-profiling.sql). Then set $wgProfileToDatabase = true;</tt> in LocalSettings.php.

$wgProfileCallTree</tt> must be set to false.

Viewing Profile Info
If you log your profiling information to the database, you can view the information in a webpage by browsing to profileinfo.php. You must also set $wgEnableProfileInfo = true;</tt> in AdminSettings.php. Then, after gathering data by browsing wiki pages, visit profileinfo.php to see how much time your profiled code is using and how many times it's being called.

To view profiling information as HTML comments appended to the bottom of a page, just add ?forceprofile=true</tt> to the URL. This feature is defined in StartProfiler.php.

Useful debugging functions

 * wfDebugDieBacktrace - dies with an optional message and prints the call-stack