How to debug

From MediaWiki.org

Jump to: navigation, search

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.

Contents

[edit] PHP errors

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

error_reporting(E_ALL);
ini_set("display_errors", 1);

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 — 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:

$wgShowExceptionDetails = true;

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

[edit] SQL errors

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

$wgShowSQLErrors = true;
$wgDebugDumpSql  = true;

[edit] In-depth debugging

[edit] 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. Change the value to a text file where you want to save the debug trace output.

/**
 * The debug log file should be not be publicly accessible if it is used, as it
 * may contain private data.  But it must be in a directory to which PHP run
 * within your Web server can write. */
$wgDebugLogFile  = 'c:/Program Files/Apache Group/htdocs/mediawiki/debug_log.txt';

[edit] Profiling

To add much more detail, you need to enable profiling.


MediaWiki version: 1.7 and earlier

Setting $wgProfiling will give you basic page timing in the file defined by $wgDebugLogFile.

Here there's specific settings that were removed in 1.8, for other settings see below.

/** Enable for more detailed by-function times in debug log */
$wgProfiling    = true;
/** Only profile every n requests when profiling is turned on */
$wgProfileSampleRate = 1;
/** If not empty, specifies profiler type to load */
$wgProfilerType = '';


MediaWiki version: 1.8 and after

To enable profiling, you need to modify the StartProfiler.php file. To enable profiling, you can replace its content by this to use the full profiler.

Then you can customize more things in LocalSettings.php (see below).

require_once(  dirname(__FILE__).'/includes/Profiler.php' );
$wgProfiler = new Profiler;

Common configuration: (both <1.7 and >1.8)

/** Only record profiling info for pages that took longer than this */
$wgProfileLimit = 0.0;
/** Don't put non-profiling info into log file */
$wgProfileOnly = false;
/** Log sums from profiling into "profiling" table in db. */
$wgProfileToDatabase = false;
/** If true, print a raw call tree instead of per-function report */
$wgProfileCallTree = false;
/** Should application server host be put into profiling table */
$wgProfilePerHost = false;
 
/** Settings for UDP profiler */
$wgUDPProfilerHost = '127.0.0.1';
$wgUDPProfilerPort = '3811';
 
/** Detects non-matching wfProfileIn/wfProfileOut calls */
$wgDebugProfiling = false;
/** Output debug message on every wfProfileIn/wfProfileOut */
$wgDebugFunctionEntry = 0;
/** Lots of debugging output from SquidUpdate.php */
$wgDebugSquid = false;

[edit] Tracing execution

Once the above is done, you can call the global function wfDebug() from wherever in the code you like. The function is defined in the includes/GlobalFunctions.php file. For example:

wfDebug("This is just testing the debug tracing stuff\n");

When the code was run, this would be added to the log file:

This is just testing the debug tracing stuff

[edit] Advanced profiling

You can modify StartProfiler.php to load a more advanced profiler. Then, wrap any function that you want to investigate as a bottleneck in the following code:

At the beginning:

$fname = 'class_name_if_applicable::function name'; # Just for display
    wfProfileIn($fname);

At the end:

wfProfileOut($fname);

In the log file you will see profiling info. Change $wgProfileCallTreein LocalSettings.php to true or false for different display formats.

[edit] Logging to Database

To log profiling to a database, first you'll have to create a profiling table in your MediaWiki database using the command in the file maintenance/archives/patch-profiling.sql , and set $wgProfileToDatabase = true; in LocalSettings.php.

Note: $wgProfileCallTree must be set to false.

Now go visit some pages (or let your users do so).

[edit] Viewing Profile Info

The page profileinfo.php in the MediaWiki root prints profile info from the profiling table. To run it, set $wgEnableProfileInfo = true; in AdminSettings.php. Then, after gathering enough data, visit profileinfo.php to see how much time your profiled code is using and how many times it's being called.

[edit] See also

Personal tools