sammela.com header image 2

Add Backtrace to the WiPeD WordPress Debugger

October 7th, 2009 · 2 Comments · WordPress, article

In a previous article (WiPeD: A Useful WordPress Debugger Plugin), I discussed the WiPeD WordPress Debugger, written by Sterling Camden, which you can read more about on the Chiptips Blog.

I’ve been educating myself about the structure of WordPress, and recently I found it useful to add some backtrace capability to WiPeD.

What is Backtrace?

Backtrace is the PHP capability to create an array containing a list of the hierarchical function calls that led to a particular point of execution.

Actually the PHP debug_backtrace function produces an array of arrays. For each function in the call hierarchy, it makes available the line number, file name, and function name. But the format isn’t very use friendly for this application, so the bulk of my backtrace addition is devoted to producing a nice format that fits into the output scheme of the WiPeD debugger.

How Backtrace Affects WiPeD

  1. Logging — the WPD_backtrace is used to perform a backtrace at any point in your code. Just add a WPD_backtrace(); function call where you want the backtrace data logged. As a coder/debugger, this is the only modification to your code you will need to make, in order to use this feature.
  2. Output — the WiPeD output now includes the results of the logged backtrace calls.

Example of Use

Here’s an example of the WPD_backtrace() function used in the main method of the WP object in the classes.php file of WordPress.

1
2
3
4
5
6
7
8
9
10
    function main($query_args = '') {
        WPD_backtrace(); //ssmela      
        $this->init();
        $this->parse_request($query_args);
        $this->send_headers();
        $this->query_posts();
        $this->handle_404();
        $this->register_globals();
        do_action_ref_array('wp', array(&$this));
    }

File and Code Modifications

CSS

I added a css style file — wiped.css. You can change the css in this file to control how the backtrace output looks.

The WPD_backtrace class defines the style of the backtrace output data.

The WPD_tracepoint class is a specialized class to define the style of the output data at the tracepoint.

You can also modify css tags used by the original author (WPD_output, WPD_message, WPD_text, WPD_element, and WPD_array).

Additions to WiPeD.php

Additions to WiPeD.php are marked as “ssmela addition 1″, “ssmela addition 2″, and “ssmela addition 3″; and they are listed below, along with brief explanations. Basically, my additions build on the existing general WiPeD logging and display capability, but they add a more specialized backtrace display capability.

  1. ssmela addition 1 –  Sets up to include the wiped.css file
  2. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    // Start ssmela addition 1
    // Set up to include the wiped.css file
    if (!defined("WP_CONTENT_URL")) define("WP_CONTENT_URL", get_option("siteurl") . "/wp-content");
    if (!defined("WP_PLUGIN_URL"))  define("WP_PLUGIN_URL",  WP_CONTENT_URL        . "/plugins");

    function wiped_head()
    {
      $css_url = WP_PLUGIN_URL . "/wiped/wiped.css";
      if (file_exists(TEMPLATEPATH . "/wiped.css"))
      {
        $css_url = get_bloginfo("template_url") . "/wiped.css";
      }
      echo "\n".'<link rel="stylesheet" href="' . $css_url . '" type="text/css" media="screen" />'."\n";
    }
    // End ssmela Addition 1
  3. ssmela addition 2 –  Call the function that includes additional css file, wiped.css
  4. 1
    2
    3
    4
        // Start ssmela addition 2
        // Call the function that includes wiped.css
        add_action('wp_head', 'wiped_head');
        // End ssmela addition 2
  5. ssmela addition 3 –  Function to add trace data to the WiPeD log
  6. 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
    // Start ssmela addition 3
    // Function to add trace data to the WiPeD log
    function WPD_backtrace()
    {
        $trace_array        = debug_backtrace(true);
        $trace_array_count  = count($trace_array);
       
        $callee_line        = "";
        $callee_file        = "";
        $callee_function    = "";
        $caller_line        = "";
        $caller_file        = "";
       
       
        for($i=0; $i<$trace_array_count; $i++)
        {
            $trace_element = $trace_array[$i];
           
            $callee_line        = $caller_line;
            $callee_file        = $caller_file;
            $callee_function    = $trace_element["function"];
            $caller_line        = $trace_element["line"];
            $caller_file        = $trace_element["file"];
           
            $call_list[] = array(
                                    "&nbsp;",
                                    "Caller file: $caller_file",
                                    "Caller line: $caller_line",
                                    "Callee file: $callee_file",
                                    "Callee line: $callee_line",
                                    "Callee function: $callee_function",
                                    "&nbsp;"
            );
        }
       
        $call_list          = array_reverse($call_list);
        $call_list_count    = count($call_list);
       
        WPD_print("<p class=\"WPD_backtrace\"><code>+++++++++++++++++++++++++++</code></p>\n");
        WPD_print("<p class=\"WPD_backtrace\"><code>|||||||||||||||||||||||||||</code></p>\n");

        for($i=0; $i<$call_list_count; $i++)
        {  
            $call_list_element = $call_list[$i];
           
            foreach($call_list_element as $call_list_element_string)
            {
                if ($i!=($call_list_count-2))
                {
                    WPD_print("<p class=\"WPD_backtrace\">".$call_list_element_string."</p>\n");
                }
                else
                {
                    WPD_print("<p class=\"WPD_tracepoint\">".$call_list_element_string."</p>\n");
                }
            }
        }
       
        WPD_print("<p class=\"WPD_backtrace\"><code>|||||||||||||||||||||||||||</code></p>\n");
        WPD_print("<p class=\"WPD_backtrace\"><code>+++++++++++++++++++++++++++</code></p>\n");
    }
    // End ssmela addition 3

Output

The output looks like the following. Towards the bottom of the output listing, note the red text color, which denotes the point in the code where the trace is actually called. The entries above the red are preceding code points in the call tree. The entries below the red is the actual trace code function, and could be removed in the future.

    +++++++++++++++++++++++++++

    |||||||||||||||||||||||||||

     

    Caller file: /home/sammela/public_html/index.php

    Caller line: 17

    Callee file: /home/sammela/public_html/wp-blog-header.php

    Callee line: 16

    Callee function: require

     

     

    Caller file: /home/sammela/public_html/wp-blog-header.php

    Caller line: 16

    Callee file: /home/sammela/public_html/wp-includes/template-loader.php

    Callee line: 37

    Callee function: require_once

     

     

    Caller file: /home/sammela/public_html/wp-includes/template-loader.php

    Caller line: 37

    Callee file: /home/sammela/public_html/wp-content/themes/Sam-Cutline-1-1.4-2ColumnRight/sandbox.php

    Callee line: 23

    Callee function: include

     

     

    Caller file: /home/sammela/public_html/wp-content/themes/Sam-Cutline-1-1.4-2ColumnRight/sandbox.php

    Caller line: 23

    Callee file:

    Callee line:

    Callee function: ssm_show_trace

     

    |||||||||||||||||||||||||||

    +++++++++++++++++++++++++++

Download WiPeD with BackTrace

Download a copy of WiPeD with BackTrace here.

As stated previously, the original author of this excellent plugin is Sterling Camden. His company is Camden Software Consulting.

Tags:

2 Comments so far ↓

Leave a Comment