1--TEST--
2mysqli_debug() - mysqlnd only control strings
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifemb.inc');
7require_once('skipifconnectfailure.inc');
8require_once('connect.inc');
9
10if (!function_exists('mysqli_debug'))
11 	die("skip mysqli_debug() not available");
12
13if (!defined('MYSQLI_DEBUG_TRACE_ENABLED'))
14	die("skip: can't say for sure if mysqli_debug works");
15
16if (defined('MYSQLI_DEBUG_TRACE_ENABLED') && !MYSQLI_DEBUG_TRACE_ENABLED)
17	die("skip: debug functionality not enabled");
18
19if (!$IS_MYSQLND)
20	die("skip mysqlnd only test");
21?>
22--FILE--
23<?php
24	require_once('connect.inc');;
25	require_once('table.inc');
26
27	function try_control_string($link, $control_string, $trace_file, $offset) {
28
29		@unlink($trace_file);
30		if (true !== ($tmp = @mysqli_debug($control_string))) {
31			printf("[%03d][control string '%s'] Expecting boolean/true, got %s/%s.\n",
32				$offset + 1,
33				$control_string,
34				gettype($tmp),
35				$tmp);
36			return false;
37		}
38
39		if (!$res = mysqli_query($link, 'SELECT * FROM test')) {
40			printf("[%03d][control string '%s'] [%d] %s.\n",
41				$offset + 2,
42				$control_string,
43				mysqli_errno($link),
44				mysqli_error($link));
45			return false;
46		}
47		while ($row = mysqli_fetch_assoc($res))
48			;
49		mysqli_free_result($res);
50
51		clearstatcache();
52		if (!file_exists($trace_file)) {
53			printf("[%03d][control string '%s'] Trace file has not been written.\n",
54				$offset + 3,
55				$control_string,
56				gettype($tmp),
57				$tmp);
58			return false;
59		}
60
61		return trim(substr(file_get_contents($trace_file), 0, 100024));
62	}
63
64	$memory_funcs = array(
65		'_mysqlnd_ecalloc',
66		'_mysqlnd_emalloc',
67		'_mysqlnd_palloc_free_thd_cache_reference',
68		'_mysqlnd_pecalloc',
69		'_mysqlnd_pefree',
70		'_mysqlnd_pemalloc',
71		'_mysqlnd_perealloc',
72	);
73	$trace_file = sprintf('%s%s%s', sys_get_temp_dir(), DIRECTORY_SEPARATOR, 'mysqli_debug_phpt.trace');
74
75	$trace = try_control_string($link, 't:m:O,' . $trace_file, $trace_file, 10);
76	if (!strstr($trace, 'SELECT * FROM test') && !strstr($trace, 'mysql_real_query'))
77		printf("[015] SELECT query cannot be found in trace. Trace contents seems wrong.\n");
78
79	$lines_trace = explode("\n", $trace);
80	$functions_trace = array();
81	foreach ($lines_trace as $k => $line) {
82		$line = trim($line);
83		if (preg_match("@^[|\s]*>([\w:]+)@ism", $line, $matches)) {
84			$functions_trace[$matches[1]] = $matches[1];
85		}
86	}
87
88	$found = 0;
89	foreach ($memory_funcs as $k => $name)
90		if (isset($functions_trace[$name]))
91			$found++;
92
93	if ($found < (count($memory_funcs) - 2))
94		printf("[016] Only %d memory functions have been found, expecting at least %d.\n",
95			$found, count($memory_funcs) - 2);
96
97	$trace = try_control_string($link, 't:O,' . $trace_file, $trace_file, 20);
98	if (!strstr($trace, 'SELECT * FROM test') && !strstr($trace, 'mysql_real_query'))
99		printf("[025] SELECT query cannot be found in trace. Trace contents seems wrong.\n");
100
101	$lines_trace = explode("\n", $trace);
102	$functions_trace = array();
103	foreach ($lines_trace as $k => $line) {
104		$line = trim($line);
105		if (preg_match("@^[|\s]*>([\w:]+)@ism", $line, $matches)) {
106			$functions_trace[$matches[1]] = $matches[1];
107		}
108	}
109
110	$found = 0;
111	foreach ($memory_funcs as $k => $name)
112		if (isset($functions_trace[$name]))
113			$found++;
114
115	if ($found > 2)
116		printf("[026] More than %d memory functions have been recorded, that's strange.\n",
117			$found);
118
119	mysqli_close($link);
120	@unlink($trace_file);
121	print "done!";
122?>
123--CLEAN--
124<?php
125	require_once("clean_table.inc");
126?>
127--EXPECTF--
128done!