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