1--TEST--
2mysqli_debug() - mysqlnd only control strings
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7require_once('connect.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 mysqlnd only test");
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    $memory_funcs = array(
64        '_mysqlnd_ecalloc',
65        '_mysqlnd_emalloc',
66        '_mysqlnd_palloc_free_thd_cache_reference',
67        '_mysqlnd_pecalloc',
68        '_mysqlnd_pefree',
69        '_mysqlnd_pemalloc',
70        '_mysqlnd_perealloc',
71    );
72    $trace_file = sprintf('%s%s%s', sys_get_temp_dir(), DIRECTORY_SEPARATOR, 'mysqli_debug_phpt.trace');
73
74    $trace = try_control_string($link, 't:m:O,' . $trace_file, $trace_file, 10);
75    if (!strstr($trace, 'SELECT * FROM test') && !strstr($trace, 'mysql_real_query'))
76        printf("[015] SELECT query cannot be found in trace. Trace contents seems wrong.\n");
77
78    $lines_trace = explode("\n", $trace);
79    $functions_trace = array();
80    foreach ($lines_trace as $k => $line) {
81        $line = trim($line);
82        if (preg_match("@^[|\s]*>([\w:]+)@ism", $line, $matches)) {
83            $functions_trace[$matches[1]] = $matches[1];
84        }
85    }
86
87    $found = 0;
88    foreach ($memory_funcs as $k => $name)
89        if (isset($functions_trace[$name]))
90            $found++;
91
92    if ($found < 1) {
93        printf("[016] Only %d memory functions have been found, expecting at least %d.\n",
94            $found, 1);
95        var_dump($trace);
96    }
97
98    $trace = try_control_string($link, 't:O,' . $trace_file, $trace_file, 20);
99    if (!strstr($trace, 'SELECT * FROM test') && !strstr($trace, 'mysql_real_query'))
100        printf("[025] SELECT query cannot be found in trace. Trace contents seems wrong.\n");
101
102    $lines_trace = explode("\n", $trace);
103    $functions_trace = array();
104    foreach ($lines_trace as $k => $line) {
105        $line = trim($line);
106        if (preg_match("@^[|\s]*>([\w:]+)@ism", $line, $matches)) {
107            $functions_trace[$matches[1]] = $matches[1];
108        }
109    }
110
111    $found = 0;
112    foreach ($memory_funcs as $k => $name)
113        if (isset($functions_trace[$name]))
114            $found++;
115
116    if ($found > 2) {
117        printf("[026] More than %d memory functions have been recorded, that's strange.\n",
118            $found);
119        var_dump($trace);
120    }
121
122    mysqli_close($link);
123    @unlink($trace_file);
124    print "done!";
125?>
126--CLEAN--
127<?php
128    require_once("clean_table.inc");
129?>
130--EXPECT--
131done!
132