From a76484d2d1e4bbcf1b6cbcdff9d431cd1d5ca18b Mon Sep 17 00:00:00 2001 From: ryan Date: Wed, 21 Nov 2007 00:14:58 +0000 Subject: [PATCH] Debug backtrace for queries. Props tellyworth. fixes #5218 git-svn-id: http://svn.automattic.com/wordpress/trunk@6342 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/wp-db.php | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/wp-includes/wp-db.php b/wp-includes/wp-db.php index 89cd2b2d1..4aaf6b586 100644 --- a/wp-includes/wp-db.php +++ b/wp-includes/wp-db.php @@ -228,7 +228,7 @@ class wpdb { ++$this->num_queries; if (SAVEQUERIES) - $this->queries[] = array( $query, $this->timer_stop() ); + $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); // If there is an error then take note of it.. if ( mysql_error($this->dbh) ) { @@ -475,6 +475,35 @@ class wpdb { { return ( version_compare(mysql_get_server_info(), '4.1.0', '>=') ); } + + /** + * Get the name of the function that called wpdb. + * @return string the name of the calling function + */ + function get_caller() { + // requires PHP 4.3+ + if ( !is_callable('debug_backtrace') ) + return ''; + + $bt = debug_backtrace(); + $caller = ''; + + foreach ( $bt as $trace ) { + if ( @$trace['class'] == __CLASS__ ) + continue; + elseif ( strtolower(@$trace['function']) == 'call_user_func_array' ) + continue; + elseif ( strtolower(@$trace['function']) == 'apply_filters' ) + continue; + elseif ( strtolower(@$trace['function']) == 'do_action' ) + continue; + + $caller = $trace['function']; + break; + } + return $caller; + } + } if ( ! isset($wpdb) )