1--TEST-- 2mysqli_debug() - all control string options supported by both mysqlnd and libmysql except oOaA 3--EXTENSIONS-- 4mysqli 5--SKIPIF-- 6<?php 7require_once 'skipifconnectfailure.inc'; 8 9if (!function_exists('mysqli_debug')) 10 die("skip: mysqli_debug() not available"); 11 12if (!defined('MYSQLI_DEBUG_TRACE_ENABLED')) 13 die("skip: can't say for sure if mysqli_debug works"); 14 15if (defined('MYSQLI_DEBUG_TRACE_ENABLED') && !MYSQLI_DEBUG_TRACE_ENABLED) 16 die("skip: debug functionality not enabled"); 17?> 18--FILE-- 19<?php 20 require_once 'connect.inc'; 21 require_once 'table.inc'; 22 23 function try_control_string($link, $control_string, $trace_file, $offset) { 24 25 @unlink($trace_file); 26 if (true !== ($tmp = @mysqli_debug($control_string))) { 27 printf("[%03d][control string '%s'] Expecting boolean/true, got %s/%s.\n", 28 $offset + 1, 29 $control_string, 30 gettype($tmp), 31 $tmp); 32 return false; 33 } 34 35 if (!$res = mysqli_query($link, 'SELECT * FROM test')) { 36 printf("[%03d][control string '%s'] [%d] %s.\n", 37 $offset + 2, 38 $control_string, 39 mysqli_errno($link), 40 mysqli_error($link)); 41 return false; 42 } 43 while ($row = mysqli_fetch_assoc($res)) 44 ; 45 mysqli_free_result($res); 46 47 clearstatcache(); 48 if (!file_exists($trace_file)) { 49 printf("[%03d][control string '%s'] Trace file has not been written.\n", 50 $offset + 3, 51 $control_string, 52 gettype($tmp), 53 $tmp); 54 return false; 55 } 56 57 return trim(substr(file_get_contents($trace_file), 0, 100024)); 58 } 59 60 $trace_file = sprintf('%s%s%s', sys_get_temp_dir(), DIRECTORY_SEPARATOR, 'mysqli_debug_phpt.trace'); 61 62 $trace = try_control_string($link, 't:O,' . $trace_file, $trace_file, 10); 63 if (!strstr($trace, 'SELECT * FROM test') && !strstr($trace, 'mysql_real_query')) 64 printf("[015] SELECT query cannot be found in trace. Trace contents seems wrong.\n"); 65 66 // T - gettimeofday() system call, system dependent format 67 // 16:57:03.350734 >mysql_real_query 68 $trace = try_control_string($link, 't:O,' . $trace_file . ':T', $trace_file, 20); 69 if (!preg_match('@^[012]{0,1}[0-9]{1}:[0-5]{0,1}[0-9]{1}:[0-5]{0,1}[0-9]{1}@ismU', $trace)) 70 printf("[025] Timestamp not found. One reason could be that the test is borked and does not recognize the format of the gettimeofday() system call. Check manually (and fix the test, if needed :-)). First characters from trace are '%s'\n", substr($trace, 0, 80)); 71 72 // i - add PID of the current process 73 // currently PHP is not multi-threaded, so it should be safe to test for the PID of this PHP process 74 if (false === ($pid = getmypid())) 75 $pid = "[\d]+"; 76 77 $trace = try_control_string($link, 't:O,' . $trace_file . ':i', $trace_file, 30); 78 if (!preg_match("@^" . $pid . "*@ismU", $trace)) 79 printf("[035] Process ID has not been found, first characters from trace are '%s'\n", substr($trace, 0, 80)); 80 81 // L - line numbers 82 $trace = try_control_string($link, 't:O,' . $trace_file . ':L', $trace_file, 40); 83 if (!preg_match("@^[\d]+@ismU", $trace)) 84 printf("[045] Line numbers have not been found, first characters from trace are '%s'\n", substr($trace, 0, 80)); 85 86 // F - file name 87 $trace = try_control_string($link, 't:O,' . $trace_file . ':F', $trace_file, 50); 88 // hopefully we'll never see a file name that's not covered by this regular expression... 89 if (!preg_match("@^\s*[/\w\\\\d\.:\-]+\.[ch]@ismU", $trace)) 90 printf("[055] File names seem to be missing, first characters from trace are '%s'\n", substr($trace, 0, 80)); 91 92 // -n - print function nesting depth 93 $trace = try_control_string($link, 't:O,' . $trace_file . ':n', $trace_file, 60); 94 if (!preg_match("@^\d+:@ismU", $trace)) 95 printf("[065] Nesting level seem to be missing, first characters from trace are '%s'\n", substr($trace, 0, 80)); 96 97 // -t,[N] - maximum nesting level 98 $trace = try_control_string($link, 't,1:n:O,' . $trace_file, $trace_file, 70); 99 $lines = explode("\n", $trace); 100 foreach ($lines as $k => $line) { 101 $line = trim($line); 102 if (!preg_match("@^(\d+):+@ismU", $line, $matches)) { 103 printf("[075] Nesting level seem to be missing, first characters from trace are '%s'\n", substr($line, 0, 80)); 104 } else { 105 if (!isset($matches[1]) || ((int)$matches[1] > 1)) { 106 printf("[076] Nesting level seem to be %d, should not be higher than 1, first characters from trace are '%s'\n", 107 $matches[1], 108 substr($line, 0, 80)); 109 } 110 } 111 } 112 113 // omitting t 114 $trace = try_control_string($link, 'n:O,' . $trace_file, $trace_file, 80); 115 $lines = explode("\n", $trace); 116 foreach ($lines as $k => $line) { 117 $line = trim($line); 118 if (preg_match("@^[|\s]*>[\w]+@ism", $line, $matches)) { 119 printf("[085] Looks like a function call, but there should be none in the trace file, first characters from trace are '%s'\n", 120 substr($line, 0, 80)); 121 } 122 } 123 124 // -f[,functions] - Limit debugger list to specified functions. Empty list -> all functions 125 $lines_all_funcs = explode("\n", try_control_string($link, 't:O,' . $trace_file, $trace_file, 90)); 126 $functions_all_funcs = array(); 127 foreach ($lines_all_funcs as $k => $line) { 128 $line = trim($line); 129 if (preg_match("@^[|\s]*>([\w:]+)@ism", $line, $matches)) { 130 $functions_all_funcs[$matches[1]] = $matches[1]; 131 } 132 } 133 134 $lines_trace = explode("\n", try_control_string($link, 't:f:O,' . $trace_file, $trace_file, 100)); 135 $functions_trace = array(); 136 foreach ($lines_trace as $k => $line) { 137 $line = trim($line); 138 if (preg_match("@^[|\s]*>([\w:]+)@ism", $line, $matches)) { 139 $functions_trace[$matches[1]] = $matches[1]; 140 } 141 } 142 143 $tmp = array_diff($functions_all_funcs, $functions_trace); 144 if (!empty($tmp)) { 145 printf("[105] Looks like not all functions are listed in the trace. Check manually, dumping diff."); 146 var_dump($tmp); 147 } 148 149 // get two or three function names to play with... 150 $test_functions = array('simple' => array(), 'doubledot' => array()); 151 152 foreach ($functions_all_funcs as $func) { 153 if (count($test_functions['simple']) < 2 && !strstr($func, '::')) 154 $test_functions['simple'][$func] = $func; 155 else if (count($test_functions['doubledot']) < 2 && strstr($func, '::')) 156 $test_functions['doubledot'][$func] = $func; 157 } 158 159 $control_string = ''; 160 if ($func = reset($test_functions['simple'])) 161 $control_string .= sprintf('%s,', $func); 162 if ($func = reset($test_functions['doubledot'])) 163 $control_string .= sprintf('%s,', $func); 164 if ($func = next($test_functions['simple'])) 165 $control_string .= sprintf('%s,', $func); 166 if ($func = next($test_functions['doubledot'])) 167 $control_string .= sprintf('%s,', $func); 168 $control_string = sprintf('t:f,%s:O,%s', $control_string, $trace_file); 169 170 $lines_trace = explode("\n", try_control_string($link, $control_string, $trace_file, 110)); 171 $functions_trace = array(); 172 foreach ($lines_trace as $k => $line) { 173 $line = trim($line); 174 if (preg_match("@^[|\s]*>([\w:]+)@ism", $line, $matches)) { 175 $functions_trace[$matches[1]] = $matches[1]; 176 } 177 } 178 179 foreach ($test_functions['simple'] as $func) 180 if (isset($functions_trace[$func])) { 181 unset($functions_trace[$func]); 182 unset($test_functions['simple'][$func]); 183 } 184 185 foreach ($test_functions['doubledot'] as $func) 186 if (isset($functions_trace[$func])) { 187 unset($functions_trace[$func]); 188 unset($test_functions['doubledot'][$func]); 189 } 190 191 if (!empty($functions_trace)) { 192 printf("[115] Dumping list of unexpected functions which should have not been shown when using control string '%s'.\n", 193 $control_string); 194 var_dump($functions_trace); 195 } 196 $tmp = array_merge($test_functions['doubledot'], $test_functions['simple']); 197 if (!empty($tmp)) { 198 printf("[116] Dumping list of functions which should have been shown when using control string '%s'.\n", 199 $control_string); 200 var_dump($tmp); 201 } 202 203 // m - trace memory allocations 204 $trace = try_control_string($link, 't:O,' . $trace_file . ':m', $trace_file, 120); 205 if (!preg_match("@^[|\s]*>\_mysqlnd_p?efree@ismU", $trace, $matches) && 206 !preg_match("@^[|\s]*>\_mysqlnd_p?emalloc@ismU", $trace, $matches)) { 207 printf("[125] Memory dump does neither contain _mysqlnd_pefree nor _mysqlnd_pemalloc calls - check manually.\n"); 208 var_dump($trace); 209 } 210 211 mysqli_close($link); 212 print "done"; 213 print "libmysql/DBUG package prints some debug info here."; 214 @unlink($trace_file); 215?> 216--CLEAN-- 217<?php 218 require_once 'clean_table.inc'; 219?> 220--EXPECTF-- 221[083][control string 'n:O,%smysqli_debug_phpt.trace'] Trace file has not been written. 222done%s 223