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