Manual:How to debug

From MediaWiki.org
Jump to: navigation, search

Contents

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.

[edit] PHP errors

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

error_reporting( -1 );
ini_set( 'display_errors', 1 );

Or set it in php.ini:

error_reporting = E_ALL | E_STRICT
display_errors = On

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()).

There are several configuration options to aid debugging. The following are all false by default. Enable them by setting them to true in your LocalSettings.php:

  • $wgShowExceptionDetails Enable more details (like a stack trace) to be shown on the "Fatal error" page.
  • $wgDebugToolbar Shows a toolbar on the page with profiling, log messages and more.
  • $wgShowDebug Adds the "log messages" part of wgDebugToolbar as a raw list to the page.

[edit] Turning display_startup_errors on

Some providers turn display_startup_errors off, which hides the errors even if you raise the error_reporting level. Turning it on within the program is too late! Instead you'll have to create a wrapper file around your file. In the case of MediaWiki you can just add this on top of mediawiki/index.php:

--- index.php
    error_reporting( -1 );
    ini_set( 'display_startup_errors', 1);
    ini_set( 'display_errors', 1);

In other environments:

--- myTestFile.php
    error_reporting( -1 );
    ini_set( 'display_startup_errors', 1);
    ini_set( 'display_errors', 1);
    require 'your_file.php' );

[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;

You can also enable backtrace on SQL error by setting $wgShowDBErrorBacktrace:

$wgShowDBErrorBacktrace = true;

[edit] In-depth debugging

[edit] Logging

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

[edit] Setting up a debug log file

To save errors and debugging information 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 writeable by the
 * PHP script runing within your Web server. */
$wgDebugLogFile = "C:/Program Files/Apache Group/htdocs/mediawiki/{$wgSitename}-debug_log.txt";

This file will contain all of the built-in MediaWiki debug information.

Database transaction lifecycle debugging can be enabled for some databases with $wgDebugDBTransactions.

[edit] Creating a custom log file

To create a custom log file that only holds your specific debug statements, use the wfErrorLog() function. This function takes 2 arguments, the text string to log and the path to the log file:

wfErrorLog( "An error occured.\n", '/tmp/my-custom-debug.log' );

[edit] Creating custom log groups

If you're debugging several different components, it may be useful to set up log groups. To create custom log groups, add this to LocalSettings.php:

/**
 * 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. */
$wgDebugLogGroups = array(
        'myextension'     => 'logs/myextension.log',
        'anotherloggroup' => 'logs/another.log',
);

To debug to one of these groups, use a statement like this:

// ...somewhere in your code
if ( $bad ) {
    wfDebugLog( 'myextension', 'Something is not right: ' . print_r( $editpage->mArticle, true ) );
}

These logs will only contain your debug statements (not built-in MediaWiki debug information).

If you have carefully followed the instructions above but nothing gets written to your logging file(s), and if your system is using SELinux, have a look at the logging section on the SELinux page to get around this SELinux context issue.

[edit] Send debug data to an HTML comment in the output

This may occasionally be useful when supporting a non-technical end-user. It's more secure than exposing the debug log file to the web, since the output only contains private data for the current user. But it's not ideal for development use since data is lost on fatal errors and redirects. Use on production sites is not recommended. Debug comments reveal information in page views which could potentially expose security risks.

 $wgDebugComments = true;

[edit] Working live with MediaWiki objects

eval.php is an interactive script to evaluate and interact with MediaWiki objects and functions in a fully initialized environment.

 $ php maintenance/eval.php
 > print wfMessage("Recentchanges")->plain();
 Recent changes

[edit] 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.


MediaWiki version: 1.18

If you are not using profiling, but have a StartProfiler.php file in the MediaWiki root folder, you may receive errors referring to /includes/Profiler.php. Deleting, or renaming, the StartProfiler.php file will resolve this error. The StartProfiler.sample file, also in the MediaWiki root folder, can serve as a template should you enable profiling in the future.


MediaWiki version: 1.8

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:

$wgProfiler['class'] = 'Profiler';

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

// 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;


MediaWiki version: 1.7

In MediaWiki 1.7 and earlier, instead of editing StartProfiler.php, you have to set $wgProfiling to true. 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:

// 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 = '';

[edit] 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:

function doSomething() {
    wfProfileIn( __METHOD__ ); # You can replace __METHOD__ with any string. This will appear in the profile.
    
    # The actual function

    wfProfileOut( __METHOD__ );
}

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 in LocalSettings.php to true or false for different display formats.

[edit] Logging to Database

To log profiling information to a database, set $wgProfileToDatabase = true; in LocalSettings.php. Then either run update.php (since 1.21) and the profiling table will be added or manually apply the file maintenance/archives/patch-profiling.sql (the recommended way to do this is php maintenance/patchSql.php profiling).

$wgProfileCallTree must be set to false.

[edit] 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; 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 to the URL. This feature is not in the standard product, you can enable it by adding this to StartProfiler.php:

if ( array_key_exists( 'forceprofile', $_REQUEST ) ) {
    $wgProfiler['class'] = 'ProfilerSimpleText';
} elseif ( array_key_exists( 'forcetrace', $_REQUEST ) ) {
    $wgProfiler['class'] = 'ProfilerSimpleTrace';
}

[edit] See also


Language: English  • 日本語 • 中文