Debug Bar Query Tracer

The Query Tracer plugin backtraces all calls to WPDB::query() and determines a) which plugin caused that database query (it ignores all queries that are a part of normal WordPress activity) and b) the function chain that led to that query.  It then displays that information on an additional panel in the Debug Bar plugin (which is required).

  • Sorpresa428

    Current have this installed on WordPress 3.2.1 and Debug Bar 0.7,  running PHP 5.3.5.  No Query Tracer panel shows up in the debug bar.

    • http://cmorrell.com Chris Morrell

      First, make sure that the query tracer plugin was successfully activated.  If it gave you a fatal error, check your logs and let me know the error so I can figure out what’s going on.  If the plugin is active, make sure your browser window is wide enough to show the Query Tracer tab.  I’ve noticed that the Debug Bar has no mechanism for handling tab overflow, so if the window isn’t wide enough, the tab just won’t show.

      If neither of those ideas work, let me know and I’ll look into it further.

      • Sorpresa428

        No errors on screen or in the logs, and there’s plenty of room for the tab in the debug bar.  I’ve tried uninstalling/deleting and then reinstalling/reactivating, and nothing.  It’s like WordPress doesn’t even recognize that it’s there.  Other features of Debug Bar work fine, but Query Tracer doesn’t show up.

        • Sorpresa428

          I actually just installed it along with Debug Bar in a clean WordPress install, and get the same results.  No Query Tracer tab.  Is there something I have to do besides activating the plugin?  Some value in wp-config.php that needs to be set, maybe?  I’ve been running it with WP_DEBUG and SAVEQUERIES set to true.

          • http://cmorrell.com Chris Morrell

            I just installed on a fresh copy of WordPress with the latest version of Debug Bar and it worked just fine.  Are you testing on a machine that you could give me temporary access to?  Or is it a local install?

          • Sorpresa428

            It’s a local install. Windows 7 running XAMPP.

          • http://cmorrell.com Chris Morrell

            Hmm… I don’t have access to a Windows machine right now.  Let me see what I can do.

          • Sorpresa428

            I did finally get it to work on my local install.  I don’t know if it was entirely related to me being on Windows or not, but this is what I ended up having to do:

            Initially, set_visible was checking to see if the user was currently in the Dashboard/Admin Panel, which was causing it to be hidden in the frontend where Debug bar actually renders.  Instead, I told it to set it to visible if the current user is super admin:

            In Panel.php, changed line 20 to $this->set_visible(is_super_admin());

            Once the tab was visible, I was running into a lot of Undefined index notices.  In Tracer.php, rewrote the getData() function as follows:

            public function getData()
                {
                    // Parse if necessary
                    if (!$this->_parsed) {
                        $pluginsPath = WP_CONTENT_DIR . ‘/plugins/’; // Have to do this due to symlink issue
                        $rawData = $this->_data;
                        $this->_data = array();
                       
                        // Gather data about existing plugins
                        $rootData = array();

                        //make sure the file with the get_plugins function is loaded
                        if(!function_exists(‘get_plugins’)) {
                            require(ABSPATH.’wp-admin/includes/plugin.php’);
                        }
                       
                        foreach (get_plugins() as $filename => $data) {
                            list($root) = explode(‘/’, $filename, 2);
                            $rootData[$root] = array_change_key_case($data);
                        }
                       
                        // Parse each query’s backtrace
                        foreach ($rawData as $query) {
                                   
                            $functionChain = array();
                            foreach ($query['backtrace'] as $call) {
                                // Add to function chain
                                $functionChain[] = (isset($call['class']) ? “{$call['class']}::” : ”) . $call['function'];
                               
                                // We’ve got a plugin
                                if (isset($call['file']) && false !== strpos($call['file'], $pluginsPath)) {
                                    list($root) = explode(‘/’, plugin_basename($call['file']), 2);
                                    $file = array_pop(explode(‘/’, plugin_basename($call['file']), 2));
                                }
                                elseif(isset($call['file'])) {
                                    $file = $call['file'];
                                }
                                else {
                                    $file = ”;
                                }
                               
                                // Make sure the array is set up
                                if (!isset($this->_data[$root])) {
                                    $this->_data[$root] = $rootData[$root];
                                    $this->_data[$root]['backtrace'] = array();
                                }
                               
                                // Make sure the backtrace for this file is set up
                                if (!isset($this->_data[$root]['backtrace'][$file])) {
                                    $this->_data[$root]['backtrace'][$file] = array();
                                }
                               
                                // Save parsed data
                                $this->_data[$root]['backtrace'][$file][] = array(
                                    ‘line’ => isset($call['line']) ? $call['line'] : ”,
                                    ‘query’ => $query['query'],
                                    ‘function_chain’ => array_reverse($functionChain),
                                );
                            }
                        }
                    }

            Still not really sure what the root issue was in my install, but maybe someone will find this useful.  :)

          • Mike Van Winkle

            So the problem appears to be on line 44 of Panel.php … the $Galahad_Query_Tracer_Panel::_renderData($data) expects the $data parameter but as called on line 44 no data is passed. 

            Change line 44 from this: 
            $this->_renderData();
            To:
            $this->_renderData($data);

            And you should be ok. 

            The undefined index: file error isn’t that significant but can be fixed by putting an @ before the $call variable on line 97 of Tracer.php … i.e. …

            line 97: if (false !==
            strpos( @$call['file'], $pluginsPath)) {

            Hope this helps. Mike

  • http://twitter.com/StephenCronin Stephen Cronin

     Doesn’t work for me either in WordPress 3.2.1. Doesn’t show up until I make the is_super_admin change suggested by Sorpresa428 – but then I just get undefined function get_plugins. I have no idea why that should be undefined…

  • http://www.webhostings.in/ Website Hosting

    Thanks for these informative guidelines here.

  • http://www.webhostings.in/ web hosting companies

    I get it clear idea about your topic.Above all the points are explained very clearly.