-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugger.module
More file actions
235 lines (214 loc) · 7.67 KB
/
debugger.module
File metadata and controls
235 lines (214 loc) · 7.67 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
* @file
* Drupal debugger analysing the performance issues on the website.
*/
define('D_MIN_TIMER', 0.02);
require_once 'debugger.api.inc'; // Load API functions.
/*
* "A tick is an event that occurs for every N low-level tickable statements executed by the parser within the declare block.
* The value for N is specified using ticks=N within the declare blocks's directive section."
* You can increase this value to increase the speed, but accordingly it's less precisely.
*/
declare(ticks = 2);
/**
* Implements of hook_boot().
*
*/
function debugger_boot() {
if (debugger_enabled()) { // To enable it, go to settings page.
if (drupal_is_cli() && !variable_get('debugger_enable_cli', FALSE)) {
return; // Disabled by default on CLI. Enable this in settings.
}
if (variable_get('debugger_enable_sql_queries', FALSE)) {
variable_set('dev_query', TRUE); // Enable query logging
} else if (variable_get('dev_query', TRUE)) {
variable_set('dev_query', FALSE);
}
// debugger_api_clear_data();
debugger_check_curr_path();
register_shutdown_function('debugger_shutdown'); // hook_exit or shutdown better?
debugger_api_db_register_request(); // Register error handler before Drupal does.
/* change default PHP time limit */
require_once 'debugger.inc'; // include additional functions
debugger_set_time_limit(0);
register_tick_function('debugger_tick'); // ACTIVATE debugger; if crashing, see: http://php.net/register_tick_function
}
}
/**
* Implements of hook_init().
*/
function debugger_init() {
if (debugger_enabled()) { // To enable it, go to settings page.
if (variable_get('debugger_enable_error_handler', TRUE)) {
set_error_handler('debugger_api_drupal_error_handler'); // register error handler again
}
}
}
/**
* Implements of hook_menu().
*/
function debugger_menu() {
/*
$items['admin/settings/debugger'] = array(
'title' => 'Debugger',
'description' => 'Manage Drupal debugger.',
'position' => 'left',
'weight' => 0,
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer debugger'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
*/
$items['admin/config/development/debugger'] = array(
'title' => 'Debugger Config',
'description' => 'Configuration of Drupal debugger.',
'page callback' => 'drupal_get_form',
'page arguments' => array('debugger_admin_form'),
'access arguments' => array('administer debugger'),
'file' => 'debugger.admin.inc',
);
return $items;
}
/**
* Implements of hook_permission().
*/
function debugger_permission() {
return array(
'administer debugger' => array(
'title' => t('Administer Drupal debugger.'),
'description' => t('Perform administration tasks for Drupal debugger module.'),
),
);
}
/**
* Implements of hook_views_api().
*
*/
function debugger_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'debugger'),
);
}
/**
* Implements of hook_exit().
*/
function debugger_exit() {
global $debugger_exit;
$debugger_exit = TRUE; // mark the exit correctly
$fids = debugger_api_db_register_file();
cache_set('debugger-files', $fids); // Store files in cache.
$fncs = debugger_api_db_register_function();
cache_set('debugger-functions', $fncs); // Store functions in cache.
}
/**
* Implements of register_shutdown_function PHP callback
*
*/
function debugger_shutdown() {
global $debugger_exit;
if (!$debugger_exit) { // If hook_exit wasn't executed, check if we are out of memory?
$ini_size = ini_get("memory_limit");
$curr_size = memory_get_usage();
/* convert format size to bytes */
switch (substr($ini_size, -1)) {
case 'M': case 'm': (int)$ini_size *= 1048576; break;
case 'K': case 'k': (int)$ini_size *= 1024; break;
case 'G': case 'g': (int)$ini_size *= 1073741824; break;
}
if ($curr_size+1024*1024 > $ini_size) { // FIXME: FIX for 1G and more
print "Fatal Error: Please increase your PHP memory limit in your php.ini! Disabling...<br>\n";
if (variable_get('debugger_auto_disable', TRUE)) {
variable_set('debugger_enable', FALSE);
}
print "After that please activate Drupal debugger from settings again!<br>\n";
}
}
if (debugger_enabled()) {
@unregister_tick_function('debugger_tick'); // Ignore: unregister_tick_function(): Unable to delete tick function executed at the moment. This happens on exit within tick handler.
if (variable_get('dev_query', TRUE) && !variable_get('debugger_enable_sql_queries', FALSE)) {
variable_set('dev_query', FALSE);
}
debugger_check_curr_path(); // Fix the Apache bug when current directory is changed on shutdown
// module_load_include('inc', 'debugger'); // include additional functions
// debugger_savetrace(); // save backtrace into db
global $queries;
$num_sql = count($queries);
debugger_api_db_finish_request();
}
}
/**
* Helper function to check if debugger should be enabled on the current page
*/
function debugger_enabled() {
static $debugger_enabled = FALSE;
if (!$debugger_enabled) {
$path = !empty($_GET['q']) ? explode('/', $_GET['q']) : '/';
$debugger_enabled = variable_get('debugger_enable', FALSE) && (!($path[0] == 'admin') || variable_get('debugger_enable_admin', FALSE));
}
return $debugger_enabled;
}
/**
* Check for Drupal root changes and fix it
* Note: Working directory of the script can be changed on registered shutdown function under some web servers, e.g. Apache.
*/
function debugger_check_curr_path() {
global $debugger_root_path;
if (!$debugger_root_path) {
$debugger_root_path = getcwd(); // Save current Drupal path, in case if we lose it
} else {
// Note: Working directory of the script can be changed on shutdown under some web servers, e.g. Apache.
$debugger_root_path <> getcwd() ? chdir($debugger_root_path) : NULL; // change it back if change has been detected
}
}
/**
* Implementation of register_tick_function PHP callback
*
* Note: register_tick_function() should not be used with threaded web server modules with PHP 5.2 or lower.
* Note: microtime() - PHP 5.0.0 The get_as_float parameter was added.
* Note: memory_get_peak_usage() - PHP 5.2.1 Compiling with --enable-memory-limit is no longer required for this function to exist. , PHP 5.2.0 real_usage was added.
*/
function debugger_tick($ticks = FALSE) {
static $tick_counter = 0;
static $last_timer = 0, $last_memory = 0; // $debugger_trace
/* CALCULATE RESOURCES FROM THE LAST TIME */
$curr_timer = microtime(TRUE);
$curr_memory = function_exists('memory_get_peak_usage') ? memory_get_peak_usage() : memory_get_usage();
if ($last_timer && $last_memory) {
$time = (float)$curr_timer-$last_timer;
$memory = (float)$curr_memory-$last_memory;
} else {
$time = $memory = 0;
$last_timer = $curr_timer;
$last_memory = $curr_memory;
}
if ($time >= (float)variable_get('debugger_precision_time', D_MIN_TIMER)) {
debugger_api_register_backtrace(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2), $time, $memory, $tick_counter++, 2);
$last_timer = $curr_timer;
$last_memory = $curr_memory;
}
if ($ticks) {
return array($tick_counter);
}
$tick_counter++;
}
/**
* Implements hook_query_TAG_alter().
*
* Makes debugging EntityFieldQuery much easier.
*
* Example usage:
* $q = new EntityFieldQuery;
* $q->entityCondition('entity_type', 'node')
* ->addTag('debug')
* ->execute();
*
* @param QueryAlterableInterface $query
*/
function debugger_query_debug_alter(QueryAlterableInterface $query) {
if ($query->hasTag('debug')) {
dpq($query);
}
}